First checkin, AXE release 0.2
[AXE.git] / src / menu.h
1 /**************************************************************************
2 **  (c) Copyright 1998, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : menu.h
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      : Definition of menu classes 
11 **
12 **  EXPORTED OBJECTS : class menu_container
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Feb 13, 1998
20 **      LAST UPDATE     : Mar 07, 1998
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: menu.h,v $
26    Revision 1.1  2002-07-25 08:01:27  arjen
27    First checkin, AXE release 0.2
28
29 *****************************/
30
31 /* static const char *RCSID = "$Id: menu.h,v 1.1 2002-07-25 08:01:27 arjen Exp $"; */
32
33 #include <X11/Xlib.h>
34 #include "xappl.h"
35
36 /*
37 ///////////////////////////////////////////////////////////////////////////
38 //  NAME           : menu_container
39 //  BASECLASS      : window
40 //  MEMBERS        : mode : Horizontal or Vertical
41 //  OPERATORS      :
42 //  METHODS        :
43 //
44 //  DESCRIPTION    : A menu_container is the parent window of menu items.
45 //                   The menu_container lines the menu items up next to
46 //                   eachother and makes them all fit together.
47 //
48 //  RELATIONS      : shown_by : menu_item that popped me up. The menu item
49 //                              which is clicked uses this to climb to
50 //                              the top of the menu tree and hide all submenus
51 //  SEE ALSO       :
52 //  LAST MODIFIED  : Feb 27, 1998
53 ///////////////////////////////////////////////////////////////////////////
54 */
55
56 class menu_item;  // Forward declaration.
57
58 class menu_container : public window
59 {
60
61 protected:
62
63    enum {Horizontal, Vertical} mode;
64
65 public:
66
67    menu_item *shown_by;
68
69    /*
70     *   Both constructors perform the same function. Create a window
71     *   with default size, set the background color and solicit
72     *   StructureNotify events.
73     *   Note that the size of the menu_container is eventually determined
74     *   by the sizes of the menu items.
75     */
76
77    menu_container() : window(0, 0, 50, 100, 1)
78    {
79       shown_by = 0;
80
81       Background(menu_normal);
82       SelectInput(StructureNotifyMask, 1);
83    }
84
85    menu_container(window &frame) : window(frame, 0, 0, 100, 15)
86    {
87       shown_by = 0;
88
89       Background(menu_normal);
90       SelectInput(StructureNotifyMask, 1);
91    }
92
93    void RearrangeItems(unsigned &overall_width, unsigned &overall_height);
94
95    /*
96     *   The position of a sub-menu depends on the mode. Horizontal menus
97     *   put submenus below the item, vertical menus put a submenu right
98     *   of the item.
99     */
100
101    void PositionSubmenu(int &x, int &y, unsigned w, unsigned h)
102    {
103       if (mode == Horizontal)
104          y += h - 3;
105       else
106          x += w - 5;
107    }
108
109    /*
110     *  Submenu management.
111     */
112
113    void HideSubmenus(void);
114
115    void Show(menu_item *by, int x, int y);
116
117 };
118
119
120 /*
121 ///////////////////////////////////////////////////////////////////////////
122 //  NAME           : menu_item
123 //  BASECLASS      : window
124 //  MEMBERS        :
125 //  OPERATORS      :
126 //  METHODS        :
127 //
128 //  DESCRIPTION    :
129 //
130 //  RELATIONS      :
131 //  SEE ALSO       :
132 //  LAST MODIFIED  :
133 ///////////////////////////////////////////////////////////////////////////
134 */
135
136 class menu_item : public window
137 {
138    int   command_code;
139
140 protected:
141  
142    int   highlighted;
143    int   enabled;
144
145    menu_container   *popup;
146
147 public:
148
149    menu_item(window &bar, int cc, int en = 1) : window(bar, 0, 0, 50, 15, 0)
150    {
151       highlighted = 0;
152       enabled     = en;
153       command_code = cc;
154       popup        = 0;
155
156       SelectInput(ExposureMask, 1);
157       Background(menu_normal);
158       SelectInput(ButtonReleaseMask|EnterWindowMask|LeaveWindowMask, enabled);
159    }
160
161    virtual void redraw(void);
162
163    virtual int EV_Expose(XExposeEvent ev)
164    {
165       redraw();
166       return 1;
167    }
168
169    virtual int EV_EnterNotify(XCrossingEvent ev);
170    virtual int EV_LeaveNotify(XCrossingEvent ev);
171    virtual int EV_ButtonRelease(XButtonEvent ev);
172
173    void HideSubmenu(void);
174    void ShowSubmenu(int x, int y);
175
176    void Popup(menu_container *pop)
177    {
178       popup = pop;
179    }
180
181    void Enable(int e)
182    {
183       enabled = e;
184       SelectInput(EnterWindowMask, e);
185    }
186
187 };
188
189 /*
190 ///////////////////////////////////////////////////////////////////////////
191 //  NAME           : menu_bar
192 //  BASECLASS      : menu_container
193 //  MEMBERS        :
194 //  OPERATORS      :
195 //  METHODS        :
196 //
197 //  DESCRIPTION    :
198 //
199 //  RELATIONS      :
200 //  SEE ALSO       :
201 //  LAST MODIFIED  :
202 ///////////////////////////////////////////////////////////////////////////
203 */
204
205 class menu_bar : public menu_container
206 {
207 public:
208
209    menu_bar(window &frame) : menu_container(frame)
210    {
211       mode = Horizontal;
212    }
213
214
215    virtual int ParentResized(int w, int h)
216    {
217       unsigned width, height;
218
219       RearrangeItems(width, height);
220
221       //  Resize to fit the parent window
222
223       Resize(w-2, height);
224
225       return 1;
226    }
227 };
228
229
230 /*
231 ///////////////////////////////////////////////////////////////////////////
232 //  NAME           : popup_menu
233 //  BASECLASS      : menu_container
234 //  MEMBERS        :
235 //  OPERATORS      :
236 //  METHODS        :
237 //
238 //  DESCRIPTION    :
239 //
240 //  RELATIONS      :
241 //  SEE ALSO       :
242 //  LAST MODIFIED  :
243 ///////////////////////////////////////////////////////////////////////////
244 */
245
246 class popup_menu: public menu_container
247 {
248 public:
249
250    popup_menu() : menu_container()
251    {
252       XSetWindowAttributes  attrib;
253       unsigned long         mask;
254
255       attrib.override_redirect = True;
256       mask                     = CWOverrideRedirect;
257
258       XChangeWindowAttributes(stddpy.Dpy(), ID(), mask, &attrib);
259       mode = Vertical;
260    }
261
262    virtual int EV_ConfigureNotify(XConfigureEvent ev)
263    {
264       unsigned width, height;
265
266       RearrangeItems(width, height);
267
268       Resize(width, height);
269
270       return 1;
271    }
272
273 };
274
275
276 /*
277 ///////////////////////////////////////////////////////////////////////////
278 //  NAME           : menu_label
279 //  BASECLASS      : menu_item
280 //  MEMBERS        :
281 //  OPERATORS      :
282 //  METHODS        :
283 //
284 //  DESCRIPTION    :
285 //
286 //  RELATIONS      :
287 //  SEE ALSO       :
288 //  LAST MODIFIED  : Mar 07, 1998
289 ///////////////////////////////////////////////////////////////////////////
290 */
291
292 class menu_label : public menu_item
293 {
294    char  *label;
295
296 public:
297
298    menu_label(window &bar, char *text, int cc = 0) : menu_item(bar, cc)
299    {
300       label = text;
301       int width = default_font.TextWidth(label) + 10;
302       int height = default_font.ascent() + default_font.descent() + 6;
303       Resize(width, height);
304    }
305
306    virtual void redraw(void);
307 };
308
309 /*
310 ///////////////////////////////////////////////////////////////////////////
311 //  NAME           : menu_separator
312 //  BASECLASS      : menu_item
313 //  MEMBERS        :
314 //  OPERATORS      :
315 //  METHODS        :
316 //
317 //  DESCRIPTION    :
318 //
319 //  RELATIONS      :
320 //  SEE ALSO       :
321 //  LAST MODIFIED  :
322 ///////////////////////////////////////////////////////////////////////////
323 */
324
325 class menu_separator : public menu_item
326 {
327 public:
328
329    menu_separator(window &bar) : menu_item(bar, 0, 0)
330    {
331       Resize(2, 7);
332    }
333
334    virtual void redraw(void);
335 };
336