36f8e426b5ed71926fc6fd608bb17fa558267264
[AXE.git] / src / xwindow.h
1 /**************************************************************************
2 **  (c) Copyright 1998, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : xwindow.h
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      :  Definition of the window class
11 **
12 **  EXPORTED OBJECTS : class window
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Feb 06, 1998
20 **      LAST UPDATE     : Jul 28, 2000
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: xwindow.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: xwindow.h,v 1.1 2002-07-25 08:01:27 arjen Exp $"; */
32
33 #ifndef AXE_XWINDOW_H
34 #define AXE_XWINDOW_H
35
36 #include <vector>
37 #include <X11/Xlib.h>
38 #include "geometry.h"
39 #include "gc.h"
40 #include "pixmap.h"
41 #include "String.h"
42
43 #define GrabPointerMask    (1L << 25)
44
45 /*
46 ///////////////////////////////////////////////////////////////////////////
47 //  NAME           : window
48 //  BASECLASS      :
49 //  MEMBERS        : win_id     : X server id of the window
50 //                   children   : Linked list of child windows.
51 //                   parent     : The parent window (0 if main window)
52 //                   selection_mask : Mask of selected events.
53 //  OPERATORS      :
54 //  METHODS        :
55 //
56 //  DESCRIPTION    : A window object encapsulates a window on the X workstation.
57 //                   A window is a rectangular area on the screen, used
58 //                   for graphical output. Input from mouse and keyboard
59 //                   is handled by event-handling functions in a window.
60 //
61 //  RELATIONS      :
62 //  SEE ALSO       : xapplication
63 //  LAST MODIFIED  : Feb 13, 1998
64 ///////////////////////////////////////////////////////////////////////////
65 */
66
67 class window;
68
69    struct win_list
70    {
71       window   *child;
72       win_list *next;
73    };
74
75
76    struct win_message
77    {
78       int     msg_id;
79       window *from;
80    };
81
82 class window
83 {
84    XID   win_id;
85
86    unsigned   w, h;           //  Size of the window
87
88    long       selection_mask;
89
90    void add_child(window *ch);
91    void remove_child(window *ch);
92
93 protected:
94
95    window     *parent;
96
97 public:
98
99    win_list   *children;
100    XID ID()
101    {
102       return win_id;
103    }
104
105    //  Event handlers
106    
107    virtual int EV_Expose(XExposeEvent);
108    virtual int EV_NoExpose(XNoExposeEvent);
109    virtual int EV_KeyPress(XKeyEvent);
110    virtual int EV_KeyRelease(XKeyEvent);
111    virtual int EV_ButtonPress(XButtonEvent);
112    virtual int EV_ButtonRelease(XButtonEvent);
113    virtual int EV_MotionNotify(XMotionEvent);
114    virtual int EV_EnterNotify(XCrossingEvent);
115    virtual int EV_LeaveNotify(XCrossingEvent);
116    virtual int EV_FocusIn(XFocusChangeEvent);
117    virtual int EV_FocusOut(XFocusChangeEvent);
118    virtual int EV_ConfigureNotify(XConfigureEvent);
119    virtual int EV_ClientMessage(XClientMessageEvent);
120
121    virtual int ParentResized(int w, int h);
122
123    virtual void ChildMessage(win_message &msg);
124
125    void    ParentMessage(int id);
126
127    int EV_Default(XEvent);
128
129    window(int x=0, int y=0, unsigned w=320, unsigned h=256, int bw = 4);
130    window(window &, int x=0, int y=0,
131           unsigned int w=100, unsigned int h=100, int bw = 1);
132    virtual ~window();
133
134    //   Mapping and unmapping
135
136    void Map(void)
137    {
138       XMapWindow(stddpy.Dpy(), win_id);
139    }
140    
141    void Realize(void)
142    {
143       XMapSubwindows(stddpy, win_id);
144       XMapRaised(stddpy, win_id);
145    }
146
147    void Unmap(void)
148    {
149       XUnmapWindow(stddpy.Dpy(), win_id);
150    }
151    
152    //  Size and position
153
154    void Size(unsigned &width, unsigned &height)
155    {
156       width = w;
157       height = h;
158    }
159
160    size Size(void)
161    {
162       return size(w, h);
163    }
164
165    void Resize(unsigned width, unsigned height);
166    void Resize(size sz);
167    void Move(int x, int y);
168
169    //   Border and background
170
171    void Border(unsigned int borderwidth)
172    {
173       XSetWindowBorderWidth(stddpy, win_id, borderwidth);
174    }
175
176    void Background(unsigned long pixel);
177    void WindowGravity(int gravity);
178    void Clear(void);
179
180    long SelectInput(long events, int on);
181    long SelectInput(void)
182    {
183       return selection_mask;
184    }
185
186    void SetFocus(void)
187    {
188       XSetInputFocus(stddpy, win_id, RevertToPointerRoot, CurrentTime);
189    }
190
191    int  GrabPointer(Cursor curs);
192    int  UngrabPointer(void);
193    void DefineCursor(Cursor curs)
194    {
195       XDefineCursor(stddpy, ID(), curs);
196    }
197    void UndefineCursor(void)
198    {
199       XUndefineCursor(stddpy, ID());
200    }
201
202    //   Graphic drawing functions
203    
204    void DrawString(const gc &graphic, int x, int y, String str) const
205    {
206       XDrawString(stddpy, win_id, graphic, x, y, str, ~str);
207    }
208
209    void DrawString(const gc &graphic, point p, String str) const
210    {
211       XDrawString(stddpy, win_id, graphic, p.x, p.y, str, ~str);
212    }
213
214    void DrawLine(const gc &graphic, int x1, int y1, int x2, int y2) const
215    {
216       XDrawLine(stddpy, win_id, graphic, x1, y1, x2, y2);
217    }
218
219    void DrawLine(const gc &graphic, point p1, point p2) const
220    {
221       XDrawLine(stddpy, win_id, graphic, p1.x, p1.y, p2.x, p2.y);
222    }
223
224    void DrawLine(const gc &graphic, std::vector<point> &l, int mode = CoordModeOrigin) const
225    {
226       int npoints  = l.size();
227       XPoint  *points = new XPoint[npoints];
228
229       for (int i = 0; i < npoints; i++)
230       {
231          points[i].x = l[i].x;
232          points[i].y = l[i].y;
233       }
234
235       XDrawLines(stddpy, win_id, graphic, points, npoints, mode);
236    }
237
238    void FillPolygon(const gc &graphic, std::vector<point> &l,
239                     int mode = CoordModeOrigin, int shape = Complex) const
240    {
241       int npoints  = l.size();
242       XPoint  *points = new XPoint[npoints];
243
244       for (int i = 0; i < npoints; i++)
245       {
246          points[i].x = l[i].x;
247          points[i].y = l[i].y;
248       }
249
250       XFillPolygon(stddpy, win_id, graphic, points, npoints, shape, mode);
251    }
252
253    void DrawRectangle(const gc &graphic, point p, size s) const
254    {
255       XDrawRectangle(stddpy, win_id, graphic, p.x, p.y, s.w, s.h);
256    }
257
258    void DrawRectangle(const gc &graphic, int x, int y, unsigned w, unsigned h) const
259    {
260       XDrawRectangle(stddpy, win_id, graphic, x, y, w, h);
261    }
262
263    void DrawRectangle(const gc &graphic, rectangle r) const
264    {
265       DrawRectangle(graphic, r.Origin(), r.Size());
266    }
267
268    void DrawRectangle(const gc &graphic, point p1, point p2) const
269    {
270       DrawRectangle(graphic, rectangle(p1, p2));
271    }
272
273    void FillRectangle(const gc &graphic, point p, size s) const
274    {
275       XFillRectangle(stddpy, win_id, graphic, p.x, p.y, s.w, s.h);
276    }
277
278    void FillRectangle(const gc &graphic, int x, int y, unsigned w, unsigned h) const
279    {
280       XFillRectangle(stddpy, win_id, graphic, x, y, w, h);
281    }
282
283    void FillRectangle(const gc &graphic, rectangle r) const
284    {
285       FillRectangle(graphic, r.Origin(), r.Size());
286    }
287
288    void FillRectangle(const gc &graphic, point p1, point p2) const
289    {
290       FillRectangle(graphic, rectangle(p1, p2));
291    }
292
293    void DrawArc(const gc &graphic, int x, int y, unsigned w, unsigned h,
294                 int angle1, int angle2) const
295    {
296       XDrawArc(stddpy, win_id, graphic, x, y, w, h, angle1, angle2);
297    }
298
299    void FillArc(const gc &graphic, int x, int y, unsigned w, unsigned h,
300                 int angle1, int angle2) const
301    {
302       XFillArc(stddpy, win_id, graphic, x, y, w, h, angle1, angle2);
303    }
304
305    void DrawPixmap(const gc &graphic, int x, int y, const pixmap &pm) const
306    {
307       XCopyArea(stddpy, pm, win_id, graphic, 0, 0,
308                 pm.Size().w, pm.Size().h, x, y);
309    }
310 };
311
312 class managed_window : public window
313 {
314    unsigned long bg;
315    int      WM_Delete_Command;  //  Command to send op 'close' button.
316
317 public:
318
319    managed_window()
320    {
321       WM_Delete_Command = 0;
322       Atom    wm_delete;
323
324       wm_delete = XInternAtom(stddpy, "WM_DELETE_WINDOW", true);
325       bg = 0;
326
327       XStoreName(stddpy, ID(), "AXE Test");
328
329       XSetWMProtocols(stddpy, ID(),&wm_delete , 1);
330
331       SelectInput(StructureNotifyMask, 1);
332       SelectInput(FocusChangeMask, 1);
333
334    }
335
336    managed_window(char *name)
337    {
338       WM_Delete_Command = 0;
339       Atom    wm_delete;
340
341       wm_delete = XInternAtom(stddpy, "WM_DELETE_WINDOW", true);
342       bg = 0;
343
344       XStoreName(stddpy, ID(), name);
345
346       XSetWMProtocols(stddpy, ID(),&wm_delete , 1);
347
348       SelectInput(StructureNotifyMask, 1);
349       SelectInput(FocusChangeMask, 1);
350
351    }
352
353    void Title(char *name)
354    {
355       XStoreName(stddpy, ID(), name);
356    }
357
358    void Command_WhenClosed(int cmd)
359    {
360       WM_Delete_Command = cmd;
361    }
362
363    virtual int EV_FocusIn(XFocusChangeEvent);
364    virtual int EV_ClientMessage(XClientMessageEvent ev);
365
366    virtual int DoCommand(int code)
367    {
368       return -1;
369    }
370 };
371
372 /*
373  *  Predefined message id's in a win_message
374  */
375
376 #define EDIT_ENTERED       1
377 #define EDIT_FOCUSLOST     2
378 #define SCROLL_POSITIONED  3
379 #define TABLE_ROW_SELECT   4
380
381 #endif /* AXE_XWINDOW_H */
382