]>
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 | 108 | xattributes.border_pixel = BlackPixel( xdisplay, xscreen ); |
5e29f97a JS |
109 | |
110 | // TODO: if we want no border, caption etc., | |
111 | // I think we set this to True to remove decorations | |
952ebeba RR |
112 | xattributes.override_redirect = False; |
113 | ||
114 | Window xwindow = XCreateWindow( xdisplay, xparent, pos.x, pos.y, size.x, size.y, | |
115 | 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes ); | |
6a44bffd | 116 | m_mainWidget = (WXWindow) xwindow; |
952ebeba RR |
117 | |
118 | XSelectInput( xdisplay, xwindow, | |
119 | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | | |
120 | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | | |
121 | KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask | | |
122 | PropertyChangeMask ); | |
123 | ||
124 | wxAddWindowToTable( xwindow, (wxWindow*) this ); | |
125 | ||
126 | XSetTransientForHint( xdisplay, xwindow, xparent ); | |
127 | ||
128 | size_hints.flags = PSize; | |
129 | size_hints.width = size.x; | |
130 | size_hints.height = size.y; | |
131 | XSetWMNormalHints( xdisplay, xwindow, &size_hints); | |
132 | ||
133 | wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */; | |
134 | wm_hints.input = True; | |
135 | wm_hints.initial_state = NormalState; | |
136 | XSetWMHints( xdisplay, xwindow, &wm_hints); | |
137 | ||
138 | Atom wm_delete_window = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False); | |
139 | XSetWMProtocols( xdisplay, xwindow, &wm_delete_window, 1); | |
140 | ||
b28d3abf | 141 | wxSetWMDecorations((Window) GetMainWindow(), style); |
83df96d6 | 142 | |
b513212d | 143 | SetTitle(title); |
952ebeba RR |
144 | |
145 | return TRUE; | |
83df96d6 JS |
146 | } |
147 | ||
1b0fb34b | 148 | wxTopLevelWindowX11::~wxTopLevelWindowX11() |
83df96d6 | 149 | { |
83df96d6 | 150 | wxTopLevelWindows.DeleteObject(this); |
83df96d6 | 151 | |
1b0fb34b JS |
152 | // If this is the last top-level window, exit. |
153 | if ( wxTheApp && (wxTopLevelWindows.Number() == 0) ) | |
83df96d6 JS |
154 | { |
155 | wxTheApp->SetTopWindow(NULL); | |
156 | ||
157 | if (wxTheApp->GetExitOnFrameDelete()) | |
158 | { | |
159 | // Signal to the app that we're going to close | |
160 | wxTheApp->ExitMainLoop(); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
1b0fb34b JS |
165 | // ---------------------------------------------------------------------------- |
166 | // wxTopLevelWindowX11 showing | |
167 | // ---------------------------------------------------------------------------- | |
83df96d6 | 168 | |
1b0fb34b | 169 | bool wxTopLevelWindowX11::Show(bool show) |
83df96d6 | 170 | { |
b513212d | 171 | return wxWindowX11::Show(show); |
83df96d6 JS |
172 | } |
173 | ||
1b0fb34b JS |
174 | // ---------------------------------------------------------------------------- |
175 | // wxTopLevelWindowX11 maximize/minimize | |
176 | // ---------------------------------------------------------------------------- | |
83df96d6 | 177 | |
1b0fb34b | 178 | void wxTopLevelWindowX11::Maximize(bool maximize) |
83df96d6 | 179 | { |
1b0fb34b | 180 | // TODO |
83df96d6 JS |
181 | } |
182 | ||
1b0fb34b | 183 | bool wxTopLevelWindowX11::IsMaximized() const |
83df96d6 | 184 | { |
1b0fb34b JS |
185 | // TODO |
186 | return TRUE; | |
83df96d6 JS |
187 | } |
188 | ||
1b0fb34b | 189 | void wxTopLevelWindowX11::Iconize(bool iconize) |
83df96d6 | 190 | { |
b513212d JS |
191 | if (!m_iconized && GetMainWindow()) |
192 | { | |
193 | if (XIconifyWindow(wxGlobalDisplay(), | |
194 | (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0) | |
195 | m_iconized = TRUE; | |
196 | } | |
83df96d6 JS |
197 | } |
198 | ||
1b0fb34b | 199 | bool wxTopLevelWindowX11::IsIconized() const |
83df96d6 | 200 | { |
1b0fb34b | 201 | return m_iconized; |
83df96d6 JS |
202 | } |
203 | ||
1b0fb34b | 204 | void wxTopLevelWindowX11::Restore() |
83df96d6 | 205 | { |
b513212d JS |
206 | // This is the way to deiconify the window, according to the X FAQ |
207 | if (m_iconized && GetMainWindow()) | |
208 | { | |
209 | XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow()); | |
210 | m_iconized = FALSE; | |
211 | } | |
83df96d6 JS |
212 | } |
213 | ||
1b0fb34b JS |
214 | // ---------------------------------------------------------------------------- |
215 | // wxTopLevelWindowX11 fullscreen | |
216 | // ---------------------------------------------------------------------------- | |
83df96d6 | 217 | |
1b0fb34b | 218 | bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style) |
83df96d6 | 219 | { |
1b0fb34b | 220 | if (show) |
83df96d6 | 221 | { |
1b0fb34b JS |
222 | if (IsFullScreen()) |
223 | return FALSE; | |
83df96d6 | 224 | |
1b0fb34b JS |
225 | m_fsIsShowing = TRUE; |
226 | m_fsStyle = style; | |
83df96d6 | 227 | |
1b0fb34b | 228 | // TODO |
83df96d6 | 229 | |
1b0fb34b | 230 | return TRUE; |
83df96d6 | 231 | } |
1b0fb34b | 232 | else |
83df96d6 | 233 | { |
1b0fb34b JS |
234 | if (!IsFullScreen()) |
235 | return FALSE; | |
83df96d6 | 236 | |
1b0fb34b | 237 | m_fsIsShowing = FALSE; |
83df96d6 | 238 | |
1b0fb34b JS |
239 | // TODO |
240 | return TRUE; | |
83df96d6 JS |
241 | } |
242 | } | |
243 | ||
1b0fb34b JS |
244 | // ---------------------------------------------------------------------------- |
245 | // wxTopLevelWindowX11 misc | |
246 | // ---------------------------------------------------------------------------- | |
83df96d6 | 247 | |
1b0fb34b | 248 | void wxTopLevelWindowX11::SetIcon(const wxIcon& icon) |
83df96d6 | 249 | { |
1b0fb34b JS |
250 | // this sets m_icon |
251 | wxTopLevelWindowBase::SetIcon(icon); | |
83df96d6 | 252 | |
b513212d JS |
253 | if (icon.Ok() && GetMainWindow()) |
254 | { | |
255 | XWMHints *wmHints = XAllocWMHints(); | |
6a44bffd | 256 | wmHints->icon_pixmap = (Pixmap) icon.GetPixmap(); |
b513212d | 257 | |
6a44bffd | 258 | wmHints->flags = IconPixmapHint; |
b513212d JS |
259 | |
260 | if (icon.GetMask()) | |
261 | { | |
6a44bffd | 262 | wmHints->flags |= IconMaskHint; |
a11672a4 | 263 | wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap(); |
b513212d JS |
264 | } |
265 | ||
1b0b798d | 266 | XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints); |
b513212d JS |
267 | XFree(wmHints); |
268 | } | |
269 | } | |
270 | ||
271 | void wxTopLevelWindowX11::SetTitle(const wxString& title) | |
272 | { | |
273 | m_title = title; | |
274 | if (GetMainWindow()) | |
275 | { | |
276 | XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(), | |
277 | (const char*) title); | |
278 | XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(), | |
279 | (const char*) title); | |
b28d3abf JS |
280 | |
281 | // Use this if the platform doesn't supply the above functions. | |
b513212d JS |
282 | #if 0 |
283 | XTextProperty textProperty; | |
284 | textProperty.value = (unsigned char*) title; | |
285 | textProperty.encoding = XA_STRING; | |
286 | textProperty.format = 8; | |
287 | textProperty.nitems = 1; | |
288 | ||
289 | XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(), | |
290 | & textProperty, WM_NAME); | |
291 | #endif | |
292 | } | |
293 | } | |
294 | ||
295 | wxString wxTopLevelWindowX11::GetTitle() const | |
296 | { | |
297 | return m_title; | |
298 | } | |
299 | ||
300 | #ifndef MWM_DECOR_BORDER | |
301 | /* bit definitions for MwmHints.flags */ | |
302 | #define MWM_HINTS_FUNCTIONS (1L << 0) | |
303 | #define MWM_HINTS_DECORATIONS (1L << 1) | |
304 | #define MWM_HINTS_INPUT_MODE (1L << 2) | |
305 | #define MWM_HINTS_STATUS (1L << 3) | |
306 | ||
307 | #define MWM_DECOR_ALL (1L << 0) | |
308 | #define MWM_DECOR_BORDER (1L << 1) | |
309 | #define MWM_DECOR_RESIZEH (1L << 2) | |
310 | #define MWM_DECOR_TITLE (1L << 3) | |
311 | #define MWM_DECOR_MENU (1L << 4) | |
312 | #define MWM_DECOR_MINIMIZE (1L << 5) | |
313 | #define MWM_DECOR_MAXIMIZE (1L << 6) | |
314 | #endif | |
315 | ||
316 | struct MwmHints { | |
317 | long flags; | |
318 | long functions; | |
319 | long decorations; | |
320 | long input_mode; | |
321 | }; | |
322 | ||
323 | #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 | |
324 | ||
325 | // Set the window manager decorations according to the | |
326 | // given wxWindows style | |
b28d3abf | 327 | bool wxSetWMDecorations(Window w, long style) |
b513212d | 328 | { |
6a44bffd | 329 | if (!wxMWMIsRunning(w)) |
b513212d JS |
330 | return FALSE; |
331 | ||
332 | Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False); | |
333 | MwmHints hints; | |
334 | hints.flags = 0; | |
335 | hints.decorations = 0; | |
336 | ||
337 | if (style & wxRESIZE_BORDER) | |
338 | { | |
339 | hints.flags |= MWM_HINTS_DECORATIONS; | |
340 | hints.decorations |= MWM_DECOR_RESIZEH; | |
341 | } | |
342 | ||
343 | if (style & wxSYSTEM_MENU) | |
344 | { | |
345 | hints.flags |= MWM_HINTS_DECORATIONS; | |
346 | hints.decorations |= MWM_DECOR_MENU; | |
347 | } | |
348 | ||
349 | if ((style & wxCAPTION) || | |
350 | (style & wxTINY_CAPTION_HORIZ) || | |
351 | (style & wxTINY_CAPTION_VERT)) | |
352 | { | |
353 | hints.flags |= MWM_HINTS_DECORATIONS; | |
354 | hints.decorations |= MWM_DECOR_TITLE; | |
355 | } | |
356 | ||
357 | if (style & wxTHICK_FRAME) | |
358 | { | |
359 | hints.flags |= MWM_HINTS_DECORATIONS; | |
360 | hints.decorations |= MWM_DECOR_BORDER; | |
361 | } | |
362 | ||
363 | if (style & wxTHICK_FRAME) | |
364 | { | |
365 | hints.flags |= MWM_HINTS_DECORATIONS; | |
366 | hints.decorations |= MWM_DECOR_BORDER; | |
367 | } | |
368 | ||
369 | if (style & wxMINIMIZE_BOX) | |
370 | { | |
371 | hints.flags |= MWM_HINTS_DECORATIONS; | |
372 | hints.decorations |= MWM_DECOR_MINIMIZE; | |
373 | } | |
374 | ||
375 | if (style & wxMAXIMIZE_BOX) | |
376 | { | |
377 | hints.flags |= MWM_HINTS_DECORATIONS; | |
378 | hints.decorations |= MWM_DECOR_MAXIMIZE; | |
379 | } | |
380 | ||
381 | XChangeProperty(wxGlobalDisplay(), | |
382 | w, | |
6a44bffd | 383 | mwm_wm_hints, mwm_wm_hints, |
b513212d JS |
384 | 32, PropModeReplace, |
385 | (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS); | |
386 | ||
387 | return TRUE; | |
388 | } | |
389 | ||
b28d3abf | 390 | bool wxMWMIsRunning(Window w) |
b513212d | 391 | { |
68e42278 | 392 | Display *dpy = (Display*)wxGetDisplay(); |
b513212d JS |
393 | Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False); |
394 | ||
395 | unsigned long length, bytesafter; | |
396 | unsigned char value[20]; | |
68e42278 RR |
397 | unsigned char *ptr = &value[0]; |
398 | int ret, format; | |
399 | Atom type; | |
b513212d JS |
400 | |
401 | type = format = length = 0; | |
68e42278 | 402 | value[0] = 0; |
b513212d JS |
403 | |
404 | ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo, | |
405 | 0L, 2, False, motifWmInfo, | |
68e42278 | 406 | &type, &format, &length, &bytesafter, &ptr); |
b513212d JS |
407 | |
408 | return (ret == Success); | |
83df96d6 | 409 | } |
6a44bffd | 410 | |
e5053ade JS |
411 | // For implementation purposes - sometimes decorations make the client area |
412 | // smaller | |
413 | wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const | |
414 | { | |
418d4918 JS |
415 | // In fact wxFrame::GetClientAreaOrigin |
416 | // does the required calculation already. | |
417 | #if 0 | |
e5053ade JS |
418 | if (this->IsKindOf(CLASSINFO(wxFrame))) |
419 | { | |
420 | wxFrame* frame = (wxFrame*) this; | |
421 | if (frame->GetMenuBar()) | |
422 | return wxPoint(0, frame->GetMenuBar()->GetSize().y); | |
423 | } | |
418d4918 | 424 | #endif |
e5053ade JS |
425 | return wxPoint(0, 0); |
426 | } | |
427 | ||
428 | void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const | |
429 | { | |
430 | wxWindowX11::DoGetClientSize(width, height); | |
418d4918 JS |
431 | // Done by wxTopLevelWindow |
432 | #if 0 | |
e5053ade JS |
433 | if (this->IsKindOf(CLASSINFO(wxFrame))) |
434 | { | |
435 | wxFrame* frame = (wxFrame*) this; | |
436 | if (frame->GetMenuBar()) | |
437 | (*height) -= frame->GetMenuBar()->GetSize().y; | |
438 | if (frame->GetStatusBar()) | |
439 | (*height) -= frame->GetStatusBar()->GetSize().y; | |
440 | } | |
418d4918 | 441 | #endif |
e5053ade JS |
442 | } |
443 | ||
444 | void wxTopLevelWindowX11::DoSetClientSize(int width, int height) | |
445 | { | |
e5053ade JS |
446 | wxWindowX11::DoSetClientSize(width, height); |
447 | #if 0 | |
448 | if (!GetMainWindow()) | |
449 | return; | |
450 | ||
451 | XWindowChanges windowChanges; | |
452 | int valueMask = 0; | |
453 | ||
454 | if (width != -1) | |
455 | { | |
456 | windowChanges.width = width ; | |
457 | valueMask |= CWWidth; | |
458 | } | |
459 | if (height != -1) | |
460 | { | |
461 | windowChanges.height = height ; | |
462 | valueMask |= CWHeight; | |
463 | } | |
464 | XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(), | |
465 | valueMask, & windowChanges); | |
466 | #endif | |
467 | } |