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
| ControlMask
| Mod1Mask
| Mod2Mask
| Mod3Mask
| Mod4Mask
| Mod5Mask
,
337 if (res
!= GrabSuccess
)
339 wxLogDebug("Failed to grab keyboard.");
340 XUngrabPointer(wxGlobalDisplay(), CurrentTime
);
341 XUngrabButton(wxGlobalDisplay(), AnyButton
, AnyModifier
,
342 (Window
) GetMainWindow());
346 m_winCaptured
= TRUE
;
350 void wxWindowX11::DoReleaseMouse()
352 g_captureWindow
= NULL
;
353 if ( !m_winCaptured
)
356 Window wMain
= (Window
)GetMainWindow();
360 XUngrabPointer(wxGlobalDisplay(), wMain
);
361 XUngrabButton(wxGlobalDisplay(), AnyButton
, AnyModifier
,
363 XUngrabKeyboard(wxGlobalDisplay(), CurrentTime
);
366 m_winCaptured
= FALSE
;
369 bool wxWindowX11::SetFont(const wxFont
& font
)
371 if ( !wxWindowBase::SetFont(font
) )
380 bool wxWindowX11::SetCursor(const wxCursor
& cursor
)
382 if ( !wxWindowBase::SetCursor(cursor
) )
388 wxCursor
* cursor2
= NULL
;
390 cursor2
= & m_cursor
;
392 cursor2
= wxSTANDARD_CURSOR
;
394 WXDisplay
*dpy
= GetXDisplay();
395 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
397 Window win
= (Window
) GetMainWindow();
398 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
403 // Coordinates relative to the window
404 void wxWindowX11::WarpPointer (int x
, int y
)
407 XWarpPointer( wxGlobalDisplay(), None
, (Window
) m_mainWidget
, 0, 0, 0, 0, x
, y
);
410 // Does a physical scroll
411 void wxWindowX11::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
416 // Use specified rectangle
417 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
421 // Use whole client area
423 GetClientSize(& w
, & h
);
426 wxNode
*cnode
= m_children
.First();
429 wxWindow
*child
= (wxWindow
*) cnode
->Data();
432 child
->GetSize( &sx
, &sy
);
433 wxPoint
pos( child
->GetPosition() );
434 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
435 cnode
= cnode
->Next();
438 int x1
= (dx
>= 0) ? x
: x
- dx
;
439 int y1
= (dy
>= 0) ? y
: y
- dy
;
440 int w1
= w
- abs(dx
);
441 int h1
= h
- abs(dy
);
442 int x2
= (dx
>= 0) ? x
+ dx
: x
;
443 int y2
= (dy
>= 0) ? y
+ dy
: y
;
445 wxClientDC
dc((wxWindow
*) this);
447 dc
.SetLogicalFunction (wxCOPY
);
449 Window window
= (Window
) GetMainWindow();
450 Display
* display
= wxGlobalDisplay();
452 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
453 x1
, y1
, w1
, h1
, x2
, y2
);
455 dc
.SetAutoSetting(TRUE
);
456 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
457 dc
.SetBrush(brush
); // FIXME: needed?
459 // We'll add rectangles to the list of update rectangles according to which
460 // bits we've exposed.
465 wxRect
*rect
= new wxRect
;
471 XFillRectangle(display
, window
,
472 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
476 rect
->width
= rect
->width
;
477 rect
->height
= rect
->height
;
479 updateRects
.Append((wxObject
*) rect
);
483 wxRect
*rect
= new wxRect
;
485 rect
->x
= x
+ w
+ dx
;
490 XFillRectangle(display
, window
,
491 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
496 rect
->width
= rect
->width
;
497 rect
->height
= rect
->height
;
499 updateRects
.Append((wxObject
*) rect
);
503 wxRect
*rect
= new wxRect
;
510 XFillRectangle(display
, window
,
511 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
515 rect
->width
= rect
->width
;
516 rect
->height
= rect
->height
;
518 updateRects
.Append((wxObject
*) rect
);
522 wxRect
*rect
= new wxRect
;
525 rect
->y
= y
+ h
+ dy
;
529 XFillRectangle(display
, window
,
530 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
534 rect
->width
= rect
->width
;
535 rect
->height
= rect
->height
;
537 updateRects
.Append((wxObject
*) rect
);
539 dc
.SetBrush(wxNullBrush
);
541 // Now send expose events
543 wxNode
* node
= updateRects
.First();
546 wxRect
* rect
= (wxRect
*) node
->Data();
550 event
.display
= display
;
551 event
.send_event
= True
;
552 event
.window
= window
;
556 event
.width
= rect
->width
;
557 event
.height
= rect
->height
;
561 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
567 // Delete the update rects
568 node
= updateRects
.First();
571 wxRect
* rect
= (wxRect
*) node
->Data();
578 // XmUpdateDisplay((Widget) GetMainWidget());
581 // ---------------------------------------------------------------------------
583 // ---------------------------------------------------------------------------
585 #if wxUSE_DRAG_AND_DROP
587 void wxWindowX11::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
594 // Old style file-manager drag&drop
595 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept
))
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
606 void wxWindowX11::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
611 #endif // wxUSE_TOOLTIPS
613 // ---------------------------------------------------------------------------
614 // moving and resizing
615 // ---------------------------------------------------------------------------
617 bool wxWindowX11::PreResize()
623 void wxWindowX11::DoGetSize(int *x
, int *y
) const
625 Window window
= (Window
) m_mainWidget
;
628 XWindowAttributes attr
;
629 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
634 *x
= attr
.width
/* + 2*m_borderSize */ ;
635 *y
= attr
.height
/* + 2*m_borderSize */ ;
640 void wxWindowX11::DoGetPosition(int *x
, int *y
) const
642 Window window
= (Window
) m_mainWidget
;
645 XWindowAttributes attr
;
646 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
654 // We may be faking the client origin. So a window that's really at (0, 30)
655 // may appear (to wxWin apps) to be at (0, 0).
658 wxPoint
pt(GetParent()->GetClientAreaOrigin());
666 void wxWindowX11::DoScreenToClient(int *x
, int *y
) const
668 Display
*display
= wxGlobalDisplay();
669 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
670 Window thisWindow
= (Window
) m_mainWidget
;
675 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
678 void wxWindowX11::DoClientToScreen(int *x
, int *y
) const
680 Display
*display
= wxGlobalDisplay();
681 Window rootWindow
= RootWindowOfScreen(DefaultScreenOfDisplay(display
));
682 Window thisWindow
= (Window
) m_mainWidget
;
687 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
691 // Get size *available for subwindows* i.e. excluding menu bar etc.
692 void wxWindowX11::DoGetClientSize(int *x
, int *y
) const
694 Window window
= (Window
) m_mainWidget
;
698 XWindowAttributes attr
;
699 Status status
= XGetWindowAttributes(wxGlobalDisplay(), window
, & attr
);
710 void wxWindowX11::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
712 if (!GetMainWindow())
715 XWindowChanges windowChanges
;
718 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
723 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
728 if (width
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
730 windowChanges
.width
= width
/* - m_borderSize*2 */;
731 valueMask
|= CWWidth
;
733 if (height
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
735 windowChanges
.height
= height
/* -m_borderSize*2*/;
736 valueMask
|= CWHeight
;
738 AdjustForParentClientOrigin( x
, y
, sizeFlags
);
740 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
741 valueMask
, & windowChanges
);
744 void wxWindowX11::DoSetClientSize(int width
, int height
)
746 if (!GetMainWindow())
749 XWindowChanges windowChanges
;
754 windowChanges
.width
= width
;
755 valueMask
|= CWWidth
;
759 windowChanges
.height
= height
;
760 valueMask
|= CWHeight
;
762 XConfigureWindow(wxGlobalDisplay(), (Window
) GetMainWindow(),
763 valueMask
, & windowChanges
);
766 // For implementation purposes - sometimes decorations make the client area
768 wxPoint
wxWindowX11::GetClientAreaOrigin() const
770 return wxPoint(0, 0);
773 // Makes an adjustment to the window position (for example, a frame that has
774 // a toolbar that it manages itself).
775 void wxWindowX11::AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
)
777 if (((sizeFlags
& wxSIZE_NO_ADJUSTMENTS
) == 0) && GetParent())
779 wxPoint
pt(GetParent()->GetClientAreaOrigin());
780 x
+= pt
.x
; y
+= pt
.y
;
784 void wxWindowX11::SetSizeHints(int minW
, int minH
, int maxW
, int maxH
, int incW
, int incH
)
791 XSizeHints sizeHints
;
794 if (minW
> -1 && minH
> -1)
796 sizeHints
.flags
|= PMinSize
;
797 sizeHints
.min_width
= minW
;
798 sizeHints
.min_height
= minH
;
800 if (maxW
> -1 && maxH
> -1)
802 sizeHints
.flags
|= PMaxSize
;
803 sizeHints
.max_width
= maxW
;
804 sizeHints
.max_height
= maxH
;
806 if (incW
> -1 && incH
> -1)
808 sizeHints
.flags
|= PResizeInc
;
809 sizeHints
.width_inc
= incW
;
810 sizeHints
.height_inc
= incH
;
813 XSetWMNormalHints(wxGlobalDisplay(), (Window
) GetMainWindow(), & sizeHints
);
816 void wxWindowX11::DoMoveWindow(int x
, int y
, int width
, int height
)
818 DoSetSize(x
, y
, width
, height
);
821 // ---------------------------------------------------------------------------
823 // ---------------------------------------------------------------------------
825 int wxWindowX11::GetCharHeight() const
827 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
829 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
831 int direction
, ascent
, descent
;
833 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
836 // return (overall.ascent + overall.descent);
837 return (ascent
+ descent
);
840 int wxWindowX11::GetCharWidth() const
842 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
844 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
846 int direction
, ascent
, descent
;
848 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
851 return overall
.width
;
854 void wxWindowX11::GetTextExtent(const wxString
& string
,
856 int *descent
, int *externalLeading
,
857 const wxFont
*theFont
) const
859 wxFont
*fontToUse
= (wxFont
*)theFont
;
861 fontToUse
= (wxFont
*) & m_font
;
863 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
865 WXFontStructPtr pFontStruct
= theFont
->GetFontStruct(1.0, GetXDisplay());
867 int direction
, ascent
, descent2
;
869 int slen
= string
.Len();
873 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
874 &ascent
, &descent2
, &overall
);
877 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
878 &direction
, &ascent
, &descent2
, &overall
);
881 *x
= (overall
.width
);
883 *y
= (ascent
+ descent2
);
887 *externalLeading
= 0;
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
895 void wxWindowX11::Refresh(bool eraseBack
, const wxRect
*rect
)
901 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
902 m_clearRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
907 GetSize( &width
, &height
);
909 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
910 m_clearRegion
.Clear();
911 m_clearRegion
.Union( 0, 0, width
, height
);
917 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
918 m_updateRegion
.Union( rect
->x
, rect
->y
, rect
->width
, rect
->height
);
923 GetSize( &width
, &height
);
925 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
926 m_updateRegion
.Clear();
927 m_updateRegion
.Union( 0, 0, width
, height
);
930 // Actually don't schedule yet..
934 void wxWindowX11::Update()
936 if (!m_updateRegion
.IsEmpty())
938 X11SendPaintEvents();
942 void wxWindowX11::Clear()
944 wxClientDC
dc((wxWindow
*) this);
945 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
946 dc
.SetBackground(brush
);
950 void wxWindowX11::X11SendPaintEvents()
952 m_clipPaintRegion
= TRUE
;
954 if (!m_clearRegion
.IsEmpty())
956 wxWindowDC
dc( (wxWindow
*)this );
957 dc
.SetClippingRegion( m_clearRegion
);
959 wxEraseEvent
erase_event( GetId(), &dc
);
960 erase_event
.SetEventObject( this );
962 if (!GetEventHandler()->ProcessEvent(erase_event
))
964 wxRegionIterator
upd( m_clearRegion
);
967 // XClearArea( ... , upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
971 m_clearRegion
.Clear();
974 wxNcPaintEvent
nc_paint_event( GetId() );
975 nc_paint_event
.SetEventObject( this );
976 GetEventHandler()->ProcessEvent( nc_paint_event
);
978 wxPaintEvent
paint_event( GetId() );
979 paint_event
.SetEventObject( this );
980 GetEventHandler()->ProcessEvent( paint_event
);
982 m_clipPaintRegion
= FALSE
;
985 // ----------------------------------------------------------------------------
987 // ----------------------------------------------------------------------------
989 // Responds to colour changes: passes event on to children.
990 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent
& event
)
992 wxWindowList::Node
*node
= GetChildren().GetFirst();
995 // Only propagate to non-top-level windows
996 wxWindow
*win
= node
->GetData();
997 if ( win
->GetParent() )
999 wxSysColourChangedEvent event2
;
1000 event
.m_eventObject
= win
;
1001 win
->GetEventHandler()->ProcessEvent(event2
);
1004 node
= node
->GetNext();
1008 void wxWindowX11::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1010 // This calls the UI-update mechanism (querying windows for
1011 // menu/toolbar/control state information)
1015 // ----------------------------------------------------------------------------
1016 // function which maintain the global hash table mapping Widgets to wxWindows
1017 // ----------------------------------------------------------------------------
1019 bool wxAddWindowToTable(Window w
, wxWindow
*win
)
1021 wxWindow
*oldItem
= NULL
;
1022 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1024 wxLogDebug("Widget table clash: new widget is %ld, %s",
1025 (long)w
, win
->GetClassInfo()->GetClassName());
1029 wxWidgetHashTable
->Put((long) w
, win
);
1031 wxLogTrace("widget", "XWindow 0x%08x <-> window %p (%s)",
1032 w
, win
, win
->GetClassInfo()->GetClassName());
1037 wxWindow
*wxGetWindowFromTable(Window w
)
1039 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1042 void wxDeleteWindowFromTable(Window w
)
1044 wxWidgetHashTable
->Delete((long)w
);
1047 // ----------------------------------------------------------------------------
1048 // add/remove window from the table
1049 // ----------------------------------------------------------------------------
1051 // ----------------------------------------------------------------------------
1052 // X11-specific accessors
1053 // ----------------------------------------------------------------------------
1055 // Get the underlying X window
1056 WXWindow
wxWindowX11::GetXWindow() const
1058 return GetMainWindow();
1061 // Get the underlying X display
1062 WXDisplay
*wxWindowX11::GetXDisplay() const
1064 return wxGetDisplay();
1067 WXWindow
wxWindowX11::GetMainWindow() const
1069 return m_mainWidget
;
1072 // ----------------------------------------------------------------------------
1073 // TranslateXXXEvent() functions
1074 // ----------------------------------------------------------------------------
1076 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
, Window window
, XEvent
*xevent
)
1078 switch (xevent
->xany
.type
)
1086 wxEventType eventType
= wxEVT_NULL
;
1088 if (xevent
->xany
.type
== EnterNotify
)
1090 //if (local_event.xcrossing.mode!=NotifyNormal)
1091 // return ; // Ignore grab events
1092 eventType
= wxEVT_ENTER_WINDOW
;
1093 // canvas->GetEventHandler()->OnSetFocus();
1095 else if (xevent
->xany
.type
== LeaveNotify
)
1097 //if (local_event.xcrossingr.mode!=NotifyNormal)
1098 // return ; // Ignore grab events
1099 eventType
= wxEVT_LEAVE_WINDOW
;
1100 // canvas->GetEventHandler()->OnKillFocus();
1102 else if (xevent
->xany
.type
== MotionNotify
)
1104 eventType
= wxEVT_MOTION
;
1106 else if (xevent
->xany
.type
== ButtonPress
)
1108 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
1110 if (xevent
->xbutton
.button
== Button1
)
1112 eventType
= wxEVT_LEFT_DOWN
;
1115 else if (xevent
->xbutton
.button
== Button2
)
1117 eventType
= wxEVT_MIDDLE_DOWN
;
1120 else if (xevent
->xbutton
.button
== Button3
)
1122 eventType
= wxEVT_RIGHT_DOWN
;
1126 // check for a double click
1127 // TODO: where can we get this value from?
1128 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1129 long dclickTime
= 200;
1130 long ts
= wxevent
.GetTimestamp();
1132 int buttonLast
= win
->GetLastClickedButton();
1133 long lastTS
= win
->GetLastClickTime();
1134 if ( buttonLast
&& buttonLast
== button
&& (ts
- lastTS
) < dclickTime
)
1137 win
->SetLastClick(0, ts
);
1138 if ( eventType
== wxEVT_LEFT_DOWN
)
1139 eventType
= wxEVT_LEFT_DCLICK
;
1140 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
1141 eventType
= wxEVT_MIDDLE_DCLICK
;
1142 else if ( eventType
== wxEVT_RIGHT_DOWN
)
1143 eventType
= wxEVT_RIGHT_DCLICK
;
1147 // not fast enough or different button
1148 win
->SetLastClick(button
, ts
);
1151 else if (xevent
->xany
.type
== ButtonRelease
)
1153 if (xevent
->xbutton
.button
== Button1
)
1155 eventType
= wxEVT_LEFT_UP
;
1157 else if (xevent
->xbutton
.button
== Button2
)
1159 eventType
= wxEVT_MIDDLE_UP
;
1161 else if (xevent
->xbutton
.button
== Button3
)
1163 eventType
= wxEVT_RIGHT_UP
;
1172 wxevent
.SetEventType(eventType
);
1174 wxevent
.m_x
= xevent
->xbutton
.x
;
1175 wxevent
.m_y
= xevent
->xbutton
.y
;
1177 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
1178 || (event_left_is_down (xevent
)
1179 && (eventType
!= wxEVT_LEFT_UP
)));
1180 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
1181 || (event_middle_is_down (xevent
)
1182 && (eventType
!= wxEVT_MIDDLE_UP
)));
1183 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
1184 || (event_right_is_down (xevent
)
1185 && (eventType
!= wxEVT_RIGHT_UP
)));
1187 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
1188 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
1189 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
1190 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
1192 wxevent
.SetId(win
->GetId());
1193 wxevent
.SetEventObject(win
);
1201 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
, Window
WXUNUSED(win
), XEvent
*xevent
)
1203 switch (xevent
->xany
.type
)
1211 (void) XLookupString ((XKeyEvent
*) xevent
, buf
, 20, &keySym
, NULL
);
1212 int id
= wxCharCodeXToWX (keySym
);
1214 if (xevent
->xkey
.state
& ShiftMask
)
1215 wxevent
.m_shiftDown
= TRUE
;
1216 if (xevent
->xkey
.state
& ControlMask
)
1217 wxevent
.m_controlDown
= TRUE
;
1218 if (xevent
->xkey
.state
& Mod3Mask
)
1219 wxevent
.m_altDown
= TRUE
;
1220 if (xevent
->xkey
.state
& Mod1Mask
)
1221 wxevent
.m_metaDown
= TRUE
;
1222 wxevent
.SetEventObject(win
);
1223 wxevent
.m_keyCode
= id
;
1224 wxevent
.SetTimestamp(xevent
->xkey
.time
);
1226 wxevent
.m_x
= xevent
->xbutton
.x
;
1227 wxevent
.m_y
= xevent
->xbutton
.y
;
1241 // ----------------------------------------------------------------------------
1243 // ----------------------------------------------------------------------------
1247 #define YAllocColor XAllocColor
1248 XColor g_itemColors
[5];
1249 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
1252 static XmColorProc colorProc
;
1254 result
= wxNO_COLORS
;
1258 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
1259 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
1260 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
1261 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
1262 if (colorProc
== (XmColorProc
) NULL
)
1264 // Get a ptr to the actual function
1265 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
1266 // And set it back to motif.
1267 XmSetColorCalculation (colorProc
);
1269 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
1270 &g_itemColors
[wxFORE_INDEX
],
1271 &g_itemColors
[wxSELE_INDEX
],
1272 &g_itemColors
[wxTOPS_INDEX
],
1273 &g_itemColors
[wxBOTS_INDEX
]);
1274 result
= wxBACK_COLORS
;
1278 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
1279 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
1280 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
1281 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
1282 if (result
== wxNO_COLORS
)
1283 result
= wxFORE_COLORS
;
1286 Display
*dpy
= display
;
1287 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
1291 /* 5 Colours to allocate */
1292 for (int i
= 0; i
< 5; i
++)
1293 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
1294 result
= wxNO_COLORS
;
1298 /* Only 1 colour to allocate */
1299 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
1300 result
= wxNO_COLORS
;
1308 bool wxWindowX11::SetBackgroundColour(const wxColour
& col
)
1310 if ( !wxWindowBase::SetBackgroundColour(col
) )
1313 if (!GetMainWindow())
1316 wxColour
colour( col
);
1317 XSetWindowAttributes attrib
;
1318 attrib
.background_pixel
= colour
.AllocColour(wxGlobalDisplay());
1320 XChangeWindowAttributes(wxGlobalDisplay(),
1321 (Window
) GetMainWindow(),
1328 bool wxWindowX11::SetForegroundColour(const wxColour
& col
)
1330 if ( !wxWindowBase::SetForegroundColour(col
) )
1336 // ----------------------------------------------------------------------------
1338 // ----------------------------------------------------------------------------
1340 wxWindow
*wxGetActiveWindow()
1343 wxFAIL_MSG("Not implemented");
1348 wxWindow
*wxWindowBase::GetCapture()
1350 return (wxWindow
*)g_captureWindow
;
1354 // Find the wxWindow at the current mouse position, returning the mouse
1356 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
1358 return wxFindWindowAtPoint(wxGetMousePosition());
1361 // Get the current mouse position.
1362 wxPoint
wxGetMousePosition()
1364 Display
*display
= wxGlobalDisplay();
1365 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
1366 Window rootReturn
, childReturn
;
1367 int rootX
, rootY
, winX
, winY
;
1368 unsigned int maskReturn
;
1370 XQueryPointer (display
,
1374 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
1375 return wxPoint(rootX
, rootY
);
1379 // ----------------------------------------------------------------------------
1380 // wxNoOptimize: switch off size optimization
1381 // ----------------------------------------------------------------------------
1383 int wxNoOptimize::ms_count
= 0;