home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / basic / emed15 / samples / owl / emedtapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-03  |  14.7 KB  |  471 lines

  1. /*  Project emedit
  2.     Early Morning Software
  3.     Copyright ⌐ 1994. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    emedit.exe Application
  6.     FILE:         emedtapp.cpp
  7.     AUTHOR:       Ted Stockwell
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of emeditApp (TApplication).
  13. */
  14.  
  15.  
  16. #include <owl\vbxctl.h>
  17. #include <owl\owlpch.h>
  18. #include <dir.h>
  19. #include <emedit.h>
  20. #pragma hdrstop
  21.  
  22. #include "emedtapp.h"
  23. #include "mdtabtdl.h"  // Definition of about dialog.
  24. #include "mdtmssgb.h"  // message bar definition
  25. #include "mdtmdicl.h"  // MDI client
  26.  
  27.  
  28.  
  29. //
  30. // Generated help file.
  31. //
  32. //const char HelpFileName[] = "empad.hlp";
  33.  
  34.  
  35.  
  36. // Drag / Drop support:
  37. TFileDrop::TFileDrop (char* fileName, TPoint& p, BOOL inClient, TModule* module)
  38. {
  39.     char    exePath[MAXPATH];
  40.  
  41.     exePath[0] = 0;
  42.     FileName = strcpy(new char[strlen(fileName) + 1], fileName);
  43.     Point = p;
  44.     InClientArea = inClient;
  45.  
  46.     Icon = (WORD)FindExecutable(FileName, ".\\", exePath) <= 32 ? 0 : ::ExtractIcon(*module, exePath, 0);
  47.  
  48.     // Use a question mark if couldn't get the icon from the executable.
  49.     //
  50.     if ((WORD)Icon <= 1) {  // 0=no icons in exe,  1=not an exe
  51.         Icon = LoadIcon(0, (WORD)Icon == 1 ? IDI_APPLICATION : IDI_QUESTION);
  52.         DefIcon = TRUE;
  53.     } else
  54.         DefIcon = FALSE;
  55. }
  56.  
  57. TFileDrop::~TFileDrop ()
  58. {
  59.     delete FileName;
  60.     if (!DefIcon)
  61.         FreeResource(Icon);
  62. }
  63.  
  64. const char *TFileDrop::WhoAmI ()
  65. {
  66.   return FileName;
  67. }
  68.  
  69.  
  70. //{{emeditApp Implementation}}
  71.  
  72. //
  73. // Build a response table for all messages/commands handled
  74. // by the application.
  75. //
  76. DEFINE_RESPONSE_TABLE1(emeditApp, TApplication)
  77. //{{emeditAppRSP_TBL_BEGIN}}
  78.     EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  79.     EV_WM_DROPFILES,
  80.     EV_WM_WININICHANGE,
  81. //{{emeditAppRSP_TBL_END}}
  82. END_RESPONSE_TABLE;
  83.  
  84.  
  85. //////////////////////////////////////////////////////////
  86. // emeditApp
  87. // =====
  88. //
  89. emeditApp::emeditApp () : TApplication("Early Morning Editpad : OWL Version")
  90. {
  91. //    HelpState = FALSE;
  92. //    ContextHelp = FALSE;
  93. //    HelpCursor = 0;
  94.  
  95.     Printer = 0;
  96.     Printing = FALSE;
  97.  
  98.     // INSERT>> Your constructor code here.
  99.     gDefaultBlockType= TVbxEditor::SelMark_1_Stream;
  100.     gInsertMode= TRUE;
  101. }
  102.  
  103.  
  104. emeditApp::~emeditApp ()
  105. {
  106.     if (Printer)
  107.         delete Printer;
  108.  
  109.     // INSERT>> Your destructor code here.
  110.  
  111. }
  112.  
  113.  
  114. BOOL emeditApp::CanClose ()
  115. {
  116.     BOOL result = TApplication::CanClose();
  117.  
  118.     //
  119.     // Close the help engine if we used it.
  120.     //
  121. //    if (result && HelpState)
  122. //        MainWindow->WinHelp(HelpFileName, HELP_QUIT, 0L);
  123.  
  124.     return result;
  125. }
  126.  
  127.  
  128. void emeditApp::SetupSpeedBar (TDecoratedMDIFrame *frame)
  129. {
  130.     //
  131.     // Create default toolbar New and associate toolbar buttons with commands.
  132.     //
  133.     TControlBar* cb = new TControlBar(frame);
  134.     cb->Insert(*new TButtonGadget(CM_MDIFILENEW, CM_MDIFILENEW));
  135.     cb->Insert(*new TButtonGadget(CM_MDIFILEOPEN, CM_MDIFILEOPEN));
  136.     cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE));
  137.     cb->Insert(*new TSeparatorGadget(6));
  138.     cb->Insert(*new TButtonGadget(CM_EDITCUT, CM_EDITCUT));
  139.     cb->Insert(*new TButtonGadget(CM_EDITCOPY, CM_EDITCOPY));
  140.     cb->Insert(*new TButtonGadget(CM_EDITPASTE, CM_EDITPASTE));
  141.     cb->Insert(*new TSeparatorGadget(6));
  142.     cb->Insert(*new TButtonGadget(CM_EDITUNDO, CM_EDITUNDO));
  143.     cb->Insert(*new TSeparatorGadget(6));
  144.     cb->Insert(*new TButtonGadget(CM_EDITFIND, CM_EDITFIND));
  145.     cb->Insert(*new TButtonGadget(CM_EDITFINDNEXT, CM_EDITFINDNEXT));
  146.     cb->Insert(*new TSeparatorGadget(6));
  147.     cb->Insert(*new TButtonGadget(CM_STREAMBLOCK, CM_STREAMBLOCK, TButtonGadget::Exclusive, TRUE, TButtonGadget::Down));
  148.     cb->Insert(*new TButtonGadget(CM_LINEBLOCK, CM_LINEBLOCK, TButtonGadget::Exclusive));
  149.     cb->Insert(*new TButtonGadget(CM_COLUMNBLOCK, CM_COLUMNBLOCK, TButtonGadget::Exclusive));
  150.     cb->Insert(*new TSeparatorGadget(6));
  151.     cb->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT));
  152.     cb->Insert(*new TButtonGadget(CM_FILEPRINTPREVIEW, CM_FILEPRINTPREVIEW));
  153.     cb->Insert(*new TSeparatorGadget(6));
  154.     cb->Insert(*new TButtonGadget(CM_HELPCONTENTS, CM_HELPCONTENTS));
  155.  
  156.     // Add fly-over help hints.
  157.     cb->SetHintMode(TGadgetWindow::EnterHints);
  158.  
  159.     frame->Insert(*cb, TDecoratedFrame::Top);
  160. }
  161.  
  162.  
  163. //////////////////////////////////////////////////////////
  164. // emeditApp
  165. // =====
  166. // Application intialization.
  167. //
  168. void emeditApp::InitMainWindow ()
  169. {
  170.     TDecoratedMDIFrame* frame = new TDecoratedMDIFrame(Name, MDI_MENU, *(new emeditMDIClient), TRUE);
  171.  
  172.     // Override the default window style for the main window.
  173.     frame->Attr.Style |= WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_THICKFRAME;
  174.     frame->Attr.Style &= ~(WS_CHILD);
  175.  
  176.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  177.  
  178.     //
  179.     // Assign ICON w/ this application.
  180.     //
  181.     frame->SetIcon(this, IDI_COFFEE);
  182.  
  183.     //
  184.     // Menu associated with window and accelerator table associated with table.
  185.     //
  186.     frame->AssignMenu(MDI_MENU);
  187.  
  188.     //
  189.     // Associate with the accelerator table.
  190.     //
  191.     frame->Attr.AccelTable = MDI_MENU;
  192.  
  193.  
  194.     SetupSpeedBar(frame);
  195.  
  196. //    TStatusBar& sb= new TStatusBar(frame, TGadget::Recessed, TStatusBar::Overtype );
  197.     frame->Insert(*(pStatusBar= new emeditMessageBar( frame )), TDecoratedFrame::Bottom);
  198.  
  199.     MainWindow = frame;
  200.  
  201. }
  202.  
  203.  
  204. //////////////////////////////////////////////////////////
  205. // emeditApp
  206. // =====
  207. // Menu Help Contents command
  208. void emeditApp::CmHelpContents ()
  209. {
  210.     //
  211.     // Show the help table of contents.
  212.     //
  213. //    HelpState = MainWindow->WinHelp(HelpFileName, HELP_CONTENTS, 0L);
  214. }
  215.  
  216.  
  217. //////////////////////////////////////////////////////////
  218. // emeditApp
  219. // =====
  220. // Menu Help Using Help command
  221. void emeditApp::CmHelpUsing ()
  222. {
  223.     //
  224.     // Display the contents of the Windows help file.
  225.     //
  226. //    HelpState = MainWindow->WinHelp(HelpFileName, HELP_HELPONHELP, 0L);
  227. }
  228.  
  229.  
  230. //////////////////////////////////////////////////////////
  231. // emeditApp
  232. // ===========
  233. // Menu Help About emedit.exe command
  234. void emeditApp::CmHelpAbout ()
  235. {
  236.     //
  237.     // Show the modal dialog.
  238.     //
  239.     emeditAboutDlg(MainWindow).Execute();
  240. }
  241.  
  242.  
  243. void emeditApp::InitInstance ()
  244. {
  245.     TApplication::InitInstance();
  246.  
  247.     // Accept files via drag/drop in the frame window.
  248.     MainWindow->DragAcceptFiles(TRUE);
  249.  
  250.     // Always set working directory to directory containing the application.
  251.     // ChDir App.Path
  252.  
  253.     //InitMDINOTE 'initialize data in MDINOTE.BAS
  254.  
  255.     TFrameWindow *tfw = TYPESAFE_DOWNCAST(MainWindow, TFrameWindow);
  256.     if (tfw) {
  257.       emeditMDIClient *theClient = TYPESAFE_DOWNCAST(tfw->GetClientWindow(), emeditMDIClient);
  258.  
  259.       string Cmd= lpCmdLine;
  260.       Cmd = Cmd.strip( string::Both );
  261.       if (Cmd.length()) { // If anything was specified on the command line...
  262.           size_t nNextToken;
  263.           do {
  264.               if ((nNextToken = Cmd.find( " " )) == NPOS) // find first blank, if any
  265.                 nNextToken = Cmd.length();
  266.  
  267.               string filename= Cmd.substr(0, nNextToken);
  268.               theClient->OpenFile( filename.c_str() ); // open the file
  269.  
  270.               Cmd = Cmd.substr(nNextToken).strip( string::Both ); // trim the filename off the command line
  271.           } while (Cmd.length()); // repeat while there are still filenames on the command line
  272.       }
  273.     }
  274. }
  275.  
  276.  
  277. void emeditApp::EvDropFiles (TDropInfo drop)
  278. {
  279.     // Number of files dropped.
  280.     int totalNumberOfFiles = drop.DragQueryFileCount();
  281.  
  282.     TFileList* files = new TFileList;
  283.  
  284.     for (int i = 0; i < totalNumberOfFiles; i++) {
  285.         // Tell DragQueryFile the file interested in (i) and the length of your buffer.
  286.         int     fileLength = drop.DragQueryFileNameLen(i) + 1;
  287.         char    *fileName = new char[fileLength];
  288.  
  289.         drop.DragQueryFile(i, fileName, fileLength);
  290.  
  291.         // Getting the file dropped. The location is relative to your client coordinates,
  292.         // and will have negative values if dropped in the non client parts of the window.
  293.         //
  294.         // DragQueryPoint copies that point where the file was dropped and returns whether
  295.         // or not the point is in the client area.  Regardless of whether or not the file
  296.         // is dropped in the client or non-client area of the window, you will still receive
  297.         // the file name.
  298.         TPoint  point;
  299.         BOOL    inClientArea = drop.DragQueryPoint(point);
  300.         files->Add(new TFileDrop(fileName, point, inClientArea, this));
  301.     }
  302.  
  303.     // Open the files that were dropped.
  304.     AddFiles(files);
  305.  
  306.     // Release the memory allocated for this handle with DragFinish.
  307.     drop.DragFinish();
  308. }
  309.  
  310.  
  311. void emeditApp::AddFiles (TFileList* files)
  312. {
  313.     // Open all files dragged in.
  314.     TFileListIter fileIter(*files);
  315.  
  316.     TFrameWindow *tfw = TYPESAFE_DOWNCAST(MainWindow, TFrameWindow);
  317.     if (tfw) {
  318.         emeditMDIClient *theClient = TYPESAFE_DOWNCAST(tfw->GetClientWindow(), emeditMDIClient);
  319.  
  320.         if (theClient)
  321.             while (fileIter) {
  322.                 theClient->OpenFile(fileIter.Current()->WhoAmI());
  323.                 fileIter++;
  324.             }
  325.     }
  326. }
  327.  
  328.  
  329. BOOL emeditApp::ProcessAppMsg (MSG& msg)
  330. {
  331. //    if (msg.message == WM_COMMAND) {
  332. //        if (ContextHelp || (GetKeyState(VK_F1) < 0)) {
  333. //            ContextHelp = FALSE;
  334. //            MainWindow->WinHelp(HelpFileName, HELP_CONTEXT, msg.wParam);
  335. //            return TRUE;
  336. //        }
  337. //    } else
  338.         switch (msg.message) {
  339.         case WM_KEYDOWN : {
  340.             switch (msg.wParam) {
  341.               case VK_INSERT : {
  342.                 const int ShiftDown= GetKeyState( VK_SHIFT ) & 0x8000;
  343.                 const int CtrlDown= GetKeyState( VK_CONTROL ) & 0x8000;
  344.                 const int AltDown= GetKeyState( VK_MENU ) & 0x8000;
  345.                 if (!ShiftDown  &&  !CtrlDown  &&  !AltDown) {
  346.                   const BOOL mode= !StatusBar()->GetInsertIndicator(); // new insert mode
  347.                   StatusBar()->SetInsertIndicator( mode ); // toggle insert mode
  348.  
  349.                   // loop through all the children and set the default block type
  350.                   TFrameWindow *tfw = TYPESAFE_DOWNCAST(MainWindow, TFrameWindow);
  351.                   emeditMDIClient *mdiclient = TYPESAFE_DOWNCAST(tfw->GetClientWindow(), emeditMDIClient);
  352.                   mdiclient->SetInsertMode( mode );
  353.                 }
  354.               } break;
  355.  
  356. //              case VK_F1:
  357. //                // If the Shift/F1 then set the help cursor and turn on the modal help state.
  358. //                if (::GetKeyState(VK_SHIFT) < 0) {
  359. //                    ContextHelp = TRUE;
  360. //                    HelpCursor = ::LoadCursor(MainWindow->GetModule()->GetInstance(), MAKEINTRESOURCE(IDC_HELPCURSOR));
  361. //                    ::SetCursor(HelpCursor);
  362. //                } else {
  363. //                    // If F1 w/o the Shift key then bring up help's main index.
  364. //                    MainWindow->WinHelp(HelpFileName, HELP_INDEX, 0L);
  365. //                }
  366. //                return TRUE;        // Gobble up the message.
  367. //
  368. //              case VK_ESCAPE : {
  369. //                if (ContextHelp) {
  370. //                    if (HelpCursor)
  371. //                        ::DestroyCursor(HelpCursor);
  372. //                    ContextHelp = FALSE;
  373. //                    HelpCursor = 0;
  374. //                    MainWindow->SetCursor(0, IDC_ARROW);
  375. //                    return TRUE;    // Gobble up the message.
  376. //                }
  377. //             } break;
  378.             };
  379.         } break;
  380.  
  381. //        case WM_MOUSEMOVE:
  382. //        case WM_NCMOUSEMOVE:
  383. //            if (ContextHelp) {
  384. //                ::SetCursor(HelpCursor);
  385. //                return TRUE;        // Gobble up the message.
  386. //            }
  387. //            break;
  388. //
  389. //        case WM_INITMENU:
  390. //            if (ContextHelp) {
  391. //                ::SetCursor(HelpCursor);
  392. //                return TRUE;        // Gobble up the message.
  393. //            }
  394. //            break;
  395. //
  396. //        case WM_ENTERIDLE:
  397. //            if (msg.wParam == MSGF_MENU)
  398. //                if (GetKeyState(VK_F1) < 0) {
  399. //                ContextHelp = TRUE;
  400. //                MainWindow->PostMessage(WM_KEYDOWN, VK_RETURN, 0L);
  401. //                return TRUE;       // Gobble up the message.
  402. //            }
  403. //            break;
  404.         };  // End of switch
  405.  
  406.     // Continue normal processing.
  407.     return TApplication::ProcessAppMsg(msg);
  408. }
  409.  
  410.  
  411. void emeditApp::EvWinIniChange (char far* section)
  412. {
  413.     if (lstrcmp(section, "windows") == 0) {
  414.         // If the device changed in the WIN.INI file then the printer
  415.         // might have changed.  If we have a TPrinter (Printer) then
  416.         // check and make sure it's identical to the current device
  417.         // entry in WIN.INI.
  418.         if (Printer) {
  419.             char printDBuffer[255];
  420.             LPSTR printDevice = printDBuffer;
  421.             LPSTR devName = 0;
  422.             LPSTR driverName = 0;
  423.             LPSTR outputName = 0;
  424.  
  425.             if (::GetProfileString("windows", "device", "", printDevice, sizeof(printDevice))) {
  426.                 // The string which should come back is something like:
  427.                 //
  428.                 //      HP LaserJet III,hppcl5a,LPT1:
  429.                 //
  430.                 // Where the format is:
  431.                 //
  432.                 //      devName,driverName,outputName
  433.                 //
  434.                 devName = printDevice;
  435.                 while (*printDevice) {
  436.                     if (*printDevice == ',') {
  437.                         *printDevice++ = 0;
  438.                         if (!driverName)
  439.                             driverName = printDevice;
  440.                         else
  441.                             outputName = printDevice;
  442.                     } else
  443.                         printDevice = AnsiNext(printDevice);
  444.                 }
  445.  
  446.                 if ((Printer->GetSetup().Error != 0)                                ||
  447.                     (lstrcmp(devName, Printer->GetSetup().GetDeviceName()) != 0)    ||
  448.                     (lstrcmp(driverName, Printer->GetSetup().GetDriverName()) != 0) ||
  449.                     (lstrcmp(outputName, Printer->GetSetup().GetOutputName()) != 0)) {
  450.  
  451.                     // New printer installed so get the new printer device now.
  452.                     delete Printer;
  453.                     Printer = new TPrinter;
  454.                 }
  455.             } else {
  456.                 // No printer installed (GetProfileString failed).
  457.                 delete Printer;
  458.                 Printer = new TPrinter;
  459.             }
  460.         }
  461.     }
  462. }
  463.  
  464.  
  465. int OwlMain (int , char* []) {
  466.     TBIVbxLibrary vbxLib;
  467.     emeditApp     App;
  468.  
  469.     return App.Run();
  470. }
  471.