/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProCsys.h>
#include <ProDrawing.h>
#include <ProDtlnote.h>
#include <ProDwgtable.h>

/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>


ProError UserTableColorAction (
    ProDwgtable *table,
    ProError    filter_status,
    ProAppData  data)
{
    int                     ncols, nrows, c, r;
    ProError                status;
    ProColor                *p_color = (ProColor*)data;

    status = ProDwgtableRowsCount(table, &nrows);
   	ERROR_CHECK("UserTableColorChange","prodrw_count_table_rows",(nrows<1));
   	if (nrows<1) return (PRO_TK_NOT_VALID);

    status = ProDwgtableColumnsCount(table, &ncols);
   	ERROR_CHECK("UserTableColorChange","ProDwgtableColumnsCount",(ncols<1));
   	if (ncols<1) return (PRO_TK_NOT_VALID);

    /*--------------------------------------------------------*\
        For each row and column in the table...
    \*--------------------------------------------------------*/
    for (r = 1; r <= nrows; r++)
    {
      for (c = 1; c <= ncols; c++)
        {
          ProDtlnote note;
          ProDtlnotedata notedata;
          ProTextStyle dtlNote_text_style;
          /*-----------------------------------------------*\
             Get the identifier of the note in this cell.
          \*-----------------------------------------------*/
          status = ProDwgtableCellNoteGet(table, c, r, &note);
          ERROR_CHECK("UserTableColorChange", "ProDwgtableCellNoteGet",status);

          /*-----------------------------------------------*\
             Get the data structure for the note.
          \*-----------------------------------------------*/
          status = ProDtlnoteDataGet(&note, NULL, PRODISPMODE_NUMERIC, &notedata);
          ERROR_CHECK("UserTableColorChange", "ProDtlnoteDataGet",status);

          /*-----------------------------------------------*\
             Modify its color.
          \*-----------------------------------------------*/
          status = ProDtlnotedataTextStyleGet(notedata, &dtlNote_text_style);
          status = ProTextStyleColorSetWithDef(dtlNote_text_style, &p_color);

          status = ProDtlnoteRemove(&note);
          ERROR_CHECK("UserTableColorChange", "ProDtlnoteRemove",status);

          status = ProDtlnoteModify(&note, NULL, notedata);
          ERROR_CHECK("UserTableColorChange", "ProDtlnoteModify",status);

          status = ProDtlnoteShow(&note);
          ERROR_CHECK("UserTableColorChange", "ProDtlnoteShow",status);
        }
    }

    return PRO_TK_NO_ERROR;
}

/*=================================================================*\
FUNCTION : UserTableColorChange
PURPOSE : Change the color of the notes in a drawing table
\*=================================================================*/
ProError UserTableColorChange ()
{
    ProError        status;
    ProDrawing      p_draw;
    int         	table_id;
    int			win_id;
    int 		color_index;
    ProColor    color;
    int			i_range[2]= {0 , PRO_COLOR_MAX};

    ProFileName WMSGFIL = {'m','s','g','_','u','g','d','w','g','.','t','x','t','\0'};
    /*------------------------------------------------------------*\
      Get the current model
    \*------------------------------------------------------------*/
    status = ProMdlCurrentGet((ProMdl*)&p_draw);
    ERROR_CHECK("UserTableColorChange","ProMdlCurrentGet",status);
    if (status != PRO_TK_NO_ERROR) return (status);

    /*------------------------------------------------------------*\
    Ask the user the color of the notes
    \*------------------------------------------------------------*/
    status = ProMessageDisplay(WMSGFIL, "USER %0s",
                                "Enter the color index : ");
    ERROR_CHECK("UserTableColorChange","ProMessageDisplay",status);

    status = ProMessageIntegerRead(i_range, &color_index);
    ERROR_CHECK("UserTableColorChange","ProMessageIntegerRead",status);
    if (status != PRO_TK_NO_ERROR) return (status);

    color.method = PRO_COLOR_METHOD_TYPE;
    color.value.type = (ProColortype)color_index;

    /*------------------------------------------------------------*\
    For each table in the drawing
    \*------------------------------------------------------------*/
    status = ProDrawingTableVisit(p_draw, (ProDwgtableVisitAction)UserTableColorAction, 
                                  (ProDwgtableFilterAction)NULL, (ProAppData)&color);


    /*------------------------------------------------------------*\
      Update the changes by repainting the window
    \*------------------------------------------------------------*/
    status = ProWindowCurrentGet(&win_id);
    ERROR_CHECK("UserSheetAdd","ProWindowCurrentGet",status);

    status = ProWindowRepaint (win_id);
    ERROR_CHECK("UserSheetAdd","ProWindowRepaint",status);

    return (PRO_TK_NO_ERROR);
}