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 // list of all frames and modeless dialogs
45 // wxWindowList wxModelessWindows;
47 // ----------------------------------------------------------------------------
48 // wxTopLevelWindowX11 creation
49 // ----------------------------------------------------------------------------
51 void wxTopLevelWindowX11::Init()
54 m_maximizeOnShow
= FALSE
;
56 // unlike (almost?) all other windows, frames are created hidden
59 // Data to save/restore when calling ShowFullScreen
61 m_fsIsMaximized
= FALSE
;
62 m_fsIsShowing
= FALSE
;
65 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
67 const wxString
& title
,
76 m_windowStyle
= style
;
81 m_windowId
= id
== -1 ? NewControlId() : id
;
84 parent
->AddChild(this);
86 wxTopLevelWindows
.Append(this);
88 Display
*xdisplay
= wxGlobalDisplay();
89 int xscreen
= DefaultScreen( xdisplay
);
90 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
91 Window xparent
= RootWindow( xdisplay
, xscreen
);
93 XSetWindowAttributes xattributes
;
94 XSizeHints size_hints
;
97 long xattributes_mask
=
99 CWBorderPixel
| CWBackPixel
;
100 xattributes
.background_pixel
= BlackPixel( xdisplay
, xscreen
);
101 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
102 xattributes
.override_redirect
= False
;
104 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
105 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
106 m_mainWidget
= (WXWindow
) xwindow
;
108 XSelectInput( xdisplay
, xwindow
,
109 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
110 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
111 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
112 PropertyChangeMask
);
114 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
116 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
118 size_hints
.flags
= PSize
;
119 size_hints
.width
= size
.x
;
120 size_hints
.height
= size
.y
;
121 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
123 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
124 wm_hints
.input
= True
;
125 wm_hints
.initial_state
= NormalState
;
126 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
128 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
129 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
131 wxSetWMDecorations((Window
) GetMainWindow(), style
);
138 wxTopLevelWindowX11::~wxTopLevelWindowX11()
140 wxTopLevelWindows
.DeleteObject(this);
142 // If this is the last top-level window, exit.
143 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
145 wxTheApp
->SetTopWindow(NULL
);
147 if (wxTheApp
->GetExitOnFrameDelete())
149 // Signal to the app that we're going to close
150 wxTheApp
->ExitMainLoop();
155 // ----------------------------------------------------------------------------
156 // wxTopLevelWindowX11 showing
157 // ----------------------------------------------------------------------------
159 bool wxTopLevelWindowX11::Show(bool show
)
161 if ( !wxWindowBase::Show(show
) )
164 return wxWindowX11::Show(show
);
167 // ----------------------------------------------------------------------------
168 // wxTopLevelWindowX11 maximize/minimize
169 // ----------------------------------------------------------------------------
171 void wxTopLevelWindowX11::Maximize(bool maximize
)
176 bool wxTopLevelWindowX11::IsMaximized() const
182 void wxTopLevelWindowX11::Iconize(bool iconize
)
184 if (!m_iconized
&& GetMainWindow())
186 if (XIconifyWindow(wxGlobalDisplay(),
187 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
192 bool wxTopLevelWindowX11::IsIconized() const
197 void wxTopLevelWindowX11::Restore()
199 // This is the way to deiconify the window, according to the X FAQ
200 if (m_iconized
&& GetMainWindow())
202 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
207 // ----------------------------------------------------------------------------
208 // wxTopLevelWindowX11 fullscreen
209 // ----------------------------------------------------------------------------
211 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
218 m_fsIsShowing
= TRUE
;
230 m_fsIsShowing
= FALSE
;
237 // ----------------------------------------------------------------------------
238 // wxTopLevelWindowX11 misc
239 // ----------------------------------------------------------------------------
241 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
244 wxTopLevelWindowBase::SetIcon(icon
);
246 if (icon
.Ok() && GetMainWindow())
248 XWMHints
*wmHints
= XAllocWMHints();
249 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
251 wmHints
->flags
= IconPixmapHint
;
255 wmHints
->flags
|= IconMaskHint
;
256 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
259 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(),
265 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
270 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
271 (const char*) title
);
272 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
273 (const char*) title
);
275 // Use this if the platform doesn't supply the above functions.
277 XTextProperty textProperty
;
278 textProperty
.value
= (unsigned char*) title
;
279 textProperty
.encoding
= XA_STRING
;
280 textProperty
.format
= 8;
281 textProperty
.nitems
= 1;
283 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
284 & textProperty
, WM_NAME
);
289 wxString
wxTopLevelWindowX11::GetTitle() const
294 #ifndef MWM_DECOR_BORDER
295 /* bit definitions for MwmHints.flags */
296 #define MWM_HINTS_FUNCTIONS (1L << 0)
297 #define MWM_HINTS_DECORATIONS (1L << 1)
298 #define MWM_HINTS_INPUT_MODE (1L << 2)
299 #define MWM_HINTS_STATUS (1L << 3)
301 #define MWM_DECOR_ALL (1L << 0)
302 #define MWM_DECOR_BORDER (1L << 1)
303 #define MWM_DECOR_RESIZEH (1L << 2)
304 #define MWM_DECOR_TITLE (1L << 3)
305 #define MWM_DECOR_MENU (1L << 4)
306 #define MWM_DECOR_MINIMIZE (1L << 5)
307 #define MWM_DECOR_MAXIMIZE (1L << 6)
317 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
319 // Set the window manager decorations according to the
320 // given wxWindows style
321 bool wxSetWMDecorations(Window w
, long style
)
323 if (!wxMWMIsRunning(w
))
326 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
329 hints
.decorations
= 0;
331 if (style
& wxRESIZE_BORDER
)
333 hints
.flags
|= MWM_HINTS_DECORATIONS
;
334 hints
.decorations
|= MWM_DECOR_RESIZEH
;
337 if (style
& wxSYSTEM_MENU
)
339 hints
.flags
|= MWM_HINTS_DECORATIONS
;
340 hints
.decorations
|= MWM_DECOR_MENU
;
343 if ((style
& wxCAPTION
) ||
344 (style
& wxTINY_CAPTION_HORIZ
) ||
345 (style
& wxTINY_CAPTION_VERT
))
347 hints
.flags
|= MWM_HINTS_DECORATIONS
;
348 hints
.decorations
|= MWM_DECOR_TITLE
;
351 if (style
& wxTHICK_FRAME
)
353 hints
.flags
|= MWM_HINTS_DECORATIONS
;
354 hints
.decorations
|= MWM_DECOR_BORDER
;
357 if (style
& wxTHICK_FRAME
)
359 hints
.flags
|= MWM_HINTS_DECORATIONS
;
360 hints
.decorations
|= MWM_DECOR_BORDER
;
363 if (style
& wxMINIMIZE_BOX
)
365 hints
.flags
|= MWM_HINTS_DECORATIONS
;
366 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
369 if (style
& wxMAXIMIZE_BOX
)
371 hints
.flags
|= MWM_HINTS_DECORATIONS
;
372 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
375 XChangeProperty(wxGlobalDisplay(),
377 mwm_wm_hints
, mwm_wm_hints
,
379 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
384 bool wxMWMIsRunning(Window w
)
386 Display
*dpy
= (Display
*)wxGetDisplay();
387 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
389 unsigned long length
, bytesafter
;
390 unsigned char value
[20];
391 unsigned char *ptr
= &value
[0];
395 type
= format
= length
= 0;
398 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
399 0L, 2, False
, motifWmInfo
,
400 &type
, &format
, &length
, &bytesafter
, &ptr
);
402 return (ret
== Success
);