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 Display
*xdisplay
= wxGlobalDisplay();
113 int xscreen
= DefaultScreen( xdisplay
);
114 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
115 Window xparent
= RootWindow( xdisplay
, xscreen
);
117 XSetWindowAttributes xattributes
;
118 XSizeHints size_hints
;
121 long xattributes_mask
=
123 CWBorderPixel
| CWBackPixel
;
124 xattributes
.background_pixel
= BlackPixel( xdisplay
, xscreen
);
125 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
126 xattributes
.override_redirect
= False
;
128 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
129 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
131 XSelectInput( xdisplay
, xwindow
,
132 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
133 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
134 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
135 PropertyChangeMask
);
137 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
139 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
141 size_hints
.flags
= PSize
;
142 size_hints
.width
= size
.x
;
143 size_hints
.height
= size
.y
;
144 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
146 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
147 wm_hints
.input
= True
;
148 wm_hints
.initial_state
= NormalState
;
149 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
151 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
152 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
155 SetWMDecorations((Window
) GetMainWindow(), style
);
163 wxTopLevelWindowX11
::~wxTopLevelWindowX11()
165 wxTopLevelWindows
.DeleteObject(this);
167 if ( wxModelessWindows
.Find(this) )
168 wxModelessWindows
.DeleteObject(this);
170 // If this is the last top-level window, exit.
171 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
173 wxTheApp
->SetTopWindow(NULL
);
175 if (wxTheApp
->GetExitOnFrameDelete())
177 // Signal to the app that we're going to close
178 wxTheApp
->ExitMainLoop();
183 // ----------------------------------------------------------------------------
184 // wxTopLevelWindowX11 showing
185 // ----------------------------------------------------------------------------
187 bool wxTopLevelWindowX11
::Show(bool show
)
189 if ( !wxWindowBase
::Show(show
) )
192 return wxWindowX11
::Show(show
);
195 // ----------------------------------------------------------------------------
196 // wxTopLevelWindowX11 maximize/minimize
197 // ----------------------------------------------------------------------------
199 void wxTopLevelWindowX11
::Maximize(bool maximize
)
204 bool wxTopLevelWindowX11
::IsMaximized() const
210 void wxTopLevelWindowX11
::Iconize(bool iconize
)
212 if (!m_iconized
&& GetMainWindow())
214 if (XIconifyWindow(wxGlobalDisplay(),
215 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
220 bool wxTopLevelWindowX11
::IsIconized() const
225 void wxTopLevelWindowX11
::Restore()
227 // This is the way to deiconify the window, according to the X FAQ
228 if (m_iconized
&& GetMainWindow())
230 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
235 // ----------------------------------------------------------------------------
236 // wxTopLevelWindowX11 fullscreen
237 // ----------------------------------------------------------------------------
239 bool wxTopLevelWindowX11
::ShowFullScreen(bool show
, long style
)
246 m_fsIsShowing
= TRUE
;
258 m_fsIsShowing
= FALSE
;
265 // ----------------------------------------------------------------------------
266 // wxTopLevelWindowX11 misc
267 // ----------------------------------------------------------------------------
269 void wxTopLevelWindowX11
::SetIcon(const wxIcon
& icon
)
272 wxTopLevelWindowBase
::SetIcon(icon
);
274 if (icon
.Ok() && GetMainWindow())
277 XWMHints
*wmHints
= XAllocWMHints();
278 wmHints
.icon_pixmap
= (Pixmap
) icon
.GetPixmap();
280 wmHints
.flags
= IconPixmapHint
;
284 wmHints
.flags
|= IconMaskHint
;
285 wmHints
.icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
288 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(),
295 void wxTopLevelWindowX11
::SetTitle(const wxString
& title
)
300 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
301 (const char*) title
);
302 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
303 (const char*) title
);
305 XTextProperty textProperty
;
306 textProperty
.value
= (unsigned char*) title
;
307 textProperty
.encoding
= XA_STRING
;
308 textProperty
.format
= 8;
309 textProperty
.nitems
= 1;
311 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
312 & textProperty
, WM_NAME
);
317 wxString wxTopLevelWindowX11
::GetTitle() const
322 #ifndef MWM_DECOR_BORDER
323 /* bit definitions for MwmHints.flags */
324 #define MWM_HINTS_FUNCTIONS (1L << 0)
325 #define MWM_HINTS_DECORATIONS (1L << 1)
326 #define MWM_HINTS_INPUT_MODE (1L << 2)
327 #define MWM_HINTS_STATUS (1L << 3)
329 #define MWM_DECOR_ALL (1L << 0)
330 #define MWM_DECOR_BORDER (1L << 1)
331 #define MWM_DECOR_RESIZEH (1L << 2)
332 #define MWM_DECOR_TITLE (1L << 3)
333 #define MWM_DECOR_MENU (1L << 4)
334 #define MWM_DECOR_MINIMIZE (1L << 5)
335 #define MWM_DECOR_MAXIMIZE (1L << 6)
345 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
347 // Set the window manager decorations according to the
348 // given wxWindows style
350 static bool SetWMDecorations(Widget w
, long style
)
352 if (!MWMIsRunning(w
))
355 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
358 hints
.decorations
= 0;
360 if (style
& wxRESIZE_BORDER
)
362 hints
.flags
|= MWM_HINTS_DECORATIONS
;
363 hints
.decorations
|= MWM_DECOR_RESIZEH
;
366 if (style
& wxSYSTEM_MENU
)
368 hints
.flags
|= MWM_HINTS_DECORATIONS
;
369 hints
.decorations
|= MWM_DECOR_MENU
;
372 if ((style
& wxCAPTION
) ||
373 (style
& wxTINY_CAPTION_HORIZ
) ||
374 (style
& wxTINY_CAPTION_VERT
))
376 hints
.flags
|= MWM_HINTS_DECORATIONS
;
377 hints
.decorations
|= MWM_DECOR_TITLE
;
380 if (style
& wxTHICK_FRAME
)
382 hints
.flags
|= MWM_HINTS_DECORATIONS
;
383 hints
.decorations
|= MWM_DECOR_BORDER
;
386 if (style
& wxTHICK_FRAME
)
388 hints
.flags
|= MWM_HINTS_DECORATIONS
;
389 hints
.decorations
|= MWM_DECOR_BORDER
;
392 if (style
& wxMINIMIZE_BOX
)
394 hints
.flags
|= MWM_HINTS_DECORATIONS
;
395 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
398 if (style
& wxMAXIMIZE_BOX
)
400 hints
.flags
|= MWM_HINTS_DECORATIONS
;
401 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
404 XChangeProperty(wxGlobalDisplay(),
406 mwm_wm_hints
, mem_wm_hints
,
408 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
414 static bool MWMIsRunning(Window w
)
416 Display
*dpy
= (Display
*)wxGetDisplay();
417 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
419 unsigned long length
, bytesafter
;
420 unsigned char value
[20];
421 unsigned char *ptr
= &value
[0];
425 type
= format
= length
= 0;
428 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
429 0L, 2, False
, motifWmInfo
,
430 &type
, &format
, &length
, &bytesafter
, &ptr
);
432 return (ret
== Success
);