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"
40 #include "wx/x11/private.h"
41 #include "X11/Xatom.h"
42 #include "X11/Xutil.h"
44 // Set the window manager decorations according to the
45 // given wxWindows style
47 static bool SetWMDecorations(Widget w
, long style
);
49 static bool MWMIsRunning(Window w
);
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // list of all frames and modeless dialogs
57 // wxWindowList wxModelessWindows;
59 // ----------------------------------------------------------------------------
60 // wxTopLevelWindowX11 creation
61 // ----------------------------------------------------------------------------
63 void wxTopLevelWindowX11::Init()
66 m_maximizeOnShow
= FALSE
;
68 // unlike (almost?) all other windows, frames are created hidden
71 // Data to save/restore when calling ShowFullScreen
73 m_fsIsMaximized
= FALSE
;
74 m_fsIsShowing
= FALSE
;
77 bool wxTopLevelWindowX11::CreateDialog(const wxString
& title
,
85 bool wxTopLevelWindowX11::CreateFrame(const wxString
& title
,
93 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
95 const wxString
& title
,
104 m_windowStyle
= style
;
108 m_windowId
= id
== -1 ? NewControlId() : id
;
110 wxTopLevelWindows
.Append(this);
112 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", False
);
114 XSetWMProtocols(wxGlobalDisplay(), (Window
) GetMainWindow(), &wm_delete_window
, 1);
116 SetWMDecorations((Window
) GetMainWindow(), style
);
121 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
123 return CreateDialog(title
, pos
, size
);
127 return CreateFrame(title
, pos
, size
);
131 wxTopLevelWindowX11::~wxTopLevelWindowX11()
133 wxTopLevelWindows
.DeleteObject(this);
135 if ( wxModelessWindows
.Find(this) )
136 wxModelessWindows
.DeleteObject(this);
138 // If this is the last top-level window, exit.
139 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
141 wxTheApp
->SetTopWindow(NULL
);
143 if (wxTheApp
->GetExitOnFrameDelete())
145 // Signal to the app that we're going to close
146 wxTheApp
->ExitMainLoop();
151 // ----------------------------------------------------------------------------
152 // wxTopLevelWindowX11 showing
153 // ----------------------------------------------------------------------------
155 bool wxTopLevelWindowX11::Show(bool show
)
157 if ( !wxWindowBase::Show(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())
245 XWMHints
*wmHints
= XAllocWMHints();
246 wmHints
.icon_pixmap
= (Pixmap
) icon
.GetPixmap();
248 wmHints
.flags
= IconPixmapHint
;
252 wmHints
.flags
|= IconMaskHint
;
253 wmHints
.icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
256 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(),
263 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
268 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
269 (const char*) title
);
270 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
271 (const char*) title
);
273 XTextProperty textProperty
;
274 textProperty
.value
= (unsigned char*) title
;
275 textProperty
.encoding
= XA_STRING
;
276 textProperty
.format
= 8;
277 textProperty
.nitems
= 1;
279 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
280 & textProperty
, WM_NAME
);
285 wxString
wxTopLevelWindowX11::GetTitle() const
290 #ifndef MWM_DECOR_BORDER
291 /* bit definitions for MwmHints.flags */
292 #define MWM_HINTS_FUNCTIONS (1L << 0)
293 #define MWM_HINTS_DECORATIONS (1L << 1)
294 #define MWM_HINTS_INPUT_MODE (1L << 2)
295 #define MWM_HINTS_STATUS (1L << 3)
297 #define MWM_DECOR_ALL (1L << 0)
298 #define MWM_DECOR_BORDER (1L << 1)
299 #define MWM_DECOR_RESIZEH (1L << 2)
300 #define MWM_DECOR_TITLE (1L << 3)
301 #define MWM_DECOR_MENU (1L << 4)
302 #define MWM_DECOR_MINIMIZE (1L << 5)
303 #define MWM_DECOR_MAXIMIZE (1L << 6)
313 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
315 // Set the window manager decorations according to the
316 // given wxWindows style
318 static bool SetWMDecorations(Widget w
, long style
)
320 if (!MWMIsRunning(w
))
323 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
326 hints
.decorations
= 0;
328 if (style
& wxRESIZE_BORDER
)
330 hints
.flags
|= MWM_HINTS_DECORATIONS
;
331 hints
.decorations
|= MWM_DECOR_RESIZEH
;
334 if (style
& wxSYSTEM_MENU
)
336 hints
.flags
|= MWM_HINTS_DECORATIONS
;
337 hints
.decorations
|= MWM_DECOR_MENU
;
340 if ((style
& wxCAPTION
) ||
341 (style
& wxTINY_CAPTION_HORIZ
) ||
342 (style
& wxTINY_CAPTION_VERT
))
344 hints
.flags
|= MWM_HINTS_DECORATIONS
;
345 hints
.decorations
|= MWM_DECOR_TITLE
;
348 if (style
& wxTHICK_FRAME
)
350 hints
.flags
|= MWM_HINTS_DECORATIONS
;
351 hints
.decorations
|= MWM_DECOR_BORDER
;
354 if (style
& wxTHICK_FRAME
)
356 hints
.flags
|= MWM_HINTS_DECORATIONS
;
357 hints
.decorations
|= MWM_DECOR_BORDER
;
360 if (style
& wxMINIMIZE_BOX
)
362 hints
.flags
|= MWM_HINTS_DECORATIONS
;
363 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
366 if (style
& wxMAXIMIZE_BOX
)
368 hints
.flags
|= MWM_HINTS_DECORATIONS
;
369 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
372 XChangeProperty(wxGlobalDisplay(),
374 mwm_wm_hints
, mem_wm_hints
,
376 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
382 static bool MWMIsRunning(Window w
)
384 Display
*dpy
= (Display
*)wxGetDisplay();
385 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
387 unsigned long length
, bytesafter
;
388 unsigned char value
[20];
389 unsigned char *ptr
= &value
[0];
393 type
= format
= length
= 0;
396 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
397 0L, 2, False
, motifWmInfo
,
398 &type
, &format
, &length
, &bytesafter
, &ptr
);
400 return (ret
== Success
);