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
46 static bool SetWMDecorations(Widget w
, long style
);
47 static bool MWMIsRunning(Window w
);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // list of all frames and modeless dialogs
55 // wxWindowList wxModelessWindows;
57 // ----------------------------------------------------------------------------
58 // wxTopLevelWindowX11 creation
59 // ----------------------------------------------------------------------------
61 void wxTopLevelWindowX11::Init()
64 m_maximizeOnShow
= FALSE
;
66 // unlike (almost?) all other windows, frames are created hidden
69 // Data to save/restore when calling ShowFullScreen
71 m_fsIsMaximized
= FALSE
;
72 m_fsIsShowing
= FALSE
;
75 bool wxTopLevelWindowX11::CreateDialog(const wxString
& title
,
83 bool wxTopLevelWindowX11::CreateFrame(const wxString
& title
,
91 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
93 const wxString
& title
,
102 m_windowStyle
= style
;
106 m_windowId
= id
== -1 ? NewControlId() : id
;
108 wxTopLevelWindows
.Append(this);
110 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", False
);
112 XSetWMProtocols(wxGlobalDisplay(), (Window
) GetMainWindow(), &wm_delete_window
, 1);
113 SetWMDecorations((Window
) GetMainWindow(), style
);
117 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
119 return CreateDialog(title
, pos
, size
);
123 return CreateFrame(title
, pos
, size
);
127 wxTopLevelWindowX11::~wxTopLevelWindowX11()
129 wxTopLevelWindows
.DeleteObject(this);
131 if ( wxModelessWindows
.Find(this) )
132 wxModelessWindows
.DeleteObject(this);
134 // If this is the last top-level window, exit.
135 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
137 wxTheApp
->SetTopWindow(NULL
);
139 if (wxTheApp
->GetExitOnFrameDelete())
141 // Signal to the app that we're going to close
142 wxTheApp
->ExitMainLoop();
147 // ----------------------------------------------------------------------------
148 // wxTopLevelWindowX11 showing
149 // ----------------------------------------------------------------------------
151 bool wxTopLevelWindowX11::Show(bool show
)
153 if ( !wxWindowBase::Show(show
) )
156 return wxWindowX11::Show(show
);
159 // ----------------------------------------------------------------------------
160 // wxTopLevelWindowX11 maximize/minimize
161 // ----------------------------------------------------------------------------
163 void wxTopLevelWindowX11::Maximize(bool maximize
)
168 bool wxTopLevelWindowX11::IsMaximized() const
174 void wxTopLevelWindowX11::Iconize(bool iconize
)
176 if (!m_iconized
&& GetMainWindow())
178 if (XIconifyWindow(wxGlobalDisplay(),
179 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
184 bool wxTopLevelWindowX11::IsIconized() const
189 void wxTopLevelWindowX11::Restore()
191 // This is the way to deiconify the window, according to the X FAQ
192 if (m_iconized
&& GetMainWindow())
194 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
199 // ----------------------------------------------------------------------------
200 // wxTopLevelWindowX11 fullscreen
201 // ----------------------------------------------------------------------------
203 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
210 m_fsIsShowing
= TRUE
;
222 m_fsIsShowing
= FALSE
;
229 // ----------------------------------------------------------------------------
230 // wxTopLevelWindowX11 misc
231 // ----------------------------------------------------------------------------
233 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
236 wxTopLevelWindowBase::SetIcon(icon
);
238 if (icon
.Ok() && GetMainWindow())
240 XWMHints
*wmHints
= XAllocWMHints();
241 wmHints
.icon_pixmap
= (Pixmap
) icon
.GetPixmap();
243 wmHints
.flags
= IconPixmapHint
;
247 wmHints
.flags
|= IconMaskHint
;
248 wmHints
.icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
251 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(),
257 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
262 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
263 (const char*) title
);
264 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
265 (const char*) title
);
267 XTextProperty textProperty
;
268 textProperty
.value
= (unsigned char*) title
;
269 textProperty
.encoding
= XA_STRING
;
270 textProperty
.format
= 8;
271 textProperty
.nitems
= 1;
273 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
274 & textProperty
, WM_NAME
);
279 wxString
wxTopLevelWindowX11::GetTitle() const
284 #ifndef MWM_DECOR_BORDER
285 /* bit definitions for MwmHints.flags */
286 #define MWM_HINTS_FUNCTIONS (1L << 0)
287 #define MWM_HINTS_DECORATIONS (1L << 1)
288 #define MWM_HINTS_INPUT_MODE (1L << 2)
289 #define MWM_HINTS_STATUS (1L << 3)
291 #define MWM_DECOR_ALL (1L << 0)
292 #define MWM_DECOR_BORDER (1L << 1)
293 #define MWM_DECOR_RESIZEH (1L << 2)
294 #define MWM_DECOR_TITLE (1L << 3)
295 #define MWM_DECOR_MENU (1L << 4)
296 #define MWM_DECOR_MINIMIZE (1L << 5)
297 #define MWM_DECOR_MAXIMIZE (1L << 6)
307 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
309 // Set the window manager decorations according to the
310 // given wxWindows style
311 static bool SetWMDecorations(Widget w
, long style
)
313 if (!MWMIsRunning(w
))
316 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
319 hints
.decorations
= 0;
321 if (style
& wxRESIZE_BORDER
)
323 hints
.flags
|= MWM_HINTS_DECORATIONS
;
324 hints
.decorations
|= MWM_DECOR_RESIZEH
;
327 if (style
& wxSYSTEM_MENU
)
329 hints
.flags
|= MWM_HINTS_DECORATIONS
;
330 hints
.decorations
|= MWM_DECOR_MENU
;
333 if ((style
& wxCAPTION
) ||
334 (style
& wxTINY_CAPTION_HORIZ
) ||
335 (style
& wxTINY_CAPTION_VERT
))
337 hints
.flags
|= MWM_HINTS_DECORATIONS
;
338 hints
.decorations
|= MWM_DECOR_TITLE
;
341 if (style
& wxTHICK_FRAME
)
343 hints
.flags
|= MWM_HINTS_DECORATIONS
;
344 hints
.decorations
|= MWM_DECOR_BORDER
;
347 if (style
& wxTHICK_FRAME
)
349 hints
.flags
|= MWM_HINTS_DECORATIONS
;
350 hints
.decorations
|= MWM_DECOR_BORDER
;
353 if (style
& wxMINIMIZE_BOX
)
355 hints
.flags
|= MWM_HINTS_DECORATIONS
;
356 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
359 if (style
& wxMAXIMIZE_BOX
)
361 hints
.flags
|= MWM_HINTS_DECORATIONS
;
362 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
365 XChangeProperty(wxGlobalDisplay(),
367 mwm_wm_hints
, mem_wm_hints
,
369 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
374 static bool MWMIsRunning(Window w
)
376 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
378 unsigned long length
, bytesafter
;
379 unsigned char value
[20];
380 int ret
, type
, format
;
382 type
= format
= length
= 0;
385 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
386 0L, 2, False
, motifWmInfo
,
387 &type
, &format
, &length
, &bytesafter
, &value
);
389 return (ret
== Success
);