1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for X11
4 // Author: Julian Smart
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toplevel.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
39 #include "wx/statusbr.h"
42 #include "wx/settings.h"
43 #include "wx/x11/private.h"
44 #include "X11/Xutil.h"
46 bool wxMWMIsRunning(Window w
);
48 // ----------------------------------------------------------------------------
49 // wxTopLevelWindowX11 creation
50 // ----------------------------------------------------------------------------
52 void wxTopLevelWindowX11::Init()
55 m_maximizeOnShow
= FALSE
;
57 // unlike (almost?) all other windows, frames are created hidden
60 // Data to save/restore when calling ShowFullScreen
62 m_fsIsMaximized
= FALSE
;
63 m_fsIsShowing
= FALSE
;
66 bool wxTopLevelWindowX11::Create(wxWindow
*parent
,
68 const wxString
& title
,
77 m_windowStyle
= style
;
82 m_windowId
= id
== -1 ? NewControlId() : id
;
85 parent
->AddChild(this);
87 wxTopLevelWindows
.Append(this);
89 Display
*xdisplay
= wxGlobalDisplay();
90 int xscreen
= DefaultScreen( xdisplay
);
91 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
92 Window xparent
= RootWindow( xdisplay
, xscreen
);
93 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
95 if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
)
96 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
98 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
99 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
102 XSetWindowAttributes xattributes
;
103 XSizeHints size_hints
;
106 long xattributes_mask
=
108 CWBorderPixel
| CWBackPixel
;
109 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
110 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
112 // TODO: if we want no border, caption etc.,
113 // I think we set this to True to remove decorations
114 xattributes
.override_redirect
= False
;
128 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
129 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
130 m_mainWidget
= (WXWindow
) xwindow
;
132 XSelectInput( xdisplay
, xwindow
,
134 GR_EVENT_MASK_CLOSE_REQ
|
136 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
137 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
138 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
139 PropertyChangeMask
);
141 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
143 // Set background to None which will prevent X11 from clearing the
144 // background completely.
145 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
147 // Messes up window management
148 // XSetTransientForHint( xdisplay, xwindow, xparent );
150 size_hints
.flags
= PSize
;
151 size_hints
.width
= size2
.x
;
152 size_hints
.height
= size2
.y
;
153 XSetWMNormalHints( xdisplay
, xwindow
, &size_hints
);
155 wm_hints
.flags
= InputHint
| StateHint
/* | WindowGroupHint */;
156 wm_hints
.input
= True
;
157 wm_hints
.initial_state
= NormalState
;
158 XSetWMHints( xdisplay
, xwindow
, &wm_hints
);
160 Atom wm_delete_window
= XInternAtom( xdisplay
, "WM_DELETE_WINDOW", False
);
161 XSetWMProtocols( xdisplay
, xwindow
, &wm_delete_window
, 1);
163 wxSetWMDecorations((Window
) GetMainWindow(), style
);
170 wxTopLevelWindowX11::~wxTopLevelWindowX11()
172 wxTopLevelWindows
.DeleteObject(this);
174 // If this is the last top-level window, exit.
175 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
177 wxTheApp
->SetTopWindow(NULL
);
179 if (wxTheApp
->GetExitOnFrameDelete())
181 // Signal to the app that we're going to close
182 wxTheApp
->ExitMainLoop();
187 // ----------------------------------------------------------------------------
188 // wxTopLevelWindowX11 showing
189 // ----------------------------------------------------------------------------
191 bool wxTopLevelWindowX11::Show(bool show
)
193 return wxWindowX11::Show(show
);
196 // ----------------------------------------------------------------------------
197 // wxTopLevelWindowX11 maximize/minimize
198 // ----------------------------------------------------------------------------
200 void wxTopLevelWindowX11::Maximize(bool maximize
)
205 bool wxTopLevelWindowX11::IsMaximized() const
211 void wxTopLevelWindowX11::Iconize(bool iconize
)
213 if (!m_iconized
&& GetMainWindow())
215 if (XIconifyWindow(wxGlobalDisplay(),
216 (Window
) GetMainWindow(), DefaultScreen(wxGlobalDisplay())) != 0)
221 bool wxTopLevelWindowX11::IsIconized() const
226 void wxTopLevelWindowX11::Restore()
228 // This is the way to deiconify the window, according to the X FAQ
229 if (m_iconized
&& GetMainWindow())
231 XMapWindow(wxGlobalDisplay(), (Window
) GetMainWindow());
236 // ----------------------------------------------------------------------------
237 // wxTopLevelWindowX11 fullscreen
238 // ----------------------------------------------------------------------------
240 bool wxTopLevelWindowX11::ShowFullScreen(bool show
, long style
)
247 m_fsIsShowing
= TRUE
;
259 m_fsIsShowing
= FALSE
;
266 // ----------------------------------------------------------------------------
267 // wxTopLevelWindowX11 misc
268 // ----------------------------------------------------------------------------
270 void wxTopLevelWindowX11::SetIcon(const wxIcon
& icon
)
273 wxTopLevelWindowBase::SetIcon(icon
);
275 if (icon
.Ok() && GetMainWindow())
277 XWMHints
*wmHints
= XAllocWMHints();
278 wmHints
->icon_pixmap
= (Pixmap
) icon
.GetPixmap();
280 wmHints
->flags
= IconPixmapHint
;
284 wmHints
->flags
|= IconMaskHint
;
285 wmHints
->icon_mask
= (Pixmap
) icon
.GetMask()->GetBitmap();
288 XSetWMHints(wxGlobalDisplay(), (Window
) GetMainWindow(), wmHints
);
293 void wxTopLevelWindowX11::SetTitle(const wxString
& title
)
298 XStoreName(wxGlobalDisplay(), (Window
) GetMainWindow(),
299 (const char*) title
);
300 XSetIconName(wxGlobalDisplay(), (Window
) GetMainWindow(),
301 (const char*) title
);
303 // Use this if the platform doesn't supply the above functions.
305 XTextProperty textProperty
;
306 textProperty
.value
= (unsigned char*) title
;
307 textProperty
.encoding
= XA_STRING
;
308 textProperty
.format
= 8;
309 textProperty
.nitems
= 1;
311 XSetTextProperty(wxGlobalDisplay(), (Window
) GetMainWindow(),
312 & textProperty
, WM_NAME
);
317 wxString
wxTopLevelWindowX11::GetTitle() const
322 #ifndef MWM_DECOR_BORDER
323 /* bit definitions for MwmHints.flags */
324 #define MWM_HINTS_FUNCTIONS (1L << 0)
325 #define MWM_HINTS_DECORATIONS (1L << 1)
326 #define MWM_HINTS_INPUT_MODE (1L << 2)
327 #define MWM_HINTS_STATUS (1L << 3)
329 #define MWM_DECOR_ALL (1L << 0)
330 #define MWM_DECOR_BORDER (1L << 1)
331 #define MWM_DECOR_RESIZEH (1L << 2)
332 #define MWM_DECOR_TITLE (1L << 3)
333 #define MWM_DECOR_MENU (1L << 4)
334 #define MWM_DECOR_MINIMIZE (1L << 5)
335 #define MWM_DECOR_MAXIMIZE (1L << 6)
345 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
347 // Set the window manager decorations according to the
348 // given wxWindows style
349 bool wxSetWMDecorations(Window w
, long style
)
351 if (!wxMWMIsRunning(w
))
354 Atom mwm_wm_hints
= XInternAtom(wxGlobalDisplay(),"_MOTIF_WM_HINTS", False
);
357 hints
.decorations
= 0;
359 if (style
& wxRESIZE_BORDER
)
361 wxLogDebug("MWM_DECOR_RESIZEH");
362 hints
.flags
|= MWM_HINTS_DECORATIONS
;
363 hints
.decorations
|= MWM_DECOR_RESIZEH
;
366 if (style
& wxSYSTEM_MENU
)
368 wxLogDebug("MWM_DECOR_MENU");
369 hints
.flags
|= MWM_HINTS_DECORATIONS
;
370 hints
.decorations
|= MWM_DECOR_MENU
;
373 if ((style
& wxCAPTION
) ||
374 (style
& wxTINY_CAPTION_HORIZ
) ||
375 (style
& wxTINY_CAPTION_VERT
))
377 wxLogDebug("MWM_DECOR_TITLE");
378 hints
.flags
|= MWM_HINTS_DECORATIONS
;
379 hints
.decorations
|= MWM_DECOR_TITLE
;
382 if ((style
& wxTHICK_FRAME
) || (style
& wxSIMPLE_BORDER
) || (style
& wxCAPTION
))
384 wxLogDebug("MWM_DECOR_BORDER");
385 hints
.flags
|= MWM_HINTS_DECORATIONS
;
386 hints
.decorations
|= MWM_DECOR_BORDER
;
389 if (style
& wxMINIMIZE_BOX
)
391 wxLogDebug("MWM_DECOR_MINIMIZE");
392 hints
.flags
|= MWM_HINTS_DECORATIONS
;
393 hints
.decorations
|= MWM_DECOR_MINIMIZE
;
396 if (style
& wxMAXIMIZE_BOX
)
398 wxLogDebug("MWM_DECOR_MAXIMIZE");
399 hints
.flags
|= MWM_HINTS_DECORATIONS
;
400 hints
.decorations
|= MWM_DECOR_MAXIMIZE
;
403 XChangeProperty(wxGlobalDisplay(),
405 mwm_wm_hints
, mwm_wm_hints
,
407 (unsigned char *) &hints
, PROP_MOTIF_WM_HINTS_ELEMENTS
);
412 bool wxMWMIsRunning(Window w
)
414 Display
*dpy
= (Display
*)wxGetDisplay();
415 Atom motifWmInfo
= XInternAtom(dpy
, "_MOTIF_WM_INFO", False
);
417 unsigned long length
, bytesafter
;
418 unsigned char value
[20];
419 unsigned char *ptr
= &value
[0];
423 type
= format
= length
= 0;
426 ret
= XGetWindowProperty(wxGlobalDisplay(), w
, motifWmInfo
,
427 0L, 2, False
, motifWmInfo
,
428 &type
, &format
, &length
, &bytesafter
, &ptr
);
430 return (ret
== Success
);
433 // For implementation purposes - sometimes decorations make the client area
435 wxPoint
wxTopLevelWindowX11::GetClientAreaOrigin() const
437 // In fact wxFrame::GetClientAreaOrigin
438 // does the required calculation already.
440 if (this->IsKindOf(CLASSINFO(wxFrame
)))
442 wxFrame
* frame
= (wxFrame
*) this;
443 if (frame
->GetMenuBar())
444 return wxPoint(0, frame
->GetMenuBar()->GetSize().y
);
447 return wxPoint(0, 0);
450 void wxTopLevelWindowX11::DoGetClientSize( int *width
, int *height
) const
452 wxWindowX11::DoGetClientSize(width
, height
);
453 // Done by wxTopLevelWindow
455 if (this->IsKindOf(CLASSINFO(wxFrame
)))
457 wxFrame
* frame
= (wxFrame
*) this;
458 if (frame
->GetMenuBar())
459 (*height
) -= frame
->GetMenuBar()->GetSize().y
;
460 if (frame
->GetStatusBar())
461 (*height
) -= frame
->GetStatusBar()->GetSize().y
;
466 void wxTopLevelWindowX11::DoSetClientSize(int width
, int height
)
468 wxWindowX11::DoSetClientSize(width
, height
);
471 if (!GetMainWindow())
474 XWindowChanges windowChanges
;
479 windowChanges
.width
= width
;
480 valueMask
|= CWWidth
;
484 windowChanges
.height
= height
;
485 valueMask
|= CWHeight
;
487 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
488 valueMask
, & windowChanges
);
492 void wxTopLevelWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
495 msg
.Printf("Setting pos: %d, %d", x
, y
);
497 wxWindowX11::DoSetSize(x
, y
, width
, height
, sizeFlags
);
499 wxPoint pt
= GetPosition();
500 msg
.Printf("After, pos: %d, %d", pt
.x
, pt
.y
);
503 XSync(wxGlobalDisplay(), False
);
507 msg
.Printf("Before setting size: %d, %d", w
, h
);
509 if (!GetMainWindow())
512 XWindowChanges windowChanges
;
515 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
518 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
522 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
525 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
529 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
531 windowChanges
.width
= width
/* - m_borderSize*2 */;
532 valueMask
|= CWWidth
;
534 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
536 windowChanges
.height
= height
/* -m_borderSize*2*/;
537 valueMask
|= CWHeight
;
540 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
541 valueMask
, & windowChanges
);
542 XSync(wxGlobalDisplay(), False
);
544 msg
.Printf("Tried to set to %d, %d. After setting size: %d, %d", width
, height
, w
, h
);
549 void wxTopLevelWindowX11::DoGetPosition(int *x
, int *y
) const
551 XSync(wxGlobalDisplay(), False
);
552 Window window
= (Window
) m_mainWidget
;
558 wxLogDebug("Translating...");
560 XTranslateCoordinates(wxGlobalDisplay(), window
, XDefaultRootWindow(wxGlobalDisplay()),
561 0, 0, & offsetX
, & offsetY
, & childWindow
);
564 msg
.Printf("Offset: %d, %d", offsetX
, offsetY
);
567 XWindowAttributes attr
;
568 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
573 *x
= attr
.x
+ offsetX
;
574 *y
= attr
.y
+ offsetY
;