]> git.saurik.com Git - wxWidgets.git/blob - src/x11/toplevel.cpp
more GTK-ish toolbar drawing
[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 wxSize size2(size);
105 if (size2.x == -1)
106 size2.x = 100;
107 if (size2.y == -1)
108 size2.y = 100;
109
110 wxPoint pos2(pos);
111 if (pos2.x == -1)
112 pos2.x = 100;
113 if (pos2.y == -1)
114 pos2.y = 100;
115
116 #if !wxUSE_NANOX
117 XSetWindowAttributes xattributes;
118 XSizeHints size_hints;
119
120 long xattributes_mask =
121 CWOverrideRedirect |
122 CWBorderPixel | CWBackPixel;
123
124 xattributes.background_pixel = m_backgroundColour.GetPixel();
125 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
126
127 // TODO: if we want no border, caption etc.,
128 // I think we set this to True to remove decorations
129 // No. RR.
130 xattributes.override_redirect = False;
131 #endif
132
133 #if wxUSE_NANOX
134 long backColor, foreColor;
135 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
136 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
137
138 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
139 0, 0, InputOutput, xvisual, backColor, foreColor);
140 #else
141 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
142 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
143 #endif
144 m_mainWidget = (WXWindow) xwindow;
145
146 int extraFlags = 0;
147 #if wxUSE_NANOX
148 extraFlags |= GR_EVENT_MASK_CLOSE_REQ;
149 #endif
150
151 XSelectInput( xdisplay, xwindow,
152 extraFlags |
153 ExposureMask |
154 KeyPressMask |
155 KeyReleaseMask |
156 ButtonPressMask |
157 ButtonReleaseMask |
158 ButtonMotionMask |
159 EnterWindowMask |
160 LeaveWindowMask |
161 PointerMotionMask |
162 KeymapStateMask |
163 FocusChangeMask |
164 ColormapChangeMask |
165 StructureNotifyMask |
166 PropertyChangeMask
167 );
168
169 wxAddWindowToTable( xwindow, (wxWindow*) this );
170
171 // Set background to None which will prevent X11 from clearing the
172 // background completely.
173 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
174
175 #if !wxUSE_NANOX
176 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG)
177 {
178 if (GetParent() && GetParent()->GetMainWindow())
179 {
180 Window xparentwindow = (Window) GetParent()->GetMainWindow();
181 XSetTransientForHint( xdisplay, xwindow, xparentwindow );
182 }
183 }
184
185 size_hints.flags = PSize | PPosition;
186 size_hints.x = pos2.x;
187 size_hints.y = pos2.y;
188 size_hints.width = size2.x;
189 size_hints.height = size2.y;
190 XSetWMNormalHints( xdisplay, xwindow, &size_hints);
191
192 XWMHints wm_hints;
193 wm_hints.flags = InputHint | StateHint /* | WindowGroupHint */;
194 wm_hints.input = True;
195 wm_hints.initial_state = NormalState;
196 XSetWMHints( xdisplay, xwindow, &wm_hints);
197
198 Atom wm_protocols[2];
199 wm_protocols[0] = XInternAtom( xdisplay, "WM_DELETE_WINDOW", False );
200 wm_protocols[1] = XInternAtom( xdisplay, "WM_TAKE_FOCUS", False );
201 XSetWMProtocols( xdisplay, xwindow, wm_protocols, 2);
202 #endif
203
204 wxSetWMDecorations( xwindow, style);
205
206 SetTitle(title);
207
208 return TRUE;
209 }
210
211 wxTopLevelWindowX11::~wxTopLevelWindowX11()
212 {
213 wxTopLevelWindows.DeleteObject(this);
214
215 // If this is the last top-level window, exit.
216 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
217 {
218 wxTheApp->SetTopWindow(NULL);
219
220 if (wxTheApp->GetExitOnFrameDelete())
221 {
222 // Signal to the app that we're going to close
223 wxTheApp->ExitMainLoop();
224 }
225 }
226 }
227
228 // ----------------------------------------------------------------------------
229 // wxTopLevelWindowX11 showing
230 // ----------------------------------------------------------------------------
231
232 bool wxTopLevelWindowX11::Show(bool show)
233 {
234 // Nano-X has to force a size event,
235 // else there's no initial size.
236 #if wxUSE_NANOX
237 if (show)
238 {
239 wxSizeEvent event(GetSize(), GetId());
240 event.SetEventObject(this);
241 GetEventHandler()->ProcessEvent(event);
242 }
243 #endif
244
245 return wxWindowX11::Show(show);
246 }
247
248 // ----------------------------------------------------------------------------
249 // wxTopLevelWindowX11 maximize/minimize
250 // ----------------------------------------------------------------------------
251
252 void wxTopLevelWindowX11::Maximize(bool maximize)
253 {
254 // TODO
255 }
256
257 bool wxTopLevelWindowX11::IsMaximized() const
258 {
259 // TODO
260 return TRUE;
261 }
262
263 void wxTopLevelWindowX11::Iconize(bool iconize)
264 {
265 if (!m_iconized && GetMainWindow())
266 {
267 if (XIconifyWindow(wxGlobalDisplay(),
268 (Window) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
269 m_iconized = TRUE;
270 }
271 }
272
273 bool wxTopLevelWindowX11::IsIconized() const
274 {
275 return m_iconized;
276 }
277
278 void wxTopLevelWindowX11::Restore()
279 {
280 // This is the way to deiconify the window, according to the X FAQ
281 if (m_iconized && GetMainWindow())
282 {
283 XMapWindow(wxGlobalDisplay(), (Window) GetMainWindow());
284 m_iconized = FALSE;
285 }
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxTopLevelWindowX11 fullscreen
290 // ----------------------------------------------------------------------------
291
292 bool wxTopLevelWindowX11::ShowFullScreen(bool show, long style)
293 {
294 if (show)
295 {
296 if (IsFullScreen())
297 return FALSE;
298
299 m_fsIsShowing = TRUE;
300 m_fsStyle = style;
301
302 // TODO
303
304 return TRUE;
305 }
306 else
307 {
308 if (!IsFullScreen())
309 return FALSE;
310
311 m_fsIsShowing = FALSE;
312
313 // TODO
314 return TRUE;
315 }
316 }
317
318 // ----------------------------------------------------------------------------
319 // wxTopLevelWindowX11 misc
320 // ----------------------------------------------------------------------------
321
322 void wxTopLevelWindowX11::SetIcon(const wxIcon& icon)
323 {
324 // this sets m_icon
325 wxTopLevelWindowBase::SetIcon(icon);
326
327 if (icon.Ok() && GetMainWindow())
328 {
329 #if wxUSE_NANOX
330 #else
331 XWMHints *wmHints = XAllocWMHints();
332 wmHints->icon_pixmap = (Pixmap) icon.GetPixmap();
333
334 wmHints->flags = IconPixmapHint;
335
336 if (icon.GetMask())
337 {
338 wmHints->flags |= IconMaskHint;
339 wmHints->icon_mask = (Pixmap) icon.GetMask()->GetBitmap();
340 }
341
342 XSetWMHints(wxGlobalDisplay(), (Window) GetMainWindow(), wmHints);
343 XFree(wmHints);
344 #endif
345 }
346 }
347
348 void wxTopLevelWindowX11::SetTitle(const wxString& title)
349 {
350 m_title = title;
351 if (GetMainWindow())
352 {
353 XStoreName(wxGlobalDisplay(), (Window) GetMainWindow(),
354 (const char*) title);
355 XSetIconName(wxGlobalDisplay(), (Window) GetMainWindow(),
356 (const char*) title);
357
358 // Use this if the platform doesn't supply the above functions.
359 #if 0
360 XTextProperty textProperty;
361 textProperty.value = (unsigned char*) title;
362 textProperty.encoding = XA_STRING;
363 textProperty.format = 8;
364 textProperty.nitems = 1;
365
366 XSetTextProperty(wxGlobalDisplay(), (Window) GetMainWindow(),
367 & textProperty, WM_NAME);
368 #endif
369 }
370 }
371
372 wxString wxTopLevelWindowX11::GetTitle() const
373 {
374 return m_title;
375 }
376
377 #ifndef MWM_DECOR_BORDER
378 /* bit definitions for MwmHints.flags */
379 #define MWM_HINTS_FUNCTIONS (1L << 0)
380 #define MWM_HINTS_DECORATIONS (1L << 1)
381 #define MWM_HINTS_INPUT_MODE (1L << 2)
382 #define MWM_HINTS_STATUS (1L << 3)
383
384 #define MWM_DECOR_ALL (1L << 0)
385 #define MWM_DECOR_BORDER (1L << 1)
386 #define MWM_DECOR_RESIZEH (1L << 2)
387 #define MWM_DECOR_TITLE (1L << 3)
388 #define MWM_DECOR_MENU (1L << 4)
389 #define MWM_DECOR_MINIMIZE (1L << 5)
390 #define MWM_DECOR_MAXIMIZE (1L << 6)
391 #endif
392
393 struct MwmHints {
394 long flags;
395 long functions;
396 long decorations;
397 long input_mode;
398 };
399
400 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
401
402 // Set the window manager decorations according to the
403 // given wxWindows style
404 bool wxSetWMDecorations(Window w, long style)
405 {
406 #if wxUSE_NANOX
407 GR_WM_PROPERTIES wmProp;
408
409 wmProp.flags = 0;
410 wmProp.props = 0;
411
412 if (style & wxRESIZE_BORDER)
413 {
414 wmProp.props |= GR_WM_PROPS_APPFRAME ;
415 wmProp.flags |= GR_WM_FLAGS_PROPS ;
416 }
417
418 if (style & wxSYSTEM_MENU)
419 {
420 wmProp.props |= GR_WM_PROPS_CLOSEBOX ;
421 wmProp.flags |= GR_WM_FLAGS_PROPS ;
422 }
423
424 if ((style & wxCAPTION) ||
425 (style & wxTINY_CAPTION_HORIZ) ||
426 (style & wxTINY_CAPTION_VERT))
427 {
428 wmProp.props |= GR_WM_PROPS_CAPTION ;
429 wmProp.flags |= GR_WM_FLAGS_PROPS ;
430
431 // The default dialog style doesn't include any kind
432 // of border, which is a bit odd. Anyway, inclusion
433 // of a caption surely implies a border.
434 style |= wxTHICK_FRAME;
435 }
436
437 if (style & wxTHICK_FRAME)
438 {
439 wmProp.props |= GR_WM_PROPS_APPFRAME ;
440 wmProp.flags |= GR_WM_FLAGS_PROPS ;
441 }
442
443 if (style & wxSIMPLE_BORDER)
444 {
445 wmProp.props |= GR_WM_PROPS_BORDER ;
446 wmProp.flags |= GR_WM_FLAGS_PROPS ;
447 }
448
449 if (style & wxMINIMIZE_BOX)
450 {
451 }
452
453 if (style & wxMAXIMIZE_BOX)
454 {
455 wmProp.props |= GR_WM_PROPS_MAXIMIZE ;
456 wmProp.flags |= GR_WM_FLAGS_PROPS ;
457 }
458
459 if (((style & wxBORDER) != wxBORDER) && ((style & wxTHICK_FRAME) != wxTHICK_FRAME)
460 && ((style & wxRESIZE_BORDER) != wxRESIZE_BORDER))
461 {
462 wmProp.props |= GR_WM_PROPS_NODECORATE ;
463 wmProp.flags |= GR_WM_FLAGS_PROPS ;
464 }
465
466 GrSetWMProperties(w, & wmProp);
467
468 #else
469 if (!wxMWMIsRunning(w))
470 return FALSE;
471
472 Atom mwm_wm_hints = XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False);
473 MwmHints hints;
474 hints.flags = 0;
475 hints.decorations = 0;
476
477 if (style & wxRESIZE_BORDER)
478 {
479 // wxLogDebug("MWM_DECOR_RESIZEH");
480 hints.flags |= MWM_HINTS_DECORATIONS;
481 hints.decorations |= MWM_DECOR_RESIZEH;
482 }
483
484 if (style & wxSYSTEM_MENU)
485 {
486 // wxLogDebug("MWM_DECOR_MENU");
487 hints.flags |= MWM_HINTS_DECORATIONS;
488 hints.decorations |= MWM_DECOR_MENU;
489 }
490
491 if ((style & wxCAPTION) ||
492 (style & wxTINY_CAPTION_HORIZ) ||
493 (style & wxTINY_CAPTION_VERT))
494 {
495 // wxLogDebug("MWM_DECOR_TITLE");
496 hints.flags |= MWM_HINTS_DECORATIONS;
497 hints.decorations |= MWM_DECOR_TITLE;
498 }
499
500 if ((style & wxTHICK_FRAME) || (style & wxSIMPLE_BORDER) || (style & wxCAPTION))
501 {
502 // wxLogDebug("MWM_DECOR_BORDER");
503 hints.flags |= MWM_HINTS_DECORATIONS;
504 hints.decorations |= MWM_DECOR_BORDER;
505 }
506
507 if (style & wxMINIMIZE_BOX)
508 {
509 // wxLogDebug("MWM_DECOR_MINIMIZE");
510 hints.flags |= MWM_HINTS_DECORATIONS;
511 hints.decorations |= MWM_DECOR_MINIMIZE;
512 }
513
514 if (style & wxMAXIMIZE_BOX)
515 {
516 // wxLogDebug("MWM_DECOR_MAXIMIZE");
517 hints.flags |= MWM_HINTS_DECORATIONS;
518 hints.decorations |= MWM_DECOR_MAXIMIZE;
519 }
520
521 XChangeProperty(wxGlobalDisplay(),
522 w,
523 mwm_wm_hints, mwm_wm_hints,
524 32, PropModeReplace,
525 (unsigned char *) &hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
526
527 #endif
528 return TRUE;
529 }
530
531 bool wxMWMIsRunning(Window w)
532 {
533 #if wxUSE_NANOX
534 return FALSE;
535 #else
536 Display *dpy = (Display*)wxGetDisplay();
537 Atom motifWmInfo = XInternAtom(dpy, "_MOTIF_WM_INFO", False);
538
539 unsigned long length, bytesafter;
540 unsigned char value[20];
541 unsigned char *ptr = &value[0];
542 int ret, format;
543 Atom type;
544
545 type = format = length = 0;
546 value[0] = 0;
547
548 ret = XGetWindowProperty(wxGlobalDisplay(), w, motifWmInfo,
549 0L, 2, False, motifWmInfo,
550 &type, &format, &length, &bytesafter, &ptr);
551
552 return (ret == Success);
553 #endif
554 }
555
556 // For implementation purposes - sometimes decorations make the client area
557 // smaller
558 wxPoint wxTopLevelWindowX11::GetClientAreaOrigin() const
559 {
560 // In fact wxFrame::GetClientAreaOrigin
561 // does the required calculation already.
562 #if 0
563 if (this->IsKindOf(CLASSINFO(wxFrame)))
564 {
565 wxFrame* frame = (wxFrame*) this;
566 if (frame->GetMenuBar())
567 return wxPoint(0, frame->GetMenuBar()->GetSize().y);
568 }
569 #endif
570 return wxPoint(0, 0);
571 }
572
573 void wxTopLevelWindowX11::DoGetClientSize( int *width, int *height ) const
574 {
575 wxWindowX11::DoGetClientSize(width, height);
576 // Done by wxTopLevelWindow
577 #if 0
578 if (this->IsKindOf(CLASSINFO(wxFrame)))
579 {
580 wxFrame* frame = (wxFrame*) this;
581 if (frame->GetMenuBar())
582 (*height) -= frame->GetMenuBar()->GetSize().y;
583 if (frame->GetStatusBar())
584 (*height) -= frame->GetStatusBar()->GetSize().y;
585 }
586 #endif
587 }
588
589 void wxTopLevelWindowX11::DoSetClientSize(int width, int height)
590 {
591 wxWindowX11::DoSetClientSize(width, height);
592
593 #if 0
594 if (!GetMainWindow())
595 return;
596
597 XWindowChanges windowChanges;
598 int valueMask = 0;
599
600 if (width != -1)
601 {
602 windowChanges.width = width ;
603 valueMask |= CWWidth;
604 }
605 if (height != -1)
606 {
607 windowChanges.height = height ;
608 valueMask |= CWHeight;
609 }
610 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
611 valueMask, & windowChanges);
612 #endif
613 }
614
615 void wxTopLevelWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
616 {
617 // wxLogDebug( "Setting pos: %d, %d", x, y );
618 wxWindowX11::DoSetSize(x, y, width, height, sizeFlags);
619
620 #if 0
621 wxPoint pt = GetPosition();
622 // wxLogDebug( "After, pos: %d, %d", pt.x, pt.y );
623
624 XSync(wxGlobalDisplay(), False);
625 int w, h;
626 GetSize(& w, & h);
627 wxString msg;
628 msg.Printf("Before setting size: %d, %d", w, h);
629 wxLogDebug(msg);
630 if (!GetMainWindow())
631 return;
632
633 XWindowChanges windowChanges;
634 int valueMask = 0;
635
636 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
637 {
638 int yy = 0;
639 AdjustForParentClientOrigin( x, yy, sizeFlags);
640 windowChanges.x = x;
641 valueMask |= CWX;
642 }
643 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
644 {
645 int xx = 0;
646 AdjustForParentClientOrigin( xx, y, sizeFlags);
647 windowChanges.y = y;
648 valueMask |= CWY;
649 }
650 if (width != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
651 {
652 windowChanges.width = width /* - m_borderSize*2 */;
653 valueMask |= CWWidth;
654 }
655 if (height != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
656 {
657 windowChanges.height = height /* -m_borderSize*2*/;
658 valueMask |= CWHeight;
659 }
660
661 XConfigureWindow(wxGlobalDisplay(), (Window) GetMainWindow(),
662 valueMask, & windowChanges);
663 XSync(wxGlobalDisplay(), False);
664 GetSize(& w, & h);
665 msg.Printf("Tried to set to %d, %d. After setting size: %d, %d", width, height, w, h);
666 wxLogDebug(msg);
667 #endif
668 }
669
670 void wxTopLevelWindowX11::DoGetPosition(int *x, int *y) const
671 {
672 XSync(wxGlobalDisplay(), False);
673 Window window = (Window) m_mainWidget;
674 if (window)
675 {
676 int offsetX = 0;
677 int offsetY = 0;
678
679 #if !wxUSE_NANOX
680 // wxLogDebug("Translating...");
681 Window childWindow;
682 XTranslateCoordinates(wxGlobalDisplay(), window, XDefaultRootWindow(wxGlobalDisplay()),
683 0, 0, & offsetX, & offsetY, & childWindow);
684
685 // wxLogDebug("Offset: %d, %d", offsetX, offsetY);
686 #endif
687
688 XWindowAttributes attr;
689 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
690 wxASSERT(status);
691
692 if (status)
693 {
694 *x = attr.x + offsetX;
695 *y = attr.y + offsetY;
696 }
697 }
698 }