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 xattributes
.override_redirect
= False
;
134 long backColor
, foreColor
;
135 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
136 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
138 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
139 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
141 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
142 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
144 m_mainWidget
= (WXWindow
) xwindow
;
148 extraFlags
|= GR_EVENT_MASK_CLOSE_REQ
;
151 XSelectInput( xdisplay
, xwindow
,
165 StructureNotifyMask
|
169 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
171 // Set background to None which will prevent X11 from clearing the
172 // background completely.
173 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
176 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
178 if (GetParent() && GetParent()->GetMainWindow())
180 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
181 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
185 size_hints
.flags
= PSize
| PPosition
;
186 size_hints
.x
= pos2
.x
;
187 size_hints
.y
= pos2
.y
;
188 size_hints
.width
= size2
.x
;
189 size_hints
.height
= size2
.y
;
190 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
193 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
194 wm_hints
.input
= True
;
195 wm_hints
.initial_state
= NormalState
;
196 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
198 Atom wm_protocols
[2];
199 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
200 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
201 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
204 wxSetWMDecorations( xwindow
, style
);
211 wxTopLevelWindowX11::~wxTopLevelWindowX11()
213 wxTopLevelWindows
.DeleteObject(this);
215 // If this is the last top-level window, exit.
216 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
218 wxTheApp
->SetTopWindow(NULL
);
220 if (wxTheApp
->GetExitOnFrameDelete())
222 // Signal to the app that we're going to close
223 wxTheApp
->ExitMainLoop();
228 void wxTopLevelWindowX11::OnInternalIdle()
230 wxWindow::OnInternalIdle();
232 if (m_needResizeInIdle
)
234 wxSizeEvent
event( GetClientSize(), GetId() );
235 event
.SetEventObject( this );
236 GetEventHandler()->ProcessEvent( event
);
238 m_needResizeInIdle
= FALSE
;
242 // ----------------------------------------------------------------------------
243 // wxTopLevelWindowX11 showing
244 // ----------------------------------------------------------------------------
246 bool wxTopLevelWindowX11::Show(bool show
)
248 // Nano-X has to force a size event,
249 // else there's no initial size.
253 if (show
&& m_needResizeInIdle
)
256 wxSizeEvent
event(GetSize(), GetId());
257 event
.SetEventObject(this);
258 GetEventHandler()->ProcessEvent(event
);
260 m_needResizeInIdle
= FALSE
;
265 // This does the layout _before_ the
266 // window is shown, else the items are
267 // drawn first at the wrong positions,
268 // then at the correct positions.
275 return wxWindowX11::Show(show
);
278 // ----------------------------------------------------------------------------
279 // wxTopLevelWindowX11 maximize/minimize
280 // ----------------------------------------------------------------------------
282 void wxTopLevelWindowX11::Maximize(bool maximize
)
287 bool wxTopLevelWindowX11::IsMaximized() const
293 void wxTopLevelWindowX11::Iconize(bool iconize
)
295 if (!m_iconized
&& GetMainWindow())
297 if (XIconifyWindow(wxGlobalDisplay(),
298 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
303 bool wxTopLevelWindowX11::IsIconized() const
308 void wxTopLevelWindowX11::Restore()
310 // This is the way to deiconify the window, according to the X FAQ
311 if (m_iconized
&& GetMainWindow())
313 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
318 // ----------------------------------------------------------------------------
319 // wxTopLevelWindowX11 fullscreen
320 // ----------------------------------------------------------------------------
322 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
329 m_fsIsShowing
= TRUE
;
341 m_fsIsShowing
= FALSE
;
348 // ----------------------------------------------------------------------------
349 // wxTopLevelWindowX11 misc
350 // ----------------------------------------------------------------------------
352 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
355 wxTopLevelWindowBase::SetIcon(icon
);
357 if (icon
.Ok() && GetMainWindow())
361 XWMHints
*wmHints
= XAllocWMHints();
362 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
364 wmHints
->flags
= IconPixmapHint
;
368 wmHints
->flags
|= IconMaskHint
;
369 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
372 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
378 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
383 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
384 (const char*) title
);
385 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
386 (const char*) title
);
388 // Use this if the platform doesn't supply the above functions.
390 XTextProperty textProperty
;
391 textProperty
.value
= (unsigned char*) title
;
392 textProperty
.encoding
= XA_STRING
;
393 textProperty
.format
= 8;
394 textProperty
.nitems
= 1;
396 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
397 & textProperty
, WM_NAME
);
402 wxString
wxTopLevelWindowX11::GetTitle() const
407 #ifndef MWM_DECOR_BORDER
408 /* bit definitions for MwmHints.flags */
409 #define MWM_HINTS_FUNCTIONS (1L << 0)
410 #define MWM_HINTS_DECORATIONS (1L << 1)
411 #define MWM_HINTS_INPUT_MODE (1L << 2)
412 #define MWM_HINTS_STATUS (1L << 3)
414 #define MWM_DECOR_ALL (1L << 0)
415 #define MWM_DECOR_BORDER (1L << 1)
416 #define MWM_DECOR_RESIZEH (1L << 2)
417 #define MWM_DECOR_TITLE (1L << 3)
418 #define MWM_DECOR_MENU (1L << 4)
419 #define MWM_DECOR_MINIMIZE (1L << 5)
420 #define MWM_DECOR_MAXIMIZE (1L << 6)
430 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
432 // Set the window manager decorations according to the
433 // given wxWindows style
434 bool wxSetWMDecorations(Window w
, long style
)
437 GR_WM_PROPERTIES wmProp
;
442 if (style
& wxRESIZE_BORDER
)
444 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
445 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
448 if (style
& wxSYSTEM_MENU
)
450 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
451 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
454 if ((style
& wxCAPTION
) ||
455 (style
& wxTINY_CAPTION_HORIZ
) ||
456 (style
& wxTINY_CAPTION_VERT
))
458 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
459 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
461 // The default dialog style doesn't include any kind
462 // of border, which is a bit odd. Anyway, inclusion
463 // of a caption surely implies a border.
464 style
|= wxTHICK_FRAME
;
467 if (style
& wxTHICK_FRAME
)
469 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
470 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
473 if (style
& wxSIMPLE_BORDER
)
475 wmProp
.props
|= GR_WM_PROPS_BORDER
;
476 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
479 if (style
& wxMINIMIZE_BOX
)
483 if (style
& wxMAXIMIZE_BOX
)
485 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
486 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
489 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
490 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
492 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
493 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
496 GrSetWMProperties(w
, & wmProp
);
499 if (!wxMWMIsRunning(w
))
502 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
505 hints
.decorations
= 0;
507 if (style
& wxRESIZE_BORDER
)
509 // wxLogDebug("MWM_DECOR_RESIZEH");
510 hints
.flags
|= MWM_HINTS_DECORATIONS
;
511 hints
.decorations
|= MWM_DECOR_RESIZEH
;
514 if (style
& wxSYSTEM_MENU
)
516 // wxLogDebug("MWM_DECOR_MENU");
517 hints
.flags
|= MWM_HINTS_DECORATIONS
;
518 hints
.decorations
|= MWM_DECOR_MENU
;
521 if ((style
& wxCAPTION
) ||
522 (style
& wxTINY_CAPTION_HORIZ
) ||
523 (style
& wxTINY_CAPTION_VERT
))
525 // wxLogDebug("MWM_DECOR_TITLE");
526 hints
.flags
|= MWM_HINTS_DECORATIONS
;
527 hints
.decorations
|= MWM_DECOR_TITLE
;
530 if ((style
& wxTHICK_FRAME
) || (style
& wxCAPTION
))
532 // wxLogDebug("MWM_DECOR_BORDER");
533 hints
.flags
|= MWM_HINTS_DECORATIONS
;
534 hints
.decorations
|= MWM_DECOR_BORDER
;
537 if (style
& wxMINIMIZE_BOX
)
539 // wxLogDebug("MWM_DECOR_MINIMIZE");
540 hints
.flags
|= MWM_HINTS_DECORATIONS
;
541 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
544 if (style
& wxMAXIMIZE_BOX
)
546 // wxLogDebug("MWM_DECOR_MAXIMIZE");
547 hints
.flags
|= MWM_HINTS_DECORATIONS
;
548 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
551 XChangeProperty(wxGlobalDisplay(),
553 mwm_wm_hints
, mwm_wm_hints
,
555 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
561 bool wxMWMIsRunning(Window w
)
566 Display
*dpy
= (Display
*)wxGetDisplay();
567 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
569 unsigned long length
, bytesafter
;
570 unsigned char value
[20];
571 unsigned char *ptr
= &value
[0];
575 type
= format
= length
= 0;
578 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
579 0L, 2, False
, motifWmInfo
,
580 &type
, &format
, &length
, &bytesafter
, &ptr
);
582 return (ret
== Success
);
586 // For implementation purposes - sometimes decorations make the client area
588 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
590 // wxFrame::GetClientAreaOrigin
591 // does the required calculation already.
592 return wxPoint(0, 0);
595 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
597 XSync(wxGlobalDisplay(), False
);
598 wxWindowX11::DoGetClientSize(width
, height
);
601 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
603 wxWindowX11::DoSetClientSize(width
, height
);
606 // Set the top-level window size
607 XSizeHints size_hints
;
608 wxSize oldSize
= GetSize();
609 wxSize oldClientSize
= GetClientSize();
611 size_hints
.flags
= PSize
;
612 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
613 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
614 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
617 // This seems to be necessary or resizes don't get performed
618 XSync(wxGlobalDisplay(), False
);
619 XSync(wxGlobalDisplay(), False
);
622 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
624 XSync(wxGlobalDisplay(), False
);
625 wxSize newSize
= GetSize();
626 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
631 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
634 // wxLogDebug( "Setting pos: %d, %d", x, y );
635 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
637 XSync(wxGlobalDisplay(), False
);
638 Window window
= (Window
) m_mainWidget
;
642 Display
*display
= (Display
*) GetXDisplay();
643 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
644 Window parent_window
= window
,
645 next_parent
= window
;
647 // search for the parent that is child of ROOT, because the WM may
648 // reparent twice and notify only the next parent (like FVWM)
649 while (next_parent
!= root
) {
656 parent_window
= next_parent
;
657 XQueryTree(display
, parent_window
, &root
,
658 &next_parent
, &theChildren
, &n
);
659 XFree(theChildren
); // not needed
662 XWindowChanges windowChanges
;
665 windowChanges
.width
= width
;
666 windowChanges
.height
= height
;
667 windowChanges
.stack_mode
= 0;
668 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
670 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
674 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
680 windowChanges
.width
= wxMax(1, width
);
681 valueMask
|= CWWidth
;
685 windowChanges
.height
= wxMax(1, height
);
686 valueMask
|= CWHeight
;
689 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
692 XSizeHints size_hints
;
693 size_hints
.flags
= 0;
694 if (x
> -1 && y
> -1)
695 size_hints
.flags
|= PPosition
;
696 if (width
> -1 && height
> -1)
697 size_hints
.flags
|= PSize
;
698 size_hints
.width
= width
;
699 size_hints
.height
= height
;
702 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
705 // This seems to be necessary or resizes don't get performed.
706 // Take them out (or even just one of them), and the About
707 // box of the minimal sample probably won't be resized right.
708 XSync(wxGlobalDisplay(), False
);
709 XSync(wxGlobalDisplay(), False
);
713 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
715 XSync(wxGlobalDisplay(), False
);
716 Window window
= (Window
) m_mainWidget
;
720 Display
*display
= (Display
*) GetXDisplay();
721 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
722 Window parent_window
= window
,
723 next_parent
= window
;
725 // search for the parent that is child of ROOT, because the WM may
726 // reparent twice and notify only the next parent (like FVWM)
727 while (next_parent
!= root
) {
734 parent_window
= next_parent
;
735 XQueryTree(display
, parent_window
, &root
,
736 &next_parent
, &theChildren
, &n
);
737 XFree(theChildren
); // not needed
740 int xx
, yy
; unsigned int dummy
;
741 XGetGeometry(display
, parent_window
, &root
,
742 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
746 XWindowAttributes attr
;
747 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);