wxX11:
[wxWidgets.git] / src / x11 / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
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"
38 #include "wx/menu.h"
39 #include "wx/statusbr.h"
40 #endif //WX_PRECOMP
41
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
45
46 bool wxMWMIsRunning(Window w);
47
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
51
52 void wxTopLevelWindowX11::Init()
53 {
54 m_iconized =
55 m_maximizeOnShow = FALSE;
56
57 // unlike (almost?) all other windows, frames are created hidden
58 m_isShown = FALSE;
59
60 // Data to save/restore when calling ShowFullScreen
61 m_fsStyle = 0;
62 m_fsIsMaximized = FALSE;
63 m_fsIsShowing = FALSE;
64
65 m_focusWidget = NULL;
66 }
67
68 bool 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();
78
79 m_windowStyle = style;
80 m_parent = parent;
81
82 SetName(name);
83
84 m_windowId = id == -1 ? NewControlId() : id;
85
86 if (parent)
87 parent->AddChild(this);
88
89 wxTopLevelWindows.Append(this);
90
91 Display *xdisplay = wxGlobalDisplay();
92 int xscreen = DefaultScreen( xdisplay );
93 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
94 Window xparent = RootWindow( xdisplay, xscreen );
95 Colormap cm = DefaultColormap( xdisplay, xscreen );
96
97 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
98 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
99 else
100 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
101 m_backgroundColour.CalcPixel( (WXColormap) cm );
102 m_hasBgCol = TRUE;
103
104 XSetWindowAttributes xattributes;
105 XSizeHints size_hints;
106 XWMHints wm_hints;
107
108 long xattributes_mask =
109 CWOverrideRedirect |
110 CWBorderPixel | CWBackPixel;
111 xattributes.background_pixel = m_backgroundColour.GetPixel();
112 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
113
114 // TODO: if we want no border, caption etc.,
115 // I think we set this to True to remove decorations
116 // No. RR.
117 xattributes.override_redirect = False;
118
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;
130
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,
136 #if wxUSE_NANOX
137 GR_EVENT_MASK_CLOSE_REQ |
138 #endif
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
146 // Set background to None which will prevent X11 from clearing the
147 // background completely.
148 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
149
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;
162 size_hints.width = size2.x;
163 size_hints.height = size2.y;
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
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);
175
176 wxSetWMDecorations( xwindow, style);
177
178 SetTitle(title);
179
180 return TRUE;
181 }
182
183 wxTopLevelWindowX11::~wxTopLevelWindowX11()
184 {
185 wxTopLevelWindows.DeleteObject(this);
186
187 // If this is the last top-level window, exit.
188 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
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
200 // ----------------------------------------------------------------------------
201 // wxTopLevelWindowX11 showing
202 // ----------------------------------------------------------------------------
203
204 bool wxTopLevelWindowX11::Show(bool show)
205 {
206 return wxWindowX11::Show(show);
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxTopLevelWindowX11 maximize/minimize
211 // ----------------------------------------------------------------------------
212
213 void wxTopLevelWindowX11::Maximize(bool maximize)
214 {
215 // TODO
216 }
217
218 bool wxTopLevelWindowX11::IsMaximized() const
219 {
220 // TODO
221 return TRUE;
222 }
223
224 void wxTopLevelWindowX11::Iconize(bool iconize)
225 {
226 if (!m_iconized && GetMainWindow())
227 {
228 if (XIconifyWindow(wxGlobalDisplay(),
229 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
230 m_iconized = TRUE;
231 }
232 }
233
234 bool wxTopLevelWindowX11::IsIconized() const
235 {
236 return m_iconized;
237 }
238
239 void wxTopLevelWindowX11::Restore()
240 {
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 }
247 }
248
249 // ----------------------------------------------------------------------------
250 // wxTopLevelWindowX11 fullscreen
251 // ----------------------------------------------------------------------------
252
253 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
254 {
255 if (show)
256 {
257 if (IsFullScreen())
258 return FALSE;
259
260 m_fsIsShowing = TRUE;
261 m_fsStyle = style;
262
263 // TODO
264
265 return TRUE;
266 }
267 else
268 {
269 if (!IsFullScreen())
270 return FALSE;
271
272 m_fsIsShowing = FALSE;
273
274 // TODO
275 return TRUE;
276 }
277 }
278
279 // ----------------------------------------------------------------------------
280 // wxTopLevelWindowX11 misc
281 // ----------------------------------------------------------------------------
282
283 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
284 {
285 // this sets m_icon
286 wxTopLevelWindowBase::SetIcon(icon);
287
288 if (icon.Ok() && GetMainWindow())
289 {
290 XWMHints *wmHints = XAllocWMHints();
291 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
292
293 wmHints->flags = IconPixmapHint;
294
295 if (icon.GetMask())
296 {
297 wmHints->flags |= IconMaskHint;
298 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
299 }
300
301 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
302 XFree(wmHints);
303 }
304 }
305
306 void 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);
315
316 // Use this if the platform doesn't supply the above functions.
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
330 wxString 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
351 struct 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
362 bool wxSetWMDecorations(Window w, long style)
363 {
364 if (!wxMWMIsRunning(w))
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 {
374 wxLogDebug("MWM_DECOR_RESIZEH");
375 hints.flags |= MWM_HINTS_DECORATIONS;
376 hints.decorations |= MWM_DECOR_RESIZEH;
377 }
378
379 if (style & wxSYSTEM_MENU)
380 {
381 wxLogDebug("MWM_DECOR_MENU");
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 {
390 wxLogDebug("MWM_DECOR_TITLE");
391 hints.flags |= MWM_HINTS_DECORATIONS;
392 hints.decorations |= MWM_DECOR_TITLE;
393 }
394
395 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
396 {
397 wxLogDebug("MWM_DECOR_BORDER");
398 hints.flags |= MWM_HINTS_DECORATIONS;
399 hints.decorations |= MWM_DECOR_BORDER;
400 }
401
402 if (style & wxMINIMIZE_BOX)
403 {
404 wxLogDebug("MWM_DECOR_MINIMIZE");
405 hints.flags |= MWM_HINTS_DECORATIONS;
406 hints.decorations |= MWM_DECOR_MINIMIZE;
407 }
408
409 if (style & wxMAXIMIZE_BOX)
410 {
411 wxLogDebug("MWM_DECOR_MAXIMIZE");
412 hints.flags |= MWM_HINTS_DECORATIONS;
413 hints.decorations |= MWM_DECOR_MAXIMIZE;
414 }
415
416 XChangeProperty(wxGlobalDisplay(),
417 w,
418 mwm_wm_hints, mwm_wm_hints,
419 32, PropModeReplace,
420 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
421
422 return TRUE;
423 }
424
425 bool wxMWMIsRunning(Window w)
426 {
427 Display *dpy = (Display*)wxGetDisplay();
428 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
429
430 unsigned long length, bytesafter;
431 unsigned char value[20];
432 unsigned char *ptr = &value[0];
433 int ret, format;
434 Atom type;
435
436 type = format = length = 0;
437 value[0] = 0;
438
439 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
440 0L, 2, False, motifWmInfo,
441 &type, &format, &length, &bytesafter, &ptr);
442
443 return (ret == Success);
444 }
445
446 // For implementation purposes - sometimes decorations make the client area
447 // smaller
448 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
449 {
450 // In fact wxFrame::GetClientAreaOrigin
451 // does the required calculation already.
452 #if 0
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 }
459 #endif
460 return wxPoint(0, 0);
461 }
462
463 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
464 {
465 wxWindowX11::DoGetClientSize(width, height);
466 // Done by wxTopLevelWindow
467 #if 0
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 }
476 #endif
477 }
478
479 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
480 {
481 wxWindowX11::DoSetClientSize(width, height);
482
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 }
504
505 void 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
562 void 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 }