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 xattributes_mask
|= CWEventMask
;
133 xattributes
.event_mask
=
134 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
135 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
136 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
139 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
140 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
142 long backColor
, foreColor
;
143 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
144 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
146 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
147 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
150 m_mainWidget
= (WXWindow
) xwindow
;
153 XSelectInput( xdisplay
, xwindow
,
154 GR_EVENT_MASK_CLOSE_REQ
|
167 StructureNotifyMask
|
172 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
174 // Set background to None which will prevent X11 from clearing the
175 // background completely.
176 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
179 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
181 if (GetParent() && GetParent()->GetMainWindow())
183 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
184 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
188 size_hints
.flags
= PSize
| PPosition
;
189 size_hints
.x
= pos2
.x
;
190 size_hints
.y
= pos2
.y
;
191 size_hints
.width
= size2
.x
;
192 size_hints
.height
= size2
.y
;
193 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
196 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
197 wm_hints
.input
= True
;
198 wm_hints
.initial_state
= NormalState
;
199 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
201 Atom wm_protocols
[2];
202 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
203 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
204 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
207 // You will need a compliant window manager for this to work
208 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
209 if (style
& wxSTAY_ON_TOP
)
211 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
212 XChangeProperty (xdisplay
,
214 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
218 (unsigned char *)&data
,
225 wxSetWMDecorations( xwindow
, style
);
232 wxTopLevelWindowX11::~wxTopLevelWindowX11()
234 wxTopLevelWindows
.DeleteObject(this);
236 // If this is the last top-level window, exit.
237 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
239 wxTheApp
->SetTopWindow(NULL
);
241 if (wxTheApp
->GetExitOnFrameDelete())
243 // Signal to the app that we're going to close
244 wxTheApp
->ExitMainLoop();
249 void wxTopLevelWindowX11::OnInternalIdle()
251 wxWindow::OnInternalIdle();
253 if (m_needResizeInIdle
)
255 wxSizeEvent
event( GetClientSize(), GetId() );
256 event
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent( event
);
259 m_needResizeInIdle
= FALSE
;
263 // ----------------------------------------------------------------------------
264 // wxTopLevelWindowX11 showing
265 // ----------------------------------------------------------------------------
267 bool wxTopLevelWindowX11::Show(bool show
)
269 // Nano-X has to force a size event,
270 // else there's no initial size.
274 if (show
&& m_needResizeInIdle
)
277 wxSizeEvent
event(GetSize(), GetId());
278 event
.SetEventObject(this);
279 GetEventHandler()->ProcessEvent(event
);
281 m_needResizeInIdle
= FALSE
;
286 // This does the layout _before_ the
287 // window is shown, else the items are
288 // drawn first at the wrong positions,
289 // then at the correct positions.
296 return wxWindowX11::Show(show
);
299 // ----------------------------------------------------------------------------
300 // wxTopLevelWindowX11 maximize/minimize
301 // ----------------------------------------------------------------------------
303 void wxTopLevelWindowX11::Maximize(bool maximize
)
308 bool wxTopLevelWindowX11::IsMaximized() const
314 void wxTopLevelWindowX11::Iconize(bool iconize
)
316 if (!m_iconized
&& GetMainWindow())
318 if (XIconifyWindow(wxGlobalDisplay(),
319 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
324 bool wxTopLevelWindowX11::IsIconized() const
329 void wxTopLevelWindowX11::Restore()
331 // This is the way to deiconify the window, according to the X FAQ
332 if (m_iconized
&& GetMainWindow())
334 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
339 // ----------------------------------------------------------------------------
340 // wxTopLevelWindowX11 fullscreen
341 // ----------------------------------------------------------------------------
343 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
350 m_fsIsShowing
= TRUE
;
362 m_fsIsShowing
= FALSE
;
369 // ----------------------------------------------------------------------------
370 // wxTopLevelWindowX11 misc
371 // ----------------------------------------------------------------------------
373 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
376 wxTopLevelWindowBase::SetIcon(icon
);
378 if (icon
.Ok() && GetMainWindow())
382 XWMHints
*wmHints
= XAllocWMHints();
383 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
385 wmHints
->flags
= IconPixmapHint
;
389 wmHints
->flags
|= IconMaskHint
;
390 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
393 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
399 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
404 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
405 (const char*) title
);
406 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
407 (const char*) title
);
409 // Use this if the platform doesn't supply the above functions.
411 XTextProperty textProperty
;
412 textProperty
.value
= (unsigned char*) title
;
413 textProperty
.encoding
= XA_STRING
;
414 textProperty
.format
= 8;
415 textProperty
.nitems
= 1;
417 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
418 & textProperty
, WM_NAME
);
423 wxString
wxTopLevelWindowX11::GetTitle() const
428 #ifndef MWM_DECOR_BORDER
430 #define MWM_HINTS_FUNCTIONS (1L << 0)
431 #define MWM_HINTS_DECORATIONS (1L << 1)
432 #define MWM_HINTS_INPUT_MODE (1L << 2)
433 #define MWM_HINTS_STATUS (1L << 3)
435 #define MWM_DECOR_ALL (1L << 0)
436 #define MWM_DECOR_BORDER (1L << 1)
437 #define MWM_DECOR_RESIZEH (1L << 2)
438 #define MWM_DECOR_TITLE (1L << 3)
439 #define MWM_DECOR_MENU (1L << 4)
440 #define MWM_DECOR_MINIMIZE (1L << 5)
441 #define MWM_DECOR_MAXIMIZE (1L << 6)
443 #define MWM_FUNC_ALL (1L << 0)
444 #define MWM_FUNC_RESIZE (1L << 1)
445 #define MWM_FUNC_MOVE (1L << 2)
446 #define MWM_FUNC_MINIMIZE (1L << 3)
447 #define MWM_FUNC_MAXIMIZE (1L << 4)
448 #define MWM_FUNC_CLOSE (1L << 5)
450 #define MWM_INPUT_MODELESS 0
451 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
452 #define MWM_INPUT_SYSTEM_MODAL 2
453 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
454 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
456 #define MWM_TEAROFF_WINDOW (1L<<0)
467 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
469 // Set the window manager decorations according to the
470 // given wxWindows style
471 bool wxSetWMDecorations(Window w
, long style
)
474 GR_WM_PROPERTIES wmProp
;
479 if (style
& wxRESIZE_BORDER
)
481 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
482 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
485 if (style
& wxSYSTEM_MENU
)
487 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
488 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
491 if ((style
& wxCAPTION
) ||
492 (style
& wxTINY_CAPTION_HORIZ
) ||
493 (style
& wxTINY_CAPTION_VERT
))
495 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
496 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
498 // The default dialog style doesn't include any kind
499 // of border, which is a bit odd. Anyway, inclusion
500 // of a caption surely implies a border.
501 style
|= wxTHICK_FRAME
;
504 if (style
& wxTHICK_FRAME
)
506 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
507 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
510 if (style
& wxSIMPLE_BORDER
)
512 wmProp
.props
|= GR_WM_PROPS_BORDER
;
513 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
516 if (style
& wxMINIMIZE_BOX
)
520 if (style
& wxMAXIMIZE_BOX
)
522 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
523 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
526 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
527 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
529 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
530 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
533 GrSetWMProperties(w
, & wmProp
);
537 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
538 if (mwm_wm_hints
== 0)
542 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
543 hints
.decorations
= 0;
546 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
552 hints
.decorations
= MWM_DECOR_BORDER
;
553 hints
.functions
= MWM_FUNC_MOVE
;
555 if ((style
& wxCAPTION
) != 0)
556 hints
.decorations
|= MWM_DECOR_TITLE
;
558 if ((style
& wxSYSTEM_MENU
) != 0)
560 hints
.functions
|= MWM_FUNC_CLOSE
;
561 hints
.decorations
|= MWM_DECOR_MENU
;
564 if ((style
& wxMINIMIZE_BOX
) != 0)
566 hints
.functions
|= MWM_FUNC_MINIMIZE
;
567 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
570 if ((style
& wxMAXIMIZE_BOX
) != 0)
572 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
573 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
576 if ((style
& wxRESIZE_BORDER
) != 0)
578 hints
.functions
|= MWM_FUNC_RESIZE
;
579 hints
.decorations
|= MWM_DECOR_RESIZEH
;
583 XChangeProperty(wxGlobalDisplay(),
585 mwm_wm_hints
, mwm_wm_hints
,
587 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
593 // For implementation purposes - sometimes decorations make the client area
595 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
597 // wxFrame::GetClientAreaOrigin
598 // does the required calculation already.
599 return wxPoint(0, 0);
602 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
604 XSync(wxGlobalDisplay(), False
);
605 wxWindowX11::DoGetClientSize(width
, height
);
608 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
610 wxWindowX11::DoSetClientSize(width
, height
);
613 // Set the top-level window size
614 XSizeHints size_hints
;
615 wxSize oldSize
= GetSize();
616 wxSize oldClientSize
= GetClientSize();
618 size_hints
.flags
= PSize
;
619 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
620 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
621 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
624 // This seems to be necessary or resizes don't get performed
625 XSync(wxGlobalDisplay(), False
);
626 XSync(wxGlobalDisplay(), False
);
629 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
631 XSync(wxGlobalDisplay(), False
);
632 wxSize newSize
= GetSize();
633 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
638 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
641 // wxLogDebug( "Setting pos: %d, %d", x, y );
642 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
644 XSync(wxGlobalDisplay(), False
);
645 Window window
= (Window
) m_mainWidget
;
649 Display
*display
= (Display
*) GetXDisplay();
650 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
651 Window parent_window
= window
,
652 next_parent
= window
;
654 // search for the parent that is child of ROOT, because the WM may
655 // reparent twice and notify only the next parent (like FVWM)
656 while (next_parent
!= root
) {
663 parent_window
= next_parent
;
664 XQueryTree(display
, parent_window
, &root
,
665 &next_parent
, &theChildren
, &n
);
666 XFree(theChildren
); // not needed
669 XWindowChanges windowChanges
;
672 windowChanges
.width
= width
;
673 windowChanges
.height
= height
;
674 windowChanges
.stack_mode
= 0;
675 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
677 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
681 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
687 windowChanges
.width
= wxMax(1, width
);
688 valueMask
|= CWWidth
;
692 windowChanges
.height
= wxMax(1, height
);
693 valueMask
|= CWHeight
;
696 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
699 XSizeHints size_hints
;
700 size_hints
.flags
= 0;
701 if (x
> -1 && y
> -1)
702 size_hints
.flags
|= PPosition
;
703 if (width
> -1 && height
> -1)
704 size_hints
.flags
|= PSize
;
705 size_hints
.width
= width
;
706 size_hints
.height
= height
;
709 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
712 // This seems to be necessary or resizes don't get performed.
713 // Take them out (or even just one of them), and the About
714 // box of the minimal sample probably won't be resized right.
715 XSync(wxGlobalDisplay(), False
);
716 XSync(wxGlobalDisplay(), False
);
720 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
722 XSync(wxGlobalDisplay(), False
);
723 Window window
= (Window
) m_mainWidget
;
727 Display
*display
= (Display
*) GetXDisplay();
728 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
729 Window parent_window
= window
,
730 next_parent
= window
;
732 // search for the parent that is child of ROOT, because the WM may
733 // reparent twice and notify only the next parent (like FVWM)
734 while (next_parent
!= root
) {
741 parent_window
= next_parent
;
742 XQueryTree(display
, parent_window
, &root
,
743 &next_parent
, &theChildren
, &n
);
744 XFree(theChildren
); // not needed
747 int xx
, yy
; unsigned int dummy
;
748 XGetGeometry(display
, parent_window
, &root
,
749 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
753 XWindowAttributes attr
;
754 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);