189b71e9fbe790eb226da19cd259ca9be01fb100
[AXE.git] / src / xappl.h
1 /**************************************************************************
2 **  (c) Copyright 1998, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : xappl.h
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.1 $
9 **
10 **  DESCRIPTION      : Definition of xapplication class 
11 **
12 **  EXPORTED OBJECTS : class xapplication
13 **  LOCAL    OBJECTS : class xw_list, class xw_hash
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     : Feb 23, 1998
21 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: xappl.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: xappl.h,v 1.1 2002-07-25 08:01:27 arjen Exp $"; */
32
33 #ifndef XAPPL_H
34 #define XAPPL_H
35
36 #include <X11/Xlib.h>
37 #include "display.h"
38 #include "xwindow.h"
39 #include "cursor.h"
40
41 /*
42 ///////////////////////////////////////////////////////////////////////////
43 //  NAME           : xw_list
44 //  BASECLASS      :
45 //  MEMBERS        : id   : X id of the window
46 //                   win  : Pointer to the window object
47 //                   next : Pointer to next xw_list
48 //  OPERATORS      :
49 //  METHODS        : xw_list(XID, window *, xw_list *)
50 //                      Insert at the start of the list.
51 //
52 //  DESCRIPTION    : Linked list of X-Window ID and window pointers.
53 //                   Is used in the hash table of windows.
54 //
55 //  RELATIONS      :
56 //  SEE ALSO       : xw_hash
57 //  LAST MODIFIED  : Feb 07, 1998
58 ///////////////////////////////////////////////////////////////////////////
59 */
60
61 class xw_list
62 {
63 public:
64    XID      id;
65    window   *win;
66    xw_list  *next;
67
68    xw_list(XID win_id, window *win_ptr, xw_list *list)
69    {
70       id   = win_id;
71       win  = win_ptr;
72       next = list;
73    }
74 };
75
76
77 /*
78 ///////////////////////////////////////////////////////////////////////////
79 //  NAME           : xw_hash
80 //  BASECLASS      :
81 //  MEMBERS        : tabsize  : Size of the hash table.
82 //                   table    : Array of xw_list entries.
83 //  OPERATORS      : window * [XID]
84 //  METHODS        : xw_hash(unsigned size)
85 //                      Create a hash table of specific size.
86 //                   xh_add(XID, window *)
87 //                      Add a new entry to the hash table.
88 //                   xh_del(XID)
89 //                      Delete an entry from the hash table.
90 //
91 //  DESCRIPTION    : The hash table associates a window's XID with a pointer
92 //                   to the window object. The xapplication object uses the
93 //                   hash table for dispatching events to windows.
94 //
95 //                   Each entry in the hash table, a xw_list pointer, is
96 //                   a linked list of these ID-pointer associations. The hash
97 //                   function is a simple modulo.
98 //
99 //  RELATIONS      : xw_list
100 //  SEE ALSO       : xapplication
101 //  LAST MODIFIED  : Feb 08, 1998
102 ///////////////////////////////////////////////////////////////////////////
103 */
104
105 class xw_hash
106 {
107    xw_list    **table;   //  The actual hash table.
108    unsigned   tabsize;   //  Nr. of entries in the hash table.
109
110    unsigned   Hash(XID win)
111    {
112       return win % tabsize;
113    }
114
115 public:
116
117    xw_hash()
118    {
119       tabsize = 100;
120
121       table = new (xw_list *)[tabsize];
122       for (unsigned i=0; i < tabsize; i++)
123       {
124          table[i] = 0;
125       }
126    }
127
128    xw_hash(unsigned size)
129    {
130       tabsize = size;
131
132       table = new (xw_list *)[size];
133       for (unsigned i=0; i < tabsize; i++)
134       {
135          table[i] = 0;
136       }
137    }
138
139    ~xw_hash()
140    {
141       delete[] table;
142    }
143
144    //   Add a new entry in the hash table.
145
146    void xh_add(XID win_id, window *win_ptr)
147    {
148       unsigned   h;   // The hash value;
149
150       h = Hash(win_id);
151       table[h] = new xw_list(win_id, win_ptr, table[h]);
152    }
153
154    //  Delete an entry from the hash table.
155
156    void xh_del(XID win_id)
157    {
158       unsigned   h;         // The hash value;
159       xw_list    *entry;    // The one we want to remove.
160       xw_list    *previous; // The one just before entry.
161
162       h = Hash(win_id);
163
164       //   find the window's entry in the list.
165       previous = 0;
166       entry = table[h];
167       while (entry && entry->id != win_id)
168       {
169          previous = entry;
170          entry = entry->next;
171       }
172
173       if (previous)
174       {
175          //   Re-link the list, thus excluding entry.
176          previous->next = entry->next;
177       }
178       else
179       {
180          //  It was the first entry.
181          table[h] = entry->next;
182       }
183       delete entry;
184    }
185
186    window * operator[](XID win_id)
187    {
188       unsigned   h;   // The hash value;
189       xw_list    *entry;
190
191       h = Hash(win_id);
192       for (entry = table[h]; entry && entry->id != win_id; entry = entry->next);
193       if (entry)
194       {
195          return entry->win;
196       }
197       else
198       {
199          return 0;
200       }
201    }
202 };
203
204
205 /*
206 ///////////////////////////////////////////////////////////////////////////
207 //  NAME           : xapplication
208 //  BASECLASS      :
209 //  MEMBERS        : wintab   : The table of maintained windows.
210 //  OPERATORS      :
211 //  METHODS        : Initialize
212 //                   ParseArguments
213 //                   SetupResources
214 //                   UserInit
215 //                   DispatchEvent
216 //                   Cleanup
217 //                   add_window
218 //                   remove_window
219 //
220 //  DESCRIPTION    : Each X Windows application MUST have one object of a
221 //                   class derived from xapplication.
222 //                   The xapplication class implements the main thread of
223 //                   control: initialization, event-loop and termination.
224 //                   It maintains a list of all windows that are created by
225 //                   the application.
226 //
227 //  RELATIONS      : xw_hash
228 //  SEE ALSO       :
229 //  LAST MODIFIED  : Feb 23, 1998
230 ///////////////////////////////////////////////////////////////////////////
231 */
232
233 class xapplication
234 {
235    xw_hash   wintab;
236
237    window    *Pgrab_window;
238
239    managed_window    *Top_window;
240
241 public:
242
243    xapplication();
244
245    virtual ~xapplication() {}
246
247    void add_window(XID w_id, window *w_ptr);
248
249    void remove_window(XID w_id);
250
251    void Pgrab(window *w_ptr);
252    void Topwin(managed_window *w_ptr);
253
254    virtual void Initialize(void);
255
256    virtual void ParseArguments(int argc, char *argv[]);
257
258    virtual void SetupResources(void);
259
260    virtual void UserInit(void);
261
262    virtual int DoCommand(int code);
263
264    virtual void Cleanup(void);
265
266    int  DispatchEvent(XEvent ev);
267    int  SendCommand(int code);
268
269 };
270
271 extern xapplication *XApp;
272
273 extern color Black, White;
274 extern color menu_normal, menu_highlight;
275 extern color inside_3D, light_rim_3D, dark_rim_3D;
276
277 extern font  default_font;
278 extern font  fixed;
279
280 extern stipple dim_pattern;
281
282 extern cursor  hand_cursor;
283 extern cursor  text_cursor;
284 extern cursor  busy_cursor;
285
286 extern gc    menu_normal_gc;
287 extern gc    menu_dimmed_gc;
288 extern gc    inside_3D_gc, light_3D_gc, dark_3D_gc;
289 extern gc    red_gc, green_gc;
290 extern gc    white_gc;
291 extern gc    text_normal_gc;
292 extern gc    edit_cursor_gc;
293
294 extern pixmap nopicture;
295
296 #endif  /*  XAPPL_H */