Updated the erase/paint event logic.
[wxWidgets.git] / src / x11 / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/frame.h"
38 #include "wx/menu.h"
39 #include "wx/statusbr.h"
40 #endif //WX_PRECOMP
41
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
45
46 bool wxMWMIsRunning(Window w);
47
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
51
52 void wxTopLevelWindowX11::Init()
53 {
54 m_iconized =
55 m_maximizeOnShow = FALSE;
56
57 // unlike (almost?) all other windows, frames are created hidden
58 m_isShown = FALSE;
59
60 // Data to save/restore when calling ShowFullScreen
61 m_fsStyle = 0;
62 m_fsIsMaximized = FALSE;
63 m_fsIsShowing = FALSE;
64 }
65
66 bool wxTopLevelWindowX11::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString& title,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString& name)
73 {
74 // init our fields
75 Init();
76
77 m_windowStyle = style;
78 m_parent = parent;
79
80 SetName(name);
81
82 m_windowId = id == -1 ? NewControlId() : id;
83
84 if (parent)
85 parent->AddChild(this);
86
87 wxTopLevelWindows.Append(this);
88
89 Display *xdisplay = wxGlobalDisplay();
90 int xscreen = DefaultScreen( xdisplay );
91 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
92 Window xparent = RootWindow( xdisplay, xscreen );
93 Colormap cm = DefaultColormap( xdisplay, xscreen );
94
95 // TODO: For dialogs, this should be wxSYS_COLOUR_3DFACE
96 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
97 m_backgroundColour.CalcPixel( (WXColormap) cm );
98 m_hasBgCol = TRUE;
99
100 XSetWindowAttributes xattributes;
101 XSizeHints size_hints;
102 XWMHints wm_hints;
103
104 long xattributes_mask =
105 CWOverrideRedirect |
106 CWBorderPixel | CWBackPixel;
107 xattributes.background_pixel = m_backgroundColour.GetPixel();
108 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
109
110 // TODO: if we want no border, caption etc.,
111 // I think we set this to True to remove decorations
112 xattributes.override_redirect = False;
113
114 wxSize size2(size);
115 if (size2.x == -1)
116 size2.x = 100;
117 if (size2.y == -1)
118 size2.y = 100;
119
120 wxPoint pos2(pos);
121 if (pos2.x == -1)
122 pos2.x = 100;
123 if (pos2.y == -1)
124 pos2.y = 100;
125
126 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
127 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
128 m_mainWidget = (WXWindow) xwindow;
129
130 XSelectInput( xdisplay, xwindow,
131 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
132 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
133 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
134 PropertyChangeMask );
135
136 wxAddWindowToTable( xwindow, (wxWindow*) this );
137
138 // Set background to None which will prevent X11 from clearing the
139 // background completely.
140 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
141
142 // Messes up window management
143 // XSetTransientForHint( xdisplay, xwindow, xparent );
144
145 size_hints.flags = PSize;
146 size_hints.width = size2.x;
147 size_hints.height = size2.y;
148 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
149
150 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
151 wm_hints.input = True;
152 wm_hints.initial_state = NormalState;
153 XSetWMHints( xdisplay, xwindow, &wm_hints);
154
155 Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False);
156 XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1);
157
158 wxSetWMDecorations((Window) GetMainWindow(), style);
159
160 SetTitle(title);
161
162 return TRUE;
163 }
164
165 wxTopLevelWindowX11::~wxTopLevelWindowX11()
166 {
167 wxTopLevelWindows.DeleteObject(this);
168
169 // If this is the last top-level window, exit.
170 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
171 {
172 wxTheApp->SetTopWindow(NULL);
173
174 if (wxTheApp->GetExitOnFrameDelete())
175 {
176 // Signal to the app that we're going to close
177 wxTheApp->ExitMainLoop();
178 }
179 }
180 }
181
182 // ----------------------------------------------------------------------------
183 // wxTopLevelWindowX11 showing
184 // ----------------------------------------------------------------------------
185
186 bool wxTopLevelWindowX11::Show(bool show)
187 {
188 return wxWindowX11::Show(show);
189 }
190
191 // ----------------------------------------------------------------------------
192 // wxTopLevelWindowX11 maximize/minimize
193 // ----------------------------------------------------------------------------
194
195 void wxTopLevelWindowX11::Maximize(bool maximize)
196 {
197 // TODO
198 }
199
200 bool wxTopLevelWindowX11::IsMaximized() const
201 {
202 // TODO
203 return TRUE;
204 }
205
206 void wxTopLevelWindowX11::Iconize(bool iconize)
207 {
208 if (!m_iconized && GetMainWindow())
209 {
210 if (XIconifyWindow(wxGlobalDisplay(),
211 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
212 m_iconized = TRUE;
213 }
214 }
215
216 bool wxTopLevelWindowX11::IsIconized() const
217 {
218 return m_iconized;
219 }
220
221 void wxTopLevelWindowX11::Restore()
222 {
223 // This is the way to deiconify the window, according to the X FAQ
224 if (m_iconized && GetMainWindow())
225 {
226 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
227 m_iconized = FALSE;
228 }
229 }
230
231 // ----------------------------------------------------------------------------
232 // wxTopLevelWindowX11 fullscreen
233 // ----------------------------------------------------------------------------
234
235 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
236 {
237 if (show)
238 {
239 if (IsFullScreen())
240 return FALSE;
241
242 m_fsIsShowing = TRUE;
243 m_fsStyle = style;
244
245 // TODO
246
247 return TRUE;
248 }
249 else
250 {
251 if (!IsFullScreen())
252 return FALSE;
253
254 m_fsIsShowing = FALSE;
255
256 // TODO
257 return TRUE;
258 }
259 }
260
261 // ----------------------------------------------------------------------------
262 // wxTopLevelWindowX11 misc
263 // ----------------------------------------------------------------------------
264
265 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
266 {
267 // this sets m_icon
268 wxTopLevelWindowBase::SetIcon(icon);
269
270 if (icon.Ok() && GetMainWindow())
271 {
272 XWMHints *wmHints = XAllocWMHints();
273 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
274
275 wmHints->flags = IconPixmapHint;
276
277 if (icon.GetMask())
278 {
279 wmHints->flags |= IconMaskHint;
280 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
281 }
282
283 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
284 XFree(wmHints);
285 }
286 }
287
288 void wxTopLevelWindowX11::SetTitle(const wxString& title)
289 {
290 m_title = title;
291 if (GetMainWindow())
292 {
293 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
294 (const char*) title);
295 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
296 (const char*) title);
297
298 // Use this if the platform doesn't supply the above functions.
299 #if 0
300 XTextProperty textProperty;
301 textProperty.value = (unsigned char*) title;
302 textProperty.encoding = XA_STRING;
303 textProperty.format = 8;
304 textProperty.nitems = 1;
305
306 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
307 & textProperty, WM_NAME);
308 #endif
309 }
310 }
311
312 wxString wxTopLevelWindowX11::GetTitle() const
313 {
314 return m_title;
315 }
316
317 #ifndef MWM_DECOR_BORDER
318 /* bit definitions for MwmHints.flags */
319 #define MWM_HINTS_FUNCTIONS (1L << 0)
320 #define MWM_HINTS_DECORATIONS (1L << 1)
321 #define MWM_HINTS_INPUT_MODE (1L << 2)
322 #define MWM_HINTS_STATUS (1L << 3)
323
324 #define MWM_DECOR_ALL (1L << 0)
325 #define MWM_DECOR_BORDER (1L << 1)
326 #define MWM_DECOR_RESIZEH (1L << 2)
327 #define MWM_DECOR_TITLE (1L << 3)
328 #define MWM_DECOR_MENU (1L << 4)
329 #define MWM_DECOR_MINIMIZE (1L << 5)
330 #define MWM_DECOR_MAXIMIZE (1L << 6)
331 #endif
332
333 struct MwmHints {
334 long flags;
335 long functions;
336 long decorations;
337 long input_mode;
338 };
339
340 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
341
342 // Set the window manager decorations according to the
343 // given wxWindows style
344 bool wxSetWMDecorations(Window w, long style)
345 {
346 if (!wxMWMIsRunning(w))
347 return FALSE;
348
349 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
350 MwmHints hints;
351 hints.flags = 0;
352 hints.decorations = 0;
353
354 if (style & wxRESIZE_BORDER)
355 {
356 wxLogDebug("MWM_DECOR_RESIZEH");
357 hints.flags |= MWM_HINTS_DECORATIONS;
358 hints.decorations |= MWM_DECOR_RESIZEH;
359 }
360
361 if (style & wxSYSTEM_MENU)
362 {
363 wxLogDebug("MWM_DECOR_MENU");
364 hints.flags |= MWM_HINTS_DECORATIONS;
365 hints.decorations |= MWM_DECOR_MENU;
366 }
367
368 if ((style & wxCAPTION) ||
369 (style & wxTINY_CAPTION_HORIZ) ||
370 (style & wxTINY_CAPTION_VERT))
371 {
372 wxLogDebug("MWM_DECOR_TITLE");
373 hints.flags |= MWM_HINTS_DECORATIONS;
374 hints.decorations |= MWM_DECOR_TITLE;
375 }
376
377 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
378 {
379 wxLogDebug("MWM_DECOR_BORDER");
380 hints.flags |= MWM_HINTS_DECORATIONS;
381 hints.decorations |= MWM_DECOR_BORDER;
382 }
383
384 if (style & wxMINIMIZE_BOX)
385 {
386 wxLogDebug("MWM_DECOR_MINIMIZE");
387 hints.flags |= MWM_HINTS_DECORATIONS;
388 hints.decorations |= MWM_DECOR_MINIMIZE;
389 }
390
391 if (style & wxMAXIMIZE_BOX)
392 {
393 wxLogDebug("MWM_DECOR_MAXIMIZE");
394 hints.flags |= MWM_HINTS_DECORATIONS;
395 hints.decorations |= MWM_DECOR_MAXIMIZE;
396 }
397
398 XChangeProperty(wxGlobalDisplay(),
399 w,
400 mwm_wm_hints, mwm_wm_hints,
401 32, PropModeReplace,
402 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
403
404 return TRUE;
405 }
406
407 bool wxMWMIsRunning(Window w)
408 {
409 Display *dpy = (Display*)wxGetDisplay();
410 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
411
412 unsigned long length, bytesafter;
413 unsigned char value[20];
414 unsigned char *ptr = &value[0];
415 int ret, format;
416 Atom type;
417
418 type = format = length = 0;
419 value[0] = 0;
420
421 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
422 0L, 2, False, motifWmInfo,
423 &type, &format, &length, &bytesafter, &ptr);
424
425 return (ret == Success);
426 }
427
428 // For implementation purposes - sometimes decorations make the client area
429 // smaller
430 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
431 {
432 // In fact wxFrame::GetClientAreaOrigin
433 // does the required calculation already.
434 #if 0
435 if (this->IsKindOf(CLASSINFO(wxFrame)))
436 {
437 wxFrame* frame = (wxFrame*) this;
438 if (frame->GetMenuBar())
439 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
440 }
441 #endif
442 return wxPoint(0, 0);
443 }
444
445 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
446 {
447 wxWindowX11::DoGetClientSize(width, height);
448 // Done by wxTopLevelWindow
449 #if 0
450 if (this->IsKindOf(CLASSINFO(wxFrame)))
451 {
452 wxFrame* frame = (wxFrame*) this;
453 if (frame->GetMenuBar())
454 (*height) -= frame->GetMenuBar()->GetSize().y;
455 if (frame->GetStatusBar())
456 (*height) -= frame->GetStatusBar()->GetSize().y;
457 }
458 #endif
459 }
460
461 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
462 {
463 wxWindowX11::DoSetClientSize(width, height);
464
465 #if 0
466 if (!GetMainWindow())
467 return;
468
469 XWindowChanges windowChanges;
470 int valueMask = 0;
471
472 if (width != -1)
473 {
474 windowChanges.width = width ;
475 valueMask |= CWWidth;
476 }
477 if (height != -1)
478 {
479 windowChanges.height = height ;
480 valueMask |= CWHeight;
481 }
482 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
483 valueMask, & windowChanges);
484 #endif
485 }
486
487 void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
488 {
489 wxString msg;
490 msg.Printf("Setting pos: %d, %d", x, y);
491 wxLogDebug(msg);
492 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
493
494 wxPoint pt = GetPosition();
495 msg.Printf("After, pos: %d, %d", pt.x, pt.y);
496 wxLogDebug(msg);
497 #if 0
498 XSync(wxGlobalDisplay(), False);
499 int w, h;
500 GetSize(& w, & h);
501 wxString msg;
502 msg.Printf("Before setting size: %d, %d", w, h);
503 wxLogDebug(msg);
504 if (!GetMainWindow())
505 return;
506
507 XWindowChanges windowChanges;
508 int valueMask = 0;
509
510 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
511 {
512 int yy = 0;
513 AdjustForParentClientOrigin( x, yy, sizeFlags);
514 windowChanges.x = x;
515 valueMask |= CWX;
516 }
517 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
518 {
519 int xx = 0;
520 AdjustForParentClientOrigin( xx, y, sizeFlags);
521 windowChanges.y = y;
522 valueMask |= CWY;
523 }
524 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
525 {
526 windowChanges.width = width /* - m_borderSize*2 */;
527 valueMask |= CWWidth;
528 }
529 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
530 {
531 windowChanges.height = height /* -m_borderSize*2*/;
532 valueMask |= CWHeight;
533 }
534
535 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
536 valueMask, & windowChanges);
537 XSync(wxGlobalDisplay(), False);
538 GetSize(& w, & h);
539 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
540 wxLogDebug(msg);
541 #endif
542 }
543
544 void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
545 {
546 XSync(wxGlobalDisplay(), False);
547 Window window = (Window) m_mainWidget;
548 if (window)
549 {
550 int offsetX = 0;
551 int offsetY = 0;
552
553 wxLogDebug("Translating...");
554 Window childWindow;
555 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
556 0, 0, & offsetX, & offsetY, & childWindow);
557
558 wxString msg;
559 msg.Printf("Offset: %d, %d", offsetX, offsetY);
560 wxLogDebug(msg);
561
562 XWindowAttributes attr;
563 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
564 wxASSERT(status);
565
566 if (status)
567 {
568 *x = attr.x + offsetX;
569 *y = attr.y + offsetY;
570 }
571 }
572 }