1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
7 // Copyright: (c) 2002 Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/toplevel.h"
30 #include "wx/string.h"
35 #include "wx/statusbr.h"
36 #include "wx/settings.h"
39 #include "wx/x11/private.h"
40 #include "X11/Xutil.h"
42 #include "wx/unix/utilsx11.h"
44 bool wxMWMIsRunning(Window w
);
46 // ----------------------------------------------------------------------------
47 // wxTopLevelWindowX11 creation
48 // ----------------------------------------------------------------------------
50 void wxTopLevelWindowX11::Init()
53 m_maximizeOnShow
= false;
55 // unlike (almost?) all other windows, frames are created hidden
58 // Data to save/restore when calling ShowFullScreen
60 m_fsIsMaximized
= false;
61 m_fsIsShowing
= false;
63 m_needResizeInIdle
= false;
71 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
73 const wxString
& title
,
82 m_windowStyle
= style
;
87 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
90 parent
->AddChild(this);
92 wxTopLevelWindows
.Append(this);
94 Display
*xdisplay
= wxGlobalDisplay();
95 int xscreen
= DefaultScreen( xdisplay
);
96 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
97 Window xparent
= RootWindow( xdisplay
, xscreen
);
98 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
100 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
101 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
103 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
104 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
124 XSetWindowAttributes xattributes
;
126 long xattributes_mask
=
127 CWBorderPixel
| CWBackPixel
;
129 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
130 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
132 if (HasFlag( wxNO_BORDER
))
134 xattributes_mask
|= CWOverrideRedirect
;
135 xattributes
.override_redirect
= True
;
138 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE
))
140 xattributes_mask
|= CWBitGravity
;
141 xattributes
.bit_gravity
= NorthWestGravity
;
144 xattributes_mask
|= CWEventMask
;
145 xattributes
.event_mask
=
146 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
147 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
148 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
151 Window xwindow
= XCreateWindow( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
152 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
154 long backColor
, foreColor
;
155 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
156 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
158 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
159 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
162 m_mainWindow
= (WXWindow
) xwindow
;
163 m_clientWindow
= (WXWindow
) xwindow
;
164 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
167 XSelectInput( xdisplay
, xwindow
,
168 GR_EVENT_MASK_CLOSE_REQ
|
181 StructureNotifyMask
|
186 // Set background to None which will prevent X11 from clearing the
187 // background completely.
188 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
191 if (HasFlag( wxSTAY_ON_TOP
))
193 Window xroot
= RootWindow( xdisplay
, xscreen
);
194 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
198 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
200 if (GetParent() && GetParent()->X11GetMainWindow())
202 Window xparentwindow
= (Window
) GetParent()->X11GetMainWindow();
203 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
208 XSizeHints size_hints
;
209 size_hints
.flags
= PSize
| PPosition
| PWinGravity
;
212 size_hints
.width
= m_width
;
213 size_hints
.height
= m_height
;
214 size_hints
.win_gravity
= NorthWestGravity
;
215 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
218 wm_hints
.flags
= InputHint
| StateHint
;
221 wm_hints
.flags
|= WindowGroupHint
;
222 wm_hints
.window_group
= (Window
) GetParent()->X11GetMainWindow();
224 wm_hints
.input
= True
;
225 wm_hints
.initial_state
= NormalState
;
226 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
228 Atom wm_protocols
[2];
229 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
230 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
231 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
235 wxSetWMDecorations( xwindow
, style
);
242 wxTopLevelWindowX11::~wxTopLevelWindowX11()
244 wxTopLevelWindows
.DeleteObject(this);
246 // If this is the last top-level window, exit.
247 if ( wxTheApp
&& (wxTopLevelWindows
.GetCount() == 0) )
249 wxTheApp
->SetTopWindow(NULL
);
251 if (wxTheApp
->GetExitOnFrameDelete())
253 // Signal to the app that we're going to close
254 wxTheApp
->ExitMainLoop();
259 void wxTopLevelWindowX11::OnInternalIdle()
261 wxWindow::OnInternalIdle();
263 // Do this only after the last idle event so that
264 // all windows have been updated before a new
265 // round of size events is sent
266 if (m_needResizeInIdle
&& !wxTheApp
->Pending())
268 wxSizeEvent
event( GetClientSize(), GetId() );
269 event
.SetEventObject( this );
270 HandleWindowEvent( event
);
272 m_needResizeInIdle
= false;
276 // ----------------------------------------------------------------------------
277 // wxTopLevelWindowX11 showing
278 // ----------------------------------------------------------------------------
280 bool wxTopLevelWindowX11::Show(bool show
)
284 wxSizeEvent
event(GetSize(), GetId());
286 event
.SetEventObject(this);
287 HandleWindowEvent(event
);
289 m_needResizeInIdle
= false;
292 bool ret
= wxWindowX11::Show(show
);
297 // ----------------------------------------------------------------------------
298 // wxTopLevelWindowX11 maximize/minimize
299 // ----------------------------------------------------------------------------
301 void wxTopLevelWindowX11::Maximize(bool WXUNUSED(maximize
))
306 bool wxTopLevelWindowX11::IsMaximized() const
312 void wxTopLevelWindowX11::Iconize(bool iconize
)
320 if (!m_iconized
&& X11GetMainWindow())
322 if (XIconifyWindow(wxGlobalDisplay(),
323 (Window
) X11GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
328 bool wxTopLevelWindowX11::IsIconized() const
333 void wxTopLevelWindowX11::Restore()
335 // This is the way to deiconify the window, according to the X FAQ
336 if (m_iconized
&& X11GetMainWindow())
338 XMapWindow(wxGlobalDisplay(), (Window
) X11GetMainWindow());
343 // ----------------------------------------------------------------------------
344 // wxTopLevelWindowX11 fullscreen
345 // ----------------------------------------------------------------------------
347 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
354 m_fsIsShowing
= true;
366 m_fsIsShowing
= false;
373 // ----------------------------------------------------------------------------
374 // wxTopLevelWindowX11 misc
375 // ----------------------------------------------------------------------------
377 void wxTopLevelWindowX11::DoSetIcon(const wxIcon
& icon
)
379 if (icon
.IsOk() && X11GetMainWindow())
382 XWMHints
*wmHints
= XAllocWMHints();
383 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
385 wmHints
->flags
= IconPixmapHint
;
389 wmHints
->flags
|= IconMaskHint
;
390 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
393 XSetWMHints(wxGlobalDisplay(), (Window
) X11GetMainWindow(), wmHints
);
399 void wxTopLevelWindowX11::SetIcons(const wxIconBundle
& icons
)
402 wxTopLevelWindowBase::SetIcons( icons
);
404 DoSetIcon( icons
.GetIcon( -1 ) );
405 wxSetIconsX11( wxGlobalDisplay(), X11GetMainWindow(), icons
);
408 bool wxTopLevelWindowX11::SetShape(const wxRegion
& region
)
410 return wxDoSetShape( wxGlobalDisplay(),
411 (Window
)X11GetMainWindow(),
415 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
419 if (X11GetMainWindow())
422 // I wonder of e.g. Metacity takes UTF-8 here
423 XStoreName(wxGlobalDisplay(), (Window
) X11GetMainWindow(),
424 (const char*) title
.ToAscii() );
425 XSetIconName(wxGlobalDisplay(), (Window
) X11GetMainWindow(),
426 (const char*) title
.ToAscii() );
428 XStoreName(wxGlobalDisplay(), (Window
) X11GetMainWindow(),
429 (const char*) title
);
430 XSetIconName(wxGlobalDisplay(), (Window
) X11GetMainWindow(),
431 (const char*) title
);
436 wxString
wxTopLevelWindowX11::GetTitle() const
441 // For implementation purposes - sometimes decorations make the client area
443 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
445 // wxFrame::GetClientAreaOrigin
446 // does the required calculation already.
447 return wxPoint(0, 0);
450 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
458 void wxTopLevelWindowX11::DoGetSize( int *width
, int *height
) const
460 // TODO add non-client size
468 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
470 int old_width
= m_width
;
471 int old_height
= m_height
;
476 if (m_width
== old_width
&& m_height
== old_height
)
479 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
482 XSizeHints size_hints
;
483 size_hints
.flags
= PSize
;
484 size_hints
.width
= width
;
485 size_hints
.height
= height
;
486 XSetWMNormalHints( wxGlobalDisplay(), (Window
) X11GetMainWindow(), &size_hints
);
489 wxWindowX11::DoSetClientSize(width
, height
);
492 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
496 int old_width
= m_width
;
497 int old_height
= m_height
;
499 if (x
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
502 if (y
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
505 if (width
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
508 if (height
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
511 if (m_x
== old_x
&& m_y
== old_y
&& m_width
== old_width
&& m_height
== old_height
)
514 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
517 XSizeHints size_hints
;
518 size_hints
.flags
= 0;
519 size_hints
.flags
|= PPosition
;
520 size_hints
.flags
|= PSize
;
523 size_hints
.width
= m_width
;
524 size_hints
.height
= m_height
;
525 XSetWMNormalHints( wxGlobalDisplay(), (Window
) X11GetMainWindow(), &size_hints
);
528 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
531 Display
*display
= wxGlobalDisplay();
532 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
533 Window parent_window
= window
,
534 next_parent
= window
;
536 // search for the parent that is child of ROOT, because the WM may
537 // reparent twice and notify only the next parent (like FVWM)
538 while (next_parent
!= root
) {
545 parent_window
= next_parent
;
546 XQueryTree(display
, parent_window
, &root
,
547 &next_parent
, &theChildren
, &n
);
548 XFree(theChildren
); // not needed
551 XWindowChanges windowChanges
;
554 windowChanges
.width
= width
;
555 windowChanges
.height
= height
;
556 windowChanges
.stack_mode
= 0;
557 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
559 if (x
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
563 if (y
!= wxDefaultCoord
|| (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
567 if (width
!= wxDefaultCoord
)
569 windowChanges
.width
= wxMax(1, width
);
570 valueMask
|= CWWidth
;
572 if (height
!= wxDefaultCoord
)
574 windowChanges
.height
= wxMax(1, height
);
575 valueMask
|= CWHeight
;
578 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
582 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
584 XSync(wxGlobalDisplay(), False
);
585 Window window
= (Window
) m_mainWindow
;
589 Display
*display
= wxGlobalDisplay();
590 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
591 Window parent_window
= window
,
592 next_parent
= window
;
594 // search for the parent that is child of ROOT, because the WM may
595 // reparent twice and notify only the next parent (like FVWM)
596 while (next_parent
!= root
) {
603 parent_window
= next_parent
;
604 XQueryTree(display
, parent_window
, &root
,
605 &next_parent
, &theChildren
, &n
);
606 XFree(theChildren
); // not needed
609 int xx
, yy
; unsigned int dummy
;
610 XGetGeometry(display
, parent_window
, &root
,
611 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
615 XWindowAttributes attr
;
616 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);
631 #ifndef MWM_DECOR_BORDER
633 #define MWM_HINTS_FUNCTIONS (1L << 0)
634 #define MWM_HINTS_DECORATIONS (1L << 1)
635 #define MWM_HINTS_INPUT_MODE (1L << 2)
636 #define MWM_HINTS_STATUS (1L << 3)
638 #define MWM_DECOR_ALL (1L << 0)
639 #define MWM_DECOR_BORDER (1L << 1)
640 #define MWM_DECOR_RESIZEH (1L << 2)
641 #define MWM_DECOR_TITLE (1L << 3)
642 #define MWM_DECOR_MENU (1L << 4)
643 #define MWM_DECOR_MINIMIZE (1L << 5)
644 #define MWM_DECOR_MAXIMIZE (1L << 6)
646 #define MWM_FUNC_ALL (1L << 0)
647 #define MWM_FUNC_RESIZE (1L << 1)
648 #define MWM_FUNC_MOVE (1L << 2)
649 #define MWM_FUNC_MINIMIZE (1L << 3)
650 #define MWM_FUNC_MAXIMIZE (1L << 4)
651 #define MWM_FUNC_CLOSE (1L << 5)
653 #define MWM_INPUT_MODELESS 0
654 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
655 #define MWM_INPUT_SYSTEM_MODAL 2
656 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
657 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
659 #define MWM_TEAROFF_WINDOW (1L<<0)
670 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
672 // Set the window manager decorations according to the
673 // given wxWidgets style
674 bool wxSetWMDecorations(Window w
, long style
)
677 GR_WM_PROPERTIES wmProp
;
682 if (style
& wxRESIZE_BORDER
)
684 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
685 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
688 if (style
& wxCLOSE_BOX
)
690 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
691 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
694 if ((style
& wxCAPTION
) ||
695 (style
& wxTINY_CAPTION
))
697 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
698 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
700 // The default dialog style doesn't include any kind
701 // of border, which is a bit odd. Anyway, inclusion
702 // of a caption surely implies a border.
703 style
|= wxRESIZE_BORDER
;
706 if (style
& wxRESIZE_BORDER
)
708 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
709 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
712 if (style
& wxSIMPLE_BORDER
)
714 wmProp
.props
|= GR_WM_PROPS_BORDER
;
715 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
718 if (style
& wxMINIMIZE_BOX
)
722 if (style
& wxMAXIMIZE_BOX
)
724 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
725 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
728 if ( !(style
& wxBORDER
) && !(style
& wxRESIZE_BORDER
) )
730 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
731 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
734 GrSetWMProperties(w
, & wmProp
);
738 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
739 if (mwm_wm_hints
== 0)
743 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
744 hints
.decorations
= 0;
747 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
753 hints
.decorations
= MWM_DECOR_BORDER
;
754 hints
.functions
= MWM_FUNC_MOVE
;
756 if ((style
& wxCAPTION
) != 0)
757 hints
.decorations
|= MWM_DECOR_TITLE
;
759 if ((style
& wxSYSTEM_MENU
) != 0)
760 hints
.decorations
|= MWM_DECOR_MENU
;
762 if ((style
& wxCLOSE_BOX
) != 0)
763 hints
.functions
|= MWM_FUNC_CLOSE
;
765 if ((style
& wxMINIMIZE_BOX
) != 0)
767 hints
.functions
|= MWM_FUNC_MINIMIZE
;
768 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
771 if ((style
& wxMAXIMIZE_BOX
) != 0)
773 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
774 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
777 if ((style
& wxRESIZE_BORDER
) != 0)
779 hints
.functions
|= MWM_FUNC_RESIZE
;
780 hints
.decorations
|= MWM_DECOR_RESIZEH
;
784 XChangeProperty(wxGlobalDisplay(),
786 mwm_wm_hints
, mwm_wm_hints
,
788 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);