home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / tutor / tcmdwin.cpp < prev    next >
C/C++ Source or Header  |  1998-07-03  |  13KB  |  356 lines

  1. //========================================================================
  2. //  tcmdwin.cpp:     Source file for tutorial cmdwin class
  3. //
  4. //      Copyright 1995,1996  Bruce E. Wampler. All rights reserved.
  5. //========================================================================
  6.  
  7. // This file contains the source code for a typical command window
  8. // derived from the vCmdWindow class. It will contain the definitions
  9. // of the menu bar and command and status bars. It represents the main
  10. // interaction point with the user.
  11. // 
  12. // We start out with the #includes needed to define this class plus
  13. // any V utility dialogs such as vNotice we use.
  14.  
  15. #include <v/vnotice.h>  // so we can use notice
  16. #include <v/vkeys.h>    // to map keys
  17. #include <v/vutil.h>    // for utilities
  18. #include <v/vfilesel.h> // for file select
  19.  
  20. #include "tcmdwin.h"            // our header file
  21.  
  22. // Now, we define static arrays for the menus, command bars, and
  23. // status bars used by this window. This consists of defining the
  24. // constants needed for IDs, followed by the static declarations of
  25. // the menu and command arrays. Note that V predefines quite a few
  26. // standard IDs which you can use instead of defining your own.
  27.  
  28.   // Start ID defines for the main window at 100
  29.  
  30.   enum {m_CheckMe = 100,    // for CheckMe command
  31.         m_CopySens,        // for Set Copy Sensitive
  32.         m_Dialog,        // to pop up the dialog
  33.     m_ModalDialog,        // for modal dialog
  34.     m_Clear};        // Clear screen
  35.  
  36. // Now, the static declarations of the menu arrays. You first define
  37. // the pulldown menus, one for each main menu bar label.
  38.  
  39.     static vMenu FileMenu[] =    // Items for File menu
  40.       {
  41.         {"New",M_New,isSens,notChk,noKeyLbl,noKey,noSub},
  42.         {"Open",M_Open,isSens,notChk,noKeyLbl,noKey,noSub},
  43.         {"Save",M_Save,notSens,notChk,noKeyLbl,noKey,noSub},
  44.         {"Save As",M_SaveAs,notSens,notChk,noKeyLbl,noKey,noSub},
  45. #ifdef vDEBUG                   // Defines V debug code
  46.         {"-",M_Line,notSens,notChk,noKeyLbl,noKey,noSub},
  47.         {"Debug",M_SetDebug,isSens,notChk,noKeyLbl,noKey,noSub},
  48. #endif
  49.         {"-",M_Line,notSens,notChk,noKeyLbl,noKey,noSub},
  50.         {"Exit",M_Exit,isSens,notChk,noKeyLbl,noKey,noSub},
  51.         {NULL}
  52.       };
  53.  
  54.     static vMenu EditMenu[] =    // Items for Edit menu
  55.       {
  56.         {"Cut",M_Cut,notSens,notChk,noKeyLbl,noKey,noSub},
  57.         {"Copy",M_Copy,notSens,notChk,noKeyLbl,noKey,noSub},
  58.         {"Paste",M_Paste,notSens,notChk,noKeyLbl,noKey,noSub},
  59.         {NULL}
  60.       };
  61.  
  62.     static vMenu TestMenu[] =   // Items for Test menu
  63.       {
  64.         {"CheckMe",m_CheckMe,isSens,notChk,noKeyLbl,
  65.             noKey,noSub},
  66.         {"Copy Sensitive",m_CopySens,isSens,notChk,noKeyLbl,
  67.             noKey,noSub},
  68.         {"Dialog",m_Dialog,isSens,notChk,noKeyLbl,
  69.             noKey,noSub},
  70.         {"Modal Dialog",m_ModalDialog,isSens,notChk,noKeyLbl,
  71.             noKey,noSub},
  72.         {NULL}
  73.       };
  74.  
  75.     // Now, define the items on the menu bar
  76.  
  77.     vMenu StandardMenu[] =     // The menu bar with three items
  78.       {
  79.         {"File",M_File,isSens,notUsed,notUsed,noKey,&FileMenu[0]},
  80.         {"Edit",M_Edit,isSens,notUsed,notUsed,noKey,&EditMenu[0]},
  81.         {"Test",M_Test,isSens,notUsed,notUsed,noKey,&TestMenu[0]},
  82.         {NULL}
  83.       };
  84.  
  85. // We now define a command bar. Command bars are optional, and there
  86. // may be more than one. You can place any CommandObject you want on a
  87. // command bar.
  88.  
  89.     static CommandObject CommandBar[] =  // A simple command bar
  90.       {
  91.         {C_Label,999,0 ,"Command Bar",NoList,CA_None,
  92.             isSens,NoFrame,0,0},
  93.         {C_Button,M_Copy,M_Copy,"Copy",NoList,CA_None,
  94.             notSens,NoFrame,0,0},
  95.         {C_Button,m_Dialog,m_Dialog,"Dialog",NoList,CA_None,
  96.             isSens,NoFrame,0,0},
  97.         {C_Button,m_Clear,m_Clear,"Clear",NoList,CA_None,
  98.             isSens,NoFrame,0,0},
  99.         {C_Button,M_Exit,M_Exit,"Exit",NoList,CA_None,
  100.             isSens,NoFrame,0,0},
  101.         {C_EndOfList,0,0,0,0,CA_None,0,0,0}  // This ends list
  102.       };
  103.  
  104. // Sometimes it is easier to define IDs near the definition of
  105. // the dialog or status bar using them.
  106.  
  107.   enum {m_cmdMsg = 110, m_cmdCount, m_keyMsg, m_keyVal};
  108.  
  109.     static vStatus StatBar[] =    // Define a simple status bar
  110.       {
  111.         {"Commands issued: ",m_cmdMsg,CA_NoBorder,isSens,0},
  112.         {" ",m_cmdCount,CA_None,isSens,0},
  113.         {"Last keypress: ",m_keyMsg,CA_NoBorder,isSens,0},
  114.         {"   ",m_keyVal,CA_None,isSens,0},
  115.         {0,0,0,0,0}           // This ends list
  116.       };
  117.  
  118.     static int copy_sens = 0;   // local for tracking sensitive
  119.  
  120. //======================>>> tCmdWindow::tCmdWindow <<<====================
  121.   tCmdWindow::tCmdWindow(char* name, int width, int height) :
  122.     vCmdWindow(name, width, height)
  123.   {
  124.     // This is the constructor for tCmdWindow. 
  125.     
  126.     UserDebug1(Constructor,"tCmdWindow::tCmdWindow(%s) Constructor\n",name)
  127.  
  128.     // The "Standard" window will consist of a menubar, a canvas, an
  129.     // optional button bar, and an optional status bar.
  130.     // 
  131.     // First, create and add the proper panes to the CmdWindow
  132.     // Note: there must be a corresponding delete in the destructor
  133.  
  134.     // Create and add the standard Menu Bar to this window
  135.     myMenu = new vMenuPane(StandardMenu);
  136.     AddPane(myMenu);
  137.  
  138.     // Create and add the command pane to this window
  139.     myCmdPane = new vCommandPane(CommandBar);
  140.     AddPane(myCmdPane);
  141.  
  142.     // Create and add our Canvas pane to this window
  143.     myCanvas = new tCanvasPane;
  144.     AddPane(myCanvas);
  145.  
  146.     // Create and add the Status Bar to this window
  147.     myStatus = new vStatusPane(StatBar);
  148.     AddPane(myStatus);
  149.  
  150.     // In the V model, a window may have dialogs. Each dialog used
  151.     // by a window must have an instance pointer. The easiest way
  152.     // to create dialogs is to construct each one using a new here
  153.     // which only defines the dialog - you need to use its
  154.     // ShowDialog method at the appropriate time to display it).
  155.     // You delete dialogs in the destructor for this window.
  156.     // 
  157.     // Now, create whatever dialogs this app defines:
  158.     // instances of tDialog and tModalDialog
  159.  
  160.     sampleDialog = new tDialog(this);
  161.     sampleModalDialog = new tModalDialog(this);
  162.  
  163.     // FINALLY, after all the panes have been constructed and
  164.     // added, we must show the window!
  165.  
  166.     ShowWindow();
  167.   }
  168.  
  169. //=====================>>> tCmdWindow::~tCmdWindow <<<====================
  170.   tCmdWindow::~tCmdWindow()
  171.   {
  172.     UserDebug(Destructor,"tCmdWindow::~tCmdWindow() destructor\n")
  173.  
  174.     // Now put a delete for each new in the constructor.
  175.  
  176.     delete myMenu;
  177.     delete myCanvas;
  178.     delete myStatus;
  179.     delete myCmdPane;
  180.     delete sampleDialog;
  181.     delete sampleModalDialog;
  182.   }
  183.  
  184. //========================>>> tCmdWindow::KeyIn <<<=======================
  185.   void tCmdWindow::KeyIn(vKey keysym, unsigned int shift)
  186.   {
  187.     // Keystrokes are routed to this window. This example code shows very
  188.     // simple processing of keystrokes, and how to update the m_keyVal
  189.     // field of the status bar.
  190.  
  191.     static char ctrl[] = "^X ";
  192.     static char chr[] = " X ";
  193.  
  194.     if (vk_IsModifier(keysym))
  195.         SetString(m_keyVal, "mod");     // change status bar
  196.     else if (keysym < ' ')              // ctrl char
  197.       {
  198.         ctrl[1] = keysym + '@';
  199.         SetString(m_keyVal, ctrl);      // change status bar
  200.       }
  201.     else if (keysym < 128)              // normal printable char
  202.       {
  203.         chr[1] = keysym;
  204.         SetString(m_keyVal, chr);       // change status bar
  205.       }
  206.     else 
  207.         SetString(m_keyVal, "+++");     // change status bar
  208.   }
  209.  
  210. //====================>>> tCmdWindow::WindowCommand <<<===================
  211.   void tCmdWindow::WindowCommand(ItemVal id, ItemVal val, CmdType cType)
  212.   {
  213.     // All commands generated from this window's menus and dialog bars
  214.     // are routed through here.  The easiest way to handle commands is to
  215.     // use a single, sometimes large switch. Each time you add a command
  216.     // to a menu or command bar, add a case to the switch here. In this
  217.     // example, we will use the V Notice dialog to indicate commands have
  218.     // been entered.
  219.  
  220.     static int cmdCount = 0;    // Used for sample status update
  221.     vNoticeDialog note(this);   // Used for default actions
  222.     char buff[20];              // buffer for status bar
  223.  
  224.     ++cmdCount;                 // count commands that have been issued
  225.     IntToStr(cmdCount,buff);    // Use V utility routine to get string
  226.     SetString(m_cmdCount, buff);        // change status bar
  227.  
  228.     UserDebug1(CmdEvents,"tCmdWindow:WindowCommand(%d)\n",id)
  229.  
  230.     switch (id)                 // The main switch to handle commands
  231.       {
  232.         // File Menu commands ------------------------------------
  233.  
  234.         case M_New:             // For this example, we will open a
  235.           {                     // new window using our NewAppWin.
  236.             (void)theApp->NewAppWin(0,"",250, 100);
  237.             break;
  238.           }
  239.  
  240.         case M_Open:            // This demos vFileSelect dialog
  241.           {
  242.             char name[100] = "";        // start out with null name
  243.             vFileSelect fsel(this);     // an instance of vFileSelect
  244.         int fI = 0;            // Filter index
  245.             static char* filter[] = {   // Filter for file select
  246.                 "*", "*.txt", "*.c *.cpp *.cxx *.h", 0 };
  247.  
  248.             // Show the file select dialog
  249.             int ans = fsel.FileSelect("Open file",name,99,filter,fI);
  250.  
  251.             if (ans && *name)   // User picked a file name
  252.               {
  253.                 SetTitle(name); // Set title of window to name
  254.                 note.Notice(name);  // Show the name
  255.               }
  256.             else                // Notify no name selected
  257.                 note.Notice("No file name selected.");
  258.         break;
  259.           }
  260.  
  261.         case M_Save:            // This would usually save a file
  262.           {
  263.             note.Notice("Save");
  264.             break;
  265.           }
  266.  
  267.         case M_SaveAs:          // Save to a specified name
  268.           {
  269.             note.Notice("Save As");
  270.             break;
  271.           }
  272.  
  273. #ifdef vDEBUG                   // Include debugging like this
  274.         case M_SetDebug:
  275.           {
  276.             vDebugDialog debug(this);   // an instance of debug 
  277.             debug.SetDebug();           // dialog - let user set
  278.             break;
  279.           }
  280. #endif
  281.  
  282.         case M_Exit:            // Standard exit command
  283.           {                     // Invoke the standard app Exit
  284.             theApp->Exit();     // to close all windows
  285.             break;              // will never get here
  286.           }
  287.  
  288.         // Edit Menu commands ------------------------------------
  289.         case M_Cut:             // Standard items for Edit menu
  290.           {
  291.             note.Notice("Cut");
  292.             break;
  293.           }
  294.  
  295.         case M_Copy:
  296.           {
  297.             note.Notice("Copy");
  298.             break;
  299.           }
  300.  
  301.         case M_Paste:
  302.           {
  303.             note.Notice("Paste");
  304.             break;
  305.           }
  306.  
  307.         // Test Menu commands ------------------------------------
  308.         case m_CheckMe:         // Demonstrate using a checked menu
  309.           {
  310.             ItemVal curval = GetValue(id); // Get current status
  311.             SetValue(m_CheckMe,!curval,Checked); // Toggle check
  312.  
  313.             if (curval)                 // Change menu label
  314.                 SetString(m_CheckMe,"Check Me");
  315.             else
  316.                 SetString(m_CheckMe,"UnChk Me");
  317.             break;
  318.           }
  319.  
  320.         case m_CopySens:        // Demo changing sensitivity
  321.           {
  322.             copy_sens = !copy_sens;     // toggle
  323.             // This will change both menu and command button
  324.             SetValue(M_Copy,copy_sens,Sensitive);
  325.             break;
  326.           }
  327.  
  328.         case m_Dialog:          // Invoke our dialog
  329.           {
  330.             if (!sampleDialog->IsDisplayed())   // not twice!
  331.                 sampleDialog->ShowDialog("Sample Modeless Dialog");
  332.             break;
  333.           }
  334.  
  335.         case m_ModalDialog:     // Invoke our modal dialog
  336.           {
  337.             ItemVal val, id;
  338.             id = sampleModalDialog->ShowModalDialog("Sample Modal",val);
  339.             // Now do something useful with id and val ...
  340.             break;
  341.           }
  342.  
  343.         case m_Clear:           // Clear the canvas
  344.           {
  345.             myCanvas->Clear();  // Invoke the canvas Clear
  346.             break;
  347.           }
  348.  
  349.         default:                // route unhandled commands up
  350.           {                     // to superclass
  351.             vCmdWindow::WindowCommand(id, val, cType);
  352.             break;
  353.           }
  354.       }
  355.   }
  356.