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_BORDER
))
128 xattributes_mask
|= CWOverrideRedirect
;
129 xattributes
.override_redirect
= True
;
132 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
134 xattributes_mask
|= CWBitGravity
;
135 xattributes
.bit_gravity
= StaticGravity
;
138 xattributes_mask
|= CWEventMask
;
139 xattributes
.event_mask
=
140 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
141 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
142 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
145 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
146 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
148 long backColor
, foreColor
;
149 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
150 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
152 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
153 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
156 m_mainWindow
= (WXWindow
) xwindow
;
157 m_clientWindow
= (WXWindow
) xwindow
;
158 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
161 XSelectInput( xdisplay
, xwindow
,
162 GR_EVENT_MASK_CLOSE_REQ
|
175 StructureNotifyMask
|
180 // Set background to None which will prevent X11 from clearing the
181 // background completely.
182 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
185 if (HasFlag( wxSTAY_ON_TOP
))
187 Window xroot
= RootWindow( xdisplay
, xscreen
);
188 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
192 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
194 if (GetParent() && GetParent()->GetMainWindow())
196 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
197 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
202 size_hints
.flags
= PSize
| PPosition
;
203 size_hints
.x
= pos2
.x
;
204 size_hints
.y
= pos2
.y
;
205 size_hints
.width
= size2
.x
;
206 size_hints
.height
= size2
.y
;
207 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
210 wm_hints
.flags
= InputHint
| StateHint
;
213 wm_hints
.flags
|= WindowGroupHint
;
214 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
216 wm_hints
.input
= True
;
217 wm_hints
.initial_state
= NormalState
;
218 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
220 Atom wm_protocols
[2];
221 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
222 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
223 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
226 // You will need a compliant window manager for this to work
227 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
228 if (style
& wxSTAY_ON_TOP
)
230 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
231 XChangeProperty (xdisplay
,
233 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
237 (unsigned char *)&data
,
244 wxSetWMDecorations( xwindow
, style
);
251 wxTopLevelWindowX11::~wxTopLevelWindowX11()
253 wxTopLevelWindows
.DeleteObject(this);
255 // If this is the last top-level window, exit.
256 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
258 wxTheApp
->SetTopWindow(NULL
);
260 if (wxTheApp
->GetExitOnFrameDelete())
262 // Signal to the app that we're going to close
263 wxTheApp
->ExitMainLoop();
268 void wxTopLevelWindowX11::OnInternalIdle()
270 wxWindow::OnInternalIdle();
272 if (m_needResizeInIdle
)
274 wxSizeEvent
event( GetClientSize(), GetId() );
275 event
.SetEventObject( this );
276 GetEventHandler()->ProcessEvent( event
);
278 m_needResizeInIdle
= FALSE
;
282 // ----------------------------------------------------------------------------
283 // wxTopLevelWindowX11 showing
284 // ----------------------------------------------------------------------------
286 bool wxTopLevelWindowX11::Show(bool show
)
288 XSync( wxGlobalDisplay(), False
);
290 // Nano-X has to force a size event,
291 // else there's no initial size.
295 if (show
&& m_needResizeInIdle
)
298 wxSizeEvent
event(GetSize(), GetId());
299 event
.SetEventObject(this);
300 GetEventHandler()->ProcessEvent(event
);
302 m_needResizeInIdle
= FALSE
;
306 // This does the layout _before_ the
307 // window is shown, else the items are
308 // drawn first at the wrong positions,
309 // then at the correct positions.
317 bool ret
= wxWindowX11::Show(show
);
321 // ----------------------------------------------------------------------------
322 // wxTopLevelWindowX11 maximize/minimize
323 // ----------------------------------------------------------------------------
325 void wxTopLevelWindowX11::Maximize(bool maximize
)
330 bool wxTopLevelWindowX11::IsMaximized() const
336 void wxTopLevelWindowX11::Iconize(bool iconize
)
338 if (!m_iconized
&& GetMainWindow())
340 if (XIconifyWindow(wxGlobalDisplay(),
341 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
346 bool wxTopLevelWindowX11::IsIconized() const
351 void wxTopLevelWindowX11::Restore()
353 // This is the way to deiconify the window, according to the X FAQ
354 if (m_iconized
&& GetMainWindow())
356 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
361 // ----------------------------------------------------------------------------
362 // wxTopLevelWindowX11 fullscreen
363 // ----------------------------------------------------------------------------
365 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
372 m_fsIsShowing
= TRUE
;
384 m_fsIsShowing
= FALSE
;
391 // ----------------------------------------------------------------------------
392 // wxTopLevelWindowX11 misc
393 // ----------------------------------------------------------------------------
395 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
398 wxTopLevelWindowBase::SetIcon(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::SetTitle(const wxString
& title
)
426 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
427 (const char*) title
);
428 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
429 (const char*) title
);
431 // Use this if the platform doesn't supply the above functions.
433 XTextProperty textProperty
;
434 textProperty
.value
= (unsigned char*) title
;
435 textProperty
.encoding
= XA_STRING
;
436 textProperty
.format
= 8;
437 textProperty
.nitems
= 1;
439 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
440 & textProperty
, WM_NAME
);
445 wxString
wxTopLevelWindowX11::GetTitle() const
450 #ifndef MWM_DECOR_BORDER
452 #define MWM_HINTS_FUNCTIONS (1L << 0)
453 #define MWM_HINTS_DECORATIONS (1L << 1)
454 #define MWM_HINTS_INPUT_MODE (1L << 2)
455 #define MWM_HINTS_STATUS (1L << 3)
457 #define MWM_DECOR_ALL (1L << 0)
458 #define MWM_DECOR_BORDER (1L << 1)
459 #define MWM_DECOR_RESIZEH (1L << 2)
460 #define MWM_DECOR_TITLE (1L << 3)
461 #define MWM_DECOR_MENU (1L << 4)
462 #define MWM_DECOR_MINIMIZE (1L << 5)
463 #define MWM_DECOR_MAXIMIZE (1L << 6)
465 #define MWM_FUNC_ALL (1L << 0)
466 #define MWM_FUNC_RESIZE (1L << 1)
467 #define MWM_FUNC_MOVE (1L << 2)
468 #define MWM_FUNC_MINIMIZE (1L << 3)
469 #define MWM_FUNC_MAXIMIZE (1L << 4)
470 #define MWM_FUNC_CLOSE (1L << 5)
472 #define MWM_INPUT_MODELESS 0
473 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
474 #define MWM_INPUT_SYSTEM_MODAL 2
475 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
476 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
478 #define MWM_TEAROFF_WINDOW (1L<<0)
489 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
491 // Set the window manager decorations according to the
492 // given wxWindows style
493 bool wxSetWMDecorations(Window w
, long style
)
496 GR_WM_PROPERTIES wmProp
;
501 if (style
& wxRESIZE_BORDER
)
503 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
504 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
507 if (style
& wxSYSTEM_MENU
)
509 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
510 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
513 if ((style
& wxCAPTION
) ||
514 (style
& wxTINY_CAPTION_HORIZ
) ||
515 (style
& wxTINY_CAPTION_VERT
))
517 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
518 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
520 // The default dialog style doesn't include any kind
521 // of border, which is a bit odd. Anyway, inclusion
522 // of a caption surely implies a border.
523 style
|= wxTHICK_FRAME
;
526 if (style
& wxTHICK_FRAME
)
528 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
529 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
532 if (style
& wxSIMPLE_BORDER
)
534 wmProp
.props
|= GR_WM_PROPS_BORDER
;
535 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
538 if (style
& wxMINIMIZE_BOX
)
542 if (style
& wxMAXIMIZE_BOX
)
544 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
545 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
548 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
549 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
551 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
552 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
555 GrSetWMProperties(w
, & wmProp
);
559 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
560 if (mwm_wm_hints
== 0)
564 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
565 hints
.decorations
= 0;
568 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
574 hints
.decorations
= MWM_DECOR_BORDER
;
575 hints
.functions
= MWM_FUNC_MOVE
;
577 if ((style
& wxCAPTION
) != 0)
578 hints
.decorations
|= MWM_DECOR_TITLE
;
580 if ((style
& wxSYSTEM_MENU
) != 0)
582 hints
.functions
|= MWM_FUNC_CLOSE
;
583 hints
.decorations
|= MWM_DECOR_MENU
;
586 if ((style
& wxMINIMIZE_BOX
) != 0)
588 hints
.functions
|= MWM_FUNC_MINIMIZE
;
589 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
592 if ((style
& wxMAXIMIZE_BOX
) != 0)
594 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
595 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
598 if ((style
& wxRESIZE_BORDER
) != 0)
600 hints
.functions
|= MWM_FUNC_RESIZE
;
601 hints
.decorations
|= MWM_DECOR_RESIZEH
;
605 XChangeProperty(wxGlobalDisplay(),
607 mwm_wm_hints
, mwm_wm_hints
,
609 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
615 // For implementation purposes - sometimes decorations make the client area
617 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
619 // wxFrame::GetClientAreaOrigin
620 // does the required calculation already.
621 return wxPoint(0, 0);
624 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
626 XSync(wxGlobalDisplay(), False
);
627 wxWindowX11::DoGetClientSize(width
, height
);
630 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
632 wxWindowX11::DoSetClientSize(width
, height
);
635 // Set the top-level window size
636 XSizeHints size_hints
;
637 wxSize oldSize
= GetSize();
638 wxSize oldClientSize
= GetClientSize();
640 size_hints
.flags
= PSize
;
641 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
642 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
643 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
646 // This seems to be necessary or resizes don't get performed
647 XSync(wxGlobalDisplay(), False
);
648 XSync(wxGlobalDisplay(), False
);
651 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
653 XSync(wxGlobalDisplay(), False
);
654 wxSize newSize
= GetSize();
655 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
660 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
662 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
665 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
667 XSync(wxGlobalDisplay(), False
);
668 Window window
= (Window
) m_mainWindow
;
672 Display
*display
= wxGlobalDisplay();
673 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
674 Window parent_window
= window
,
675 next_parent
= window
;
677 // search for the parent that is child of ROOT, because the WM may
678 // reparent twice and notify only the next parent (like FVWM)
679 while (next_parent
!= root
) {
686 parent_window
= next_parent
;
687 XQueryTree(display
, parent_window
, &root
,
688 &next_parent
, &theChildren
, &n
);
689 XFree(theChildren
); // not needed
692 XWindowChanges windowChanges
;
695 windowChanges
.width
= width
;
696 windowChanges
.height
= height
;
697 windowChanges
.stack_mode
= 0;
698 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
700 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
704 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
710 windowChanges
.width
= wxMax(1, width
);
711 valueMask
|= CWWidth
;
715 windowChanges
.height
= wxMax(1, height
);
716 valueMask
|= CWHeight
;
719 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
722 XSizeHints size_hints
;
723 size_hints
.flags
= 0;
724 if (x
> -1 && y
> -1)
725 size_hints
.flags
|= PPosition
;
726 if (width
> -1 && height
> -1)
727 size_hints
.flags
|= PSize
;
728 size_hints
.width
= width
;
729 size_hints
.height
= height
;
732 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
735 // This seems to be necessary or resizes don't get performed.
736 // Take them out (or even just one of them), and the About
737 // box of the minimal sample probably won't be resized right.
738 XSync(wxGlobalDisplay(), False
);
739 XSync(wxGlobalDisplay(), False
);
742 wxSizeEvent
event(wxSize(width
, height
), GetId());
743 event
.SetEventObject(this);
744 GetEventHandler()->ProcessEvent(event
);
748 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
750 XSync(wxGlobalDisplay(), False
);
751 Window window
= (Window
) m_mainWindow
;
755 Display
*display
= wxGlobalDisplay();
756 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
757 Window parent_window
= window
,
758 next_parent
= window
;
760 // search for the parent that is child of ROOT, because the WM may
761 // reparent twice and notify only the next parent (like FVWM)
762 while (next_parent
!= root
) {
769 parent_window
= next_parent
;
770 XQueryTree(display
, parent_window
, &root
,
771 &next_parent
, &theChildren
, &n
);
772 XFree(theChildren
); // not needed
775 int xx
, yy
; unsigned int dummy
;
776 XGetGeometry(display
, parent_window
, &root
,
777 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
781 XWindowAttributes attr
;
782 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);