├── GUI.cpp ├── GUI.h ├── Graph.cpp ├── Graph.h ├── Gui.h ├── Matrix.h ├── Matrix11.h ├── MatrixIO.h ├── MatrixIO11.h ├── Point.h ├── Simple_window.cpp ├── Simple_window.h ├── Window.cpp ├── Window.h ├── fltk.h └── std_lib_facilities.h /GUI.cpp: -------------------------------------------------------------------------------- 1 | #include "GUI.h" 2 | #include "std_lib_facilities.h" 3 | #include 4 | 5 | using namespace Graph_lib; 6 | 7 | 8 | void Button::attach(Window& win) 9 | { 10 | pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str()); 11 | pw->callback(reinterpret_cast(do_it), &win); // pass the window 12 | own = &win; 13 | } 14 | 15 | int In_box::get_int() 16 | { 17 | Fl_Input& pi = reference_to(pw); 18 | // return atoi(pi.value()); 19 | const char* p = pi.value(); 20 | if (!isdigit(p[0])) return -999999; 21 | return atoi(p); 22 | } 23 | 24 | string In_box::get_string() 25 | { 26 | Fl_Input& pi = reference_to(pw); 27 | return string(pi.value()); 28 | } 29 | 30 | void In_box::attach(Window& win) 31 | { 32 | pw = new Fl_Input(loc.x, loc.y, width, height, label.c_str()); 33 | own = &win; 34 | } 35 | 36 | void Out_box::put(int i) 37 | { 38 | Fl_Output& po = reference_to(pw); 39 | std::stringstream ss; 40 | ss << i; 41 | po.value(ss.str().c_str()); 42 | } 43 | 44 | void Out_box::put(const string& s) 45 | { 46 | reference_to(pw).value(s.c_str()); 47 | } 48 | 49 | void Out_box::attach(Window& win) 50 | { 51 | pw = new Fl_Output(loc.x, loc.y, width, height, label.c_str()); 52 | own = &win; 53 | } 54 | 55 | Menu::Menu(Point xy, int w, int h, Kind kk, const string& s) 56 | :Widget(xy,w,h,s,0), k(kk), offset(0) 57 | { 58 | } 59 | 60 | int Menu::attach(Button& b) 61 | { 62 | b.width = width; 63 | b.height = height; 64 | 65 | switch(k) { 66 | case horizontal: 67 | b.loc = Point(loc.x+offset,loc.y); 68 | offset+=b.width; 69 | break; 70 | case vertical: 71 | b.loc = Point(loc.x,loc.y+offset); 72 | offset+=b.height; 73 | break; 74 | } 75 | selection.push_back(&b); 76 | return int(selection.size()-1); 77 | } 78 | 79 | int Menu::attach(Button* p) 80 | { 81 | // owned.push_back(p); 82 | return attach(*p); 83 | } -------------------------------------------------------------------------------- /GUI.h: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // This is a GUI support code to the chapters 12-16 of the book 4 | // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup 5 | // 6 | 7 | #ifndef GUI_GUARD 8 | #define GUI_GUARD 9 | 10 | #include "Window.h" 11 | #include "Graph.h" 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 W& reference_to(Address pw) 23 | // treat an address as a reference to a W 24 | { 25 | return *static_cast(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 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 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 string& s) 73 | :Widget(xy,w,h,s,0) { } 74 | int get_int(); 75 | 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 string& label) 96 | : Widget(xy,w,h,label,0), k(kk), offset(0) 97 | {} 98 | 99 | Vector_ref