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/module.h"
40 #include "wx/menuitem.h"
43 #if wxUSE_DRAG_AND_DROP
47 #include "wx/x11/private.h"
48 #include "X11/Xutil.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static const int SCROLL_MARGIN
= 4;
58 // ----------------------------------------------------------------------------
59 // global variables for this module
60 // ----------------------------------------------------------------------------
62 extern wxHashTable
*wxWidgetHashTable
;
63 static wxWindow
* g_captureWindow
= NULL
;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
70 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
71 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11
, wxWindowBase
)
79 BEGIN_EVENT_TABLE(wxWindowX11
, wxWindowBase
)
80 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged
)
81 EVT_IDLE(wxWindowX11::OnIdle
)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 void wxWindowX11::Init()
98 // generic initializations first
102 m_mainWidget
= (WXWindow
) 0;
104 m_winCaptured
= FALSE
;
107 m_isBeingDeleted
= FALSE
;
113 // real construction (Init() must have been called before!)
114 bool wxWindowX11::Create(wxWindow
*parent
, wxWindowID id
,
118 const wxString
& name
)
120 wxCHECK_MSG( parent
, FALSE
, "can't create wxWindow without parent" );
122 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
125 parent
->AddChild(this);
127 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
128 m_foregroundColour
= *wxBLACK
;
130 int w
= size
.GetWidth();
131 int h
= size
.GetHeight();
139 int screen
= DefaultScreen(wxGlobalDisplay());
143 parentWindow
= (Window
) parent
->GetMainWindow();
145 parentWindow
= RootWindow(wxGlobalDisplay(), screen
);
147 Window window
= XCreateSimpleWindow(wxGlobalDisplay(), parentWindow
,
149 m_backgroundColour
.AllocColour(wxGlobalDisplay()),
150 m_foregroundColour
.AllocColour(wxGlobalDisplay()));
152 // Select event types wanted
153 XSelectInput(wxGlobalDisplay(), window
,
154 ExposureMask
| KeyPressMask
| KeyReleaseMask
| ButtonPressMask
| ButtonReleaseMask
|
155 ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
|
156 KeymapStateMask
| FocusChangeMask
| ColormapChangeMask
| StructureNotifyMask
|
159 wxAddWindowToTable(window
, (wxWindow
*) this);
161 // Is a subwindow, so map immediately
163 XMapWindow(wxGlobalDisplay(), window
);
165 // Without this, the cursor may not be restored properly (e.g. in splitter
167 SetCursor(*wxSTANDARD_CURSOR
);
168 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
169 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
175 wxWindowX11::~wxWindowX11()
177 if (g_captureWindow
== this)
178 g_captureWindow
= NULL
;
180 m_isBeingDeleted
= TRUE
;
182 // X11-specific actions first
183 Window main
= (Window
) m_mainWidget
;
186 // Removes event handlers
187 //DetachWidget(main);
191 m_parent
->RemoveChild( this );
195 // Destroy the window
198 XSelectInput( wxGlobalDisplay(), main
, NoEventMask
);
199 wxDeleteWindowFromTable( main
);
200 XDestroyWindow( wxGlobalDisplay(), main
);
205 // ---------------------------------------------------------------------------
207 // ---------------------------------------------------------------------------
209 void wxWindowX11::SetFocus()
211 Window wMain
= (Window
) GetMainWindow();
214 XSetInputFocus(wxGlobalDisplay(), wMain
, RevertToParent
, CurrentTime
);
217 wmhints
.flags
= InputHint
;
218 wmhints
.input
= True
;
219 XSetWMHints(wxGlobalDisplay(), wMain
, &wmhints
)
223 // Get the window with the focus
224 wxWindow
*wxWindowBase::FindFocus()
226 Window wFocus
= (Window
) 0;
229 XGetInputFocus(wxGlobalDisplay(), & wFocus
, & revert
);
232 wxWindow
*win
= NULL
;
235 win
= wxGetWindowFromTable(wFocus
);
236 wFocus
= wxGetWindowParent(wFocus
);
237 } while (wFocus
&& !win
);
245 // Enabling/disabling handled by event loop, and not sending events
247 bool wxWindowX11::Enable(bool enable
)
249 if ( !wxWindowBase::Enable(enable
) )
255 bool wxWindowX11::Show(bool show
)
257 if ( !wxWindowBase::Show(show
) )
260 Window xwin
= (Window
) GetXWindow();
261 Display
*xdisp
= (Display
*) GetXDisplay();
264 XMapWindow(xdisp
, xwin
);
268 XUnmapWindow(xdisp
, xwin
);
274 // Raise the window to the top of the Z order
275 void wxWindowX11::Raise()
278 XRaiseWindow( wxGlobalDisplay(), (Window
) m_mainWidget
);
281 // Lower the window to the bottom of the Z order
282 void wxWindowX11::Lower()
285 XLowerWindow( wxGlobalDisplay(), (Window
) m_mainWidget
);
288 void wxWindowX11::DoCaptureMouse()
290 g_captureWindow
= (wxWindow
*) this;
296 int res
= XGrabPointer(wxGlobalDisplay(), (Window
) GetMainWindow(),
298 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
| EnterWindowMask
| LeaveWindowMask
| PointerMotionMask
,
302 None
, /* cursor */ // TODO: This may need to be set to the cursor of this window
305 if (res
!= GrabSuccess
)
307 wxLogDebug("Failed to grab pointer.");
311 res
= XGrabButton(wxGlobalDisplay(), AnyButton
, AnyModifier
,
312 (Window
) GetMainWindow(),
314 ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
,
320 if (res
!= GrabSuccess
)
322 wxLogDebug("Failed to grab mouse buttons.");
323 XUngrabPointer(wxGlobalDisplay(), CurrentTime
);
327 res
= XGrabKeyboard(wxGlobalDisplay(), (Window
) GetMainWindow(),
329 ShiftMask
, LockMask
, Control
-Mask
, Mod1Mask
, Mod2Mask
, Mod3Mask
, Mod4Mask
, and Mod5Mask
.,
334 if (res
!= GrabSuccess
)
336 wxLogDebug("Failed to grab keyboard.");
337 XUngrabPointer(wxGlobalDisplay(), CurrentTime
);
338 XUngrabButton(wxGlobalDisplay(), AnyButton
, AnyModifier
,
339 (Window
) GetMainWindow());
343 m_winCaptured
= TRUE
;
347 void wxWindowX11::DoReleaseMouse()
349 g_captureWindow
= NULL
;
350 if ( !m_winCaptured
)
353 Window wMain
= (Window
)GetMainWindow();
357 XUngrabPointer(wxGlobalDisplay(), wMain
);
358 XUngrabButton(wxGlobalDisplay(), AnyButton
, AnyModifier
,
360 XUngrabKeyboard(wxGlobalDisplay(), CurrentTime
);
363 m_winCaptured
= FALSE
;
366 bool wxWindowX11::SetFont(const wxFont
& font
)
368 if ( !wxWindowBase::SetFont(font
) )
377 bool wxWindowX11::SetCursor(const wxCursor
& cursor
)
379 if ( !wxWindowBase::SetCursor(cursor
) )
385 wxCursor
* cursor2
= NULL
;
387 cursor2
= & m_cursor
;
389 cursor2
= wxSTANDARD_CURSOR
;
391 WXDisplay
*dpy
= GetXDisplay();
392 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
394 Window win
= (Window
) GetMainWindow();
395 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
400 // Coordinates relative to the window
401 void wxWindowX11::WarpPointer (int x
, int y
)
404 XWarpPointer( wxGlobalDisplay(), None
, (Window
) m_mainWidget
, 0, 0, 0, 0, x
, y
);
407 // Does a physical scroll
408 void wxWindowX11::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
413 // Use specified rectangle
414 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
418 // Use whole client area
420 GetClientSize(& w
, & h
);
423 wxNode
*cnode
= m_children
.First();
426 wxWindow
*child
= (wxWindow
*) cnode
->Data();
429 child
->GetSize( &sx
, &sy
);
430 wxPoint
pos( child
->GetPosition() );
431 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
432 cnode
= cnode
->Next();
435 int x1
= (dx
>= 0) ? x
: x
- dx
;
436 int y1
= (dy
>= 0) ? y
: y
- dy
;
437 int w1
= w
- abs(dx
);
438 int h1
= h
- abs(dy
);
439 int x2
= (dx
>= 0) ? x
+ dx
: x
;
440 int y2
= (dy
>= 0) ? y
+ dy
: y
;
442 wxClientDC
dc((wxWindow
*) this);
444 dc
.SetLogicalFunction (wxCOPY
);
446 Window window
= (Window
) GetMainWindow();
447 Display
* display
= wxGlobalDisplay();
449 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
450 x1
, y1
, w1
, h1
, x2
, y2
);
452 dc
.SetAutoSetting(TRUE
);
453 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
454 dc
.SetBrush(brush
); // FIXME: needed?
456 // We'll add rectangles to the list of update rectangles according to which
457 // bits we've exposed.
462 wxRect
*rect
= new wxRect
;
468 XFillRectangle(display
, window
,
469 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
473 rect
->width
= rect
->width
;
474 rect
->height
= rect
->height
;
476 updateRects
.Append((wxObject
*) rect
);
480 wxRect
*rect
= new wxRect
;
482 rect
->x
= x
+ w
+ dx
;
487 XFillRectangle(display
, window
,
488 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
493 rect
->width
= rect
->width
;
494 rect
->height
= rect
->height
;
496 updateRects
.Append((wxObject
*) rect
);
500 wxRect
*rect
= new wxRect
;
507 XFillRectangle(display
, window
,
508 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
512 rect
->width
= rect
->width
;
513 rect
->height
= rect
->height
;
515 updateRects
.Append((wxObject
*) rect
);
519 wxRect
*rect
= new wxRect
;
522 rect
->y
= y
+ h
+ dy
;
526 XFillRectangle(display
, window
,
527 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
531 rect
->width
= rect
->width
;
532 rect
->height
= rect
->height
;
534 updateRects
.Append((wxObject
*) rect
);
536 dc
.SetBrush(wxNullBrush
);
538 // Now send expose events
540 wxNode
* node
= updateRects
.First();
543 wxRect
* rect
= (wxRect
*) node
->Data();
547 event
.display
= display
;
548 event
.send_event
= True
;
549 event
.window
= window
;
553 event
.width
= rect
->width
;
554 event
.height
= rect
->height
;
558 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
564 // Delete the update rects
565 node
= updateRects
.First();
568 wxRect
* rect
= (wxRect
*) node
->Data();
575 // XmUpdateDisplay((Widget) GetMainWidget());
578 // ---------------------------------------------------------------------------
580 // ---------------------------------------------------------------------------
582 #if wxUSE_DRAG_AND_DROP
584 void wxWindowX11::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
591 // Old style file-manager drag&drop
592 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept
))
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
603 void wxWindowX11::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
608 #endif // wxUSE_TOOLTIPS
610 // ---------------------------------------------------------------------------
611 // moving and resizing
612 // ---------------------------------------------------------------------------
614 bool wxWindowX11::PreResize()
620 void wxWindowX11::DoGetSize(int *x
, int *y
) const
622 Window window
= (Window
) m_mainWidget
;
625 XWindowAttributes attr
;
626 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
631 *x
= attr
.width
/* + 2*m_borderSize */ ;
632 *y
= attr
.height
/* + 2*m_borderSize */ ;
637 void wxWindowX11::DoGetPosition(int *x
, int *y
) const
639 Window window
= (Window
) m_mainWidget
;
642 XWindowAttributes attr
;
643 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
651 // We may be faking the client origin. So a window that's really at (0, 30)
652 // may appear (to wxWin apps) to be at (0, 0).
655 wxPoint
pt(GetParent()->GetClientAreaOrigin());
663 void wxWindowX11::DoScreenToClient(int *x
, int *y
) const
665 Display
*display
= wxGlobalDisplay();
666 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
667 Window thisWindow
= (Window
) m_mainWidget
;
672 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
675 void wxWindowX11::DoClientToScreen(int *x
, int *y
) const
677 Display
*display
= wxGlobalDisplay();
678 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
679 Window thisWindow
= (Window
) m_mainWidget
;
684 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
688 // Get size *available for subwindows* i.e. excluding menu bar etc.
689 void wxWindowX11::DoGetClientSize(int *x
, int *y
) const
691 Window window
= (Window
) m_mainWidget
;
695 XWindowAttributes attr
;
696 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
707 void wxWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
709 if (!GetMainWindow())
712 XWindowChanges windowChanges
;
715 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
720 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
725 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
727 windowChanges
.width
= width
/* - m_borderSize*2 */;
728 valueMask
|= CWWidth
;
730 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
732 windowChanges
.height
= height
/* -m_borderSize*2*/;
733 valueMask
|= CWHeight
;
735 AdjustForParentClientOrigin( x
, y
, sizeFlags
);
737 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
738 valueMask
, & windowChanges
);
741 void wxWindowX11::DoSetClientSize(int width
, int height
)
743 if (!GetMainWindow())
746 XWindowChanges windowChanges
;
751 windowChanges
.width
= width
;
752 valueMask
|= CWWidth
;
756 windowChanges
.height
= height
;
757 valueMask
|= CWHeight
;
759 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
760 valueMask
, & windowChanges
);
763 // For implementation purposes - sometimes decorations make the client area
765 wxPoint
wxWindowX11::GetClientAreaOrigin() const
767 return wxPoint(0, 0);
770 // Makes an adjustment to the window position (for example, a frame that has
771 // a toolbar that it manages itself).
772 void wxWindowX11::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
774 if (((sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) == 0) && GetParent())
776 wxPoint
pt(GetParent()->GetClientAreaOrigin());
777 x
+= pt
.x
; y
+= pt
.y
;
781 void wxWindowX11::SetSizeHints(int minW
, int minH
, int maxW
, int maxH
, int incW
, int incH
)
788 XSizeHints sizeHints
;
791 if (minW
> -1 && minH
> -1)
793 sizeHints
.flags
|= PMinSize
;
794 sizeHints
.min_width
= minW
;
795 sizeHints
.min_height
= minH
;
797 if (maxW
> -1 && maxH
> -1)
799 sizeHints
.flags
|= PMaxSize
;
800 sizeHints
.max_width
= maxW
;
801 sizeHints
.max_height
= maxH
;
803 if (incW
> -1 && incH
> -1)
805 sizeHints
.flags
|= PResizeInc
;
806 sizeHints
.width_inc
= incW
;
807 sizeHints
.height_inc
= incH
;
810 XSetWMNormalHints(wxGlobalDisplay(), (Window
) GetMainWindow(), & sizeHints
);
813 void wxWindowX11::DoMoveWindow(int x
, int y
, int width
, int height
)
815 DoSetSize(x
, y
, width
, height
);
818 // ---------------------------------------------------------------------------
820 // ---------------------------------------------------------------------------
822 int wxWindowX11::GetCharHeight() const
824 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
826 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
828 int direction
, ascent
, descent
;
830 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
833 // return (overall.ascent + overall.descent);
834 return (ascent
+ descent
);
837 int wxWindowX11::GetCharWidth() const
839 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
841 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
843 int direction
, ascent
, descent
;
845 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
848 return overall
.width
;
851 void wxWindowX11::GetTextExtent(const wxString
& string
,
853 int *descent
, int *externalLeading
,
854 const wxFont
*theFont
) const
856 wxFont
*fontToUse
= (wxFont
*)theFont
;
858 fontToUse
= (wxFont
*) & m_font
;
860 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
862 WXFontStructPtr pFontStruct
= theFont
->GetFontStruct(1.0, GetXDisplay());
864 int direction
, ascent
, descent2
;
866 int slen
= string
.Len();
870 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
871 &ascent
, &descent2
, &overall
);
874 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
875 &direction
, &ascent
, &descent2
, &overall
);
878 *x
= (overall
.width
);
880 *y
= (ascent
+ descent2
);
884 *externalLeading
= 0;
888 // ----------------------------------------------------------------------------
890 // ----------------------------------------------------------------------------
892 void wxWindowX11::Refresh(bool eraseBack
, const wxRect
*rect
)
898 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
899 m_clearRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
904 GetSize( &width
, &height
);
906 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
907 m_clearRegion
.Clear();
908 m_clearRegion
.Union( 0, 0, width
, height
);
914 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
915 m_updateRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
920 GetSize( &width
, &height
);
922 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
923 m_updateRegion
.Clear();
924 m_updateRegion
.Union( 0, 0, width
, height
);
927 // Actually don't schedule yet..
931 void wxWindowX11::Update()
933 if (!m_updateRegion
.IsEmpty())
935 X11SendPaintEvents();
939 void wxWindowX11::Clear()
941 wxClientDC
dc((wxWindow
*) this);
942 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
943 dc
.SetBackground(brush
);
947 void wxWindowX11::X11SendPaintEvents()
949 m_clipPaintRegion
= TRUE
;
951 if (!m_clearRegion
.IsEmpty())
953 wxWindowDC
dc( (wxWindow
*)this );
954 dc
.SetClippingRegion( m_clearRegion
);
956 wxEraseEvent
erase_event( GetId(), &dc
);
957 erase_event
.SetEventObject( this );
959 if (!GetEventHandler()->ProcessEvent(erase_event
))
961 wxRegionIterator
upd( m_clearRegion
);
964 // XClearArea( ... , upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
968 m_clearRegion
.Clear();
971 wxNcPaintEvent
nc_paint_event( GetId() );
972 nc_paint_event
.SetEventObject( this );
973 GetEventHandler()->ProcessEvent( nc_paint_event
);
975 wxPaintEvent
paint_event( GetId() );
976 paint_event
.SetEventObject( this );
977 GetEventHandler()->ProcessEvent( paint_event
);
979 m_clipPaintRegion
= FALSE
;
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 // Responds to colour changes: passes event on to children.
987 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent
& event
)
989 wxWindowList::Node
*node
= GetChildren().GetFirst();
992 // Only propagate to non-top-level windows
993 wxWindow
*win
= node
->GetData();
994 if ( win
->GetParent() )
996 wxSysColourChangedEvent event2
;
997 event
.m_eventObject
= win
;
998 win
->GetEventHandler()->ProcessEvent(event2
);
1001 node
= node
->GetNext();
1005 void wxWindowX11::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1007 // This calls the UI-update mechanism (querying windows for
1008 // menu/toolbar/control state information)
1012 // ----------------------------------------------------------------------------
1013 // function which maintain the global hash table mapping Widgets to wxWindows
1014 // ----------------------------------------------------------------------------
1016 bool wxAddWindowToTable(Window w
, wxWindow
*win
)
1018 wxWindow
*oldItem
= NULL
;
1019 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1021 wxLogDebug("Widget table clash: new widget is %ld, %s",
1022 (long)w
, win
->GetClassInfo()->GetClassName());
1026 wxWidgetHashTable
->Put((long) w
, win
);
1028 wxLogTrace("widget", "XWindow 0x%08x <-> window %p (%s)",
1029 w
, win
, win
->GetClassInfo()->GetClassName());
1034 wxWindow
*wxGetWindowFromTable(Window w
)
1036 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1039 void wxDeleteWindowFromTable(Window w
)
1041 wxWidgetHashTable
->Delete((long)w
);
1044 // ----------------------------------------------------------------------------
1045 // add/remove window from the table
1046 // ----------------------------------------------------------------------------
1048 // ----------------------------------------------------------------------------
1049 // X11-specific accessors
1050 // ----------------------------------------------------------------------------
1052 // Get the underlying X window
1053 WXWindow
wxWindowX11::GetXWindow() const
1055 return GetMainWindow();
1058 // Get the underlying X display
1059 WXDisplay
*wxWindowX11::GetXDisplay() const
1061 return wxGetDisplay();
1064 WXWindow
wxWindowX11::GetMainWindow() const
1066 return m_mainWidget
;
1069 // ----------------------------------------------------------------------------
1070 // TranslateXXXEvent() functions
1071 // ----------------------------------------------------------------------------
1073 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
, Window window
, XEvent
*xevent
)
1075 switch (xevent
->xany
.type
)
1083 wxEventType eventType
= wxEVT_NULL
;
1085 if (xevent
->xany
.type
== EnterNotify
)
1087 //if (local_event.xcrossing.mode!=NotifyNormal)
1088 // return ; // Ignore grab events
1089 eventType
= wxEVT_ENTER_WINDOW
;
1090 // canvas->GetEventHandler()->OnSetFocus();
1092 else if (xevent
->xany
.type
== LeaveNotify
)
1094 //if (local_event.xcrossingr.mode!=NotifyNormal)
1095 // return ; // Ignore grab events
1096 eventType
= wxEVT_LEAVE_WINDOW
;
1097 // canvas->GetEventHandler()->OnKillFocus();
1099 else if (xevent
->xany
.type
== MotionNotify
)
1101 eventType
= wxEVT_MOTION
;
1103 else if (xevent
->xany
.type
== ButtonPress
)
1105 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
1107 if (xevent
->xbutton
.button
== Button1
)
1109 eventType
= wxEVT_LEFT_DOWN
;
1112 else if (xevent
->xbutton
.button
== Button2
)
1114 eventType
= wxEVT_MIDDLE_DOWN
;
1117 else if (xevent
->xbutton
.button
== Button3
)
1119 eventType
= wxEVT_RIGHT_DOWN
;
1123 // check for a double click
1124 // TODO: where can we get this value from?
1125 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1126 long dclickTime
= 200;
1127 long ts
= wxevent
.GetTimestamp();
1129 int buttonLast
= win
->GetLastClickedButton();
1130 long lastTS
= win
->GetLastClickTime();
1131 if ( buttonLast
&& buttonLast
== button
&& (ts
- lastTS
) < dclickTime
)
1134 win
->SetLastClick(0, ts
);
1135 if ( eventType
== wxEVT_LEFT_DOWN
)
1136 eventType
= wxEVT_LEFT_DCLICK
;
1137 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
1138 eventType
= wxEVT_MIDDLE_DCLICK
;
1139 else if ( eventType
== wxEVT_RIGHT_DOWN
)
1140 eventType
= wxEVT_RIGHT_DCLICK
;
1144 // not fast enough or different button
1145 win
->SetLastClick(button
, ts
);
1148 else if (xevent
->xany
.type
== ButtonRelease
)
1150 if (xevent
->xbutton
.button
== Button1
)
1152 eventType
= wxEVT_LEFT_UP
;
1154 else if (xevent
->xbutton
.button
== Button2
)
1156 eventType
= wxEVT_MIDDLE_UP
;
1158 else if (xevent
->xbutton
.button
== Button3
)
1160 eventType
= wxEVT_RIGHT_UP
;
1169 wxevent
.SetEventType(eventType
);
1171 wxevent
.m_x
= xevent
->xbutton
.x
;
1172 wxevent
.m_y
= xevent
->xbutton
.y
;
1174 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
1175 || (event_left_is_down (xevent
)
1176 && (eventType
!= wxEVT_LEFT_UP
)));
1177 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
1178 || (event_middle_is_down (xevent
)
1179 && (eventType
!= wxEVT_MIDDLE_UP
)));
1180 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
1181 || (event_right_is_down (xevent
)
1182 && (eventType
!= wxEVT_RIGHT_UP
)));
1184 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
1185 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
1186 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
1187 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
1189 wxevent
.SetId(win
->GetId());
1190 wxevent
.SetEventObject(win
);
1198 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
, Window
WXUNUSED(win
), XEvent
*xevent
)
1200 switch (xevent
->xany
.type
)
1208 (void) XLookupString ((XKeyEvent
*) xevent
, buf
, 20, &keySym
, NULL
);
1209 int id
= wxCharCodeXToWX (keySym
);
1211 if (xevent
->xkey
.state
& ShiftMask
)
1212 wxevent
.m_shiftDown
= TRUE
;
1213 if (xevent
->xkey
.state
& ControlMask
)
1214 wxevent
.m_controlDown
= TRUE
;
1215 if (xevent
->xkey
.state
& Mod3Mask
)
1216 wxevent
.m_altDown
= TRUE
;
1217 if (xevent
->xkey
.state
& Mod1Mask
)
1218 wxevent
.m_metaDown
= TRUE
;
1219 wxevent
.SetEventObject(win
);
1220 wxevent
.m_keyCode
= id
;
1221 wxevent
.SetTimestamp(xevent
->xkey
.time
);
1223 wxevent
.m_x
= xevent
->xbutton
.x
;
1224 wxevent
.m_y
= xevent
->xbutton
.y
;
1238 // ----------------------------------------------------------------------------
1240 // ----------------------------------------------------------------------------
1244 #define YAllocColor XAllocColor
1245 XColor g_itemColors
[5];
1246 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
1249 static XmColorProc colorProc
;
1251 result
= wxNO_COLORS
;
1255 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
1256 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
1257 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
1258 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
1259 if (colorProc
== (XmColorProc
) NULL
)
1261 // Get a ptr to the actual function
1262 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
1263 // And set it back to motif.
1264 XmSetColorCalculation (colorProc
);
1266 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
1267 &g_itemColors
[wxFORE_INDEX
],
1268 &g_itemColors
[wxSELE_INDEX
],
1269 &g_itemColors
[wxTOPS_INDEX
],
1270 &g_itemColors
[wxBOTS_INDEX
]);
1271 result
= wxBACK_COLORS
;
1275 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
1276 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
1277 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
1278 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
1279 if (result
== wxNO_COLORS
)
1280 result
= wxFORE_COLORS
;
1283 Display
*dpy
= display
;
1284 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
1288 /* 5 Colours to allocate */
1289 for (int i
= 0; i
< 5; i
++)
1290 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
1291 result
= wxNO_COLORS
;
1295 /* Only 1 colour to allocate */
1296 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
1297 result
= wxNO_COLORS
;
1305 bool wxWindowX11::SetBackgroundColour(const wxColour
& col
)
1307 if ( !wxWindowBase::SetBackgroundColour(col
) )
1310 if (!GetMainWindow())
1313 XSetWindowAttributes attrib
;
1314 attrib
.background_pixel
= col
.AllocColour(wxGlobalDisplay());
1316 XChangeWindowAttributes(wxGlobalDisplay(),
1317 (Window
) GetMainWindow(),
1324 bool wxWindowX11::SetForegroundColour(const wxColour
& col
)
1326 if ( !wxWindowBase::SetForegroundColour(col
) )
1332 // ----------------------------------------------------------------------------
1334 // ----------------------------------------------------------------------------
1336 wxWindow
*wxGetActiveWindow()
1339 wxFAIL_MSG("Not implemented");
1344 wxWindow
*wxWindowBase::GetCapture()
1346 return (wxWindow
*)g_captureWindow
;
1350 // Find the wxWindow at the current mouse position, returning the mouse
1352 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
1354 return wxFindWindowAtPoint(wxGetMousePosition());
1357 // Get the current mouse position.
1358 wxPoint
wxGetMousePosition()
1360 Display
*display
= wxGlobalDisplay();
1361 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
1362 Window rootReturn
, childReturn
;
1363 int rootX
, rootY
, winX
, winY
;
1364 unsigned int maskReturn
;
1366 XQueryPointer (display
,
1370 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
1371 return wxPoint(rootX
, rootY
);
1375 // ----------------------------------------------------------------------------
1376 // wxNoOptimize: switch off size optimization
1377 // ----------------------------------------------------------------------------
1379 int wxNoOptimize::ms_count
= 0;