home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CMDENABL.PAK / CMDENAB2.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  4KB  |  160 lines

  1. //----------------------------------------------------------------------------
  2. //  ObjectWindows - (C) Copyright 1994 by Borland International
  3. //    Command-enabling sample application
  4. //    Second version implements command enabling for file commands.
  5. //----------------------------------------------------------------------------
  6. #include <owl/owlpch.h>
  7. #include <owl/applicat.h>
  8. #include <owl/framewin.h>
  9. #include "cmdenabl.h"
  10.  
  11. //  Class definitions:
  12. //  TCmdEnableApp -- application object initializes TApplication, overrides
  13. //    InitMainWindow.
  14. class TCmdEnableApp : public TApplication
  15. {
  16.   public:
  17.     TCmdEnableApp() : TApplication() {}
  18.   protected:
  19.     void InitMainWindow();
  20. };
  21.  
  22. //  TCmdEnableWindow -- window object initializes TWindow, adds four event
  23. //    handlers, adds response table for four events.
  24. class TCmdEnableWindow : public TWindow {
  25.   public:
  26.     TCmdEnableWindow(TWindow* parent = 0);
  27.  
  28.   protected:
  29.     bool isDirty;
  30.     bool isNewFile;
  31.  
  32.     // Event handlers
  33.     void CmFileNew();
  34.     void CmFileOpen();
  35.     void CmFileSave();
  36.     void CmFileSaveAs();
  37.     void CmToggleDirty();
  38.     void CmToggleNew();
  39.     void CmShowState();
  40.  
  41.     // Command-enabling functions
  42.     void CeFileNew(TCommandEnabler&);
  43.     void CeFileOpen(TCommandEnabler&);
  44.     void CeFileSave(TCommandEnabler&);
  45.  
  46.     DECLARE_RESPONSE_TABLE(TCmdEnableWindow);
  47. };
  48.  
  49. DEFINE_RESPONSE_TABLE1(TCmdEnableWindow, TWindow)
  50.   EV_COMMAND(CM_FILENEW, CmFileNew),
  51.   EV_COMMAND(CM_FILEOPEN, CmFileOpen),
  52.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  53.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  54.   EV_COMMAND(CM_TOGGLEDIRTY, CmToggleDirty),
  55.   EV_COMMAND(CM_TOGGLENEW, CmToggleNew),
  56.   EV_COMMAND(CM_SHOWSTATE, CmShowState),
  57.   EV_COMMAND_ENABLE(CM_FILENEW, CeFileNew),
  58.   EV_COMMAND_ENABLE(CM_FILEOPEN, CeFileOpen),
  59.   EV_COMMAND_ENABLE(CM_FILESAVE, CeFileSave),
  60. END_RESPONSE_TABLE;
  61.  
  62. // Put the OwlMain here just to get it out of the way
  63. int OwlMain(int /*argc*/, char* /*argv*/ [])
  64. {
  65.   return TCmdEnableApp().Run();
  66. }
  67.  
  68. //  Class function definitions
  69. //  class TCmdEnableApp
  70. //  TCmdEnableApp::InitMainWindow
  71. void
  72. TCmdEnableApp::InitMainWindow()
  73. {
  74.   // Construct the frame window
  75.   TFrameWindow* frame = new TFrameWindow(0, "Command-enabling sample application", new TCmdEnableWindow, true);
  76.  
  77.   // Set the main window and its menu
  78.   SetMainWindow(frame);
  79.   GetMainWindow()->AssignMenu("COMMANDS");
  80. }
  81.  
  82. TCmdEnableWindow::TCmdEnableWindow(TWindow *parent) : TWindow(parent)
  83. {
  84.   isNewFile = true;
  85.   isDirty = false;
  86. }
  87.  
  88. void
  89. TCmdEnableWindow::CmFileNew()
  90. {
  91.   isDirty=false;
  92.   isNewFile=true;
  93.   MessageBox("Opened a new file.\nisDirty=false\nisNewFile=true", "File action taken");
  94. }
  95.  
  96. void
  97. TCmdEnableWindow::CmFileOpen()
  98. {
  99.   isDirty=false;
  100.   isNewFile=false;
  101.   MessageBox("Opened an existing file.\nisDirty=false\nisNewFile=false", "File action taken");
  102. }
  103.  
  104. void
  105. TCmdEnableWindow::CmFileSave()
  106. {
  107.   isDirty=false;
  108.   isNewFile=false;
  109.   MessageBox("Saved an existing file.\nisDirty=false\nisNewFile=false", "File action taken");
  110. }
  111.  
  112. void
  113. TCmdEnableWindow::CmFileSaveAs()
  114. {
  115.   isDirty=false;
  116.   isNewFile=false;
  117.   MessageBox("Saved a file under a new name.\nisDirty=false\nisNewFile=false", "File action taken");
  118. }
  119.  
  120. void
  121. TCmdEnableWindow::CmToggleDirty()
  122. {
  123.   isDirty=!isDirty;
  124. }
  125.  
  126. void
  127. TCmdEnableWindow::CmToggleNew()
  128. {
  129.   isNewFile=!isNewFile;
  130. }
  131.  
  132. void
  133. TCmdEnableWindow::CmShowState()
  134. {
  135.   string str(isDirty ? "isDirty = true\n" : "isDirty = false\n");
  136.   str += (isNewFile ? "isNewFile = true" : "isNewFile = false");
  137.   MessageBox(str.c_str(), "Current state");
  138. }
  139.  
  140. void
  141. TCmdEnableWindow::CeFileNew(TCommandEnabler& ce)
  142. {
  143.   // Enable CmFileNew if not dirty
  144.   ce.Enable(!isDirty);
  145. }
  146.  
  147. void
  148. TCmdEnableWindow::CeFileOpen(TCommandEnabler& ce)
  149. {
  150.   // Enable CmFileSave if not new file and is dirty.
  151.   ce.Enable(!isNewFile && isDirty);
  152. }
  153.  
  154. void
  155. TCmdEnableWindow::CeFileSave(TCommandEnabler& ce)
  156. {
  157.   // Enable CmFileSave if not new file
  158.   ce.Enable(!isNewFile);
  159. }
  160.