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

  1. /* g4list.c  ListBox 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. G4LIST *S4FUNCTION g4list( W4ENTRY *ew, int x, int y, int width, int height,
  11.                            char *ptr, unsigned ptr_len, int do_sort )
  12. {
  13.    G4LIST *la ;
  14.    C4CONTROLITEM control ;
  15.  
  16.    #ifdef S4DEBUG
  17.       if ( ew == 0 )
  18.          e4severe( e4parm, "g4list()", E4_PARM_ZER, (char *) 0 ) ;
  19.    #endif
  20.  
  21.    if ( ew->gw.cw->cb.error_code < 0 )  return 0 ;
  22.  
  23.    control.X     = x ;
  24.    control.Y     = y ;
  25.    control.CX    = width ;
  26.    control.CY    = height ;
  27.    control.Id    = entry4_nextid( ew ) ;
  28.    control.Style = LBS_NOTIFY | WS_VSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD ;
  29.  
  30.    if ( do_sort )  control.Style |= LBS_SORT ;
  31.  
  32.    la = list4_create( ew, &control, 0, -1, -1 ) ;
  33.    if ( la == 0 )  return 0 ;
  34.    g4list_buffer( la, ptr, ptr_len ) ;
  35.  
  36.    return la ;
  37. }
  38.  
  39. int S4FUNCTION g4list_add( G4LIST *list, char *choice, int do_select )
  40. {
  41.    int index ;
  42.  
  43.    #ifdef S4DEBUG
  44.       if ( list == 0 )
  45.          e4severe( e4parm, "g4list_add()", E4_PARM_ZER, (char *) 0 ) ;
  46.    #endif
  47.  
  48.    if ( list->ca.gw.cw->cb.error_code < 0 )  return -1 ;
  49.  
  50.    #ifdef S4DEBUG
  51.       if ( lstrlen( choice ) == 0 )
  52.          return e4error( g4cb( list ), e4parm, "g4list_add()", E4_PARM_ADD, (char *) 0 ) ;
  53.    #endif
  54.  
  55.    if ( do_select == 1 )
  56.    {
  57.       int len = lstrlen( choice ) + 1 ;
  58.       if ( list->select != 0 )  u4free( list->select ) ;
  59.       list->select = (char *) u4alloc( len ) ;
  60.       if ( list->select == 0 )
  61.          return e4error( g4cb( list ), e4memory, "g4list_add()", (char *) 0 ) ;
  62.       u4ncpy( list->select, choice, len ) ;
  63.  
  64.       index = SendMessage( list->ca.gw.hWindow, LB_ADDSTRING, 0, (DWORD)(LPSTR) choice ) ;
  65.       SendMessage( list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
  66.    }
  67.    else
  68.       index = SendMessage( list->ca.gw.hWindow, LB_ADDSTRING, 0, (DWORD)(LPSTR) choice ) ;
  69.  
  70.    list->index = index ;
  71.    if ( index < 0 )
  72.       return e4error( g4cb( list ), e4result, "g4list_add()", E4_RESULT_ADD, (char *) 0 ) ;
  73.    return index ;
  74. }
  75.  
  76. int list4_setselect( W4ENTRY *ew )
  77. {
  78.    G4LIST *list ;
  79.    G4EDIT *edit ;
  80.    G4COMBO *combo ;
  81.    int index ;
  82.    G4CONTROL *ca ;
  83.  
  84.    for ( ca = 0 ; ca = (G4CONTROL *) l4next( &ew->ControlList, ca ) ; )
  85.    {
  86.       if ( ca->area_type == c4list )
  87.       {
  88.          list = (G4LIST *) ca->area ;
  89.          if ( list->select != 0 )
  90.          {
  91.             index = (int) SendMessage( list->ca.gw.hWindow, LB_FINDSTRING,
  92.                                        (WORD) -1, (DWORD) list->select ) ;
  93.             SendMessage( list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
  94.             list4_setselection( list->ca.gw.hWindow, ew ) ;
  95.          }
  96.       }
  97.       if ( ca->area_type == c4edit )
  98.       {
  99.          edit = (G4EDIT *) ca->area ;
  100.          if ( edit->combo != 0 )
  101.          {
  102.             combo = (G4COMBO *) edit->combo ;
  103.             if ( combo->list->select != 0 )
  104.             {
  105.                index = (int) SendMessage( combo->list->ca.gw.hWindow, LB_FINDSTRING,
  106.                                     -1, (DWORD) combo->list->select ) ;
  107.                SendMessage( combo->list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
  108.                combo4_setselection( edit->ca.gw.hWindow, ew, 0 ) ;
  109.             }
  110.          }
  111.       }
  112.    }
  113.    return 0 ;
  114. }
  115.  
  116. void list4_setselection( HWND hwnd, W4ENTRY *ew )
  117. {
  118.    int id = GetWindowWord( hwnd, GWW_ID ) ;
  119.    int index = (int) SendMessage( hwnd, LB_GETCURSEL, 0, 0 ) ;
  120.    G4LIST *list = g4lookup_list( ew, id ) ;
  121.    if ( list->pStr != 0 )  u4free( list->pStr ) ;
  122.    list->pStr = (char *) u4alloc( SendMessage( hwnd, LB_GETTEXTLEN, index, 0 ) + 1 ) ;
  123.    if ( list->pStr == 0 )
  124.    {
  125.       e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
  126.       return ;
  127.    }
  128.    SendMessage( hwnd, LB_GETTEXT, index, (DWORD) list->pStr ) ;
  129. }
  130.  
  131. void S4FUNCTION g4list_buffer( G4LIST *la, char *ptr, unsigned ptr_len )
  132. {
  133.    #ifdef S4DEBUG
  134.       if ( la == 0 )
  135.          e4severe( e4parm, "g4list_buffer()", E4_PARM_ZER, (char *) 0 ) ;
  136.    #endif
  137.  
  138.    la->pResult    = ptr ;
  139.    la->pResultLen = ptr_len ;
  140. }
  141.  
  142. void S4FUNCTION g4list_save( G4LIST *la )
  143. {
  144.    int l ;
  145. /* for now, assume no pictures in the listbox (same with combobox ...)    */
  146.  
  147.    if ( la->pStr == 0 )
  148.    {
  149.       la->pResult[0] = 0 ;
  150.       return ;
  151.    }
  152.    l = lstrlen( la->pStr ) ;
  153.  
  154.    if ( l < la->pResultLen )
  155.    {
  156.       memcpy( la->pResult, la->pStr, l ) ;
  157.       la->pResult[l] = 0 ;
  158.    }
  159.    else
  160.       memcpy( la->pResult, la->pStr, la->pResultLen ) ;
  161. }
  162.  
  163. G4LIST *list4_create( W4ENTRY *ew, C4CONTROLITEM *control, char *text,
  164.                       int low, int high )
  165. {
  166.    G4LIST *la = (G4LIST *) u4alloc( sizeof(G4LIST) ) ;
  167.    if ( la == 0 )
  168.    {
  169.       e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
  170.       return 0 ;
  171.    }
  172.    control4_construct( &la->ca, ew ) ;
  173.    l4add( &ew->ControlList, (void *) &la->ca ) ;
  174.  
  175.    if ( low < 0 )
  176.       la->ca.gw.hWindow = w4create_window( "ListBox", text, control->Style,
  177.                                      control->X, control->Y,
  178.                                      control->CX, control->CY,
  179.                                      ew->gw.hWindow, control->Id,
  180.                                      w4hInst( ew ), 0 ) ;
  181.    else
  182.       la->ca.gw.hWindow = w4create_window( "ListBox", text, control->Style,
  183.                                      (control->X*low)/4, (control->Y*high)/8,
  184.                                      (control->CX*low)/4, (control->CY*high)/8,
  185.                                      ew->gw.hWindow, control->Id,
  186.                                      w4hInst( ew ), 0 ) ;
  187.  
  188.    la->ca.gw.OldWindowProc = (FARPROC) GetWindowLong( la->ca.gw.hWindow,
  189.                                                       GWL_WNDPROC ) ;
  190.    SetWindowLong( la->ca.gw.hWindow, GWL_WNDPROC, (LONG) ew->gw.cw->list4_proc ) ;
  191.             
  192.    la->ca.hParent   = ew->gw.hWindow ;
  193.    la->ca.ew        = ew ;
  194.    la->ca.gw.Id     = control->Id ;
  195.    la->ca.gw.Style  = control->Style | WS_TABSTOP | WS_VISIBLE ;
  196.    la->ca.gw.cw     = ew->gw.cw ;
  197.    la->ca.area_type = c4list ;
  198.    la->ca.area      = (void *) la ;
  199.    la->combo        = 0 ;
  200.  
  201.    return la ;
  202. }
  203.  
  204. int list4_process_message( G4LIST *list, WORD msg, WORD wParam, LONG lParam )
  205. {
  206.    G4COMBO *combo ;
  207.  
  208.    switch( msg )
  209.    {
  210.       case WM_LBUTTONDBLCLK:
  211.       case WM_LBUTTONDOWN:
  212.          if ( list->combo == 0 )
  213.             if ( control4_ok( w4entry( list ), 1 ) != 0 )  return 1 ;
  214.          break ;
  215.    }
  216.  
  217.    if ( list->combo == 0 )
  218.       return control4_process_message( &list->ca, msg, wParam, lParam ) ;
  219.  
  220.    switch( msg )
  221.    {
  222.       case WM_LBUTTONDBLCLK:
  223.       case WM_LBUTTONDOWN:
  224.          if ( control4_ok( w4entry( list->combo ), 1 ) != 0 )  return 1 ;
  225.          break ;
  226.  
  227.       case WM_LBUTTONUP:
  228.          combo4_process_message( (G4COMBO *) list->combo, WM_SYSKEYDOWN, VK_UP, 0x20000000L ) ;   
  229.          break ;
  230.  
  231.       case WM_VSCROLL:
  232.          control4_process_message( &list->ca, msg, wParam, lParam ) ;
  233.          break ;
  234.  
  235.       case WM_SETFOCUS:
  236.          combo = (G4COMBO *)list->combo ;
  237.          SetFocus( combo->edit.ca.gw.hWindow ) ;
  238.          {
  239.             W4ENTRY *ew = w4entry( combo ) ;
  240.             ew->ControlList.selected = (void *) &combo->edit.ca ;
  241.          }
  242.          break ;
  243.  
  244.       case WM_SYSKEYDOWN:
  245.          if ( wParam == VK_UP )
  246.             if ( lParam & 0x20000000L )  /* alt key */
  247.             {
  248.                combo = (G4COMBO *)list->combo ;
  249.                SetFocus( combo->edit.ca.gw.hWindow ) ;
  250.                {
  251.                   W4ENTRY *ew = w4entry( combo ) ;
  252.                   ew->ControlList.selected = (void *) &combo->edit.ca ;
  253.                }
  254.                combo4_process_message( combo, WM_SYSKEYDOWN, VK_UP, 0x20000000L ) ;   
  255.             }
  256.          break ;
  257.    }
  258.    return control4_process_message( &list->ca, msg, wParam, lParam ) ;
  259. }
  260.  
  261. int S4FUNCTION g4list_reset( G4LIST *la )
  262. {
  263.    if ( la->select != 0 )
  264.    {
  265.       u4free( la->select ) ;
  266.       la->select = 0 ;
  267.    }
  268.    SendMessage( g4handle( la ), LB_RESETCONTENT, 0, 0 ) ;
  269.    return 0 ;
  270. }
  271.  
  272. /* for the future, add the void pointer parameter to the call to g4list_add,
  273.    ie., do it all in one step */
  274. /* I am assuming that g4list_ptr_save is called immediately after the call to
  275.    g4list_add, if not, the internal variable 'int index' could be obsolete, or
  276.    be a reference to another element of the listbox */
  277. /* if rc == LB_ERR, e4error if S4DEBUG, return -1 (error) */
  278.  
  279. int S4FUNCTION g4list_ptr_save( G4LIST *la, void *ptr )
  280. {
  281.    return SendMessage( g4handle( la ), LB_SETITEMDATA, (WORD) la->index, (DWORD)(LPSTR) ptr ) ;
  282. }
  283.  
  284. void * S4FUNCTION g4list_ptr( G4LIST *la )
  285. {
  286. /* check the returns for the SendMessage function calls */
  287. DWORD ptr ;
  288.    int index = SendMessage( g4handle( la ), LB_GETCURSEL, 0, 0 ) ;
  289.    if ( index == LB_ERR ) /* no selection */
  290.       return 0 ;
  291.    ptr = SendMessage( g4handle( la ), LB_GETITEMDATA, index, 0 ) ;
  292.  
  293.    return (void *)(char *) ptr ;
  294. }
  295.