DragonOsman

GUI.h

Mar 17th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. //
  2. // This is a GUI support code to the chapters 12-16 of the book
  3. // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
  4. //
  5.  
  6. #ifndef GUI_GUARD
  7. #define GUI_GUARD
  8.  
  9. #include "Window.h"
  10. #include "Graph.h"
  11. #include <string>
  12.  
  13. namespace Graph_lib {
  14.  
  15.     //------------------------------------------------------------------------------
  16.  
  17.     typedef void* Address;    // Address is a synonym for void*
  18.     typedef void(*Callback)(Address, Address);    // FLTK's required function type for all callbacks
  19.  
  20.                                                   //------------------------------------------------------------------------------
  21.  
  22.     template<class W> W& reference_to(Address pw)
  23.         // treat an address as a reference to a W
  24.     {
  25.         return *static_cast<W*>(pw);
  26.     }
  27.  
  28.     //------------------------------------------------------------------------------
  29.  
  30.     class Widget {
  31.         // Widget is a handle to an Fl_widget - it is *not* an Fl_widget
  32.         // We try to keep our interface classes at arm's length from FLTK
  33.     public:
  34.         Widget(Point xy, int w, int h, const std::string& s, Callback cb)
  35.             : loc(xy), width(w), height(h), label(s), do_it(cb)
  36.         {}
  37.  
  38.         virtual void move(int dx, int dy) { hide(); pw->position(loc.x += dx, loc.y += dy); show(); }
  39.         virtual void hide() { pw->hide(); }
  40.         virtual void show() { pw->show(); }
  41.         virtual void attach(Window&) = 0;
  42.  
  43.         Point loc;
  44.         int width;
  45.         int height;
  46.         string label;
  47.         Callback do_it;
  48.  
  49.         virtual ~Widget() { }
  50.  
  51.     protected:
  52.         Window* own;    // every Widget belongs to a Window
  53.         Fl_Widget* pw;  // connection to the FLTK Widget
  54.     private:
  55.         Widget& operator=(const Widget&); // don't copy Widgets
  56.         Widget(const Widget&);
  57.     };
  58.  
  59.     //------------------------------------------------------------------------------
  60.  
  61.     struct Button : Widget {
  62.         Button(Point xy, int w, int h, const std::string& label, Callback cb)
  63.             : Widget(xy, w, h, label, cb)
  64.         {}
  65.  
  66.         void attach(Window&);
  67.     };
  68.  
  69.     //------------------------------------------------------------------------------
  70.  
  71.     struct In_box : Widget {
  72.         In_box(Point xy, int w, int h, const std::string& s)
  73.             :Widget(xy, w, h, s, 0) { }
  74.         int get_int();
  75.         std::string get_string();
  76.  
  77.         void attach(Window& win);
  78.     };
  79.  
  80.     //------------------------------------------------------------------------------
  81.  
  82.     struct Out_box : Widget {
  83.         Out_box(Point xy, int w, int h, const string& s)
  84.             :Widget(xy, w, h, s, 0) { }
  85.         void put(int);
  86.         void put(const string&);
  87.  
  88.         void attach(Window& win);
  89.     };
  90.  
  91.     //------------------------------------------------------------------------------
  92.  
  93.     struct Menu : Widget {
  94.         enum Kind { horizontal, vertical };
  95.         Menu(Point xy, int w, int h, Kind kk, const std::string& label)
  96.             : Widget(xy, w, h, label, 0), k(kk), offset(0)
  97.         {}
  98.  
  99.         Vector_ref<Button> selection;
  100.         Kind k;
  101.         int offset;
  102.         int attach(Button& b);      // Menu does not delete &b
  103.         int attach(Button* p);      // Menu deletes p
  104.  
  105.         void show()                 // show all buttons
  106.         {
  107.             for (auto i = 0; i<selection.size(); ++i)
  108.                 selection[i].show();
  109.         }
  110.         void hide()                 // hide all buttons
  111.         {
  112.             for (auto i = 0; i<selection.size(); ++i)
  113.                 selection[i].hide();
  114.         }
  115.         void move(int dx, int dy)   // move all buttons
  116.         {
  117.             for (auto i = 0; i<selection.size(); ++i)
  118.                 selection[i].move(dx, dy);
  119.         }
  120.  
  121.         void attach(Window& win)    // attach all buttons
  122.         {
  123.             for (int i = 0; i<selection.size(); ++i) win.attach(selection[i]);
  124.             own = &win;
  125.         }
  126.  
  127.     };
  128.  
  129.     //------------------------------------------------------------------------------
  130.  
  131. } // of namespace Graph_lib
  132.  
  133. #endif // GUI_GUARD
Advertisement
Add Comment
Please, Sign In to add comment