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 // You will need a compliant window manager for this to work
207 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
208 if (style
& wxSTAY_ON_TOP
)
210 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
211 XChangeProperty (xdisplay
,
213 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
217 (unsigned char *)&data
,
224 wxSetWMDecorations( xwindow
, style
);
231 wxTopLevelWindowX11::~wxTopLevelWindowX11()
233 wxTopLevelWindows
.DeleteObject(this);
235 // If this is the last top-level window, exit.
236 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
238 wxTheApp
->SetTopWindow(NULL
);
240 if (wxTheApp
->GetExitOnFrameDelete())
242 // Signal to the app that we're going to close
243 wxTheApp
->ExitMainLoop();
248 void wxTopLevelWindowX11::OnInternalIdle()
250 wxWindow::OnInternalIdle();
252 if (m_needResizeInIdle
)
254 wxSizeEvent
event( GetClientSize(), GetId() );
255 event
.SetEventObject( this );
256 GetEventHandler()->ProcessEvent( event
);
258 m_needResizeInIdle
= FALSE
;
262 // ----------------------------------------------------------------------------
263 // wxTopLevelWindowX11 showing
264 // ----------------------------------------------------------------------------
266 bool wxTopLevelWindowX11::Show(bool show
)
268 // Nano-X has to force a size event,
269 // else there's no initial size.
273 if (show
&& m_needResizeInIdle
)
276 wxSizeEvent
event(GetSize(), GetId());
277 event
.SetEventObject(this);
278 GetEventHandler()->ProcessEvent(event
);
280 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.
295 return wxWindowX11::Show(show
);
298 // ----------------------------------------------------------------------------
299 // wxTopLevelWindowX11 maximize/minimize
300 // ----------------------------------------------------------------------------
302 void wxTopLevelWindowX11::Maximize(bool maximize
)
307 bool wxTopLevelWindowX11::IsMaximized() const
313 void wxTopLevelWindowX11::Iconize(bool iconize
)
315 if (!m_iconized
&& GetMainWindow())
317 if (XIconifyWindow(wxGlobalDisplay(),
318 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
323 bool wxTopLevelWindowX11::IsIconized() const
328 void wxTopLevelWindowX11::Restore()
330 // This is the way to deiconify the window, according to the X FAQ
331 if (m_iconized
&& GetMainWindow())
333 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
338 // ----------------------------------------------------------------------------
339 // wxTopLevelWindowX11 fullscreen
340 // ----------------------------------------------------------------------------
342 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
349 m_fsIsShowing
= TRUE
;
361 m_fsIsShowing
= FALSE
;
368 // ----------------------------------------------------------------------------
369 // wxTopLevelWindowX11 misc
370 // ----------------------------------------------------------------------------
372 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
375 wxTopLevelWindowBase::SetIcon(icon
);
377 if (icon
.Ok() && GetMainWindow())
381 XWMHints
*wmHints
= XAllocWMHints();
382 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
384 wmHints
->flags
= IconPixmapHint
;
388 wmHints
->flags
|= IconMaskHint
;
389 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
392 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
398 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
403 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
404 (const char*) title
);
405 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
406 (const char*) title
);
408 // Use this if the platform doesn't supply the above functions.
410 XTextProperty textProperty
;
411 textProperty
.value
= (unsigned char*) title
;
412 textProperty
.encoding
= XA_STRING
;
413 textProperty
.format
= 8;
414 textProperty
.nitems
= 1;
416 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
417 & textProperty
, WM_NAME
);
422 wxString
wxTopLevelWindowX11::GetTitle() const
427 #ifndef MWM_DECOR_BORDER
428 /* bit definitions for MwmHints.flags */
429 #define MWM_HINTS_FUNCTIONS (1L << 0)
430 #define MWM_HINTS_DECORATIONS (1L << 1)
431 #define MWM_HINTS_INPUT_MODE (1L << 2)
432 #define MWM_HINTS_STATUS (1L << 3)
434 #define MWM_DECOR_ALL (1L << 0)
435 #define MWM_DECOR_BORDER (1L << 1)
436 #define MWM_DECOR_RESIZEH (1L << 2)
437 #define MWM_DECOR_TITLE (1L << 3)
438 #define MWM_DECOR_MENU (1L << 4)
439 #define MWM_DECOR_MINIMIZE (1L << 5)
440 #define MWM_DECOR_MAXIMIZE (1L << 6)
450 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
452 // Set the window manager decorations according to the
453 // given wxWindows style
454 bool wxSetWMDecorations(Window w
, long style
)
457 GR_WM_PROPERTIES wmProp
;
462 if (style
& wxRESIZE_BORDER
)
464 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
465 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
468 if (style
& wxSYSTEM_MENU
)
470 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
471 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
474 if ((style
& wxCAPTION
) ||
475 (style
& wxTINY_CAPTION_HORIZ
) ||
476 (style
& wxTINY_CAPTION_VERT
))
478 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
479 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
481 // The default dialog style doesn't include any kind
482 // of border, which is a bit odd. Anyway, inclusion
483 // of a caption surely implies a border.
484 style
|= wxTHICK_FRAME
;
487 if (style
& wxTHICK_FRAME
)
489 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
490 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
493 if (style
& wxSIMPLE_BORDER
)
495 wmProp
.props
|= GR_WM_PROPS_BORDER
;
496 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
499 if (style
& wxMINIMIZE_BOX
)
503 if (style
& wxMAXIMIZE_BOX
)
505 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
506 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
509 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
510 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
512 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
513 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
516 GrSetWMProperties(w
, & wmProp
);
519 if (!wxMWMIsRunning(w
))
522 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
525 hints
.decorations
= 0;
527 if (style
& wxRESIZE_BORDER
)
529 // wxLogDebug("MWM_DECOR_RESIZEH");
530 hints
.flags
|= MWM_HINTS_DECORATIONS
;
531 hints
.decorations
|= MWM_DECOR_RESIZEH
;
534 if (style
& wxSYSTEM_MENU
)
536 // wxLogDebug("MWM_DECOR_MENU");
537 hints
.flags
|= MWM_HINTS_DECORATIONS
;
538 hints
.decorations
|= MWM_DECOR_MENU
;
541 if ((style
& wxCAPTION
) ||
542 (style
& wxTINY_CAPTION_HORIZ
) ||
543 (style
& wxTINY_CAPTION_VERT
))
545 // wxLogDebug("MWM_DECOR_TITLE");
546 hints
.flags
|= MWM_HINTS_DECORATIONS
;
547 hints
.decorations
|= MWM_DECOR_TITLE
;
550 if ((style
& wxTHICK_FRAME
) || (style
& wxCAPTION
))
552 // wxLogDebug("MWM_DECOR_BORDER");
553 hints
.flags
|= MWM_HINTS_DECORATIONS
;
554 hints
.decorations
|= MWM_DECOR_BORDER
;
557 if (style
& wxMINIMIZE_BOX
)
559 // wxLogDebug("MWM_DECOR_MINIMIZE");
560 hints
.flags
|= MWM_HINTS_DECORATIONS
;
561 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
564 if (style
& wxMAXIMIZE_BOX
)
566 // wxLogDebug("MWM_DECOR_MAXIMIZE");
567 hints
.flags
|= MWM_HINTS_DECORATIONS
;
568 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
571 XChangeProperty(wxGlobalDisplay(),
573 mwm_wm_hints
, mwm_wm_hints
,
575 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
581 bool wxMWMIsRunning(Window w
)
586 Display
*dpy
= (Display
*)wxGetDisplay();
587 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
589 unsigned long length
, bytesafter
;
590 unsigned char value
[20];
591 unsigned char *ptr
= &value
[0];
595 type
= format
= length
= 0;
598 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
599 0L, 2, False
, motifWmInfo
,
600 &type
, &format
, &length
, &bytesafter
, &ptr
);
602 return (ret
== Success
);
606 // For implementation purposes - sometimes decorations make the client area
608 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
610 // wxFrame::GetClientAreaOrigin
611 // does the required calculation already.
612 return wxPoint(0, 0);
615 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
617 XSync(wxGlobalDisplay(), False
);
618 wxWindowX11::DoGetClientSize(width
, height
);
621 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
623 wxWindowX11::DoSetClientSize(width
, height
);
626 // Set the top-level window size
627 XSizeHints size_hints
;
628 wxSize oldSize
= GetSize();
629 wxSize oldClientSize
= GetClientSize();
631 size_hints
.flags
= PSize
;
632 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
633 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
634 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
637 // This seems to be necessary or resizes don't get performed
638 XSync(wxGlobalDisplay(), False
);
639 XSync(wxGlobalDisplay(), False
);
642 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
644 XSync(wxGlobalDisplay(), False
);
645 wxSize newSize
= GetSize();
646 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
651 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
654 // wxLogDebug( "Setting pos: %d, %d", x, y );
655 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
657 XSync(wxGlobalDisplay(), False
);
658 Window window
= (Window
) m_mainWidget
;
662 Display
*display
= (Display
*) GetXDisplay();
663 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
664 Window parent_window
= window
,
665 next_parent
= window
;
667 // search for the parent that is child of ROOT, because the WM may
668 // reparent twice and notify only the next parent (like FVWM)
669 while (next_parent
!= root
) {
676 parent_window
= next_parent
;
677 XQueryTree(display
, parent_window
, &root
,
678 &next_parent
, &theChildren
, &n
);
679 XFree(theChildren
); // not needed
682 XWindowChanges windowChanges
;
685 windowChanges
.width
= width
;
686 windowChanges
.height
= height
;
687 windowChanges
.stack_mode
= 0;
688 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
690 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
694 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
700 windowChanges
.width
= wxMax(1, width
);
701 valueMask
|= CWWidth
;
705 windowChanges
.height
= wxMax(1, height
);
706 valueMask
|= CWHeight
;
709 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
712 XSizeHints size_hints
;
713 size_hints
.flags
= 0;
714 if (x
> -1 && y
> -1)
715 size_hints
.flags
|= PPosition
;
716 if (width
> -1 && height
> -1)
717 size_hints
.flags
|= PSize
;
718 size_hints
.width
= width
;
719 size_hints
.height
= height
;
722 XSetWMNormalHints( (Display
*) GetXDisplay(), (Window
) GetMainWindow(),
725 // This seems to be necessary or resizes don't get performed.
726 // Take them out (or even just one of them), and the About
727 // box of the minimal sample probably won't be resized right.
728 XSync(wxGlobalDisplay(), False
);
729 XSync(wxGlobalDisplay(), False
);
733 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
735 XSync(wxGlobalDisplay(), False
);
736 Window window
= (Window
) m_mainWidget
;
740 Display
*display
= (Display
*) GetXDisplay();
741 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
742 Window parent_window
= window
,
743 next_parent
= window
;
745 // search for the parent that is child of ROOT, because the WM may
746 // reparent twice and notify only the next parent (like FVWM)
747 while (next_parent
!= root
) {
754 parent_window
= next_parent
;
755 XQueryTree(display
, parent_window
, &root
,
756 &next_parent
, &theChildren
, &n
);
757 XFree(theChildren
); // not needed
760 int xx
, yy
; unsigned int dummy
;
761 XGetGeometry(display
, parent_window
, &root
,
762 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
766 XWindowAttributes attr
;
767 Status status
= XGetWindowAttributes((Display
*) GetXDisplay(), parent_window
, & attr
);