TomoVision_Tools: Buttons

 

 

 

The Buttons class enable you to draw a rectangular button in the interface.  You can detect a mouse click on these buttons and give a visual feedback by having the button be depressed, or by changing the color of an optional indicator on the button.  The button's graphic can also be an icon or an image.

 

The flags that affect the buttons are:

 

 

OUTIL_INVISIBLE

// no graphics

 

OUTIL_DISABLE

// no select (but graphics OK)

 

OUTIL_VERTICAL

// the name is drawn vertically

 

OUTIL_EMPTY

// no fill

 

OUTIL_OUTLINE

// border as blue outline

 

OUTIL_NO_3D

// flat fill

 

OUTIL_NO_BORDER

 

// do not draw the outside box

 

NAME_V_CENT

NAME_TOP

NAME_BOTTOM

 

// vertical position of the name in the tool

 

NAME_H_CENT

NAME_LEFT

NAME_RIGHT

 

// Horizontal position of the name in the tool

 

TOOL_ICON_V_CENT

TOOL_ICON_TOP

TOOL_ICON_BOTTOM

 

// vertical position of the icon's graphic

 

TOOL_ICON_H_CENT

TOOL_ICON_LEFT

TOOL_ICON_RIGHT

 

// vertical position of the icon's graphic

 

BUTTON_LIGHT

// an indicator light on the left hand side

 

BUTTON_MULTICOLOR

 

 

// the indicator light can have multiple colors depending on the value of the "Light" variable: 0=off, 1=green, 2=blue, 3=yellow, 4=red

 

BUTTON_ITEM

// child of another tool, no outside border is drawn

 

 

The variables that are specific to the buttons class are:

 

 

short          Cur_Value ;

// 0=raised button, not zero=depressed button

 

short          Light ;

// 0=off, not zero=yellow light. If BUTTON_MULTICOLOR then we have more color choices.

          

 

The methods that are specific to the buttons are:

 

 

int          Read( void ) ;

// return the value of the "Cur_Value" variable.

 

An example of the usage of the Buttons class is given in the Samples\Demo Tools\Buttons demo directory.  the resulting interface for this demo look like this:

 

 

The tool_test_buttons_ctrl.cpp file from the Buttons demo

 

 

...

 

// --- Interface tools ---

static          Boxs          box_box ;

 

static          Buttons          but_demo_1 ;

static          Buttons          but_demo_2 ;

static          Buttons          but_demo_3 ;

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Register

//

//          Parameters:          mode (unsigned short *)          Used only for Tools.

//                                                            If this pointer is not NULL,

//                                                            use it to specify to what tool

//                                                            window we belong.

//                                                            SLICEO_TOOL_2D, _BASIC,_3D...

//                                                  

//          Returns:          (int)                    Vertical size of your menu (in pixels)

//

//          This is called once at the start of the program.  We need it to

//          know you are using your own func. and how much space to reserve

//          in the menu interface.  You must also change the "mode" flag to

//          reflect in what tool window you want to appear.

//

//          Note: If you do not need a graphic interface, just comment out

//                the "Ctrl_Register" function.

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Register( unsigned short *mode )

{

// --------------------------------------------------------------------

// In the "Config" menu, the user can change the interface's size.

// This action change a number of variables that are used to compute

// the size of the tool in the intearface.  We now load a few of these

// variables and use them to compute the dimension of our interface box.

// --------------------------------------------------------------------

short          Button_Dim_Big = (short) Fct_Variable_Value( "$INTERFACE_BUTTON_DIM_BIG" ) ;

short          Title_Dim =           (short) Fct_Variable_Value( "$INTERFACE_TITLE_DIM_BIG" ) ;

short          Space_Dim =           (short) Fct_Variable_Value( "$INTERFACE_SPACE_DIM" ) ;

 

          // --- compute the height of the interface ---

          short height = Title_Dim + 2                    // title at top of tool

                         + Button_Dim_Big                    // Button 1

                         + Space_Dim                    // space between button 1 and button 2

                         + Button_Dim_Big                    // Button 2

                         + Space_Dim                    // space between button 2 and button 3

                         + Button_Dim_Big                    // Button 3

                         + Space_Dim ;                    // space at the bottom

 

          return( height ) ;

}

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Create

//

//          Parameters:          wnd (HWND)          Pointer to Window's created window

//                              width (int)          horiz. size of the window

//                              height (int)          vert. size of the window

//          Returns:          (int)                    0 if error

//

//          This is called once when your control window is created

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Create( HWND wnd, int width, int height )

{

char   *Title_Font_Name =   (char *) Fct_Variable_Value( "$INTERFACE_TITLE_FONT_NAME" ) ;

char   *Title_Font_Weight = (char *) Fct_Variable_Value( "$INTERFACE_TITLE_FONT_WEIGHT" ) ;

short          Title_Font_Scale =  (short)  Fct_Variable_Value( "$INTERFACE_TITLE_FONT_SCALE" ) ;

short          Border_Dim =              (short)  Fct_Variable_Value( "$INTERFACE_BORDER_DIM" ) ;

 

          box_box.Name = "------- Tool Test Buttons -------" ;

          box_box.Flag      = OUTIL_DISABLE | OUTIL_NO_3D ;

          box_box.Pos_X     = 1 ;

          box_box.Pos_Y     = 1 ;

          box_box.Dim_X     = width - 2 ;

          box_box.Dim_Y     = height - 2 ;

          box_box.border    = Border_Dim;

          box_box.color.set( WINDOW_COLOR_TOOL_R,

                                 WINDOW_COLOR_TOOL_G,

                                 WINDOW_COLOR_TOOL_B ) ;

          box_box.Font_Name   = Title_Font_Name ;

          box_box.Font_Scale  = Title_Font_Scale ;

          box_box.Font_Weight = Title_Font_Weight ;

          box_box.Init( wnd ) ; 

 

          return( 1 ) ;

}

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Resize

//

//          Parameters:          wnd (HWND)          Pointer to Window's created window

//                              width (int)          horiz. size of the window

//                              height (int)          vert. size of the window

//          Returns:          (int)                    0 if error

//

//          This is called once when your control window is created

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Resize( HWND wnd, int width, int height )

{

short          p_x, p_y, d_x, d_y ;

 

// -----------------------------------------------------------------------

// In the "Config" menu, the user can change the interface's size.

// This action change a number of variables that are used to compute

// the size of the tool in the intearface.  We now load a few of these

// variables and use them to compute the dimension of our interface tools.

// -----------------------------------------------------------------------

char   *Title_Font_Name =   (char *) Fct_Variable_Value( "$INTERFACE_TITLE_FONT_NAME" ) ;

char   *Title_Font_Weight = (char *) Fct_Variable_Value( "$INTERFACE_TITLE_FONT_WEIGHT" ) ;

short          Title_Font_Scale =  (short)  Fct_Variable_Value( "$INTERFACE_TITLE_FONT_SCALE" ) ;

char   *Tools_Font_Name =   (char *) Fct_Variable_Value( "$INTERFACE_TOOL_FONT_NAME" ) ;

char   *Tools_Font_Weigth = (char *) Fct_Variable_Value( "$INTERFACE_TOOL_FONT_WEIGHT" ) ;

short          Tools_Font_Scale =  (short)  Fct_Variable_Value( "$INTERFACE_TOOL_FONT_SCALE" ) ;

 

short          Button_Dim_Big =    (short)  Fct_Variable_Value( "$INTERFACE_BUTTON_DIM_BIG" ) ;

short          Space_Dim =              (short)  Fct_Variable_Value( "$INTERFACE_SPACE_DIM" ) ;

short          Border_Dim =              (short)  Fct_Variable_Value( "$INTERFACE_BORDER_DIM" ) ;

 

 

 

          if ( wnd ) {

              box_box.Pos_X     = 1 ;

              box_box.Pos_Y     = 1 ;

              box_box.Dim_X     = width - 2 ;

              box_box.Dim_Y     = height - 2 ;

              box_box.border    = Border_Dim;

              box_box.Font_Name   = Title_Font_Name ;

              box_box.Font_Scale  = Title_Font_Scale ;

              box_box.Font_Weight = Title_Font_Weight ;

              box_box.Init( wnd ) ; 

          } else {

              wnd = box_box.wnd ;

          }

 

          // ===========================================================================

          // --- Box 1:

          //

          // Default FLAG value (=NULL)

          // ===========================================================================

          p_x = box_box.Pos_X + Space_Dim ;

          p_y = box_box.Pos_Y + Space_Dim ;

          d_x = box_box.Dim_X - 2*Space_Dim ;

          d_y = Button_Dim_Big ;

 

          but_demo_1.Name      = "Demo Button 1" ;

          but_demo_1.Pos_X     = p_x ;

          but_demo_1.Pos_Y     = p_y ;

          but_demo_1.Dim_X     = d_x ;

          but_demo_1.Dim_Y     = d_y ;

          but_demo_1.border    = Border_Dim ;

          but_demo_1.Font_Name   = Tools_Font_Name ;

          but_demo_1.Font_Weight = Tools_Font_Weigth ;

          but_demo_1.Font_Scale  = Tools_Font_Scale ;

          but_demo_1.Init( wnd ) ;

 

          // ===========================================================================

          // --- Box 2

          //

          // disbaled (blue outline only)

          //

          // Note: we use "OUTIL_OUT". It is an or of 3 bits:

          //       OUTIL_DISABLE   (so that the "select" has no effect)

          //           OUTIL_INVISIBLE (the normal graphic is not drawn)

          //           OUTIL_OUTLINE   (blue outlines of the tool are drawn)

          // ===========================================================================

          p_y += d_y + Space_Dim ;

          d_y = Button_Dim_Big ;

 

          but_demo_2.Flag      = OUTIL_OUT ;

          but_demo_2.Name      = "Demo Button 2" ;

          but_demo_2.Pos_X     = p_x ;

          but_demo_2.Pos_Y     = p_y ;

          but_demo_2.Dim_X     = d_x ;

          but_demo_2.Dim_Y     = d_y ;

          but_demo_2.border    = Border_Dim ;

          but_demo_2.Font_Name   = Tools_Font_Name ;

          but_demo_2.Font_Weight = Tools_Font_Weigth ;

          but_demo_2.Font_Scale  = Tools_Font_Scale ;

          but_demo_2.Init( wnd ) ;

 

          // ===========================================================================

          // --- Box 3

          //

          // button with an indicator light

          // ===========================================================================

          p_y += d_y + Space_Dim ;

          d_y = Button_Dim_Big ;

 

          but_demo_3.Flag      = BUTTON_LIGHT | BUTTON_MULTICOLOR;

          but_demo_3.Name      = "Demo Button 3" ;

          but_demo_3.Pos_X     = p_x ;

          but_demo_3.Pos_Y     = p_y ;

          but_demo_3.Dim_X     = d_x ;

          but_demo_3.Dim_Y     = d_y ;

          but_demo_3.border    = Border_Dim ;

          but_demo_3.Font_Name   = Tools_Font_Name ;

          but_demo_3.Font_Weight = Tools_Font_Weigth ;

          but_demo_3.Font_Scale  = Tools_Font_Scale ;

 

          but_demo_3.Init( wnd ) ;

 

          return( 1 ) ;

}

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Draw

//

//          Parameters:          (void)

//          Returns:          (int)                    0 if error

//

//          This is called each time your menu has to be re-drawn

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Draw( HWND wnd )

{

          // --- if the window if off or invible, we can get out ---

          if ( ! Flag_Ctrl_Wnd_On )

              return( 1 ) ;

          if ( ! Flag_Ctrl_Visible )

              return( 1 ) ;

 

          box_box.Draw() ;

 

          // --- We draw each of our 3 boxes ---

          but_demo_1.Draw() ;

          but_demo_2.Draw() ;

          but_demo_3.Draw() ;

 

          return( 1 ) ;

}

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Leave

//

//          Parameters:          (void)

//          Returns:          (int)                    0 if error

//

//          This is called each time the cursor leave your menu

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Leave( void )

{

          // --- if the window if off or invible, we can get out ---

          if ( ! Flag_Ctrl_Wnd_On )

              return( 1 ) ;

          if ( ! Flag_Ctrl_Visible )

              return( 1 ) ;

 

          but_demo_1.Deselect() ;

          but_demo_2.Deselect() ;

          but_demo_3.Deselect() ;

 

          return( 1 ) ;

}

 

// ============================================================================

// ============================================================================

// ============================================================================

// ============================================================================

// ============================================================================

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Click

//

//          Parameters:          wnd (HWND)          Pointer to Window's generating the signal

//                              up_down (int)          state of the mouse buttons

//                              x, y (int)          current cursor position

//          Returns:          (int)                    0 if error

//

//          This is called each time the cursor is inside your menu and the state

//          of the mouse buttons has changed.

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Click( HWND wnd, int up_down, short x, short y )

{

 

          // --- We only refresh the image when we release the button ---

          if ( ! up_down ) {

              return( 0 ) ;

          }

 

          // ------------------------------------------------

          // We received a mouse click, now we try to find in

          // which button the click was.  For this we use the

          // "select" method.  If will tell us if the mouse

          // is inside the button.

          // ------------------------------------------------

          if ( but_demo_1.Select( up_down ) ) {

              Error_Fct( hwnd, ERROR_CODE_REPORT, "Click in button #1" ) ;

              // --- change the state of the button ---

              but_demo_1.Cur_Value = ! but_demo_1.Cur_Value ;

              // update the graphic for the new state ---

              but_demo_1.Adjust() ;

              return( 1 ) ;

          }

 

          // ------------------------------------------------

          // Note: since the button #2 is disabled, the "select"

          //       will always return "0" and we will never

          //       get this click.

          // ------------------------------------------------

          if ( but_demo_2.Select( up_down ) ) {

              Error_Fct( hwnd, ERROR_CODE_REPORT, "Click in button #2" ) ;

              return( 1 ) ;

          }

          // ------------------------------------------------

          if ( but_demo_3.Select( up_down ) ) {

              // --- change the state of the light ---

              // Cycle through: off=0, green=1, blue=2, yellow=3, red=4.

              but_demo_3.Light = (but_demo_3.Light+1) % 5 ;

              // update the graphic for the new state ---

              but_demo_3.Adjust() ;

 

              Error_Fct( hwnd, ERROR_CODE_REPORT, "Click in button #3 (Light=%d)", but_demo_3.Light ) ;

              return( 1 ) ;

          }

 

          return( 1 ) ;

}

 

// ----------------------------------------------------------------------------

//

//          Function:          Ctrl_Motion

//

//          Parameters:          wnd (HWND)          Pointer to Window's generating the signal

//                              up_down (int)          state of the mouse buttons

//                              x, y (int)          current cursor position

//          Returns:          (int)                    0 if error

//

//          This is called each time the cursor is inside your menu and the cursor

//          has moved.

//

// ----------------------------------------------------------------------------

extern "C" __declspec(dllexport) int          Ctrl_Motion( HWND wnd, int up_down, short x, short y )

{

          // ------------------------------------------------

          // The mouse has moved.  By calling "select" we

          // insure that the button will get the "select" color

          // if the cursor is over it.

          //

          // Note: since button #2 has the "OUTIL_DISABLE" bit

          //       set in its flag, the "select" has no

          //       effect on it.

          // ------------------------------------------------

          but_demo_1.Select( up_down ) ;

          but_demo_2.Select( up_down ) ;

          but_demo_3.Select( up_down ) ;

 

          return( 0 ) ;

}

 

...