home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / windows / intervie / 3614 < prev    next >
Encoding:
Text File  |  1993-01-22  |  3.6 KB  |  110 lines

  1. Newsgroups: comp.windows.interviews
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!att!cbnewsc!cbfsb!cbnewsf.cb.att.com!dac
  3. From: dac@cbnewsf.cb.att.com (david.a.copperman)
  4. Subject: IV code fragment
  5. Message-ID: <1993Jan22.135848.14542@cbfsb.cb.att.com>
  6. Keywords: InputHandler
  7. Sender: news@cbfsb.cb.att.com
  8. Organization: AT&T
  9. Date: Fri, 22 Jan 1993 13:58:48 GMT
  10. Lines: 98
  11.  
  12. Recently Paul Shannon (pshannon@nrao.edu) posted a program that he had
  13. written as an InterViews learning tool.  I am climbing that learning tree
  14. myself, and am finding it extremely slow going.  Paul's code has been
  15. very helpful, so I thought I'd follow suit and post something (much smaller)
  16. for discussion.
  17.  
  18. Coming out of the Xt world, I'm used to the generic event handler,
  19. which can be applied to any widget and which carries, optionally, an
  20. application-supplied value, providing context for the routine.  So I
  21. began to investigate a way to create a class that could be generalized
  22. for this.  (One person suggested an extension of the Callback macro,
  23. as an alternative to what I'v come up with.)
  24.  
  25. The code below defines a class derived from InputHandler.  While the code
  26. here is fairly trivial, you can see that this class can be readily designed
  27. as a base for numerous other classes corresponding to different widgets
  28. or whatever.  I wrote it to demonstrate to myself that I could write
  29. event handlers that would have access to the Event class, which I need for a 
  30. so I'll know which mouse button is in play, while still controlling widget
  31. behavior where I deemed it important.  (For example, I want to control the
  32. mapping/unmapping of all popups myself, and while they are visible, the
  33. button that pops them up is pushed in, and is released only when they pop down.)
  34.  
  35. In this code I create a button using WidgetKit::push_button, and retain
  36. the result pointer to be able to call Button::press.  I've been told it's
  37. more intuitively reasonable to derive this from the Button class than
  38. from InputHandler, but I've been unable to derive a Button that gives
  39. me the same look and feel as WidgetKit::push_button and that also calls
  40. Button::press successfully.
  41.  
  42. I hope this code fragment is interesting for some, and perhaps a little
  43. provocative.  For any IV guru reading this - are you offended by this
  44. buttonHandler class?  Have I violated some important precept in IV
  45. programming conventions?  I'd be interested in any and all comments.
  46.  
  47. Dave Copperman
  48. dac@hoqaa.att.com
  49.  
  50.  
  51. #include <IV-look/kit.h>
  52. #include <InterViews/background.h>
  53. #include <InterViews/button.h>
  54. #include <InterViews/event.h>
  55. #include <InterViews/layout.h>
  56. #include <InterViews/session.h>
  57. #include <InterViews/window.h>
  58. #include <stdio.h>
  59.  
  60. static WidgetKit* kit;
  61. static Style* kitstyle;
  62. static Window* win;
  63.  
  64. class buttonHandler : public InputHandler {
  65. private:
  66.     Button* _button;
  67. public:
  68.     buttonHandler :: buttonHandler (const char*);
  69.     void press (const Event& event);
  70. };
  71.  
  72. buttonHandler :: buttonHandler (const char* label) :
  73.     InputHandler (_button = kit->push_button (label, nil), kitstyle)
  74. {
  75. }
  76.  
  77. /*
  78.  * Toggle button position to show explicit control.
  79.  */
  80. void buttonHandler :: press (const Event& event)
  81. {
  82.     static int up = 1;
  83.  
  84.     if (event.pointer_button () != Event::left)
  85.         return;
  86.     if (up) {
  87.         _button->press (event);
  88.         up = 0;
  89.     }
  90.     else {
  91.         _button->release (event);
  92.         up = 1;
  93.     }
  94. }
  95.  
  96. main (int argc, char** argv) {
  97.     Session* session = new Session ("Himom", argc, argv);
  98.     kit = WidgetKit::instance();
  99.     kitstyle = kit->style ();
  100.     const LayoutKit& layout = *LayoutKit::instance();
  101.     buttonHandler *handler = new buttonHandler ("  Push Me  ");
  102.     win = new ApplicationWindow(
  103.         kit->inset_frame (
  104.         layout.margin (handler, 10.0)
  105.         )
  106.     );
  107.  
  108.     session->run_window (win);
  109. }
  110.