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