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/x11/private.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
;
64 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
66 const wxString
& title
,
75 m_windowStyle
= style
;
80 m_windowId
= id
== -1 ? NewControlId() : id
;
83 parent
->AddChild(this);
85 wxTopLevelWindows
.Append(this);
87 Display
*xdisplay
= wxGlobalDisplay();
88 int xscreen
= DefaultScreen( xdisplay
);
89 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
90 Window xparent
= RootWindow( xdisplay
, xscreen
);
92 XSetWindowAttributes xattributes
;
93 XSizeHints size_hints
;
96 long xattributes_mask
=
98 CWBorderPixel
| CWBackPixel
;
99 xattributes
.background_pixel
= BlackPixel( xdisplay
, xscreen
);
100 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
101 xattributes
.override_redirect
= False
;
103 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
104 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
105 m_mainWidget
= (WXWindow
) xwindow
;
107 XSelectInput( xdisplay
, xwindow
,
108 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
109 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
110 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
111 PropertyChangeMask
);
113 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
115 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
117 size_hints
.flags
= PSize
;
118 size_hints
.width
= size
.x
;
119 size_hints
.height
= size
.y
;
120 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
122 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
123 wm_hints
.input
= True
;
124 wm_hints
.initial_state
= NormalState
;
125 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
127 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
128 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
130 wxSetWMDecorations((Window
) GetMainWindow(), style
);
137 wxTopLevelWindowX11::~wxTopLevelWindowX11()
139 wxTopLevelWindows
.DeleteObject(this);
141 // If this is the last top-level window, exit.
142 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
144 wxTheApp
->SetTopWindow(NULL
);
146 if (wxTheApp
->GetExitOnFrameDelete())
148 // Signal to the app that we're going to close
149 wxTheApp
->ExitMainLoop();
154 // ----------------------------------------------------------------------------
155 // wxTopLevelWindowX11 showing
156 // ----------------------------------------------------------------------------
158 bool wxTopLevelWindowX11::Show(bool show
)
160 return wxWindowX11::Show(show
);
163 // ----------------------------------------------------------------------------
164 // wxTopLevelWindowX11 maximize/minimize
165 // ----------------------------------------------------------------------------
167 void wxTopLevelWindowX11::Maximize(bool maximize
)
172 bool wxTopLevelWindowX11::IsMaximized() const
178 void wxTopLevelWindowX11::Iconize(bool iconize
)
180 if (!m_iconized
&& GetMainWindow())
182 if (XIconifyWindow(wxGlobalDisplay(),
183 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
188 bool wxTopLevelWindowX11::IsIconized() const
193 void wxTopLevelWindowX11::Restore()
195 // This is the way to deiconify the window, according to the X FAQ
196 if (m_iconized
&& GetMainWindow())
198 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
203 // ----------------------------------------------------------------------------
204 // wxTopLevelWindowX11 fullscreen
205 // ----------------------------------------------------------------------------
207 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
214 m_fsIsShowing
= TRUE
;
226 m_fsIsShowing
= FALSE
;
233 // ----------------------------------------------------------------------------
234 // wxTopLevelWindowX11 misc
235 // ----------------------------------------------------------------------------
237 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
240 wxTopLevelWindowBase::SetIcon(icon
);
242 if (icon
.Ok() && GetMainWindow())
244 XWMHints
*wmHints
= XAllocWMHints();
245 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
247 wmHints
->flags
= IconPixmapHint
;
251 wmHints
->flags
|= IconMaskHint
;
252 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
255 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
260 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
265 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
266 (const char*) title
);
267 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
268 (const char*) title
);
270 // Use this if the platform doesn't supply the above functions.
272 XTextProperty textProperty
;
273 textProperty
.value
= (unsigned char*) title
;
274 textProperty
.encoding
= XA_STRING
;
275 textProperty
.format
= 8;
276 textProperty
.nitems
= 1;
278 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
279 & textProperty
, WM_NAME
);
284 wxString
wxTopLevelWindowX11::GetTitle() const
289 #ifndef MWM_DECOR_BORDER
290 /* bit definitions for MwmHints.flags */
291 #define MWM_HINTS_FUNCTIONS (1L << 0)
292 #define MWM_HINTS_DECORATIONS (1L << 1)
293 #define MWM_HINTS_INPUT_MODE (1L << 2)
294 #define MWM_HINTS_STATUS (1L << 3)
296 #define MWM_DECOR_ALL (1L << 0)
297 #define MWM_DECOR_BORDER (1L << 1)
298 #define MWM_DECOR_RESIZEH (1L << 2)
299 #define MWM_DECOR_TITLE (1L << 3)
300 #define MWM_DECOR_MENU (1L << 4)
301 #define MWM_DECOR_MINIMIZE (1L << 5)
302 #define MWM_DECOR_MAXIMIZE (1L << 6)
312 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
314 // Set the window manager decorations according to the
315 // given wxWindows style
316 bool wxSetWMDecorations(Window w
, long style
)
318 if (!wxMWMIsRunning(w
))
321 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
324 hints
.decorations
= 0;
326 if (style
& wxRESIZE_BORDER
)
328 hints
.flags
|= MWM_HINTS_DECORATIONS
;
329 hints
.decorations
|= MWM_DECOR_RESIZEH
;
332 if (style
& wxSYSTEM_MENU
)
334 hints
.flags
|= MWM_HINTS_DECORATIONS
;
335 hints
.decorations
|= MWM_DECOR_MENU
;
338 if ((style
& wxCAPTION
) ||
339 (style
& wxTINY_CAPTION_HORIZ
) ||
340 (style
& wxTINY_CAPTION_VERT
))
342 hints
.flags
|= MWM_HINTS_DECORATIONS
;
343 hints
.decorations
|= MWM_DECOR_TITLE
;
346 if (style
& wxTHICK_FRAME
)
348 hints
.flags
|= MWM_HINTS_DECORATIONS
;
349 hints
.decorations
|= MWM_DECOR_BORDER
;
352 if (style
& wxTHICK_FRAME
)
354 hints
.flags
|= MWM_HINTS_DECORATIONS
;
355 hints
.decorations
|= MWM_DECOR_BORDER
;
358 if (style
& wxMINIMIZE_BOX
)
360 hints
.flags
|= MWM_HINTS_DECORATIONS
;
361 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
364 if (style
& wxMAXIMIZE_BOX
)
366 hints
.flags
|= MWM_HINTS_DECORATIONS
;
367 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
370 XChangeProperty(wxGlobalDisplay(),
372 mwm_wm_hints
, mwm_wm_hints
,
374 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
379 bool wxMWMIsRunning(Window w
)
381 Display
*dpy
= (Display
*)wxGetDisplay();
382 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
384 unsigned long length
, bytesafter
;
385 unsigned char value
[20];
386 unsigned char *ptr
= &value
[0];
390 type
= format
= length
= 0;
393 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
394 0L, 2, False
, motifWmInfo
,
395 &type
, &format
, &length
, &bytesafter
, &ptr
);
397 return (ret
== Success
);
400 // For implementation purposes - sometimes decorations make the client area
402 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
404 if (this->IsKindOf(CLASSINFO(wxFrame
)))
406 wxFrame
* frame
= (wxFrame
*) this;
407 if (frame
->GetMenuBar())
408 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
410 return wxPoint(0, 0);
413 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
415 wxWindowX11::DoGetClientSize(width
, height
);
416 if (this->IsKindOf(CLASSINFO(wxFrame
)))
418 wxFrame
* frame
= (wxFrame
*) this;
419 if (frame
->GetMenuBar())
420 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
421 if (frame
->GetStatusBar())
422 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
426 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
428 // TODO - take menubar and status line into account
429 wxWindowX11::DoSetClientSize(width
, height
);
431 if (!GetMainWindow())
434 XWindowChanges windowChanges
;
439 windowChanges
.width
= width
;
440 valueMask
|= CWWidth
;
444 windowChanges
.height
= height
;
445 valueMask
|= CWHeight
;
447 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
448 valueMask
, & windowChanges
);