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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/unix/utilsx11.h"
48 bool wxMWMIsRunning(Window w
);
50 // ----------------------------------------------------------------------------
51 // wxTopLevelWindowX11 creation
52 // ----------------------------------------------------------------------------
54 void wxTopLevelWindowX11::Init()
57 m_maximizeOnShow
= FALSE
;
59 // unlike (almost?) all other windows, frames are created hidden
62 // Data to save/restore when calling ShowFullScreen
64 m_fsIsMaximized
= FALSE
;
65 m_fsIsShowing
= FALSE
;
67 m_needResizeInIdle
= FALSE
;
75 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
77 const wxString
& title
,
86 m_windowStyle
= style
;
91 m_windowId
= id
== -1 ? NewControlId() : id
;
94 parent
->AddChild(this);
96 wxTopLevelWindows
.Append(this);
98 Display
*xdisplay
= wxGlobalDisplay();
99 int xscreen
= DefaultScreen( xdisplay
);
100 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
101 Window xparent
= RootWindow( xdisplay
, xscreen
);
102 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
104 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
105 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
107 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
108 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
128 XSetWindowAttributes xattributes
;
130 long xattributes_mask
=
131 CWBorderPixel
| CWBackPixel
;
133 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
134 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
136 if (HasFlag( wxNO_BORDER
))
138 xattributes_mask
|= CWOverrideRedirect
;
139 xattributes
.override_redirect
= True
;
142 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE
))
144 xattributes_mask
|= CWBitGravity
;
145 xattributes
.bit_gravity
= NorthWestGravity
;
148 xattributes_mask
|= CWEventMask
;
149 xattributes
.event_mask
=
150 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
151 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
152 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
155 Window xwindow
= XCreateWindow( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
156 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
158 long backColor
, foreColor
;
159 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
160 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
162 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, m_x
, m_y
, m_width
, m_height
,
163 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
166 m_mainWindow
= (WXWindow
) xwindow
;
167 m_clientWindow
= (WXWindow
) xwindow
;
168 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
171 XSelectInput( xdisplay
, xwindow
,
172 GR_EVENT_MASK_CLOSE_REQ
|
185 StructureNotifyMask
|
190 // Set background to None which will prevent X11 from clearing the
191 // background completely.
192 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
195 if (HasFlag( wxSTAY_ON_TOP
))
197 Window xroot
= RootWindow( xdisplay
, xscreen
);
198 XSetTransientForHint( xdisplay
, xwindow
, xroot
);
202 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
204 if (GetParent() && GetParent()->GetMainWindow())
206 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
207 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
212 XSizeHints size_hints
;
213 size_hints
.flags
= PSize
| PPosition
| PWinGravity
;
216 size_hints
.width
= m_width
;
217 size_hints
.height
= m_height
;
218 size_hints
.win_gravity
= NorthWestGravity
;
219 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
222 wm_hints
.flags
= InputHint
| StateHint
;
225 wm_hints
.flags
|= WindowGroupHint
;
226 wm_hints
.window_group
= (Window
) GetParent()->GetMainWindow();
228 wm_hints
.input
= True
;
229 wm_hints
.initial_state
= NormalState
;
230 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
232 Atom wm_protocols
[2];
233 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
234 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
235 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
239 wxSetWMDecorations( xwindow
, style
);
246 wxTopLevelWindowX11::~wxTopLevelWindowX11()
248 wxTopLevelWindows
.DeleteObject(this);
250 // If this is the last top-level window, exit.
251 if ( wxTheApp
&& (wxTopLevelWindows
.GetCount() == 0) )
253 wxTheApp
->SetTopWindow(NULL
);
255 if (wxTheApp
->GetExitOnFrameDelete())
257 // Signal to the app that we're going to close
258 wxTheApp
->ExitMainLoop();
263 void wxTopLevelWindowX11::OnInternalIdle()
265 wxWindow::OnInternalIdle();
267 // Do this only after the last idle event so that
268 // all windows have been updated before a new
269 // round of size events is sent
270 if (m_needResizeInIdle
&& !wxTheApp
->Pending())
272 wxSizeEvent
event( GetClientSize(), GetId() );
273 event
.SetEventObject( this );
274 GetEventHandler()->ProcessEvent( event
);
276 m_needResizeInIdle
= FALSE
;
280 // ----------------------------------------------------------------------------
281 // wxTopLevelWindowX11 showing
282 // ----------------------------------------------------------------------------
284 bool wxTopLevelWindowX11::Show(bool show
)
288 wxSizeEvent
event(GetSize(), GetId());
290 event
.SetEventObject(this);
291 GetEventHandler()->ProcessEvent(event
);
293 m_needResizeInIdle
= FALSE
;
296 bool ret
= wxWindowX11::Show(show
);
301 // ----------------------------------------------------------------------------
302 // wxTopLevelWindowX11 maximize/minimize
303 // ----------------------------------------------------------------------------
305 void wxTopLevelWindowX11::Maximize(bool maximize
)
310 bool wxTopLevelWindowX11::IsMaximized() const
316 void wxTopLevelWindowX11::Iconize(bool iconize
)
318 if (!m_iconized
&& GetMainWindow())
320 if (XIconifyWindow(wxGlobalDisplay(),
321 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
326 bool wxTopLevelWindowX11::IsIconized() const
331 void wxTopLevelWindowX11::Restore()
333 // This is the way to deiconify the window, according to the X FAQ
334 if (m_iconized
&& GetMainWindow())
336 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
341 // ----------------------------------------------------------------------------
342 // wxTopLevelWindowX11 fullscreen
343 // ----------------------------------------------------------------------------
345 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
352 m_fsIsShowing
= TRUE
;
364 m_fsIsShowing
= FALSE
;
371 // ----------------------------------------------------------------------------
372 // wxTopLevelWindowX11 misc
373 // ----------------------------------------------------------------------------
375 void wxTopLevelWindowX11::DoSetIcon(const wxIcon
& 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::SetIcons(const wxIconBundle
& icons
)
401 wxTopLevelWindowBase::SetIcons( icons
);
403 DoSetIcon( icons
.GetIcon( -1 ) );
404 wxSetIconsX11( wxGlobalDisplay(), GetMainWindow(), icons
);
407 bool wxTopLevelWindowX11::SetShape(const wxRegion
& region
)
409 return wxDoSetShape( wxGlobalDisplay(),
410 (Window
)GetMainWindow(),
414 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
421 // I wonder of e.g. Metacity takes UTF-8 here
422 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
423 (const char*) title
.ToAscii() );
424 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
425 (const char*) title
.ToAscii() );
427 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
428 (const char*) title
);
429 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
430 (const char*) title
);
435 wxString
wxTopLevelWindowX11::GetTitle() const
440 // For implementation purposes - sometimes decorations make the client area
442 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
444 // wxFrame::GetClientAreaOrigin
445 // does the required calculation already.
446 return wxPoint(0, 0);
449 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
457 void wxTopLevelWindowX11::DoGetSize( int *width
, int *height
) const
459 // TODO add non-client size
467 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
469 int old_width
= m_width
;
470 int old_height
= m_height
;
475 if (m_width
== old_width
&& m_height
== old_height
)
478 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
481 XSizeHints size_hints
;
482 size_hints
.flags
= PSize
;
483 size_hints
.width
= width
;
484 size_hints
.height
= height
;
485 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
488 wxWindowX11::DoSetClientSize(width
, height
);
491 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
495 int old_width
= m_width
;
496 int old_height
= m_height
;
498 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
501 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
504 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
507 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
510 if (m_x
== old_x
&& m_y
== old_y
&& m_width
== old_width
&& m_height
== old_height
)
513 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
516 XSizeHints size_hints
;
517 size_hints
.flags
= 0;
518 size_hints
.flags
|= PPosition
;
519 size_hints
.flags
|= PSize
;
522 size_hints
.width
= m_width
;
523 size_hints
.height
= m_height
;
524 XSetWMNormalHints( wxGlobalDisplay(), (Window
) GetMainWindow(), &size_hints
);
527 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
530 Display
*display
= wxGlobalDisplay();
531 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
532 Window parent_window
= window
,
533 next_parent
= window
;
535 // search for the parent that is child of ROOT, because the WM may
536 // reparent twice and notify only the next parent (like FVWM)
537 while (next_parent
!= root
) {
544 parent_window
= next_parent
;
545 XQueryTree(display
, parent_window
, &root
,
546 &next_parent
, &theChildren
, &n
);
547 XFree(theChildren
); // not needed
550 XWindowChanges windowChanges
;
553 windowChanges
.width
= width
;
554 windowChanges
.height
= height
;
555 windowChanges
.stack_mode
= 0;
556 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
558 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
562 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
568 windowChanges
.width
= wxMax(1, width
);
569 valueMask
|= CWWidth
;
573 windowChanges
.height
= wxMax(1, height
);
574 valueMask
|= CWHeight
;
577 XConfigureWindow( display
, parent_window
, valueMask
, &windowChanges
);
581 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
583 XSync(wxGlobalDisplay(), False
);
584 Window window
= (Window
) m_mainWindow
;
588 Display
*display
= wxGlobalDisplay();
589 Window root
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
590 Window parent_window
= window
,
591 next_parent
= window
;
593 // search for the parent that is child of ROOT, because the WM may
594 // reparent twice and notify only the next parent (like FVWM)
595 while (next_parent
!= root
) {
602 parent_window
= next_parent
;
603 XQueryTree(display
, parent_window
, &root
,
604 &next_parent
, &theChildren
, &n
);
605 XFree(theChildren
); // not needed
608 int xx
, yy
; unsigned int dummy
;
609 XGetGeometry(display
, parent_window
, &root
,
610 &xx
, &yy
, &dummy
, &dummy
, &dummy
, &dummy
);
614 XWindowAttributes attr
;
615 Status status
= XGetWindowAttributes( wxGlobalDisplay(), parent_window
, & attr
);
630 #ifndef MWM_DECOR_BORDER
632 #define MWM_HINTS_FUNCTIONS (1L << 0)
633 #define MWM_HINTS_DECORATIONS (1L << 1)
634 #define MWM_HINTS_INPUT_MODE (1L << 2)
635 #define MWM_HINTS_STATUS (1L << 3)
637 #define MWM_DECOR_ALL (1L << 0)
638 #define MWM_DECOR_BORDER (1L << 1)
639 #define MWM_DECOR_RESIZEH (1L << 2)
640 #define MWM_DECOR_TITLE (1L << 3)
641 #define MWM_DECOR_MENU (1L << 4)
642 #define MWM_DECOR_MINIMIZE (1L << 5)
643 #define MWM_DECOR_MAXIMIZE (1L << 6)
645 #define MWM_FUNC_ALL (1L << 0)
646 #define MWM_FUNC_RESIZE (1L << 1)
647 #define MWM_FUNC_MOVE (1L << 2)
648 #define MWM_FUNC_MINIMIZE (1L << 3)
649 #define MWM_FUNC_MAXIMIZE (1L << 4)
650 #define MWM_FUNC_CLOSE (1L << 5)
652 #define MWM_INPUT_MODELESS 0
653 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
654 #define MWM_INPUT_SYSTEM_MODAL 2
655 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
656 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
658 #define MWM_TEAROFF_WINDOW (1L<<0)
669 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
671 // Set the window manager decorations according to the
672 // given wxWindows style
673 bool wxSetWMDecorations(Window w
, long style
)
676 GR_WM_PROPERTIES wmProp
;
681 if (style
& wxRESIZE_BORDER
)
683 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
684 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
687 if (style
& wxCLOSE_BOX
)
689 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
690 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
693 if ((style
& wxCAPTION
) ||
694 (style
& wxTINY_CAPTION_HORIZ
) ||
695 (style
& wxTINY_CAPTION_VERT
))
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
|= wxTHICK_FRAME
;
706 if (style
& wxTHICK_FRAME
)
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
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
729 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
731 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
732 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
735 GrSetWMProperties(w
, & wmProp
);
739 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
740 if (mwm_wm_hints
== 0)
744 hints
.flags
= MWM_HINTS_DECORATIONS
| MWM_HINTS_FUNCTIONS
;
745 hints
.decorations
= 0;
748 if ((style
& wxSIMPLE_BORDER
) || (style
& wxNO_BORDER
))
754 hints
.decorations
= MWM_DECOR_BORDER
;
755 hints
.functions
= MWM_FUNC_MOVE
;
757 if ((style
& wxCAPTION
) != 0)
758 hints
.decorations
|= MWM_DECOR_TITLE
;
760 if ((style
& wxSYSTEM_MENU
) != 0)
761 hints
.decorations
|= MWM_DECOR_MENU
;
763 if ((style
& wxCLOSE_BOX
) != 0)
764 hints
.functions
|= MWM_FUNC_CLOSE
;
766 if ((style
& wxMINIMIZE_BOX
) != 0)
768 hints
.functions
|= MWM_FUNC_MINIMIZE
;
769 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
772 if ((style
& wxMAXIMIZE_BOX
) != 0)
774 hints
.functions
|= MWM_FUNC_MAXIMIZE
;
775 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
778 if ((style
& wxRESIZE_BORDER
) != 0)
780 hints
.functions
|= MWM_FUNC_RESIZE
;
781 hints
.decorations
|= MWM_DECOR_RESIZEH
;
785 XChangeProperty(wxGlobalDisplay(),
787 mwm_wm_hints
, mwm_wm_hints
,
789 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);