home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
progmisc
/
codebase.zip
/
U4BROWSE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-20
|
14KB
|
523 lines
/* u4browse.c (c)Copyright Sequiter Software Inc., 1991. All rights reserved. */
/* Application support files, which come with CodeBase, are used by
this utility. Consequently, it is only necessary to define the
appropriate menu choices. */
#include "c4windows.h" /* CodeWindows header file */
#include <direct.h>
#include "ben.h"
D4DATA *data = 0 ; /* The data file to browse or edit. */
W4ENTRY *entry ;
F4FIELD *field ;
G4DISPLAY *display ;
G4EDIT *edit ;
HDC hDC ;
TEXTMETRIC tm ;
RECT r ;
int i, max_width ;
C4WINDOWS browsec4windows_info ;
C4WINDOWS *browsec4windows = &browsec4windows_info ;
C4CODE *browsec4base ;
HANDLE main_browse ;
long FAR PASCAL _export browse_proc ( HWND, UINT, UINT, LONG ) ;
BOOL browseInitApplication ( HANDLE ) ;
BOOL browseInitInstance ( HANDLE, int ) ;
#define FILE_NAME 100
#define PATH 106
#define FILE_LIST 110
#define DIR_LIST 115
#define DRIVE_LIST 120
#define OK 200
#define CANCEL 210
char wild_card[14] ;
char *return_buf ;
int return_buf_len ;
char *title ;
FARPROC proc = 0 ;
HWND hDisable ;
int browseWinMain() ;
int browseWinMain()
{
MSG msg;
if ( ! browseInitApplication( hinst ) )
return 0 ;
if ( ! browseInitInstance( hinst, SW_SHOWNORMAL ) )
return 0 ;
browsec4windows = w4init( hinst, main_browse ) ;
browsec4base = &browsec4windows->cb ;
if ( browseuser_init() < 0 )
SendMessage( main_browse, WM_CLOSE, 0, 0 ) ;
while ( GetMessage( &msg, NULL, NULL, NULL ) )
{
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
browseuser_cleanup() ;
w4init_undo( browsec4windows ) ;
return ( msg.wParam ) ;
}
BOOL browseInitApplication( HANDLE hInstance )
{
WNDCLASS wc;
wc.style = NULL ;
wc.lpfnWndProc = browse_proc ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = sizeof( LONG ) ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ) ;
wc.hCursor = LoadCursor( NULL, IDC_ARROW ) ;
wc.hbrBackground = GetStockObject( WHITE_BRUSH ) ;
wc.lpszMenuName = 0 ;
wc.lpszClassName = "TestClass" ;
return( RegisterClass( &wc ) ) ;
}
BOOL browseInitInstance( HANDLE hInstance, int nCmdShow )
{
HMENU hMenu = LoadMenu( hInstance, "USER" ) ;
main_browse = CreateWindow( "TestClass", "CodeWindows Examples",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, hMenu, hInstance, NULL ) ;
if ( !main_browse )
return 0 ;
ShowWindow( main_browse, nCmdShow ) ;
UpdateWindow( main_browse ) ;
return 1 ;
}
long FAR PASCAL _export browse_proc( HWND hWnd, UINT message, UINT wParam,
LONG lParam )
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage( 0 ) ;
break;
case WM_COMMAND:
if ( LOWORD( lParam ) == 0 )
{
if ( browseuser_menu_actions( wParam ) < 0 )
PostMessage( hWnd, WM_CLOSE, 0, 0 ) ;
break ;
}
default:
return( DefWindowProc( hWnd, message, wParam, lParam ) ) ;
}
return( NULL ) ;
}
int browseuser_menu_actions( WORD wParam )
{
switch( wParam )
{
case SPECIFY_DBF:
specify_dbf() ; /* Open box for .DBF file */
break ;
case SPECIFY_MDX:
specify_mdx() ; /* Open box for .MDX file */
break ;
case DO_BROWSE:
u4quick_browse( data ) ; /* Menu option 'Browse' */
break ;
case DO_EDIT:
u4quick_edit( data ) ; /* Menu option 'Edit' */
break ;
}
return browsec4base->error_code ; /* Return error code, if not */
} /* equal to 0, an error occurred */
int specify_dbf()
{
char buf[100] ; memset( buf, '\0', 100 ) ;
browseu4get_file_name( "File To Open", "*.DBF", buf, sizeof(buf), 0 ) ;
if ( data != 0 )
{
d4close_all( browsec4base ) ; /* Close the currently opened */
data = 0 ; /* data file. */
}
if ( *buf != '\0' )
data = d4open( browsec4base, buf ) ; /* open the indicated file */
if ( data == 0 )
{
e4error_set( browsec4base, 0 ) ; /* if an error occurred, return */
return 0 ;
}
return 0 ;
}
int specify_mdx()
{
char buf[100] ; memset( buf, '\0', 100 ) ;
if ( data == 0 ) return 0 ;
browseu4get_file_name( "File To Open", "*.MDX", buf, sizeof(buf), 0 ) ;
if ( *buf == '\0' ) return 0 ;
if ( d4index( data, buf ) != 0 ) return 0 ;
if ( i4open( data, buf ) == 0 )
{
d4close_all( browsec4base ) ; /* Error opening index, close */
data = 0 ; /* everything, reset data and */
e4error_set( browsec4base, 0 ) ; /* return. */
return 0 ;
}
return 0 ;
}
/* To simplify the data being put in the window, a maximum of 5 fields will be
shown. These will be the first 5 fields. This limitation can be removed or
altered in the following code */
u4quick_browse( D4DATA *d4 )
{
int on_pos, width, n_chars ;
if ( data == 0 )
{
MessageBox( main_browse, "Please open a database first.", "Note",
MB_ICONINFORMATION | MB_OK ) ;
return 0 ;
}
/* Create the browse window. */
entry = b4create( browsec4windows, "Browse", 0, 50, 50, 600, 400,
main_browse, data, 1 ) ;
if ( entry == 0 ) return -1 ;
hDC = w4get_dc( w4cb( entry ), entry->gw.hWindow ) ;
if ( hDC == 0 ) return -1 ;
w4get_metrics( hDC, &tm ) ;
max_width = tm.tmMaxCharWidth ;
if ( w4release_dc( w4cb( entry ), entry->gw.hWindow, hDC ) == 0 )
return -1 ;
/* for every field in the database, make a 'column' in the browse */
for ( on_pos = 14, i = 1; i <= d4num_fields(d4); i++ )
{
field = d4field_j( d4, i ) ;
/* Define the edit controls and static controls. */
display = g4display( entry, on_pos, 16, f4name(field) ) ;
if ( display == 0 ) return -1 ;
edit = g4field( entry, on_pos, 48, field ) ;
if ( edit == 0 ) return -1 ;
g4width( edit, f4len( field ) * max_width ) ;
/* Space out the columns by using the length of field name, or the
length of the field (whichever is longer. */
n_chars = strlen(f4name(field)) ;
if ( n_chars < f4len(field) )
n_chars = f4len(field) ;
width = n_chars * max_width + 5 ;
on_pos += width ;
if ( i>4 ) break ; /* limit of 5 fields */
}
g4read( entry, 0 ) ;
return 0 ;
}
/* To simplify the data being put in the window, only the fields which will fit
in the window will be shown. This limitation can be removed or altered in
the following code */
u4quick_edit( D4DATA *d4 )
{
if ( data == 0 )
{
MessageBox( main_browse, "Please open a database first.", "Note",
MB_ICONINFORMATION | MB_OK ) ;
return 0 ;
}
/* Create the browse window. */
entry = b4create( browsec4windows, "Edit", 0, 50, 50, 600, 400,
main_browse, data, 0 ) ;
if ( entry == 0 ) return -1 ;
hDC = w4get_dc( w4cb( entry ), entry->gw.hWindow ) ;
if ( hDC == 0 ) return -1 ;
w4get_metrics( hDC, &tm ) ;
max_width = tm.tmMaxCharWidth ;
if ( w4release_dc( w4cb( entry ), entry->gw.hWindow, hDC ) == 0 )
return -1 ;
w4get_rect( w4handle( entry ), &r, 0 ) ;
for ( i = 1; i <= d4num_fields( d4 ) ; i++ )
{
/* only display the fields that will fit in the window */
if ( (30*i) > 350 )
break ;
/* get a F4FIELD pointer to the ith field */
field = d4field_j( d4, i ) ;
/* display the field name at position 10, 30*i */
display = g4display( entry, 10, 30*i, f4name(field) ) ;
if ( display == 0 ) return -1 ;
/* display an edit control at position 120, 30*i, the entered text
will be put directly in the field pointer, ie. the database */
edit = g4field( entry, 120, 30*i, field ) ;
if ( edit == 0 ) return -1 ;
/* 160 = 120 (beginning of edit) + 40 (margin for window) */
if ( r.right < (f4len( field) * max_width) + 160 )
g4width( edit, r.right - 160 ) ;
else
g4width( edit, f4len( field ) * max_width ) ;
}
g4read( entry, 0 ) ;
return 0 ;
}
int browseuser_init()
{
return 0 ;
}
int browseuser_cleanup()
{
return d4close_all( browsec4base ) ; /* close any open files */
}
int browseu4get_file_name( char *title_parm, char *wild_card_parm, char *buf, unsigned buf_len, HWND hDisableParm )
{
int rc = 0 ;
return_buf = buf ;
return_buf_len = buf_len ;
u4ncpy( wild_card, wild_card_parm, sizeof(wild_card) ) ;
title = title_parm ;
hDisable = hDisableParm ;
if( proc == 0 )
proc = MakeProcInstance( (FARPROC) browsedesign4file_open_dlg_proc, browsec4base->hInst ) ;
rc = DialogBox( browsec4base->hInst, "BROWSEFILE", main_browse, proc ) ;
return rc ;
}
static void directory_trim( char *path )
{
int len, i ;
len = i = strlen(path) ;
for(; --i >= 0; )
if( path[i] == '\\' || path[i] == ':' )
{
memmove( path, path+i+1, len - i ) ;
return ;
}
}
static void name_trim( char *path )
{
int len ;
for( len = strlen(path); --len >= 0; )
{
if ( path[len] == '\\' )
{
if ( len == 0 ) break ;
if ( len == 2 && path[1] == ':' ) break ;
path[len] = 0 ;
break ;
}
if ( path[len] == ':' ) break ;
path[len] = 0 ;
}
}
static void u4ncat( char *to, char *from, unsigned to_len )
{
unsigned len = strlen(to) ;
if ( len >= to_len ) return ;
u4ncpy( to+ len, from, to_len-len ) ;
}
static void do_ok( HWND hDlg )
{
char path[96], buf[96], file_name[96] ;
int name_has_wild_card = 0 ;
int i ;
GetDlgItemText( hDlg, FILE_NAME, file_name, sizeof(file_name) ) ;
file_name[sizeof(file_name)-1] = 0 ;
path[1] = 0 ;
u4ncpy( path, file_name, sizeof(path) ) ;
name_trim( path ) ;
if( path[1] == ':' )
if( _chdrive( path[0] - 'a' + 1 ) < 0 )
return ;
if( path[0] != 0 )
if( chdir( path ) < 0 )
return ;
directory_trim( file_name ) ;
for( i = 0; file_name[i] != 0; i++ )
if( file_name[i] == '*' || file_name[i] == '?' )
name_has_wild_card = 1 ;
if( name_has_wild_card )
{
DlgDirList( hDlg, file_name, FILE_LIST, PATH, 0x21 ) ;
DlgDirList( hDlg, "*.*", DIR_LIST, 0, 0x8010 ) ;
SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
}
else
{
int len ;
getcwd( return_buf, return_buf_len ) ;
len = strlen( return_buf ) ;
if ( len > 0 )
if ( return_buf[len-1] =='\\' )
return_buf[len-1] = 0 ;
u4ncat( return_buf, "\\", return_buf_len ) ;
u4ncat( return_buf, file_name, return_buf_len ) ;
EndDialog( hDlg, c4return ) ;
}
}
BOOL FAR PASCAL _export browsedesign4file_open_dlg_proc( HWND hDlg, WORD message, WORD wParam, LONG lParam )
{
char buf[96], file_name[96] ;
switch( message )
{
case WM_INITDIALOG:
if( hDisable != 0 )
EnableWindow( hDisable, 0 ) ;
SetWindowText( hDlg, title ) ;
DlgDirList( hDlg, "*.*", DRIVE_LIST, 0, 0xC000 ) ;
SetDlgItemText( hDlg, FILE_NAME, wild_card ) ;
do_ok( hDlg ) ;
return TRUE ;
case WM_DESTROY:
if( hDisable != 0 )
{
EnableWindow( hDisable, 1 ) ;
SetFocus( hDisable ) ;
}
break ;
case WM_COMMAND:
switch( wParam )
{
case FILE_LIST:
switch( HIWORD(lParam) )
{
case LBN_SELCHANGE:
memset( file_name, 0, sizeof(file_name) ) ;
DlgDirSelect( hDlg, file_name, FILE_LIST ) ;
SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
return TRUE ;
case LBN_DBLCLK:
do_ok( hDlg ) ;
return TRUE ;
case LBN_KILLFOCUS:
return FALSE ;
case LBN_SETFOCUS:
return FALSE ;
}
break ;
case DIR_LIST:
switch( HIWORD(lParam) )
{
case LBN_SELCHANGE:
memset( file_name, 0, sizeof(file_name) ) ;
DlgDirSelect( hDlg, file_name, DIR_LIST ) ;
strcat( file_name, wild_card ) ;
SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
return TRUE ;
case LBN_DBLCLK:
do_ok( hDlg ) ;
return TRUE ;
}
break ;
case DRIVE_LIST:
switch( HIWORD(lParam) )
{
case LBN_SELCHANGE:
memset( buf, 0, sizeof(buf) ) ;
DlgDirSelect( hDlg, buf, DRIVE_LIST ) ;
if( _chdrive( buf[0] - 'a' + 1 ) < 0 )
return TRUE ;
SetDlgItemText( hDlg, FILE_NAME, wild_card ) ;
DlgDirList( hDlg, wild_card, FILE_LIST, PATH, 0x21 ) ;
strcpy( buf, "*.*" ) ;
DlgDirList( hDlg, buf, DIR_LIST, 0, 0x8010 ) ;
return TRUE ;
case LBN_DBLCLK:
do_ok( hDlg ) ;
return TRUE ;
}
break ;
case OK:
do_ok( hDlg ) ;
return TRUE ;
case CANCEL:
EndDialog( hDlg, c4cancel ) ;
return TRUE ;
}
break ;
}
return FALSE ;
}