home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / demo / dialog3.cpp < prev    next >
C/C++ Source or Header  |  1997-02-26  |  8KB  |  178 lines

  1. // Kroni's Classes: Demonstration: Input dialog with user defined classes
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: dialog3.cpp
  4.  
  5. // ************************************************************************* //
  6. // *                 Demonstration of Kroni's Classes                      * //
  7. // *                                                                       * //
  8. // *              Input Dialog with user defined classes                   * //
  9. // *                                                                       * //
  10. // *  Demonstrated classes:                                                * //
  11. // *    KrAsyncMain                                                        * //
  12. // *    KrCommonTextOutput                                                 * //
  13. // *    KrWinComm                                                          * //
  14. // *    KrBitfield                                                         * //
  15. // *    KrUserDataField                                                    * //
  16. // *    KrTrace                                                            * //
  17. // ************************************************************************* //
  18.  
  19.  
  20. #include "krcto.hpp"                             // Support to re-route cout to a window
  21. #include "krasyncm.hpp"                          // Support for asynchronous main function
  22. #include "krwc.hpp"                              // Support for dialog input (windowed communication)
  23. #include "krtrace.hpp"                           // Support for throwing & handling exceptions
  24.  
  25. #include <iframe.hpp>                            // IFrameWindow
  26. #include <icheckbx.hpp>                          // ICheckBox
  27.  
  28.  
  29. // First of all, all global window objects should be declared static and before all functions.
  30. //   This is the comon UICL way to define a frame window and its childs:
  31.  
  32. static IFrameWindow mainWindow ("Demonstration of Kroni's Classes: User Defined Dialog Classes", 0x1000);
  33.                                                  // Our main window
  34. static KrCommonTextOutput text (IC_FRAME_CLIENT_ID, &mainWindow, &mainWindow);
  35.                                                  // This window will hold all text output
  36.  
  37. // The object which take user input does not need to be defined here, but it's easier that way.
  38.  
  39. static KrWinComm wcin;
  40.  
  41.  
  42. // We must always derive a class from the virtual class KrAsyncMain in a standard way:
  43.  
  44. class MyMain : public KrAsyncMain
  45. {
  46. public:
  47.   MyMain (IWindow * mainWindow) : KrAsyncMain (mainWindow) {};
  48.                                                  // We must forward the original object's only constructor
  49.   virtual void main (IEvent & event);            // This will be our actual new main function
  50. };
  51.  
  52.  
  53. // Now let's get to work. We will define an input method for Boolean variables.
  54.  
  55. // First of all, we must derive a new class from KrUserDataField, and it must have a constructor
  56. //   which accepts a pointer to a Boolean.
  57.  
  58. class BooleanInput : public KrUserDataField
  59. {
  60.  
  61. public:
  62.  
  63.   BooleanInput (Boolean *aBool);                 // The aforementioned constructor
  64.   ~BooleanInput ();                              // To fight memory leaks
  65.  
  66.   virtual Boolean transform (Boolean doIt);      // These two virtual functions must always be defined.
  67.   virtual int initialize (IMultiCellCanvas & c, int start);
  68.  
  69. private:
  70.  
  71.   Boolean * bool;                                // A pointer to the boolean variable to be input
  72.   ICheckBox * checkBox;                          // We'll use this check box for user input
  73.  
  74. };
  75.  
  76. BooleanInput::BooleanInput (Boolean * aBool)     // This needs only to do basic initialization
  77. {
  78.   bool = aBool;                                  // Remember the pointer to the variable to be input
  79.   checkBox = 0;                                  // Simply to indicate that this has not yet been defined
  80. };
  81.  
  82. BooleanInput::~BooleanInput ()
  83. {
  84.   if (checkBox) delete checkBox;
  85. };
  86.  
  87. Boolean BooleanInput::transform (Boolean doIt)
  88. {
  89.   if (!doIt)                                     // doIt = false: only check for valid input
  90.      return true;                                // No check neccessary: user can't type in invalid input
  91.  
  92.   if (checkBox)                                  // This should always be the case.
  93.      {                                           // Replace (checkBox) by (!checkBox) to see the exception!
  94.      (*bool) = checkBox->isSelected ();          // Write the check box state into the variable
  95.      }
  96.   else
  97.      KRNAMEDTHROW (IException("CheckBox undefined!"),"Kroni's Classes Demo");
  98.                                                  // Throw an exception. Do the above replacement to see it.
  99.   return true;                                   // The input is valid.
  100. };
  101.  
  102. int BooleanInput::initialize (IMultiCellCanvas & c, int start)
  103. {
  104.   if (checkBox) delete checkBox;                 // Clean up if neccessary
  105.  
  106.   checkBox = new ICheckBox (0x1000+0x40*start, &c, &c);
  107.                                                  // Create the actual check box for the user input.
  108.                                                  // You may use this window ID and the next 0x3f here.
  109.   checkBox -> select (*bool);                    // Give the check box the right starting value
  110.   checkBox -> setText ("Yes/No");                // Give some name to the check box to ease clicking
  111.  
  112.   c.addToCell (checkBox,4,start);                // Column 4 is reserved for the actual entry fields.
  113.                                                  // "start" is the first row you may need.
  114.   return ++start;                                // We return the first un-occupied row.
  115. };
  116.  
  117.  
  118. // Now we must overload the >> operator to accept Boolean variables. This is absolutely similar
  119. //   for all types of variables.
  120.  
  121. KrWinComm & operator >> (KrWinComm & aWinComm, Boolean & aBool)
  122. {
  123.   BooleanInput * bi;
  124.   bi = new BooleanInput (&aBool);                // Create a new object which will survive the exit from
  125.                                                  //   this operator function
  126.   return (aWinComm >> bi);                       // Forward the stuff to this predifined operator
  127. };
  128.  
  129. // Now we are done defining a new type for input using KrWinComm!
  130.  
  131.  
  132. // Next define the asynchronous main function. Doing calculations here does not block message processing.
  133.  
  134. void MyMain::main (IEvent & event)               // Do in this effective main function whatever you want!
  135. {
  136.  
  137.   switch (event.parameter1())                    // Use this to define the different parts of your program
  138.      {
  139.      case cmdStartUp:                            // This part will be run when the program first comes up.
  140.  
  141.         cout = text.stream();                    // Assign the KrTextOutput object's stream to cout
  142.         cout.setf (ios::unitbuf);                // Unfortunately, the above line does not copy this property
  143.  
  144.         static Boolean b = true;                 // The variable we want to input
  145.  
  146.         wcin << "I like Star Trek!" >> b >> display;
  147.  
  148.         cout << "Your choice:\n";
  149.         cout << " You " << (b?"do":"don't") << " like Star Trek!\n";
  150.  
  151.         break;                                   // Our effective main program ends here.
  152.      };
  153. };
  154.  
  155.  
  156. int main (int argc, char **argv, char **envv)    // This should include all initial window definitions;
  157. {                                                //   the other stuff should be in MyMain::main().
  158.   IApplication::current().setArgs(argc,argv);    // This is "good style" when using the UICL.
  159.                                                  //   Do always include this line.
  160.   KrTrace::tellUser (KrTrace::full);             // Give the user full information in a popup window
  161.                                                  //   if any exception occurs!
  162.  
  163.   // First, we must make sure that our window is shown on the screen.
  164.   //   This is ordinary UICL code.
  165.  
  166.   mainWindow.setClient (&text);
  167.   mainWindow.sizeTo (ISize(500,300));
  168.   mainWindow.setFocus ();
  169.   mainWindow.show ();
  170.  
  171.   // These should always be the very last statements in the main() function:
  172.  
  173.   MyMain myMain (&mainWindow);                   // initializes the use of the function MyMain::main
  174.   IApplication::current().run();
  175.   return 0;
  176. };
  177.  
  178.