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
;
66 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
68 const wxString
& title
,
77 m_windowStyle
= style
;
82 m_windowId
= id
== -1 ? NewControlId() : id
;
85 parent
->AddChild(this);
87 wxTopLevelWindows
.Append(this);
89 Display
*xdisplay
= wxGlobalDisplay();
90 int xscreen
= DefaultScreen( xdisplay
);
91 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
92 Window xparent
= RootWindow( xdisplay
, xscreen
);
93 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
95 // TODO: For dialogs, this should be wxSYS_COLOUR_3DFACE
96 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
97 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
100 XSetWindowAttributes xattributes
;
101 XSizeHints size_hints
;
104 long xattributes_mask
=
106 CWBorderPixel
| CWBackPixel
;
107 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
108 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
110 // TODO: if we want no border, caption etc.,
111 // I think we set this to True to remove decorations
112 xattributes
.override_redirect
= False
;
114 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
115 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
116 m_mainWidget
= (WXWindow
) xwindow
;
118 XSelectInput( xdisplay
, xwindow
,
119 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
120 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
121 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
122 PropertyChangeMask
);
124 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
126 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
128 size_hints
.flags
= PSize
;
129 size_hints
.width
= size
.x
;
130 size_hints
.height
= size
.y
;
131 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
133 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
134 wm_hints
.input
= True
;
135 wm_hints
.initial_state
= NormalState
;
136 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
138 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
139 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
141 wxSetWMDecorations((Window
) GetMainWindow(), style
);
148 wxTopLevelWindowX11::~wxTopLevelWindowX11()
150 wxTopLevelWindows
.DeleteObject(this);
152 // If this is the last top-level window, exit.
153 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
155 wxTheApp
->SetTopWindow(NULL
);
157 if (wxTheApp
->GetExitOnFrameDelete())
159 // Signal to the app that we're going to close
160 wxTheApp
->ExitMainLoop();
165 // ----------------------------------------------------------------------------
166 // wxTopLevelWindowX11 showing
167 // ----------------------------------------------------------------------------
169 bool wxTopLevelWindowX11::Show(bool show
)
171 return wxWindowX11::Show(show
);
174 // ----------------------------------------------------------------------------
175 // wxTopLevelWindowX11 maximize/minimize
176 // ----------------------------------------------------------------------------
178 void wxTopLevelWindowX11::Maximize(bool maximize
)
183 bool wxTopLevelWindowX11::IsMaximized() const
189 void wxTopLevelWindowX11::Iconize(bool iconize
)
191 if (!m_iconized
&& GetMainWindow())
193 if (XIconifyWindow(wxGlobalDisplay(),
194 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
199 bool wxTopLevelWindowX11::IsIconized() const
204 void wxTopLevelWindowX11::Restore()
206 // This is the way to deiconify the window, according to the X FAQ
207 if (m_iconized
&& GetMainWindow())
209 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
214 // ----------------------------------------------------------------------------
215 // wxTopLevelWindowX11 fullscreen
216 // ----------------------------------------------------------------------------
218 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
225 m_fsIsShowing
= TRUE
;
237 m_fsIsShowing
= FALSE
;
244 // ----------------------------------------------------------------------------
245 // wxTopLevelWindowX11 misc
246 // ----------------------------------------------------------------------------
248 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
251 wxTopLevelWindowBase::SetIcon(icon
);
253 if (icon
.Ok() && GetMainWindow())
255 XWMHints
*wmHints
= XAllocWMHints();
256 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
258 wmHints
->flags
= IconPixmapHint
;
262 wmHints
->flags
|= IconMaskHint
;
263 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
266 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
271 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
276 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
277 (const char*) title
);
278 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
279 (const char*) title
);
281 // Use this if the platform doesn't supply the above functions.
283 XTextProperty textProperty
;
284 textProperty
.value
= (unsigned char*) title
;
285 textProperty
.encoding
= XA_STRING
;
286 textProperty
.format
= 8;
287 textProperty
.nitems
= 1;
289 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
290 & textProperty
, WM_NAME
);
295 wxString
wxTopLevelWindowX11::GetTitle() const
300 #ifndef MWM_DECOR_BORDER
301 /* bit definitions for MwmHints.flags */
302 #define MWM_HINTS_FUNCTIONS (1L << 0)
303 #define MWM_HINTS_DECORATIONS (1L << 1)
304 #define MWM_HINTS_INPUT_MODE (1L << 2)
305 #define MWM_HINTS_STATUS (1L << 3)
307 #define MWM_DECOR_ALL (1L << 0)
308 #define MWM_DECOR_BORDER (1L << 1)
309 #define MWM_DECOR_RESIZEH (1L << 2)
310 #define MWM_DECOR_TITLE (1L << 3)
311 #define MWM_DECOR_MENU (1L << 4)
312 #define MWM_DECOR_MINIMIZE (1L << 5)
313 #define MWM_DECOR_MAXIMIZE (1L << 6)
323 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
325 // Set the window manager decorations according to the
326 // given wxWindows style
327 bool wxSetWMDecorations(Window w
, long style
)
329 if (!wxMWMIsRunning(w
))
332 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
335 hints
.decorations
= 0;
337 if (style
& wxRESIZE_BORDER
)
339 hints
.flags
|= MWM_HINTS_DECORATIONS
;
340 hints
.decorations
|= MWM_DECOR_RESIZEH
;
343 if (style
& wxSYSTEM_MENU
)
345 hints
.flags
|= MWM_HINTS_DECORATIONS
;
346 hints
.decorations
|= MWM_DECOR_MENU
;
349 if ((style
& wxCAPTION
) ||
350 (style
& wxTINY_CAPTION_HORIZ
) ||
351 (style
& wxTINY_CAPTION_VERT
))
353 hints
.flags
|= MWM_HINTS_DECORATIONS
;
354 hints
.decorations
|= MWM_DECOR_TITLE
;
357 if (style
& wxTHICK_FRAME
)
359 hints
.flags
|= MWM_HINTS_DECORATIONS
;
360 hints
.decorations
|= MWM_DECOR_BORDER
;
363 if (style
& wxTHICK_FRAME
)
365 hints
.flags
|= MWM_HINTS_DECORATIONS
;
366 hints
.decorations
|= MWM_DECOR_BORDER
;
369 if (style
& wxMINIMIZE_BOX
)
371 hints
.flags
|= MWM_HINTS_DECORATIONS
;
372 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
375 if (style
& wxMAXIMIZE_BOX
)
377 hints
.flags
|= MWM_HINTS_DECORATIONS
;
378 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
381 XChangeProperty(wxGlobalDisplay(),
383 mwm_wm_hints
, mwm_wm_hints
,
385 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
390 bool wxMWMIsRunning(Window w
)
392 Display
*dpy
= (Display
*)wxGetDisplay();
393 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
395 unsigned long length
, bytesafter
;
396 unsigned char value
[20];
397 unsigned char *ptr
= &value
[0];
401 type
= format
= length
= 0;
404 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
405 0L, 2, False
, motifWmInfo
,
406 &type
, &format
, &length
, &bytesafter
, &ptr
);
408 return (ret
== Success
);
411 // For implementation purposes - sometimes decorations make the client area
413 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
415 // In fact wxFrame::GetClientAreaOrigin
416 // does the required calculation already.
418 if (this->IsKindOf(CLASSINFO(wxFrame
)))
420 wxFrame
* frame
= (wxFrame
*) this;
421 if (frame
->GetMenuBar())
422 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
425 return wxPoint(0, 0);
428 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
430 wxWindowX11::DoGetClientSize(width
, height
);
431 // Done by wxTopLevelWindow
433 if (this->IsKindOf(CLASSINFO(wxFrame
)))
435 wxFrame
* frame
= (wxFrame
*) this;
436 if (frame
->GetMenuBar())
437 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
438 if (frame
->GetStatusBar())
439 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
444 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
446 wxWindowX11::DoSetClientSize(width
, height
);
448 if (!GetMainWindow())
451 XWindowChanges windowChanges
;
456 windowChanges
.width
= width
;
457 valueMask
|= CWWidth
;
461 windowChanges
.height
= height
;
462 valueMask
|= CWHeight
;
464 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
465 valueMask
, & windowChanges
);