]>
Commit | Line | Data |
---|---|---|
1b0fb34b JS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: x11/toplevel.cpp | |
3 | // Purpose: implements wxTopLevelWindow for X11 | |
83df96d6 JS |
4 | // Author: Julian Smart |
5 | // Modified by: | |
1b0fb34b | 6 | // Created: 24.09.01 |
83df96d6 | 7 | // RCS-ID: $Id$ |
1b0fb34b JS |
8 | // Copyright: (c) 2002 Julian Smart |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
83df96d6 JS |
11 | |
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
1b0fb34b | 21 | #pragma implementation "toplevel.h" |
83df96d6 JS |
22 | #endif |
23 | ||
1b0fb34b JS |
24 | // For compilers that support precompilation, includes "wx.h". |
25 | #include "wx/wxprec.h" | |
83df96d6 | 26 | |
1b0fb34b JS |
27 | #ifdef __BORLANDC__ |
28 | #pragma hdrstop | |
83df96d6 JS |
29 | #endif |
30 | ||
1b0fb34b JS |
31 | #ifndef WX_PRECOMP |
32 | #include "wx/app.h" | |
33 | #include "wx/toplevel.h" | |
34 | #include "wx/string.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/intl.h" | |
37 | #include "wx/frame.h" | |
e5053ade JS |
38 | #include "wx/menu.h" |
39 | #include "wx/statusbr.h" | |
1b0fb34b | 40 | #endif //WX_PRECOMP |
83df96d6 | 41 | |
2034b748 | 42 | #include "wx/settings.h" |
1b0fb34b | 43 | #include "wx/x11/private.h" |
2034b748 | 44 | #include "X11/Xutil.h" |
b513212d | 45 | |
1b0b798d | 46 | bool wxMWMIsRunning(Window w); |
b513212d | 47 | |
83df96d6 | 48 | // ---------------------------------------------------------------------------- |
1b0fb34b | 49 | // wxTopLevelWindowX11 creation |
83df96d6 JS |
50 | // ---------------------------------------------------------------------------- |
51 | ||
1b0fb34b JS |
52 | void wxTopLevelWindowX11::Init() |
53 | { | |
54 | m_iconized = | |
55 | m_maximizeOnShow = FALSE; | |
83df96d6 | 56 | |
1b0fb34b JS |
57 | // unlike (almost?) all other windows, frames are created hidden |
58 | m_isShown = FALSE; | |
83df96d6 | 59 | |
1b0fb34b JS |
60 | // Data to save/restore when calling ShowFullScreen |
61 | m_fsStyle = 0; | |
62 | m_fsIsMaximized = FALSE; | |
63 | m_fsIsShowing = FALSE; | |
64 | } | |
83df96d6 | 65 | |
1b0fb34b JS |
66 | bool wxTopLevelWindowX11::Create(wxWindow *parent, |
67 | wxWindowID id, | |
68 | const wxString& title, | |
69 | const wxPoint& pos, | |
70 | const wxSize& size, | |
71 | long style, | |
72 | const wxString& name) | |
73 | { | |
74 | // init our fields | |
75 | Init(); | |
83df96d6 JS |
76 | |
77 | m_windowStyle = style; | |
b28d3abf | 78 | m_parent = parent; |
83df96d6 | 79 | |
1b0fb34b | 80 | SetName(name); |
83df96d6 | 81 | |
1b0fb34b | 82 | m_windowId = id == -1 ? NewControlId() : id; |
83df96d6 | 83 | |
b28d3abf JS |
84 | if (parent) |
85 | parent->AddChild(this); | |
86 | ||
1b0fb34b | 87 | wxTopLevelWindows.Append(this); |
952ebeba RR |
88 | |
89 | Display *xdisplay = wxGlobalDisplay(); | |
90 | int xscreen = DefaultScreen( xdisplay ); | |
91 | Visual *xvisual = DefaultVisual( xdisplay, xscreen ); | |
92 | Window xparent = RootWindow( xdisplay, xscreen ); | |
2034b748 | 93 | Colormap cm = DefaultColormap( xdisplay, xscreen ); |
952ebeba | 94 | |
2034b748 JS |
95 | // TODO: For dialogs, this should be wxSYS_COLOUR_3DFACE |
96 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE); | |
97 | m_backgroundColour.CalcPixel( (WXColormap) cm ); | |
56cb684a JS |
98 | m_hasBgCol = TRUE; |
99 | ||
952ebeba RR |
100 | XSetWindowAttributes xattributes; |
101 | XSizeHints size_hints; | |
102 | XWMHints wm_hints; | |
103 | ||
104 | long xattributes_mask = | |
ea596687 | 105 | CWOverrideRedirect | |
952ebeba | 106 | CWBorderPixel | CWBackPixel; |
2034b748 | 107 | xattributes.background_pixel = m_backgroundColour.GetPixel(); |
952ebeba RR |
108 | xattributes.border_pixel = BlackPixel( xdisplay, xscreen ); |
109 | xattributes.override_redirect = False; | |
110 | ||
111 | Window xwindow = XCreateWindow( xdisplay, xparent, pos.x, pos.y, size.x, size.y, | |
112 | 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes ); | |
6a44bffd | 113 | m_mainWidget = (WXWindow) xwindow; |
952ebeba RR |
114 | |
115 | XSelectInput( xdisplay, xwindow, | |
116 | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | | |
117 | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | | |
118 | KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask | | |
119 | PropertyChangeMask ); | |
120 | ||
121 | wxAddWindowToTable( xwindow, (wxWindow*) this ); | |
122 | ||
123 | XSetTransientForHint( xdisplay, xwindow, xparent ); | |
124 | ||
125 | size_hints.flags = PSize; | |
126 | size_hints.width = size.x; | |
127 | size_hints.height = size.y; | |
128 | XSetWMNormalHints( xdisplay, xwindow, &size_hints); | |
129 | ||
130 | wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */; | |
131 | wm_hints.input = True; | |
132 | wm_hints.initial_state = NormalState; | |
133 | XSetWMHints( xdisplay, xwindow, &wm_hints); | |
134 | ||
135 | Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False); | |
136 | XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1); | |
137 | ||
b28d3abf | 138 | wxSetWMDecorations((Window) GetMainWindow(), style); |
83df96d6 | 139 | |
b513212d | 140 | SetTitle(title); |
952ebeba RR |
141 | |
142 | return TRUE; | |
83df96d6 JS |
143 | } |
144 | ||
1b0fb34b | 145 | wxTopLevelWindowX11::~wxTopLevelWindowX11() |
83df96d6 | 146 | { |
83df96d6 | 147 | wxTopLevelWindows.DeleteObject(this); |
83df96d6 | 148 | |
1b0fb34b JS |
149 | // If this is the last top-level window, exit. |
150 | if ( wxTheApp && (wxTopLevelWindows.Number() == 0) ) | |
83df96d6 JS |
151 | { |
152 | wxTheApp->SetTopWindow(NULL); | |
153 | ||
154 | if (wxTheApp->GetExitOnFrameDelete()) | |
155 | { | |
156 | // Signal to the app that we're going to close | |
157 | wxTheApp->ExitMainLoop(); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
1b0fb34b JS |
162 | // ---------------------------------------------------------------------------- |
163 | // wxTopLevelWindowX11 showing | |
164 | // ---------------------------------------------------------------------------- | |
83df96d6 | 165 | |
1b0fb34b | 166 | bool wxTopLevelWindowX11::Show(bool show) |
83df96d6 | 167 | { |
b513212d | 168 | return wxWindowX11::Show(show); |
83df96d6 JS |
169 | } |
170 | ||
1b0fb34b JS |
171 | // ---------------------------------------------------------------------------- |
172 | // wxTopLevelWindowX11 maximize/minimize | |
173 | // ---------------------------------------------------------------------------- | |
83df96d6 | 174 | |
1b0fb34b | 175 | void wxTopLevelWindowX11::Maximize(bool maximize) |
83df96d6 | 176 | { |
1b0fb34b | 177 | // TODO |
83df96d6 JS |
178 | } |
179 | ||
1b0fb34b | 180 | bool wxTopLevelWindowX11::IsMaximized() const |
83df96d6 | 181 | { |
1b0fb34b JS |
182 | // TODO |
183 | return TRUE; | |
83df96d6 JS |
184 | } |
185 | ||
1b0fb34b | 186 | void wxTopLevelWindowX11::Iconize(bool iconize) |
83df96d6 | 187 | { |
b513212d JS |
188 | if (!m_iconized && GetMainWindow()) |
189 | { | |
190 | if (XIconifyWindow(wxGlobalDisplay(), | |
191 | (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0) | |
192 | m_iconized = TRUE; | |
193 | } | |
83df96d6 JS |
194 | } |
195 | ||
1b0fb34b | 196 | bool wxTopLevelWindowX11::IsIconized() const |
83df96d6 | 197 | { |
1b0fb34b | 198 | return m_iconized; |
83df96d6 JS |
199 | } |
200 | ||
1b0fb34b | 201 | void wxTopLevelWindowX11::Restore() |
83df96d6 | 202 | { |
b513212d JS |
203 | // This is the way to deiconify the window, according to the X FAQ |
204 | if (m_iconized && GetMainWindow()) | |
205 | { | |
206 | XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow()); | |
207 | m_iconized = FALSE; | |
208 | } | |
83df96d6 JS |
209 | } |
210 | ||
1b0fb34b JS |
211 | // ---------------------------------------------------------------------------- |
212 | // wxTopLevelWindowX11 fullscreen | |
213 | // ---------------------------------------------------------------------------- | |
83df96d6 | 214 | |
1b0fb34b | 215 | bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style) |
83df96d6 | 216 | { |
1b0fb34b | 217 | if (show) |
83df96d6 | 218 | { |
1b0fb34b JS |
219 | if (IsFullScreen()) |
220 | return FALSE; | |
83df96d6 | 221 | |
1b0fb34b JS |
222 | m_fsIsShowing = TRUE; |
223 | m_fsStyle = style; | |
83df96d6 | 224 | |
1b0fb34b | 225 | // TODO |
83df96d6 | 226 | |
1b0fb34b | 227 | return TRUE; |
83df96d6 | 228 | } |
1b0fb34b | 229 | else |
83df96d6 | 230 | { |
1b0fb34b JS |
231 | if (!IsFullScreen()) |
232 | return FALSE; | |
83df96d6 | 233 | |
1b0fb34b | 234 | m_fsIsShowing = FALSE; |
83df96d6 | 235 | |
1b0fb34b JS |
236 | // TODO |
237 | return TRUE; | |
83df96d6 JS |
238 | } |
239 | } | |
240 | ||
1b0fb34b JS |
241 | // ---------------------------------------------------------------------------- |
242 | // wxTopLevelWindowX11 misc | |
243 | // ---------------------------------------------------------------------------- | |
83df96d6 | 244 | |
1b0fb34b | 245 | void wxTopLevelWindowX11::SetIcon(const wxIcon& icon) |
83df96d6 | 246 | { |
1b0fb34b JS |
247 | // this sets m_icon |
248 | wxTopLevelWindowBase::SetIcon(icon); | |
83df96d6 | 249 | |
b513212d JS |
250 | if (icon.Ok() && GetMainWindow()) |
251 | { | |
252 | XWMHints *wmHints = XAllocWMHints(); | |
6a44bffd | 253 | wmHints->icon_pixmap = (Pixmap) icon.GetPixmap(); |
b513212d | 254 | |
6a44bffd | 255 | wmHints->flags = IconPixmapHint; |
b513212d JS |
256 | |
257 | if (icon.GetMask()) | |
258 | { | |
6a44bffd JS |
259 | wmHints->flags |= IconMaskHint; |
260 | wmHints->icon_mask = (Pixmap) icon.GetMask()->GetPixmap(); | |
b513212d JS |
261 | } |
262 | ||
1b0b798d | 263 | XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints); |
b513212d JS |
264 | XFree(wmHints); |
265 | } | |
266 | } | |
267 | ||
268 | void wxTopLevelWindowX11::SetTitle(const wxString& title) | |
269 | { | |
270 | m_title = title; | |
271 | if (GetMainWindow()) | |
272 | { | |
273 | XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(), | |
274 | (const char*) title); | |
275 | XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(), | |
276 | (const char*) title); | |
b28d3abf JS |
277 | |
278 | // Use this if the platform doesn't supply the above functions. | |
b513212d JS |
279 | #if 0 |
280 | XTextProperty textProperty; | |
281 | textProperty.value = (unsigned char*) title; | |
282 | textProperty.encoding = XA_STRING; | |
283 | textProperty.format = 8; | |
284 | textProperty.nitems = 1; | |
285 | ||
286 | XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(), | |
287 | & textProperty, WM_NAME); | |
288 | #endif | |
289 | } | |
290 | } | |
291 | ||
292 | wxString wxTopLevelWindowX11::GetTitle() const | |
293 | { | |
294 | return m_title; | |
295 | } | |
296 | ||
297 | #ifndef MWM_DECOR_BORDER | |
298 | /* bit definitions for MwmHints.flags */ | |
299 | #define MWM_HINTS_FUNCTIONS (1L << 0) | |
300 | #define MWM_HINTS_DECORATIONS (1L << 1) | |
301 | #define MWM_HINTS_INPUT_MODE (1L << 2) | |
302 | #define MWM_HINTS_STATUS (1L << 3) | |
303 | ||
304 | #define MWM_DECOR_ALL (1L << 0) | |
305 | #define MWM_DECOR_BORDER (1L << 1) | |
306 | #define MWM_DECOR_RESIZEH (1L << 2) | |
307 | #define MWM_DECOR_TITLE (1L << 3) | |
308 | #define MWM_DECOR_MENU (1L << 4) | |
309 | #define MWM_DECOR_MINIMIZE (1L << 5) | |
310 | #define MWM_DECOR_MAXIMIZE (1L << 6) | |
311 | #endif | |
312 | ||
313 | struct MwmHints { | |
314 | long flags; | |
315 | long functions; | |
316 | long decorations; | |
317 | long input_mode; | |
318 | }; | |
319 | ||
320 | #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 | |
321 | ||
322 | // Set the window manager decorations according to the | |
323 | // given wxWindows style | |
b28d3abf | 324 | bool wxSetWMDecorations(Window w, long style) |
b513212d | 325 | { |
6a44bffd | 326 | if (!wxMWMIsRunning(w)) |
b513212d JS |
327 | return FALSE; |
328 | ||
329 | Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False); | |
330 | MwmHints hints; | |
331 | hints.flags = 0; | |
332 | hints.decorations = 0; | |
333 | ||
334 | if (style & wxRESIZE_BORDER) | |
335 | { | |
336 | hints.flags |= MWM_HINTS_DECORATIONS; | |
337 | hints.decorations |= MWM_DECOR_RESIZEH; | |
338 | } | |
339 | ||
340 | if (style & wxSYSTEM_MENU) | |
341 | { | |
342 | hints.flags |= MWM_HINTS_DECORATIONS; | |
343 | hints.decorations |= MWM_DECOR_MENU; | |
344 | } | |
345 | ||
346 | if ((style & wxCAPTION) || | |
347 | (style & wxTINY_CAPTION_HORIZ) || | |
348 | (style & wxTINY_CAPTION_VERT)) | |
349 | { | |
350 | hints.flags |= MWM_HINTS_DECORATIONS; | |
351 | hints.decorations |= MWM_DECOR_TITLE; | |
352 | } | |
353 | ||
354 | if (style & wxTHICK_FRAME) | |
355 | { | |
356 | hints.flags |= MWM_HINTS_DECORATIONS; | |
357 | hints.decorations |= MWM_DECOR_BORDER; | |
358 | } | |
359 | ||
360 | if (style & wxTHICK_FRAME) | |
361 | { | |
362 | hints.flags |= MWM_HINTS_DECORATIONS; | |
363 | hints.decorations |= MWM_DECOR_BORDER; | |
364 | } | |
365 | ||
366 | if (style & wxMINIMIZE_BOX) | |
367 | { | |
368 | hints.flags |= MWM_HINTS_DECORATIONS; | |
369 | hints.decorations |= MWM_DECOR_MINIMIZE; | |
370 | } | |
371 | ||
372 | if (style & wxMAXIMIZE_BOX) | |
373 | { | |
374 | hints.flags |= MWM_HINTS_DECORATIONS; | |
375 | hints.decorations |= MWM_DECOR_MAXIMIZE; | |
376 | } | |
377 | ||
378 | XChangeProperty(wxGlobalDisplay(), | |
379 | w, | |
6a44bffd | 380 | mwm_wm_hints, mwm_wm_hints, |
b513212d JS |
381 | 32, PropModeReplace, |
382 | (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS); | |
383 | ||
384 | return TRUE; | |
385 | } | |
386 | ||
b28d3abf | 387 | bool wxMWMIsRunning(Window w) |
b513212d | 388 | { |
68e42278 | 389 | Display *dpy = (Display*)wxGetDisplay(); |
b513212d JS |
390 | Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False); |
391 | ||
392 | unsigned long length, bytesafter; | |
393 | unsigned char value[20]; | |
68e42278 RR |
394 | unsigned char *ptr = &value[0]; |
395 | int ret, format; | |
396 | Atom type; | |
b513212d JS |
397 | |
398 | type = format = length = 0; | |
68e42278 | 399 | value[0] = 0; |
b513212d JS |
400 | |
401 | ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo, | |
402 | 0L, 2, False, motifWmInfo, | |
68e42278 | 403 | &type, &format, &length, &bytesafter, &ptr); |
b513212d JS |
404 | |
405 | return (ret == Success); | |
83df96d6 | 406 | } |
6a44bffd | 407 | |
e5053ade JS |
408 | // For implementation purposes - sometimes decorations make the client area |
409 | // smaller | |
410 | wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const | |
411 | { | |
418d4918 JS |
412 | // In fact wxFrame::GetClientAreaOrigin |
413 | // does the required calculation already. | |
414 | #if 0 | |
e5053ade JS |
415 | if (this->IsKindOf(CLASSINFO(wxFrame))) |
416 | { | |
417 | wxFrame* frame = (wxFrame*) this; | |
418 | if (frame->GetMenuBar()) | |
419 | return wxPoint(0, frame->GetMenuBar()->GetSize().y); | |
420 | } | |
418d4918 | 421 | #endif |
e5053ade JS |
422 | return wxPoint(0, 0); |
423 | } | |
424 | ||
425 | void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const | |
426 | { | |
427 | wxWindowX11::DoGetClientSize(width, height); | |
418d4918 JS |
428 | // Done by wxTopLevelWindow |
429 | #if 0 | |
e5053ade JS |
430 | if (this->IsKindOf(CLASSINFO(wxFrame))) |
431 | { | |
432 | wxFrame* frame = (wxFrame*) this; | |
433 | if (frame->GetMenuBar()) | |
434 | (*height) -= frame->GetMenuBar()->GetSize().y; | |
435 | if (frame->GetStatusBar()) | |
436 | (*height) -= frame->GetStatusBar()->GetSize().y; | |
437 | } | |
418d4918 | 438 | #endif |
e5053ade JS |
439 | } |
440 | ||
441 | void wxTopLevelWindowX11::DoSetClientSize(int width, int height) | |
442 | { | |
e5053ade JS |
443 | wxWindowX11::DoSetClientSize(width, height); |
444 | #if 0 | |
445 | if (!GetMainWindow()) | |
446 | return; | |
447 | ||
448 | XWindowChanges windowChanges; | |
449 | int valueMask = 0; | |
450 | ||
451 | if (width != -1) | |
452 | { | |
453 | windowChanges.width = width ; | |
454 | valueMask |= CWWidth; | |
455 | } | |
456 | if (height != -1) | |
457 | { | |
458 | windowChanges.height = height ; | |
459 | valueMask |= CWHeight; | |
460 | } | |
461 | XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(), | |
462 | valueMask, & windowChanges); | |
463 | #endif | |
464 | } |