1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "window.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
28 #define XtDisplay XTDISPLAY
29 #define XtWindow XTWINDOW
30 #define XtScreen XTSCREEN
36 #include "wx/dcclient.h"
39 #include "wx/layout.h"
40 #include "wx/button.h"
41 #include "wx/settings.h"
43 #include "wx/scrolwin.h"
44 #include "wx/module.h"
45 #include "wx/menuitem.h"
47 #include "wx/evtloop.h"
50 #if wxUSE_DRAG_AND_DROP
54 // DoSetSizeIntr and DoMoveWindowIntr
56 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
57 // did not work and/or segfaulted because
58 // 1) wxWindow::Create calls SetSize,
59 // which results in a call to DoSetSize much earlier than in the other ports
60 // 2) if wxWindow::Create is called (wxControl::Create calls it)
61 // then DoSetSize is never called, causing layout problems in composite
65 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
66 // DoSetPosition directly or indirectly from wxWindow::Create
67 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
70 #pragma message disable nosimpint
74 #include <Xm/DrawingA.h>
75 #include <Xm/ScrolledW.h>
76 #include <Xm/ScrollBar.h>
79 #include <Xm/RowColumn.h> // for XmMenuPosition
81 #pragma message enable nosimpint
84 #include "wx/motif/private.h"
88 // ----------------------------------------------------------------------------
89 // global variables for this module
90 // ----------------------------------------------------------------------------
92 extern wxHashTable
*wxWidgetHashTable
;
93 static wxWindow
* g_captureWindow
= NULL
;
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 static void wxCanvasRepaintProc(Widget
, XtPointer
, XmDrawingAreaCallbackStruct
* cbs
);
101 static void wxCanvasInputEvent(Widget drawingArea
, XtPointer data
, XmDrawingAreaCallbackStruct
* cbs
);
102 static void wxCanvasMotionEvent(Widget
, XButtonEvent
* event
);
103 static void wxCanvasEnterLeave(Widget drawingArea
, XtPointer clientData
, XCrossingEvent
* event
);
104 static void wxScrollBarCallback(Widget widget
, XtPointer clientData
,
105 XmScrollBarCallbackStruct
*cbs
);
106 static void wxPanelItemEventHandler(Widget wid
,
107 XtPointer client_data
,
109 Boolean
*continueToDispatch
);
114 // Helper function for 16-bit fonts
115 static int str16len(const char *s
)
119 while (s
[0] && s
[1]) {
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
134 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
135 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxWindow
, wxWindowBase
)
143 BEGIN_EVENT_TABLE(wxWindow
, wxWindowBase
)
144 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged
)
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
157 Widget w
= (Widget
)widget
;
165 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
167 Widget w
= (Widget
)widget
;
171 // Rationale: a lot of common operations (including but not
172 // limited to moving, resizing and appending items to a listbox)
173 // unmamange the widget, do their work, then manage it again.
174 // This means that, for example adding an item to a listbox will show it,
175 // or that most controls are shown every time they are moved or resized!
176 XtSetMappedWhenManaged( w
, domap
);
178 // if the widget is not unmanaged, it still intercepts
179 // mouse events, even if it is not mapped (and hence invisible)
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void wxWindow::Init()
201 m_needsRefresh
= true;
202 m_mainWidget
= (WXWidget
) 0;
204 m_winCaptured
= false;
212 m_drawingArea
= (WXWidget
) 0;
217 m_backingPixmap
= (WXPixmap
) 0;
228 // real construction (Init() must have been called before!)
229 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
233 const wxString
& name
)
235 wxCHECK_MSG( parent
, false, "can't create wxWindow without parent" );
237 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
239 parent
->AddChild(this);
241 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
242 m_foregroundColour
= *wxBLACK
;
244 //// TODO: we should probably optimize by only creating a
245 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
246 //// But for now, let's simplify things by always creating the
247 //// drawing area, since otherwise the translations are different.
249 // New translations for getting mouse motion feedback
250 static const String translations
= wxMOTIF_STR(
251 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
252 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
253 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
254 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
255 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
256 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
257 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
258 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
259 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
260 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
261 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
262 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
263 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
264 <Key>: DrawingAreaInput()");
266 XtActionsRec actions
[1];
267 actions
[0].string
= wxMOTIF_STR("wxCanvasMotionEvent");
268 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
269 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
271 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
272 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
274 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
277 xmScrolledWindowWidgetClass
,
278 m_borderWidget
? (Widget
) m_borderWidget
280 XmNresizePolicy
, XmRESIZE_NONE
,
282 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
283 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
287 XtTranslations ptr
= XtParseTranslationTable(translations
);
288 m_drawingArea
= (WXWidget
)XtVaCreateWidget
291 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
292 XmNunitType
, XmPIXELS
,
293 // XmNresizePolicy, XmRESIZE_ANY,
294 XmNresizePolicy
, XmRESIZE_NONE
,
297 XmNtranslations
, ptr
,
300 XtFree((char *) ptr
);
303 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
305 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
306 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
307 XtFree ((char *) ptr
);
311 wxAddWindowToTable((Widget
) m_drawingArea
, this);
312 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
314 // This order is very important in Motif 1.2.1
315 XtRealizeWidget ((Widget
) m_scrolledWindow
);
316 XtRealizeWidget ((Widget
) m_drawingArea
);
317 XtManageChild ((Widget
) m_drawingArea
);
319 ptr
= XtParseTranslationTable("<Configure>: resize()");
320 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
321 XtFree ((char *) ptr
);
323 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
324 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
327 (Widget
)m_drawingArea
,
328 PointerMotionHintMask
| EnterWindowMask
|
329 LeaveWindowMask
| FocusChangeMask
,
331 (XtEventHandler
) wxCanvasEnterLeave
,
335 // Scrolled widget needs to have its colour changed or we get a little blue
336 // square where the scrollbars abutt
337 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
338 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, true);
339 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, true);
341 XmScrolledWindowSetAreas(
342 (Widget
)m_scrolledWindow
,
343 (Widget
) 0, (Widget
) 0,
344 (Widget
) m_drawingArea
);
346 // Without this, the cursor may not be restored properly (e.g. in splitter
348 SetCursor(*wxSTANDARD_CURSOR
);
349 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
350 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, true);
355 wxWindow::~wxWindow()
357 if (g_captureWindow
== this)
358 g_captureWindow
= NULL
;
360 m_isBeingDeleted
= true;
362 // Motif-specific actions first
363 WXWidget wMain
= GetMainWidget();
366 // Removes event handlers
370 // If m_drawingArea, we're a fully-fledged window with drawing area,
371 // scrollbars etc. (what wxCanvas used to be)
374 // Destroy children before destroying self
378 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
380 Widget w
= (Widget
) m_drawingArea
;
381 wxDeleteWindowFromTable(w
);
386 m_drawingArea
= (WXWidget
) 0;
389 // Only if we're _really_ a canvas (not a dialog box/panel)
390 if (m_scrolledWindow
)
392 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
397 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
398 XtUnmanageChild((Widget
) m_hScrollBar
);
402 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
403 XtUnmanageChild((Widget
) m_vScrollBar
);
407 XtDestroyWidget((Widget
) m_hScrollBar
);
409 XtDestroyWidget((Widget
) m_vScrollBar
);
411 UnmanageAndDestroy(m_scrolledWindow
);
415 XtDestroyWidget ((Widget
) m_borderWidget
);
416 m_borderWidget
= (WXWidget
) 0;
419 else // Why wasn't this here before? JACS 8/3/2000
423 // Destroy the window
426 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
427 // Child widgets will get destroyed automatically when a frame
428 // or dialog is destroyed, but before that you may get some memory
429 // leaks and potential layout problems if you delete and then add
432 // GRG, Feb/2000: commented this out when adding support for
433 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
434 // that this call crashed wxMotif under OS/2, so it seems
435 // that leaving it out is the right thing to do.
436 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
437 XtDestroyWidget((Widget
) GetMainWidget());
438 SetMainWidget((WXWidget
) NULL
);
442 // ----------------------------------------------------------------------------
443 // scrollbar management
444 // ----------------------------------------------------------------------------
446 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
447 wxOrientation orientation
,
450 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
452 XtVaCreateManagedWidget( "scrollBarWidget",
453 xmScrollBarWidgetClass
, (Widget
)parent
,
454 XmNorientation
, orient
,
459 XtPointer o
= (XtPointer
)orientation
;
460 XtCallbackProc cb
= (XtCallbackProc
)callback
;
462 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
463 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
464 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
465 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
466 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
467 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
468 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
469 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
475 void wxWindow::CreateScrollbar(wxOrientation orientation
)
477 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
479 XtVaSetValues( (Widget
) m_scrolledWindow
,
480 XmNresizePolicy
, XmRESIZE_NONE
,
483 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
484 // Add scrollbars if required
485 if (orientation
== wxHORIZONTAL
)
487 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
488 (void (*)())wxScrollBarCallback
);
490 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, true);
492 XtRealizeWidget( (Widget
)m_hScrollBar
);
494 XtVaSetValues((Widget
) m_scrolledWindow
,
495 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
498 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
500 else if (orientation
== wxVERTICAL
)
502 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
503 (void (*)())wxScrollBarCallback
);
505 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, true);
507 XtRealizeWidget((Widget
)m_vScrollBar
);
509 XtVaSetValues((Widget
) m_scrolledWindow
,
510 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
513 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
516 XtVaSetValues( (Widget
) m_scrolledWindow
,
517 XmNresizePolicy
, XmRESIZE_ANY
,
521 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
523 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
525 XtVaSetValues((Widget
) m_scrolledWindow
,
526 XmNresizePolicy
, XmRESIZE_NONE
,
528 String stringSB
= orientation
== wxHORIZONTAL
?
529 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
530 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
531 &m_hScrollBar
: &m_vScrollBar
;
535 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
536 XtDestroyWidget( (Widget
)*widgetSB
);
537 *widgetSB
= (WXWidget
)NULL
;
540 XtVaSetValues( (Widget
)m_scrolledWindow
,
541 stringSB
, (Widget
) 0,
544 XtVaSetValues((Widget
) m_scrolledWindow
,
545 XmNresizePolicy
, XmRESIZE_ANY
,
549 // ---------------------------------------------------------------------------
551 // ---------------------------------------------------------------------------
553 void wxWindow::SetFocus()
555 Widget wMain
= (Widget
) GetMainWidget();
556 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
557 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
560 // Get the window with the focus
561 wxWindow
*wxWindowBase::DoFindFocus()
564 // (1) Can there be multiple focussed widgets in an application?
565 // In which case we need to find the top-level window that's
567 // (2) The widget with the focus may not be in the widget table
568 // depending on which widgets I put in the table
569 wxWindow
*winFocus
= (wxWindow
*)NULL
;
570 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
572 node
= node
->GetNext() )
574 wxWindow
*win
= node
->GetData();
576 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
578 if (w
!= (Widget
) NULL
)
580 winFocus
= wxGetWindowFromTable(w
);
589 bool wxWindow::Enable(bool enable
)
591 if ( !wxWindowBase::Enable(enable
) )
594 Widget wMain
= (Widget
)GetMainWidget();
597 XtSetSensitive(wMain
, enable
);
598 XmUpdateDisplay(wMain
);
604 bool wxWindow::Show(bool show
)
606 if ( !wxWindowBase::Show(show
) )
609 if (m_borderWidget
|| m_scrolledWindow
)
611 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
612 // MapOrUnmap(m_drawingArea, show);
616 if ( !MapOrUnmap(GetTopWidget(), show
) )
617 MapOrUnmap(GetMainWidget(), show
);
623 // Raise the window to the top of the Z order
624 void wxWindow::Raise()
626 Widget wTop
= (Widget
) GetTopWidget();
627 Window window
= XtWindow(wTop
);
628 XRaiseWindow(XtDisplay(wTop
), window
);
631 // Lower the window to the bottom of the Z order
632 void wxWindow::Lower()
634 Widget wTop
= (Widget
) GetTopWidget();
635 Window window
= XtWindow(wTop
);
636 XLowerWindow(XtDisplay(wTop
), window
);
639 void wxWindow::SetTitle(const wxString
& title
)
641 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
644 wxString
wxWindow::GetTitle() const
647 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
649 return wxString(title
);
652 void wxWindow::DoCaptureMouse()
654 g_captureWindow
= this;
658 Widget wMain
= (Widget
)GetMainWidget();
660 XtAddGrab(wMain
, True
, False
);
662 m_winCaptured
= true;
665 void wxWindow::DoReleaseMouse()
667 g_captureWindow
= NULL
;
668 if ( !m_winCaptured
)
671 Widget wMain
= (Widget
)GetMainWidget();
675 m_winCaptured
= false;
678 bool wxWindow::SetFont(const wxFont
& font
)
680 if ( !wxWindowBase::SetFont(font
) )
691 bool wxWindow::SetCursor(const wxCursor
& cursor
)
693 if ( !wxWindowBase::SetCursor(cursor
) )
699 // wxASSERT_MSG( m_cursor.Ok(),
700 // wxT("cursor must be valid after call to the base version"));
701 wxCursor
* cursor2
= NULL
;
703 cursor2
= & m_cursor
;
705 cursor2
= wxSTANDARD_CURSOR
;
707 WXDisplay
*dpy
= GetXDisplay();
708 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
710 Widget w
= (Widget
) GetMainWidget();
711 Window win
= XtWindow(w
);
712 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
717 // Coordinates relative to the window
718 void wxWindow::WarpPointer (int x
, int y
)
720 Widget wClient
= (Widget
)GetClientWidget();
722 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
725 // ---------------------------------------------------------------------------
727 // ---------------------------------------------------------------------------
729 int wxWindow::GetScrollPos(int orient
) const
731 if (orient
== wxHORIZONTAL
)
737 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
741 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
749 // This now returns the whole range, not just the number of positions that we
751 int wxWindow::GetScrollRange(int orient
) const
753 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
754 // CE scintilla windows don't always have these scrollbars
755 // and it tends to pile up a whole bunch of asserts
756 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
760 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
764 int wxWindow::GetScrollThumb(int orient
) const
766 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
767 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
771 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
775 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
777 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
781 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
784 SetInternalScrollPos((wxOrientation
)orient
, pos
);
787 // New function that will replace some of the above.
788 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
789 int range
, bool WXUNUSED(refresh
))
792 GetSize(& oldW
, & oldH
);
796 if (thumbVisible
== 0)
799 if (thumbVisible
> range
)
800 thumbVisible
= range
;
802 // Save the old state to see if it changed
803 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
805 if (orient
== wxHORIZONTAL
)
807 if (thumbVisible
== range
)
810 DestroyScrollbar(wxHORIZONTAL
);
815 CreateScrollbar(wxHORIZONTAL
);
818 if (orient
== wxVERTICAL
)
820 if (thumbVisible
== range
)
823 DestroyScrollbar(wxVERTICAL
);
828 CreateScrollbar(wxVERTICAL
);
831 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
833 if (oldScrollBar
!= newScrollBar
)
835 // This is important! Without it, scrollbars misbehave badly.
836 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
837 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
838 XtRealizeWidget((Widget
) m_scrolledWindow
);
839 XtManageChild((Widget
) m_scrolledWindow
);
844 XtVaSetValues((Widget
) newScrollBar
,
848 XmNsliderSize
, thumbVisible
,
852 SetInternalScrollPos((wxOrientation
)orient
, pos
);
855 GetSize(& newW
, & newH
);
857 // Adjusting scrollbars can resize the canvas accidentally
858 if (newW
!= oldW
|| newH
!= oldH
)
859 SetSize(-1, -1, oldW
, oldH
);
862 // Does a physical scroll
863 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
868 // Use specified rectangle
869 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
873 // Use whole client area
875 GetClientSize(& w
, & h
);
878 int x1
= (dx
>= 0) ? x
: x
- dx
;
879 int y1
= (dy
>= 0) ? y
: y
- dy
;
880 int w1
= w
- abs(dx
);
881 int h1
= h
- abs(dy
);
882 int x2
= (dx
>= 0) ? x
+ dx
: x
;
883 int y2
= (dy
>= 0) ? y
+ dy
: y
;
887 dc
.SetLogicalFunction (wxCOPY
);
889 Widget widget
= (Widget
) GetMainWidget();
890 Window window
= XtWindow(widget
);
891 Display
* display
= XtDisplay(widget
);
893 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
894 x1
, y1
, w1
, h1
, x2
, y2
);
896 dc
.SetAutoSetting(true);
897 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
898 dc
.SetBrush(brush
); // FIXME: needed?
900 wxWindowList::compatibility_iterator cnode
= m_children
.GetFirst();
903 wxWindow
*child
= cnode
->GetData();
906 child
->GetSize( &sx
, &sy
);
907 wxPoint
pos( child
->GetPosition() );
908 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
909 cnode
= cnode
->GetNext();
912 // We'll add rectangles to the list of update rectangles according to which
913 // bits we've exposed.
918 wxRect
*rect
= new wxRect
;
924 XFillRectangle(display
, window
,
925 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
929 rect
->width
= rect
->width
;
930 rect
->height
= rect
->height
;
932 updateRects
.Append((wxObject
*) rect
);
936 wxRect
*rect
= new wxRect
;
938 rect
->x
= x
+ w
+ dx
;
943 XFillRectangle(display
, window
,
944 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
949 rect
->width
= rect
->width
;
950 rect
->height
= rect
->height
;
952 updateRects
.Append((wxObject
*) rect
);
956 wxRect
*rect
= new wxRect
;
963 XFillRectangle(display
, window
,
964 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
968 rect
->width
= rect
->width
;
969 rect
->height
= rect
->height
;
971 updateRects
.Append((wxObject
*) rect
);
975 wxRect
*rect
= new wxRect
;
978 rect
->y
= y
+ h
+ dy
;
982 XFillRectangle(display
, window
,
983 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
987 rect
->width
= rect
->width
;
988 rect
->height
= rect
->height
;
990 updateRects
.Append((wxObject
*) rect
);
992 dc
.SetBrush(wxNullBrush
);
994 // Now send expose events
996 wxList::compatibility_iterator node
= updateRects
.GetFirst();
999 wxRect
* rect
= (wxRect
*) node
->GetData();
1002 event
.type
= Expose
;
1003 event
.display
= display
;
1004 event
.send_event
= True
;
1005 event
.window
= window
;
1009 event
.width
= rect
->width
;
1010 event
.height
= rect
->height
;
1014 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1016 node
= node
->GetNext();
1020 // Delete the update rects
1021 node
= updateRects
.GetFirst();
1024 wxRect
* rect
= (wxRect
*) node
->GetData();
1026 node
= node
->GetNext();
1029 XmUpdateDisplay((Widget
) GetMainWidget());
1032 // ---------------------------------------------------------------------------
1034 // ---------------------------------------------------------------------------
1036 #if wxUSE_DRAG_AND_DROP
1038 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1045 // Old style file-manager drag&drop
1046 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1051 // ----------------------------------------------------------------------------
1053 // ----------------------------------------------------------------------------
1057 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1062 #endif // wxUSE_TOOLTIPS
1064 // ----------------------------------------------------------------------------
1066 // ----------------------------------------------------------------------------
1070 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
1072 if ( x
== -1 && y
== -1 )
1074 wxPoint mouse
= ScreenToClient(wxGetMousePosition());
1075 x
= mouse
.x
; y
= mouse
.y
;
1078 Widget widget
= (Widget
) GetMainWidget();
1080 /* The menuId field seems to be usused, so we'll use it to
1081 indicate whether a menu is popped up or not:
1082 0: Not currently created as a popup
1083 -1: Created as a popup, but not active
1087 if (menu
->GetParent() && (menu
->GetId() != -1))
1090 if (menu
->GetMainWidget())
1092 menu
->DestroyMenu(true);
1095 menu
->SetId(1); /* Mark as popped-up */
1096 menu
->CreateMenu(NULL
, widget
, menu
);
1097 menu
->SetInvokingWindow(this);
1101 // menu->SetParent(parent);
1102 // parent->children->Append(menu); // Store menu for later deletion
1104 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1112 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1114 wxCanvas *canvas = (wxCanvas *) this;
1115 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1116 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1120 Display
*display
= XtDisplay (widget
);
1121 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1122 Window thisWindow
= XtWindow (widget
);
1124 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1125 &rootX
, &rootY
, &childWindow
);
1127 XButtonPressedEvent event
;
1128 event
.type
= ButtonPress
;
1134 event
.x_root
= rootX
;
1135 event
.y_root
= rootY
;
1137 XmMenuPosition (menuWidget
, &event
);
1138 XtManageChild (menuWidget
);
1140 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1141 // idle-time destroy routine.
1142 // Waiting until this ID changes causes this function to block until
1143 // the menu has been dismissed and the widgets cleaned up.
1144 // In other words, once this routine returns, it is safe to delete
1146 // Ian Brown <ian.brown@printsoft.de>
1148 wxEventLoop evtLoop
;
1150 while (menu
->GetId() == 1)
1152 wxDoEventLoopIteration( evtLoop
);
1160 // ---------------------------------------------------------------------------
1161 // moving and resizing
1162 // ---------------------------------------------------------------------------
1164 bool wxWindow::PreResize()
1170 void wxWindow::DoGetSize(int *x
, int *y
) const
1172 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1173 ( m_borderWidget
? m_borderWidget
:
1174 m_scrolledWindow
? m_scrolledWindow
:
1178 XtVaGetValues( widget
,
1186 void wxWindow::DoGetPosition(int *x
, int *y
) const
1188 Widget widget
= (Widget
)
1190 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1194 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1196 // We may be faking the client origin. So a window that's really at (0, 30)
1197 // may appear (to wxWin apps) to be at (0, 0).
1200 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1209 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1211 Widget widget
= (Widget
) GetClientWidget();
1212 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1213 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1214 Window thisWindow
= XtWindow(widget
);
1219 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1222 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1224 Widget widget
= (Widget
) GetClientWidget();
1225 Display
*display
= XtDisplay(widget
);
1226 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1227 Window thisWindow
= XtWindow(widget
);
1232 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1236 // Get size *available for subwindows* i.e. excluding menu bar etc.
1237 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1239 Widget widget
= (Widget
) GetClientWidget();
1241 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1242 if(x
) *x
= xx
; if(y
) *y
= yy
;
1245 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1247 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, false);
1250 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1251 int sizeFlags
, bool fromCtor
)
1253 // A bit of optimization to help sort out the flickers.
1254 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1257 GetSize(& oldW
, & oldH
);
1258 GetPosition(& oldX
, & oldY
);
1261 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1269 wxSize
size(-1, -1);
1272 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1274 size
= DoGetBestSize();
1285 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1287 if( size
.x
== -1 ) size
= DoGetBestSize();
1296 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1297 || !wxNoOptimize::CanOptimize() )
1303 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1306 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1310 flags
|= wxMOVE_WIDTH
;
1313 flags
|= wxMOVE_HEIGHT
;
1315 int xx
= x
; int yy
= y
;
1316 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1318 DoMoveWindow( xx
, yy
, width
, height
);
1320 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1325 Widget widget
= (Widget
) GetTopWidget();
1329 bool managed
= XtIsManaged( widget
);
1331 XtUnmanageChild(widget
);
1335 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1337 DoMoveWindow(xx
, yy
, width
, height
);
1340 XtManageChild(widget
);
1344 void wxWindow::DoSetClientSize(int width
, int height
)
1348 Widget drawingArea
= (Widget
) m_drawingArea
;
1350 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1353 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1355 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1357 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1361 Widget widget
= (Widget
) GetTopWidget();
1364 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1366 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1369 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1374 Widget drawingArea
= (Widget
) m_drawingArea
;
1375 Widget borderOrScrolled
= m_borderWidget
?
1376 (Widget
) m_borderWidget
:
1377 (Widget
) m_scrolledWindow
;
1379 bool managed
= XtIsManaged(borderOrScrolled
);
1381 XtUnmanageChild (borderOrScrolled
);
1382 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1384 if (flags
& wxMOVE_X
)
1385 XtVaSetValues (borderOrScrolled
,
1388 if (flags
& wxMOVE_Y
)
1389 XtVaSetValues (borderOrScrolled
,
1393 if (flags
& wxMOVE_WIDTH
)
1397 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1398 short thick
, margin
;
1399 XtVaGetValues ((Widget
) m_borderWidget
,
1400 XmNshadowThickness
, &thick
,
1401 XmNmarginWidth
, &margin
,
1403 w
-= 2 * (thick
+ margin
);
1407 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1410 if (flags
& wxMOVE_HEIGHT
)
1414 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1415 short thick
, margin
;
1416 XtVaGetValues ((Widget
) m_borderWidget
,
1417 XmNshadowThickness
, &thick
,
1418 XmNmarginHeight
, &margin
,
1420 h
-= 2 * (thick
+ margin
);
1424 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1428 XtManageChild (borderOrScrolled
);
1429 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1433 if( xx
< 0 ) xx
= 0;
1434 if( yy
< 0 ) yy
= 0;
1438 XtVaSetValues((Widget
)GetTopWidget(),
1447 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1449 DoMoveWindowIntr (x
, y
, width
, height
,
1450 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1453 // ---------------------------------------------------------------------------
1455 // ---------------------------------------------------------------------------
1457 int wxWindow::GetCharHeight() const
1459 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1463 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1464 "x", NULL
, &height
, NULL
, NULL
);
1469 int wxWindow::GetCharWidth() const
1471 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1475 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1476 "x", &width
, NULL
, NULL
, NULL
);
1481 void wxWindow::GetTextExtent(const wxString
& string
,
1483 int *descent
, int *externalLeading
,
1484 const wxFont
*theFont
) const
1486 const wxFont
*fontToUse
= theFont
? theFont
: &m_font
;
1488 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1490 if (externalLeading
)
1491 *externalLeading
= 0;
1492 wxGetTextExtent (GetXDisplay(), *fontToUse
, 1.0,
1493 string
, x
, y
, NULL
, descent
);
1496 // ----------------------------------------------------------------------------
1498 // ----------------------------------------------------------------------------
1500 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1502 m_updateRegion
.Union( x
, y
, w
, h
);
1505 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1507 m_needsRefresh
= true;
1508 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1509 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1511 XExposeEvent dummyEvent
;
1513 GetSize(&width
, &height
);
1515 dummyEvent
.type
= Expose
;
1516 dummyEvent
.display
= display
;
1517 dummyEvent
.send_event
= True
;
1518 dummyEvent
.window
= thisWindow
;
1521 dummyEvent
.x
= rect
->x
;
1522 dummyEvent
.y
= rect
->y
;
1523 dummyEvent
.width
= rect
->width
;
1524 dummyEvent
.height
= rect
->height
;
1530 dummyEvent
.width
= width
;
1531 dummyEvent
.height
= height
;
1533 dummyEvent
.count
= 0;
1537 wxClientDC
dc(this);
1538 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1539 dc
.SetBackground(backgroundBrush
);
1546 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1549 void wxWindow::DoPaint()
1551 //TODO : make a temporary gc so we can do the XCopyArea below
1552 if (m_backingPixmap
&& !m_needsRefresh
)
1556 GC tempGC
= (GC
) dc
.GetBackingGC();
1558 Widget widget
= (Widget
) GetMainWidget();
1563 // We have to test whether it's a wxScrolledWindow (hack!) because
1564 // otherwise we don't know how many pixels have been scrolled. We might
1565 // solve this in the future by defining virtual wxWindow functions to get
1566 // the scroll position in pixels. Or, each kind of scrolled window has to
1567 // implement backing stores itself, using generic wxWidgets code.
1568 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1569 if ( scrolledWindow
)
1572 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1578 // TODO: This could be optimized further by only copying the areas in the
1579 // current update region.
1581 // Only blit the part visible in the client area. The backing pixmap
1582 // always starts at 0, 0 but we may be looking at only a portion of it.
1583 wxSize clientArea
= GetClientSize();
1584 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1585 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1587 // Copy whichever is samller, the amount of pixmap we have to copy,
1588 // or the size of the client area.
1589 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1590 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1592 // Make sure we're not negative
1593 toBlitX
= wxMax(0, toBlitX
);
1594 toBlitY
= wxMax(0, toBlitY
);
1599 (Pixmap
) m_backingPixmap
,
1602 scrollPosX
, scrollPosY
, // Start at the scroll position
1603 toBlitX
, toBlitY
, // How much of the pixmap to copy
1609 wxWindowDC
dc(this);
1610 // Set an erase event first
1611 wxEraseEvent
eraseEvent(GetId(), &dc
);
1612 eraseEvent
.SetEventObject(this);
1613 GetEventHandler()->ProcessEvent(eraseEvent
);
1615 wxPaintEvent
event(GetId());
1616 event
.SetEventObject(this);
1617 GetEventHandler()->ProcessEvent(event
);
1619 m_needsRefresh
= false;
1623 // ----------------------------------------------------------------------------
1625 // ----------------------------------------------------------------------------
1627 // Responds to colour changes: passes event on to children.
1628 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1630 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1633 // Only propagate to non-top-level windows
1634 wxWindow
*win
= node
->GetData();
1635 if ( win
->GetParent() )
1637 wxSysColourChangedEvent event2
;
1638 event
.SetEventObject(win
);
1639 win
->GetEventHandler()->ProcessEvent(event2
);
1642 node
= node
->GetNext();
1646 void wxWindow::OnInternalIdle()
1648 // This calls the UI-update mechanism (querying windows for
1649 // menu/toolbar/control state information)
1650 if (wxUpdateUIEvent::CanUpdate(this))
1651 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1654 // ----------------------------------------------------------------------------
1656 // ----------------------------------------------------------------------------
1658 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1661 if (!m_acceleratorTable
.Ok())
1664 int count
= m_acceleratorTable
.GetCount();
1665 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1667 for (i
= 0; i
< count
; i
++)
1669 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1670 if (entry
->MatchesEvent(event
))
1672 // Bingo, we have a match. Now find a control that matches the
1673 // entry command id.
1675 // Need to go up to the top of the window hierarchy, since it might
1676 // be e.g. a menu item
1677 wxWindow
* parent
= this;
1678 while ( parent
&& !parent
->IsTopLevel() )
1679 parent
= parent
->GetParent();
1684 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1688 // Try for a menu command
1689 if (frame
->GetMenuBar())
1691 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1694 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1695 commandEvent
.SetEventObject(frame
);
1697 // If ProcessEvent returns true (it was handled), then
1698 // the calling code will skip the event handling.
1699 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1705 // Find a child matching the command id
1706 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1712 // Now we process those kinds of windows that we can.
1713 // For now, only buttons.
1714 if ( wxDynamicCast(child
, wxButton
) )
1716 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1717 commandEvent
.SetEventObject(child
);
1718 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1726 // We didn't match the key event against an accelerator.
1730 // ============================================================================
1731 // Motif-specific stuff from here on
1732 // ============================================================================
1734 // ----------------------------------------------------------------------------
1735 // function which maintain the global hash table mapping Widgets to wxWidgets
1736 // ----------------------------------------------------------------------------
1738 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1740 const long key
= (long)w
;
1741 if ( wxWidgetHashTable
->Get(key
))
1743 wxLogDebug("Widget table clash: new widget is %ld, %s",
1744 key
, win
->GetClassInfo()->GetClassName());
1748 wxWidgetHashTable
->Put(key
, win
);
1750 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1751 w
, win
, win
->GetClassInfo()->GetClassName());
1756 wxWindow
*wxGetWindowFromTable(Widget w
)
1758 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1761 void wxDeleteWindowFromTable(Widget w
)
1763 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1765 wxWidgetHashTable
->Delete((long)w
);
1768 // ----------------------------------------------------------------------------
1769 // add/remove window from the table
1770 // ----------------------------------------------------------------------------
1772 // Add to hash table, add event handler
1773 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1774 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1776 wxAddWindowToTable((Widget
) mainWidget
, this);
1777 XtAddEventHandler( (Widget
) mainWidget
,
1778 ButtonPressMask
| ButtonReleaseMask
1779 | PointerMotionMask
,
1781 wxPanelItemEventHandler
,
1787 XtOverrideTranslations ((Widget
) mainWidget
,
1788 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1789 XtFree ((char *) ptr
);
1792 // Some widgets have a parent form widget, e.g. wxRadioBox
1795 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1799 XtOverrideTranslations ((Widget
) formWidget
,
1800 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1801 XtFree ((char *) ptr
);
1808 DoSetSize (x
, y
, width
, height
, wxSIZE_USE_EXISTING
);
1813 // Remove event handler, remove from hash table
1814 bool wxWindow::DetachWidget(WXWidget widget
)
1816 XtRemoveEventHandler( (Widget
) widget
,
1817 ButtonPressMask
| ButtonReleaseMask
1818 | PointerMotionMask
,
1820 wxPanelItemEventHandler
,
1823 wxDeleteWindowFromTable((Widget
) widget
);
1827 // ----------------------------------------------------------------------------
1828 // Motif-specific accessors
1829 // ----------------------------------------------------------------------------
1831 WXWindow
wxWindow::GetClientXWindow() const
1833 Widget wMain
= (Widget
)GetClientWidget();
1835 return (WXWindow
) XtWindow(wMain
);
1837 return (WXWindow
) 0;
1840 // Get the underlying X window
1841 WXWindow
wxWindow::GetXWindow() const
1843 Widget wMain
= (Widget
)GetMainWidget();
1845 return (WXWindow
) XtWindow(wMain
);
1847 return (WXWindow
) 0;
1850 // Get the underlying X display
1851 WXDisplay
*wxWindow::GetXDisplay() const
1853 Widget wMain
= (Widget
)GetMainWidget();
1855 return (WXDisplay
*) XtDisplay(wMain
);
1857 return (WXDisplay
*) NULL
;
1860 WXWidget
wxWindow::GetMainWidget() const
1863 return m_drawingArea
;
1865 return m_mainWidget
;
1868 WXWidget
wxWindow::GetClientWidget() const
1870 if (m_drawingArea
!= (WXWidget
) 0)
1871 return m_drawingArea
;
1873 return GetMainWidget();
1876 WXWidget
wxWindow::GetTopWidget() const
1878 return GetMainWidget();
1881 WXWidget
wxWindow::GetLabelWidget() const
1883 return GetMainWidget();
1886 // ----------------------------------------------------------------------------
1888 // ----------------------------------------------------------------------------
1890 // All widgets should have this as their resize proc.
1891 // OnSize sent to wxWindow via client data.
1892 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1893 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1895 wxWindow
*win
= wxGetWindowFromTable(w
);
1899 if (win
->PreResize())
1902 win
->GetSize(&width
, &height
);
1903 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1904 sizeEvent
.SetEventObject(win
);
1905 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1909 static void wxCanvasRepaintProc(Widget drawingArea
,
1910 XtPointer clientData
,
1911 XmDrawingAreaCallbackStruct
* cbs
)
1913 if (!wxGetWindowFromTable(drawingArea
))
1916 XEvent
* event
= cbs
->event
;
1917 wxWindow
* win
= (wxWindow
*) clientData
;
1919 switch (event
->type
)
1923 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1924 event
->xexpose
.width
, event
->xexpose
.height
);
1926 if (event
-> xexpose
.count
== 0)
1935 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1936 static void wxCanvasEnterLeave(Widget drawingArea
,
1937 XtPointer
WXUNUSED(clientData
),
1938 XCrossingEvent
* event
)
1940 XmDrawingAreaCallbackStruct cbs
;
1943 ((XCrossingEvent
&) ev
) = *event
;
1945 cbs
.reason
= XmCR_INPUT
;
1948 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1951 // Fix to make it work under Motif 1.0 (!)
1952 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1953 XButtonEvent
*WXUNUSED(event
))
1955 #if XmVersion <= 1000
1956 XmDrawingAreaCallbackStruct cbs
;
1959 ev
= *((XEvent
*) event
);
1960 cbs
.reason
= XmCR_INPUT
;
1963 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
1964 #endif // XmVersion <= 1000
1967 static void wxCanvasInputEvent(Widget drawingArea
,
1968 XtPointer
WXUNUSED(data
),
1969 XmDrawingAreaCallbackStruct
* cbs
)
1971 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
1972 XEvent
* xevent
= cbs
->event
;
1977 if (cbs
->reason
!= XmCR_INPUT
)
1980 switch (xevent
->xany
.type
)
1988 wxMouseEvent
wxevent(0);
1989 if (wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, xevent
))
1991 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
1997 wxKeyEvent
event (wxEVT_CHAR
);
1998 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, xevent
))
2000 // Implement wxFrame::OnCharHook by checking ancestor.
2001 wxWindow
*parent
= canvas
;
2002 while (parent
&& !parent
->IsTopLevel())
2003 parent
= parent
->GetParent();
2007 event
.SetEventType(wxEVT_CHAR_HOOK
);
2008 if (parent
->GetEventHandler()->ProcessEvent(event
))
2012 // For simplicity, OnKeyDown is the same as OnChar
2013 // TODO: filter modifier key presses from OnChar
2014 event
.SetEventType(wxEVT_KEY_DOWN
);
2016 // Only process OnChar if OnKeyDown didn't swallow it
2017 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2019 event
.SetEventType(wxEVT_CHAR
);
2020 canvas
->GetEventHandler()->ProcessEvent (event
);
2027 wxKeyEvent
event (wxEVT_KEY_UP
);
2028 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, xevent
))
2030 canvas
->GetEventHandler()->ProcessEvent (event
);
2036 if (xevent
->xfocus
.detail
!= NotifyPointer
)
2038 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2039 event
.SetEventObject(canvas
);
2040 canvas
->GetEventHandler()->ProcessEvent(event
);
2046 if (xevent
->xfocus
.detail
!= NotifyPointer
)
2048 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2049 event
.SetEventObject(canvas
);
2050 canvas
->GetEventHandler()->ProcessEvent(event
);
2059 static void wxPanelItemEventHandler(Widget wid
,
2060 XtPointer
WXUNUSED(client_data
),
2062 Boolean
*continueToDispatch
)
2064 // Widget can be a label or the actual widget.
2066 wxWindow
*window
= wxGetWindowFromTable(wid
);
2069 wxMouseEvent
wxevent(0);
2070 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2072 window
->GetEventHandler()->ProcessEvent(wxevent
);
2076 // TODO: probably the key to allowing default behaviour to happen. Say we
2077 // set a m_doDefault flag to false at the start of this function. Then in
2078 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2079 // true, indicating that default processing can happen. Thus, behaviour can
2080 // appear to be overridden just by adding an event handler and not calling
2081 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2082 // way of handling drawing area events, to simplify things.
2083 *continueToDispatch
= True
;
2086 static void wxScrollBarCallback(Widget scrollbar
,
2087 XtPointer clientData
,
2088 XmScrollBarCallbackStruct
*cbs
)
2090 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2091 wxOrientation orientation
= (wxOrientation
)wxPtrToUInt(clientData
);
2093 wxEventType eventType
= wxEVT_NULL
;
2094 switch (cbs
->reason
)
2096 case XmCR_INCREMENT
:
2098 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2101 case XmCR_DECREMENT
:
2103 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2108 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2111 case XmCR_VALUE_CHANGED
:
2113 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2116 case XmCR_PAGE_INCREMENT
:
2118 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2121 case XmCR_PAGE_DECREMENT
:
2123 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2128 eventType
= wxEVT_SCROLLWIN_TOP
;
2131 case XmCR_TO_BOTTOM
:
2133 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2138 // Should never get here
2139 wxFAIL_MSG("Unknown scroll event.");
2144 wxScrollWinEvent
event(eventType
,
2147 event
.SetEventObject( win
);
2148 win
->GetEventHandler()->ProcessEvent(event
);
2151 // For repainting arbitrary windows
2152 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2154 wxWindow
* win
= wxGetWindowFromTable(w
);
2158 switch ( event
->type
)
2162 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2163 event
->xexpose
.width
, event
->xexpose
.height
);
2165 if ( event
->xexpose
.count
== 0 )
2175 // ----------------------------------------------------------------------------
2176 // TranslateXXXEvent() functions
2177 // ----------------------------------------------------------------------------
2179 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2180 Widget widget
, const XEvent
*xevent
)
2182 switch (xevent
->xany
.type
)
2187 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2188 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2189 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2195 int eventx
= xevent
->xbutton
.x
, eventy
= xevent
->xbutton
.y
;
2197 wxEventType eventType
= wxEVT_NULL
;
2199 if (xevent
->xany
.type
== LeaveNotify
)
2201 eventType
= wxEVT_LEAVE_WINDOW
;
2203 if (xevent
->xany
.type
== EnterNotify
)
2205 eventType
= wxEVT_ENTER_WINDOW
;
2207 else if (xevent
->xany
.type
== MotionNotify
)
2209 eventType
= wxEVT_MOTION
;
2211 if (xevent
->xmotion
.is_hint
== NotifyHint
)
2216 Display
*dpy
= XtDisplay (widget
);
2218 XQueryPointer (dpy
, XtWindow (widget
),
2220 &x_root
, &y_root
, &eventx
, &eventy
, &state
);
2223 else if (xevent
->xany
.type
== ButtonPress
)
2225 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2227 if (xevent
->xbutton
.button
== Button1
)
2229 eventType
= wxEVT_LEFT_DOWN
;
2232 else if (xevent
->xbutton
.button
== Button2
)
2234 eventType
= wxEVT_MIDDLE_DOWN
;
2237 else if (xevent
->xbutton
.button
== Button3
)
2239 eventType
= wxEVT_RIGHT_DOWN
;
2243 // check for a double click
2245 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2246 long ts
= wxevent
.GetTimestamp();
2248 int buttonLast
= win
->GetLastClickedButton();
2249 long lastTS
= win
->GetLastClickTime();
2250 if ( buttonLast
&& buttonLast
== button
&&
2251 (ts
- lastTS
) < dclickTime
)
2254 win
->SetLastClick(0, ts
);
2255 if ( eventType
== wxEVT_LEFT_DOWN
)
2256 eventType
= wxEVT_LEFT_DCLICK
;
2257 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2258 eventType
= wxEVT_MIDDLE_DCLICK
;
2259 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2260 eventType
= wxEVT_RIGHT_DCLICK
;
2264 // not fast enough or different button
2265 win
->SetLastClick(button
, ts
);
2268 else if (xevent
->xany
.type
== ButtonRelease
)
2270 if (xevent
->xbutton
.button
== Button1
)
2272 eventType
= wxEVT_LEFT_UP
;
2274 else if (xevent
->xbutton
.button
== Button2
)
2276 eventType
= wxEVT_MIDDLE_UP
;
2278 else if (xevent
->xbutton
.button
== Button3
)
2280 eventType
= wxEVT_RIGHT_UP
;
2290 wxevent
.SetEventType(eventType
);
2293 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2296 win
->GetPosition(&x2
, &y2
);
2298 // The button x/y must be translated to wxWidgets
2299 // window space - the widget might be a label or button,
2303 if (widget
!= (Widget
)win
->GetMainWidget())
2309 wxevent
.m_x
= eventx
+ dx
;
2310 wxevent
.m_y
= eventy
+ dy
;
2312 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2313 || (event_left_is_down (xevent
)
2314 && (eventType
!= wxEVT_LEFT_UP
)));
2315 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2316 || (event_middle_is_down (xevent
)
2317 && (eventType
!= wxEVT_MIDDLE_UP
)));
2318 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2319 || (event_right_is_down (xevent
)
2320 && (eventType
!= wxEVT_RIGHT_UP
)));
2322 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2323 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2324 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2325 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2327 wxevent
.SetId(win
->GetId());
2328 wxevent
.SetEventObject(win
);
2336 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2337 Widget
WXUNUSED(widget
), const XEvent
*xevent
)
2339 switch (xevent
->xany
.type
)
2347 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2348 int id
= wxCharCodeXToWX (keySym
);
2349 // id may be WXK_xxx code - these are outside ASCII range, so we
2350 // can't just use toupper() on id
2351 if (id
>= 'a' && id
<= 'z')
2354 if (xevent
->xkey
.state
& ShiftMask
)
2355 wxevent
.m_shiftDown
= true;
2356 if (xevent
->xkey
.state
& ControlMask
)
2357 wxevent
.m_controlDown
= true;
2358 if (xevent
->xkey
.state
& Mod3Mask
)
2359 wxevent
.m_altDown
= true;
2360 if (xevent
->xkey
.state
& Mod1Mask
)
2361 wxevent
.m_metaDown
= true;
2362 wxevent
.SetEventObject(win
);
2363 wxevent
.m_keyCode
= id
;
2364 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2366 wxevent
.m_x
= xevent
->xbutton
.x
;
2367 wxevent
.m_y
= xevent
->xbutton
.y
;
2381 // ----------------------------------------------------------------------------
2383 // ----------------------------------------------------------------------------
2385 #define YAllocColor XAllocColor
2386 XColor g_itemColors
[5];
2387 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2390 static XmColorProc colorProc
;
2392 result
= wxNO_COLORS
;
2396 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2397 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2398 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2399 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2400 if (colorProc
== (XmColorProc
) NULL
)
2402 // Get a ptr to the actual function
2403 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2404 // And set it back to motif.
2405 XmSetColorCalculation (colorProc
);
2407 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2408 &g_itemColors
[wxFORE_INDEX
],
2409 &g_itemColors
[wxSELE_INDEX
],
2410 &g_itemColors
[wxTOPS_INDEX
],
2411 &g_itemColors
[wxBOTS_INDEX
]);
2412 result
= wxBACK_COLORS
;
2416 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2417 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2418 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2419 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2420 if (result
== wxNO_COLORS
)
2421 result
= wxFORE_COLORS
;
2424 Display
*dpy
= display
;
2425 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2429 /* 5 Colours to allocate */
2430 for (int i
= 0; i
< 5; i
++)
2431 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2432 result
= wxNO_COLORS
;
2436 /* Only 1 colour to allocate */
2437 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2438 result
= wxNO_COLORS
;
2444 // Changes the foreground and background colours to be derived from the current
2445 // background colour. To change the foreground colour, you must call
2446 // SetForegroundColour explicitly.
2447 void wxWindow::ChangeBackgroundColour()
2449 WXWidget mainWidget
= GetMainWidget();
2451 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2452 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2453 wxDoChangeForegroundColour(m_scrolledWindow
, m_backgroundColour
);
2456 void wxWindow::ChangeForegroundColour()
2458 WXWidget mainWidget
= GetMainWidget();
2460 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2461 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2462 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2465 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2467 if ( !wxWindowBase::SetBackgroundColour(col
) )
2470 ChangeBackgroundColour();
2475 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2477 if ( !wxWindowBase::SetForegroundColour(col
) )
2480 ChangeForegroundColour();
2485 void wxWindow::ChangeFont(bool keepOriginalSize
)
2487 // Note that this causes the widget to be resized back
2488 // to its original size! We therefore have to set the size
2489 // back again. TODO: a better way in Motif?
2490 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2491 if (w
&& m_font
.Ok())
2493 int width
, height
, width1
, height1
;
2494 GetSize(& width
, & height
);
2496 wxDoChangeFont( GetLabelWidget(), m_font
);
2498 GetSize(& width1
, & height1
);
2499 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2501 SetSize(-1, -1, width
, height
);
2506 // ----------------------------------------------------------------------------
2508 // ----------------------------------------------------------------------------
2510 wxWindow
*wxGetActiveWindow()
2513 wxFAIL_MSG("Not implemented");
2518 wxWindow
*wxWindowBase::GetCapture()
2520 return (wxWindow
*)g_captureWindow
;
2524 // Find the wxWindow at the current mouse position, returning the mouse
2526 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2528 return wxFindWindowAtPoint(wxGetMousePosition());
2531 // Get the current mouse position.
2532 wxPoint
wxGetMousePosition()
2534 Display
*display
= wxGlobalDisplay();
2535 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2536 Window rootReturn
, childReturn
;
2537 int rootX
, rootY
, winX
, winY
;
2538 unsigned int maskReturn
;
2540 XQueryPointer (display
,
2544 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2545 return wxPoint(rootX
, rootY
);
2549 // ----------------------------------------------------------------------------
2550 // wxNoOptimize: switch off size optimization
2551 // ----------------------------------------------------------------------------
2553 int wxNoOptimize::ms_count
= 0;