home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c163 / 3.ddi / SCRE_SOU.EXE / G4BUTTON.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-29  |  7.8 KB  |  257 lines

  1. /* g4button.c  Button Area Routines  (c)Copyright Sequiter Software Inc., 1991.  All rights reserved. */
  2.  
  3. #include "c4window.h"
  4. #include "e4error.h"
  5.  
  6. #ifdef __TURBOC__
  7.    #pragma hdrstop
  8. #endif
  9.  
  10. int button4_process_message( G4BUTTON *button, WORD message, WORD wParam,
  11.                                                              LONG lParam )
  12. {
  13.    switch( message )
  14.    {
  15.       case WM_CHAR:
  16.          switch( wParam )
  17.          {
  18.             case VK_SPACE:
  19.                return button4_process_message( button, WM_LBUTTONUP, 0, 0 ) ;
  20.  
  21.             case VK_RETURN:
  22.                if ( (button->ButtonType == BS_PUSHBUTTON) ||
  23.                     (button->ButtonType == BS_DEFPUSHBUTTON) )
  24.                   return button4_process_message( button, WM_LBUTTONUP, 0, 0 ) ;
  25.                else
  26.                {
  27.                   G4CONTROL *ca1 ;
  28.                   L4LIST *list = &button->ca.ew->ControlList ;
  29.                   for ( ca1 = 0 ; ca1 = (G4CONTROL *) l4next( list, ca1 ) ; )
  30.                      if ( ca1->is_default == 1 )
  31.                         return button4_process_message( (G4BUTTON *) ca1->area, WM_CHAR, VK_RETURN, 0 ) ;
  32.                   return 1 ;
  33.                }
  34.          }
  35.          break ;
  36.  
  37.       case WM_LBUTTONUP:
  38.       {
  39.          char text[7] ;
  40.          GetWindowText( g4handle( button ), text, 7 ) ;
  41.          text[6] = 0 ;
  42.          if ( memcmp( AnsiUpper( text ), AnsiUpper( E4_MESSAG_CAN ), 6 ) != 0 )
  43.             if ( control4_ok( button->ca.ew, 1 ) != 0 )  return 1 ;
  44.  
  45.          if ( ! u4ptr_equal( (void *) 0, (void *) button->routine ) )
  46.          {
  47.             int rc = (*button->routine)( button ) ;
  48.             if ( rc == c4cancel )
  49.             {
  50.                control4_process_message( &button->ca, WM_CHAR, VK_ESCAPE, 0 ) ;
  51.                return 0 ;
  52.             }
  53.             if ( rc != 0 )
  54.             {
  55.                control4_process_message( &button->ca, WM_CHAR, VK_RETURN, 0 ) ;
  56.                if ( button->ca.ew->return_code == VK_RETURN )
  57.                   button->ca.ew->return_code = rc ;
  58.             }
  59.             return 0 ;
  60.      }
  61.       }
  62.       break ;
  63.  
  64.       case WM_LBUTTONDOWN:
  65.          if ( control4_ok( button->ca.ew, 1 ) != 0 )  return 1 ;
  66.          break ;
  67.    }
  68.    return control4_process_message( &button->ca, message, wParam, lParam ) ;
  69. }
  70.  
  71. void button4_construct( G4BUTTON *button, W4ENTRY *ew )
  72. {
  73.    control4_construct( &button->ca, ew ) ;
  74.  
  75.    button->ca.gw.OldWindowProc = 0 ;
  76.    button->ca.gw.Id            = 0 ;
  77.    button->ca.hParent          = 0 ;
  78.    button->ca.area_type        = c4button ;
  79.    button->ca.area             = (void *) button ;
  80.  
  81.    button->ButtonType   = -1 ;
  82.    button->routine      =  0 ;
  83.    button->routine_data =  0 ;
  84.    button->isChecked    =  0 ;
  85.    button->internal     =  0 ;
  86. }
  87.  
  88. G4BUTTON *button4_create( W4ENTRY *ew, C4CONTROLITEM *control, char *text,
  89.                           int low, int high )
  90. {
  91.    char *txt ;
  92.    G4BUTTON *ba ;
  93.    BYTE low_byte ;
  94.  
  95.    int len = lstrlen( text ) ;
  96.    txt = (char *) u4alloc( len+1 ) ;
  97.    if ( txt == 0 )
  98.    {
  99.       e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
  100.       return 0 ;
  101.    }
  102.    u4ncpy( txt, text, len+1 ) ;
  103.  
  104.    ba = (G4BUTTON *) u4alloc( sizeof(G4BUTTON) ) ;
  105.    if ( ba == 0 )
  106.    {
  107.       e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
  108.       u4free( txt ) ;
  109.       return 0 ;
  110.    }
  111.    button4_construct( ba, ew ) ;
  112.    l4add( &ew->ControlList, (void *) &ba->ca ) ;
  113.  
  114.    if ( LOBYTE( LOWORD( control->Style )) == BS_GROUPBOX )
  115.       ba->ca.gw.Style = control->Style | WS_VISIBLE ;
  116.    else
  117.       ba->ca.gw.Style = control->Style | WS_VISIBLE | WS_TABSTOP ;
  118.  
  119.    if ( low < 0 )
  120.       ba->ca.gw.hWindow = w4create_window( "Button", text, control->Style,
  121.                                      control->X, control->Y,
  122.                                      control->CX, control->CY,
  123.                                      w4handle( ew ), control->Id, w4hInst( ew ), 0 ) ;
  124.    else
  125.       ba->ca.gw.hWindow = w4create_window( "Button", text, control->Style,
  126.                                      (control->X*low)/4, (control->Y*high)/8,
  127.                                      (control->CX*low)/4, (control->CY*high)/8,
  128.                                      w4handle( ew ), control->Id, w4hInst( ew ), 0 ) ;
  129.  
  130.    ba->ca.gw.OldWindowProc = (FARPROC) GetWindowLong( ba->ca.gw.hWindow,
  131.                                                       GWL_WNDPROC ) ;
  132.    SetWindowLong( ba->ca.gw.hWindow, GWL_WNDPROC, (LONG) ew->gw.cw->button4_proc ) ;
  133.             
  134.    txt = AnsiUpper( txt ) ;
  135.    if ( memcmp( txt, E4_MESSAG_OK, 2 ) == 0 )
  136.    {
  137.       g4button_function_work( ba, (S4BUTTON *) button4_okproc, (void *) 0 ) ;
  138.       ba->internal = 1 ;
  139.       ba->ButtonType = BS_PUSHBUTTON ;
  140.    }
  141.    if ( memcmp( txt, AnsiUpper( E4_MESSAG_CAN ), 6 ) == 0 )
  142.    {
  143.       g4button_function_work( ba, (S4BUTTON *) button4_cancelproc, (void *) 0 ) ;
  144.       ba->internal = 1 ;
  145.       ba->ButtonType = BS_PUSHBUTTON ;
  146.    }
  147.    u4free( txt ) ;
  148.  
  149.    low_byte = LOBYTE( LOWORD( control->Style )) ;
  150.    if ( low_byte == BS_PUSHBUTTON ) 
  151.       ba->ButtonType = BS_PUSHBUTTON ;
  152.  
  153.    if ( low_byte == BS_DEFPUSHBUTTON )
  154.    {
  155.       ba->ca.is_default = 1 ;
  156.       ba->ButtonType = BS_DEFPUSHBUTTON ;
  157.    }
  158.  
  159.    ba->ca.hParent = ew->gw.hWindow ;
  160.    ba->ca.ew      = ew ;
  161.    ba->ca.gw.Id   = control->Id ;
  162.    ba->ca.gw.cw   = ew->gw.cw ;
  163.  
  164.    return ba ;
  165. }
  166.  
  167. G4BUTTON *S4FUNCTION g4button_work( W4ENTRY *ew, long style, int x, int y, int width,
  168.                 int height, char *text, S4BUTTON *routine, void *routine_data )
  169. {
  170.    G4BUTTON *button ;
  171.    C4CONTROLITEM control ;
  172.  
  173.    #ifdef S4DEBUG
  174.       if ( ew == 0 )
  175.          e4severe( e4parm, "g4button()", E4_PARM_ZER, (char *) 0 ) ;
  176.    #endif
  177.  
  178.    if ( ew->gw.cw->cb.error_code < 0 )  return 0 ;
  179.  
  180.    control.X  = x ;
  181.    control.Y  = y ;
  182.    control.CX = width ;
  183.    control.CY = height ;
  184.  
  185.    if ( LOBYTE( LOWORD( style )) == BS_GROUPBOX ) 
  186.       control.Style = style | WS_CHILD | WS_VISIBLE ;
  187.    else
  188.       control.Style = style | WS_TABSTOP | WS_CHILD | WS_VISIBLE ;
  189.  
  190.    control.Id = entry4_nextid( ew ) ;
  191.  
  192.    button = button4_create( ew, &control, text, -1, -1 ) ;
  193.    if ( button == 0 )  return 0 ;
  194.    g4button_function_work( button, routine, routine_data ) ;
  195.  
  196.    return button ;
  197. }
  198.  
  199. G4BUTTON *S4FUNCTION g4button_ok( W4ENTRY *ew, int x, int y )
  200. {
  201.    return g4button_work( ew, BS_DEFPUSHBUTTON, x, y, 100, 24, E4_MESSAG_OK,
  202.                          (S4BUTTON *) button4_okproc, (void *) 0 ) ;
  203. }
  204.  
  205. G4BUTTON *S4FUNCTION g4button_cancel( W4ENTRY *ew, int x, int y )
  206. {
  207.    return g4button_work( ew, BS_PUSHBUTTON, x, y, 100, 24, E4_MESSAG_CAN, 
  208.                          (S4BUTTON *) button4_cancelproc, (void *) 0 ) ;
  209. }
  210.  
  211. int S4CALL button4_okproc( G4BUTTON *button )
  212. {
  213.    return c4return ;
  214. }
  215.  
  216. int S4CALL button4_cancelproc( G4BUTTON *button )
  217. {
  218.    return c4cancel ;
  219. }
  220.  
  221. void S4FUNCTION g4button_function_work( G4BUTTON *button, S4BUTTON *function,
  222.                                         void *function_data )
  223. {
  224.    #ifdef S4DLL
  225.       if ( button->internal == 0 )
  226.       {
  227.          if ( ! u4ptr_equal( (void *) 0, (void *) button->routine ))
  228.             FreeProcInstance( (FARPROC) button->routine ) ;
  229.       }
  230.       else
  231.          button->internal=0 ;
  232.    #endif
  233.  
  234.    button->routine      = function ;
  235.    button->routine_data = function_data ; 
  236. }
  237.  
  238. void *S4FUNCTION g4button_data( G4BUTTON *button )
  239. {
  240.    return (void *) button->routine_data ;
  241. }
  242.  
  243. void S4FUNCTION g4button_save( G4BUTTON *button )
  244. {
  245.    if ( button->isChecked != 0 )
  246.    {
  247.       int rc = SendMessage( g4handle( button ), BM_GETCHECK, 0, 0 ) ;
  248.       *button->isChecked = ( rc == 0 ) ? 0 : 1 ;
  249.    }
  250. }
  251.  
  252. void S4FUNCTION g4button_buffer( G4BUTTON *button, int *checked )
  253. {
  254.    SendMessage( button->ca.gw.hWindow, BM_SETCHECK, *checked, 0L ) ;
  255.    button->isChecked = checked ;
  256. }
  257.