1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "window.h"
27 #include "wx/dcclient.h"
31 #include "wx/layout.h"
32 #include "wx/dialog.h"
33 #include "wx/listbox.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
36 #include "wx/msgdlg.h"
38 #include "wx/scrolwin.h"
39 #include "wx/scrolbar.h"
40 #include "wx/module.h"
41 #include "wx/menuitem.h"
43 #include "wx/fontutil.h"
44 #include "wx/univ/renderer.h"
47 #if wxUSE_DRAG_AND_DROP
51 #include "wx/x11/private.h"
52 #include "X11/Xutil.h"
55 // For wxGetLocalTime, used by XButtonEventGetTime
61 // ----------------------------------------------------------------------------
62 // global variables for this module
63 // ----------------------------------------------------------------------------
65 extern wxHashTable
*wxWidgetHashTable
;
66 extern wxHashTable
*wxClientWidgetHashTable
;
67 static wxWindow
* g_captureWindow
= NULL
;
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
75 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
76 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11
, wxWindowBase
)
84 BEGIN_EVENT_TABLE(wxWindowX11
, wxWindowBase
)
85 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged
)
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 void wxWindowX11::Init()
103 m_mainWindow
= (WXWindow
) 0;
104 m_clientWindow
= (WXWindow
) 0;
105 m_insertIntoMain
= FALSE
;
106 m_updateNcArea
= FALSE
;
108 m_winCaptured
= FALSE
;
109 m_needsInputFocus
= FALSE
;
115 // real construction (Init() must have been called before!)
116 bool wxWindowX11::Create(wxWindow
*parent
, wxWindowID id
,
120 const wxString
& name
)
122 wxCHECK_MSG( parent
, FALSE
, wxT("can't create wxWindow without parent") );
124 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
126 parent
->AddChild(this);
128 Display
*xdisplay
= (Display
*) wxGlobalDisplay();
129 int xscreen
= DefaultScreen( xdisplay
);
130 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
131 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
133 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
134 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
136 m_foregroundColour
= *wxBLACK
;
137 m_foregroundColour
.CalcPixel( (WXColormap
) cm
);
139 Window xparent
= (Window
) parent
->GetClientAreaWindow();
141 // Add window's own scrollbars to main window, not to client window
142 if (parent
->GetInsertIntoMain())
144 // wxLogDebug( "Inserted into main: %s", GetName().c_str() );
145 xparent
= (Window
) parent
->GetMainWindow();
148 // Size (not including the border) must be nonzero (or a Value error results)!
149 // Note: The Xlib manual doesn't mention this restriction of XCreateWindow.
162 #if wxUSE_TWO_WINDOWS
163 bool need_two_windows
=
164 ((( wxSUNKEN_BORDER
| wxRAISED_BORDER
| wxSIMPLE_BORDER
| wxHSCROLL
| wxVSCROLL
) & m_windowStyle
) != 0);
166 bool need_two_windows
= FALSE
;
170 long xattributes
= 0;
172 XSetWindowAttributes xattributes
;
173 long xattributes_mask
= 0;
175 xattributes_mask
|= CWBackPixel
;
176 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
178 xattributes_mask
|= CWBorderPixel
;
179 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
181 xattributes_mask
|= CWEventMask
;
184 if (need_two_windows
)
187 long backColor
, foreColor
;
188 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
189 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
191 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
192 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
193 XSelectInput( xdisplay
, xwindow
,
194 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
195 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
196 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
197 PropertyChangeMask
);
201 xattributes
.event_mask
=
202 ExposureMask
| StructureNotifyMask
| ColormapChangeMask
;
204 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
205 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
209 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
211 m_mainWindow
= (WXWindow
) xwindow
;
212 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
214 XMapWindow( xdisplay
, xwindow
);
217 xattributes
.event_mask
=
218 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
219 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
220 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
221 PropertyChangeMask
| VisibilityChangeMask
;
223 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE
))
225 xattributes_mask
|= CWBitGravity
;
226 xattributes
.bit_gravity
= StaticGravity
;
230 if (HasFlag( wxSUNKEN_BORDER
) || HasFlag( wxRAISED_BORDER
))
237 else if (HasFlag( wxSIMPLE_BORDER
))
250 // Make again sure the size is nonzero.
257 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
258 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
260 xwindow
= XCreateWindowWithColor( xdisplay
, xwindow
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
261 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
262 XSelectInput( xdisplay
, xwindow
,
263 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
264 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
265 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
266 PropertyChangeMask
);
269 xwindow
= XCreateWindow( xdisplay
, xwindow
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
270 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
273 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
275 m_clientWindow
= (WXWindow
) xwindow
;
276 wxAddClientWindowToTable( xwindow
, (wxWindow
*) this );
278 XMapWindow( xdisplay
, xwindow
);
282 // wxLogDebug( "No two windows needed %s", GetName().c_str() );
284 long backColor
, foreColor
;
285 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
286 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
288 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
289 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
290 XSelectInput( xdisplay
, xwindow
,
291 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
292 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
293 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
294 PropertyChangeMask
);
297 xattributes
.event_mask
=
298 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
299 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
300 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
301 PropertyChangeMask
| VisibilityChangeMask
;
303 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE
))
305 xattributes_mask
|= CWBitGravity
;
306 xattributes
.bit_gravity
= NorthWestGravity
;
309 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
310 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
313 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
315 m_mainWindow
= (WXWindow
) xwindow
;
316 m_clientWindow
= m_mainWindow
;
317 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
319 XMapWindow( xdisplay
, xwindow
);
322 // Is a subwindow, so map immediately
325 // Without this, the cursor may not be restored properly (e.g. in splitter
327 SetCursor(*wxSTANDARD_CURSOR
);
328 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
330 // Don't call this, it can have nasty repercussions for composite controls,
332 // SetSize(pos.x, pos.y, size.x, size.y);
338 wxWindowX11::~wxWindowX11()
342 if (g_captureWindow
== this)
343 g_captureWindow
= NULL
;
345 m_isBeingDeleted
= TRUE
;
349 if (m_clientWindow
!= m_mainWindow
)
351 // Destroy the cleint window
352 Window xwindow
= (Window
) m_clientWindow
;
353 wxDeleteClientWindowFromTable( xwindow
);
354 XDestroyWindow( wxGlobalDisplay(), xwindow
);
355 m_clientWindow
= NULL
;
358 // Destroy the window
359 Window xwindow
= (Window
) m_mainWindow
;
360 wxDeleteWindowFromTable( xwindow
);
361 XDestroyWindow( wxGlobalDisplay(), xwindow
);
365 // ---------------------------------------------------------------------------
367 // ---------------------------------------------------------------------------
369 void wxWindowX11::SetFocus()
371 Window xwindow
= (Window
) m_clientWindow
;
373 wxCHECK_RET( xwindow
, wxT("invalid window") );
375 wxCHECK_RET( AcceptsFocus(), wxT("set focus on window that doesn't accept the focus") );
378 if (GetName() == "scrollBar")
385 if (wxWindowIsVisible(xwindow
))
387 wxLogTrace( _T("focus"), _T("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
388 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
389 XSetInputFocus( wxGlobalDisplay(), xwindow
, RevertToNone
, CurrentTime
);
390 m_needsInputFocus
= FALSE
;
394 m_needsInputFocus
= TRUE
;
398 // Get the window with the focus
399 wxWindow
*wxWindowBase::FindFocus()
401 Window xfocus
= (Window
) 0;
404 XGetInputFocus( wxGlobalDisplay(), &xfocus
, &revert
);
407 wxWindow
*win
= wxGetWindowFromTable( xfocus
);
410 win
= wxGetClientWindowFromTable( xfocus
);
419 // Enabling/disabling handled by event loop, and not sending events
421 bool wxWindowX11::Enable(bool enable
)
423 if ( !wxWindowBase::Enable(enable
) )
429 bool wxWindowX11::Show(bool show
)
431 wxWindowBase::Show(show
);
433 Window xwindow
= (Window
) m_mainWindow
;
434 Display
*xdisp
= wxGlobalDisplay();
437 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
438 XMapWindow(xdisp
, xwindow
);
442 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
443 XUnmapWindow(xdisp
, xwindow
);
449 // Raise the window to the top of the Z order
450 void wxWindowX11::Raise()
453 XRaiseWindow( wxGlobalDisplay(), (Window
) m_mainWindow
);
456 // Lower the window to the bottom of the Z order
457 void wxWindowX11::Lower()
460 XLowerWindow( wxGlobalDisplay(), (Window
) m_mainWindow
);
463 void wxWindowX11::DoCaptureMouse()
465 if ((g_captureWindow
!= NULL
) && (g_captureWindow
!= this))
467 wxASSERT_MSG(FALSE
, wxT("Trying to capture before mouse released."));
478 Window xwindow
= (Window
) m_clientWindow
;
480 wxCHECK_RET( xwindow
, wxT("invalid window") );
482 g_captureWindow
= (wxWindow
*) this;
486 int res
= XGrabPointer(wxGlobalDisplay(), xwindow
,
488 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
492 None
, /* cursor */ // TODO: This may need to be set to the cursor of this window
495 if (res
!= GrabSuccess
)
498 msg
.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
500 if (res
== GrabNotViewable
)
501 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
503 g_captureWindow
= NULL
;
507 m_winCaptured
= TRUE
;
511 void wxWindowX11::DoReleaseMouse()
513 g_captureWindow
= NULL
;
515 if ( !m_winCaptured
)
518 Window xwindow
= (Window
) m_clientWindow
;
522 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
525 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
527 m_winCaptured
= FALSE
;
530 bool wxWindowX11::SetFont(const wxFont
& font
)
532 if ( !wxWindowBase::SetFont(font
) )
541 bool wxWindowX11::SetCursor(const wxCursor
& cursor
)
543 if ( !wxWindowBase::SetCursor(cursor
) )
549 Window xwindow
= (Window
) m_clientWindow
;
551 wxCHECK_MSG( xwindow
, FALSE
, wxT("invalid window") );
553 wxCursor cursorToUse
;
555 cursorToUse
= m_cursor
;
557 cursorToUse
= *wxSTANDARD_CURSOR
;
559 Cursor xcursor
= (Cursor
) cursorToUse
.GetCursor();
561 XDefineCursor( wxGlobalDisplay(), xwindow
, xcursor
);
566 // Coordinates relative to the window
567 void wxWindowX11::WarpPointer (int x
, int y
)
569 Window xwindow
= (Window
) m_clientWindow
;
571 wxCHECK_RET( xwindow
, wxT("invalid window") );
573 XWarpPointer( wxGlobalDisplay(), None
, xwindow
, 0, 0, 0, 0, x
, y
);
576 // Does a physical scroll
577 void wxWindowX11::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
579 // No scrolling requested.
580 if ((dx
== 0) && (dy
== 0)) return;
582 if (!m_updateRegion
.IsEmpty())
584 m_updateRegion
.Offset( dx
, dy
);
588 GetSize( &cw
, &ch
); // GetClientSize() ??
589 m_updateRegion
.Intersect( 0, 0, cw
, ch
);
592 if (!m_clearRegion
.IsEmpty())
594 m_clearRegion
.Offset( dx
, dy
);
598 GetSize( &cw
, &ch
); // GetClientSize() ??
599 m_clearRegion
.Intersect( 0, 0, cw
, ch
);
602 Window xwindow
= (Window
) GetClientAreaWindow();
604 wxCHECK_RET( xwindow
, wxT("invalid window") );
606 Display
*xdisplay
= wxGlobalDisplay();
608 GC xgc
= XCreateGC( xdisplay
, xwindow
, 0, NULL
);
609 XSetGraphicsExposures( xdisplay
, xgc
, True
);
627 GetClientSize( &cw
, &ch
);
630 #if wxUSE_TWO_WINDOWS
631 wxPoint
offset( 0,0 );
633 wxPoint offset
= GetClientAreaOrigin();
638 int w
= cw
- abs(dx
);
639 int h
= ch
- abs(dy
);
641 if ((h
< 0) || (w
< 0))
648 if (dx
< 0) rect
.x
= cw
+dx
+ offset
.x
; else rect
.x
= s_x
;
649 if (dy
< 0) rect
.y
= ch
+dy
+ offset
.y
; else rect
.y
= s_y
;
650 if (dy
!= 0) rect
.width
= cw
; else rect
.width
= abs(dx
);
651 if (dx
!= 0) rect
.height
= ch
; else rect
.height
= abs(dy
);
656 if (dx
< 0) s_x
+= -dx
;
657 if (dy
< 0) s_y
+= -dy
;
658 if (dx
> 0) d_x
= dx
+ offset
.x
;
659 if (dy
> 0) d_y
= dy
+ offset
.y
;
661 XCopyArea( xdisplay
, xwindow
, xwindow
, xgc
, s_x
, s_y
, w
, h
, d_x
, d_y
);
663 // wxLogDebug( "Copy: s_x %d s_y %d w %d h %d d_x %d d_y %d", s_x, s_y, w, h, d_x, d_y );
665 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
667 m_updateRegion
.Union( rect
);
668 m_clearRegion
.Union( rect
);
671 XFreeGC( xdisplay
, xgc
);
674 // ---------------------------------------------------------------------------
676 // ---------------------------------------------------------------------------
678 #if wxUSE_DRAG_AND_DROP
680 void wxWindowX11::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
687 // Old style file-manager drag&drop
688 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept
))
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
699 void wxWindowX11::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
704 #endif // wxUSE_TOOLTIPS
706 // ---------------------------------------------------------------------------
707 // moving and resizing
708 // ---------------------------------------------------------------------------
710 bool wxWindowX11::PreResize()
716 void wxWindowX11::DoGetSize(int *x
, int *y
) const
718 Window xwindow
= (Window
) m_mainWindow
;
720 wxCHECK_RET( xwindow
, wxT("invalid window") );
722 //XSync(wxGlobalDisplay(), False);
724 XWindowAttributes attr
;
725 Status status
= XGetWindowAttributes( wxGlobalDisplay(), xwindow
, &attr
);
730 *x
= attr
.width
/* + 2*m_borderSize */ ;
731 *y
= attr
.height
/* + 2*m_borderSize */ ;
735 void wxWindowX11::DoGetPosition(int *x
, int *y
) const
737 Window window
= (Window
) m_mainWindow
;
740 //XSync(wxGlobalDisplay(), False);
741 XWindowAttributes attr
;
742 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
750 // We may be faking the client origin. So a window that's really at (0, 30)
751 // may appear (to wxWin apps) to be at (0, 0).
754 wxPoint
pt(GetParent()->GetClientAreaOrigin());
762 void wxWindowX11::DoScreenToClient(int *x
, int *y
) const
764 Display
*display
= wxGlobalDisplay();
765 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
766 Window thisWindow
= (Window
) m_clientWindow
;
771 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
774 void wxWindowX11::DoClientToScreen(int *x
, int *y
) const
776 Display
*display
= wxGlobalDisplay();
777 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
778 Window thisWindow
= (Window
) m_clientWindow
;
783 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
787 // Get size *available for subwindows* i.e. excluding menu bar etc.
788 void wxWindowX11::DoGetClientSize(int *x
, int *y
) const
790 Window window
= (Window
) m_mainWindow
;
794 XWindowAttributes attr
;
795 Status status
= XGetWindowAttributes( wxGlobalDisplay(), window
, &attr
);
806 void wxWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
808 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
810 Window xwindow
= (Window
) m_mainWindow
;
812 wxCHECK_RET( xwindow
, wxT("invalid window") );
814 XWindowAttributes attr
;
815 Status status
= XGetWindowAttributes( wxGlobalDisplay(), xwindow
, &attr
);
816 wxCHECK_RET( status
, wxT("invalid window attributes") );
820 int new_w
= attr
.width
;
821 int new_h
= attr
.height
;
823 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
826 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
829 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
832 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
848 DoMoveWindow( new_x
, new_y
, new_w
, new_h
);
851 void wxWindowX11::DoSetClientSize(int width
, int height
)
853 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
855 Window xwindow
= (Window
) m_mainWindow
;
857 wxCHECK_RET( xwindow
, wxT("invalid window") );
859 XResizeWindow( wxGlobalDisplay(), xwindow
, width
, height
);
861 if (m_mainWindow
!= m_clientWindow
)
863 xwindow
= (Window
) m_clientWindow
;
865 wxWindow
*window
= (wxWindow
*) this;
866 wxRenderer
*renderer
= window
->GetRenderer();
869 wxRect border
= renderer
->GetBorderDimensions( (wxBorder
)(m_windowStyle
& wxBORDER_MASK
) );
870 width
-= border
.x
+ border
.width
;
871 height
-= border
.y
+ border
.height
;
874 XResizeWindow( wxGlobalDisplay(), xwindow
, width
, height
);
878 // For implementation purposes - sometimes decorations make the client area
880 wxPoint
wxWindowX11::GetClientAreaOrigin() const
882 return wxPoint(0, 0);
885 void wxWindowX11::DoMoveWindow(int x
, int y
, int width
, int height
)
887 Window xwindow
= (Window
) m_mainWindow
;
889 wxCHECK_RET( xwindow
, wxT("invalid window") );
893 XMoveResizeWindow( wxGlobalDisplay(), xwindow
, x
, y
, width
, height
);
894 if (m_mainWindow
!= m_clientWindow
)
896 xwindow
= (Window
) m_clientWindow
;
898 wxWindow
*window
= (wxWindow
*) this;
899 wxRenderer
*renderer
= window
->GetRenderer();
902 wxRect border
= renderer
->GetBorderDimensions( (wxBorder
)(m_windowStyle
& wxBORDER_MASK
) );
905 width
-= border
.x
+ border
.width
;
906 height
-= border
.y
+ border
.height
;
914 wxScrollBar
*sb
= window
->GetScrollbar( wxHORIZONTAL
);
915 if (sb
&& sb
->IsShown())
917 wxSize size
= sb
->GetSize();
920 sb
= window
->GetScrollbar( wxVERTICAL
);
921 if (sb
&& sb
->IsShown())
923 wxSize size
= sb
->GetSize();
927 XMoveResizeWindow( wxGlobalDisplay(), xwindow
, x
, y
, wxMax(1, width
), wxMax(1, height
) );
932 XWindowChanges windowChanges
;
935 windowChanges
.width
= width
;
936 windowChanges
.height
= height
;
937 windowChanges
.stack_mode
= 0;
938 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
940 XConfigureWindow( wxGlobalDisplay(), xwindow
, valueMask
, &windowChanges
);
945 void wxWindowX11::SetSizeHints(int minW
, int minH
, int maxW
, int maxH
, int incW
, int incH
)
953 XSizeHints sizeHints
;
956 if (minW
> -1 && minH
> -1)
958 sizeHints
.flags
|= PMinSize
;
959 sizeHints
.min_width
= minW
;
960 sizeHints
.min_height
= minH
;
962 if (maxW
> -1 && maxH
> -1)
964 sizeHints
.flags
|= PMaxSize
;
965 sizeHints
.max_width
= maxW
;
966 sizeHints
.max_height
= maxH
;
968 if (incW
> -1 && incH
> -1)
970 sizeHints
.flags
|= PResizeInc
;
971 sizeHints
.width_inc
= incW
;
972 sizeHints
.height_inc
= incH
;
975 XSetWMNormalHints(wxGlobalDisplay(), (Window
) m_mainWindow
, &sizeHints
);
979 // ---------------------------------------------------------------------------
981 // ---------------------------------------------------------------------------
983 int wxWindowX11::GetCharHeight() const
985 wxCHECK_MSG( m_font
.Ok(), 0, wxT("valid window font needed") );
988 // There should be an easier way.
989 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
990 pango_layout_set_font_description( layout
, GetFont().GetNativeFontInfo()->description
);
991 pango_layout_set_text(layout
, "H", 1 );
993 pango_layout_get_pixel_size(layout
, &w
, &h
);
994 g_object_unref( G_OBJECT( layout
) );
998 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, wxGlobalDisplay());
1000 int direction
, ascent
, descent
;
1001 XCharStruct overall
;
1002 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1003 &descent
, &overall
);
1005 // return (overall.ascent + overall.descent);
1006 return (ascent
+ descent
);
1010 int wxWindowX11::GetCharWidth() const
1012 wxCHECK_MSG( m_font
.Ok(), 0, wxT("valid window font needed") );
1015 // There should be an easier way.
1016 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
1017 pango_layout_set_font_description( layout
, GetFont().GetNativeFontInfo()->description
);
1018 pango_layout_set_text(layout
, "H", 1 );
1020 pango_layout_get_pixel_size(layout
, &w
, &h
);
1021 g_object_unref( G_OBJECT( layout
) );
1025 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, wxGlobalDisplay());
1027 int direction
, ascent
, descent
;
1028 XCharStruct overall
;
1029 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1030 &descent
, &overall
);
1032 return overall
.width
;
1036 void wxWindowX11::GetTextExtent(const wxString
& string
,
1038 int *descent
, int *externalLeading
,
1039 const wxFont
*theFont
) const
1041 wxFont fontToUse
= m_font
;
1042 if (theFont
) fontToUse
= *theFont
;
1044 wxCHECK_RET( fontToUse
.Ok(), wxT("invalid font") );
1046 if (string
.IsEmpty())
1054 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
1056 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
1057 pango_layout_set_font_description(layout
, desc
);
1059 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( string
);
1060 pango_layout_set_text(layout
, (const char*) data
, strlen( (const char*) data
));
1062 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
1065 PangoRectangle rect
;
1066 pango_layout_line_get_extents(line
, NULL
, &rect
);
1068 if (x
) (*x
) = (wxCoord
) (rect
.width
/ PANGO_SCALE
);
1069 if (y
) (*y
) = (wxCoord
) (rect
.height
/ PANGO_SCALE
);
1072 // Do something about metrics here
1075 if (externalLeading
) (*externalLeading
) = 0; // ??
1077 g_object_unref( G_OBJECT( layout
) );
1079 WXFontStructPtr pFontStruct
= fontToUse
.GetFontStruct(1.0, wxGlobalDisplay());
1081 int direction
, ascent
, descent2
;
1082 XCharStruct overall
;
1083 int slen
= string
.Len();
1085 XTextExtents((XFontStruct
*) pFontStruct
, (char*) string
.c_str(), slen
,
1086 &direction
, &ascent
, &descent2
, &overall
);
1089 *x
= (overall
.width
);
1091 *y
= (ascent
+ descent2
);
1093 *descent
= descent2
;
1094 if (externalLeading
)
1095 *externalLeading
= 0;
1099 // ----------------------------------------------------------------------------
1101 // ----------------------------------------------------------------------------
1103 void wxWindowX11::Refresh(bool eraseBack
, const wxRect
*rect
)
1109 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1110 m_clearRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
1115 GetSize( &width
, &height
);
1117 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1118 m_clearRegion
.Clear();
1119 m_clearRegion
.Union( 0, 0, width
, height
);
1125 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1126 m_updateRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
1131 GetSize( &width
, &height
);
1133 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1134 m_updateRegion
.Clear();
1135 m_updateRegion
.Union( 0, 0, width
, height
);
1139 void wxWindowX11::Update()
1143 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1144 // Send nc paint events.
1145 SendNcPaintEvents();
1148 if (!m_updateRegion
.IsEmpty())
1150 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1151 // Actually send erase events.
1154 // Actually send paint events.
1159 void wxWindowX11::SendEraseEvents()
1161 if (m_clearRegion
.IsEmpty()) return;
1163 wxClientDC
dc( (wxWindow
*)this );
1164 dc
.SetClippingRegion( m_clearRegion
);
1166 wxEraseEvent
erase_event( GetId(), &dc
);
1167 erase_event
.SetEventObject( this );
1169 if (!GetEventHandler()->ProcessEvent(erase_event
) )
1171 Display
*xdisplay
= wxGlobalDisplay();
1172 Window xwindow
= (Window
) GetClientAreaWindow();
1173 XSetForeground( xdisplay
, g_eraseGC
, m_backgroundColour
.GetPixel() );
1175 wxRegionIterator
upd( m_clearRegion
);
1178 XFillRectangle( xdisplay
, xwindow
, g_eraseGC
,
1179 upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
1184 m_clearRegion
.Clear();
1187 void wxWindowX11::SendPaintEvents()
1189 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1191 m_clipPaintRegion
= TRUE
;
1193 wxPaintEvent
paint_event( GetId() );
1194 paint_event
.SetEventObject( this );
1195 GetEventHandler()->ProcessEvent( paint_event
);
1197 m_updateRegion
.Clear();
1199 m_clipPaintRegion
= FALSE
;
1202 void wxWindowX11::SendNcPaintEvents()
1204 wxWindow
*window
= (wxWindow
*) this;
1206 // All this for drawing the small square between the scrollbars.
1211 wxScrollBar
*sb
= window
->GetScrollbar( wxHORIZONTAL
);
1212 if (sb
&& sb
->IsShown())
1214 height
= sb
->GetSize().y
;
1215 y
= sb
->GetPosition().y
;
1217 sb
= window
->GetScrollbar( wxVERTICAL
);
1218 if (sb
&& sb
->IsShown())
1220 width
= sb
->GetSize().x
;
1221 x
= sb
->GetPosition().x
;
1223 Display
*xdisplay
= wxGlobalDisplay();
1224 Window xwindow
= (Window
) GetMainWindow();
1225 Colormap cm
= (Colormap
) wxTheApp
->GetMainColormap( wxGetDisplay() );
1226 wxColour colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1227 colour
.CalcPixel( (WXColormap
) cm
);
1229 XSetForeground( xdisplay
, g_eraseGC
, colour
.GetPixel() );
1231 XFillRectangle( xdisplay
, xwindow
, g_eraseGC
, x
, y
, width
, height
);
1235 wxNcPaintEvent
nc_paint_event( GetId() );
1236 nc_paint_event
.SetEventObject( this );
1237 GetEventHandler()->ProcessEvent( nc_paint_event
);
1239 m_updateNcArea
= FALSE
;
1242 // ----------------------------------------------------------------------------
1244 // ----------------------------------------------------------------------------
1246 // Responds to colour changes: passes event on to children.
1247 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1249 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1252 // Only propagate to non-top-level windows
1253 wxWindow
*win
= node
->GetData();
1254 if ( win
->GetParent() )
1256 wxSysColourChangedEvent event2
;
1257 event
.m_eventObject
= win
;
1258 win
->GetEventHandler()->ProcessEvent(event2
);
1261 node
= node
->GetNext();
1265 // See handler for InFocus case in app.cpp for details.
1266 wxWindow
* g_GettingFocus
= NULL
;
1268 void wxWindowX11::OnInternalIdle()
1270 // Update invalidated regions.
1273 // This calls the UI-update mechanism (querying windows for
1274 // menu/toolbar/control state information)
1275 if (wxUpdateUIEvent::CanUpdate((wxWindow
*) this))
1276 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1278 // Set the input focus if couldn't do it before
1279 if (m_needsInputFocus
)
1283 msg
.Printf("Setting focus for %s from OnInternalIdle\n", GetClassInfo()->GetClassName());
1284 printf(msg
.c_str());
1288 // If it couldn't set the focus now, there's
1289 // no point in trying again.
1290 m_needsInputFocus
= FALSE
;
1292 g_GettingFocus
= NULL
;
1295 // ----------------------------------------------------------------------------
1296 // function which maintain the global hash table mapping Widgets to wxWindows
1297 // ----------------------------------------------------------------------------
1299 bool wxAddWindowToTable(Window w
, wxWindow
*win
)
1301 wxWindow
*oldItem
= NULL
;
1302 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1304 wxLogDebug( wxT("Widget table clash: new widget is %ld, %s"),
1305 (long)w
, win
->GetClassInfo()->GetClassName());
1309 wxWidgetHashTable
->Put((long) w
, win
);
1311 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1312 (unsigned int) w
, win
, win
->GetClassInfo()->GetClassName());
1317 wxWindow
*wxGetWindowFromTable(Window w
)
1319 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1322 void wxDeleteWindowFromTable(Window w
)
1324 wxWidgetHashTable
->Delete((long)w
);
1327 // ----------------------------------------------------------------------------
1328 // function which maintain the global hash table mapping client widgets
1329 // ----------------------------------------------------------------------------
1331 bool wxAddClientWindowToTable(Window w
, wxWindow
*win
)
1333 wxWindow
*oldItem
= NULL
;
1334 if ((oldItem
= (wxWindow
*)wxClientWidgetHashTable
->Get ((long) w
)))
1336 wxLogDebug( wxT("Client window table clash: new window is %ld, %s"),
1337 (long)w
, win
->GetClassInfo()->GetClassName());
1341 wxClientWidgetHashTable
->Put((long) w
, win
);
1343 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1344 (unsigned int) w
, win
, win
->GetClassInfo()->GetClassName());
1349 wxWindow
*wxGetClientWindowFromTable(Window w
)
1351 return (wxWindow
*)wxClientWidgetHashTable
->Get((long) w
);
1354 void wxDeleteClientWindowFromTable(Window w
)
1356 wxClientWidgetHashTable
->Delete((long)w
);
1359 // ----------------------------------------------------------------------------
1360 // add/remove window from the table
1361 // ----------------------------------------------------------------------------
1363 // ----------------------------------------------------------------------------
1364 // X11-specific accessors
1365 // ----------------------------------------------------------------------------
1367 WXWindow
wxWindowX11::GetMainWindow() const
1369 return m_mainWindow
;
1372 WXWindow
wxWindowX11::GetClientAreaWindow() const
1374 return m_clientWindow
;
1377 // ----------------------------------------------------------------------------
1378 // TranslateXXXEvent() functions
1379 // ----------------------------------------------------------------------------
1381 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
, Window window
, XEvent
*xevent
)
1383 switch (XEventGetType(xevent
))
1391 wxEventType eventType
= wxEVT_NULL
;
1393 if (XEventGetType(xevent
) == EnterNotify
)
1395 //if (local_event.xcrossing.mode!=NotifyNormal)
1396 // return ; // Ignore grab events
1397 eventType
= wxEVT_ENTER_WINDOW
;
1398 // canvas->GetEventHandler()->OnSetFocus();
1400 else if (XEventGetType(xevent
) == LeaveNotify
)
1402 //if (local_event.xcrossingr.mode!=NotifyNormal)
1403 // return ; // Ignore grab events
1404 eventType
= wxEVT_LEAVE_WINDOW
;
1405 // canvas->GetEventHandler()->OnKillFocus();
1407 else if (XEventGetType(xevent
) == MotionNotify
)
1409 eventType
= wxEVT_MOTION
;
1411 else if (XEventGetType(xevent
) == ButtonPress
)
1413 wxevent
.SetTimestamp(XButtonEventGetTime(xevent
));
1415 if (XButtonEventLChanged(xevent
))
1417 eventType
= wxEVT_LEFT_DOWN
;
1420 else if (XButtonEventMChanged(xevent
))
1422 eventType
= wxEVT_MIDDLE_DOWN
;
1425 else if (XButtonEventRChanged(xevent
))
1427 eventType
= wxEVT_RIGHT_DOWN
;
1431 // check for a double click
1432 // TODO: where can we get this value from?
1433 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1434 long dclickTime
= 200;
1435 long ts
= wxevent
.GetTimestamp();
1437 int buttonLast
= win
->GetLastClickedButton();
1438 long lastTS
= win
->GetLastClickTime();
1439 if ( buttonLast
&& buttonLast
== button
&& (ts
- lastTS
) < dclickTime
)
1442 win
->SetLastClick(0, ts
);
1443 if ( eventType
== wxEVT_LEFT_DOWN
)
1444 eventType
= wxEVT_LEFT_DCLICK
;
1445 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
1446 eventType
= wxEVT_MIDDLE_DCLICK
;
1447 else if ( eventType
== wxEVT_RIGHT_DOWN
)
1448 eventType
= wxEVT_RIGHT_DCLICK
;
1452 // not fast enough or different button
1453 win
->SetLastClick(button
, ts
);
1456 else if (XEventGetType(xevent
) == ButtonRelease
)
1458 if (XButtonEventLChanged(xevent
))
1460 eventType
= wxEVT_LEFT_UP
;
1462 else if (XButtonEventMChanged(xevent
))
1464 eventType
= wxEVT_MIDDLE_UP
;
1466 else if (XButtonEventRChanged(xevent
))
1468 eventType
= wxEVT_RIGHT_UP
;
1477 wxevent
.SetEventType(eventType
);
1479 wxevent
.m_x
= XButtonEventGetX(xevent
);
1480 wxevent
.m_y
= XButtonEventGetY(xevent
);
1482 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
1483 || (XButtonEventLIsDown(xevent
)
1484 && (eventType
!= wxEVT_LEFT_UP
)));
1485 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
1486 || (XButtonEventMIsDown(xevent
)
1487 && (eventType
!= wxEVT_MIDDLE_UP
)));
1488 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
1489 || (XButtonEventRIsDown (xevent
)
1490 && (eventType
!= wxEVT_RIGHT_UP
)));
1492 wxevent
.m_shiftDown
= XButtonEventShiftIsDown(xevent
);
1493 wxevent
.m_controlDown
= XButtonEventCtrlIsDown(xevent
);
1494 wxevent
.m_altDown
= XButtonEventAltIsDown(xevent
);
1495 wxevent
.m_metaDown
= XButtonEventMetaIsDown(xevent
);
1497 wxevent
.SetId(win
->GetId());
1498 wxevent
.SetEventObject(win
);
1506 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
, Window
WXUNUSED(win
), XEvent
*xevent
, bool isAscii
)
1508 switch (XEventGetType(xevent
))
1516 (void) XLookupString ((XKeyEvent
*) xevent
, buf
, 20, &keySym
, NULL
);
1517 int id
= wxCharCodeXToWX (keySym
);
1518 // id may be WXK_xxx code - these are outside ASCII range, so we
1519 // can't just use toupper() on id.
1520 // Only change this if we want the raw key that was pressed,
1521 // and don't change it if we want an ASCII value.
1522 if (!isAscii
&& (id
>= 'a' && id
<= 'z'))
1524 id
= id
+ 'A' - 'a';
1527 wxevent
.m_shiftDown
= XKeyEventShiftIsDown(xevent
);
1528 wxevent
.m_controlDown
= XKeyEventCtrlIsDown(xevent
);
1529 wxevent
.m_altDown
= XKeyEventAltIsDown(xevent
);
1530 wxevent
.m_metaDown
= XKeyEventMetaIsDown(xevent
);
1531 wxevent
.SetEventObject(win
);
1532 wxevent
.m_keyCode
= id
;
1533 wxevent
.SetTimestamp(XKeyEventGetTime(xevent
));
1535 wxevent
.m_x
= XKeyEventGetX(xevent
);
1536 wxevent
.m_y
= XKeyEventGetY(xevent
);
1546 // ----------------------------------------------------------------------------
1548 // ----------------------------------------------------------------------------
1550 bool wxWindowX11::SetBackgroundColour(const wxColour
& col
)
1552 wxWindowBase::SetBackgroundColour(col
);
1554 Display
*xdisplay
= (Display
*) wxGlobalDisplay();
1555 int xscreen
= DefaultScreen( xdisplay
);
1556 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
1558 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
1560 // We don't set the background colour as we paint
1561 // the background ourselves.
1562 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1567 bool wxWindowX11::SetForegroundColour(const wxColour
& col
)
1569 if ( !wxWindowBase::SetForegroundColour(col
) )
1575 // ----------------------------------------------------------------------------
1577 // ----------------------------------------------------------------------------
1579 wxWindow
*wxGetActiveWindow()
1582 wxFAIL_MSG(wxT("Not implemented"));
1587 wxWindow
*wxWindowBase::GetCapture()
1589 return (wxWindow
*)g_captureWindow
;
1593 // Find the wxWindow at the current mouse position, returning the mouse
1595 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
1597 return wxFindWindowAtPoint(wxGetMousePosition());
1600 // Get the current mouse position.
1601 wxPoint
wxGetMousePosition()
1605 return wxPoint(0, 0);
1607 Display
*display
= wxGlobalDisplay();
1608 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
1609 Window rootReturn
, childReturn
;
1610 int rootX
, rootY
, winX
, winY
;
1611 unsigned int maskReturn
;
1613 XQueryPointer (display
,
1617 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
1618 return wxPoint(rootX
, rootY
);
1623 // ----------------------------------------------------------------------------
1624 // wxNoOptimize: switch off size optimization
1625 // ----------------------------------------------------------------------------
1627 int wxNoOptimize::ms_count
= 0;
1630 // ----------------------------------------------------------------------------
1632 // ----------------------------------------------------------------------------
1634 class wxWinModule
: public wxModule
1641 DECLARE_DYNAMIC_CLASS(wxWinModule
)
1644 IMPLEMENT_DYNAMIC_CLASS(wxWinModule
, wxModule
)
1646 bool wxWinModule::OnInit()
1648 Display
*xdisplay
= wxGlobalDisplay();
1649 int xscreen
= DefaultScreen( xdisplay
);
1650 Window xroot
= RootWindow( xdisplay
, xscreen
);
1651 g_eraseGC
= XCreateGC( xdisplay
, xroot
, 0, NULL
);
1652 XSetFillStyle( xdisplay
, g_eraseGC
, FillSolid
);
1657 void wxWinModule::OnExit()
1659 Display
*xdisplay
= wxGlobalDisplay();
1660 XFreeGC( xdisplay
, g_eraseGC
);