Advertisement
tattersail

GUI.h

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