1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
46 #if wxUSE_DRAG_AND_DROP
50 #include "wx/x11/private.h"
51 #include "X11/Xutil.h"
54 // For wxGetLocalTime, used by XButtonEventGetTime
60 // ----------------------------------------------------------------------------
61 // global variables for this module
62 // ----------------------------------------------------------------------------
64 extern wxHashTable
*wxWidgetHashTable
;
65 extern wxHashTable
*wxClientWidgetHashTable
;
66 static wxWindow
* g_captureWindow
= NULL
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
74 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
75 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11
, wxWindowBase
)
83 BEGIN_EVENT_TABLE(wxWindowX11
, wxWindowBase
)
84 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged
)
87 // ============================================================================
89 // ============================================================================
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 void wxWindowX11::Init()
101 // generic initializations first
105 m_mainWindow
= (WXWindow
) 0;
106 m_clientWindow
= (WXWindow
) 0;
107 m_insertIntoMain
= FALSE
;
108 m_updateNcArea
= FALSE
;
110 m_winCaptured
= FALSE
;
111 m_needsInputFocus
= FALSE
;
113 m_isBeingDeleted
= FALSE
;
118 // real construction (Init() must have been called before!)
119 bool wxWindowX11::Create(wxWindow
*parent
, wxWindowID id
,
123 const wxString
& name
)
125 wxCHECK_MSG( parent
, FALSE
, "can't create wxWindow without parent" );
127 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
129 parent
->AddChild(this);
131 Display
*xdisplay
= (Display
*) wxGlobalDisplay();
132 int xscreen
= DefaultScreen( xdisplay
);
133 Visual
*xvisual
= DefaultVisual( xdisplay
, xscreen
);
134 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
136 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
137 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
139 m_foregroundColour
= *wxBLACK
;
140 m_foregroundColour
.CalcPixel( (WXColormap
) cm
);
142 Window xparent
= (Window
) parent
->GetClientAreaWindow();
144 // Add window's own scrollbars to main window, not to client window
145 if (parent
->GetInsertIntoMain())
147 // wxLogDebug( "Inserted into main: %s", GetName().c_str() );
148 xparent
= (Window
) parent
->GetMainWindow();
151 // Size (not including the border) must be nonzero (or a Value error results)!
152 // Note: The Xlib manual doesn't mention this restriction of XCreateWindow.
165 #if wxUSE_TWO_WINDOWS
166 bool need_two_windows
=
167 ((( wxSUNKEN_BORDER
| wxRAISED_BORDER
| wxSIMPLE_BORDER
| wxHSCROLL
| wxVSCROLL
) & m_windowStyle
) != 0);
169 bool need_two_windows
= FALSE
;
173 long xattributes
= 0;
175 XSetWindowAttributes xattributes
;
176 long xattributes_mask
= 0;
178 xattributes_mask
|= CWBackPixel
;
179 xattributes
.background_pixel
= m_backgroundColour
.GetPixel();
181 xattributes_mask
|= CWBorderPixel
;
182 xattributes
.border_pixel
= BlackPixel( xdisplay
, xscreen
);
184 xattributes_mask
|= CWEventMask
;
187 if (need_two_windows
)
190 long backColor
, foreColor
;
191 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
192 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
194 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
195 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
196 XSelectInput( xdisplay
, xwindow
,
197 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
198 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
199 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
200 PropertyChangeMask
);
204 xattributes
.event_mask
=
205 ExposureMask
| StructureNotifyMask
| ColormapChangeMask
;
207 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
208 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
212 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
214 m_mainWindow
= (WXWindow
) xwindow
;
215 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
217 XMapWindow( xdisplay
, xwindow
);
220 xattributes
.event_mask
=
221 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
222 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
223 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
224 PropertyChangeMask
| VisibilityChangeMask
;
226 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
228 xattributes_mask
|= CWBitGravity
;
229 xattributes
.bit_gravity
= StaticGravity
;
233 if (HasFlag( wxSUNKEN_BORDER
) || HasFlag( wxRAISED_BORDER
))
240 else if (HasFlag( wxSIMPLE_BORDER
))
253 // Make again sure the size is nonzero.
260 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
261 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
263 xwindow
= XCreateWindowWithColor( xdisplay
, xwindow
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
264 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
265 XSelectInput( xdisplay
, xwindow
,
266 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
267 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
268 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
269 PropertyChangeMask
);
272 xwindow
= XCreateWindow( xdisplay
, xwindow
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
273 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
276 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
278 m_clientWindow
= (WXWindow
) xwindow
;
279 wxAddClientWindowToTable( xwindow
, (wxWindow
*) this );
281 XMapWindow( xdisplay
, xwindow
);
285 // wxLogDebug( "No two windows needed %s", GetName().c_str() );
287 long backColor
, foreColor
;
288 backColor
= GR_RGB(m_backgroundColour
.Red(), m_backgroundColour
.Green(), m_backgroundColour
.Blue());
289 foreColor
= GR_RGB(m_foregroundColour
.Red(), m_foregroundColour
.Green(), m_foregroundColour
.Blue());
291 Window xwindow
= XCreateWindowWithColor( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
292 0, 0, InputOutput
, xvisual
, backColor
, foreColor
);
293 XSelectInput( xdisplay
, xwindow
,
294 GR_EVENT_MASK_CLOSE_REQ
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
295 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
296 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
297 PropertyChangeMask
);
300 xattributes
.event_mask
=
301 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
302 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
303 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
304 PropertyChangeMask
| VisibilityChangeMask
;
306 if (HasFlag( wxNO_FULL_REPAINT_ON_RESIZE
))
308 xattributes_mask
|= CWBitGravity
;
309 xattributes
.bit_gravity
= NorthWestGravity
;
312 Window xwindow
= XCreateWindow( xdisplay
, xparent
, pos2
.x
, pos2
.y
, size2
.x
, size2
.y
,
313 0, DefaultDepth(xdisplay
,xscreen
), InputOutput
, xvisual
, xattributes_mask
, &xattributes
);
316 XSetWindowBackgroundPixmap( xdisplay
, xwindow
, None
);
318 m_mainWindow
= (WXWindow
) xwindow
;
319 m_clientWindow
= m_mainWindow
;
320 wxAddWindowToTable( xwindow
, (wxWindow
*) this );
322 XMapWindow( xdisplay
, xwindow
);
325 // Is a subwindow, so map immediately
328 // Without this, the cursor may not be restored properly (e.g. in splitter
330 SetCursor(*wxSTANDARD_CURSOR
);
331 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
333 // Don't call this, it can have nasty repercussions for composite controls,
335 // SetSize(pos.x, pos.y, size.x, size.y);
341 wxWindowX11::~wxWindowX11()
343 if (g_captureWindow
== this)
344 g_captureWindow
= NULL
;
346 m_isBeingDeleted
= TRUE
;
349 m_parent
->RemoveChild( this );
353 if (m_clientWindow
!= m_mainWindow
)
355 // Destroy the cleint window
356 Window xwindow
= (Window
) m_clientWindow
;
357 wxDeleteClientWindowFromTable( xwindow
);
358 XDestroyWindow( wxGlobalDisplay(), xwindow
);
359 m_clientWindow
= NULL
;
362 // Destroy the window
363 Window xwindow
= (Window
) m_mainWindow
;
364 wxDeleteWindowFromTable( xwindow
);
365 XDestroyWindow( wxGlobalDisplay(), xwindow
);
369 // ---------------------------------------------------------------------------
371 // ---------------------------------------------------------------------------
373 void wxWindowX11::SetFocus()
375 Window xwindow
= (Window
) m_clientWindow
;
377 wxCHECK_RET( xwindow
, wxT("invalid window") );
379 wxCHECK_RET( AcceptsFocus(), wxT("set focus on window that doesn't accept the focus") );
382 if (GetName() == "scrollBar")
389 if (wxWindowIsVisible(xwindow
))
391 wxLogTrace( _T("focus"), _T("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
392 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
393 XSetInputFocus( wxGlobalDisplay(), xwindow
, RevertToNone
, CurrentTime
);
394 m_needsInputFocus
= FALSE
;
398 m_needsInputFocus
= TRUE
;
402 // Get the window with the focus
403 wxWindow
*wxWindowBase::FindFocus()
405 Window xfocus
= (Window
) 0;
408 XGetInputFocus( wxGlobalDisplay(), &xfocus
, &revert
);
411 wxWindow
*win
= wxGetWindowFromTable( xfocus
);
414 win
= wxGetClientWindowFromTable( xfocus
);
423 // Enabling/disabling handled by event loop, and not sending events
425 bool wxWindowX11::Enable(bool enable
)
427 if ( !wxWindowBase::Enable(enable
) )
433 bool wxWindowX11::Show(bool show
)
435 wxWindowBase::Show(show
);
437 Window xwindow
= (Window
) m_mainWindow
;
438 Display
*xdisp
= wxGlobalDisplay();
441 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
442 XMapWindow(xdisp
, xwindow
);
446 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
447 XUnmapWindow(xdisp
, xwindow
);
453 // Raise the window to the top of the Z order
454 void wxWindowX11::Raise()
457 XRaiseWindow( wxGlobalDisplay(), (Window
) m_mainWindow
);
460 // Lower the window to the bottom of the Z order
461 void wxWindowX11::Lower()
464 XLowerWindow( wxGlobalDisplay(), (Window
) m_mainWindow
);
467 void wxWindowX11::DoCaptureMouse()
469 if ((g_captureWindow
!= NULL
) && (g_captureWindow
!= this))
471 wxASSERT_MSG(FALSE
, "Trying to capture before mouse released.");
482 Window xwindow
= (Window
) m_clientWindow
;
484 wxCHECK_RET( xwindow
, wxT("invalid window") );
486 g_captureWindow
= (wxWindow
*) this;
490 int res
= XGrabPointer(wxGlobalDisplay(), xwindow
,
492 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
496 None
, /* cursor */ // TODO: This may need to be set to the cursor of this window
499 if (res
!= GrabSuccess
)
502 msg
.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
504 if (res
== GrabNotViewable
)
505 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
507 g_captureWindow
= NULL
;
511 m_winCaptured
= TRUE
;
515 void wxWindowX11::DoReleaseMouse()
517 g_captureWindow
= NULL
;
519 if ( !m_winCaptured
)
522 Window xwindow
= (Window
) m_clientWindow
;
526 XUngrabPointer( wxGlobalDisplay(), CurrentTime
);
529 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
531 m_winCaptured
= FALSE
;
534 bool wxWindowX11::SetFont(const wxFont
& font
)
536 if ( !wxWindowBase::SetFont(font
) )
545 bool wxWindowX11::SetCursor(const wxCursor
& cursor
)
547 if ( !wxWindowBase::SetCursor(cursor
) )
553 Window xwindow
= (Window
) m_clientWindow
;
555 wxCHECK_MSG( xwindow
, FALSE
, wxT("invalid window") );
557 wxCursor cursorToUse
;
559 cursorToUse
= m_cursor
;
561 cursorToUse
= *wxSTANDARD_CURSOR
;
563 Cursor xcursor
= (Cursor
) cursorToUse
.GetCursor();
565 XDefineCursor( wxGlobalDisplay(), xwindow
, xcursor
);
570 // Coordinates relative to the window
571 void wxWindowX11::WarpPointer (int x
, int y
)
573 Window xwindow
= (Window
) m_clientWindow
;
575 wxCHECK_RET( xwindow
, wxT("invalid window") );
577 XWarpPointer( wxGlobalDisplay(), None
, xwindow
, 0, 0, 0, 0, x
, y
);
580 // Does a physical scroll
581 void wxWindowX11::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
583 // No scrolling requested.
584 if ((dx
== 0) && (dy
== 0)) return;
586 if (!m_updateRegion
.IsEmpty())
588 m_updateRegion
.Offset( dx
, dy
);
592 GetSize( &cw
, &ch
); // GetClientSize() ??
593 m_updateRegion
.Intersect( 0, 0, cw
, ch
);
596 if (!m_clearRegion
.IsEmpty())
598 m_clearRegion
.Offset( dx
, dy
);
602 GetSize( &cw
, &ch
); // GetClientSize() ??
603 m_clearRegion
.Intersect( 0, 0, cw
, ch
);
606 Window xwindow
= (Window
) GetClientAreaWindow();
608 wxCHECK_RET( xwindow
, wxT("invalid window") );
610 Display
*xdisplay
= wxGlobalDisplay();
612 GC xgc
= XCreateGC( xdisplay
, xwindow
, 0, NULL
);
613 XSetGraphicsExposures( xdisplay
, xgc
, True
);
631 GetClientSize( &cw
, &ch
);
634 #if wxUSE_TWO_WINDOWS
635 wxPoint
offset( 0,0 );
637 wxPoint offset
= GetClientAreaOrigin();
642 int w
= cw
- abs(dx
);
643 int h
= ch
- abs(dy
);
645 if ((h
< 0) || (w
< 0))
652 if (dx
< 0) rect
.x
= cw
+dx
+ offset
.x
; else rect
.x
= s_x
;
653 if (dy
< 0) rect
.y
= ch
+dy
+ offset
.y
; else rect
.y
= s_y
;
654 if (dy
!= 0) rect
.width
= cw
; else rect
.width
= abs(dx
);
655 if (dx
!= 0) rect
.height
= ch
; else rect
.height
= abs(dy
);
660 if (dx
< 0) s_x
+= -dx
;
661 if (dy
< 0) s_y
+= -dy
;
662 if (dx
> 0) d_x
= dx
+ offset
.x
;
663 if (dy
> 0) d_y
= dy
+ offset
.y
;
665 XCopyArea( xdisplay
, xwindow
, xwindow
, xgc
, s_x
, s_y
, w
, h
, d_x
, d_y
);
667 // 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 );
669 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
671 m_updateRegion
.Union( rect
);
672 m_clearRegion
.Union( rect
);
675 XFreeGC( xdisplay
, xgc
);
678 // ---------------------------------------------------------------------------
680 // ---------------------------------------------------------------------------
682 #if wxUSE_DRAG_AND_DROP
684 void wxWindowX11::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
691 // Old style file-manager drag&drop
692 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept
))
697 // ----------------------------------------------------------------------------
699 // ----------------------------------------------------------------------------
703 void wxWindowX11::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
708 #endif // wxUSE_TOOLTIPS
710 // ---------------------------------------------------------------------------
711 // moving and resizing
712 // ---------------------------------------------------------------------------
714 bool wxWindowX11::PreResize()
720 void wxWindowX11::DoGetSize(int *x
, int *y
) const
722 Window xwindow
= (Window
) m_mainWindow
;
724 wxCHECK_RET( xwindow
, wxT("invalid window") );
726 //XSync(wxGlobalDisplay(), False);
728 XWindowAttributes attr
;
729 Status status
= XGetWindowAttributes( wxGlobalDisplay(), xwindow
, &attr
);
734 *x
= attr
.width
/* + 2*m_borderSize */ ;
735 *y
= attr
.height
/* + 2*m_borderSize */ ;
739 void wxWindowX11::DoGetPosition(int *x
, int *y
) const
741 Window window
= (Window
) m_mainWindow
;
744 //XSync(wxGlobalDisplay(), False);
745 XWindowAttributes attr
;
746 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
754 // We may be faking the client origin. So a window that's really at (0, 30)
755 // may appear (to wxWin apps) to be at (0, 0).
758 wxPoint
pt(GetParent()->GetClientAreaOrigin());
766 void wxWindowX11::DoScreenToClient(int *x
, int *y
) const
768 Display
*display
= wxGlobalDisplay();
769 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
770 Window thisWindow
= (Window
) m_clientWindow
;
775 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
778 void wxWindowX11::DoClientToScreen(int *x
, int *y
) const
780 Display
*display
= wxGlobalDisplay();
781 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
782 Window thisWindow
= (Window
) m_clientWindow
;
787 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
791 // Get size *available for subwindows* i.e. excluding menu bar etc.
792 void wxWindowX11::DoGetClientSize(int *x
, int *y
) const
794 Window window
= (Window
) m_mainWindow
;
798 XWindowAttributes attr
;
799 Status status
= XGetWindowAttributes( wxGlobalDisplay(), window
, &attr
);
810 void wxWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
812 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
814 Window xwindow
= (Window
) m_mainWindow
;
816 wxCHECK_RET( xwindow
, wxT("invalid window") );
818 XWindowAttributes attr
;
819 Status status
= XGetWindowAttributes( wxGlobalDisplay(), xwindow
, &attr
);
820 wxCHECK_RET( status
, wxT("invalid window attributes") );
824 int new_w
= attr
.width
;
825 int new_h
= attr
.height
;
827 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
830 AdjustForParentClientOrigin( x
, yy
, sizeFlags
);
833 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
836 AdjustForParentClientOrigin( xx
, y
, sizeFlags
);
852 DoMoveWindow( new_x
, new_y
, new_w
, new_h
);
855 void wxWindowX11::DoSetClientSize(int width
, int height
)
857 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
859 Window xwindow
= (Window
) m_mainWindow
;
861 wxCHECK_RET( xwindow
, wxT("invalid window") );
863 XResizeWindow( wxGlobalDisplay(), xwindow
, width
, height
);
865 if (m_mainWindow
!= m_clientWindow
)
867 xwindow
= (Window
) m_clientWindow
;
869 wxWindow
*window
= (wxWindow
*) this;
870 wxRenderer
*renderer
= window
->GetRenderer();
873 wxRect border
= renderer
->GetBorderDimensions( (wxBorder
)(m_windowStyle
& wxBORDER_MASK
) );
874 width
-= border
.x
+ border
.width
;
875 height
-= border
.y
+ border
.height
;
878 XResizeWindow( wxGlobalDisplay(), xwindow
, width
, height
);
882 // For implementation purposes - sometimes decorations make the client area
884 wxPoint
wxWindowX11::GetClientAreaOrigin() const
886 return wxPoint(0, 0);
889 void wxWindowX11::DoMoveWindow(int x
, int y
, int width
, int height
)
891 Window xwindow
= (Window
) m_mainWindow
;
893 wxCHECK_RET( xwindow
, wxT("invalid window") );
897 XMoveResizeWindow( wxGlobalDisplay(), xwindow
, x
, y
, width
, height
);
898 if (m_mainWindow
!= m_clientWindow
)
900 xwindow
= (Window
) m_clientWindow
;
902 wxWindow
*window
= (wxWindow
*) this;
903 wxRenderer
*renderer
= window
->GetRenderer();
906 wxRect border
= renderer
->GetBorderDimensions( (wxBorder
)(m_windowStyle
& wxBORDER_MASK
) );
909 width
-= border
.x
+ border
.width
;
910 height
-= border
.y
+ border
.height
;
918 wxScrollBar
*sb
= window
->GetScrollbar( wxHORIZONTAL
);
919 if (sb
&& sb
->IsShown())
921 wxSize size
= sb
->GetSize();
924 sb
= window
->GetScrollbar( wxVERTICAL
);
925 if (sb
&& sb
->IsShown())
927 wxSize size
= sb
->GetSize();
931 XMoveResizeWindow( wxGlobalDisplay(), xwindow
, x
, y
, wxMax(1, width
), wxMax(1, height
) );
936 XWindowChanges windowChanges
;
939 windowChanges
.width
= width
;
940 windowChanges
.height
= height
;
941 windowChanges
.stack_mode
= 0;
942 int valueMask
= CWX
| CWY
| CWWidth
| CWHeight
;
944 XConfigureWindow( wxGlobalDisplay(), xwindow
, valueMask
, &windowChanges
);
949 void wxWindowX11::SetSizeHints(int minW
, int minH
, int maxW
, int maxH
, int incW
, int incH
)
957 XSizeHints sizeHints
;
960 if (minW
> -1 && minH
> -1)
962 sizeHints
.flags
|= PMinSize
;
963 sizeHints
.min_width
= minW
;
964 sizeHints
.min_height
= minH
;
966 if (maxW
> -1 && maxH
> -1)
968 sizeHints
.flags
|= PMaxSize
;
969 sizeHints
.max_width
= maxW
;
970 sizeHints
.max_height
= maxH
;
972 if (incW
> -1 && incH
> -1)
974 sizeHints
.flags
|= PResizeInc
;
975 sizeHints
.width_inc
= incW
;
976 sizeHints
.height_inc
= incH
;
979 XSetWMNormalHints(wxGlobalDisplay(), (Window
) m_mainWindow
, &sizeHints
);
983 // ---------------------------------------------------------------------------
985 // ---------------------------------------------------------------------------
987 int wxWindowX11::GetCharHeight() const
989 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
992 // There should be an easier way.
993 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
994 pango_layout_set_font_description( layout
, GetFont().GetNativeFontInfo()->description
);
995 pango_layout_set_text(layout
, "H", 1 );
997 pango_layout_get_pixel_size(layout
, &w
, &h
);
998 g_object_unref( G_OBJECT( layout
) );
1002 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, wxGlobalDisplay());
1004 int direction
, ascent
, descent
;
1005 XCharStruct overall
;
1006 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1007 &descent
, &overall
);
1009 // return (overall.ascent + overall.descent);
1010 return (ascent
+ descent
);
1014 int wxWindowX11::GetCharWidth() const
1016 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1019 // There should be an easier way.
1020 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
1021 pango_layout_set_font_description( layout
, GetFont().GetNativeFontInfo()->description
);
1022 pango_layout_set_text(layout
, "H", 1 );
1024 pango_layout_get_pixel_size(layout
, &w
, &h
);
1025 g_object_unref( G_OBJECT( layout
) );
1029 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, wxGlobalDisplay());
1031 int direction
, ascent
, descent
;
1032 XCharStruct overall
;
1033 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1034 &descent
, &overall
);
1036 return overall
.width
;
1040 void wxWindowX11::GetTextExtent(const wxString
& string
,
1042 int *descent
, int *externalLeading
,
1043 const wxFont
*theFont
) const
1045 wxFont fontToUse
= m_font
;
1046 if (theFont
) fontToUse
= *theFont
;
1048 wxCHECK_RET( fontToUse
.Ok(), wxT("invalid font") );
1050 if (string
.IsEmpty())
1058 PangoLayout
*layout
= pango_layout_new( wxTheApp
->GetPangoContext() );
1060 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
1061 pango_layout_set_font_description(layout
, desc
);
1063 const wxCharBuffer data
= wxConvUTF8
.cWC2MB( string
);
1064 pango_layout_set_text(layout
, (const char*) data
, strlen( (const char*) data
));
1066 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
1069 PangoRectangle rect
;
1070 pango_layout_line_get_extents(line
, NULL
, &rect
);
1072 if (x
) (*x
) = (wxCoord
) (rect
.width
/ PANGO_SCALE
);
1073 if (y
) (*y
) = (wxCoord
) (rect
.height
/ PANGO_SCALE
);
1076 // Do something about metrics here
1079 if (externalLeading
) (*externalLeading
) = 0; // ??
1081 g_object_unref( G_OBJECT( layout
) );
1083 WXFontStructPtr pFontStruct
= fontToUse
.GetFontStruct(1.0, wxGlobalDisplay());
1085 int direction
, ascent
, descent2
;
1086 XCharStruct overall
;
1087 int slen
= string
.Len();
1089 XTextExtents((XFontStruct
*) pFontStruct
, (char*) string
.c_str(), slen
,
1090 &direction
, &ascent
, &descent2
, &overall
);
1093 *x
= (overall
.width
);
1095 *y
= (ascent
+ descent2
);
1097 *descent
= descent2
;
1098 if (externalLeading
)
1099 *externalLeading
= 0;
1103 // ----------------------------------------------------------------------------
1105 // ----------------------------------------------------------------------------
1107 void wxWindowX11::Refresh(bool eraseBack
, const wxRect
*rect
)
1113 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1114 m_clearRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
1119 GetSize( &width
, &height
);
1121 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1122 m_clearRegion
.Clear();
1123 m_clearRegion
.Union( 0, 0, width
, height
);
1129 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1130 m_updateRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
1135 GetSize( &width
, &height
);
1137 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1138 m_updateRegion
.Clear();
1139 m_updateRegion
.Union( 0, 0, width
, height
);
1143 void wxWindowX11::Update()
1147 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1148 // Send nc paint events.
1149 SendNcPaintEvents();
1152 if (!m_updateRegion
.IsEmpty())
1154 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1155 // Actually send erase events.
1158 // Actually send paint events.
1163 void wxWindowX11::Clear()
1165 // wxClientDC dc((wxWindow*) this);
1166 // wxBrush brush(GetBackgroundColour(), wxSOLID);
1167 // dc.SetBackground(brush);
1171 void wxWindowX11::SendEraseEvents()
1173 if (m_clearRegion
.IsEmpty()) return;
1175 wxClientDC
dc( (wxWindow
*)this );
1176 dc
.SetClippingRegion( m_clearRegion
);
1178 wxEraseEvent
erase_event( GetId(), &dc
);
1179 erase_event
.SetEventObject( this );
1181 if (!GetEventHandler()->ProcessEvent(erase_event
) )
1183 Display
*xdisplay
= wxGlobalDisplay();
1184 Window xwindow
= (Window
) GetClientAreaWindow();
1185 XSetForeground( xdisplay
, g_eraseGC
, m_backgroundColour
.GetPixel() );
1187 wxRegionIterator
upd( m_clearRegion
);
1190 XFillRectangle( xdisplay
, xwindow
, g_eraseGC
,
1191 upd
.GetX(), upd
.GetY(), upd
.GetWidth(), upd
.GetHeight() );
1196 m_clearRegion
.Clear();
1199 void wxWindowX11::SendPaintEvents()
1201 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1203 m_clipPaintRegion
= TRUE
;
1205 wxPaintEvent
paint_event( GetId() );
1206 paint_event
.SetEventObject( this );
1207 GetEventHandler()->ProcessEvent( paint_event
);
1209 m_updateRegion
.Clear();
1211 m_clipPaintRegion
= FALSE
;
1214 void wxWindowX11::SendNcPaintEvents()
1216 wxWindow
*window
= (wxWindow
*) this;
1218 // All this for drawing the small square between the scrollbars.
1223 wxScrollBar
*sb
= window
->GetScrollbar( wxHORIZONTAL
);
1224 if (sb
&& sb
->IsShown())
1226 height
= sb
->GetSize().y
;
1227 y
= sb
->GetPosition().y
;
1229 sb
= window
->GetScrollbar( wxVERTICAL
);
1230 if (sb
&& sb
->IsShown())
1232 width
= sb
->GetSize().x
;
1233 x
= sb
->GetPosition().x
;
1235 Display
*xdisplay
= wxGlobalDisplay();
1236 Window xwindow
= (Window
) GetMainWindow();
1237 Colormap cm
= (Colormap
) wxTheApp
->GetMainColormap( wxGetDisplay() );
1238 wxColour colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
);
1239 colour
.CalcPixel( (WXColormap
) cm
);
1241 XSetForeground( xdisplay
, g_eraseGC
, colour
.GetPixel() );
1243 XFillRectangle( xdisplay
, xwindow
, g_eraseGC
, x
, y
, width
, height
);
1247 wxNcPaintEvent
nc_paint_event( GetId() );
1248 nc_paint_event
.SetEventObject( this );
1249 GetEventHandler()->ProcessEvent( nc_paint_event
);
1251 m_updateNcArea
= FALSE
;
1254 // ----------------------------------------------------------------------------
1256 // ----------------------------------------------------------------------------
1258 // Responds to colour changes: passes event on to children.
1259 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1261 wxWindowList::Node
*node
= GetChildren().GetFirst();
1264 // Only propagate to non-top-level windows
1265 wxWindow
*win
= node
->GetData();
1266 if ( win
->GetParent() )
1268 wxSysColourChangedEvent event2
;
1269 event
.m_eventObject
= win
;
1270 win
->GetEventHandler()->ProcessEvent(event2
);
1273 node
= node
->GetNext();
1277 // See handler for InFocus case in app.cpp for details.
1278 wxWindow
* g_GettingFocus
= NULL
;
1280 void wxWindowX11::OnInternalIdle()
1282 // Update invalidated regions.
1285 // This calls the UI-update mechanism (querying windows for
1286 // menu/toolbar/control state information)
1289 // Set the input focus if couldn't do it before
1290 if (m_needsInputFocus
)
1294 msg
.Printf("Setting focus for %s from OnInternalIdle\n", GetClassInfo()->GetClassName());
1295 printf(msg
.c_str());
1299 // If it couldn't set the focus now, there's
1300 // no point in trying again.
1301 m_needsInputFocus
= FALSE
;
1303 g_GettingFocus
= NULL
;
1306 // ----------------------------------------------------------------------------
1307 // function which maintain the global hash table mapping Widgets to wxWindows
1308 // ----------------------------------------------------------------------------
1310 bool wxAddWindowToTable(Window w
, wxWindow
*win
)
1312 wxWindow
*oldItem
= NULL
;
1313 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1315 wxLogDebug( wxT("Widget table clash: new widget is %ld, %s"),
1316 (long)w
, win
->GetClassInfo()->GetClassName());
1320 wxWidgetHashTable
->Put((long) w
, win
);
1322 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1323 (unsigned int) w
, win
, win
->GetClassInfo()->GetClassName());
1328 wxWindow
*wxGetWindowFromTable(Window w
)
1330 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1333 void wxDeleteWindowFromTable(Window w
)
1335 wxWidgetHashTable
->Delete((long)w
);
1338 // ----------------------------------------------------------------------------
1339 // function which maintain the global hash table mapping client widgets
1340 // ----------------------------------------------------------------------------
1342 bool wxAddClientWindowToTable(Window w
, wxWindow
*win
)
1344 wxWindow
*oldItem
= NULL
;
1345 if ((oldItem
= (wxWindow
*)wxClientWidgetHashTable
->Get ((long) w
)))
1347 wxLogDebug( wxT("Client window table clash: new window is %ld, %s"),
1348 (long)w
, win
->GetClassInfo()->GetClassName());
1352 wxClientWidgetHashTable
->Put((long) w
, win
);
1354 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1355 (unsigned int) w
, win
, win
->GetClassInfo()->GetClassName());
1360 wxWindow
*wxGetClientWindowFromTable(Window w
)
1362 return (wxWindow
*)wxClientWidgetHashTable
->Get((long) w
);
1365 void wxDeleteClientWindowFromTable(Window w
)
1367 wxClientWidgetHashTable
->Delete((long)w
);
1370 // ----------------------------------------------------------------------------
1371 // add/remove window from the table
1372 // ----------------------------------------------------------------------------
1374 // ----------------------------------------------------------------------------
1375 // X11-specific accessors
1376 // ----------------------------------------------------------------------------
1378 WXWindow
wxWindowX11::GetMainWindow() const
1380 return m_mainWindow
;
1383 WXWindow
wxWindowX11::GetClientAreaWindow() const
1385 return m_clientWindow
;
1388 // ----------------------------------------------------------------------------
1389 // TranslateXXXEvent() functions
1390 // ----------------------------------------------------------------------------
1392 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
, Window window
, XEvent
*xevent
)
1394 switch (XEventGetType(xevent
))
1402 wxEventType eventType
= wxEVT_NULL
;
1404 if (XEventGetType(xevent
) == EnterNotify
)
1406 //if (local_event.xcrossing.mode!=NotifyNormal)
1407 // return ; // Ignore grab events
1408 eventType
= wxEVT_ENTER_WINDOW
;
1409 // canvas->GetEventHandler()->OnSetFocus();
1411 else if (XEventGetType(xevent
) == LeaveNotify
)
1413 //if (local_event.xcrossingr.mode!=NotifyNormal)
1414 // return ; // Ignore grab events
1415 eventType
= wxEVT_LEAVE_WINDOW
;
1416 // canvas->GetEventHandler()->OnKillFocus();
1418 else if (XEventGetType(xevent
) == MotionNotify
)
1420 eventType
= wxEVT_MOTION
;
1422 else if (XEventGetType(xevent
) == ButtonPress
)
1424 wxevent
.SetTimestamp(XButtonEventGetTime(xevent
));
1426 if (XButtonEventLChanged(xevent
))
1428 eventType
= wxEVT_LEFT_DOWN
;
1431 else if (XButtonEventMChanged(xevent
))
1433 eventType
= wxEVT_MIDDLE_DOWN
;
1436 else if (XButtonEventRChanged(xevent
))
1438 eventType
= wxEVT_RIGHT_DOWN
;
1442 // check for a double click
1443 // TODO: where can we get this value from?
1444 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1445 long dclickTime
= 200;
1446 long ts
= wxevent
.GetTimestamp();
1448 int buttonLast
= win
->GetLastClickedButton();
1449 long lastTS
= win
->GetLastClickTime();
1450 if ( buttonLast
&& buttonLast
== button
&& (ts
- lastTS
) < dclickTime
)
1453 win
->SetLastClick(0, ts
);
1454 if ( eventType
== wxEVT_LEFT_DOWN
)
1455 eventType
= wxEVT_LEFT_DCLICK
;
1456 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
1457 eventType
= wxEVT_MIDDLE_DCLICK
;
1458 else if ( eventType
== wxEVT_RIGHT_DOWN
)
1459 eventType
= wxEVT_RIGHT_DCLICK
;
1463 // not fast enough or different button
1464 win
->SetLastClick(button
, ts
);
1467 else if (XEventGetType(xevent
) == ButtonRelease
)
1469 if (XButtonEventLChanged(xevent
))
1471 eventType
= wxEVT_LEFT_UP
;
1473 else if (XButtonEventMChanged(xevent
))
1475 eventType
= wxEVT_MIDDLE_UP
;
1477 else if (XButtonEventRChanged(xevent
))
1479 eventType
= wxEVT_RIGHT_UP
;
1488 wxevent
.SetEventType(eventType
);
1490 wxevent
.m_x
= XButtonEventGetX(xevent
);
1491 wxevent
.m_y
= XButtonEventGetY(xevent
);
1493 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
1494 || (XButtonEventLIsDown(xevent
)
1495 && (eventType
!= wxEVT_LEFT_UP
)));
1496 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
1497 || (XButtonEventMIsDown(xevent
)
1498 && (eventType
!= wxEVT_MIDDLE_UP
)));
1499 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
1500 || (XButtonEventRIsDown (xevent
)
1501 && (eventType
!= wxEVT_RIGHT_UP
)));
1503 wxevent
.m_shiftDown
= XButtonEventShiftIsDown(xevent
);
1504 wxevent
.m_controlDown
= XButtonEventCtrlIsDown(xevent
);
1505 wxevent
.m_altDown
= XButtonEventAltIsDown(xevent
);
1506 wxevent
.m_metaDown
= XButtonEventMetaIsDown(xevent
);
1508 wxevent
.SetId(win
->GetId());
1509 wxevent
.SetEventObject(win
);
1517 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
, Window
WXUNUSED(win
), XEvent
*xevent
, bool isAscii
)
1519 switch (XEventGetType(xevent
))
1527 (void) XLookupString ((XKeyEvent
*) xevent
, buf
, 20, &keySym
, NULL
);
1528 int id
= wxCharCodeXToWX (keySym
);
1529 // id may be WXK_xxx code - these are outside ASCII range, so we
1530 // can't just use toupper() on id.
1531 // Only change this if we want the raw key that was pressed,
1532 // and don't change it if we want an ASCII value.
1533 if (!isAscii
&& (id
>= 'a' && id
<= 'z'))
1535 id
= id
+ 'A' - 'a';
1538 wxevent
.m_shiftDown
= XKeyEventShiftIsDown(xevent
);
1539 wxevent
.m_controlDown
= XKeyEventCtrlIsDown(xevent
);
1540 wxevent
.m_altDown
= XKeyEventAltIsDown(xevent
);
1541 wxevent
.m_metaDown
= XKeyEventMetaIsDown(xevent
);
1542 wxevent
.SetEventObject(win
);
1543 wxevent
.m_keyCode
= id
;
1544 wxevent
.SetTimestamp(XKeyEventGetTime(xevent
));
1546 wxevent
.m_x
= XKeyEventGetX(xevent
);
1547 wxevent
.m_y
= XKeyEventGetY(xevent
);
1557 // ----------------------------------------------------------------------------
1559 // ----------------------------------------------------------------------------
1561 bool wxWindowX11::SetBackgroundColour(const wxColour
& col
)
1563 wxWindowBase::SetBackgroundColour(col
);
1565 Display
*xdisplay
= (Display
*) wxGlobalDisplay();
1566 int xscreen
= DefaultScreen( xdisplay
);
1567 Colormap cm
= DefaultColormap( xdisplay
, xscreen
);
1569 m_backgroundColour
.CalcPixel( (WXColormap
) cm
);
1571 // We don't set the background colour as we paint
1572 // the background ourselves.
1573 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1578 bool wxWindowX11::SetForegroundColour(const wxColour
& col
)
1580 if ( !wxWindowBase::SetForegroundColour(col
) )
1586 // ----------------------------------------------------------------------------
1588 // ----------------------------------------------------------------------------
1590 wxWindow
*wxGetActiveWindow()
1593 wxFAIL_MSG("Not implemented");
1598 wxWindow
*wxWindowBase::GetCapture()
1600 return (wxWindow
*)g_captureWindow
;
1604 // Find the wxWindow at the current mouse position, returning the mouse
1606 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
1608 return wxFindWindowAtPoint(wxGetMousePosition());
1611 // Get the current mouse position.
1612 wxPoint
wxGetMousePosition()
1616 return wxPoint(0, 0);
1618 Display
*display
= wxGlobalDisplay();
1619 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
1620 Window rootReturn
, childReturn
;
1621 int rootX
, rootY
, winX
, winY
;
1622 unsigned int maskReturn
;
1624 XQueryPointer (display
,
1628 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
1629 return wxPoint(rootX
, rootY
);
1634 // ----------------------------------------------------------------------------
1635 // wxNoOptimize: switch off size optimization
1636 // ----------------------------------------------------------------------------
1638 int wxNoOptimize::ms_count
= 0;
1641 // ----------------------------------------------------------------------------
1643 // ----------------------------------------------------------------------------
1645 class wxWinModule
: public wxModule
1652 DECLARE_DYNAMIC_CLASS(wxWinModule
)
1655 IMPLEMENT_DYNAMIC_CLASS(wxWinModule
, wxModule
)
1657 bool wxWinModule::OnInit()
1659 Display
*xdisplay
= wxGlobalDisplay();
1660 int xscreen
= DefaultScreen( xdisplay
);
1661 Window xroot
= RootWindow( xdisplay
, xscreen
);
1662 g_eraseGC
= XCreateGC( xdisplay
, xroot
, 0, NULL
);
1663 XSetFillStyle( xdisplay
, g_eraseGC
, FillSolid
);
1668 void wxWinModule::OnExit()
1670 Display
*xdisplay
= wxGlobalDisplay();
1671 XFreeGC( xdisplay
, g_eraseGC
);