home *** CD-ROM | disk | FTP | other *** search
- /* g4list.c ListBox Area Routines (c)Copyright Sequiter Software Inc., 1991. All rights reserved. */
-
- #include "c4window.h"
- #include "e4error.h"
-
- #ifdef __TURBOC__
- #pragma hdrstop
- #endif
-
- G4LIST *S4FUNCTION g4list( W4ENTRY *ew, int x, int y, int width, int height,
- char *ptr, unsigned ptr_len, int do_sort )
- {
- G4LIST *la ;
- C4CONTROLITEM control ;
-
- #ifdef S4DEBUG
- if ( ew == 0 )
- e4severe( e4parm, "g4list()", E4_PARM_ZER, (char *) 0 ) ;
- #endif
-
- if ( ew->gw.cw->cb.error_code < 0 ) return 0 ;
-
- control.X = x ;
- control.Y = y ;
- control.CX = width ;
- control.CY = height ;
- control.Id = entry4_nextid( ew ) ;
- control.Style = LBS_NOTIFY | WS_VSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD ;
-
- if ( do_sort ) control.Style |= LBS_SORT ;
-
- la = list4_create( ew, &control, 0, -1, -1 ) ;
- if ( la == 0 ) return 0 ;
- g4list_buffer( la, ptr, ptr_len ) ;
-
- return la ;
- }
-
- int S4FUNCTION g4list_add( G4LIST *list, char *choice, int do_select )
- {
- int index ;
-
- #ifdef S4DEBUG
- if ( list == 0 )
- e4severe( e4parm, "g4list_add()", E4_PARM_ZER, (char *) 0 ) ;
- #endif
-
- if ( list->ca.gw.cw->cb.error_code < 0 ) return -1 ;
-
- #ifdef S4DEBUG
- if ( lstrlen( choice ) == 0 )
- return e4error( g4cb( list ), e4parm, "g4list_add()", E4_PARM_ADD, (char *) 0 ) ;
- #endif
-
- if ( do_select == 1 )
- {
- int len = lstrlen( choice ) + 1 ;
- if ( list->select != 0 ) u4free( list->select ) ;
- list->select = (char *) u4alloc( len ) ;
- if ( list->select == 0 )
- return e4error( g4cb( list ), e4memory, "g4list_add()", (char *) 0 ) ;
- u4ncpy( list->select, choice, len ) ;
-
- index = SendMessage( list->ca.gw.hWindow, LB_ADDSTRING, 0, (DWORD)(LPSTR) choice ) ;
- SendMessage( list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
- }
- else
- index = SendMessage( list->ca.gw.hWindow, LB_ADDSTRING, 0, (DWORD)(LPSTR) choice ) ;
-
- list->index = index ;
- if ( index < 0 )
- return e4error( g4cb( list ), e4result, "g4list_add()", E4_RESULT_ADD, (char *) 0 ) ;
- return index ;
- }
-
- int list4_setselect( W4ENTRY *ew )
- {
- G4LIST *list ;
- G4EDIT *edit ;
- G4COMBO *combo ;
- int index ;
- G4CONTROL *ca ;
-
- for ( ca = 0 ; ca = (G4CONTROL *) l4next( &ew->ControlList, ca ) ; )
- {
- if ( ca->area_type == c4list )
- {
- list = (G4LIST *) ca->area ;
- if ( list->select != 0 )
- {
- index = (int) SendMessage( list->ca.gw.hWindow, LB_FINDSTRING,
- (WORD) -1, (DWORD) list->select ) ;
- SendMessage( list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
- list4_setselection( list->ca.gw.hWindow, ew ) ;
- }
- }
- if ( ca->area_type == c4edit )
- {
- edit = (G4EDIT *) ca->area ;
- if ( edit->combo != 0 )
- {
- combo = (G4COMBO *) edit->combo ;
- if ( combo->list->select != 0 )
- {
- index = (int) SendMessage( combo->list->ca.gw.hWindow, LB_FINDSTRING,
- -1, (DWORD) combo->list->select ) ;
- SendMessage( combo->list->ca.gw.hWindow, LB_SETCURSEL, index, 0 ) ;
- combo4_setselection( edit->ca.gw.hWindow, ew, 0 ) ;
- }
- }
- }
- }
- return 0 ;
- }
-
- void list4_setselection( HWND hwnd, W4ENTRY *ew )
- {
- int id = GetWindowWord( hwnd, GWW_ID ) ;
- int index = (int) SendMessage( hwnd, LB_GETCURSEL, 0, 0 ) ;
- G4LIST *list = g4lookup_list( ew, id ) ;
- if ( list->pStr != 0 ) u4free( list->pStr ) ;
- list->pStr = (char *) u4alloc( SendMessage( hwnd, LB_GETTEXTLEN, index, 0 ) + 1 ) ;
- if ( list->pStr == 0 )
- {
- e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
- return ;
- }
- SendMessage( hwnd, LB_GETTEXT, index, (DWORD) list->pStr ) ;
- }
-
- void S4FUNCTION g4list_buffer( G4LIST *la, char *ptr, unsigned ptr_len )
- {
- #ifdef S4DEBUG
- if ( la == 0 )
- e4severe( e4parm, "g4list_buffer()", E4_PARM_ZER, (char *) 0 ) ;
- #endif
-
- la->pResult = ptr ;
- la->pResultLen = ptr_len ;
- }
-
- void S4FUNCTION g4list_save( G4LIST *la )
- {
- int l ;
- /* for now, assume no pictures in the listbox (same with combobox ...) */
-
- if ( la->pStr == 0 )
- {
- la->pResult[0] = 0 ;
- return ;
- }
- l = lstrlen( la->pStr ) ;
-
- if ( l < la->pResultLen )
- {
- memcpy( la->pResult, la->pStr, l ) ;
- la->pResult[l] = 0 ;
- }
- else
- memcpy( la->pResult, la->pStr, la->pResultLen ) ;
- }
-
- G4LIST *list4_create( W4ENTRY *ew, C4CONTROLITEM *control, char *text,
- int low, int high )
- {
- G4LIST *la = (G4LIST *) u4alloc( sizeof(G4LIST) ) ;
- if ( la == 0 )
- {
- e4error( w4cb( ew ), e4memory, (char *) 0 ) ;
- return 0 ;
- }
- control4_construct( &la->ca, ew ) ;
- l4add( &ew->ControlList, (void *) &la->ca ) ;
-
- if ( low < 0 )
- la->ca.gw.hWindow = w4create_window( "ListBox", text, control->Style,
- control->X, control->Y,
- control->CX, control->CY,
- ew->gw.hWindow, control->Id,
- w4hInst( ew ), 0 ) ;
- else
- la->ca.gw.hWindow = w4create_window( "ListBox", text, control->Style,
- (control->X*low)/4, (control->Y*high)/8,
- (control->CX*low)/4, (control->CY*high)/8,
- ew->gw.hWindow, control->Id,
- w4hInst( ew ), 0 ) ;
-
- la->ca.gw.OldWindowProc = (FARPROC) GetWindowLong( la->ca.gw.hWindow,
- GWL_WNDPROC ) ;
- SetWindowLong( la->ca.gw.hWindow, GWL_WNDPROC, (LONG) ew->gw.cw->list4_proc ) ;
-
- la->ca.hParent = ew->gw.hWindow ;
- la->ca.ew = ew ;
- la->ca.gw.Id = control->Id ;
- la->ca.gw.Style = control->Style | WS_TABSTOP | WS_VISIBLE ;
- la->ca.gw.cw = ew->gw.cw ;
- la->ca.area_type = c4list ;
- la->ca.area = (void *) la ;
- la->combo = 0 ;
-
- return la ;
- }
-
- int list4_process_message( G4LIST *list, WORD msg, WORD wParam, LONG lParam )
- {
- G4COMBO *combo ;
-
- switch( msg )
- {
- case WM_LBUTTONDBLCLK:
- case WM_LBUTTONDOWN:
- if ( list->combo == 0 )
- if ( control4_ok( w4entry( list ), 1 ) != 0 ) return 1 ;
- break ;
- }
-
- if ( list->combo == 0 )
- return control4_process_message( &list->ca, msg, wParam, lParam ) ;
-
- switch( msg )
- {
- case WM_LBUTTONDBLCLK:
- case WM_LBUTTONDOWN:
- if ( control4_ok( w4entry( list->combo ), 1 ) != 0 ) return 1 ;
- break ;
-
- case WM_LBUTTONUP:
- combo4_process_message( (G4COMBO *) list->combo, WM_SYSKEYDOWN, VK_UP, 0x20000000L ) ;
- break ;
-
- case WM_VSCROLL:
- control4_process_message( &list->ca, msg, wParam, lParam ) ;
- break ;
-
- case WM_SETFOCUS:
- combo = (G4COMBO *)list->combo ;
- SetFocus( combo->edit.ca.gw.hWindow ) ;
- {
- W4ENTRY *ew = w4entry( combo ) ;
- ew->ControlList.selected = (void *) &combo->edit.ca ;
- }
- break ;
-
- case WM_SYSKEYDOWN:
- if ( wParam == VK_UP )
- if ( lParam & 0x20000000L ) /* alt key */
- {
- combo = (G4COMBO *)list->combo ;
- SetFocus( combo->edit.ca.gw.hWindow ) ;
- {
- W4ENTRY *ew = w4entry( combo ) ;
- ew->ControlList.selected = (void *) &combo->edit.ca ;
- }
- combo4_process_message( combo, WM_SYSKEYDOWN, VK_UP, 0x20000000L ) ;
- }
- break ;
- }
- return control4_process_message( &list->ca, msg, wParam, lParam ) ;
- }
-
- int S4FUNCTION g4list_reset( G4LIST *la )
- {
- if ( la->select != 0 )
- {
- u4free( la->select ) ;
- la->select = 0 ;
- }
- SendMessage( g4handle( la ), LB_RESETCONTENT, 0, 0 ) ;
- return 0 ;
- }
-
- /* for the future, add the void pointer parameter to the call to g4list_add,
- ie., do it all in one step */
- /* I am assuming that g4list_ptr_save is called immediately after the call to
- g4list_add, if not, the internal variable 'int index' could be obsolete, or
- be a reference to another element of the listbox */
- /* if rc == LB_ERR, e4error if S4DEBUG, return -1 (error) */
-
- int S4FUNCTION g4list_ptr_save( G4LIST *la, void *ptr )
- {
- return SendMessage( g4handle( la ), LB_SETITEMDATA, (WORD) la->index, (DWORD)(LPSTR) ptr ) ;
- }
-
- void * S4FUNCTION g4list_ptr( G4LIST *la )
- {
- /* check the returns for the SendMessage function calls */
- DWORD ptr ;
- int index = SendMessage( g4handle( la ), LB_GETCURSEL, 0, 0 ) ;
- if ( index == LB_ERR ) /* no selection */
- return 0 ;
- ptr = SendMessage( g4handle( la ), LB_GETITEMDATA, index, 0 ) ;
-
- return (void *)(char *) ptr ;
- }
-