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
;
285 // This does the layout _before_ the
286 // window is shown, else the items are
287 // drawn first at the wrong positions,
288 // then at the correct positions.
296 bool ret
= wxWindowX11::Show(show
);
300 // ----------------------------------------------------------------------------
301 // wxTopLevelWindowX11 maximize/minimize
302 // ----------------------------------------------------------------------------
304 void wxTopLevelWindowX11::Maximize(bool maximize
)
309 bool wxTopLevelWindowX11::IsMaximized() const
315 void wxTopLevelWindowX11::Iconize(bool iconize
)
317 if (!m_iconized
&& GetMainWindow())
319 if (XIconifyWindow(wxGlobalDisplay(),
320 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
325 bool wxTopLevelWindowX11::IsIconized() const
330 void wxTopLevelWindowX11::Restore()
332 // This is the way to deiconify the window, according to the X FAQ
333 if (m_iconized
&& GetMainWindow())
335 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
340 // ----------------------------------------------------------------------------
341 // wxTopLevelWindowX11 fullscreen
342 // ----------------------------------------------------------------------------
344 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
351 m_fsIsShowing
= TRUE
;
363 m_fsIsShowing
= FALSE
;
370 // ----------------------------------------------------------------------------
371 // wxTopLevelWindowX11 misc
372 // ----------------------------------------------------------------------------
374 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
377 wxTopLevelWindowBase::SetIcon(icon
);
379 if (icon
.Ok() && GetMainWindow())
383 XWMHints
*wmHints
= XAllocWMHints();
384 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
386 wmHints
->flags
= IconPixmapHint
;
390 wmHints
->flags
|= IconMaskHint
;
391 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
394 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
400 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
405 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
406 (const char*) title
);
407 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
408 (const char*) title
);
410 // Use this if the platform doesn't supply the above functions.
412 XTextProperty textProperty
;
413 textProperty
.value
= (unsigned char*) title
;
414 textProperty
.encoding
= XA_STRING
;
415 textProperty
.format
= 8;
416 textProperty
.nitems
= 1;
418 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
419 & textProperty
, WM_NAME
);
424 wxString
wxTopLevelWindowX11::GetTitle() const
429 #ifndef MWM_DECOR_BORDER
431 #define MWM_HINTS_FUNCTIONS (1L << 0)
432 #define MWM_HINTS_DECORATIONS (1L << 1)
433 #define MWM_HINTS_INPUT_MODE (1L << 2)
434 #define MWM_HINTS_STATUS (1L << 3)
436 #define MWM_DECOR_ALL (1L << 0)
437 #define MWM_DECOR_BORDER (1L << 1)
438 #define MWM_DECOR_RESIZEH (1L << 2)
439 #define MWM_DECOR_TITLE (1L << 3)
440 #define MWM_DECOR_MENU (1L << 4)
441 #define MWM_DECOR_MINIMIZE (1L << 5)
442 #define MWM_DECOR_MAXIMIZE (1L << 6)
444 #define MWM_FUNC_ALL (1L << 0)
445 #define MWM_FUNC_RESIZE (1L << 1)
446 #define MWM_FUNC_MOVE (1L << 2)
447 #define MWM_FUNC_MINIMIZE (1L << 3)
448 #define MWM_FUNC_MAXIMIZE (1L << 4)
449 #define MWM_FUNC_CLOSE (1L << 5)
451 #define MWM_INPUT_MODELESS 0
452 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
453 #define MWM_INPUT_SYSTEM_MODAL 2
454 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
455 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
457 #define MWM_TEAROFF_WINDOW (1L<<0)
468 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
470 // Set the window manager decorations according to the
471 // given wxWindows style
472 bool wxSetWMDecorations(Window w
, long style
)
475 GR_WM_PROPERTIES wmProp
;
480 if (style
& wxRESIZE_BORDER
)
482 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
483 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
486 if (style
& wxSYSTEM_MENU
)
488 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
489 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
492 if ((style
& wxCAPTION
) ||
493 (style
& wxTINY_CAPTION_HORIZ
) ||
494 (style
& wxTINY_CAPTION_VERT
))
496 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
497 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
499 // The default dialog style doesn't include any kind
500 // of border, which is a bit odd. Anyway, inclusion
501 // of a caption surely implies a border.
502 style
|= wxTHICK_FRAME
;
505 if (style
& wxTHICK_FRAME
)
507 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
508 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
511 if (style
& wxSIMPLE_BORDER
)
513 wmProp
.props
|= GR_WM_PROPS_BORDER
;
514 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
517 if (style
& wxMINIMIZE_BOX
)
521 if (style
& wxMAXIMIZE_BOX
)
523 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
524 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
527 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
528 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
530 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
531 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
534 GrSetWMProperties(w
, & wmProp
);
538 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
539 if (mwm_wm_hints
== 0)
543 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
544 hints
.decorations
= 0;
547 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
553 hints
.decorations
= MWM_DECOR_BORDER
;
554 hints
.functions
= MWM_FUNC_MOVE
;
556 if ((style
& wxCAPTION
) != 0)
557 hints
.decorations
|= MWM_DECOR_TITLE
;
559 if ((style
& wxSYSTEM_MENU
) != 0)
561 hints
.functions
|= MWM_FUNC_CLOSE
;
562 hints
.decorations
|= MWM_DECOR_MENU
;
565 if ((style
& wxMINIMIZE_BOX
) != 0)
567 hints
.functions
|= MWM_FUNC_MINIMIZE
;
568 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
571 if ((style
& wxMAXIMIZE_BOX
) != 0)
573 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
574 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
577 if ((style
& wxRESIZE_BORDER
) != 0)
579 hints
.functions
|= MWM_FUNC_RESIZE
;
580 hints
.decorations
|= MWM_DECOR_RESIZEH
;
584 XChangeProperty(wxGlobalDisplay(),
586 mwm_wm_hints
, mwm_wm_hints
,
588 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
594 // For implementation purposes - sometimes decorations make the client area
596 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
598 // wxFrame::GetClientAreaOrigin
599 // does the required calculation already.
600 return wxPoint(0, 0);
603 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
605 XSync(wxGlobalDisplay(), False
);
606 wxWindowX11::DoGetClientSize(width
, height
);
609 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
611 wxWindowX11::DoSetClientSize(width
, height
);
614 // Set the top-level window size
615 XSizeHints size_hints
;
616 wxSize oldSize
= GetSize();
617 wxSize oldClientSize
= GetClientSize();
619 size_hints
.flags
= PSize
;
620 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
621 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
622 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
625 // This seems to be necessary or resizes don't get performed
626 XSync(wxGlobalDisplay(), False
);
627 XSync(wxGlobalDisplay(), False
);
630 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
632 XSync(wxGlobalDisplay(), False
);
633 wxSize newSize
= GetSize();
634 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
639 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
641 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
644 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
646 XSync(wxGlobalDisplay(), False
);
647 Window window
= (Window
) m_mainWidget
;
651 Display
*display
= (Display
*) GetXDisplay();
652 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
653 Window parent_window
= window
,
654 next_parent
= window
;
656 // search for the parent that is child of ROOT, because the WM may
657 // reparent twice and notify only the next parent (like FVWM)
658 while (next_parent
!= root
) {
665 parent_window
= next_parent
;
666 XQueryTree(display
, parent_window
, &root
,
667 &next_parent
, &theChildren
, &n
);
668 XFree(theChildren
); // not needed
671 XWindowChanges windowChanges
;
674 windowChanges
.width
= width
;
675 windowChanges
.height
= height
;
676 windowChanges
.stack_mode
= 0;
677 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
679 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
683 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
689 windowChanges
.width
= wxMax(1, width
);
690 valueMask
|= CWWidth
;
694 windowChanges
.height
= wxMax(1, height
);
695 valueMask
|= CWHeight
;
698 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
701 XSizeHints size_hints
;
702 size_hints
.flags
= 0;
703 if (x
> -1 && y
> -1)
704 size_hints
.flags
|= PPosition
;
705 if (width
> -1 && height
> -1)
706 size_hints
.flags
|= PSize
;
707 size_hints
.width
= width
;
708 size_hints
.height
= height
;
711 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
714 // This seems to be necessary or resizes don't get performed.
715 // Take them out (or even just one of them), and the About
716 // box of the minimal sample probably won't be resized right.
717 XSync(wxGlobalDisplay(), False
);
718 XSync(wxGlobalDisplay(), False
);
721 wxSizeEvent
event(wxSize(width
, height
), GetId());
722 event
.SetEventObject(this);
723 GetEventHandler()->ProcessEvent(event
);
727 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
729 XSync(wxGlobalDisplay(), False
);
730 Window window
= (Window
) m_mainWidget
;
734 Display
*display
= (Display
*) GetXDisplay();
735 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
736 Window parent_window
= window
,
737 next_parent
= window
;
739 // search for the parent that is child of ROOT, because the WM may
740 // reparent twice and notify only the next parent (like FVWM)
741 while (next_parent
!= root
) {
748 parent_window
= next_parent
;
749 XQueryTree(display
, parent_window
, &root
,
750 &next_parent
, &theChildren
, &n
);
751 XFree(theChildren
); // not needed
754 int xx
, yy
; unsigned int dummy
;
755 XGetGeometry(display
, parent_window
, &root
,
756 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
760 XWindowAttributes attr
;
761 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);