home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / krcls012.zip / KrClass / include / krwc.hpp < prev   
C/C++ Source or Header  |  1997-02-25  |  12KB  |  267 lines

  1. // Kroni's Classes: objects for user communication using windows
  2. // (c) 1997 Wolfgang Kronberg
  3. // file: krwc.hpp
  4.  
  5. // **********************************************************************************************************
  6. //
  7. // defines these classes:
  8. //
  9. //   KrBitfieldData          Data structure with options each of which may be set or unset
  10. //   KrChoiceData            Data structure with options of which exactly one is set
  11. //   KrBitfield              For easy handling of KrBitfieldData
  12. //   KrChoice                For easy handling of KrChoiceData
  13. //   KrUserDataField         Virtual class to allow >>-like input for any data structure
  14. //   KrWinComm               Allow >>-like input by presenting the user a dialog window
  15. //
  16. // defines overloads of these functions:
  17. //
  18. //   <<                      Add prompt lines to KrWinComm dialog window
  19. //   >>                      Input data from KrWinComm dialog window
  20. //   display                 Actually display the KrWinComm dialog window
  21. //
  22. // defines those global symbols for private use:
  23. //
  24. //   _KrChoiceDataEntry
  25. //   _KrBitfieldDataEntry
  26. //   _KrWinCommDataField
  27. //   key ()
  28. //
  29. // **********************************************************************************************************
  30.  
  31.  
  32. #ifndef __KRWC_HPP__
  33. #define __KRWC_HPP__
  34.  
  35.  
  36. #include <iksset.h>                              // Key sorted set
  37.  
  38. #include <istring.hpp>                           // IString
  39. #include <iframe.hpp>                            // IFrameWindow
  40. #include <imcelcv.hpp>                           // IMultiCellCanvas
  41. #include <ititle.hpp>                            // ITitle
  42. #include <ifont.hpp>                             // IFont
  43.  
  44.  
  45.  
  46. class _KrChoiceDataEntry                         // Represents one radio button
  47. {
  48. public:
  49.   _KrChoiceDataEntry (int aId, IString aText = IString())
  50.     {id = aId; text = aText;};
  51.   int id;
  52.   IString text;
  53. };
  54.  
  55.  
  56. class _KrBitfieldDataEntry                       // Represents one check box
  57. {
  58. public:
  59.   _KrBitfieldDataEntry (int aId, IString aText = IString(), Boolean aChosen = false)
  60.     {id = aId; text = aText; chosen = aChosen;};
  61.   int id;
  62.   IString text;
  63.   Boolean chosen;
  64. };
  65.  
  66.  
  67. int const & key (_KrChoiceDataEntry const & t);  // These are required for IKeySortedSet
  68. int const & key (_KrBitfieldDataEntry const & t);
  69.  
  70. typedef IKeySortedSet <_KrChoiceDataEntry,int> KrChoiceData;
  71.                                                  // Defines sets of KrChoiceData elements
  72. typedef IKeySortedSet <_KrBitfieldDataEntry,int> KrBitfieldData;
  73.                                                  // Defines sets of KrBitfieldData elements
  74.  
  75.  
  76. class KrChoice : public IBase                    // Represents a collection of radio buttons
  77. {
  78.  
  79. public:
  80.  
  81.   KrChoice ();                                   // Default constructor
  82.   KrChoice (const KrChoiceData & aData);         // Constructor from data field
  83.  
  84.   int add (int id, const IString & newChoice);   // Adds a new choice, returns its ID or zero if id invalid
  85.   int add (const IString & newChoice);           // Adds a new choice, returns its ID
  86.   void set (int id);                             // Set choice #id active
  87.   int get ();                                    // Get active ID
  88.  
  89.   int numberOfEntries ();                        // How many times was "add" called?
  90.  
  91.   int getFirstKey ();                            // Gets the ID of the first radio button
  92.   int getNextKey (int id);                       // Gets the ID of the next radio button
  93.   IString & getText (int id);                    // Gets the next beneath radio button # "id"
  94.  
  95. private:
  96.  
  97.   int nextFree ();                               // Gets a free ID
  98.   int active;                                    // Active choice
  99.   KrChoiceData data;                             // Contains all added choices
  100.  
  101. };
  102.  
  103.  
  104. class KrBitfield : public IBase                  // Represents a collection of check boxes
  105. {
  106.  
  107. public:
  108.  
  109.   KrBitfield () {};                              // Default constructor
  110.   KrBitfield (const KrBitfieldData & aData);     // Constructor from data field
  111.  
  112.   int add (int id, const IString & newEntry, Boolean checked = false);
  113.                                                  // Adds a new entry, returns its ID or zero if id invalid
  114.   int add (const IString & newEntry, Boolean checked = false);
  115.                                                  // Adds a new entry, returns its ID
  116.   Boolean toggle (int id, Boolean check);        // Check or uncheck entry #id, return new check status
  117.   Boolean toggle (int id);                       // Reverse check status of entry #id, return it
  118.   Boolean isChecked (int id);                    // Is entry checked?
  119.  
  120.   int numberOfEntries ();                        // How many times was "add" called?
  121.  
  122.   int getFirstKey ();                            // Gets the ID of the first check box
  123.   int getNextKey (int id);                       // Gets the ID of the next check box
  124.   IString & getText (int id);                    // Gets the next beneath check box # "id"
  125.  
  126. private:
  127.  
  128.   int nextFree ();                               // Gets a free ID
  129.   KrBitfieldData data;                           // Contains all added entries
  130.  
  131. };
  132.  
  133.  
  134. class KrUserDataField : public IBase
  135. {
  136. public:
  137.   virtual Boolean transform (Boolean doIt) = 0;  // Transforms the user input to the actual data structure
  138.                                                  //   which is to be input, if doIt = true. Otherwise, only
  139.                                                  //   says if that transformation *would* be successful.
  140.   virtual int initialize (IMultiCellCanvas & c, int start) = 0;
  141.                                                  // Performs all actions neccessary to initialize the
  142.                                                  //   user input window. This is called by
  143.                                                  //   KrWindowedCommunication::setEntryField().
  144.                                                  //   The input window should be placed in the canvas c,
  145.                                                  //   starting at row "start". The first unused row should
  146.                                                  //   be returned. Use columns 2-5 for framed groups and
  147.                                                  //   columns 3-4 otherwise.
  148. };
  149.  
  150.  
  151. typedef Boolean (* KrWinCommTranslator) (Boolean doIt, void * data, void * window);
  152.                                                  // Translates window -> data.
  153.                                                  //   return value (0/1) = success (y/n)
  154.                                                  //   doIt = false: don't translate, just test user data
  155.                                                  //   in "window": is it valid for translation?
  156.  
  157. class _KrWinCommDataField                         // Internal data structure for KrWinComm
  158. {
  159. public:
  160.   _KrWinCommDataField (int i, const IString & l, void * d, void * w,
  161.       KrWinCommTranslator t)
  162.     {id = i; label = l; data = d; window = w; translator = t;};
  163.   IString label;
  164.   void * data;
  165.   void * window;
  166.   KrWinCommTranslator translator;
  167.   int id;
  168. };
  169.  
  170.  
  171. int const & key (_KrWinCommDataField const & t); // Required for IKeySortedSet
  172. typedef IKeySortedSet <_KrWinCommDataField,int> KrWinCommData;
  173.                                                  // Collection of KrWinCommDataField objects
  174.  
  175. class KrWinComm : public IBase                   // Display and evaluate a dialog
  176. {
  177.  
  178. public:
  179.  
  180.   KrWinComm (const IString & aTitle = "Dialog"); // Constructor. Sets the Window Title.
  181.   ~KrWinComm ();                                 // Destructor
  182.  
  183.   void setNextPrompt (const IString & title);    // Sets an introducting line for the next input
  184.   void addToNextPrompt (const IString & title);  // Appends to the introducting line for the next input
  185.   void setTitle (const IString & aTitle);        // Sets the dialog title
  186.   void setFont (const IFont & aFont);            // Sets the dialog font
  187.  
  188.   void addEntryField (IString & s);              // Inputs data from the user. These are all input
  189.   void addEntryField (double & d);               //   as IString and converted internally.
  190.   void addEntryField (unsigned long & l);
  191.   void addEntryField (signed long & l);
  192.  
  193.   void addEntryField (KrBitfield & b);           // These are input as checkboxes / radio buttons.
  194.   void addEntryField (KrChoice & c);             //   NOTE: do not use b's or c's "add" functions
  195.                                                  //   after calling these methods (or you'll most
  196.                                                  //   likely get a sys3175 after closing the dialog)!
  197.  
  198.   void addEntryField (KrUserDataField & f);      // This lets the user define any other type of variable
  199.                                                  //   to input.
  200.  
  201.   void addEntryField (KrUserDataField * f);      // This can be used to input even predefined types that
  202.                                                  //   are none of the above and that do not inherit
  203.                                                  //   from KrUserDataField
  204.  
  205.   Boolean ok ();                                 // Did the user press OK?
  206.   Boolean run ();                                // Display the dialog and wait for user input
  207.                                                  //   returnes: did the user press OK?
  208. private:
  209.  
  210.   typedef struct typeExitList {
  211.     KrUserDataField * entry;
  212.     typeExitList * next;
  213.   } _tagTypeExitList;
  214.  
  215.   int nextFree ();
  216.  
  217.   void winInit ();                               // Does the initialization for window elements
  218.   Boolean callWinInit;                           // must winInit still be called?
  219.  
  220.   static Boolean translateString (Boolean doIt, void * data, void * window);
  221.   static Boolean translateDouble (Boolean doIt, void * data, void * window);
  222.   static Boolean translateUnsignedLong (Boolean doIt, void * data, void * window);
  223.   static Boolean translateSignedLong (Boolean doIt, void * data, void * window);
  224.   static Boolean translateBitfield (Boolean doIt, void * data, void * window);
  225.   static Boolean translateChoice (Boolean doIt, void * data, void * window);
  226.   static Boolean translateUserData (Boolean doIt, void * data, void * window);
  227.  
  228.   typeExitList * exitList;
  229.   IString currentLabel;
  230.   int currentLine;
  231.   IMultiCellCanvas * canvas;
  232.   IFrameWindow * frame;
  233.   ITitle * title;
  234.   IString titleName;
  235.   IMultiCellCanvas * buttons;
  236.   Boolean okPressed;
  237.   Boolean errorOccurred;
  238.   KrWinCommData data;
  239.   IFont font;
  240.  
  241.   friend class _KrWcHandler;                     // Defined elsewhere; handles dialog message queue
  242. };
  243.  
  244.  
  245. KrWinComm & operator << (KrWinComm & aWinComm, const IString & aString);
  246.                                                  // Operator access for KrWinComm::addToNextPrompt ()
  247.  
  248. KrWinComm & operator >> (KrWinComm & aWinComm, IString & aString);
  249.                                                  // Operator access for KrWinComm::addEntryField (...)
  250. KrWinComm & operator >> (KrWinComm & aWinComm, double & aDouble);
  251. KrWinComm & operator >> (KrWinComm & aWinComm, unsigned long & aUnsigned);
  252. KrWinComm & operator >> (KrWinComm & aWinComm, signed long & aSigned);
  253. KrWinComm & operator >> (KrWinComm & aWinComm, KrBitfield & aBitfield);
  254. KrWinComm & operator >> (KrWinComm & aWinComm, KrChoice & aChoice);
  255. KrWinComm & operator >> (KrWinComm & aWinComm, KrUserDataField & aUserData);
  256. KrWinComm & operator >> (KrWinComm & aWinComm, KrUserDataField * aUserData);
  257.  
  258. KrWinComm & display (KrWinComm & aWinComm);      // Operator access for KrWinComm.run ()
  259.  
  260. KrWinComm & operator >> (KrWinComm & aWinComm, KrWinComm & (*f) (KrWinComm&) );
  261.                                                  // These two are needed for display ()
  262. KrWinComm & operator << (KrWinComm & aWinComm, KrWinComm & (*f) (KrWinComm&) );
  263.  
  264.  
  265. #endif
  266.  
  267.