]> git.saurik.com Git - wxWidgets.git/blame - src/x11/toplevel.cpp
Always send an erase event to satisfy some users...
[wxWidgets.git] / src / x11 / toplevel.cpp
CommitLineData
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 46bool wxMWMIsRunning(Window w);
b513212d 47
83df96d6 48// ----------------------------------------------------------------------------
1b0fb34b 49// wxTopLevelWindowX11 creation
83df96d6
JS
50// ----------------------------------------------------------------------------
51
1b0fb34b
JS
52void 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;
7e4501ee
RR
64
65 m_focusWidget = NULL;
1b0fb34b 66}
83df96d6 67
1b0fb34b
JS
68bool wxTopLevelWindowX11::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxString& title,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75{
76 // init our fields
77 Init();
83df96d6
JS
78
79 m_windowStyle = style;
b28d3abf 80 m_parent = parent;
83df96d6 81
1b0fb34b 82 SetName(name);
83df96d6 83
1b0fb34b 84 m_windowId = id == -1 ? NewControlId() : id;
83df96d6 85
b28d3abf
JS
86 if (parent)
87 parent->AddChild(this);
88
1b0fb34b 89 wxTopLevelWindows.Append(this);
952ebeba
RR
90
91 Display *xdisplay = wxGlobalDisplay();
92 int xscreen = DefaultScreen( xdisplay );
93 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
94 Window xparent = RootWindow( xdisplay, xscreen );
2034b748 95 Colormap cm = DefaultColormap( xdisplay, xscreen );
952ebeba 96
7e085304
RR
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
98 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
99 else
100 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
2034b748 101 m_backgroundColour.CalcPixel( (WXColormap) cm );
56cb684a
JS
102 m_hasBgCol = TRUE;
103
952ebeba
RR
104 XSetWindowAttributes xattributes;
105 XSizeHints size_hints;
106 XWMHints wm_hints;
107
108 long xattributes_mask =
ea596687 109 CWOverrideRedirect |
952ebeba 110 CWBorderPixel | CWBackPixel;
2034b748 111 xattributes.background_pixel = m_backgroundColour.GetPixel();
952ebeba 112 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
5e29f97a
JS
113
114 // TODO: if we want no border, caption etc.,
115 // I think we set this to True to remove decorations
7e4501ee 116 // No. RR.
952ebeba
RR
117 xattributes.override_redirect = False;
118
edaf57a4
JS
119 wxSize size2(size);
120 if (size2.x == -1)
121 size2.x = 100;
122 if (size2.y == -1)
123 size2.y = 100;
124
125 wxPoint pos2(pos);
126 if (pos2.x == -1)
127 pos2.x = 100;
128 if (pos2.y == -1)
129 pos2.y = 100;
952ebeba 130
307be31a
JS
131 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
132 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
133 m_mainWidget = (WXWindow) xwindow;
134
135 XSelectInput( xdisplay, xwindow,
256d631a
JS
136#if wxUSE_NANOX
137 GR_EVENT_MASK_CLOSE_REQ |
138#endif
307be31a
JS
139 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
140 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
141 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
142 PropertyChangeMask );
143
144 wxAddWindowToTable( xwindow, (wxWindow*) this );
145
ba696cfa
RR
146 // Set background to None which will prevent X11 from clearing the
147 // background completely.
148 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
149
7e4501ee
RR
150 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
151 {
152 if (GetParent() && GetParent()->GetMainWindow())
153 {
154 Window xparentwindow = (Window) GetParent()->GetMainWindow();
155 XSetTransientForHint( xdisplay, xwindow, xparentwindow );
156 }
157 }
158
159 size_hints.flags = PSize | PPosition;
160 size_hints.x = pos2.x;
161 size_hints.y = pos2.y;
edaf57a4
JS
162 size_hints.width = size2.x;
163 size_hints.height = size2.y;
952ebeba
RR
164 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
165
166 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
167 wm_hints.input = True;
168 wm_hints.initial_state = NormalState;
169 XSetWMHints( xdisplay, xwindow, &wm_hints);
170
7e4501ee
RR
171 Atom wm_protocols[2];
172 wm_protocols[0] = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False );
173 wm_protocols[1] = XInternAtom( xdisplay, "WM_TAKE_FOCUS", False );
174 XSetWMProtocols( xdisplay, xwindow, wm_protocols, 2);
952ebeba 175
7e4501ee 176 wxSetWMDecorations( xwindow, style);
83df96d6 177
b513212d 178 SetTitle(title);
952ebeba
RR
179
180 return TRUE;
83df96d6
JS
181}
182
1b0fb34b 183wxTopLevelWindowX11::~wxTopLevelWindowX11()
83df96d6 184{
83df96d6 185 wxTopLevelWindows.DeleteObject(this);
83df96d6 186
1b0fb34b
JS
187 // If this is the last top-level window, exit.
188 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
83df96d6
JS
189 {
190 wxTheApp->SetTopWindow(NULL);
191
192 if (wxTheApp->GetExitOnFrameDelete())
193 {
194 // Signal to the app that we're going to close
195 wxTheApp->ExitMainLoop();
196 }
197 }
198}
199
1b0fb34b
JS
200// ----------------------------------------------------------------------------
201// wxTopLevelWindowX11 showing
202// ----------------------------------------------------------------------------
83df96d6 203
1b0fb34b 204bool wxTopLevelWindowX11::Show(bool show)
83df96d6 205{
b513212d 206 return wxWindowX11::Show(show);
83df96d6
JS
207}
208
1b0fb34b
JS
209// ----------------------------------------------------------------------------
210// wxTopLevelWindowX11 maximize/minimize
211// ----------------------------------------------------------------------------
83df96d6 212
1b0fb34b 213void wxTopLevelWindowX11::Maximize(bool maximize)
83df96d6 214{
1b0fb34b 215 // TODO
83df96d6
JS
216}
217
1b0fb34b 218bool wxTopLevelWindowX11::IsMaximized() const
83df96d6 219{
1b0fb34b
JS
220 // TODO
221 return TRUE;
83df96d6
JS
222}
223
1b0fb34b 224void wxTopLevelWindowX11::Iconize(bool iconize)
83df96d6 225{
b513212d
JS
226 if (!m_iconized && GetMainWindow())
227 {
228 if (XIconifyWindow(wxGlobalDisplay(),
229 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
230 m_iconized = TRUE;
231 }
83df96d6
JS
232}
233
1b0fb34b 234bool wxTopLevelWindowX11::IsIconized() const
83df96d6 235{
1b0fb34b 236 return m_iconized;
83df96d6
JS
237}
238
1b0fb34b 239void wxTopLevelWindowX11::Restore()
83df96d6 240{
b513212d
JS
241 // This is the way to deiconify the window, according to the X FAQ
242 if (m_iconized && GetMainWindow())
243 {
244 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
245 m_iconized = FALSE;
246 }
83df96d6
JS
247}
248
1b0fb34b
JS
249// ----------------------------------------------------------------------------
250// wxTopLevelWindowX11 fullscreen
251// ----------------------------------------------------------------------------
83df96d6 252
1b0fb34b 253bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
83df96d6 254{
1b0fb34b 255 if (show)
83df96d6 256 {
1b0fb34b
JS
257 if (IsFullScreen())
258 return FALSE;
83df96d6 259
1b0fb34b
JS
260 m_fsIsShowing = TRUE;
261 m_fsStyle = style;
83df96d6 262
1b0fb34b 263 // TODO
83df96d6 264
1b0fb34b 265 return TRUE;
83df96d6 266 }
1b0fb34b 267 else
83df96d6 268 {
1b0fb34b
JS
269 if (!IsFullScreen())
270 return FALSE;
83df96d6 271
1b0fb34b 272 m_fsIsShowing = FALSE;
83df96d6 273
1b0fb34b
JS
274 // TODO
275 return TRUE;
83df96d6
JS
276 }
277}
278
1b0fb34b
JS
279// ----------------------------------------------------------------------------
280// wxTopLevelWindowX11 misc
281// ----------------------------------------------------------------------------
83df96d6 282
1b0fb34b 283void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
83df96d6 284{
1b0fb34b
JS
285 // this sets m_icon
286 wxTopLevelWindowBase::SetIcon(icon);
83df96d6 287
b513212d
JS
288 if (icon.Ok() && GetMainWindow())
289 {
290 XWMHints *wmHints = XAllocWMHints();
6a44bffd 291 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
b513212d 292
6a44bffd 293 wmHints->flags = IconPixmapHint;
b513212d
JS
294
295 if (icon.GetMask())
296 {
6a44bffd 297 wmHints->flags |= IconMaskHint;
a11672a4 298 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
b513212d
JS
299 }
300
1b0b798d 301 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
b513212d
JS
302 XFree(wmHints);
303 }
304}
305
306void wxTopLevelWindowX11::SetTitle(const wxString& title)
307{
308 m_title = title;
309 if (GetMainWindow())
310 {
311 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
312 (const char*) title);
313 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
314 (const char*) title);
b28d3abf
JS
315
316 // Use this if the platform doesn't supply the above functions.
b513212d
JS
317#if 0
318 XTextProperty textProperty;
319 textProperty.value = (unsigned char*) title;
320 textProperty.encoding = XA_STRING;
321 textProperty.format = 8;
322 textProperty.nitems = 1;
323
324 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
325 & textProperty, WM_NAME);
326#endif
327 }
328}
329
330wxString wxTopLevelWindowX11::GetTitle() const
331{
332 return m_title;
333}
334
335#ifndef MWM_DECOR_BORDER
336/* bit definitions for MwmHints.flags */
337#define MWM_HINTS_FUNCTIONS (1L << 0)
338#define MWM_HINTS_DECORATIONS (1L << 1)
339#define MWM_HINTS_INPUT_MODE (1L << 2)
340#define MWM_HINTS_STATUS (1L << 3)
341
342#define MWM_DECOR_ALL (1L << 0)
343#define MWM_DECOR_BORDER (1L << 1)
344#define MWM_DECOR_RESIZEH (1L << 2)
345#define MWM_DECOR_TITLE (1L << 3)
346#define MWM_DECOR_MENU (1L << 4)
347#define MWM_DECOR_MINIMIZE (1L << 5)
348#define MWM_DECOR_MAXIMIZE (1L << 6)
349#endif
350
351struct MwmHints {
352 long flags;
353 long functions;
354 long decorations;
355 long input_mode;
356};
357
358#define PROP_MOTIF_WM_HINTS_ELEMENTS 5
359
360// Set the window manager decorations according to the
361// given wxWindows style
b28d3abf 362bool wxSetWMDecorations(Window w, long style)
b513212d 363{
6a44bffd 364 if (!wxMWMIsRunning(w))
b513212d
JS
365 return FALSE;
366
367 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
368 MwmHints hints;
369 hints.flags = 0;
370 hints.decorations = 0;
371
372 if (style & wxRESIZE_BORDER)
373 {
edaf57a4 374 wxLogDebug("MWM_DECOR_RESIZEH");
b513212d
JS
375 hints.flags |= MWM_HINTS_DECORATIONS;
376 hints.decorations |= MWM_DECOR_RESIZEH;
377 }
378
379 if (style & wxSYSTEM_MENU)
380 {
edaf57a4 381 wxLogDebug("MWM_DECOR_MENU");
b513212d
JS
382 hints.flags |= MWM_HINTS_DECORATIONS;
383 hints.decorations |= MWM_DECOR_MENU;
384 }
385
386 if ((style & wxCAPTION) ||
387 (style & wxTINY_CAPTION_HORIZ) ||
388 (style & wxTINY_CAPTION_VERT))
389 {
edaf57a4 390 wxLogDebug("MWM_DECOR_TITLE");
b513212d
JS
391 hints.flags |= MWM_HINTS_DECORATIONS;
392 hints.decorations |= MWM_DECOR_TITLE;
393 }
394
307be31a 395 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
b513212d 396 {
edaf57a4 397 wxLogDebug("MWM_DECOR_BORDER");
b513212d
JS
398 hints.flags |= MWM_HINTS_DECORATIONS;
399 hints.decorations |= MWM_DECOR_BORDER;
400 }
401
402 if (style & wxMINIMIZE_BOX)
403 {
edaf57a4 404 wxLogDebug("MWM_DECOR_MINIMIZE");
b513212d
JS
405 hints.flags |= MWM_HINTS_DECORATIONS;
406 hints.decorations |= MWM_DECOR_MINIMIZE;
407 }
408
409 if (style & wxMAXIMIZE_BOX)
410 {
edaf57a4 411 wxLogDebug("MWM_DECOR_MAXIMIZE");
b513212d
JS
412 hints.flags |= MWM_HINTS_DECORATIONS;
413 hints.decorations |= MWM_DECOR_MAXIMIZE;
414 }
415
416 XChangeProperty(wxGlobalDisplay(),
417 w,
6a44bffd 418 mwm_wm_hints, mwm_wm_hints,
b513212d
JS
419 32, PropModeReplace,
420 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
421
422 return TRUE;
423}
424
b28d3abf 425bool wxMWMIsRunning(Window w)
b513212d 426{
68e42278 427 Display *dpy = (Display*)wxGetDisplay();
b513212d
JS
428 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
429
430 unsigned long length, bytesafter;
431 unsigned char value[20];
68e42278
RR
432 unsigned char *ptr = &value[0];
433 int ret, format;
434 Atom type;
b513212d
JS
435
436 type = format = length = 0;
68e42278 437 value[0] = 0;
b513212d
JS
438
439 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
440 0L, 2, False, motifWmInfo,
68e42278 441 &type, &format, &length, &bytesafter, &ptr);
b513212d
JS
442
443 return (ret == Success);
83df96d6 444}
6a44bffd 445
e5053ade
JS
446// For implementation purposes - sometimes decorations make the client area
447// smaller
448wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
449{
418d4918
JS
450 // In fact wxFrame::GetClientAreaOrigin
451 // does the required calculation already.
452#if 0
e5053ade
JS
453 if (this->IsKindOf(CLASSINFO(wxFrame)))
454 {
455 wxFrame* frame = (wxFrame*) this;
456 if (frame->GetMenuBar())
457 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
458 }
418d4918 459#endif
e5053ade
JS
460 return wxPoint(0, 0);
461}
462
463void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
464{
465 wxWindowX11::DoGetClientSize(width, height);
418d4918
JS
466 // Done by wxTopLevelWindow
467#if 0
e5053ade
JS
468 if (this->IsKindOf(CLASSINFO(wxFrame)))
469 {
470 wxFrame* frame = (wxFrame*) this;
471 if (frame->GetMenuBar())
472 (*height) -= frame->GetMenuBar()->GetSize().y;
473 if (frame->GetStatusBar())
474 (*height) -= frame->GetStatusBar()->GetSize().y;
475 }
418d4918 476#endif
e5053ade
JS
477}
478
479void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
480{
e5053ade 481 wxWindowX11::DoSetClientSize(width, height);
3a0b23eb 482
e5053ade
JS
483#if 0
484 if (!GetMainWindow())
485 return;
486
487 XWindowChanges windowChanges;
488 int valueMask = 0;
489
490 if (width != -1)
491 {
492 windowChanges.width = width ;
493 valueMask |= CWWidth;
494 }
495 if (height != -1)
496 {
497 windowChanges.height = height ;
498 valueMask |= CWHeight;
499 }
500 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
501 valueMask, & windowChanges);
502#endif
503}
3a0b23eb
JS
504
505void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
506{
507 wxString msg;
508 msg.Printf("Setting pos: %d, %d", x, y);
509 wxLogDebug(msg);
510 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
511
512 wxPoint pt = GetPosition();
513 msg.Printf("After, pos: %d, %d", pt.x, pt.y);
514 wxLogDebug(msg);
515#if 0
516 XSync(wxGlobalDisplay(), False);
517 int w, h;
518 GetSize(& w, & h);
519 wxString msg;
520 msg.Printf("Before setting size: %d, %d", w, h);
521 wxLogDebug(msg);
522 if (!GetMainWindow())
523 return;
524
525 XWindowChanges windowChanges;
526 int valueMask = 0;
527
528 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
529 {
530 int yy = 0;
531 AdjustForParentClientOrigin( x, yy, sizeFlags);
532 windowChanges.x = x;
533 valueMask |= CWX;
534 }
535 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
536 {
537 int xx = 0;
538 AdjustForParentClientOrigin( xx, y, sizeFlags);
539 windowChanges.y = y;
540 valueMask |= CWY;
541 }
542 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
543 {
544 windowChanges.width = width /* - m_borderSize*2 */;
545 valueMask |= CWWidth;
546 }
547 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
548 {
549 windowChanges.height = height /* -m_borderSize*2*/;
550 valueMask |= CWHeight;
551 }
552
553 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
554 valueMask, & windowChanges);
555 XSync(wxGlobalDisplay(), False);
556 GetSize(& w, & h);
557 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
558 wxLogDebug(msg);
559#endif
560}
561
562void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
563{
564 XSync(wxGlobalDisplay(), False);
565 Window window = (Window) m_mainWidget;
566 if (window)
567 {
568 int offsetX = 0;
569 int offsetY = 0;
570
571 wxLogDebug("Translating...");
572 Window childWindow;
573 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
574 0, 0, & offsetX, & offsetY, & childWindow);
575
576 wxString msg;
577 msg.Printf("Offset: %d, %d", offsetX, offsetY);
578 wxLogDebug(msg);
579
580 XWindowAttributes attr;
581 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
582 wxASSERT(status);
583
584 if (status)
585 {
586 *x = attr.x + offsetX;
587 *y = attr.y + offsetY;
588 }
589 }
590}