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"
43 bool wxMWMIsRunning(Window w
);
45 // ----------------------------------------------------------------------------
46 // wxTopLevelWindowX11 creation
47 // ----------------------------------------------------------------------------
49 void wxTopLevelWindowX11::Init()
52 m_maximizeOnShow
= FALSE
;
54 // unlike (almost?) all other windows, frames are created hidden
57 // Data to save/restore when calling ShowFullScreen
59 m_fsIsMaximized
= FALSE
;
60 m_fsIsShowing
= FALSE
;
63 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
65 const wxString
& title
,
74 m_windowStyle
= style
;
79 m_windowId
= id
== -1 ? NewControlId() : id
;
82 parent
->AddChild(this);
84 wxTopLevelWindows
.Append(this);
86 Display
*xdisplay
= wxGlobalDisplay();
87 int xscreen
= DefaultScreen( xdisplay
);
88 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
89 Window xparent
= RootWindow( xdisplay
, xscreen
);
91 XSetWindowAttributes xattributes
;
92 XSizeHints size_hints
;
95 long xattributes_mask
=
97 CWBorderPixel
| CWBackPixel
;
98 xattributes
.background_pixel
= BlackPixel( xdisplay
, xscreen
);
99 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
100 xattributes
.override_redirect
= False
;
102 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos
.x
, pos
.y
, size
.x
, size
.y
,
103 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
104 m_mainWidget
= (WXWindow
) xwindow
;
106 XSelectInput( xdisplay
, xwindow
,
107 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
108 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
109 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
110 PropertyChangeMask
);
112 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
114 XSetTransientForHint( xdisplay
, xwindow
, xparent
);
116 size_hints
.flags
= PSize
;
117 size_hints
.width
= size
.x
;
118 size_hints
.height
= size
.y
;
119 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
121 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
122 wm_hints
.input
= True
;
123 wm_hints
.initial_state
= NormalState
;
124 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
126 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
127 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
129 wxSetWMDecorations((Window
) GetMainWindow(), style
);
136 wxTopLevelWindowX11::~wxTopLevelWindowX11()
138 wxTopLevelWindows
.DeleteObject(this);
140 // If this is the last top-level window, exit.
141 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
143 wxTheApp
->SetTopWindow(NULL
);
145 if (wxTheApp
->GetExitOnFrameDelete())
147 // Signal to the app that we're going to close
148 wxTheApp
->ExitMainLoop();
153 // ----------------------------------------------------------------------------
154 // wxTopLevelWindowX11 showing
155 // ----------------------------------------------------------------------------
157 bool wxTopLevelWindowX11::Show(bool show
)
159 if ( !wxWindowBase::Show(show
) )
162 return wxWindowX11::Show(show
);
165 // ----------------------------------------------------------------------------
166 // wxTopLevelWindowX11 maximize/minimize
167 // ----------------------------------------------------------------------------
169 void wxTopLevelWindowX11::Maximize(bool maximize
)
174 bool wxTopLevelWindowX11::IsMaximized() const
180 void wxTopLevelWindowX11::Iconize(bool iconize
)
182 if (!m_iconized
&& GetMainWindow())
184 if (XIconifyWindow(wxGlobalDisplay(),
185 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
190 bool wxTopLevelWindowX11::IsIconized() const
195 void wxTopLevelWindowX11::Restore()
197 // This is the way to deiconify the window, according to the X FAQ
198 if (m_iconized
&& GetMainWindow())
200 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
205 // ----------------------------------------------------------------------------
206 // wxTopLevelWindowX11 fullscreen
207 // ----------------------------------------------------------------------------
209 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
216 m_fsIsShowing
= TRUE
;
228 m_fsIsShowing
= FALSE
;
235 // ----------------------------------------------------------------------------
236 // wxTopLevelWindowX11 misc
237 // ----------------------------------------------------------------------------
239 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
242 wxTopLevelWindowBase::SetIcon(icon
);
244 if (icon
.Ok() && GetMainWindow())
246 XWMHints
*wmHints
= XAllocWMHints();
247 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
249 wmHints
->flags
= IconPixmapHint
;
253 wmHints
->flags
|= IconMaskHint
;
254 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetPixmap();
257 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
262 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
267 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
268 (const char*) title
);
269 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
270 (const char*) title
);
272 // Use this if the platform doesn't supply the above functions.
274 XTextProperty textProperty
;
275 textProperty
.value
= (unsigned char*) title
;
276 textProperty
.encoding
= XA_STRING
;
277 textProperty
.format
= 8;
278 textProperty
.nitems
= 1;
280 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
281 & textProperty
, WM_NAME
);
286 wxString
wxTopLevelWindowX11::GetTitle() const
291 #ifndef MWM_DECOR_BORDER
292 /* bit definitions for MwmHints.flags */
293 #define MWM_HINTS_FUNCTIONS (1L << 0)
294 #define MWM_HINTS_DECORATIONS (1L << 1)
295 #define MWM_HINTS_INPUT_MODE (1L << 2)
296 #define MWM_HINTS_STATUS (1L << 3)
298 #define MWM_DECOR_ALL (1L << 0)
299 #define MWM_DECOR_BORDER (1L << 1)
300 #define MWM_DECOR_RESIZEH (1L << 2)
301 #define MWM_DECOR_TITLE (1L << 3)
302 #define MWM_DECOR_MENU (1L << 4)
303 #define MWM_DECOR_MINIMIZE (1L << 5)
304 #define MWM_DECOR_MAXIMIZE (1L << 6)
314 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
316 // Set the window manager decorations according to the
317 // given wxWindows style
318 bool wxSetWMDecorations(Window w
, long style
)
320 if (!wxMWMIsRunning(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
, mwm_wm_hints
,
376 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
381 bool wxMWMIsRunning(Window w
)
383 Display
*dpy
= (Display
*)wxGetDisplay();
384 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
386 unsigned long length
, bytesafter
;
387 unsigned char value
[20];
388 unsigned char *ptr
= &value
[0];
392 type
= format
= length
= 0;
395 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
396 0L, 2, False
, motifWmInfo
,
397 &type
, &format
, &length
, &bytesafter
, &ptr
);
399 return (ret
== Success
);