1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
39 #include "wx/statusbr.h"
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
46 bool wxMWMIsRunning(Window w
);
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
52 void wxTopLevelWindowX11::Init()
55 m_maximizeOnShow
= FALSE
;
57 // unlike (almost?) all other windows, frames are created hidden
60 // Data to save/restore when calling ShowFullScreen
62 m_fsIsMaximized
= FALSE
;
63 m_fsIsShowing
= FALSE
;
65 m_needResizeInIdle
= FALSE
;
68 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
70 const wxString
& title
,
79 m_windowStyle
= style
;
84 m_windowId
= id
== -1 ? NewControlId() : id
;
87 parent
->AddChild(this);
89 wxTopLevelWindows
.Append(this);
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
);
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
98 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
100 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
101 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
117 XSetWindowAttributes xattributes
;
118 XSizeHints size_hints
;
120 long xattributes_mask
=
122 CWBorderPixel
| CWBackPixel
;
124 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
125 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
127 // TODO: if we want no border, caption etc.,
128 // I think we set this to True to remove decorations
130 xattributes
.override_redirect
= False
;
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());
138 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
139 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
141 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
142 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
144 m_mainWidget
= (WXWindow
) xwindow
;
148 extraFlags
|= GR_EVENT_MASK_CLOSE_REQ
;
151 XSelectInput( xdisplay
, xwindow
,
165 StructureNotifyMask
|
169 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
171 // Set background to None which will prevent X11 from clearing the
172 // background completely.
173 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
176 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
178 if (GetParent() && GetParent()->GetMainWindow())
180 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
181 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
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
);
193 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
194 wm_hints
.input
= True
;
195 wm_hints
.initial_state
= NormalState
;
196 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
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);
204 wxSetWMDecorations( xwindow
, style
);
211 wxTopLevelWindowX11::~wxTopLevelWindowX11()
213 wxTopLevelWindows
.DeleteObject(this);
215 // If this is the last top-level window, exit.
216 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
218 wxTheApp
->SetTopWindow(NULL
);
220 if (wxTheApp
->GetExitOnFrameDelete())
222 // Signal to the app that we're going to close
223 wxTheApp
->ExitMainLoop();
228 void wxTopLevelWindowX11::OnInternalIdle()
230 wxWindow::OnInternalIdle();
232 if (m_needResizeInIdle
)
234 wxSizeEvent
event( GetClientSize(), GetId() );
235 event
.SetEventObject( this );
236 GetEventHandler()->ProcessEvent( event
);
238 m_needResizeInIdle
= FALSE
;
242 // ----------------------------------------------------------------------------
243 // wxTopLevelWindowX11 showing
244 // ----------------------------------------------------------------------------
246 bool wxTopLevelWindowX11::Show(bool show
)
248 // Nano-X has to force a size event,
249 // else there's no initial size.
253 if (show
&& m_needResizeInIdle
)
256 wxSizeEvent
event(GetSize(), GetId());
257 event
.SetEventObject(this);
258 GetEventHandler()->ProcessEvent(event
);
260 m_needResizeInIdle
= FALSE
;
263 return wxWindowX11::Show(show
);
266 // ----------------------------------------------------------------------------
267 // wxTopLevelWindowX11 maximize/minimize
268 // ----------------------------------------------------------------------------
270 void wxTopLevelWindowX11::Maximize(bool maximize
)
275 bool wxTopLevelWindowX11::IsMaximized() const
281 void wxTopLevelWindowX11::Iconize(bool iconize
)
283 if (!m_iconized
&& GetMainWindow())
285 if (XIconifyWindow(wxGlobalDisplay(),
286 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
291 bool wxTopLevelWindowX11::IsIconized() const
296 void wxTopLevelWindowX11::Restore()
298 // This is the way to deiconify the window, according to the X FAQ
299 if (m_iconized
&& GetMainWindow())
301 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
306 // ----------------------------------------------------------------------------
307 // wxTopLevelWindowX11 fullscreen
308 // ----------------------------------------------------------------------------
310 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
317 m_fsIsShowing
= TRUE
;
329 m_fsIsShowing
= FALSE
;
336 // ----------------------------------------------------------------------------
337 // wxTopLevelWindowX11 misc
338 // ----------------------------------------------------------------------------
340 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
343 wxTopLevelWindowBase::SetIcon(icon
);
345 if (icon
.Ok() && GetMainWindow())
349 XWMHints
*wmHints
= XAllocWMHints();
350 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
352 wmHints
->flags
= IconPixmapHint
;
356 wmHints
->flags
|= IconMaskHint
;
357 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
360 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
366 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
371 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
372 (const char*) title
);
373 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
374 (const char*) title
);
376 // Use this if the platform doesn't supply the above functions.
378 XTextProperty textProperty
;
379 textProperty
.value
= (unsigned char*) title
;
380 textProperty
.encoding
= XA_STRING
;
381 textProperty
.format
= 8;
382 textProperty
.nitems
= 1;
384 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
385 & textProperty
, WM_NAME
);
390 wxString
wxTopLevelWindowX11::GetTitle() const
395 #ifndef MWM_DECOR_BORDER
396 /* bit definitions for MwmHints.flags */
397 #define MWM_HINTS_FUNCTIONS (1L << 0)
398 #define MWM_HINTS_DECORATIONS (1L << 1)
399 #define MWM_HINTS_INPUT_MODE (1L << 2)
400 #define MWM_HINTS_STATUS (1L << 3)
402 #define MWM_DECOR_ALL (1L << 0)
403 #define MWM_DECOR_BORDER (1L << 1)
404 #define MWM_DECOR_RESIZEH (1L << 2)
405 #define MWM_DECOR_TITLE (1L << 3)
406 #define MWM_DECOR_MENU (1L << 4)
407 #define MWM_DECOR_MINIMIZE (1L << 5)
408 #define MWM_DECOR_MAXIMIZE (1L << 6)
418 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
420 // Set the window manager decorations according to the
421 // given wxWindows style
422 bool wxSetWMDecorations(Window w
, long style
)
425 GR_WM_PROPERTIES wmProp
;
430 if (style
& wxRESIZE_BORDER
)
432 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
433 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
436 if (style
& wxSYSTEM_MENU
)
438 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
439 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
442 if ((style
& wxCAPTION
) ||
443 (style
& wxTINY_CAPTION_HORIZ
) ||
444 (style
& wxTINY_CAPTION_VERT
))
446 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
447 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
449 // The default dialog style doesn't include any kind
450 // of border, which is a bit odd. Anyway, inclusion
451 // of a caption surely implies a border.
452 style
|= wxTHICK_FRAME
;
455 if (style
& wxTHICK_FRAME
)
457 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
458 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
461 if (style
& wxSIMPLE_BORDER
)
463 wmProp
.props
|= GR_WM_PROPS_BORDER
;
464 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
467 if (style
& wxMINIMIZE_BOX
)
471 if (style
& wxMAXIMIZE_BOX
)
473 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
474 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
477 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
478 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
480 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
481 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
484 GrSetWMProperties(w
, & wmProp
);
487 if (!wxMWMIsRunning(w
))
490 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
493 hints
.decorations
= 0;
495 if (style
& wxRESIZE_BORDER
)
497 // wxLogDebug("MWM_DECOR_RESIZEH");
498 hints
.flags
|= MWM_HINTS_DECORATIONS
;
499 hints
.decorations
|= MWM_DECOR_RESIZEH
;
502 if (style
& wxSYSTEM_MENU
)
504 // wxLogDebug("MWM_DECOR_MENU");
505 hints
.flags
|= MWM_HINTS_DECORATIONS
;
506 hints
.decorations
|= MWM_DECOR_MENU
;
509 if ((style
& wxCAPTION
) ||
510 (style
& wxTINY_CAPTION_HORIZ
) ||
511 (style
& wxTINY_CAPTION_VERT
))
513 // wxLogDebug("MWM_DECOR_TITLE");
514 hints
.flags
|= MWM_HINTS_DECORATIONS
;
515 hints
.decorations
|= MWM_DECOR_TITLE
;
518 if ((style
& wxTHICK_FRAME
) || (style
& wxCAPTION
))
520 // wxLogDebug("MWM_DECOR_BORDER");
521 hints
.flags
|= MWM_HINTS_DECORATIONS
;
522 hints
.decorations
|= MWM_DECOR_BORDER
;
525 if (style
& wxMINIMIZE_BOX
)
527 // wxLogDebug("MWM_DECOR_MINIMIZE");
528 hints
.flags
|= MWM_HINTS_DECORATIONS
;
529 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
532 if (style
& wxMAXIMIZE_BOX
)
534 // wxLogDebug("MWM_DECOR_MAXIMIZE");
535 hints
.flags
|= MWM_HINTS_DECORATIONS
;
536 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
539 XChangeProperty(wxGlobalDisplay(),
541 mwm_wm_hints
, mwm_wm_hints
,
543 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
549 bool wxMWMIsRunning(Window w
)
554 Display
*dpy
= (Display
*)wxGetDisplay();
555 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
557 unsigned long length
, bytesafter
;
558 unsigned char value
[20];
559 unsigned char *ptr
= &value
[0];
563 type
= format
= length
= 0;
566 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
567 0L, 2, False
, motifWmInfo
,
568 &type
, &format
, &length
, &bytesafter
, &ptr
);
570 return (ret
== Success
);
574 // For implementation purposes - sometimes decorations make the client area
576 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
578 // wxFrame::GetClientAreaOrigin
579 // does the required calculation already.
580 return wxPoint(0, 0);
583 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
585 XSync(wxGlobalDisplay(), False
);
586 wxWindowX11::DoGetClientSize(width
, height
);
589 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
591 wxWindowX11::DoSetClientSize(width
, height
);
594 // Set the top-level window size
595 XSizeHints size_hints
;
596 wxSize oldSize
= GetSize();
597 wxSize oldClientSize
= GetClientSize();
599 size_hints
.flags
= PSize
;
600 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
601 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
602 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
605 // This seems to be necessary or resizes don't get performed
606 XSync(wxGlobalDisplay(), False
);
607 XSync(wxGlobalDisplay(), False
);
610 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
612 XSync(wxGlobalDisplay(), False
);
613 wxSize newSize
= GetSize();
614 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
619 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
622 // wxLogDebug( "Setting pos: %d, %d", x, y );
623 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
625 XSync(wxGlobalDisplay(), False
);
626 Window window
= (Window
) m_mainWidget
;
630 Display
*display
= (Display
*) GetXDisplay();
631 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
632 Window parent_window
= window
,
633 next_parent
= window
;
635 // search for the parent that is child of ROOT, because the WM may
636 // reparent twice and notify only the next parent (like FVWM)
637 while (next_parent
!= root
) {
644 parent_window
= next_parent
;
645 XQueryTree(display
, parent_window
, &root
,
646 &next_parent
, &theChildren
, &n
);
647 XFree(theChildren
); // not needed
650 XWindowChanges windowChanges
;
653 windowChanges
.width
= width
;
654 windowChanges
.height
= height
;
655 windowChanges
.stack_mode
= 0;
656 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
658 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
662 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
668 windowChanges
.width
= wxMax(1, width
);
669 valueMask
|= CWWidth
;
673 windowChanges
.height
= wxMax(1, height
);
674 valueMask
|= CWHeight
;
677 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
680 XSizeHints size_hints
;
681 size_hints
.flags
= 0;
682 if (x
> -1 && y
> -1)
683 size_hints
.flags
|= PPosition
;
684 if (width
> -1 && height
> -1)
685 size_hints
.flags
|= PSize
;
686 size_hints
.width
= width
;
687 size_hints
.height
= height
;
690 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
693 // This seems to be necessary or resizes don't get performed.
694 // Take them out (or even just one of them), and the About
695 // box of the minimal sample probably won't be resized right.
696 XSync(wxGlobalDisplay(), False
);
697 XSync(wxGlobalDisplay(), False
);
701 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
703 XSync(wxGlobalDisplay(), False
);
704 Window window
= (Window
) m_mainWidget
;
708 Display
*display
= (Display
*) GetXDisplay();
709 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
710 Window parent_window
= window
,
711 next_parent
= window
;
713 // search for the parent that is child of ROOT, because the WM may
714 // reparent twice and notify only the next parent (like FVWM)
715 while (next_parent
!= root
) {
722 parent_window
= next_parent
;
723 XQueryTree(display
, parent_window
, &root
,
724 &next_parent
, &theChildren
, &n
);
725 XFree(theChildren
); // not needed
728 int xx
, yy
; unsigned int dummy
;
729 XGetGeometry(display
, parent_window
, &root
,
730 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
734 XWindowAttributes attr
;
735 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);