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
;
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 xattributes
.override_redirect
= False
;
134 long backColor
, foreColor
;
135 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
136 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
138 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
139 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
141 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
142 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
144 m_mainWidget
= (WXWindow
) xwindow
;
148 extraFlags
|= GR_EVENT_MASK_CLOSE_REQ
;
151 XSelectInput( xdisplay
, xwindow
,
165 StructureNotifyMask
|
170 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
172 // Set background to None which will prevent X11 from clearing the
173 // background completely.
174 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
177 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
179 if (GetParent() && GetParent()->GetMainWindow())
181 Window xparentwindow
= (Window
) GetParent()->GetMainWindow();
182 XSetTransientForHint( xdisplay
, xwindow
, xparentwindow
);
186 size_hints
.flags
= PSize
| PPosition
;
187 size_hints
.x
= pos2
.x
;
188 size_hints
.y
= pos2
.y
;
189 size_hints
.width
= size2
.x
;
190 size_hints
.height
= size2
.y
;
191 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
194 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
195 wm_hints
.input
= True
;
196 wm_hints
.initial_state
= NormalState
;
197 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
199 Atom wm_protocols
[2];
200 wm_protocols
[0] = XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
201 wm_protocols
[1] = XInternAtom( xdisplay
, "WM_TAKE_FOCUS", False
);
202 XSetWMProtocols( xdisplay
, xwindow
, wm_protocols
, 2);
205 wxSetWMDecorations( xwindow
, style
);
212 wxTopLevelWindowX11
::~wxTopLevelWindowX11()
214 wxTopLevelWindows
.DeleteObject(this);
216 // If this is the last top-level window, exit.
217 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
219 wxTheApp
->SetTopWindow(NULL
);
221 if (wxTheApp
->GetExitOnFrameDelete())
223 // Signal to the app that we're going to close
224 wxTheApp
->ExitMainLoop();
229 // ----------------------------------------------------------------------------
230 // wxTopLevelWindowX11 showing
231 // ----------------------------------------------------------------------------
233 bool wxTopLevelWindowX11
::Show(bool show
)
235 return wxWindowX11
::Show(show
);
238 // ----------------------------------------------------------------------------
239 // wxTopLevelWindowX11 maximize/minimize
240 // ----------------------------------------------------------------------------
242 void wxTopLevelWindowX11
::Maximize(bool maximize
)
247 bool wxTopLevelWindowX11
::IsMaximized() const
253 void wxTopLevelWindowX11
::Iconize(bool iconize
)
255 if (!m_iconized
&& GetMainWindow())
257 if (XIconifyWindow(wxGlobalDisplay(),
258 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
263 bool wxTopLevelWindowX11
::IsIconized() const
268 void wxTopLevelWindowX11
::Restore()
270 // This is the way to deiconify the window, according to the X FAQ
271 if (m_iconized
&& GetMainWindow())
273 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
278 // ----------------------------------------------------------------------------
279 // wxTopLevelWindowX11 fullscreen
280 // ----------------------------------------------------------------------------
282 bool wxTopLevelWindowX11
::ShowFullScreen(bool show
, long style
)
289 m_fsIsShowing
= TRUE
;
301 m_fsIsShowing
= FALSE
;
308 // ----------------------------------------------------------------------------
309 // wxTopLevelWindowX11 misc
310 // ----------------------------------------------------------------------------
312 void wxTopLevelWindowX11
::SetIcon(const wxIcon
& icon
)
315 wxTopLevelWindowBase
::SetIcon(icon
);
317 if (icon
.Ok() && GetMainWindow())
321 XWMHints
*wmHints
= XAllocWMHints();
322 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
324 wmHints
->flags
= IconPixmapHint
;
328 wmHints
->flags
|= IconMaskHint
;
329 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
332 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
338 void wxTopLevelWindowX11
::SetTitle(const wxString
& title
)
343 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
344 (const char*) title
);
345 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
346 (const char*) title
);
348 // Use this if the platform doesn't supply the above functions.
350 XTextProperty textProperty
;
351 textProperty
.value
= (unsigned char*) title
;
352 textProperty
.encoding
= XA_STRING
;
353 textProperty
.format
= 8;
354 textProperty
.nitems
= 1;
356 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
357 & textProperty
, WM_NAME
);
362 wxString wxTopLevelWindowX11
::GetTitle() const
367 #ifndef MWM_DECOR_BORDER
368 /* bit definitions for MwmHints.flags */
369 #define MWM_HINTS_FUNCTIONS (1L << 0)
370 #define MWM_HINTS_DECORATIONS (1L << 1)
371 #define MWM_HINTS_INPUT_MODE (1L << 2)
372 #define MWM_HINTS_STATUS (1L << 3)
374 #define MWM_DECOR_ALL (1L << 0)
375 #define MWM_DECOR_BORDER (1L << 1)
376 #define MWM_DECOR_RESIZEH (1L << 2)
377 #define MWM_DECOR_TITLE (1L << 3)
378 #define MWM_DECOR_MENU (1L << 4)
379 #define MWM_DECOR_MINIMIZE (1L << 5)
380 #define MWM_DECOR_MAXIMIZE (1L << 6)
390 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
392 // Set the window manager decorations according to the
393 // given wxWindows style
394 bool wxSetWMDecorations(Window w
, long style
)
397 GR_WM_PROPERTIES wmProp
;
402 if (style
& wxRESIZE_BORDER
)
404 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
405 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
408 if (style
& wxSYSTEM_MENU
)
410 wmProp
.props
|= GR_WM_PROPS_CLOSEBOX
;
411 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
414 if ((style
& wxCAPTION
) ||
415 (style
& wxTINY_CAPTION_HORIZ
) ||
416 (style
& wxTINY_CAPTION_VERT
))
418 wmProp
.props
|= GR_WM_PROPS_CAPTION
;
419 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
421 // The default dialog style doesn't include any kind
422 // of border, which is a bit odd. Anyway, inclusion
423 // of a caption surely implies a border.
424 style
|= wxTHICK_FRAME
;
427 if (style
& wxTHICK_FRAME
)
429 wmProp
.props
|= GR_WM_PROPS_APPFRAME
;
430 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
433 if (style
& wxSIMPLE_BORDER
)
435 wmProp
.props
|= GR_WM_PROPS_BORDER
;
436 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
439 if (style
& wxMINIMIZE_BOX
)
443 if (style
& wxMAXIMIZE_BOX
)
445 wmProp
.props
|= GR_WM_PROPS_MAXIMIZE
;
446 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
449 if (((style
& wxBORDER
) != wxBORDER
) && ((style
& wxTHICK_FRAME
) != wxTHICK_FRAME
)
450 && ((style
& wxRESIZE_BORDER
) != wxRESIZE_BORDER
))
452 wmProp
.props
|= GR_WM_PROPS_NODECORATE
;
453 wmProp
.flags
|= GR_WM_FLAGS_PROPS
;
456 GrSetWMProperties(w
, & wmProp
);
459 if (!wxMWMIsRunning(w
))
462 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
465 hints
.decorations
= 0;
467 if (style
& wxRESIZE_BORDER
)
469 // wxLogDebug("MWM_DECOR_RESIZEH");
470 hints
.flags
|= MWM_HINTS_DECORATIONS
;
471 hints
.decorations
|= MWM_DECOR_RESIZEH
;
474 if (style
& wxSYSTEM_MENU
)
476 // wxLogDebug("MWM_DECOR_MENU");
477 hints
.flags
|= MWM_HINTS_DECORATIONS
;
478 hints
.decorations
|= MWM_DECOR_MENU
;
481 if ((style
& wxCAPTION
) ||
482 (style
& wxTINY_CAPTION_HORIZ
) ||
483 (style
& wxTINY_CAPTION_VERT
))
485 // wxLogDebug("MWM_DECOR_TITLE");
486 hints
.flags
|= MWM_HINTS_DECORATIONS
;
487 hints
.decorations
|= MWM_DECOR_TITLE
;
490 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
492 // wxLogDebug("MWM_DECOR_BORDER");
493 hints
.flags
|= MWM_HINTS_DECORATIONS
;
494 hints
.decorations
|= MWM_DECOR_BORDER
;
497 if (style
& wxMINIMIZE_BOX
)
499 // wxLogDebug("MWM_DECOR_MINIMIZE");
500 hints
.flags
|= MWM_HINTS_DECORATIONS
;
501 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
504 if (style
& wxMAXIMIZE_BOX
)
506 // wxLogDebug("MWM_DECOR_MAXIMIZE");
507 hints
.flags
|= MWM_HINTS_DECORATIONS
;
508 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
511 XChangeProperty(wxGlobalDisplay(),
513 mwm_wm_hints
, mwm_wm_hints
,
515 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
521 bool wxMWMIsRunning(Window w
)
526 Display
*dpy
= (Display
*)wxGetDisplay();
527 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
529 unsigned long length
, bytesafter
;
530 unsigned char value
[20];
531 unsigned char *ptr
= &value
[0];
535 type
= format
= length
= 0;
538 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
539 0L, 2, False
, motifWmInfo
,
540 &type
, &format
, &length
, &bytesafter
, &ptr
);
542 return (ret
== Success
);
546 // For implementation purposes - sometimes decorations make the client area
548 wxPoint wxTopLevelWindowX11
::GetClientAreaOrigin() const
550 // In fact wxFrame::GetClientAreaOrigin
551 // does the required calculation already.
553 if (this->IsKindOf(CLASSINFO(wxFrame
)))
555 wxFrame
* frame
= (wxFrame
*) this;
556 if (frame
->GetMenuBar())
557 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
560 return wxPoint(0, 0);
563 void wxTopLevelWindowX11
::DoGetClientSize( int *width
, int *height
) const
565 wxWindowX11
::DoGetClientSize(width
, height
);
566 // Done by wxTopLevelWindow
568 if (this->IsKindOf(CLASSINFO(wxFrame
)))
570 wxFrame
* frame
= (wxFrame
*) this;
571 if (frame
->GetMenuBar())
572 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
573 if (frame
->GetStatusBar())
574 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
579 void wxTopLevelWindowX11
::DoSetClientSize(int width
, int height
)
581 wxWindowX11
::DoSetClientSize(width
, height
);
584 if (!GetMainWindow())
587 XWindowChanges windowChanges
;
592 windowChanges
.width
= width
;
593 valueMask
|= CWWidth
;
597 windowChanges
.height
= height
;
598 valueMask
|= CWHeight
;
600 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
601 valueMask
, & windowChanges
);
605 void wxTopLevelWindowX11
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
607 // wxLogDebug( "Setting pos: %d, %d", x, y );
608 wxWindowX11
::DoSetSize(x
, y
, width
, height
, sizeFlags
);
610 wxPoint pt
= GetPosition();
611 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
613 XSync(wxGlobalDisplay(), False
);
617 msg
.Printf("Before setting size: %d, %d", w
, h
);
619 if (!GetMainWindow())
622 XWindowChanges windowChanges
;
625 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
628 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
632 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
635 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
639 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
641 windowChanges
.width
= width
/* - m_borderSize*2 */;
642 valueMask
|= CWWidth
;
644 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
646 windowChanges
.height
= height
/* -m_borderSize*2*/;
647 valueMask
|= CWHeight
;
650 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
651 valueMask
, & windowChanges
);
652 XSync(wxGlobalDisplay(), False
);
654 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
659 void wxTopLevelWindowX11
::DoGetPosition(int *x
, int *y
) const
661 XSync(wxGlobalDisplay(), False
);
662 Window window
= (Window
) m_mainWidget
;
669 // wxLogDebug("Translating...");
671 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
672 0, 0, & offsetX
, & offsetY
, & childWindow
);
674 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
677 XWindowAttributes attr
;
678 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
683 *x
= attr
.x
+ offsetX
;
684 *y
= attr
.y
+ offsetY
;