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

  1. // Kroni's Classes: Demonstration: OS/2 ini file access
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: ini.cpp
  4.  
  5. // ************************************************************************* //
  6. // *                 Demonstration of Kroni's Classes                      * //
  7. // *                                                                       * //
  8. // *                         ini file access                               * //
  9. // *                                                                       * //
  10. // *  Demonstrated classes:                                                * //
  11. // *    KrAsyncMain                                                        * //
  12. // *    KrCommonTextOutput                                                 * //
  13. // *    KrMenuBar                                                          * //
  14. // *    KrProfile                                                          * //
  15. // ************************************************************************* //
  16.  
  17.  
  18. #include "krcto.hpp"                             // Support to re-route cout to a window
  19. #include "krasyncm.hpp"                          // Support for asynchronous main function
  20. #include "krmenu.hpp"                            // Support for "correct" menu bar
  21. #include "krprof.hpp"                            // Support for ini file access
  22.  
  23. #include <iframe.hpp>                            // IFrameWindow
  24.  
  25.  
  26. // First of all, all global window objects should be declared static and before all functions.
  27. //   This is the comon UICL way to define a frame window and its childs:
  28.  
  29. static IFrameWindow mainWindow ("Demonstration of Kroni's Classes: Ini files", 0x1000);
  30.                                                  // Our main window
  31. static KrCommonTextOutput text (IC_FRAME_CLIENT_ID, &mainWindow, &mainWindow);
  32.                                                  // This window will hold all text output
  33. static KrMenuBar menu (&mainWindow);             // Our menu bar
  34.  
  35.  
  36. // We need to define a constant for each menu entry we are aplanning to create.
  37.  
  38. const static int menuWrite = KrAsyncMain::cmdUser + 1;
  39. const static int menuWriteData = KrAsyncMain::cmdUser + 2;
  40. const static int menuWritePicard = KrAsyncMain::cmdUser + 3;
  41. const static int menuDelete = KrAsyncMain::cmdUser + 4;
  42. const static int menuRead = KrAsyncMain::cmdUser + 5;
  43. const static int menuExit = KrAsyncMain::cmdUser + 6;
  44.  
  45.  
  46. // We must always derive a class from the virtual class KrAsyncMain in a standard way:
  47.  
  48. class MyMain : public KrAsyncMain
  49. {
  50. public:
  51.   MyMain (IWindow * mainWindow) : KrAsyncMain (mainWindow) {};
  52.                                                  // We must forward the original object's only constructor
  53.   virtual void main (IEvent & event);            // This will be our actual new main function
  54. };
  55.  
  56.  
  57. // Next define the asynchronous main function. Doing calculations here does not block message processing.
  58.  
  59. void MyMain::main (IEvent & event)               // Do in this effective main function whatever you want!
  60. {
  61.  
  62.   static KrProfile profile ("KrClassesDemoIni"); // For reading and writing anything which belongs to this
  63.                                                  //   application from/to the OS/2 user ini file.
  64.                                                  //   Be sure to use a name no-one else will choose!
  65.   IString s;
  66.  
  67.   switch (event.parameter1())                    // Use this to define the different parts of your program
  68.      {
  69.      case cmdStartUp:                            // This part will be run when the program first comes up.
  70.  
  71.         cout = text.stream();                    // Assign the KrTextOutput object's stream to cout
  72.         cout.setf (ios::unitbuf);                // Unfortunately, the above line does not copy this property
  73.  
  74.         break;                                   // Our effective main program ends here.
  75.  
  76.      case menuWriteData :                        // Just use menu item IDs in this place
  77.         profile.setKey ("Data");
  78.         profile << "I'm an android.";            // Writes this text to the "Data" section of the
  79.                                                  //   application's area in the ini file.
  80.         cout << "Written to profile, key ""Data"":\n  'I'm an android.'\n";
  81.         break;
  82.  
  83.      case menuWritePicard :
  84.         profile.setKey ("Picard");
  85.         profile << "Captain";
  86.         cout << "Written to profile, key ""Picard"":\n  'Captain'\n";
  87.         break;
  88.  
  89.      case menuDelete :
  90.         profile.removeApp();
  91.         profile << clear;                        // Or the previous data will still be in the buffer
  92.         cout << "All application data deleted.\n";
  93.         break;
  94.  
  95.      case menuRead :
  96.         profile.setKey ("Data");
  97.         profile >> s;                            // Careful: like in ordinary stream handling, this will
  98.                                                  //   just read one word and not the complete data!
  99.         cout << "Data section, first word: " << s << "\n";
  100.         profile.setKey ("Picard");
  101.         profile >> s;
  102.         cout << "Picard section, first word: " << s << "\n";
  103.         break;
  104.  
  105.      case menuExit :
  106.         mainWindow.close ();
  107.         break;
  108.  
  109.      };
  110. };
  111.  
  112.  
  113. int main (int argc, char **argv, char **envv)    // This should include all initial window definitions;
  114. {                                                //   the other stuff should be in MyMain::main().
  115.   IApplication::current().setArgs(argc,argv);    // This is "good style" when using the UICL.
  116.                                                  //   Do always include this line.
  117.  
  118.   // First, we must make sure that our window is shown on the screen.
  119.   //   This is ordinary UICL code.
  120.  
  121.   mainWindow.setClient (&text);
  122.   mainWindow.sizeTo (ISize(500,300));
  123.   mainWindow.setFocus ();
  124.   mainWindow.show ();
  125.  
  126.   // We now build up our menu
  127.  
  128.   menu.addText (menuWrite, "Write...");          // Our first menu entry...
  129.   menu.addSubmenu (menuWrite);                   // ...will be a pull-down menu
  130.   menu.addText (menuWriteData, "Data", menuWrite);
  131.                                                  // These entries belong to the submenu
  132.   menu.addText (menuWritePicard, "Picard", menuWrite);
  133.   menu.addSeparator (0, menuWrite);
  134.   menu.addText (menuDelete, "Delete", menuWrite);
  135.   menu.addText (menuRead, "Read");               // This does not belong to the submenu
  136.   menu.addText (menuExit, "Quit");
  137.  
  138.   // These should always be the very last statements in the main() function:
  139.  
  140.   MyMain myMain (&mainWindow);                   // initializes the use of the function MyMain::main
  141.   IApplication::current().run();
  142.   return 0;
  143. };
  144.  
  145.