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

#include <ProToolkit.h>
#include <ProDimension.h>
#include <ProFeature.h>
#include <ProSelection.h>
#include <ProUICheckbutton.h>
#include <ProUIPushbutton.h>
#include <ProUIDialog.h>
#include <ProUIInputpanel.h>
#include <ProUILabel.h>
#include <ProUISlider.h>
#include <PTApplsUnicodeUtils.h>
#include <ProUIOptionmenu.h>
#define  OK     1
#define  CANCEL 0

static ProName msgfil;
int UsrAngleGet();
/*====================================================================*\ 
FUNCTION : UsrModAngle() 
PURPOSE  : Command to modify an angular dimension using a slider 
\*====================================================================*/ 
void UsrModAngle()
 {
     ProError status;
     ProSelection *sel;
     int n_sel, angle;
     double value;
     ProName wname;
     ProCharName name;
     ProCharLine prompt;
     ProDimension dimension;
     ProDimensiontype dtype;

    while(1)
     { 

/*--------------------------------------------------------------------*\ 
        Select a feature or a dimension 
\*--------------------------------------------------------------------*/ 
        status = ProSelect("feature,dimension",1,
                            NULL,NULL,NULL,NULL,&sel,&n_sel);
        if(status != PRO_TK_NO_ERROR || n_sel < 1)
             break;

        ProSelectionModelitemGet(sel[0], &dimension);

/*--------------------------------------------------------------------*\ 
        If it's a feature, display the dimensions 
\*--------------------------------------------------------------------*/ 
        if(dimension.type == PRO_FEATURE)
             ProFeatureParamsDisplay(sel[0], PRO_DIM_PARAM);
        else
         { 

/*--------------------------------------------------------------------*\ 
            Check that it's angular 
\*--------------------------------------------------------------------*/ 
            ProDimensionTypeGet(&dimension, &dtype);
            if(dtype != PRODIMTYPE_ANGLE)
                 continue;

/*--------------------------------------------------------------------*\ 
            Get the current value 
\*--------------------------------------------------------------------*/ 
            ProDimensionValueGet(&dimension, &value);

            angle = (int)value;

/*--------------------------------------------------------------------*\ 
            Get the name and form a prompt string 
\*--------------------------------------------------------------------*/ 
            ProDimensionSymbolGet(&dimension, wname);
            ProWstringToString(name, wname);
            ProTKSprintf(prompt,"Enter the new value of %s",name);

/*--------------------------------------------------------------------*\ 
            Input the new angle using a dialog box with a slider 
\*--------------------------------------------------------------------*/ 
            if(!UsrAngleGet(prompt, &angle))
                 continue;

/*--------------------------------------------------------------------*\ 
            Set the new value and update the dim display 
\*--------------------------------------------------------------------*/ 
            value = (double)angle;
             if(ProDimensionValueSet(&dimension, value) != PRO_TK_NO_ERROR)
             {
               ProMessageDisplay(msgfil,"USER Could not modify dimension");
                 continue;

             }

             ProDimensionDisplayUpdate(&dimension);
         }
     }
 }

/*====================================================================*\ 
FUNCTION : UsrOKAction() 
PURPOSE  : Action function for the OK button 
\*====================================================================*/ 
static void UsrSliderOKAction(
     char *dialog,
     char *component,
     ProAppData data)
 {
     ProUIDialogExit(dialog, OK);
 } 

/*====================================================================*\ 
FUNCTION : UsrCancelAction() 
PURPOSE  : Action function for the CANCEL button 
\*====================================================================*/ 
static void UsrSliderCancelAction(
     char *dialog,
     char *component,
     ProAppData data)
 {
     ProUIDialogExit(dialog, CANCEL);
 } 


/*====================================================================*\ 
FUNCTION : UsrSliderAction() PURPOSE  : Callback for movement of the 
slider 
\*====================================================================*/ 
void UsrSliderAction(
     char *dialog,
     char *component,
     ProAppData data)
 {
     ProName wstr;
     ProCharName str;
     int *angle = (int*)data;

/*--------------------------------------------------------------------*\ 
    Get the new value 
\*--------------------------------------------------------------------*/ 
    ProUISliderIntegerGet(dialog,component,angle);

/*--------------------------------------------------------------------*\ 
    Set the input panel to the same value 
\*--------------------------------------------------------------------*/ 
    ProTKSprintf(str,"%d",*angle);
     ProStringToWstring(wstr, str);
     ProUIInputpanelValueSet(dialog,"InputPanel",wstr);
 } 

/*====================================================================*\ 
FUNCTION : UsrInputpanelAction() 
PURPOSE  : Callback for input to the input panel 
\*====================================================================*/ 
void UsrInputpanelAction(
     char *dialog,
     char *component,
     ProAppData data)
 {
     wchar_t *wstr;
     ProCharName str;
     int *angle = (int*)data;

/*--------------------------------------------------------------------*\ 
    Get the current value as an integer 
\*--------------------------------------------------------------------*/ 
    ProUIInputpanelValueGet(dialog,component,&wstr);
     ProWstringToString(str, wstr);
     *angle = atoi(str);

/*--------------------------------------------------------------------*\ 
    Set the slider to the same value 
\*--------------------------------------------------------------------*/ 
    ProUISliderIntegerSet(dialog,"Slider",*angle);
 }

/*====================================================================*\ 
FUNCTION : UsrAngleGet() 
PURPOSE  : Input an angle using a dialog with a slider 
\*====================================================================*/ 
int UsrAngleGet(
     char *text, /* Text of the prompt displayed on the dialog */
     int *angle)
 {
     int status;
     ProName wstr;
     ProCharName str;
     ProLine wline;

/*--------------------------------------------------------------------*\ 
    Load the dialog from the resource file 
\*--------------------------------------------------------------------*/ 
    ProUIDialogCreate("angle","angle");

/*--------------------------------------------------------------------*\ 
    Set the prompt as the text of the Label component 
\*--------------------------------------------------------------------*/ 
    ProStringToWstring(wline,text);
     ProUILabelTextSet("angle","Prompt",wline);

/*--------------------------------------------------------------------*\ 
    Set the OK and Cancel callbacks 
\*--------------------------------------------------------------------*/ 
    ProUIPushbuttonActivateActionSet("angle","OK",
				     UsrSliderOKAction,NULL);
    ProUIPushbuttonActivateActionSet("angle","Cancel",
				     UsrSliderCancelAction,NULL);

/*--------------------------------------------------------------------*\ 
    Set the slider action and initial value 
\*--------------------------------------------------------------------*/ 
    ProUISliderUpdateActionSet("angle","Slider",UsrSliderAction, angle);
    ProUISliderIntegerSet("angle","Slider",*angle);

/*--------------------------------------------------------------------*\ 
    Set the input panel action and initial value 
\*--------------------------------------------------------------------*/ 
    ProUIInputpanelActivateActionSet("angle","InputPanel",
                                             UsrInputpanelAction,angle);
     ProTKSprintf(str,"%d",*angle);
     ProStringToWstring(wstr, str);
     ProUIInputpanelValueSet("angle","InputPanel",wstr);

/*--------------------------------------------------------------------*\ 
    Display and activate the dialog 
\*--------------------------------------------------------------------*/ 
    ProUIDialogActivate("angle",&status);

/*--------------------------------------------------------------------*\ 
    Dispose of the dialog 
\*--------------------------------------------------------------------*/ 
    ProUIDialogDestroy("angle");

    return(status==OK?1:0);
 }