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
= NorthWestGravity
;
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
| PWinGravity
;
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 size_hints
.win_gravity
= NorthWestGravity
;
208 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
211 wm_hints
.flags
= InputHint
| StateHint
;
214 wm_hints
.flags
|= WindowGroupHint
;
215 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
217 wm_hints
.input
= True
;
218 wm_hints
.initial_state
= NormalState
;
219 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
221 Atom wm_protocols
[2];
222 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
223 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
224 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
227 // You will need a compliant window manager for this to work
228 // (e.g. sawfish/enlightenment/kde/icewm/windowmaker)
229 if (style
& wxSTAY_ON_TOP
)
231 CARD32 data
= 4; // or should this be 6? According to http://developer.gnome.org/doc/standards/wm/c44.html
232 XChangeProperty (xdisplay
,
234 XInternAtom (xdisplay
, "_WIN_LAYER", False
),
238 (unsigned char *)&data
,
245 wxSetWMDecorations( xwindow
, style
);
252 wxTopLevelWindowX11::~wxTopLevelWindowX11()
254 wxTopLevelWindows
.DeleteObject(this);
256 // If this is the last top-level window, exit.
257 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
259 wxTheApp
->SetTopWindow(NULL
);
261 if (wxTheApp
->GetExitOnFrameDelete())
263 // Signal to the app that we're going to close
264 wxTheApp
->ExitMainLoop();
269 void wxTopLevelWindowX11::OnInternalIdle()
271 wxWindow::OnInternalIdle();
273 if (m_needResizeInIdle
)
275 wxSizeEvent
event( GetClientSize(), GetId() );
276 event
.SetEventObject( this );
277 GetEventHandler()->ProcessEvent( event
);
279 m_needResizeInIdle
= FALSE
;
283 // ----------------------------------------------------------------------------
284 // wxTopLevelWindowX11 showing
285 // ----------------------------------------------------------------------------
287 bool wxTopLevelWindowX11::Show(bool show
)
289 XSync( wxGlobalDisplay(), False
);
291 // Nano-X has to force a size event,
292 // else there's no initial size.
296 if (show
&& m_needResizeInIdle
)
299 wxSizeEvent
event(GetSize(), GetId());
300 event
.SetEventObject(this);
301 GetEventHandler()->ProcessEvent(event
);
303 m_needResizeInIdle
= FALSE
;
307 // This does the layout _before_ the
308 // window is shown, else the items are
309 // drawn first at the wrong positions,
310 // then at the correct positions.
318 bool ret
= wxWindowX11::Show(show
);
322 // ----------------------------------------------------------------------------
323 // wxTopLevelWindowX11 maximize/minimize
324 // ----------------------------------------------------------------------------
326 void wxTopLevelWindowX11::Maximize(bool maximize
)
331 bool wxTopLevelWindowX11::IsMaximized() const
337 void wxTopLevelWindowX11::Iconize(bool iconize
)
339 if (!m_iconized
&& GetMainWindow())
341 if (XIconifyWindow(wxGlobalDisplay(),
342 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
347 bool wxTopLevelWindowX11::IsIconized() const
352 void wxTopLevelWindowX11::Restore()
354 // This is the way to deiconify the window, according to the X FAQ
355 if (m_iconized
&& GetMainWindow())
357 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
362 // ----------------------------------------------------------------------------
363 // wxTopLevelWindowX11 fullscreen
364 // ----------------------------------------------------------------------------
366 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
373 m_fsIsShowing
= TRUE
;
385 m_fsIsShowing
= FALSE
;
392 // ----------------------------------------------------------------------------
393 // wxTopLevelWindowX11 misc
394 // ----------------------------------------------------------------------------
396 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
399 wxTopLevelWindowBase::SetIcon(icon
);
401 if (icon
.Ok() && GetMainWindow())
405 XWMHints
*wmHints
= XAllocWMHints();
406 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
408 wmHints
->flags
= IconPixmapHint
;
412 wmHints
->flags
|= IconMaskHint
;
413 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
416 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
422 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
427 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
428 (const char*) title
);
429 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
430 (const char*) title
);
432 // Use this if the platform doesn't supply the above functions.
434 XTextProperty textProperty
;
435 textProperty
.value
= (unsigned char*) title
;
436 textProperty
.encoding
= XA_STRING
;
437 textProperty
.format
= 8;
438 textProperty
.nitems
= 1;
440 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
441 & textProperty
, WM_NAME
);
446 wxString
wxTopLevelWindowX11::GetTitle() const
451 #ifndef MWM_DECOR_BORDER
453 #define MWM_HINTS_FUNCTIONS (1L << 0)
454 #define MWM_HINTS_DECORATIONS (1L << 1)
455 #define MWM_HINTS_INPUT_MODE (1L << 2)
456 #define MWM_HINTS_STATUS (1L << 3)
458 #define MWM_DECOR_ALL (1L << 0)
459 #define MWM_DECOR_BORDER (1L << 1)
460 #define MWM_DECOR_RESIZEH (1L << 2)
461 #define MWM_DECOR_TITLE (1L << 3)
462 #define MWM_DECOR_MENU (1L << 4)
463 #define MWM_DECOR_MINIMIZE (1L << 5)
464 #define MWM_DECOR_MAXIMIZE (1L << 6)
466 #define MWM_FUNC_ALL (1L << 0)
467 #define MWM_FUNC_RESIZE (1L << 1)
468 #define MWM_FUNC_MOVE (1L << 2)
469 #define MWM_FUNC_MINIMIZE (1L << 3)
470 #define MWM_FUNC_MAXIMIZE (1L << 4)
471 #define MWM_FUNC_CLOSE (1L << 5)
473 #define MWM_INPUT_MODELESS 0
474 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
475 #define MWM_INPUT_SYSTEM_MODAL 2
476 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
477 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
479 #define MWM_TEAROFF_WINDOW (1L<<0)
490 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
492 // Set the window manager decorations according to the
493 // given wxWindows style
494 bool wxSetWMDecorations(Window w
, long style
)
497 GR_WM_PROPERTIES wmProp
;
502 if (style
& wxRESIZE_BORDER
)
504 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
505 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
508 if (style
& wxSYSTEM_MENU
)
510 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
511 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
514 if ((style
& wxCAPTION
) ||
515 (style
& wxTINY_CAPTION_HORIZ
) ||
516 (style
& wxTINY_CAPTION_VERT
))
518 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
519 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
521 // The default dialog style doesn't include any kind
522 // of border, which is a bit odd. Anyway, inclusion
523 // of a caption surely implies a border.
524 style
|= wxTHICK_FRAME
;
527 if (style
& wxTHICK_FRAME
)
529 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
530 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
533 if (style
& wxSIMPLE_BORDER
)
535 wmProp
.props
|= GR_WM_PROPS_BORDER
;
536 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
539 if (style
& wxMINIMIZE_BOX
)
543 if (style
& wxMAXIMIZE_BOX
)
545 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
546 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
549 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
550 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
552 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
553 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
556 GrSetWMProperties(w
, & wmProp
);
560 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
561 if (mwm_wm_hints
== 0)
565 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
566 hints
.decorations
= 0;
569 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
575 hints
.decorations
= MWM_DECOR_BORDER
;
576 hints
.functions
= MWM_FUNC_MOVE
;
578 if ((style
& wxCAPTION
) != 0)
579 hints
.decorations
|= MWM_DECOR_TITLE
;
581 if ((style
& wxSYSTEM_MENU
) != 0)
583 hints
.functions
|= MWM_FUNC_CLOSE
;
584 hints
.decorations
|= MWM_DECOR_MENU
;
587 if ((style
& wxMINIMIZE_BOX
) != 0)
589 hints
.functions
|= MWM_FUNC_MINIMIZE
;
590 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
593 if ((style
& wxMAXIMIZE_BOX
) != 0)
595 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
596 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
599 if ((style
& wxRESIZE_BORDER
) != 0)
601 hints
.functions
|= MWM_FUNC_RESIZE
;
602 hints
.decorations
|= MWM_DECOR_RESIZEH
;
606 XChangeProperty(wxGlobalDisplay(),
608 mwm_wm_hints
, mwm_wm_hints
,
610 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
616 // For implementation purposes - sometimes decorations make the client area
618 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
620 // wxFrame::GetClientAreaOrigin
621 // does the required calculation already.
622 return wxPoint(0, 0);
625 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
627 XSync(wxGlobalDisplay(), False
);
628 wxWindowX11::DoGetClientSize(width
, height
);
631 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
633 wxWindowX11::DoSetClientSize(width
, height
);
636 // Set the top-level window size
637 XSizeHints size_hints
;
638 wxSize oldSize
= GetSize();
639 wxSize oldClientSize
= GetClientSize();
641 size_hints
.flags
= PSize
;
642 size_hints
.width
= width
+ (oldSize
.x
- oldClientSize
.x
);
643 size_hints
.height
= height
+ (oldSize
.y
- oldClientSize
.y
);
644 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
647 // This seems to be necessary or resizes don't get performed
648 XSync(wxGlobalDisplay(), False
);
649 XSync(wxGlobalDisplay(), False
);
652 wxLogDebug("DoSetClientSize: Tried to set size to %d, %d", (int) size_hints
.width
, (int) size_hints
.height
);
654 XSync(wxGlobalDisplay(), False
);
655 wxSize newSize
= GetSize();
656 wxLogDebug("New size is %d, %d", (int) newSize
.x
, (int) newSize
.y
);
661 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
663 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
666 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
668 XSync(wxGlobalDisplay(), False
);
669 Window window
= (Window
) m_mainWindow
;
673 Display
*display
= wxGlobalDisplay();
674 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
675 Window parent_window
= window
,
676 next_parent
= window
;
678 // search for the parent that is child of ROOT, because the WM may
679 // reparent twice and notify only the next parent (like FVWM)
680 while (next_parent
!= root
) {
687 parent_window
= next_parent
;
688 XQueryTree(display
, parent_window
, &root
,
689 &next_parent
, &theChildren
, &n
);
690 XFree(theChildren
); // not needed
693 XWindowChanges windowChanges
;
696 windowChanges
.width
= width
;
697 windowChanges
.height
= height
;
698 windowChanges
.stack_mode
= 0;
699 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
701 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
705 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
711 windowChanges
.width
= wxMax(1, width
);
712 valueMask
|= CWWidth
;
716 windowChanges
.height
= wxMax(1, height
);
717 valueMask
|= CWHeight
;
720 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
723 XSizeHints size_hints
;
724 size_hints
.flags
= 0;
725 if (x
> -1 && y
> -1)
726 size_hints
.flags
|= PPosition
;
727 if (width
> -1 && height
> -1)
728 size_hints
.flags
|= PSize
;
729 size_hints
.width
= width
;
730 size_hints
.height
= height
;
733 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(),
736 // This seems to be necessary or resizes don't get performed.
737 // Take them out (or even just one of them), and the About
738 // box of the minimal sample probably won't be resized right.
739 XSync(wxGlobalDisplay(), False
);
740 XSync(wxGlobalDisplay(), False
);
743 wxSizeEvent
event(wxSize(width
, height
), GetId());
744 event
.SetEventObject(this);
745 GetEventHandler()->ProcessEvent(event
);
749 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
751 XSync(wxGlobalDisplay(), False
);
752 Window window
= (Window
) m_mainWindow
;
756 Display
*display
= wxGlobalDisplay();
757 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
758 Window parent_window
= window
,
759 next_parent
= window
;
761 // search for the parent that is child of ROOT, because the WM may
762 // reparent twice and notify only the next parent (like FVWM)
763 while (next_parent
!= root
) {
770 parent_window
= next_parent
;
771 XQueryTree(display
, parent_window
, &root
,
772 &next_parent
, &theChildren
, &n
);
773 XFree(theChildren
); // not needed
776 int xx
, yy
; unsigned int dummy
;
777 XGetGeometry(display
, parent_window
, &root
,
778 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
782 XWindowAttributes attr
;
783 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);