try make bootstrap work in one pass for other people..
[mikachu/openbox.git] / src / Window.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Window.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef   __Window_hh
25 #define   __Window_hh
26
27 extern "C" {
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #ifdef    SHAPE
31 #  include <X11/extensions/shape.h>
32 #endif // SHAPE
33 }
34
35 #include <string>
36
37 #include "BaseDisplay.hh"
38 #include "Timer.hh"
39 #include "Util.hh"
40 #include "Windowmenu.hh"
41
42 #define MwmHintsFunctions     (1l << 0)
43 #define MwmHintsDecorations   (1l << 1)
44
45 #define MwmFuncAll            (1l << 0)
46 #define MwmFuncResize         (1l << 1)
47 #define MwmFuncMove           (1l << 2)
48 #define MwmFuncIconify        (1l << 3)
49 #define MwmFuncMaximize       (1l << 4)
50 #define MwmFuncClose          (1l << 5)
51
52 #define MwmDecorAll           (1l << 0)
53 #define MwmDecorBorder        (1l << 1)
54 #define MwmDecorHandle        (1l << 2)
55 #define MwmDecorTitle         (1l << 3)
56 #define MwmDecorMenu          (1l << 4) // not used
57 #define MwmDecorIconify       (1l << 5)
58 #define MwmDecorMaximize      (1l << 6)
59
60 // this structure only contains 3 elements... the Motif 2.0 structure contains
61 // 5... we only need the first 3... so that is all we will define
62 typedef struct MwmHints {
63   unsigned long flags, functions, decorations;
64 } MwmHints;
65
66 #define PropMwmHintsElements  3
67
68 class BWindowGroup {
69 private:
70   Blackbox *blackbox;
71   Window group;
72   BlackboxWindowList windowList;
73
74 public:
75   BWindowGroup(Blackbox *b, Window _group);
76   ~BWindowGroup(void);
77
78   inline Window groupWindow(void) const { return group; }
79
80   inline bool empty(void) const { return windowList.empty(); }
81
82   void addWindow(BlackboxWindow *w) { windowList.push_back(w); }
83   void removeWindow(BlackboxWindow *w) { windowList.remove(w); }
84
85   /*
86     find a window on the specified screen. the focused window (if any) is
87     checked first, otherwise the first matching window found is returned.
88     transients are returned only if allow_transients is True.
89   */
90   BlackboxWindow *find(BScreen *screen, bool allow_transients = False) const;
91 };
92
93
94 class BlackboxWindow : public TimeoutHandler {
95 public:
96   enum Function { Func_Resize   = (1l << 0),
97                   Func_Move     = (1l << 1),
98                   Func_Iconify  = (1l << 2),
99                   Func_Maximize = (1l << 3),
100                   Func_Close    = (1l << 4) };
101   typedef unsigned char FunctionFlags;
102
103   enum Decoration { Decor_Titlebar = (1l << 0),
104                     Decor_Handle   = (1l << 1),
105                     Decor_Border   = (1l << 2),
106                     Decor_Iconify  = (1l << 3),
107                     Decor_Maximize = (1l << 4),
108                     Decor_Close    = (1l << 5) };
109   typedef unsigned char DecorationFlags;
110
111   enum WindowType { Type_Desktop,
112                     Type_Dock,
113                     Type_Toolbar,
114                     Type_Menu,
115                     Type_Utility,
116                     Type_Splash,
117                     Type_Dialog,
118                     Type_Normal };
119
120   enum Corner { TopLeft,
121                 TopRight,
122                 BottomLeft,
123                 BottomRight };
124
125 private:
126   Blackbox *blackbox;
127   BScreen *screen;
128   XAtom *xatom;
129   BTimer *timer;
130   BlackboxAttributes blackbox_attrib;
131
132   Time lastButtonPressTime;  // used for double clicks, when were we clicked
133   Windowmenu *windowmenu;
134
135   unsigned int window_number;
136   unsigned long current_state;
137
138   enum FocusMode { F_NoInput = 0, F_Passive,
139                    F_LocallyActive, F_GloballyActive };
140   FocusMode focus_mode;
141
142   struct _flags {
143     bool moving,             // is moving?
144       resizing,              // is resizing?
145       shaded,                // is shaded?
146       visible,               // is visible?
147       iconic,                // is iconified?
148       focused,               // has focus?
149       stuck,                 // is omnipresent?
150       modal,                 // is modal? (must be dismissed to continue)
151       skip_taskbar,          // skipped by taskbars?
152       skip_pager,            // skipped by pagers?
153       fullscreen,            // a fullscreen window?
154       send_focus_message,    // should we send focus messages to our client?
155       shaped;                // does the frame use the shape extension?
156     unsigned int maximized;  // maximize is special, the number corresponds
157                              // with a mouse button
158                              // if 0, not maximized
159                              // 1 = HorizVert, 2 = Vertical, 3 = Horizontal
160   } flags;
161
162   struct _client {
163     Window window,                  // the client's window
164       window_group;
165     BlackboxWindow *transient_for;  // which window are we a transient for?
166     BlackboxWindowList transientList; // which windows are our transients?
167
168     std::string title, icon_title;
169
170     Rect rect;
171     Strut strut;
172
173     int old_bw;                       // client's borderwidth
174
175     unsigned int
176       min_width, min_height,        // can not be resized smaller
177       max_width, max_height,        // can not be resized larger
178       width_inc, height_inc,        // increment step
179 #if 0 // not supported at the moment
180       min_aspect_x, min_aspect_y,   // minimum aspect ratio
181       max_aspect_x, max_aspect_y,   // maximum aspect ratio
182 #endif
183       base_width, base_height,
184       win_gravity;
185
186     unsigned long initial_state, normal_hint_flags;
187   } client;
188
189   FunctionFlags functions;
190   /*
191    * what decorations do we have?
192    * this is based on the type of the client window as well as user input
193    * the menu is not really decor, but it goes hand in hand with the decor
194    */
195   DecorationFlags decorations;
196   Corner resize_dir;
197   WindowType window_type;
198
199   /*
200    * client window = the application's window
201    * frame window = the window drawn around the outside of the client window
202    *                by the window manager which contains items like the
203    *                titlebar and close button
204    * title = the titlebar drawn above the client window, it displays the
205    *         window's name and any buttons for interacting with the window,
206    *         such as iconify, maximize, and close
207    * label = the window in the titlebar where the title is drawn
208    * buttons = maximize, iconify, close
209    * handle = the bar drawn at the bottom of the window, which contains the
210    *          left and right grips used for resizing the window
211    * grips = the smaller reactangles in the handle, one of each side of it.
212    *         When clicked and dragged, these resize the window interactively
213    * border = the line drawn around the outside edge of the frame window,
214    *          between the title, the bordered client window, and the handle.
215    *          Also drawn between the grips and the handle
216    */
217
218   struct _frame {
219     // u -> unfocused, f -> has focus
220     unsigned long ulabel_pixel, flabel_pixel, utitle_pixel,
221       ftitle_pixel, uhandle_pixel, fhandle_pixel, ubutton_pixel,
222       fbutton_pixel, pbutton_pixel, uborder_pixel, fborder_pixel,
223       ugrip_pixel, fgrip_pixel;
224     Pixmap ulabel, flabel, utitle, ftitle, uhandle, fhandle,
225       ubutton, fbutton, pbutton, ugrip, fgrip;
226
227     Window window,       // the frame
228       plate,             // holds the client
229       title,
230       label,
231       handle,
232       close_button, iconify_button, maximize_button,
233       right_grip, left_grip;
234
235     /*
236      * size and location of the box drawn while the window dimensions or
237      * location is being changed, ie. resized or moved
238      */
239     Rect changing;
240
241     Rect rect;                  // frame geometry
242     Strut margin;               // margins between the frame and client
243
244     int grab_x, grab_y;         // where was the window when it was grabbed?
245
246     unsigned int inside_w, inside_h, // window w/h without border_w
247       title_h, label_w, label_h, handle_h,
248       button_w, grip_w, mwm_border_w, border_w,
249       bevel_w;
250   } frame;
251
252   BlackboxWindow(const BlackboxWindow&);
253   BlackboxWindow& operator=(const BlackboxWindow&);
254
255   bool getState(void);
256   Window createToplevelWindow();
257   Window createChildWindow(Window parent, Cursor = None);
258
259   void getWindowType(void);
260   void updateStrut(void);
261   void getWMName(void);
262   void getWMIconName(void);
263   void getWMNormalHints(void);
264   void getWMProtocols(void);
265   void getWMHints(void);
266   void getNetWMHints(void);
267   void getMWMHints(void);
268   bool getBlackboxHints(void);
269   void getTransientInfo(void);
270   void setNetWMAttributes(void);
271   void associateClientWindow(void);
272   void decorate(void);
273   void decorateLabel(void);
274   void positionButtons(bool redecorate_label = False);
275   void positionWindows(void);
276   void createHandle(void);
277   void destroyHandle(void);
278   void createTitlebar(void);
279   void destroyTitlebar(void);
280   void createCloseButton(void);
281   void destroyCloseButton(void);
282   void createIconifyButton(void);
283   void destroyIconifyButton(void);
284   void createMaximizeButton(void);
285   void destroyMaximizeButton(void);
286   void redrawWindowFrame(void) const;
287   void redrawLabel(void) const;
288   void redrawAllButtons(void) const;
289   void redrawCloseButton(bool pressed) const;
290   void redrawIconifyButton(bool pressed) const;
291   void redrawMaximizeButton(bool pressed) const;
292   void applyGravity(Rect &r);
293   void restoreGravity(Rect &r);
294   void setAllowedActions(void);
295   void setState(unsigned long new_state);
296   void upsize(void);
297   void doMove(int x_root, int y_root);
298   void endMove(void);
299   void doResize(int x_root, int y_root);
300   void endResize(void);
301
302   void constrain(Corner anchor, int *pw = 0, int *ph = 0);
303
304 public:
305   BlackboxWindow(Blackbox *b, Window w, BScreen *s);
306   virtual ~BlackboxWindow(void);
307
308   inline bool isTransient(void) const { return client.transient_for != 0; }
309   inline bool isFocused(void) const { return flags.focused; }
310   inline bool isVisible(void) const { return flags.visible; }
311   inline bool isIconic(void) const { return flags.iconic; }
312   inline bool isShaded(void) const { return flags.shaded; }
313   inline bool isMaximized(void) const { return flags.maximized; }
314   inline bool isMaximizedHoriz(void) const { return flags.maximized == 3; }
315   inline bool isMaximizedVert(void) const { return flags.maximized == 2; }
316   inline bool isMaximizedFull(void) const { return flags.maximized == 1; }
317   inline bool isStuck(void) const { return flags.stuck; }
318   inline bool isModal(void) const { return flags.modal; }
319   inline bool isIconifiable(void) const { return functions & Func_Iconify; }
320   inline bool isMaximizable(void) const { return functions & Func_Maximize; }
321   inline bool isResizable(void) const { return functions & Func_Resize; }
322   inline bool isClosable(void) const { return functions & Func_Close; }
323
324   // is a 'normal' window? meaning, a standard client application
325   inline bool isNormal(void) const
326   { return window_type == Type_Dialog || window_type == Type_Normal; }
327   inline bool isDesktop(void) const { return window_type == Type_Desktop; }
328   
329   inline bool hasTitlebar(void) const { return decorations & Decor_Titlebar; }
330
331   inline const BlackboxWindowList &getTransients(void) const
332   { return client.transientList; }
333   BlackboxWindow *getTransientFor(void) const;
334
335   inline BScreen *getScreen(void) const { return screen; }
336
337   inline Window getFrameWindow(void) const { return frame.window; }
338   inline Window getClientWindow(void) const { return client.window; }
339   inline Window getGroupWindow(void) const { return client.window_group; }
340
341   inline Windowmenu * getWindowmenu(void) const { return windowmenu; }
342
343   inline const char *getTitle(void) const
344   { return client.title.c_str(); }
345   inline const char *getIconTitle(void) const
346   { return client.icon_title.c_str(); }
347
348   inline unsigned int getWorkspaceNumber(void) const
349   { return blackbox_attrib.workspace; }
350   inline unsigned int getWindowNumber(void) const { return window_number; }
351
352   inline const Rect &frameRect(void) const { return frame.rect; }
353   inline const Rect &clientRect(void) const { return client.rect; }
354
355   inline unsigned int getTitleHeight(void) const
356   { return frame.title_h; }
357
358   inline void setWindowNumber(int n) { window_number = n; }
359
360   bool validateClient(void) const;
361   bool setInputFocus(void);
362
363   // none of these are used by the window manager, they are here to persist
364   // them properly in the window's netwm state property.
365   inline bool skipTaskbar(void) const { return flags.skip_taskbar; }
366   inline void setSkipTaskbar(const bool s) { flags.skip_taskbar = s; }
367   inline bool skipPager(void) const { return flags.skip_pager; }
368   inline void setSkipPager(const bool s) { flags.skip_pager = s; }
369   inline bool isFullscreen(void) const { return flags.fullscreen; }
370   inline void setFullscreen(const bool f) { flags.fullscreen = f; }
371
372   inline void setModal(const bool m) { flags.modal = m; }
373
374   void beginMove(int x_root, int y_root);
375   void beginResize(int x_root, int y_root, Corner dir);
376   void setFocusFlag(bool focus);
377   void iconify(void);
378   void deiconify(bool reassoc = True, bool raise = True);
379   void show(void);
380   void close(void);
381   void withdraw(void);
382   void maximize(unsigned int button);
383   void remaximize(void);
384   void shade(void);
385   void stick(void);
386   void reconfigure(void);
387   void grabButtons(void);
388   void ungrabButtons(void);
389   void installColormap(bool install);
390   void restore(bool remap);
391   void configure(int dx, int dy, unsigned int dw, unsigned int dh);
392   void setWorkspace(unsigned int n);
393   void changeBlackboxHints(const BlackboxHints *net);
394   void restoreAttributes(void);
395
396   void buttonPressEvent(const XButtonEvent *be);
397   void buttonReleaseEvent(const XButtonEvent *re);
398   void motionNotifyEvent(const XMotionEvent *me);
399   void destroyNotifyEvent(const XDestroyWindowEvent */*unused*/);
400   void mapRequestEvent(const XMapRequestEvent *mre);
401   void unmapNotifyEvent(const XUnmapEvent */*unused*/);
402   void reparentNotifyEvent(const XReparentEvent */*unused*/);
403   void propertyNotifyEvent(const XPropertyEvent *pe);
404   void exposeEvent(const XExposeEvent *ee);
405   void configureRequestEvent(const XConfigureRequestEvent *cr);
406
407 #ifdef    SHAPE
408   void configureShape(void);
409   void shapeEvent(XShapeEvent * /*unused*/);
410 #endif // SHAPE
411
412   virtual void timeout(void);
413 };
414
415
416 #endif // __Window_hh