1 
2 /******************************************************************************
3 * MODULE     : widget.hpp
4 * DESCRIPTION: Definition of abstract widgets
5 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11 
12 #ifndef WIDGET_H
13 #define WIDGET_H
14 #include "list.hpp"
15 #include "tree.hpp"
16 #include "blackbox.hpp"
17 #include "command.hpp"
18 #include "timer.hpp"
19 #include "renderer.hpp" // for PIXEL
20 
21 class window_rep;
22 typedef window_rep* window;
23 class url;
24 class widget;
25 class slot;
26 class widget_connection;
27 template<class T> class promise;
28 
29 /******************************************************************************
30 * The abstract widget class
31 ******************************************************************************/
32 
33 class widget_rep: public abstract_struct {
34 protected:
35   list<widget_connection> in;
36   list<widget_connection> out;
37 
38 public:
39   widget_rep ();
40   virtual ~widget_rep ();
41   virtual tm_ostream& print (tm_ostream& out);
42 
43   virtual void send (slot s, blackbox val);
44     // send a message val to the slot s
45   virtual blackbox query (slot s, int type_id);
46     // obtain information of a given type from the slot s
47   virtual widget read (slot s, blackbox index);
48     // abstract read access (of type s) of a subwidget at position index
49   virtual void write (slot s, blackbox index, widget w);
50     // abstract write access (of type s) of a subwidget at position index
51   virtual void notify (slot s, blackbox new_val);
52     // notification of a change on a slot s which contains a state variable
53   virtual void connect (slot s, widget w2, slot s2);
54     // connect a state slot s to another slot s2 of another widget w2
55   virtual void deconnect (slot s, widget w2, slot s2);
56     // deconnect a state slot s from another slot s2 of another widget w2
57 
58   friend class widget;
59 };
60 
61 class widget {
62 public:
63 ABSTRACT_NULL(widget);
operator ==(widget w)64   inline bool operator == (widget w) { return rep == w.rep; }
operator !=(widget w)65   inline bool operator != (widget w) { return rep != w.rep; }
66 };
67 ABSTRACT_NULL_CODE(widget);
68 
69 inline tm_ostream&
operator <<(tm_ostream & out,widget w)70 operator << (tm_ostream& out, widget w) {
71   if (is_nil (w)) return out << "nil";
72   else return w->print (out);
73 }
74 
75 extern bool use_side_tools;
76 
77 /******************************************************************************
78 * Widget style parameters
79 ******************************************************************************/
80 
81 #define WIDGET_STYLE_MINI                1
82   // use smaller text font inside widget
83 #define WIDGET_STYLE_MONOSPACED          2
84   // use monospaced font inside widget
85 #define WIDGET_STYLE_GREY                4
86   // use grey text font
87 #define WIDGET_STYLE_PRESSED             8
88   // indicate that a button is currently pressed
89 #define WIDGET_STYLE_INERT              16
90   // only render but don't associate any action to widget
91 #define WIDGET_STYLE_BUTTON             32
92   // indicate that a button should explicitly rendered as a button
93 #define WIDGET_STYLE_CENTERED           64
94   // use centered text
95 #define WIDGET_STYLE_BOLD              128
96   // use bold text
97 
98 /******************************************************************************
99 * Window widgets
100 ******************************************************************************/
101 
102 widget plain_window_widget (widget w, string s, command quit= command ());
103   // creates a decorated window with name s and contents w
104 widget popup_window_widget (widget w, string s);
105   // creates an undecorated window with name s and contents w
106 void   destroy_window_widget (widget w);
107   // destroys a window as created by the above routines
108 
109 /******************************************************************************
110 * Top-level widgets, typically given as an argument to plain_window_widget
111 * See also message.hpp for specific messages for these widgets
112 ******************************************************************************/
113 
114 widget texmacs_widget (int mask, command quit);
115   // the main TeXmacs widget and a command which is called on exit
116   // the mask variable indicates whether the menu, icon bars, status bar, etc.
117   // are visible or not
118 widget file_chooser_widget (command cmd, string type, string prompt);
119   // file chooser widget for files of a given 'type';
120   // for files of type "image", the widget includes a previsualizer for images
121   // 'prompt' contains a prompt if we intend to save the file
122   // and the empty string otherwise
123 widget printer_widget (command cmd, url ps_pdf_file);
124   // widget for printing a file, offering a way for selecting a page range,
125   // changing the paper type and orientation, previewing, etc.;
126   // the command cmd is called on exit
127 widget color_picker_widget (command cmd, bool bg, array<tree> proposals);
128   // widgets for selecting a color, a pattern or a background image,
129   // encoded by a tree. On input, we give a list of recently used proposals
130   // on termination the command is called with the selected color as argument
131   // the bg flag specifies whether we are picking a background color or fill
132 widget inputs_list_widget (command call_back, array<string> prompts);
133   // a dialogue widget with Ok and Cancel buttons and a series of textual
134   // input widgets with specified prompts
135 widget popup_widget (widget w);
136   // a widget container which results w to be unmapped as soon as
137   // the pointer quits the widget
138 
139 /******************************************************************************
140 * Widgets for the construction of menus
141 ******************************************************************************/
142 
143 widget horizontal_menu (array<widget> a);
144   // a horizontal menu made up of the widgets in a
145 widget vertical_menu (array<widget> a);
146   // a vertical menu made up of the widgets in a
147 widget tile_menu (array<widget> a, int cols);
148   // a menu rendered as a table of cols columns wide & made up of widgets in a
149 widget minibar_menu (array<widget> a);
150   // a small minibar, which can for instance occur inside another iconbar
151 widget menu_separator (bool vertical);
152   // a horizontal or vertical menu separator
153 widget menu_group (string name, int style);
154   // a menu group of a given style; the name should be greyed and centered
155 
156 widget pulldown_button (widget w, promise<widget> pw);
157   // a button w with a lazy pulldown menu pw
158 widget pullright_button (widget w, promise<widget> pw);
159   // a button w with a lazy pullright menu pw
160 widget menu_button (widget w, command cmd,
161 		    string pre= "", string ks= "", int style= 0);
162   // a command button with an optional prefix (o, * or v) and
163   // keyboard shortcut; if ok does not hold, then the button is greyed
164   // for pressed styles, the button is displayed as a pressed button
165 widget balloon_widget (widget w, widget help);
166   // given a button widget w, specify a help balloon which should be displayed
167   // when the user leaves the mouse pointer on the button for a small while
168 
169 widget text_widget (string s, int style, color col, bool tsp= true);
170   // a text widget with a given style, color and transparency
171 widget xpm_widget (url file_name);
172   // a widget with an X pixmap icon
173 widget input_text_widget (command call_back, string type, array<string> def,
174 			  int style= 0, string width= "1w");
175   // a textual input widget for input of a given type and a list of suggested
176   // default inputs (the first one should be displayed, if there is one)
177   // an optional width may be specified for the input field
178   // the width is specified in TeXmacs length format with units em, px or w
179 widget enum_widget (command cb, array<string> vals, string val,
180                     int st= 0, string w= "1w");
181   // select a value from a list of possible values
182 widget choice_widget (command cb, array<string> vals, string val);
183   // select a value from a long list of possible values
184 widget choice_widget (command cb, array<string> vals, array<string> mc);
185   // select multiple values from a long list
186 widget choice_widget (command cb, array<string> vals, string val, string filt);
187   // select a value from a long list with scrollbars and an input to filter
188 widget tree_view_widget (command cmd, tree data, tree data_roles);
189   // A widget with a tree view which observes the data and updates automatically
190 
191 /******************************************************************************
192 * Other widgets
193 ******************************************************************************/
194 
195 widget empty_widget ();
196   // an empty widget of size zero
197 widget glue_widget (bool hx=true, bool vx=true, SI w=0, SI h=0);
198   // an empty widget of minimal width w and height h and which is horizontally
199   // resp. vertically extensible if hx resp. vx is true
200 widget glue_widget (tree col, bool hx=true, bool vx=true, SI w=0, SI h=0);
201   // a colored variant of the above widget, with colors as in the color picker
202 widget horizontal_list (array<widget> a);
203   // a horizontal list made up of the widgets in a
204 widget vertical_list (array<widget> a);
205   // a vertical list made up of the widgets in a
206 widget aligned_widget (array<widget> lhs, array<widget> rhs,
207                        SI hsep= 3*PIXEL, SI vsep= 3*PIXEL,
208                        SI lpad= 0, SI rpad= 0);
209   // a table with two columns, the first one being right aligned and
210   // the second one being left aligned
211 widget tabs_widget (array<widget> tabs, array<widget> bodies);
212   // a tab bar where one and only of the bodies can be selected
213 widget icon_tabs_widget (array<url> us, array<widget> ss, array<widget> bs);
214   // a variant of tabs_widget with named icon tabs
215 widget wrapped_widget (widget w, command quit);
216   // copy of w, but with a separate reference counter,
217   // and with a command to be called upon destruction
218 widget user_canvas_widget (widget wid, int style= 0);
219   // a widget whose contents can be scrolled
220   // if the size of the inner contents exceed the specified size
221 widget resize_widget (widget w, int style, string w1, string h1,
222                       string w2, string h2, string w3, string h3,
223                       string hpos, string vpos);
224   // resize the widget w to be of minimal size (w1, h1),
225   // of default size (w2, h2), of maximal size (w3, h3),
226   // and initial scrolling position (hpos, vpos)
227 widget hsplit_widget (widget l, widget r);
228   // two horizontally juxtaposed widgets l and r with an ajustable border
229 widget vsplit_widget (widget t, widget b);
230   // two vertically juxtaposed widgets t and b with an ajustable border
231 widget extend (widget w, array<widget> a);
232   // extend the size of w to the maximum of the sizes of
233   // the widgets in the list a
234 widget toggle_widget (command cmd, bool on= false, int style= 0);
235   // an input toggle
236 widget wait_widget (SI width, SI height, string message);
237   // a widget of a specified width and height, displaying a wait message
238   // this widget is only needed when using the X11 plugin
239 widget ink_widget (command cb);
240   // widget for inking a sketch. The input may later be passed to
241   // an external program for handwriting recognition,
242   // using the callback routine
243 widget refresh_widget (string tmwid, string kind= "any");
244   // a widget which is automatically constructed from the a dynamic
245   // scheme widget tmwid. When receiving the send_refresh event,
246   // the contents should also be updated dynamically by reevaluating
247   // the scheme widget (in case of matching kind)
248 widget refreshable_widget (object prom, string kind= "any");
249   // a widget which is automatically constructed from the a dynamic
250   // scheme widget promise. When receiving the send_refresh event,
251   // the contents should also be updated dynamically by reevaluating
252   // the scheme widget promise (in case of matching kind)
253 
254 /******************************************************************************
255 * Besides the widget constructors, any GUI implementation should also provide
256 * a simple_widget_rep class with the following virtual methods:
257 ******************************************************************************/
258 
259 // bool simple_widget_rep::is_editor_widget ();
260 //   should return true for editor widgets only
261 // void simple_widget_rep::handle_get_size_hint (SI& w, SI& h);
262 //   propose a size for the widget
263 // void simple_widget_rep::handle_notify_resize (SI w, SI h);
264 //   issued when the size of the widget has changed
265 // void simple_widget_rep::handle_keypress (string key, time_t t);
266 //   issed when a key is pressed
267 // void simple_widget_rep::handle_keyboard_focus (bool new_focus, time_t t);
268 //   issued when the keyboard focus of the widget has changed
269 // void simple_widget_rep::handle_mouse
270 //        (string kind, SI x, SI y, int mods, time_t t);
271 //   a mouse event of a given kind at position (x, y) and time t
272 //   mods contains the active keyboard modifiers at time t
273 // void simple_widget_rep::handle_set_zoom_factor (double zoom);
274 //   set the zoom factor for painting
275 // void simple_widget_rep::handle_clear (SI x1, SI y1, SI x2, SI y2);
276 //   clear the widget to the background color
277 //   this event may for instance occur when scrolling
278 // void simple_widget_rep::handle_repaint (SI x1, SI y1, SI x2, SI y2);
279 //   repaint the region (x1, y1, x2, y2)
280 
281 #endif // defined WIDGET_H
282