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