home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 20 / ln1120 / cpptest3.cpp < prev    next >
C/C++ Source or Header  |  1992-09-05  |  6KB  |  227 lines

  1. /*
  2. Title:  CPPTEST3.CPP
  3. Caption: This is the BC++ version of the Test3 program.
  4. */
  5.  
  6. #include <windows.h>
  7. #include <commdlg.h>
  8. #include <owl.h>
  9. #include <string.h>
  10. #include <listbox.h>
  11. #include <bwcc.h>
  12.  
  13. _CLASSDEF(TFileDlg)
  14.  
  15. class TFileDlg : public TDialog {
  16. public:
  17.   OPENFILENAME OFN;
  18.   char InitialDir[80];
  19.  
  20.   TFileDlg(PTWindowsObject AParent,
  21.                       long AFlags,
  22.                      LPSTR AFileName,
  23.                        int ANameLength,
  24.                   PTModule Module = NULL);
  25.   virtual BOOL Create()
  26.                { return(0); };
  27.   virtual int  Execute();
  28.   virtual void Ok(TMessage& Msg) = [ID_FIRST + IDOK]
  29.      { Msg.Result = !CanClose(); };
  30.   virtual void Cancel(TMessage& Msg) = [ID_FIRST + IDCANCEL]
  31.      { Msg.Result = 0; };
  32. };
  33.  
  34.  
  35.  
  36. TFileDlg::TFileDlg( PTWindowsObject AParent,
  37.                                long AFlags,
  38.                               LPSTR AFileName,
  39.                                 int ANameLength,
  40.                            PTModule Module)
  41.                   : TDialog(AParent, NULL, Module)
  42. {
  43.   char TempName[9];
  44.   char TempExt[5];
  45.   char Drive[3];
  46.  
  47.   _fmemset(&OFN, 0, sizeof(OFN));
  48.   OFN.lStructSize = sizeof(OFN);
  49.   OFN.hwndOwner = AParent->HWindow;
  50.   (void *)OFN.lpfnHook = (void *)GetInstance();
  51.   OFN.Flags = AFlags | OFN_ENABLEHOOK;
  52.   OFN.hInstance = GetApplication()->hInstance;
  53.   OFN.lpstrFileTitle = NULL;
  54.   OFN.nMaxFileTitle = 0;
  55.   OFN.lpstrInitialDir = InitialDir;
  56.   OFN.lpstrFile = AFileName;
  57.   OFN.nMaxFile = ANameLength;
  58.   OFN.lpstrTitle = "Open File";
  59.   _fullpath(OFN.lpstrFile, AFileName, ANameLength);
  60.   _splitpath(OFN.lpstrFile, Drive, InitialDir, TempName, TempExt);
  61.   strcat(strcpy(OFN.lpstrFile,TempName),TempExt);
  62.  
  63.   SetFlags(WB_ALIAS, True);
  64. };
  65.  
  66.  
  67. static BOOL RegisterFails(void *AWindowsObject, void *)
  68. {
  69.   return !((PTWindowsObject)AWindowsObject)->Register();
  70. }
  71.  
  72. extern PTWindowsObject DlgCreationWindow;
  73.  
  74. int TFileDlg::Execute()
  75. /* Basically, This is the code from TDialog.Execute with the call to
  76.   DialogBoxParam changed to GetOpenFileName */
  77. {
  78.   long CDError;
  79.   int ReturnValue = -1;
  80.   PTWindowsObject OldKBHandler;
  81.  
  82.   IsModal = TRUE; 
  83.   if ( Status == 0 && Register() )
  84.   {
  85.     DisableAutoCreate();
  86.     EnableKBHandler();
  87.     if ( GetApplication() )
  88.       OldKBHandler = GetApplication()->KBHandlerWnd;
  89.  
  90.     DlgCreationWindow = this;
  91.     /* Register all the dialog's child objects (for custom control
  92.        support) */
  93.     if ( FirstThat(RegisterFails, NULL) == NULL )
  94.     {
  95.       if (GetOpenFileName(&OFN))
  96.         ReturnValue = IDOK;
  97.       else
  98.       {
  99.         CDError = CommDlgExtendedError();
  100.         if (!CDError)
  101.           ReturnValue = IDCANCEL;
  102.         else
  103.         {
  104.           Status = -CDError;
  105.           ReturnValue = Status;
  106.         }
  107.       }
  108.     }
  109.  
  110.     DlgCreationWindow = NULL;
  111.  
  112.     if ( GetApplication() )
  113.       GetApplication()->SetKBHandler(OldKBHandler);
  114.   }
  115.   if ( Status == 0 )
  116.     delete this;
  117.   else
  118.       if (ReturnValue != -1)
  119.           ReturnValue = BAD_DIALOG_STATUS;  // dialog ran, but status != 0
  120.   return ReturnValue;
  121. };
  122.  
  123.  
  124. _CLASSDEF(TCDListBox)
  125.  
  126. class TCDListBox : public TListBox {
  127. public:
  128.   HBRUSH Brush;
  129.   TCDListBox(PTWindowsObject AParent,
  130.                          int ResourceID,
  131.                     PTModule Module = NULL);
  132.   virtual ~TCDListBox()
  133.      { DeleteObject(Brush); };
  134.   virtual void  WMEraseBkgnd(TMessage&) = [WM_FIRST+WM_ERASEBKGND];
  135.   virtual void  WMDrawItem(TMessage& Msg) = [WM_FIRST+WM_DRAWITEM]
  136.      { DefWndProc(Msg); };
  137. };
  138.  
  139. TCDListBox::TCDListBox(PTWindowsObject AParent,
  140.                                    int ResourceID,
  141.                               PTModule Module)
  142.            : TListBox(AParent, ResourceID, Module)
  143. {
  144.   Brush = CreateSolidBrush( GetSysColor( COLOR_WINDOW ));
  145.   SetFlags(WB_ALIAS, True); 
  146. };
  147.  
  148. void TCDListBox::WMEraseBkgnd(TMessage& Msg)
  149. {
  150.   RECT R;
  151.   GetClientRect(HWindow,&R);
  152.   FillRect(Msg.WParam,&R,Brush);
  153.   Msg.Result = 1;
  154. };
  155.  
  156.  
  157. _CLASSDEF(TBWCCFileDlg)
  158.  
  159. class TBWCCFileDlg : public TFileDlg {
  160. public:
  161.   TBWCCFileDlg(PTWindowsObject AParent,
  162.                           long AFlags,
  163.                          LPSTR AFileName,
  164.                            int ANameLength);
  165. };
  166.  
  167. TBWCCFileDlg::TBWCCFileDlg(PTWindowsObject AParent,
  168.                                       long AFlags,
  169.                                      LPSTR AFileName,
  170.                                        int ANameLength)
  171.             : TFileDlg(AParent, AFlags, AFileName, ANameLength)
  172. {
  173.   OFN.Flags |= OFN_ENABLETEMPLATE;
  174.   OFN.lpTemplateName = "FileOpen_BWCC";
  175.   new TCDListBox(this, 1120);
  176.   new TCDListBox(this, 1121); 
  177. };
  178.  
  179.  
  180. _CLASSDEF(TCommDlgDemoWindow)
  181.  
  182. class TCommDlgDemoWindow : public TWindow {
  183. public:
  184.   TCommDlgDemoWindow(LPSTR ATitle)
  185.      : TWindow(NULL, ATitle)
  186.      { AssignMenu("MainMenu"); };
  187.   virtual void CMFileOpen(TMessage&) = [CM_FIRST + 101];
  188. };
  189.  
  190. void TCommDlgDemoWindow::CMFileOpen(TMessage&)
  191. {
  192.   char FileName[80];
  193.   int Result;
  194.  
  195.   strcpy(FileName, "*.cpp");
  196.   Result = GetApplication()->ExecDialog( new TBWCCFileDlg
  197.     (this, OFN_FILEMUSTEXIST, FileName, sizeof(FileName)));
  198.   if (Result == IDOK)
  199.     MessageBox(HWindow, FileName, "You selected", MB_OK);
  200. };
  201.  
  202.  
  203. _CLASSDEF(TCommdlgDemoApp)
  204.  
  205. class TCommDlgDemoApp : public TApplication {
  206. public:
  207.   TCommDlgDemoApp(    LPSTR AName,
  208.                   HINSTANCE hInstance,
  209.                   HINSTANCE hPrevInstance,
  210.                       LPSTR lpCmd,
  211.                         int nCmdShow)
  212.      : TApplication(AName, hInstance, hPrevInstance,
  213.                     lpCmd, nCmdShow) {};
  214.   virtual void InitMainWindow()
  215.      { MainWindow = new TCommDlgDemoWindow("Test CommDlg"); };
  216. };
  217.  
  218.  
  219. /* Run the CommDlgDemoApp */
  220. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  221.            LPSTR lpCmd, int nCmdShow)
  222. {
  223.   TCommDlgDemoApp DemoApp("TestCommDlg", hInstance, hPrevInstance, lpCmd, nCmdShow);
  224.   DemoApp.Run();
  225.   return DemoApp.Status;
  226. }
  227.