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
=
121 CWBorderPixel
| CWBackPixel
;
123 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
124 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
126 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
128 xattributes_mask
|= CWBitGravity
;
129 xattributes
.bit_gravity
= StaticGravity
;
132 // TODO: if we want no border, caption etc.,
133 // I think we set this to True to remove decorations
135 // Yes :-) JACS (because some WMs don't respect
137 // xattributes.override_redirect = (style & wxNO_BORDER) ? True : False;
139 xattributes_mask
|= CWEventMask
;
140 xattributes
.event_mask
=
141 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
142 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
143 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
146 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
147 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
149 long backColor
, foreColor
;
150 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
151 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
153 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
154 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
157 m_mainWidget
= (WXWindow
) xwindow
;
160 XSelectInput( xdisplay
, xwindow
,
161 GR_EVENT_MASK_CLOSE_REQ
|
174 StructureNotifyMask
|
179 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
181 // Set background to None which will prevent X11 from clearing the
182 // background completely.
183 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
186 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
188 if (GetParent() && GetParent()->GetMainWindow())
190 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
191 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
195 size_hints
.flags
= PSize
| PPosition
;
196 size_hints
.x
= pos2
.x
;
197 size_hints
.y
= pos2
.y
;
198 size_hints
.width
= size2
.x
;
199 size_hints
.height
= size2
.y
;
200 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
203 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
204 wm_hints
.input
= True
;
205 wm_hints
.initial_state
= NormalState
;
206 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
208 Atom wm_protocols
[2];
209 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
210 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
211 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
214 // You will need a compliant window manager for this to work
215 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
216 if (style
& wxSTAY_ON_TOP
)
218 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
219 XChangeProperty (xdisplay
,
221 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
225 (unsigned char *)&data
,
232 wxSetWMDecorations( xwindow
, style
);
239 wxTopLevelWindowX11
::~wxTopLevelWindowX11()
241 wxTopLevelWindows
.DeleteObject(this);
243 // If this is the last top-level window, exit.
244 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
246 wxTheApp
->SetTopWindow(NULL
);
248 if (wxTheApp
->GetExitOnFrameDelete())
250 // Signal to the app that we're going to close
251 wxTheApp
->ExitMainLoop();
256 void wxTopLevelWindowX11
::OnInternalIdle()
258 wxWindow
::OnInternalIdle();
260 if (m_needResizeInIdle
)
262 wxSizeEvent
event( GetClientSize(), GetId() );
263 event
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent( event
);
266 m_needResizeInIdle
= FALSE
;
270 // ----------------------------------------------------------------------------
271 // wxTopLevelWindowX11 showing
272 // ----------------------------------------------------------------------------
274 bool wxTopLevelWindowX11
::Show(bool show
)
276 // Nano-X has to force a size event,
277 // else there's no initial size.
281 if (show
&& m_needResizeInIdle
)
284 wxSizeEvent
event(GetSize(), GetId());
285 event
.SetEventObject(this);
286 GetEventHandler()->ProcessEvent(event
);
288 m_needResizeInIdle
= FALSE
;
293 // This does the layout _before_ the
294 // window is shown, else the items are
295 // drawn first at the wrong positions,
296 // then at the correct positions.
303 return wxWindowX11
::Show(show
);
306 // ----------------------------------------------------------------------------
307 // wxTopLevelWindowX11 maximize/minimize
308 // ----------------------------------------------------------------------------
310 void wxTopLevelWindowX11
::Maximize(bool maximize
)
315 bool wxTopLevelWindowX11
::IsMaximized() const
321 void wxTopLevelWindowX11
::Iconize(bool iconize
)
323 if (!m_iconized
&& GetMainWindow())
325 if (XIconifyWindow(wxGlobalDisplay(),
326 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
331 bool wxTopLevelWindowX11
::IsIconized() const
336 void wxTopLevelWindowX11
::Restore()
338 // This is the way to deiconify the window, according to the X FAQ
339 if (m_iconized
&& GetMainWindow())
341 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
346 // ----------------------------------------------------------------------------
347 // wxTopLevelWindowX11 fullscreen
348 // ----------------------------------------------------------------------------
350 bool wxTopLevelWindowX11
::ShowFullScreen(bool show
, long style
)
357 m_fsIsShowing
= TRUE
;
369 m_fsIsShowing
= FALSE
;
376 // ----------------------------------------------------------------------------
377 // wxTopLevelWindowX11 misc
378 // ----------------------------------------------------------------------------
380 void wxTopLevelWindowX11
::SetIcon(const wxIcon
& icon
)
383 wxTopLevelWindowBase
::SetIcon(icon
);
385 if (icon
.Ok() && GetMainWindow())
389 XWMHints
*wmHints
= XAllocWMHints();
390 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
392 wmHints
->flags
= IconPixmapHint
;
396 wmHints
->flags
|= IconMaskHint
;
397 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
400 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
406 void wxTopLevelWindowX11
::SetTitle(const wxString
& title
)
411 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
412 (const char*) title
);
413 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
414 (const char*) title
);
416 // Use this if the platform doesn't supply the above functions.
418 XTextProperty textProperty
;
419 textProperty
.value
= (unsigned char*) title
;
420 textProperty
.encoding
= XA_STRING
;
421 textProperty
.format
= 8;
422 textProperty
.nitems
= 1;
424 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
425 & textProperty
, WM_NAME
);
430 wxString wxTopLevelWindowX11
::GetTitle() const
435 #ifndef MWM_DECOR_BORDER
436 /* bit definitions for MwmHints.flags */
437 #define MWM_HINTS_FUNCTIONS (1L << 0)
438 #define MWM_HINTS_DECORATIONS (1L << 1)
439 #define MWM_HINTS_INPUT_MODE (1L << 2)
440 #define MWM_HINTS_STATUS (1L << 3)
442 #define MWM_DECOR_ALL (1L << 0)
443 #define MWM_DECOR_BORDER (1L << 1)
444 #define MWM_DECOR_RESIZEH (1L << 2)
445 #define MWM_DECOR_TITLE (1L << 3)
446 #define MWM_DECOR_MENU (1L << 4)
447 #define MWM_DECOR_MINIMIZE (1L << 5)
448 #define MWM_DECOR_MAXIMIZE (1L << 6)
458 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
460 // Set the window manager decorations according to the
461 // given wxWindows style
462 bool wxSetWMDecorations(Window w
, long style
)
465 GR_WM_PROPERTIES wmProp
;
470 if (style
& wxRESIZE_BORDER
)
472 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
473 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
476 if (style
& wxSYSTEM_MENU
)
478 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
479 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
482 if ((style
& wxCAPTION
) ||
483 (style
& wxTINY_CAPTION_HORIZ
) ||
484 (style
& wxTINY_CAPTION_VERT
))
486 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
487 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
489 // The default dialog style doesn't include any kind
490 // of border, which is a bit odd. Anyway, inclusion
491 // of a caption surely implies a border.
492 style
|= wxTHICK_FRAME
;
495 if (style
& wxTHICK_FRAME
)
497 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
498 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
501 if (style
& wxSIMPLE_BORDER
)
503 wmProp
.props
|= GR_WM_PROPS_BORDER
;
504 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
507 if (style
& wxMINIMIZE_BOX
)
511 if (style
& wxMAXIMIZE_BOX
)
513 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
514 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
517 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
518 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
520 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
521 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
524 GrSetWMProperties(w
, & wmProp
);
527 if (!wxMWMIsRunning(w
))
530 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
533 hints
.decorations
= 0;
535 if (style
& wxRESIZE_BORDER
)
537 // wxLogDebug("MWM_DECOR_RESIZEH");
538 hints
.flags
|= MWM_HINTS_DECORATIONS
;
539 hints
.decorations
|= MWM_DECOR_RESIZEH
;
542 if (style
& wxSYSTEM_MENU
)
544 // wxLogDebug("MWM_DECOR_MENU");
545 hints
.flags
|= MWM_HINTS_DECORATIONS
;
546 hints
.decorations
|= MWM_DECOR_MENU
;
549 if ((style
& wxCAPTION
) ||
550 (style
& wxTINY_CAPTION_HORIZ
) ||
551 (style
& wxTINY_CAPTION_VERT
))
553 // wxLogDebug("MWM_DECOR_TITLE");
554 hints
.flags
|= MWM_HINTS_DECORATIONS
;
555 hints
.decorations
|= MWM_DECOR_TITLE
;
558 if ((style
& wxTHICK_FRAME
) || (style
& wxCAPTION
))
560 // wxLogDebug("MWM_DECOR_BORDER");
561 hints
.flags
|= MWM_HINTS_DECORATIONS
;
562 hints
.decorations
|= MWM_DECOR_BORDER
;
565 if (style
& wxMINIMIZE_BOX
)
567 // wxLogDebug("MWM_DECOR_MINIMIZE");
568 hints
.flags
|= MWM_HINTS_DECORATIONS
;
569 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
572 if (style
& wxMAXIMIZE_BOX
)
574 // wxLogDebug("MWM_DECOR_MAXIMIZE");
575 hints
.flags
|= MWM_HINTS_DECORATIONS
;
576 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
579 XChangeProperty(wxGlobalDisplay(),
581 mwm_wm_hints
, mwm_wm_hints
,
583 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
589 bool wxMWMIsRunning(Window w
)
594 Display
*dpy
= (Display
*)wxGetDisplay();
595 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
597 unsigned long length
, bytesafter
;
598 unsigned char value
[20];
599 unsigned char *ptr
= &value
[0];
603 type
= format
= length
= 0;
606 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
607 0L, 2, False
, motifWmInfo
,
608 &type
, &format
, &length
, &bytesafter
, &ptr
);
610 return (ret
== Success
);
614 // For implementation purposes - sometimes decorations make the client area
616 wxPoint wxTopLevelWindowX11
::GetClientAreaOrigin() const
618 // wxFrame::GetClientAreaOrigin
619 // does the required calculation already.
620 return wxPoint(0, 0);
623 void wxTopLevelWindowX11
::DoGetClientSize( int *width
, int *height
) const
625 XSync(wxGlobalDisplay(), False
);
626 wxWindowX11
::DoGetClientSize(width
, height
);
629 void wxTopLevelWindowX11
::DoSetClientSize(int width
, int height
)
631 wxWindowX11
::DoSetClientSize(width
, height
);
634 // Set the top-level window size
635 XSizeHints size_hints
;
636 wxSize oldSize
= GetSize();
637 wxSize oldClientSize
= GetClientSize();
639 size_hints
.flags
= PSize
;
640 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
641 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
642 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
645 // This seems to be necessary or resizes don't get performed
646 XSync(wxGlobalDisplay(), False
);
647 XSync(wxGlobalDisplay(), False
);
650 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
652 XSync(wxGlobalDisplay(), False
);
653 wxSize newSize
= GetSize();
654 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
659 void wxTopLevelWindowX11
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
662 // wxLogDebug( "Setting pos: %d, %d", x, y );
663 wxWindowX11
::DoSetSize(x
, y
, width
, height
, sizeFlags
);
665 XSync(wxGlobalDisplay(), False
);
666 Window window
= (Window
) m_mainWidget
;
670 Display
*display
= (Display
*) GetXDisplay();
671 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
672 Window parent_window
= window
,
673 next_parent
= window
;
675 // search for the parent that is child of ROOT, because the WM may
676 // reparent twice and notify only the next parent (like FVWM)
677 while (next_parent
!= root
) {
684 parent_window
= next_parent
;
685 XQueryTree(display
, parent_window
, &root
,
686 &next_parent
, &theChildren
, &n
);
687 XFree(theChildren
); // not needed
690 XWindowChanges windowChanges
;
693 windowChanges
.width
= width
;
694 windowChanges
.height
= height
;
695 windowChanges
.stack_mode
= 0;
696 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
698 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
702 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
708 windowChanges
.width
= wxMax(1, width
);
709 valueMask
|= CWWidth
;
713 windowChanges
.height
= wxMax(1, height
);
714 valueMask
|= CWHeight
;
717 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
720 XSizeHints size_hints
;
721 size_hints
.flags
= 0;
722 if (x
> -1 && y
> -1)
723 size_hints
.flags
|= PPosition
;
724 if (width
> -1 && height
> -1)
725 size_hints
.flags
|= PSize
;
726 size_hints
.width
= width
;
727 size_hints
.height
= height
;
730 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
733 // This seems to be necessary or resizes don't get performed.
734 // Take them out (or even just one of them), and the About
735 // box of the minimal sample probably won't be resized right.
736 XSync(wxGlobalDisplay(), False
);
737 XSync(wxGlobalDisplay(), False
);
741 void wxTopLevelWindowX11
::DoGetPosition(int *x
, int *y
) const
743 XSync(wxGlobalDisplay(), False
);
744 Window window
= (Window
) m_mainWidget
;
748 Display
*display
= (Display
*) GetXDisplay();
749 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
750 Window parent_window
= window
,
751 next_parent
= window
;
753 // search for the parent that is child of ROOT, because the WM may
754 // reparent twice and notify only the next parent (like FVWM)
755 while (next_parent
!= root
) {
762 parent_window
= next_parent
;
763 XQueryTree(display
, parent_window
, &root
,
764 &next_parent
, &theChildren
, &n
);
765 XFree(theChildren
); // not needed
768 int xx
, yy
; unsigned int dummy
;
769 XGetGeometry(display
, parent_window
, &root
,
770 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
774 XWindowAttributes attr
;
775 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);