home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / demo / dialog.cpp < prev    next >
Text File  |  1997-02-24  |  5KB  |  106 lines

  1. // Kroni's Classes: Demonstration: Input dialog
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: dialog.cpp
  4.  
  5. // ************************************************************************* //
  6. // *                 Demonstration of Kroni's Classes                      * //
  7. // *                                                                       * //
  8. // *                           Input Dialog                                * //
  9. // *                                                                       * //
  10. // *  Demonstrated classes:                                                * //
  11. // *    KrAsyncMain                                                        * //
  12. // *    KrCommonTextOutput                                                 * //
  13. // *    KrWinComm                                                          * //
  14. // ************************************************************************* //
  15.  
  16.  
  17. #include "krcto.hpp"                             // Support to re-route cout to a window
  18. #include "krasyncm.hpp"                          // Support for asynchronous main function
  19. #include "krwc.hpp"                              // Support for dialog input (windowed communication)
  20.  
  21. #include <iframe.hpp>                            // IFrameWindow
  22.  
  23.  
  24. // First of all, all global window objects should be declared static and before all functions.
  25. //   This is the comon UICL way to define a frame window and its childs:
  26.  
  27. static IFrameWindow mainWindow ("Demonstration of Kroni's Classes: Dialog sample", 0x1000);
  28.                                                  // Our main window
  29. static KrCommonTextOutput text (IC_FRAME_CLIENT_ID, &mainWindow, &mainWindow);
  30.                                                  // This window will hold all text output, e.g. for use
  31.                                                  //   with common cout.
  32.  
  33.  
  34. // The object which take user input does not need to be defined here, but it's easier that way.
  35.  
  36. static KrWinComm wcin;                           // We could also give the dialog a title: ...wcin(title).
  37.  
  38.  
  39. // We must always derive a class from the virtual class KrAsyncMain in a standard way:
  40.  
  41. class MyMain : public KrAsyncMain
  42. {
  43. public:
  44.   MyMain (IWindow * mainWindow) : KrAsyncMain (mainWindow) {};
  45.                                                  // We must forward the original object's only constructor
  46.   virtual void main (IEvent & event);            // This will be our actual new main function
  47. };
  48.  
  49.  
  50. // Next define the asynchronous main function. Doing calculations here does not block message processing.
  51.  
  52. void MyMain::main (IEvent & event)               // Do in this effective main function whatever you want!
  53. {
  54.  
  55.   IString s;                                     // Two values we want the user to input.
  56.   double d;
  57.  
  58.   switch (event.parameter1())                    // Use this to define the different parts of your program
  59.      {
  60.      case cmdStartUp:                            // This part will be run when the program first comes up.
  61.  
  62.         cout = text.stream();                    // Assign the KrTextOutput object's stream to cout
  63.         cout.setf (ios::unitbuf);                // Unfortunately, the above line does not copy this property
  64.  
  65.         s = "Old value of the string";           // Since the user will be offered to use the old values,
  66.         d = 42;                                  //   those should be set to a sensible value.
  67.  
  68.         wcin << "Please enter a string here:" >> s;
  69.                                                  // Ask for a string with this text explaining what to do...
  70.         wcin << "...and a number here:" >> d     // ...and for a number.
  71.              << display;                         // The display manipulator will display all the
  72.                                                  //   queries we've made (in one dialog window)
  73.  
  74.         if (wcin.ok())                           // The result of the last dialog
  75.            cout << "You pressed OK.\n";
  76.         else
  77.            cout << "You aborted the dialog.\n";
  78.  
  79.         cout << "  s = " << s << "\n  d = " << d;
  80.  
  81.         break;                                   // Our effective main program ends here.
  82.      };
  83. };
  84.  
  85.  
  86. int main (int argc, char **argv, char **envv)    // This should include all initial window definitions;
  87. {                                                //   the other stuff should be in MyMain::main().
  88.   IApplication::current().setArgs(argc,argv);    // This is "good style" when using the UICL.
  89.                                                  //   Do always include this line.
  90.  
  91.   // First, we must make sure that our window is shown on the screen.
  92.   //   This is ordinary UICL code.
  93.  
  94.   mainWindow.setClient (&text);
  95.   mainWindow.sizeTo (ISize(500,300));
  96.   mainWindow.setFocus ();
  97.   mainWindow.show ();
  98.  
  99.   // These should always be the very last statements in the main() function:
  100.  
  101.   MyMain myMain (&mainWindow);                   // initializes the use of the function MyMain::main
  102.   IApplication::current().run();
  103.   return 0;
  104. };
  105.  
  106.