1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/toplevel.h"
31 #include "wx/string.h"
36 #include "wx/statusbr.h"
37 #include "wx/settings.h"
40 #include "wx/x11/private.h"
41 #include "X11/Xutil.h"
43 #include "wx/unix/utilsx11.h"
45 bool wxMWMIsRunning(Window w
);
47 // ----------------------------------------------------------------------------
48 // wxTopLevelWindowX11 creation
49 // ----------------------------------------------------------------------------
51 void wxTopLevelWindowX11::Init()
54 m_maximizeOnShow
= false;
56 // unlike (almost?) all other windows, frames are created hidden
59 // Data to save/restore when calling ShowFullScreen
61 m_fsIsMaximized
= false;
62 m_fsIsShowing
= false;
64 m_needResizeInIdle
= false;
72 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
74 const wxString
& title
,
83 m_windowStyle
= style
;
88 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
91 parent
->AddChild(this);
93 wxTopLevelWindows
.Append(this);
95 Display
*xdisplay
= wxGlobalDisplay();
96 int xscreen
= DefaultScreen( xdisplay
);
97 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
98 Window xparent
= RootWindow( xdisplay
, xscreen
);
99 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
101 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
102 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
104 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
105 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
125 XSetWindowAttributes xattributes
;
127 long xattributes_mask
=
128 CWBorderPixel
| CWBackPixel
;
130 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
131 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
133 if (HasFlag( wxNO_BORDER
))
135 xattributes_mask
|= CWOverrideRedirect
;
136 xattributes
.override_redirect
= True
;
139 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE
))
141 xattributes_mask
|= CWBitGravity
;
142 xattributes
.bit_gravity
= NorthWestGravity
;
145 xattributes_mask
|= CWEventMask
;
146 xattributes
.event_mask
=
147 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
148 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
149 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
152 Window xwindow
= XCreateWindow( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
153 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
155 long backColor
, foreColor
;
156 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
157 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
159 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
160 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
163 m_mainWindow
= (WXWindow
) xwindow
;
164 m_clientWindow
= (WXWindow
) xwindow
;
165 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
168 XSelectInput( xdisplay
, xwindow
,
169 GR_EVENT_MASK_CLOSE_REQ
|
182 StructureNotifyMask
|
187 // Set background to None which will prevent X11 from clearing the
188 // background completely.
189 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
192 if (HasFlag( wxSTAY_ON_TOP
))
194 Window xroot
= RootWindow( xdisplay
, xscreen
);
195 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
199 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
201 if (GetParent() && GetParent()->GetMainWindow())
203 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
204 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
209 XSizeHints size_hints
;
210 size_hints
.flags
= PSize
| PPosition
| PWinGravity
;
213 size_hints
.width
= m_width
;
214 size_hints
.height
= m_height
;
215 size_hints
.win_gravity
= NorthWestGravity
;
216 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
219 wm_hints
.flags
= InputHint
| StateHint
;
222 wm_hints
.flags
|= WindowGroupHint
;
223 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
225 wm_hints
.input
= True
;
226 wm_hints
.initial_state
= NormalState
;
227 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
229 Atom wm_protocols
[2];
230 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
231 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
232 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
236 wxSetWMDecorations( xwindow
, style
);
243 wxTopLevelWindowX11::~wxTopLevelWindowX11()
245 wxTopLevelWindows
.DeleteObject(this);
247 // If this is the last top-level window, exit.
248 if ( wxTheApp
&& (wxTopLevelWindows
.GetCount() == 0) )
250 wxTheApp
->SetTopWindow(NULL
);
252 if (wxTheApp
->GetExitOnFrameDelete())
254 // Signal to the app that we're going to close
255 wxTheApp
->ExitMainLoop();
260 void wxTopLevelWindowX11::OnInternalIdle()
262 wxWindow::OnInternalIdle();
264 // Do this only after the last idle event so that
265 // all windows have been updated before a new
266 // round of size events is sent
267 if (m_needResizeInIdle
&& !wxTheApp
->Pending())
269 wxSizeEvent
event( GetClientSize(), GetId() );
270 event
.SetEventObject( this );
271 GetEventHandler()->ProcessEvent( event
);
273 m_needResizeInIdle
= false;
277 // ----------------------------------------------------------------------------
278 // wxTopLevelWindowX11 showing
279 // ----------------------------------------------------------------------------
281 bool wxTopLevelWindowX11::Show(bool show
)
285 wxSizeEvent
event(GetSize(), GetId());
287 event
.SetEventObject(this);
288 GetEventHandler()->ProcessEvent(event
);
290 m_needResizeInIdle
= false;
293 bool ret
= 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::DoSetIcon(const wxIcon
& icon
)
374 if (icon
.Ok() && GetMainWindow())
378 XWMHints
*wmHints
= XAllocWMHints();
379 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
381 wmHints
->flags
= IconPixmapHint
;
385 wmHints
->flags
|= IconMaskHint
;
386 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
389 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
395 void wxTopLevelWindowX11::SetIcons(const wxIconBundle
& icons
)
398 wxTopLevelWindowBase::SetIcons( icons
);
400 DoSetIcon( icons
.GetIcon( -1 ) );
401 wxSetIconsX11( wxGlobalDisplay(), GetMainWindow(), icons
);
404 bool wxTopLevelWindowX11::SetShape(const wxRegion
& region
)
406 return wxDoSetShape( wxGlobalDisplay(),
407 (Window
)GetMainWindow(),
411 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
418 // I wonder of e.g. Metacity takes UTF-8 here
419 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
420 (const char*) title
.ToAscii() );
421 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
422 (const char*) title
.ToAscii() );
424 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
425 (const char*) title
);
426 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
427 (const char*) title
);
432 wxString
wxTopLevelWindowX11::GetTitle() const
437 // For implementation purposes - sometimes decorations make the client area
439 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
441 // wxFrame::GetClientAreaOrigin
442 // does the required calculation already.
443 return wxPoint(0, 0);
446 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
454 void wxTopLevelWindowX11::DoGetSize( int *width
, int *height
) const
456 // TODO add non-client size
464 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
466 int old_width
= m_width
;
467 int old_height
= m_height
;
472 if (m_width
== old_width
&& m_height
== old_height
)
475 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
478 XSizeHints size_hints
;
479 size_hints
.flags
= PSize
;
480 size_hints
.width
= width
;
481 size_hints
.height
= height
;
482 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
485 wxWindowX11::DoSetClientSize(width
, height
);
488 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
492 int old_width
= m_width
;
493 int old_height
= m_height
;
495 if (x
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
498 if (y
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
501 if (width
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
504 if (height
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
507 if (m_x
== old_x
&& m_y
== old_y
&& m_width
== old_width
&& m_height
== old_height
)
510 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
513 XSizeHints size_hints
;
514 size_hints
.flags
= 0;
515 size_hints
.flags
|= PPosition
;
516 size_hints
.flags
|= PSize
;
519 size_hints
.width
= m_width
;
520 size_hints
.height
= m_height
;
521 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
524 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
527 Display
*display
= wxGlobalDisplay();
528 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
529 Window parent_window
= window
,
530 next_parent
= window
;
532 // search for the parent that is child of ROOT, because the WM may
533 // reparent twice and notify only the next parent (like FVWM)
534 while (next_parent
!= root
) {
541 parent_window
= next_parent
;
542 XQueryTree(display
, parent_window
, &root
,
543 &next_parent
, &theChildren
, &n
);
544 XFree(theChildren
); // not needed
547 XWindowChanges windowChanges
;
550 windowChanges
.width
= width
;
551 windowChanges
.height
= height
;
552 windowChanges
.stack_mode
= 0;
553 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
555 if (x
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
559 if (y
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
563 if (width
!= wxDefaultCoord
)
565 windowChanges
.width
= wxMax(1, width
);
566 valueMask
|= CWWidth
;
568 if (height
!= wxDefaultCoord
)
570 windowChanges
.height
= wxMax(1, height
);
571 valueMask
|= CWHeight
;
574 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
578 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
580 XSync(wxGlobalDisplay(), False
);
581 Window window
= (Window
) m_mainWindow
;
585 Display
*display
= wxGlobalDisplay();
586 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
587 Window parent_window
= window
,
588 next_parent
= window
;
590 // search for the parent that is child of ROOT, because the WM may
591 // reparent twice and notify only the next parent (like FVWM)
592 while (next_parent
!= root
) {
599 parent_window
= next_parent
;
600 XQueryTree(display
, parent_window
, &root
,
601 &next_parent
, &theChildren
, &n
);
602 XFree(theChildren
); // not needed
605 int xx
, yy
; unsigned int dummy
;
606 XGetGeometry(display
, parent_window
, &root
,
607 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
611 XWindowAttributes attr
;
612 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);
627 #ifndef MWM_DECOR_BORDER
629 #define MWM_HINTS_FUNCTIONS (1L << 0)
630 #define MWM_HINTS_DECORATIONS (1L << 1)
631 #define MWM_HINTS_INPUT_MODE (1L << 2)
632 #define MWM_HINTS_STATUS (1L << 3)
634 #define MWM_DECOR_ALL (1L << 0)
635 #define MWM_DECOR_BORDER (1L << 1)
636 #define MWM_DECOR_RESIZEH (1L << 2)
637 #define MWM_DECOR_TITLE (1L << 3)
638 #define MWM_DECOR_MENU (1L << 4)
639 #define MWM_DECOR_MINIMIZE (1L << 5)
640 #define MWM_DECOR_MAXIMIZE (1L << 6)
642 #define MWM_FUNC_ALL (1L << 0)
643 #define MWM_FUNC_RESIZE (1L << 1)
644 #define MWM_FUNC_MOVE (1L << 2)
645 #define MWM_FUNC_MINIMIZE (1L << 3)
646 #define MWM_FUNC_MAXIMIZE (1L << 4)
647 #define MWM_FUNC_CLOSE (1L << 5)
649 #define MWM_INPUT_MODELESS 0
650 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
651 #define MWM_INPUT_SYSTEM_MODAL 2
652 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
653 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
655 #define MWM_TEAROFF_WINDOW (1L<<0)
666 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
668 // Set the window manager decorations according to the
669 // given wxWidgets style
670 bool wxSetWMDecorations(Window w
, long style
)
673 GR_WM_PROPERTIES wmProp
;
678 if (style
& wxRESIZE_BORDER
)
680 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
681 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
684 if (style
& wxCLOSE_BOX
)
686 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
687 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
690 if ((style
& wxCAPTION
) ||
691 (style
& wxTINY_CAPTION_HORIZ
) ||
692 (style
& wxTINY_CAPTION_VERT
))
694 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
695 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
697 // The default dialog style doesn't include any kind
698 // of border, which is a bit odd. Anyway, inclusion
699 // of a caption surely implies a border.
700 style
|= wxRESIZE_BORDER
;
703 if (style
& wxRESIZE_BORDER
)
705 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
706 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
709 if (style
& wxSIMPLE_BORDER
)
711 wmProp
.props
|= GR_WM_PROPS_BORDER
;
712 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
715 if (style
& wxMINIMIZE_BOX
)
719 if (style
& wxMAXIMIZE_BOX
)
721 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
722 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
725 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
)
726 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
728 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
729 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
732 GrSetWMProperties(w
, & wmProp
);
736 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
737 if (mwm_wm_hints
== 0)
741 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
742 hints
.decorations
= 0;
745 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
751 hints
.decorations
= MWM_DECOR_BORDER
;
752 hints
.functions
= MWM_FUNC_MOVE
;
754 if ((style
& wxCAPTION
) != 0)
755 hints
.decorations
|= MWM_DECOR_TITLE
;
757 if ((style
& wxSYSTEM_MENU
) != 0)
758 hints
.decorations
|= MWM_DECOR_MENU
;
760 if ((style
& wxCLOSE_BOX
) != 0)
761 hints
.functions
|= MWM_FUNC_CLOSE
;
763 if ((style
& wxMINIMIZE_BOX
) != 0)
765 hints
.functions
|= MWM_FUNC_MINIMIZE
;
766 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
769 if ((style
& wxMAXIMIZE_BOX
) != 0)
771 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
772 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
775 if ((style
& wxRESIZE_BORDER
) != 0)
777 hints
.functions
|= MWM_FUNC_RESIZE
;
778 hints
.decorations
|= MWM_DECOR_RESIZEH
;
782 XChangeProperty(wxGlobalDisplay(),
784 mwm_wm_hints
, mwm_wm_hints
,
786 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);