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