home *** CD-ROM | disk | FTP | other *** search
- /* g4combo.c ComboBox Area Routines (c)Copyright Sequiter Software Inc., 1991. All rights reserved. */
-
- #include "c4window.h"
- #include "e4error.h"
-
- #ifdef __TURBOC__
- #pragma hdrstop
- #endif
-
- int S4FUNCTION g4combo_add( G4COMBO *combo, char *choice, int do_select )
- {
- int rc = g4list_add( combo->list, choice, do_select ) ;
- if ( rc < 0 ) return rc ;
- if ( do_select )
- if ( combo->list->select != 0 )
- combo4_setselection( combo->edit.ca.gw.hWindow, w4entry( combo ), 0 ) ;
- return rc ;
- }
-
- int S4CALL combo4_buttonproc( G4BUTTON *button )
- {
- G4COMBO *combo = (G4COMBO *) g4button_data( button ) ;
- W4ENTRY *ew = w4entry( combo ) ;
-
- if ( combo->is_down )
- combo4_process_message( combo, WM_SYSKEYDOWN, VK_UP, 0x20000000L ) ;
- else
- combo4_process_message( combo, WM_SYSKEYDOWN, VK_DOWN, 0x20000000L ) ;
-
- SetFocus( g4handle( &combo->edit ) ) ;
- ew->ControlList.selected = (void *) &combo->edit.ca ;
-
- return 0 ; /* don't close entry window */
- }
-
- /* '0' is returned to send message to the G4EDIT proc. */
-
- int combo4_process_message( G4COMBO *combo, WORD msg, WORD wParam, LONG lParam )
- {
- int index, index2 ;
-
- switch( msg )
- {
- case WM_CHAR:
- if ( combo->edit.isModify == 0 )
- {
- SendMessage( g4handle( combo->list ), WM_CHAR, wParam, lParam ) ;
- return 0 ;
- }
-
- case WM_KEYDOWN:
- if ( combo->edit.Buffer == 0 )
- {
- combo->edit.Buffer = (char *) u4alloc( combo->edit.pResultLen+1 ) ;
- if ( combo->edit.Buffer == 0 )
- {
- e4error( g4cb( combo ), e4memory, (char *) 0 ) ;
- return 1 ;
- }
- combo->edit.Buffer[combo->edit.pResultLen] = 0 ;
- }
-
- index2 = (int) SendMessage( g4handle( combo->list ), LB_GETCURSEL, 0, 0 ) ;
- CallWindowProc( combo->edit.ca.gw.OldWindowProc,
- g4handle( &combo->edit ), msg, wParam, lParam ) ;
-
- index = (int) SendMessage( g4handle( combo->list ), LB_FINDSTRING,
- -1, (DWORD) (LPSTR) combo->edit.Buffer ) ;
-
- if ( index < 0 )
- SendMessage( g4handle( combo->list ), LB_SETCURSEL, -1, 0 ) ;
-
- if ( (index2 < 0) && (index >= 0) )
- SendMessage( g4handle( combo->list ), LB_SETCURSEL, index, NULL ) ;
-
- return 1 ;
-
- case WM_SYSKEYDOWN:
- switch( wParam )
- {
- case VK_DOWN:
- if ( lParam & 0x20000000L )
- {
- G4CONTROL *ca ;
- int combo_height = combo4_combosize( w4entry( combo )) ;
-
- /* 'height' of listbox has to be a multiple of tm.tmHeight */
- TEXTMETRIC tm ;
- HDC hDC = w4get_dc( g4cb( combo ), g4handle( combo->list )) ;
- if ( hDC == 0 ) return 0 ;
- w4get_metrics( hDC, &tm ) ;
- if ( w4release_dc( g4cb( combo ), g4handle( combo->list ), hDC ) == 0 ) return 0 ;
-
- if ( combo->is_down == 1 ) return 1 ;
- if ( combo->isDrop == 1 )
- {
- ShowWindow( g4handle( combo->list ), SW_SHOWNORMAL ) ;
- UpdateWindow( g4handle( combo->list )) ;
- }
- combo->is_down = 1 ;
-
- for ( ca = 0 ; ca = (G4CONTROL *) l4next( &combo->edit.ca.ew->ControlList, ca ) ; )
- {
- if ( ca->area_type == c4edit )
- {
- G4EDIT *ea = (G4EDIT *) ca->area ;
- if ( ea->combo != 0 )
- {
- G4COMBO *cba = ea->combo ;
- ValidateRect( g4handle( cba->list ), 0 ) ;
- ValidateRect( g4handle( &cba->edit ), 0 ) ;
- if ( cba->button != 0 )
- ValidateRect( g4handle( cba->button ), 0 ) ;
- }
- }
- ca->nopaint = 1 ;
- ValidateRect( g4handle( ca ), 0 ) ;
- }
- return 1 ;
- }
- else
- {
- SendMessage( g4handle( combo->list ), WM_KEYDOWN, VK_DOWN, NULL ) ;
- return 1 ;
- }
-
- case VK_UP:
- if ( lParam & 0x20000000L )
- {
- combo->is_down = 0 ;
- if ( combo->isDrop == 1 )
- {
- ShowWindow( g4handle( combo->list ), SW_HIDE ) ;
- UpdateWindow( g4handle( combo->list )) ;
- /*****
- (uncomment the following 2 lines for the following effect)
-
- The validation function (if one exists) is called when the combobox is
- closed. This allows the user to update controls that exist 'under' the
- combobox. Since the validation is also called when the selection changes,
- the validation could be called twice.
- *****/
- /* if ( ! u4ptr_equal( (void *) 0, (void *) combo->edit.valid ) ) */
- /* (*combo->edit.valid)( (struct G4EDIT_st *) &combo->edit, combo->edit.valid_data ) ; */
- }
- return 1 ;
- }
- break ;
- }
- break ;
- }
- return 0 ;
- }
-
- /* if 'refresh' = 1, then refresh from original, otherwise do not */
-
- void combo4_setselection( HWND hwnd, W4ENTRY *ew, int refresh )
- {
- int id = GetWindowWord( hwnd, GWW_ID ) ;
- G4EDIT *ea = g4lookup_edit( ew, id ) ;
- G4COMBO *combo = (G4COMBO *) ea->combo ;
-
- long index ;
-
- if ( ea->pResultLen <= 0 ) return ; /* no edit buffer */
- index = SendMessage( g4handle( combo->list ), LB_GETCURSEL, 0, 0 ) ;
-
- if ( index == LB_ERR )
- {
- index++ ;
- SendMessage( g4handle( combo->list ), LB_SETCURSEL, (WORD) index, (DWORD) NULL ) ;
- }
- if ( combo->edit.Buffer != 0 )
- u4free( combo->edit.Buffer ) ;
-
- combo->edit.Buffer = (char *) u4alloc( combo->edit.pResultLen+1 ) ;
- if ( combo->edit.Buffer == 0 )
- {
- e4error( g4cb( combo ), e4memory, (char *) 0 ) ;
- return ;
- }
- memset( combo->edit.Buffer, ' ', combo->edit.pResultLen ) ;
- combo->edit.Buffer[combo->edit.pResultLen] = 0 ;
-
- /* The combobox choices could be longer than the edit buffer */
- {
- char buf[256] ; memset( buf, ' ', 255 ) ; buf[255] = 0 ;
- SendMessage( g4handle( combo->list ), LB_GETTEXT, (int) index, (DWORD) buf ) ;
- u4ncpy( ea->Buffer, buf, ea->pResultLen+1 ) ;
- }
-
- if ( ! u4ptr_equal( (void *) 0, (void *) combo->edit.valid ) )
- if ( (*combo->edit.valid)( (struct G4EDIT_st *) &combo->edit, combo->edit.valid_data ) != 0 )
- return ;
-
- InvalidateRect( g4handle( &combo->edit ), 0, 0 ) ;
-
- if ( refresh == 0 ) return ;
-
- if ( combo->edit.data_field != 0 )
- {
- memcpy( combo->edit.Buffer, combo->edit.pResult, combo->edit.pResultLen ) ;
-
- index = SendMessage( g4handle( combo->list ), LB_FINDSTRING, -1, (DWORD) combo->edit.Buffer ) ;
- if ( index == LB_ERR )
- SendMessage( g4handle( combo->list ), LB_SETCURSEL, -1, 0 ) ;
- else
- SendMessage( g4handle( combo->list ), LB_SETCURSEL, (int) index, 0 ) ;
- }
- }
-
- G4COMBO *S4FUNCTION g4combo( W4ENTRY *ew, int x, int y, int width, int height, char *ptr,
- unsigned ptr_len, int do_sort )
- {
- 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.Id = entry4_nextid( ew ) ;
- control.Style = ( do_sort == 0 ) ? 0 : CBS_SORT ;
- control.Y = y ;
- control.X = x ;
- control.CX = width ;
- control.CY = height ;
-
- return combo4_create( ew, &control, ptr, ptr_len, -1, -1 ) ;
- }
-
- void S4FUNCTION g4combo_simple( G4COMBO *combo )
- {
- int combo_height ;
-
- #ifdef S4DEBUG
- if ( combo == 0 )
- e4severe( e4parm, "g4combo_simple()", E4_PARM_ZER, (char *) 0 ) ;
- #endif
-
- if ( combo->button == 0 ) return ;
- combo->isDrop = 0 ;
-
- DestroyWindow( g4handle( (void *) combo->button ) ) ;
- button4_free( combo->button ) ;
- combo->button = 0 ;
-
- combo->edit.width = combo->width ;
- combo_height = combo4_combosize( w4entry( combo )) ;
-
- ShowWindow( g4handle( combo->list ), SW_SHOWNORMAL ) ;
- UpdateWindow( g4handle( combo->list )) ;
- }
-
- void combo4_buttondrawbitmap( HDC hdc, HBITMAP hBitmap, short xStart, short yStart )
- {
- BITMAP bm ;
- POINT ptSize, ptOrg ;
-
- HDC hdcMem = CreateCompatibleDC( hdc ) ;
- if ( hdcMem == 0 )
- e4severe( e4result, E4_RESULT_CDC, (char *) 0 ) ;
- SelectObject( hdcMem, hBitmap ) ;
- SetMapMode( hdcMem, GetMapMode( hdc )) ;
-
- GetObject( hBitmap, sizeof( BITMAP ), (LPSTR) &bm ) ;
-
- ptSize.x = bm.bmWidth ;
- ptSize.y = bm.bmHeight ;
- DPtoLP( hdc, &ptSize, 1 ) ;
-
- ptOrg.x = ptOrg.y = 0 ;
- DPtoLP( hdcMem, &ptOrg, 1 ) ;
-
- BitBlt( hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, ptOrg.x, ptOrg.y,
- SRCCOPY ) ;
- DeleteDC( hdcMem ) ;
- }
-
- void combo4_buttondraw( LPDRAWITEMSTRUCT draw, W4ENTRY *ew )
- {
- RECT rc;
- HBRUSH hbr;
-
- CopyRect( (LPRECT)&rc, (LPRECT)&draw->rcItem ) ;
-
- hbr = CreateSolidBrush( 0x00FFFFFFL ) ;
- FillRect( draw->hDC, (LPRECT)&rc, hbr ) ;
- DeleteObject( hbr ) ;
-
- combo4_buttondrawbitmap( draw->hDC, ew->gw.cw->combo_arrow, 5, 5 ) ;
-
- hbr = GetStockObject( BLACK_BRUSH ) ;
- FrameRect( draw->hDC, (LPRECT)&rc, hbr ) ;
- DeleteObject( hbr ) ;
-
- hbr = GetStockObject( LTGRAY_BRUSH ) ;
- SelectObject( draw->hDC, hbr ) ;
- ExtFloodFill( draw->hDC, 1, 1, 0x00000000, FLOODFILLBORDER ) ;
- DeleteObject( hbr ) ;
- }
-
- int combo4_combosize( W4ENTRY *ew )
- {
- TEXTMETRIC tm ;
-
- HDC hDC = w4get_dc( w4cb( ew ), ew->gw.hWindow ) ;
- if ( hDC == 0 ) return 0 ;
-
- w4get_metrics( hDC, &tm ) ;
- if ( w4release_dc( w4cb( ew ), ew->gw.hWindow, hDC ) == 0 ) return 0 ;
-
- return ( tm.tmHeight + tm.tmInternalLeading ) ;
- }
-
- G4COMBO *combo4_create( W4ENTRY *ew, C4CONTROLITEM *control, char *ptr,
- unsigned ptr_len, int low, int high )
- {
- G4COMBO *combo ;
- int combo_height = combo4_combosize( ew ) ;
-
- if ( ew->gw.cw->cb.error_code < 0 ) return 0 ;
-
- if ( combo_height == 0 ) return 0 ;
- combo = combo4_makecombo( control, low, high, combo_height ) ;
- if ( combo == 0 ) return 0 ;
- if ( combo4_makeedit( ew, combo, control, ptr, ptr_len ) < 0 )
- {
- u4free( combo ) ;
- return 0 ;
- }
- if ( combo4_makelist( ew, combo, control->Style ) < 0 )
- {
- if ( combo->edit.Pict != 0 ) u4free( combo->edit.Pict ) ;
- u4free( combo ) ;
- return 0 ;
- }
- if ( combo4_makebutton( ew, combo, combo_height ) < 0 )
- {
- u4free( combo->list ) ;
- if ( combo->edit.Pict != 0 ) u4free( combo->edit.Pict ) ;
- u4free( combo ) ;
- return 0 ;
- }
- l4add( &ew->ControlList, (void *) &combo->edit.ca ) ;
-
- return combo ;
- }
-
- G4COMBO *combo4_makecombo ( C4CONTROLITEM *control, int low, int high, int combo_height )
- {
- G4COMBO *cba = (G4COMBO *) u4alloc( sizeof(G4COMBO) ) ;
- if ( cba == 0 )
- e4severe( e4memory, (char *) 0 ) ;
-
- cba->is_down = 0 ;
- cba->isDrop = 1 ;
-
- if ( low < 0 )
- {
- cba->x = control->X ;
- cba->y = control->Y ;
- cba->width = control->CX ;
- cba->height = control->CY ;
- }
- else
- {
- cba->x = (control->X * low )/4 ;
- cba->y = (control->Y * high)/8 ;
- cba->width = (control->CX * low )/4 ;
- cba->height = (control->CY * high)/8 ;
- }
- return cba ;
- }
-
- int combo4_makelist( W4ENTRY *ew, G4COMBO *cba, LONG style ) /* use list4_create ??? */
- {
- int combo_height = combo4_combosize( w4entry( cba )) ;
-
- cba->list = (G4LIST *) u4alloc( sizeof( G4LIST )) ;
- if ( cba->list == 0 )
- return e4error( g4cb( cba ), e4memory, (char *) 0 ) ;
-
- control4_construct( &cba->list->ca, ew ) ;
- cba->list->ca.gw.Id = entry4_nextid( ew ) ;
-
- cba->list->ca.gw.Style = LBS_NOTIFY | WS_VSCROLL | WS_BORDER | WS_CHILD ;
- if ( CBS_SORT == (style & CBS_SORT) )
- cba->list->ca.gw.Style |= LBS_SORT ;
-
- cba->list->ca.gw.hWindow = w4create_window( "ListBox", 0,
- cba->list->ca.gw.Style,
- cba->x, cba->y+combo_height,
- cba->width, cba->height-combo_height,
- w4handle( ew ), cba->list->ca.gw.Id,
- w4hInst( ew ), 0 ) ;
-
- cba->list->ca.gw.OldWindowProc = (FARPROC) GetWindowLong( g4handle( cba->list ),
- GWL_WNDPROC ) ;
- SetWindowLong( g4handle( cba->list ), GWL_WNDPROC, (LONG) ew->gw.cw->list4_proc ) ;
-
- cba->list->ca.hParent = w4handle( ew ) ;
- cba->list->ca.ew = ew ;
- cba->list->ca.gw.cw = ew->gw.cw ;
- cba->list->ca.area_type = c4list ;
- cba->list->ca.area = (void *) cba->list ;
-
- cba->list->combo = (struct G4COMBO_st *) cba ;
-
- return 0 ;
- }
-
- int combo4_makebutton( W4ENTRY *ew, G4COMBO *cba, int combo_height ) /* use button4_create ??? */
- {
- cba->button = (G4BUTTON *) u4alloc( sizeof(G4BUTTON) ) ;
- if ( cba->button == 0 )
- return e4error( g4cb( cba ), e4memory, (char *) 0 ) ;
-
- button4_construct( cba->button, ew ) ;
- cba->button->ca.gw.Id = entry4_nextid( ew ) ;
- cba->button->ca.gw.Style = WS_TABSTOP | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ;
-
- cba->button->ca.gw.hWindow = w4create_window( "Button", "C",
- cba->button->ca.gw.Style | BS_OWNERDRAW,
- cba->x+cba->width-GetSystemMetrics( SM_CXVSCROLL), cba->y, GetSystemMetrics( SM_CXVSCROLL ), combo_height,
- w4handle( ew ), cba->button->ca.gw.Id,
- w4hInst( ew ), 0 ) ;
-
- cba->button->ca.gw.OldWindowProc = (FARPROC) GetWindowLong( g4handle( cba->button ),
- GWL_WNDPROC ) ;
- SetWindowLong( g4handle( cba->button ), GWL_WNDPROC, (LONG) ew->gw.cw->button4_proc ) ;
-
- cba->button->ca.hParent = w4handle( ew ) ;
- cba->button->ca.ew = ew ;
- cba->button->ca.gw.cw = ew->gw.cw ;
-
- cba->button->routine = (S4BUTTON *) combo4_buttonproc ;
- cba->button->routine_data = (void *) cba ;
-
- cba->button->combo = (struct G4COMBO_st *) cba ;
-
- return 0 ;
- }
-
- int combo4_makeedit( W4ENTRY *ew, G4COMBO *cba, C4CONTROLITEM *control,
- char *ptr, unsigned ptr_len )
- {
- edit4_make( ew, &cba->edit ) ;
- if ( edit4_make2( ew, &cba->edit, cba->x, cba->y, 0, t4str, -1, -1, control->Id ) < 0 )
- {
- if ( cba->edit.Pict != 0 ) u4free( cba->edit.Pict ) ;
- return -1 ;
- }
-
- g4width( &cba->edit, cba->width-GetSystemMetrics( SM_CXVSCROLL )) ;
- g4set_buffer( &cba->edit, ptr, ptr_len ) ;
- cba->edit.combo = (struct G4COMBO_st *) cba ;
-
- cba->edit.ca.gw.OldWindowProc = (FARPROC) GetWindowLong( g4handle( &cba->edit ),
- GWL_WNDPROC ) ;
- SetWindowLong( g4handle( &cba->edit ), GWL_WNDPROC, (LONG) ew->gw.cw->combo4_proc ) ;
-
- return 0 ;
- }
-
- int S4FUNCTION g4combo_reset( G4COMBO *cba )
- {
- int len ;
-
- if ( cba->edit.Buffer != 0 )
- {
- len = lstrlen( cba->edit.Buffer ) ;
- if ( len > 0 ) memset( cba->edit.Buffer, ' ', len ) ;
- InvalidateRect( g4handle( &cba->edit ), 0, 0 ) ;
- }
- return g4list_reset( cba->list ) ;
- }
-
- int S4FUNCTION g4combo_ptr_save( G4COMBO *cba, void *ptr )
- {
- return g4list_ptr_save( cba->list, ptr ) ;
- }
-
- void * S4FUNCTION g4combo_ptr( G4COMBO *cba )
- {
- return g4list_ptr( cba->list ) ;
- }
-