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 #include "wx/unix/utilsx11.h"
48 bool wxMWMIsRunning(Window w
);
50 // ----------------------------------------------------------------------------
51 // wxTopLevelWindowX11 creation
52 // ----------------------------------------------------------------------------
54 void wxTopLevelWindowX11::Init()
57 m_maximizeOnShow
= FALSE
;
59 // unlike (almost?) all other windows, frames are created hidden
62 // Data to save/restore when calling ShowFullScreen
64 m_fsIsMaximized
= FALSE
;
65 m_fsIsShowing
= FALSE
;
67 m_needResizeInIdle
= FALSE
;
70 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
72 const wxString
& title
,
81 m_windowStyle
= style
;
86 m_windowId
= id
== -1 ? NewControlId() : id
;
89 parent
->AddChild(this);
91 wxTopLevelWindows
.Append(this);
93 Display
*xdisplay
= wxGlobalDisplay();
94 int xscreen
= DefaultScreen( xdisplay
);
95 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
96 Window xparent
= RootWindow( xdisplay
, xscreen
);
97 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
99 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
100 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
102 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
103 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
119 XSetWindowAttributes xattributes
;
120 XSizeHints size_hints
;
122 long xattributes_mask
=
123 CWBorderPixel
| CWBackPixel
;
125 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
126 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
128 if (HasFlag( wxNO_BORDER
))
130 xattributes_mask
|= CWOverrideRedirect
;
131 xattributes
.override_redirect
= True
;
134 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
136 xattributes_mask
|= CWBitGravity
;
137 xattributes
.bit_gravity
= NorthWestGravity
;
140 xattributes_mask
|= CWEventMask
;
141 xattributes
.event_mask
=
142 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
143 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
144 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
147 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
148 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
150 long backColor
, foreColor
;
151 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
152 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
154 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
155 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
158 m_mainWindow
= (WXWindow
) xwindow
;
159 m_clientWindow
= (WXWindow
) xwindow
;
160 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
163 XSelectInput( xdisplay
, xwindow
,
164 GR_EVENT_MASK_CLOSE_REQ
|
177 StructureNotifyMask
|
182 // Set background to None which will prevent X11 from clearing the
183 // background completely.
184 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
187 if (HasFlag( wxSTAY_ON_TOP
))
189 Window xroot
= RootWindow( xdisplay
, xscreen
);
190 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
194 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
196 if (GetParent() && GetParent()->GetMainWindow())
198 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
199 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
204 size_hints
.flags
= PSize
| PPosition
| PWinGravity
;
205 size_hints
.x
= pos2
.x
;
206 size_hints
.y
= pos2
.y
;
207 size_hints
.width
= size2
.x
;
208 size_hints
.height
= size2
.y
;
209 size_hints
.win_gravity
= NorthWestGravity
;
210 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
213 wm_hints
.flags
= InputHint
| StateHint
;
216 wm_hints
.flags
|= WindowGroupHint
;
217 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
219 wm_hints
.input
= True
;
220 wm_hints
.initial_state
= NormalState
;
221 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
223 Atom wm_protocols
[2];
224 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
225 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
226 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
229 // You will need a compliant window manager for this to work
230 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
231 if (style
& wxSTAY_ON_TOP
)
233 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
234 XChangeProperty (xdisplay
,
236 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
240 (unsigned char *)&data
,
247 wxSetWMDecorations( xwindow
, style
);
254 wxTopLevelWindowX11::~wxTopLevelWindowX11()
256 wxTopLevelWindows
.DeleteObject(this);
258 // If this is the last top-level window, exit.
259 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
261 wxTheApp
->SetTopWindow(NULL
);
263 if (wxTheApp
->GetExitOnFrameDelete())
265 // Signal to the app that we're going to close
266 wxTheApp
->ExitMainLoop();
271 void wxTopLevelWindowX11::OnInternalIdle()
273 wxWindow::OnInternalIdle();
275 if (m_needResizeInIdle
)
277 wxSizeEvent
event( GetClientSize(), GetId() );
278 event
.SetEventObject( this );
279 GetEventHandler()->ProcessEvent( event
);
281 m_needResizeInIdle
= FALSE
;
285 // ----------------------------------------------------------------------------
286 // wxTopLevelWindowX11 showing
287 // ----------------------------------------------------------------------------
289 bool wxTopLevelWindowX11::Show(bool show
)
291 XSync( wxGlobalDisplay(), False
);
293 // Nano-X has to force a size event,
294 // else there's no initial size.
298 if (show
&& m_needResizeInIdle
)
301 wxSizeEvent
event(GetSize(), GetId());
302 event
.SetEventObject(this);
303 GetEventHandler()->ProcessEvent(event
);
305 m_needResizeInIdle
= FALSE
;
309 // This does the layout _before_ the
310 // window is shown, else the items are
311 // drawn first at the wrong positions,
312 // then at the correct positions.
320 bool ret
= wxWindowX11::Show(show
);
324 // ----------------------------------------------------------------------------
325 // wxTopLevelWindowX11 maximize/minimize
326 // ----------------------------------------------------------------------------
328 void wxTopLevelWindowX11::Maximize(bool maximize
)
333 bool wxTopLevelWindowX11::IsMaximized() const
339 void wxTopLevelWindowX11::Iconize(bool iconize
)
341 if (!m_iconized
&& GetMainWindow())
343 if (XIconifyWindow(wxGlobalDisplay(),
344 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
349 bool wxTopLevelWindowX11::IsIconized() const
354 void wxTopLevelWindowX11::Restore()
356 // This is the way to deiconify the window, according to the X FAQ
357 if (m_iconized
&& GetMainWindow())
359 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
364 // ----------------------------------------------------------------------------
365 // wxTopLevelWindowX11 fullscreen
366 // ----------------------------------------------------------------------------
368 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
375 m_fsIsShowing
= TRUE
;
387 m_fsIsShowing
= FALSE
;
394 // ----------------------------------------------------------------------------
395 // wxTopLevelWindowX11 misc
396 // ----------------------------------------------------------------------------
398 void wxTopLevelWindowX11::DoSetIcon(const wxIcon
& icon
)
400 if (icon
.Ok() && GetMainWindow())
404 XWMHints
*wmHints
= XAllocWMHints();
405 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
407 wmHints
->flags
= IconPixmapHint
;
411 wmHints
->flags
|= IconMaskHint
;
412 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
415 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
421 void wxTopLevelWindowX11::SetIcons(const wxIconBundle
& icons
)
424 wxTopLevelWindowBase::SetIcons( icons
);
426 DoSetIcon( icons
.GetIcon( -1 ) );
427 wxSetIconsX11( wxGlobalDisplay(), GetMainWindow(), icons
);
430 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
435 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
436 (const char*) title
);
437 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
438 (const char*) title
);
440 // Use this if the platform doesn't supply the above functions.
442 XTextProperty textProperty
;
443 textProperty
.value
= (unsigned char*) title
;
444 textProperty
.encoding
= XA_STRING
;
445 textProperty
.format
= 8;
446 textProperty
.nitems
= 1;
448 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
449 & textProperty
, WM_NAME
);
454 wxString
wxTopLevelWindowX11::GetTitle() const
459 #ifndef MWM_DECOR_BORDER
461 #define MWM_HINTS_FUNCTIONS (1L << 0)
462 #define MWM_HINTS_DECORATIONS (1L << 1)
463 #define MWM_HINTS_INPUT_MODE (1L << 2)
464 #define MWM_HINTS_STATUS (1L << 3)
466 #define MWM_DECOR_ALL (1L << 0)
467 #define MWM_DECOR_BORDER (1L << 1)
468 #define MWM_DECOR_RESIZEH (1L << 2)
469 #define MWM_DECOR_TITLE (1L << 3)
470 #define MWM_DECOR_MENU (1L << 4)
471 #define MWM_DECOR_MINIMIZE (1L << 5)
472 #define MWM_DECOR_MAXIMIZE (1L << 6)
474 #define MWM_FUNC_ALL (1L << 0)
475 #define MWM_FUNC_RESIZE (1L << 1)
476 #define MWM_FUNC_MOVE (1L << 2)
477 #define MWM_FUNC_MINIMIZE (1L << 3)
478 #define MWM_FUNC_MAXIMIZE (1L << 4)
479 #define MWM_FUNC_CLOSE (1L << 5)
481 #define MWM_INPUT_MODELESS 0
482 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
483 #define MWM_INPUT_SYSTEM_MODAL 2
484 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
485 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
487 #define MWM_TEAROFF_WINDOW (1L<<0)
498 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
500 // Set the window manager decorations according to the
501 // given wxWindows style
502 bool wxSetWMDecorations(Window w
, long style
)
505 GR_WM_PROPERTIES wmProp
;
510 if (style
& wxRESIZE_BORDER
)
512 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
513 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
516 if (style
& wxSYSTEM_MENU
)
518 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
519 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
522 if ((style
& wxCAPTION
) ||
523 (style
& wxTINY_CAPTION_HORIZ
) ||
524 (style
& wxTINY_CAPTION_VERT
))
526 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
527 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
529 // The default dialog style doesn't include any kind
530 // of border, which is a bit odd. Anyway, inclusion
531 // of a caption surely implies a border.
532 style
|= wxTHICK_FRAME
;
535 if (style
& wxTHICK_FRAME
)
537 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
538 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
541 if (style
& wxSIMPLE_BORDER
)
543 wmProp
.props
|= GR_WM_PROPS_BORDER
;
544 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
547 if (style
& wxMINIMIZE_BOX
)
551 if (style
& wxMAXIMIZE_BOX
)
553 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
554 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
557 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
558 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
560 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
561 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
564 GrSetWMProperties(w
, & wmProp
);
568 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
569 if (mwm_wm_hints
== 0)
573 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
574 hints
.decorations
= 0;
577 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
583 hints
.decorations
= MWM_DECOR_BORDER
;
584 hints
.functions
= MWM_FUNC_MOVE
;
586 if ((style
& wxCAPTION
) != 0)
587 hints
.decorations
|= MWM_DECOR_TITLE
;
589 if ((style
& wxSYSTEM_MENU
) != 0)
591 hints
.functions
|= MWM_FUNC_CLOSE
;
592 hints
.decorations
|= MWM_DECOR_MENU
;
595 if ((style
& wxMINIMIZE_BOX
) != 0)
597 hints
.functions
|= MWM_FUNC_MINIMIZE
;
598 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
601 if ((style
& wxMAXIMIZE_BOX
) != 0)
603 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
604 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
607 if ((style
& wxRESIZE_BORDER
) != 0)
609 hints
.functions
|= MWM_FUNC_RESIZE
;
610 hints
.decorations
|= MWM_DECOR_RESIZEH
;
614 XChangeProperty(wxGlobalDisplay(),
616 mwm_wm_hints
, mwm_wm_hints
,
618 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
624 // For implementation purposes - sometimes decorations make the client area
626 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
628 // wxFrame::GetClientAreaOrigin
629 // does the required calculation already.
630 return wxPoint(0, 0);
633 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
635 XSync(wxGlobalDisplay(), False
);
636 wxWindowX11::DoGetClientSize(width
, height
);
639 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
641 wxWindowX11::DoSetClientSize(width
, height
);
644 // Set the top-level window size
645 XSizeHints size_hints
;
646 wxSize oldSize
= GetSize();
647 wxSize oldClientSize
= GetClientSize();
649 size_hints
.flags
= PSize
;
650 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
651 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
652 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
655 // This seems to be necessary or resizes don't get performed
656 XSync(wxGlobalDisplay(), False
);
657 XSync(wxGlobalDisplay(), False
);
660 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
662 XSync(wxGlobalDisplay(), False
);
663 wxSize newSize
= GetSize();
664 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
669 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
671 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
674 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
676 XSync(wxGlobalDisplay(), False
);
677 Window window
= (Window
) m_mainWindow
;
681 Display
*display
= wxGlobalDisplay();
682 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
683 Window parent_window
= window
,
684 next_parent
= window
;
686 // search for the parent that is child of ROOT, because the WM may
687 // reparent twice and notify only the next parent (like FVWM)
688 while (next_parent
!= root
) {
695 parent_window
= next_parent
;
696 XQueryTree(display
, parent_window
, &root
,
697 &next_parent
, &theChildren
, &n
);
698 XFree(theChildren
); // not needed
701 XWindowChanges windowChanges
;
704 windowChanges
.width
= width
;
705 windowChanges
.height
= height
;
706 windowChanges
.stack_mode
= 0;
707 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
709 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
713 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
719 windowChanges
.width
= wxMax(1, width
);
720 valueMask
|= CWWidth
;
724 windowChanges
.height
= wxMax(1, height
);
725 valueMask
|= CWHeight
;
728 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
731 XSizeHints size_hints
;
732 size_hints
.flags
= 0;
733 if (x
> -1 && y
> -1)
734 size_hints
.flags
|= PPosition
;
735 if (width
> -1 && height
> -1)
736 size_hints
.flags
|= PSize
;
737 size_hints
.width
= width
;
738 size_hints
.height
= height
;
741 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
744 // This seems to be necessary or resizes don't get performed.
745 // Take them out (or even just one of them), and the About
746 // box of the minimal sample probably won't be resized right.
747 XSync(wxGlobalDisplay(), False
);
748 XSync(wxGlobalDisplay(), False
);
751 wxSizeEvent
event(wxSize(width
, height
), GetId());
752 event
.SetEventObject(this);
753 GetEventHandler()->ProcessEvent(event
);
757 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
759 XSync(wxGlobalDisplay(), False
);
760 Window window
= (Window
) m_mainWindow
;
764 Display
*display
= wxGlobalDisplay();
765 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
766 Window parent_window
= window
,
767 next_parent
= window
;
769 // search for the parent that is child of ROOT, because the WM may
770 // reparent twice and notify only the next parent (like FVWM)
771 while (next_parent
!= root
) {
778 parent_window
= next_parent
;
779 XQueryTree(display
, parent_window
, &root
,
780 &next_parent
, &theChildren
, &n
);
781 XFree(theChildren
); // not needed
784 int xx
, yy
; unsigned int dummy
;
785 XGetGeometry(display
, parent_window
, &root
,
786 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
790 XWindowAttributes attr
;
791 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);