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
=
122 CWBorderPixel
| CWBackPixel
;
124 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
125 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
127 // TODO: if we want no border, caption etc.,
128 // I think we set this to True to remove decorations
130 // Yes :-) JACS (because some WMs don't respect
132 xattributes
.override_redirect
= (style
& wxNO_BORDER
) ? True
: False
;
136 long backColor
, foreColor
;
137 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
138 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
140 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
141 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
143 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
144 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
146 m_mainWidget
= (WXWindow
) xwindow
;
150 extraFlags
|= GR_EVENT_MASK_CLOSE_REQ
;
153 XSelectInput( xdisplay
, xwindow
,
167 StructureNotifyMask
|
171 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
173 // Set background to None which will prevent X11 from clearing the
174 // background completely.
175 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
178 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
180 if (GetParent() && GetParent()->GetMainWindow())
182 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
183 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
187 size_hints
.flags
= PSize
| PPosition
;
188 size_hints
.x
= pos2
.x
;
189 size_hints
.y
= pos2
.y
;
190 size_hints
.width
= size2
.x
;
191 size_hints
.height
= size2
.y
;
192 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
195 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
196 wm_hints
.input
= True
;
197 wm_hints
.initial_state
= NormalState
;
198 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
200 Atom wm_protocols
[2];
201 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
202 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
203 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
206 wxSetWMDecorations( xwindow
, style
);
213 wxTopLevelWindowX11::~wxTopLevelWindowX11()
215 wxTopLevelWindows
.DeleteObject(this);
217 // If this is the last top-level window, exit.
218 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
220 wxTheApp
->SetTopWindow(NULL
);
222 if (wxTheApp
->GetExitOnFrameDelete())
224 // Signal to the app that we're going to close
225 wxTheApp
->ExitMainLoop();
230 void wxTopLevelWindowX11::OnInternalIdle()
232 wxWindow::OnInternalIdle();
234 if (m_needResizeInIdle
)
236 wxSizeEvent
event( GetClientSize(), GetId() );
237 event
.SetEventObject( this );
238 GetEventHandler()->ProcessEvent( event
);
240 m_needResizeInIdle
= FALSE
;
244 // ----------------------------------------------------------------------------
245 // wxTopLevelWindowX11 showing
246 // ----------------------------------------------------------------------------
248 bool wxTopLevelWindowX11::Show(bool show
)
250 // Nano-X has to force a size event,
251 // else there's no initial size.
255 if (show
&& m_needResizeInIdle
)
258 wxSizeEvent
event(GetSize(), GetId());
259 event
.SetEventObject(this);
260 GetEventHandler()->ProcessEvent(event
);
262 m_needResizeInIdle
= FALSE
;
267 // This does the layout _before_ the
268 // window is shown, else the items are
269 // drawn first at the wrong positions,
270 // then at the correct positions.
277 return wxWindowX11::Show(show
);
280 // ----------------------------------------------------------------------------
281 // wxTopLevelWindowX11 maximize/minimize
282 // ----------------------------------------------------------------------------
284 void wxTopLevelWindowX11::Maximize(bool maximize
)
289 bool wxTopLevelWindowX11::IsMaximized() const
295 void wxTopLevelWindowX11::Iconize(bool iconize
)
297 if (!m_iconized
&& GetMainWindow())
299 if (XIconifyWindow(wxGlobalDisplay(),
300 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
305 bool wxTopLevelWindowX11::IsIconized() const
310 void wxTopLevelWindowX11::Restore()
312 // This is the way to deiconify the window, according to the X FAQ
313 if (m_iconized
&& GetMainWindow())
315 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
320 // ----------------------------------------------------------------------------
321 // wxTopLevelWindowX11 fullscreen
322 // ----------------------------------------------------------------------------
324 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
331 m_fsIsShowing
= TRUE
;
343 m_fsIsShowing
= FALSE
;
350 // ----------------------------------------------------------------------------
351 // wxTopLevelWindowX11 misc
352 // ----------------------------------------------------------------------------
354 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
357 wxTopLevelWindowBase::SetIcon(icon
);
359 if (icon
.Ok() && GetMainWindow())
363 XWMHints
*wmHints
= XAllocWMHints();
364 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
366 wmHints
->flags
= IconPixmapHint
;
370 wmHints
->flags
|= IconMaskHint
;
371 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
374 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
380 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
385 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
386 (const char*) title
);
387 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
388 (const char*) title
);
390 // Use this if the platform doesn't supply the above functions.
392 XTextProperty textProperty
;
393 textProperty
.value
= (unsigned char*) title
;
394 textProperty
.encoding
= XA_STRING
;
395 textProperty
.format
= 8;
396 textProperty
.nitems
= 1;
398 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
399 & textProperty
, WM_NAME
);
404 wxString
wxTopLevelWindowX11::GetTitle() const
409 #ifndef MWM_DECOR_BORDER
410 /* bit definitions for MwmHints.flags */
411 #define MWM_HINTS_FUNCTIONS (1L << 0)
412 #define MWM_HINTS_DECORATIONS (1L << 1)
413 #define MWM_HINTS_INPUT_MODE (1L << 2)
414 #define MWM_HINTS_STATUS (1L << 3)
416 #define MWM_DECOR_ALL (1L << 0)
417 #define MWM_DECOR_BORDER (1L << 1)
418 #define MWM_DECOR_RESIZEH (1L << 2)
419 #define MWM_DECOR_TITLE (1L << 3)
420 #define MWM_DECOR_MENU (1L << 4)
421 #define MWM_DECOR_MINIMIZE (1L << 5)
422 #define MWM_DECOR_MAXIMIZE (1L << 6)
432 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
434 // Set the window manager decorations according to the
435 // given wxWindows style
436 bool wxSetWMDecorations(Window w
, long style
)
439 GR_WM_PROPERTIES wmProp
;
444 if (style
& wxRESIZE_BORDER
)
446 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
447 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
450 if (style
& wxSYSTEM_MENU
)
452 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
453 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
456 if ((style
& wxCAPTION
) ||
457 (style
& wxTINY_CAPTION_HORIZ
) ||
458 (style
& wxTINY_CAPTION_VERT
))
460 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
461 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
463 // The default dialog style doesn't include any kind
464 // of border, which is a bit odd. Anyway, inclusion
465 // of a caption surely implies a border.
466 style
|= wxTHICK_FRAME
;
469 if (style
& wxTHICK_FRAME
)
471 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
472 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
475 if (style
& wxSIMPLE_BORDER
)
477 wmProp
.props
|= GR_WM_PROPS_BORDER
;
478 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
481 if (style
& wxMINIMIZE_BOX
)
485 if (style
& wxMAXIMIZE_BOX
)
487 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
488 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
491 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
492 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
494 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
495 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
498 GrSetWMProperties(w
, & wmProp
);
501 if (!wxMWMIsRunning(w
))
504 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
507 hints
.decorations
= 0;
509 if (style
& wxRESIZE_BORDER
)
511 // wxLogDebug("MWM_DECOR_RESIZEH");
512 hints
.flags
|= MWM_HINTS_DECORATIONS
;
513 hints
.decorations
|= MWM_DECOR_RESIZEH
;
516 if (style
& wxSYSTEM_MENU
)
518 // wxLogDebug("MWM_DECOR_MENU");
519 hints
.flags
|= MWM_HINTS_DECORATIONS
;
520 hints
.decorations
|= MWM_DECOR_MENU
;
523 if ((style
& wxCAPTION
) ||
524 (style
& wxTINY_CAPTION_HORIZ
) ||
525 (style
& wxTINY_CAPTION_VERT
))
527 // wxLogDebug("MWM_DECOR_TITLE");
528 hints
.flags
|= MWM_HINTS_DECORATIONS
;
529 hints
.decorations
|= MWM_DECOR_TITLE
;
532 if ((style
& wxTHICK_FRAME
) || (style
& wxCAPTION
))
534 // wxLogDebug("MWM_DECOR_BORDER");
535 hints
.flags
|= MWM_HINTS_DECORATIONS
;
536 hints
.decorations
|= MWM_DECOR_BORDER
;
539 if (style
& wxMINIMIZE_BOX
)
541 // wxLogDebug("MWM_DECOR_MINIMIZE");
542 hints
.flags
|= MWM_HINTS_DECORATIONS
;
543 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
546 if (style
& wxMAXIMIZE_BOX
)
548 // wxLogDebug("MWM_DECOR_MAXIMIZE");
549 hints
.flags
|= MWM_HINTS_DECORATIONS
;
550 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
553 XChangeProperty(wxGlobalDisplay(),
555 mwm_wm_hints
, mwm_wm_hints
,
557 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
563 bool wxMWMIsRunning(Window w
)
568 Display
*dpy
= (Display
*)wxGetDisplay();
569 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
571 unsigned long length
, bytesafter
;
572 unsigned char value
[20];
573 unsigned char *ptr
= &value
[0];
577 type
= format
= length
= 0;
580 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
581 0L, 2, False
, motifWmInfo
,
582 &type
, &format
, &length
, &bytesafter
, &ptr
);
584 return (ret
== Success
);
588 // For implementation purposes - sometimes decorations make the client area
590 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
592 // wxFrame::GetClientAreaOrigin
593 // does the required calculation already.
594 return wxPoint(0, 0);
597 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
599 XSync(wxGlobalDisplay(), False
);
600 wxWindowX11::DoGetClientSize(width
, height
);
603 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
605 wxWindowX11::DoSetClientSize(width
, height
);
608 // Set the top-level window size
609 XSizeHints size_hints
;
610 wxSize oldSize
= GetSize();
611 wxSize oldClientSize
= GetClientSize();
613 size_hints
.flags
= PSize
;
614 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
615 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
616 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
619 // This seems to be necessary or resizes don't get performed
620 XSync(wxGlobalDisplay(), False
);
621 XSync(wxGlobalDisplay(), False
);
624 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
626 XSync(wxGlobalDisplay(), False
);
627 wxSize newSize
= GetSize();
628 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
633 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
636 // wxLogDebug( "Setting pos: %d, %d", x, y );
637 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
639 XSync(wxGlobalDisplay(), False
);
640 Window window
= (Window
) m_mainWidget
;
644 Display
*display
= (Display
*) GetXDisplay();
645 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
646 Window parent_window
= window
,
647 next_parent
= window
;
649 // search for the parent that is child of ROOT, because the WM may
650 // reparent twice and notify only the next parent (like FVWM)
651 while (next_parent
!= root
) {
658 parent_window
= next_parent
;
659 XQueryTree(display
, parent_window
, &root
,
660 &next_parent
, &theChildren
, &n
);
661 XFree(theChildren
); // not needed
664 XWindowChanges windowChanges
;
667 windowChanges
.width
= width
;
668 windowChanges
.height
= height
;
669 windowChanges
.stack_mode
= 0;
670 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
672 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
676 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
682 windowChanges
.width
= wxMax(1, width
);
683 valueMask
|= CWWidth
;
687 windowChanges
.height
= wxMax(1, height
);
688 valueMask
|= CWHeight
;
691 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
694 XSizeHints size_hints
;
695 size_hints
.flags
= 0;
696 if (x
> -1 && y
> -1)
697 size_hints
.flags
|= PPosition
;
698 if (width
> -1 && height
> -1)
699 size_hints
.flags
|= PSize
;
700 size_hints
.width
= width
;
701 size_hints
.height
= height
;
704 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
707 // This seems to be necessary or resizes don't get performed.
708 // Take them out (or even just one of them), and the About
709 // box of the minimal sample probably won't be resized right.
710 XSync(wxGlobalDisplay(), False
);
711 XSync(wxGlobalDisplay(), False
);
715 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
717 XSync(wxGlobalDisplay(), False
);
718 Window window
= (Window
) m_mainWidget
;
722 Display
*display
= (Display
*) GetXDisplay();
723 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
724 Window parent_window
= window
,
725 next_parent
= window
;
727 // search for the parent that is child of ROOT, because the WM may
728 // reparent twice and notify only the next parent (like FVWM)
729 while (next_parent
!= root
) {
736 parent_window
= next_parent
;
737 XQueryTree(display
, parent_window
, &root
,
738 &next_parent
, &theChildren
, &n
);
739 XFree(theChildren
); // not needed
742 int xx
, yy
; unsigned int dummy
;
743 XGetGeometry(display
, parent_window
, &root
,
744 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
748 XWindowAttributes attr
;
749 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);