home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / codebase.zip / U4BROWSE.C < prev    next >
C/C++ Source or Header  |  1992-12-20  |  14KB  |  523 lines

  1. /* u4browse.c (c)Copyright Sequiter Software Inc., 1991. All rights reserved. */
  2.  
  3. /* Application support files, which come with CodeBase, are used by
  4.    this utility.  Consequently, it is only necessary to define the
  5.    appropriate menu choices. */
  6.  
  7. #include "c4windows.h"                  /* CodeWindows header file */
  8. #include <direct.h>
  9.  
  10. #include "ben.h"
  11.  
  12. D4DATA    *data = 0 ;               /* The data file to browse or edit. */
  13. W4ENTRY   *entry ;
  14. F4FIELD   *field ;
  15. G4DISPLAY *display ;
  16. G4EDIT    *edit ;
  17. HDC hDC ;
  18. TEXTMETRIC tm ;
  19. RECT r ;
  20. int i, max_width ;
  21.  
  22. C4WINDOWS   browsec4windows_info ;
  23. C4WINDOWS  *browsec4windows = &browsec4windows_info ;
  24. C4CODE     *browsec4base ;
  25.  
  26. HANDLE      main_browse ;
  27.  
  28. long FAR PASCAL _export browse_proc     ( HWND, UINT, UINT, LONG ) ;
  29. BOOL            browseInitApplication ( HANDLE ) ;
  30. BOOL            browseInitInstance    ( HANDLE, int ) ;
  31.  
  32. #define FILE_NAME     100
  33. #define PATH          106
  34. #define FILE_LIST     110
  35. #define DIR_LIST      115
  36. #define DRIVE_LIST    120
  37. #define OK            200
  38. #define CANCEL        210
  39.  
  40. char  wild_card[14] ;
  41. char *return_buf ;
  42. int   return_buf_len ;
  43. char *title ;
  44. FARPROC proc = 0 ;
  45. HWND hDisable ;
  46.  
  47. int browseWinMain() ;
  48. int browseWinMain()
  49. {
  50.    MSG msg;
  51.  
  52.    if ( ! browseInitApplication( hinst ) )
  53.       return 0 ;
  54.  
  55.    if ( ! browseInitInstance( hinst, SW_SHOWNORMAL ) )
  56.        return 0 ;
  57.  
  58.    browsec4windows = w4init( hinst, main_browse ) ;
  59.    browsec4base = &browsec4windows->cb ;
  60.    if ( browseuser_init() < 0 )
  61.       SendMessage( main_browse, WM_CLOSE, 0, 0 ) ;
  62.  
  63.    while ( GetMessage( &msg, NULL, NULL, NULL ) )
  64.    {
  65.       TranslateMessage( &msg ) ;
  66.       DispatchMessage( &msg ) ;
  67.    }
  68.  
  69.    browseuser_cleanup() ;
  70.    w4init_undo( browsec4windows ) ;
  71.  
  72.    return ( msg.wParam ) ;
  73. }
  74.  
  75. BOOL browseInitApplication( HANDLE hInstance )
  76. {
  77.     WNDCLASS  wc;
  78.  
  79.     wc.style         = NULL ;
  80.     wc.lpfnWndProc   = browse_proc ;
  81.     wc.cbClsExtra    = 0 ;
  82.     wc.cbWndExtra    = sizeof( LONG ) ;
  83.     wc.hInstance     = hInstance ;
  84.     wc.hIcon         = LoadIcon( NULL, IDI_APPLICATION ) ;
  85.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW ) ;
  86.     wc.hbrBackground = GetStockObject( WHITE_BRUSH ) ;
  87.     wc.lpszMenuName  =  0 ;
  88.     wc.lpszClassName = "TestClass" ;
  89.  
  90.     return( RegisterClass( &wc ) ) ;
  91. }
  92.  
  93. BOOL browseInitInstance( HANDLE hInstance, int nCmdShow )
  94. {
  95.    HMENU hMenu =  LoadMenu( hInstance, "USER" ) ;
  96.  
  97.    main_browse = CreateWindow( "TestClass", "CodeWindows Examples",
  98.                      WS_OVERLAPPEDWINDOW,
  99.                      CW_USEDEFAULT, CW_USEDEFAULT,
  100.                 CW_USEDEFAULT, CW_USEDEFAULT,
  101.                 NULL, hMenu, hInstance, NULL ) ;
  102.    if ( !main_browse )
  103.       return 0 ;
  104.  
  105.    ShowWindow( main_browse, nCmdShow ) ;
  106.    UpdateWindow( main_browse ) ;
  107.  
  108.    return 1 ;
  109. }
  110.  
  111. long FAR PASCAL _export browse_proc( HWND hWnd, UINT message, UINT wParam,
  112.                       LONG lParam )
  113. {
  114.    switch (message)
  115.    {
  116.       case WM_DESTROY:
  117.      PostQuitMessage( 0 ) ;
  118.      break;
  119.  
  120.       case WM_COMMAND:
  121.      if ( LOWORD( lParam ) == 0 )
  122.      {
  123.         if ( browseuser_menu_actions( wParam ) < 0 )
  124.            PostMessage( hWnd, WM_CLOSE, 0, 0 ) ;
  125.         break ;
  126.      }
  127.  
  128.       default:
  129.      return( DefWindowProc( hWnd, message, wParam, lParam ) ) ;
  130.    }
  131.    return( NULL ) ;
  132. }
  133.  
  134. int browseuser_menu_actions( WORD wParam )
  135. {
  136.    switch( wParam )
  137.    {
  138.       case SPECIFY_DBF:
  139.      specify_dbf() ;                /* Open box for .DBF file */
  140.      break ;
  141.  
  142.       case SPECIFY_MDX:
  143.      specify_mdx() ;                /* Open box for .MDX file */
  144.      break ;
  145.  
  146.       case DO_BROWSE:
  147.      u4quick_browse( data ) ;       /* Menu option 'Browse' */
  148.      break ;
  149.  
  150.       case DO_EDIT:
  151.      u4quick_edit( data ) ;         /* Menu option 'Edit' */
  152.      break ;
  153.    }
  154.    return browsec4base->error_code ;          /* Return error code, if not */
  155. }                                       /* equal to 0, an error occurred */
  156.  
  157. int specify_dbf()
  158. {
  159.    char buf[100] ;  memset( buf, '\0', 100 ) ;
  160.  
  161.    browseu4get_file_name( "File To Open", "*.DBF", buf, sizeof(buf), 0 ) ;
  162.  
  163.    if ( data != 0 )
  164.    {
  165.       d4close_all( browsec4base ) ;           /* Close the currently opened */
  166.       data = 0 ;                        /* data file. */                  
  167.    }
  168.  
  169.    if ( *buf != '\0' )
  170.       data = d4open( browsec4base, buf ) ;    /* open the indicated file */
  171.    if ( data == 0 )
  172.    {
  173.       e4error_set( browsec4base, 0 ) ;        /* if an error occurred, return */
  174.       return 0 ;
  175.    }
  176.    return 0 ;
  177. }
  178.  
  179. int specify_mdx()
  180. {
  181.    char buf[100] ;  memset( buf, '\0', 100 ) ;
  182.  
  183.    if ( data == 0 )  return 0 ;
  184.  
  185.    browseu4get_file_name( "File To Open", "*.MDX", buf, sizeof(buf), 0 ) ;
  186.  
  187.    if ( *buf == '\0' )  return 0 ;
  188.    if ( d4index( data, buf ) != 0 )  return 0 ;
  189.  
  190.    if ( i4open( data, buf ) == 0 )
  191.    {
  192.       d4close_all( browsec4base ) ;           /* Error opening index, close */
  193.       data = 0 ;                        /* everything, reset data and */
  194.       e4error_set( browsec4base, 0 ) ;        /* return. */
  195.       return 0 ;
  196.    }
  197.    return 0 ;
  198. }
  199.  
  200. /* To simplify the data being put in the window, a maximum of 5 fields will be
  201.    shown.  These will be the first 5 fields.  This limitation can be removed or
  202.    altered in the following code */
  203.  
  204. u4quick_browse( D4DATA *d4 )
  205. {
  206.    int on_pos, width, n_chars ;
  207.  
  208.    if ( data == 0 )  
  209.    {
  210.       MessageBox( main_browse, "Please open a database first.", "Note", 
  211.                         MB_ICONINFORMATION | MB_OK ) ;
  212.       return 0 ;
  213.    }
  214.  
  215.    /* Create the browse window. */
  216.    entry = b4create( browsec4windows, "Browse", 0, 50, 50, 600, 400,
  217.                          main_browse, data, 1 ) ;
  218.    if ( entry == 0 )  return -1 ;
  219.  
  220.    hDC = w4get_dc( w4cb( entry ), entry->gw.hWindow ) ;
  221.    if ( hDC == 0 )  return -1 ;
  222.  
  223.    w4get_metrics( hDC, &tm ) ;
  224.    max_width = tm.tmMaxCharWidth ;
  225.  
  226.    if ( w4release_dc( w4cb( entry ), entry->gw.hWindow, hDC ) == 0 )
  227.       return -1 ;
  228.  
  229.    /* for every field in the database, make a 'column' in the browse */
  230.    for ( on_pos = 14, i = 1; i <= d4num_fields(d4); i++ )
  231.    {
  232.       field = d4field_j( d4, i ) ;
  233.  
  234.       /* Define the edit controls and static controls. */
  235.       display = g4display( entry, on_pos, 16, f4name(field) ) ;
  236.       if ( display == 0 )  return -1 ;
  237.  
  238.       edit = g4field( entry, on_pos, 48, field ) ;
  239.       if ( edit == 0 )  return -1 ;
  240.  
  241.       g4width( edit, f4len( field ) * max_width ) ; 
  242.  
  243.       /* Space out the columns by using the length of field name, or the
  244.      length of the field (whichever is longer. */
  245.  
  246.       n_chars = strlen(f4name(field)) ;
  247.       if ( n_chars < f4len(field) )
  248.      n_chars = f4len(field) ;
  249.  
  250.       width = n_chars * max_width + 5 ;
  251.       on_pos += width ;
  252.       if ( i>4 )  break ;                  /* limit of 5 fields */
  253.    }
  254.    g4read( entry, 0 ) ;
  255.    return 0 ;
  256. }
  257.  
  258. /* To simplify the data being put in the window, only the fields which will fit
  259.    in the window will be shown.  This limitation can be removed or altered in
  260.    the following code */
  261.  
  262. u4quick_edit( D4DATA *d4 )
  263. {
  264.    if ( data == 0 )  
  265.    {
  266.       MessageBox( main_browse, "Please open a database first.", "Note", 
  267.                         MB_ICONINFORMATION | MB_OK ) ;
  268.       return 0 ;
  269.    }
  270.  
  271.    /* Create the browse window. */
  272.    entry = b4create( browsec4windows, "Edit", 0, 50, 50, 600, 400,
  273.                            main_browse, data, 0 ) ;
  274.    if ( entry == 0 )  return -1 ;
  275.  
  276.    hDC = w4get_dc( w4cb( entry ), entry->gw.hWindow ) ;
  277.    if ( hDC == 0 )  return -1 ;
  278.  
  279.    w4get_metrics( hDC, &tm ) ;
  280.    max_width = tm.tmMaxCharWidth ;
  281.  
  282.    if ( w4release_dc( w4cb( entry ), entry->gw.hWindow, hDC ) == 0 )
  283.       return -1 ;
  284.  
  285.    w4get_rect( w4handle( entry ), &r, 0 ) ;
  286.  
  287.    for ( i = 1; i <= d4num_fields( d4 ) ; i++ )
  288.    {
  289.       /* only display the fields that will fit in the window */
  290.       if ( (30*i) > 350 )
  291.      break ;
  292.  
  293.       /* get a F4FIELD pointer to the ith field */
  294.       field = d4field_j( d4, i ) ;
  295.  
  296.       /* display the field name at position 10, 30*i */
  297.       display = g4display( entry, 10, 30*i, f4name(field) ) ;
  298.       if ( display == 0 )  return -1 ;
  299.  
  300.       /* display an edit control at position 120, 30*i, the entered text
  301.      will be put directly in the field pointer, ie. the database */
  302.  
  303.       edit = g4field( entry, 120, 30*i, field ) ;
  304.       if ( edit == 0 )  return -1 ;
  305.  
  306.       /* 160 = 120 (beginning of edit) + 40 (margin for window) */  
  307.       if ( r.right < (f4len( field) * max_width) + 160 )
  308.      g4width( edit, r.right - 160 ) ; 
  309.       else
  310.      g4width( edit, f4len( field ) * max_width ) ; 
  311.    }
  312.    g4read( entry, 0 ) ;
  313.    return 0 ;
  314. }
  315.  
  316. int browseuser_init()
  317. {
  318.    return 0 ;
  319. }
  320.  
  321. int browseuser_cleanup()
  322. {
  323.    return d4close_all( browsec4base ) ;         /* close any open files */
  324. }
  325.  
  326. int browseu4get_file_name( char *title_parm, char *wild_card_parm, char *buf, unsigned buf_len, HWND hDisableParm )
  327. {
  328.    int rc = 0 ;
  329.  
  330.    return_buf =  buf ;
  331.    return_buf_len =  buf_len ;
  332.    u4ncpy( wild_card, wild_card_parm, sizeof(wild_card) ) ;
  333.    title =  title_parm ;
  334.    hDisable =  hDisableParm ;
  335.  
  336.    if( proc == 0 )
  337.       proc = MakeProcInstance( (FARPROC) browsedesign4file_open_dlg_proc, browsec4base->hInst ) ;
  338.    rc = DialogBox( browsec4base->hInst, "BROWSEFILE", main_browse, proc ) ;
  339.  
  340.    return rc ;
  341. }
  342.  
  343. static void directory_trim( char *path )
  344. {
  345.    int len, i ;
  346.    len = i  =  strlen(path) ;
  347.    for(; --i >= 0; )
  348.       if( path[i] == '\\' || path[i] == ':' )
  349.       {
  350.      memmove( path, path+i+1, len - i ) ;
  351.      return ;
  352.       }
  353. }
  354.  
  355. static void name_trim( char *path )
  356. {
  357.    int len ;
  358.    for( len = strlen(path); --len >= 0; )
  359.    {
  360.       if ( path[len] == '\\' )
  361.       {
  362.      if ( len == 0 )  break ;
  363.      if ( len == 2 && path[1] == ':' )  break ;
  364.      path[len] = 0 ;
  365.      break ;
  366.       }
  367.       if ( path[len] == ':' )  break ;
  368.       path[len] =  0 ;
  369.    }
  370. }
  371.  
  372. static void u4ncat( char *to, char *from, unsigned to_len )
  373. {
  374.    unsigned len =  strlen(to) ;
  375.    if ( len >= to_len )  return ;
  376.  
  377.    u4ncpy( to+ len, from, to_len-len ) ;
  378. }
  379.  
  380. static void do_ok( HWND hDlg )
  381. {
  382.    char path[96], buf[96], file_name[96] ;
  383.    int name_has_wild_card =  0 ;
  384.    int i ;
  385.  
  386.    GetDlgItemText( hDlg, FILE_NAME, file_name, sizeof(file_name) ) ;
  387.    file_name[sizeof(file_name)-1] = 0 ;
  388.  
  389.    path[1] = 0 ;
  390.    u4ncpy( path, file_name, sizeof(path) ) ;
  391.    name_trim( path ) ;
  392.    if( path[1] == ':' )
  393.       if( _chdrive( path[0] - 'a' + 1 ) < 0 )
  394.      return ;
  395.    if( path[0] != 0 )
  396.       if( chdir( path ) < 0 )
  397.      return ;
  398.    directory_trim( file_name ) ;
  399.  
  400.    for( i = 0; file_name[i] != 0; i++ )
  401.       if( file_name[i] == '*' || file_name[i] == '?' )
  402.      name_has_wild_card =  1 ;
  403.  
  404.    if( name_has_wild_card )
  405.    {
  406.       DlgDirList( hDlg, file_name, FILE_LIST, PATH, 0x21 ) ;
  407.       DlgDirList( hDlg, "*.*", DIR_LIST, 0, 0x8010 ) ;
  408.       SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
  409.    }
  410.    else
  411.    {
  412.       int len ;
  413.       getcwd( return_buf, return_buf_len ) ;
  414.       len = strlen( return_buf ) ;
  415.       if ( len > 0 )
  416.      if ( return_buf[len-1] =='\\' )
  417.         return_buf[len-1] = 0 ;
  418.  
  419.       u4ncat( return_buf, "\\", return_buf_len ) ;
  420.       u4ncat( return_buf, file_name, return_buf_len ) ;
  421.  
  422.       EndDialog( hDlg, c4return ) ;
  423.    }
  424. }
  425.  
  426. BOOL FAR PASCAL _export browsedesign4file_open_dlg_proc( HWND hDlg, WORD message, WORD wParam, LONG lParam )
  427. {
  428.    char buf[96], file_name[96] ;
  429.  
  430.    switch( message )
  431.    {
  432.       case WM_INITDIALOG:
  433.      if( hDisable != 0 )
  434.         EnableWindow( hDisable, 0 ) ;
  435.      SetWindowText( hDlg, title ) ;
  436.      DlgDirList( hDlg, "*.*", DRIVE_LIST, 0, 0xC000 ) ;
  437.      SetDlgItemText( hDlg, FILE_NAME, wild_card ) ;
  438.      do_ok( hDlg ) ;
  439.      return TRUE ;
  440.  
  441.       case WM_DESTROY:
  442.      if( hDisable != 0 )
  443.      {
  444.         EnableWindow( hDisable, 1 ) ;
  445.         SetFocus( hDisable ) ;
  446.      }
  447.      break ;
  448.  
  449.       case WM_COMMAND:
  450.      switch( wParam )
  451.      {
  452.         case FILE_LIST:
  453.            switch( HIWORD(lParam) )
  454.            {
  455.           case LBN_SELCHANGE:
  456.              memset( file_name, 0, sizeof(file_name) ) ;
  457.              DlgDirSelect( hDlg, file_name, FILE_LIST ) ;
  458.              SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
  459.              return TRUE ;
  460.  
  461.           case LBN_DBLCLK:
  462.              do_ok( hDlg ) ;
  463.              return TRUE ;
  464.  
  465.           case LBN_KILLFOCUS:
  466.              return FALSE ;
  467.  
  468.           case LBN_SETFOCUS:
  469.              return FALSE ;
  470.            }
  471.            break ;
  472.  
  473.         case DIR_LIST:
  474.            switch( HIWORD(lParam) )
  475.            {
  476.           case LBN_SELCHANGE:
  477.              memset( file_name, 0, sizeof(file_name) ) ;
  478.              DlgDirSelect( hDlg, file_name, DIR_LIST ) ;
  479.              strcat( file_name, wild_card ) ;
  480.              SetDlgItemText( hDlg, FILE_NAME, file_name ) ;
  481.              return TRUE ;
  482.  
  483.           case LBN_DBLCLK:
  484.              do_ok( hDlg ) ;
  485.              return TRUE ;
  486.            }
  487.            break ;
  488.  
  489.         case DRIVE_LIST:
  490.            switch( HIWORD(lParam) )
  491.            {
  492.           case LBN_SELCHANGE:
  493.              memset( buf, 0, sizeof(buf) ) ;
  494.              DlgDirSelect( hDlg, buf, DRIVE_LIST ) ;
  495.              if( _chdrive( buf[0] - 'a' + 1 ) < 0 )
  496.             return TRUE ;
  497.  
  498.              SetDlgItemText( hDlg, FILE_NAME, wild_card ) ;
  499.              DlgDirList( hDlg, wild_card, FILE_LIST, PATH, 0x21 ) ;
  500.  
  501.              strcpy( buf, "*.*" ) ;
  502.              DlgDirList( hDlg, buf, DIR_LIST, 0, 0x8010 ) ;
  503.              return TRUE ;
  504.  
  505.           case LBN_DBLCLK:
  506.              do_ok( hDlg ) ;
  507.              return TRUE ;
  508.            }
  509.            break ;
  510.  
  511.         case OK:
  512.            do_ok( hDlg ) ;
  513.            return TRUE ;
  514.  
  515.         case CANCEL:
  516.            EndDialog( hDlg, c4cancel ) ;
  517.            return TRUE ;
  518.      }
  519.      break ;
  520.    }
  521.    return FALSE ;
  522. }
  523.