Window management and sizer layout corrections
[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 m_focusWidget = NULL;
66 }
67
68 bool wxTopLevelWindowX11::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxString& title,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 // init our fields
77 Init();
78
79 m_windowStyle = style;
80 m_parent = parent;
81
82 SetName(name);
83
84 m_windowId = id == -1 ? NewControlId() : id;
85
86 if (parent)
87 parent->AddChild(this);
88
89 wxTopLevelWindows.Append(this);
90
91 Display *xdisplay = wxGlobalDisplay();
92 int xscreen = DefaultScreen( xdisplay );
93 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
94 Window xparent = RootWindow( xdisplay, xscreen );
95 Colormap cm = DefaultColormap( xdisplay, xscreen );
96
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
98 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
99 else
100 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
101 m_backgroundColour.CalcPixel( (WXColormap) cm );
102 m_hasBgCol = TRUE;
103
104 wxSize size2(size);
105 if (size2.x == -1)
106 size2.x = 100;
107 if (size2.y == -1)
108 size2.y = 100;
109
110 wxPoint pos2(pos);
111 if (pos2.x == -1)
112 pos2.x = 100;
113 if (pos2.y == -1)
114 pos2.y = 100;
115
116 #if !wxUSE_NANOX
117 XSetWindowAttributes xattributes;
118 XSizeHints size_hints;
119
120 long xattributes_mask =
121 CWOverrideRedirect |
122 CWBorderPixel | CWBackPixel;
123
124 xattributes.background_pixel = m_backgroundColour.GetPixel();
125 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
126
127 // TODO: if we want no border, caption etc.,
128 // I think we set this to True to remove decorations
129 // No. RR.
130 xattributes.override_redirect = False;
131 #endif
132
133 #if wxUSE_NANOX
134 long backColor, foreColor;
135 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
136 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
137
138 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
139 0, 0, InputOutput, xvisual, backColor, foreColor);
140 #else
141 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
142 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
143 #endif
144 m_mainWidget = (WXWindow) xwindow;
145
146 int extraFlags = 0;
147 #if wxUSE_NANOX
148 extraFlags |= GR_EVENT_MASK_CLOSE_REQ;
149 #endif
150
151 XSelectInput( xdisplay, xwindow,
152 extraFlags |
153 ExposureMask |
154 KeyPressMask |
155 KeyReleaseMask |
156 ButtonPressMask |
157 ButtonReleaseMask |
158 ButtonMotionMask |
159 EnterWindowMask |
160 LeaveWindowMask |
161 PointerMotionMask |
162 KeymapStateMask |
163 FocusChangeMask |
164 ColormapChangeMask |
165 StructureNotifyMask |
166 ConfigureNotify |
167 PropertyChangeMask
168 );
169
170 wxAddWindowToTable( xwindow, (wxWindow*) this );
171
172 // Set background to None which will prevent X11 from clearing the
173 // background completely.
174 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
175
176 #if !wxUSE_NANOX
177 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
178 {
179 if (GetParent() && GetParent()->GetMainWindow())
180 {
181 Window xparentwindow = (Window) GetParent()->GetMainWindow();
182 XSetTransientForHint( xdisplay, xwindow, xparentwindow );
183 }
184 }
185
186 size_hints.flags = PSize | PPosition;
187 size_hints.x = pos2.x;
188 size_hints.y = pos2.y;
189 size_hints.width = size2.x;
190 size_hints.height = size2.y;
191 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
192
193 XWMHints wm_hints;
194 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
195 wm_hints.input = True;
196 wm_hints.initial_state = NormalState;
197 XSetWMHints( xdisplay, xwindow, &wm_hints);
198
199 Atom wm_protocols[2];
200 wm_protocols[0] = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False );
201 wm_protocols[1] = XInternAtom( xdisplay, "WM_TAKE_FOCUS", False );
202 XSetWMProtocols( xdisplay, xwindow, wm_protocols, 2);
203 #endif
204
205 wxSetWMDecorations( xwindow, style);
206
207 SetTitle(title);
208
209 return TRUE;
210 }
211
212 wxTopLevelWindowX11::~wxTopLevelWindowX11()
213 {
214 wxTopLevelWindows.DeleteObject(this);
215
216 // If this is the last top-level window, exit.
217 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
218 {
219 wxTheApp->SetTopWindow(NULL);
220
221 if (wxTheApp->GetExitOnFrameDelete())
222 {
223 // Signal to the app that we're going to close
224 wxTheApp->ExitMainLoop();
225 }
226 }
227 }
228
229 // ----------------------------------------------------------------------------
230 // wxTopLevelWindowX11 showing
231 // ----------------------------------------------------------------------------
232
233 bool wxTopLevelWindowX11::Show(bool show)
234 {
235 return wxWindowX11::Show(show);
236 }
237
238 // ----------------------------------------------------------------------------
239 // wxTopLevelWindowX11 maximize/minimize
240 // ----------------------------------------------------------------------------
241
242 void wxTopLevelWindowX11::Maximize(bool maximize)
243 {
244 // TODO
245 }
246
247 bool wxTopLevelWindowX11::IsMaximized() const
248 {
249 // TODO
250 return TRUE;
251 }
252
253 void wxTopLevelWindowX11::Iconize(bool iconize)
254 {
255 if (!m_iconized && GetMainWindow())
256 {
257 if (XIconifyWindow(wxGlobalDisplay(),
258 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
259 m_iconized = TRUE;
260 }
261 }
262
263 bool wxTopLevelWindowX11::IsIconized() const
264 {
265 return m_iconized;
266 }
267
268 void wxTopLevelWindowX11::Restore()
269 {
270 // This is the way to deiconify the window, according to the X FAQ
271 if (m_iconized && GetMainWindow())
272 {
273 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
274 m_iconized = FALSE;
275 }
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxTopLevelWindowX11 fullscreen
280 // ----------------------------------------------------------------------------
281
282 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
283 {
284 if (show)
285 {
286 if (IsFullScreen())
287 return FALSE;
288
289 m_fsIsShowing = TRUE;
290 m_fsStyle = style;
291
292 // TODO
293
294 return TRUE;
295 }
296 else
297 {
298 if (!IsFullScreen())
299 return FALSE;
300
301 m_fsIsShowing = FALSE;
302
303 // TODO
304 return TRUE;
305 }
306 }
307
308 // ----------------------------------------------------------------------------
309 // wxTopLevelWindowX11 misc
310 // ----------------------------------------------------------------------------
311
312 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
313 {
314 // this sets m_icon
315 wxTopLevelWindowBase::SetIcon(icon);
316
317 if (icon.Ok() && GetMainWindow())
318 {
319 #if wxUSE_NANOX
320 #else
321 XWMHints *wmHints = XAllocWMHints();
322 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
323
324 wmHints->flags = IconPixmapHint;
325
326 if (icon.GetMask())
327 {
328 wmHints->flags |= IconMaskHint;
329 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
330 }
331
332 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
333 XFree(wmHints);
334 #endif
335 }
336 }
337
338 void wxTopLevelWindowX11::SetTitle(const wxString& title)
339 {
340 m_title = title;
341 if (GetMainWindow())
342 {
343 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
344 (const char*) title);
345 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
346 (const char*) title);
347
348 // Use this if the platform doesn't supply the above functions.
349 #if 0
350 XTextProperty textProperty;
351 textProperty.value = (unsigned char*) title;
352 textProperty.encoding = XA_STRING;
353 textProperty.format = 8;
354 textProperty.nitems = 1;
355
356 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
357 & textProperty, WM_NAME);
358 #endif
359 }
360 }
361
362 wxString wxTopLevelWindowX11::GetTitle() const
363 {
364 return m_title;
365 }
366
367 #ifndef MWM_DECOR_BORDER
368 /* bit definitions for MwmHints.flags */
369 #define MWM_HINTS_FUNCTIONS (1L << 0)
370 #define MWM_HINTS_DECORATIONS (1L << 1)
371 #define MWM_HINTS_INPUT_MODE (1L << 2)
372 #define MWM_HINTS_STATUS (1L << 3)
373
374 #define MWM_DECOR_ALL (1L << 0)
375 #define MWM_DECOR_BORDER (1L << 1)
376 #define MWM_DECOR_RESIZEH (1L << 2)
377 #define MWM_DECOR_TITLE (1L << 3)
378 #define MWM_DECOR_MENU (1L << 4)
379 #define MWM_DECOR_MINIMIZE (1L << 5)
380 #define MWM_DECOR_MAXIMIZE (1L << 6)
381 #endif
382
383 struct MwmHints {
384 long flags;
385 long functions;
386 long decorations;
387 long input_mode;
388 };
389
390 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
391
392 // Set the window manager decorations according to the
393 // given wxWindows style
394 bool wxSetWMDecorations(Window w, long style)
395 {
396 #if wxUSE_NANOX
397 GR_WM_PROPERTIES wmProp;
398
399 wmProp.flags = 0;
400 wmProp.props = 0;
401
402 if (style & wxRESIZE_BORDER)
403 {
404 wmProp.props |= GR_WM_PROPS_APPFRAME ;
405 wmProp.flags |= GR_WM_FLAGS_PROPS ;
406 }
407
408 if (style & wxSYSTEM_MENU)
409 {
410 wmProp.props |= GR_WM_PROPS_CLOSEBOX ;
411 wmProp.flags |= GR_WM_FLAGS_PROPS ;
412 }
413
414 if ((style & wxCAPTION) ||
415 (style & wxTINY_CAPTION_HORIZ) ||
416 (style & wxTINY_CAPTION_VERT))
417 {
418 wmProp.props |= GR_WM_PROPS_CAPTION ;
419 wmProp.flags |= GR_WM_FLAGS_PROPS ;
420
421 // The default dialog style doesn't include any kind
422 // of border, which is a bit odd. Anyway, inclusion
423 // of a caption surely implies a border.
424 style |= wxTHICK_FRAME;
425 }
426
427 if (style & wxTHICK_FRAME)
428 {
429 wmProp.props |= GR_WM_PROPS_APPFRAME ;
430 wmProp.flags |= GR_WM_FLAGS_PROPS ;
431 }
432
433 if (style & wxSIMPLE_BORDER)
434 {
435 wmProp.props |= GR_WM_PROPS_BORDER ;
436 wmProp.flags |= GR_WM_FLAGS_PROPS ;
437 }
438
439 if (style & wxMINIMIZE_BOX)
440 {
441 }
442
443 if (style & wxMAXIMIZE_BOX)
444 {
445 wmProp.props |= GR_WM_PROPS_MAXIMIZE ;
446 wmProp.flags |= GR_WM_FLAGS_PROPS ;
447 }
448
449 if (((style & wxBORDER) != wxBORDER) && ((style & wxTHICK_FRAME) != wxTHICK_FRAME)
450 && ((style & wxRESIZE_BORDER) != wxRESIZE_BORDER))
451 {
452 wmProp.props |= GR_WM_PROPS_NODECORATE ;
453 wmProp.flags |= GR_WM_FLAGS_PROPS ;
454 }
455
456 GrSetWMProperties(w, & wmProp);
457
458 #else
459 if (!wxMWMIsRunning(w))
460 return FALSE;
461
462 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
463 MwmHints hints;
464 hints.flags = 0;
465 hints.decorations = 0;
466
467 if (style & wxRESIZE_BORDER)
468 {
469 // wxLogDebug("MWM_DECOR_RESIZEH");
470 hints.flags |= MWM_HINTS_DECORATIONS;
471 hints.decorations |= MWM_DECOR_RESIZEH;
472 }
473
474 if (style & wxSYSTEM_MENU)
475 {
476 // wxLogDebug("MWM_DECOR_MENU");
477 hints.flags |= MWM_HINTS_DECORATIONS;
478 hints.decorations |= MWM_DECOR_MENU;
479 }
480
481 if ((style & wxCAPTION) ||
482 (style & wxTINY_CAPTION_HORIZ) ||
483 (style & wxTINY_CAPTION_VERT))
484 {
485 // wxLogDebug("MWM_DECOR_TITLE");
486 hints.flags |= MWM_HINTS_DECORATIONS;
487 hints.decorations |= MWM_DECOR_TITLE;
488 }
489
490 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
491 {
492 // wxLogDebug("MWM_DECOR_BORDER");
493 hints.flags |= MWM_HINTS_DECORATIONS;
494 hints.decorations |= MWM_DECOR_BORDER;
495 }
496
497 if (style & wxMINIMIZE_BOX)
498 {
499 // wxLogDebug("MWM_DECOR_MINIMIZE");
500 hints.flags |= MWM_HINTS_DECORATIONS;
501 hints.decorations |= MWM_DECOR_MINIMIZE;
502 }
503
504 if (style & wxMAXIMIZE_BOX)
505 {
506 // wxLogDebug("MWM_DECOR_MAXIMIZE");
507 hints.flags |= MWM_HINTS_DECORATIONS;
508 hints.decorations |= MWM_DECOR_MAXIMIZE;
509 }
510
511 XChangeProperty(wxGlobalDisplay(),
512 w,
513 mwm_wm_hints, mwm_wm_hints,
514 32, PropModeReplace,
515 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
516
517 #endif
518 return TRUE;
519 }
520
521 bool wxMWMIsRunning(Window w)
522 {
523 #if wxUSE_NANOX
524 return FALSE;
525 #else
526 Display *dpy = (Display*)wxGetDisplay();
527 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
528
529 unsigned long length, bytesafter;
530 unsigned char value[20];
531 unsigned char *ptr = &value[0];
532 int ret, format;
533 Atom type;
534
535 type = format = length = 0;
536 value[0] = 0;
537
538 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
539 0L, 2, False, motifWmInfo,
540 &type, &format, &length, &bytesafter, &ptr);
541
542 return (ret == Success);
543 #endif
544 }
545
546 // For implementation purposes - sometimes decorations make the client area
547 // smaller
548 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
549 {
550 // In fact wxFrame::GetClientAreaOrigin
551 // does the required calculation already.
552 #if 0
553 if (this->IsKindOf(CLASSINFO(wxFrame)))
554 {
555 wxFrame* frame = (wxFrame*) this;
556 if (frame->GetMenuBar())
557 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
558 }
559 #endif
560 return wxPoint(0, 0);
561 }
562
563 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
564 {
565 wxWindowX11::DoGetClientSize(width, height);
566 // Done by wxTopLevelWindow
567 #if 0
568 if (this->IsKindOf(CLASSINFO(wxFrame)))
569 {
570 wxFrame* frame = (wxFrame*) this;
571 if (frame->GetMenuBar())
572 (*height) -= frame->GetMenuBar()->GetSize().y;
573 if (frame->GetStatusBar())
574 (*height) -= frame->GetStatusBar()->GetSize().y;
575 }
576 #endif
577 }
578
579 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
580 {
581 wxWindowX11::DoSetClientSize(width, height);
582
583 #if 0
584 if (!GetMainWindow())
585 return;
586
587 XWindowChanges windowChanges;
588 int valueMask = 0;
589
590 if (width != -1)
591 {
592 windowChanges.width = width ;
593 valueMask |= CWWidth;
594 }
595 if (height != -1)
596 {
597 windowChanges.height = height ;
598 valueMask |= CWHeight;
599 }
600 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
601 valueMask, & windowChanges);
602 #endif
603 }
604
605 void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
606 {
607 // wxLogDebug( "Setting pos: %d, %d", x, y );
608 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
609
610 wxPoint pt = GetPosition();
611 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
612 #if 0
613 XSync(wxGlobalDisplay(), False);
614 int w, h;
615 GetSize(& w, & h);
616 wxString msg;
617 msg.Printf("Before setting size: %d, %d", w, h);
618 wxLogDebug(msg);
619 if (!GetMainWindow())
620 return;
621
622 XWindowChanges windowChanges;
623 int valueMask = 0;
624
625 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
626 {
627 int yy = 0;
628 AdjustForParentClientOrigin( x, yy, sizeFlags);
629 windowChanges.x = x;
630 valueMask |= CWX;
631 }
632 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
633 {
634 int xx = 0;
635 AdjustForParentClientOrigin( xx, y, sizeFlags);
636 windowChanges.y = y;
637 valueMask |= CWY;
638 }
639 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
640 {
641 windowChanges.width = width /* - m_borderSize*2 */;
642 valueMask |= CWWidth;
643 }
644 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
645 {
646 windowChanges.height = height /* -m_borderSize*2*/;
647 valueMask |= CWHeight;
648 }
649
650 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
651 valueMask, & windowChanges);
652 XSync(wxGlobalDisplay(), False);
653 GetSize(& w, & h);
654 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
655 wxLogDebug(msg);
656 #endif
657 }
658
659 void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
660 {
661 XSync(wxGlobalDisplay(), False);
662 Window window = (Window) m_mainWidget;
663 if (window)
664 {
665 int offsetX = 0;
666 int offsetY = 0;
667
668 #if !wxUSE_NANOX
669 // wxLogDebug("Translating...");
670 Window childWindow;
671 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
672 0, 0, & offsetX, & offsetY, & childWindow);
673
674 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
675 #endif
676
677 XWindowAttributes attr;
678 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
679 wxASSERT(status);
680
681 if (status)
682 {
683 *x = attr.x + offsetX;
684 *y = attr.y + offsetY;
685 }
686 }
687 }