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"
25 #define XtDisplay XTDISPLAY
26 #define XtWindow XTWINDOW
27 #define XtScreen XTSCREEN
33 #include "wx/dcclient.h"
36 #include "wx/layout.h"
37 #include "wx/button.h"
38 #include "wx/settings.h"
40 #include "wx/scrolwin.h"
41 #include "wx/module.h"
42 #include "wx/menuitem.h"
44 #include "wx/evtloop.h"
47 #if wxUSE_DRAG_AND_DROP
51 // DoSetSizeIntr and DoMoveWindowIntr
53 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
54 // did nott work and/or segfaulted because
55 // 1) wxWindow::Create calls SetSize,
56 // which results in a call to DoSetSize much earlier than in the other ports
57 // 2) if wxWindow::Create is called (wxControl::Create calls it)
58 // then DoSetSize is never called, causing layout problems in composite
62 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
63 // DoSetPosition directly or indirectly from wxWindow::Create
64 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
67 #pragma message disable nosimpint
71 #include <Xm/DrawingA.h>
72 #include <Xm/ScrolledW.h>
73 #include <Xm/ScrollBar.h>
76 #include <Xm/RowColumn.h> // for XmMenuPosition
78 #pragma message enable nosimpint
81 #include "wx/motif/private.h"
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 static const int SCROLL_MARGIN
= 4;
91 // ----------------------------------------------------------------------------
92 // global variables for this module
93 // ----------------------------------------------------------------------------
95 extern wxHashTable
*wxWidgetHashTable
;
96 static wxWindow
* g_captureWindow
= NULL
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 static void wxCanvasRepaintProc(Widget
, XtPointer
, XmDrawingAreaCallbackStruct
* cbs
);
104 static void wxCanvasInputEvent(Widget drawingArea
, XtPointer data
, XmDrawingAreaCallbackStruct
* cbs
);
105 static void wxCanvasMotionEvent(Widget
, XButtonEvent
* event
);
106 static void wxCanvasEnterLeave(Widget drawingArea
, XtPointer clientData
, XCrossingEvent
* event
);
107 static void wxScrollBarCallback(Widget widget
, XtPointer clientData
,
108 XmScrollBarCallbackStruct
*cbs
);
109 static void wxPanelItemEventHandler(Widget wid
,
110 XtPointer client_data
,
112 Boolean
*continueToDispatch
);
117 // Helper function for 16-bit fonts
118 static int str16len(const char *s
)
122 while (s
[0] && s
[1]) {
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
137 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
138 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxWindow
, wxWindowBase
)
146 BEGIN_EVENT_TABLE(wxWindow
, wxWindowBase
)
147 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged
)
150 // ============================================================================
152 // ============================================================================
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
160 Widget w
= (Widget
)widget
;
168 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
170 Widget w
= (Widget
)widget
;
174 // Rationale: a lot of common operations (including but not
175 // limited to moving, resizing and appending items to a listbox)
176 // unmamange the widget, do their work, then manage it again.
177 // This means that, for example adding an item to a listbox will show it,
178 // or that most controls are shown every time they are moved or resized!
179 XtSetMappedWhenManaged( w
, domap
);
181 // if the widget is not unmanaged, it still intercepts
182 // mouse events, even if it is not mapped (and hence invisible)
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 void wxWindow::Init()
204 m_needsRefresh
= TRUE
;
205 m_mainWidget
= (WXWidget
) 0;
207 m_winCaptured
= FALSE
;
215 m_drawingArea
= (WXWidget
) 0;
220 m_backingPixmap
= (WXPixmap
) 0;
231 // real construction (Init() must have been called before!)
232 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
236 const wxString
& name
)
238 wxCHECK_MSG( parent
, FALSE
, "can't create wxWindow without parent" );
240 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
242 parent
->AddChild(this);
244 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
245 m_foregroundColour
= *wxBLACK
;
247 //// TODO: we should probably optimize by only creating a
248 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
249 //// But for now, let's simplify things by always creating the
250 //// drawing area, since otherwise the translations are different.
252 // New translations for getting mouse motion feedback
253 static const String translations
=
254 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
255 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
256 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
257 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
258 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
259 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
260 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
261 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
262 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
263 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
264 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
265 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
266 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
267 <Key>: DrawingAreaInput()";
269 XtActionsRec actions
[1];
270 actions
[0].string
= "wxCanvasMotionEvent";
271 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
272 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
274 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
275 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
277 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
280 xmScrolledWindowWidgetClass
,
281 m_borderWidget
? (Widget
) m_borderWidget
283 XmNresizePolicy
, XmRESIZE_NONE
,
285 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
286 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
290 XtTranslations ptr
= XtParseTranslationTable(translations
);
291 m_drawingArea
= (WXWidget
)XtVaCreateWidget
294 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
295 XmNunitType
, XmPIXELS
,
296 // XmNresizePolicy, XmRESIZE_ANY,
297 XmNresizePolicy
, XmRESIZE_NONE
,
300 XmNtranslations
, ptr
,
303 XtFree((char *) ptr
);
306 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
308 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
309 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
310 XtFree ((char *) ptr
);
314 wxAddWindowToTable((Widget
) m_drawingArea
, this);
315 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
317 // This order is very important in Motif 1.2.1
318 XtRealizeWidget ((Widget
) m_scrolledWindow
);
319 XtRealizeWidget ((Widget
) m_drawingArea
);
320 XtManageChild ((Widget
) m_drawingArea
);
322 ptr
= XtParseTranslationTable("<Configure>: resize()");
323 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
324 XtFree ((char *) ptr
);
326 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
327 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
330 (Widget
)m_drawingArea
,
331 PointerMotionHintMask
| EnterWindowMask
|
332 LeaveWindowMask
| FocusChangeMask
,
334 (XtEventHandler
) wxCanvasEnterLeave
,
338 // Scrolled widget needs to have its colour changed or we get a little blue
339 // square where the scrollbars abutt
340 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
341 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, TRUE
);
342 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, TRUE
);
344 XmScrolledWindowSetAreas(
345 (Widget
)m_scrolledWindow
,
346 (Widget
) 0, (Widget
) 0,
347 (Widget
) m_drawingArea
);
349 // Without this, the cursor may not be restored properly (e.g. in splitter
351 SetCursor(*wxSTANDARD_CURSOR
);
352 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
353 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, TRUE
);
358 wxWindow::~wxWindow()
360 if (g_captureWindow
== this)
361 g_captureWindow
= NULL
;
363 m_isBeingDeleted
= TRUE
;
365 // Motif-specific actions first
366 WXWidget wMain
= GetMainWidget();
369 // Removes event handlers
373 // If m_drawingArea, we're a fully-fledged window with drawing area,
374 // scrollbars etc. (what wxCanvas used to be)
377 // Destroy children before destroying self
381 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
383 Widget w
= (Widget
) m_drawingArea
;
384 wxDeleteWindowFromTable(w
);
389 m_drawingArea
= (WXWidget
) 0;
392 // Only if we're _really_ a canvas (not a dialog box/panel)
393 if (m_scrolledWindow
)
395 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
400 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
401 XtUnmanageChild((Widget
) m_hScrollBar
);
405 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
406 XtUnmanageChild((Widget
) m_vScrollBar
);
410 XtDestroyWidget((Widget
) m_hScrollBar
);
412 XtDestroyWidget((Widget
) m_vScrollBar
);
414 UnmanageAndDestroy(m_scrolledWindow
);
418 XtDestroyWidget ((Widget
) m_borderWidget
);
419 m_borderWidget
= (WXWidget
) 0;
422 else // Why wasn't this here before? JACS 8/3/2000
426 // Destroy the window
429 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
430 // Child widgets will get destroyed automatically when a frame
431 // or dialog is destroyed, but before that you may get some memory
432 // leaks and potential layout problems if you delete and then add
435 // GRG, Feb/2000: commented this out when adding support for
436 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
437 // that this call crashed wxMotif under OS/2, so it seems
438 // that leaving it out is the right thing to do.
439 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
440 XtDestroyWidget((Widget
) GetMainWidget());
441 SetMainWidget((WXWidget
) NULL
);
445 // ----------------------------------------------------------------------------
446 // scrollbar management
447 // ----------------------------------------------------------------------------
449 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
450 wxOrientation orientation
,
453 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
455 XtVaCreateManagedWidget( "scrollBarWidget",
456 xmScrollBarWidgetClass
, (Widget
)parent
,
457 XmNorientation
, orient
,
462 XtPointer o
= (XtPointer
)orientation
;
463 XtCallbackProc cb
= (XtCallbackProc
)callback
;
465 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
466 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
467 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
468 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
469 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
470 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
471 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
472 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
478 void wxWindow::CreateScrollbar(wxOrientation orientation
)
480 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
482 XtVaSetValues( (Widget
) m_scrolledWindow
,
483 XmNresizePolicy
, XmRESIZE_NONE
,
486 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
487 // Add scrollbars if required
488 if (orientation
== wxHORIZONTAL
)
490 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
491 (void (*)())wxScrollBarCallback
);
493 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, TRUE
);
495 XtRealizeWidget( (Widget
)m_hScrollBar
);
497 XtVaSetValues((Widget
) m_scrolledWindow
,
498 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
501 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
503 else if (orientation
== wxVERTICAL
)
505 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
506 (void (*)())wxScrollBarCallback
);
508 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, TRUE
);
510 XtRealizeWidget((Widget
)m_vScrollBar
);
512 XtVaSetValues((Widget
) m_scrolledWindow
,
513 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
516 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
519 XtVaSetValues( (Widget
) m_scrolledWindow
,
520 XmNresizePolicy
, XmRESIZE_ANY
,
524 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
526 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
528 XtVaSetValues((Widget
) m_scrolledWindow
,
529 XmNresizePolicy
, XmRESIZE_NONE
,
531 String stringSB
= orientation
== wxHORIZONTAL
?
532 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
533 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
534 &m_hScrollBar
: &m_vScrollBar
;
538 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
539 XtDestroyWidget( (Widget
)*widgetSB
);
540 *widgetSB
= (WXWidget
)NULL
;
543 XtVaSetValues( (Widget
)m_scrolledWindow
,
544 stringSB
, (Widget
) 0,
547 XtVaSetValues((Widget
) m_scrolledWindow
,
548 XmNresizePolicy
, XmRESIZE_ANY
,
552 // ---------------------------------------------------------------------------
554 // ---------------------------------------------------------------------------
556 void wxWindow::SetFocus()
558 Widget wMain
= (Widget
) GetMainWidget();
559 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
560 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
563 // Get the window with the focus
564 wxWindow
*wxWindowBase::FindFocus()
567 // (1) Can there be multiple focussed widgets in an application?
568 // In which case we need to find the top-level window that's
570 // (2) The widget with the focus may not be in the widget table
571 // depending on which widgets I put in the table
572 wxWindow
*winFocus
= (wxWindow
*)NULL
;
573 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
575 node
= node
->GetNext() )
577 wxWindow
*win
= node
->GetData();
579 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
581 if (w
!= (Widget
) NULL
)
583 winFocus
= wxGetWindowFromTable(w
);
592 bool wxWindow::Enable(bool enable
)
594 if ( !wxWindowBase::Enable(enable
) )
597 Widget wMain
= (Widget
)GetMainWidget();
600 XtSetSensitive(wMain
, enable
);
601 XmUpdateDisplay(wMain
);
607 bool wxWindow::Show(bool show
)
609 if ( !wxWindowBase::Show(show
) )
612 if (m_borderWidget
|| m_scrolledWindow
)
614 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
615 // MapOrUnmap(m_drawingArea, show);
619 if ( !MapOrUnmap(GetTopWidget(), show
) )
620 MapOrUnmap(GetMainWidget(), show
);
626 // Raise the window to the top of the Z order
627 void wxWindow::Raise()
629 Widget wTop
= (Widget
) GetTopWidget();
630 Window window
= XtWindow(wTop
);
631 XRaiseWindow(XtDisplay(wTop
), window
);
634 // Lower the window to the bottom of the Z order
635 void wxWindow::Lower()
637 Widget wTop
= (Widget
) GetTopWidget();
638 Window window
= XtWindow(wTop
);
639 XLowerWindow(XtDisplay(wTop
), window
);
642 void wxWindow::SetTitle(const wxString
& title
)
644 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
647 wxString
wxWindow::GetTitle() const
650 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
652 return wxString(title
);
655 void wxWindow::DoCaptureMouse()
657 g_captureWindow
= this;
661 Widget wMain
= (Widget
)GetMainWidget();
663 XtAddGrab(wMain
, TRUE
, FALSE
);
665 m_winCaptured
= TRUE
;
668 void wxWindow::DoReleaseMouse()
670 g_captureWindow
= NULL
;
671 if ( !m_winCaptured
)
674 Widget wMain
= (Widget
)GetMainWidget();
678 m_winCaptured
= FALSE
;
681 bool wxWindow::SetFont(const wxFont
& font
)
683 if ( !wxWindowBase::SetFont(font
) )
694 bool wxWindow::SetCursor(const wxCursor
& cursor
)
696 if ( !wxWindowBase::SetCursor(cursor
) )
702 // wxASSERT_MSG( m_cursor.Ok(),
703 // wxT("cursor must be valid after call to the base version"));
704 wxCursor
* cursor2
= NULL
;
706 cursor2
= & m_cursor
;
708 cursor2
= wxSTANDARD_CURSOR
;
710 WXDisplay
*dpy
= GetXDisplay();
711 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
713 Widget w
= (Widget
) GetMainWidget();
714 Window win
= XtWindow(w
);
715 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
720 // Coordinates relative to the window
721 void wxWindow::WarpPointer (int x
, int y
)
723 Widget wClient
= (Widget
)GetClientWidget();
725 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
728 // ---------------------------------------------------------------------------
730 // ---------------------------------------------------------------------------
732 int wxWindow::GetScrollPos(int orient
) const
734 if (orient
== wxHORIZONTAL
)
740 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
744 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
752 // This now returns the whole range, not just the number of positions that we
754 int wxWindow::GetScrollRange(int orient
) const
756 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
757 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
761 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
765 int wxWindow::GetScrollThumb(int orient
) const
767 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
768 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 Widget widget
= (Widget
) GetMainWidget();
1074 /* The menuId field seems to be usused, so we'll use it to
1075 indicate whether a menu is popped up or not:
1076 0: Not currently created as a popup
1077 -1: Created as a popup, but not active
1081 if (menu
->GetParent() && (menu
->GetId() != -1))
1084 if (menu
->GetMainWidget())
1086 menu
->DestroyMenu(TRUE
);
1089 menu
->SetId(1); /* Mark as popped-up */
1090 menu
->CreateMenu(NULL
, widget
, menu
);
1091 menu
->SetInvokingWindow(this);
1095 // menu->SetParent(parent);
1096 // parent->children->Append(menu); // Store menu for later deletion
1098 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1106 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1108 wxCanvas *canvas = (wxCanvas *) this;
1109 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1110 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1114 Display
*display
= XtDisplay (widget
);
1115 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1116 Window thisWindow
= XtWindow (widget
);
1118 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1119 &rootX
, &rootY
, &childWindow
);
1121 XButtonPressedEvent event
;
1122 event
.type
= ButtonPress
;
1128 event
.x_root
= rootX
;
1129 event
.y_root
= rootY
;
1131 XmMenuPosition (menuWidget
, &event
);
1132 XtManageChild (menuWidget
);
1134 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1135 // idle-time destroy routine.
1136 // Waiting until this ID changes causes this function to block until
1137 // the menu has been dismissed and the widgets cleaned up.
1138 // In other words, once this routine returns, it is safe to delete
1140 // Ian Brown <ian.brown@printsoft.de>
1142 wxEventLoop evtLoop
;
1144 while (menu
->GetId() == 1)
1146 wxDoEventLoopIteration( evtLoop
);
1154 // ---------------------------------------------------------------------------
1155 // moving and resizing
1156 // ---------------------------------------------------------------------------
1158 bool wxWindow::PreResize()
1164 void wxWindow::DoGetSize(int *x
, int *y
) const
1166 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1167 ( m_borderWidget
? m_borderWidget
:
1168 m_scrolledWindow
? m_scrolledWindow
:
1172 XtVaGetValues( widget
,
1180 void wxWindow::DoGetPosition(int *x
, int *y
) const
1182 Widget widget
= (Widget
)
1184 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1188 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1190 // We may be faking the client origin. So a window that's really at (0, 30)
1191 // may appear (to wxWin apps) to be at (0, 0).
1194 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1203 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1205 Widget widget
= (Widget
) GetClientWidget();
1206 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1207 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1208 Window thisWindow
= XtWindow(widget
);
1213 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1216 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1218 Widget widget
= (Widget
) GetClientWidget();
1219 Display
*display
= XtDisplay(widget
);
1220 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1221 Window thisWindow
= XtWindow(widget
);
1226 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1230 // Get size *available for subwindows* i.e. excluding menu bar etc.
1231 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1233 Widget widget
= (Widget
) GetClientWidget();
1235 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1236 if(x
) *x
= xx
; if(y
) *y
= yy
;
1239 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1241 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, FALSE
);
1244 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1245 int sizeFlags
, bool fromCtor
)
1247 // A bit of optimization to help sort out the flickers.
1248 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1251 GetSize(& oldW
, & oldH
);
1252 GetPosition(& oldX
, & oldY
);
1255 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1263 wxSize
size(-1, -1);
1266 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1268 size
= DoGetBestSize();
1279 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1281 if( size
.x
== -1 ) size
= DoGetBestSize();
1290 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1291 || !wxNoOptimize::CanOptimize() )
1297 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1300 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1304 flags
|= wxMOVE_WIDTH
;
1307 flags
|= wxMOVE_HEIGHT
;
1309 int xx
= x
; int yy
= y
;
1310 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1312 DoMoveWindow( xx
, yy
, width
, height
);
1314 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1319 Widget widget
= (Widget
) GetTopWidget();
1323 bool managed
= XtIsManaged( widget
);
1325 XtUnmanageChild(widget
);
1329 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1331 DoMoveWindow(xx
, yy
, width
, height
);
1334 XtManageChild(widget
);
1338 void wxWindow::DoSetClientSize(int width
, int height
)
1342 Widget drawingArea
= (Widget
) m_drawingArea
;
1344 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1347 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1349 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1351 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1355 Widget widget
= (Widget
) GetTopWidget();
1358 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1360 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1363 // For implementation purposes - sometimes decorations make the client area
1365 wxPoint
wxWindow::GetClientAreaOrigin() const
1367 return wxPoint(0, 0);
1370 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1375 Widget drawingArea
= (Widget
) m_drawingArea
;
1376 Widget borderOrScrolled
= m_borderWidget
?
1377 (Widget
) m_borderWidget
:
1378 (Widget
) m_scrolledWindow
;
1380 bool managed
= XtIsManaged(borderOrScrolled
);
1382 XtUnmanageChild (borderOrScrolled
);
1383 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1385 if (flags
& wxMOVE_X
)
1386 XtVaSetValues (borderOrScrolled
,
1389 if (flags
& wxMOVE_Y
)
1390 XtVaSetValues (borderOrScrolled
,
1394 if (flags
& wxMOVE_WIDTH
)
1398 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1399 short thick
, margin
;
1400 XtVaGetValues ((Widget
) m_borderWidget
,
1401 XmNshadowThickness
, &thick
,
1402 XmNmarginWidth
, &margin
,
1404 w
-= 2 * (thick
+ margin
);
1408 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1411 if (flags
& wxMOVE_HEIGHT
)
1415 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1416 short thick
, margin
;
1417 XtVaGetValues ((Widget
) m_borderWidget
,
1418 XmNshadowThickness
, &thick
,
1419 XmNmarginHeight
, &margin
,
1421 h
-= 2 * (thick
+ margin
);
1425 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1429 XtManageChild (borderOrScrolled
);
1430 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1437 XtVaSetValues((Widget
)GetTopWidget(),
1446 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1448 DoMoveWindowIntr (x
, y
, width
, height
,
1449 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1452 // ---------------------------------------------------------------------------
1454 // ---------------------------------------------------------------------------
1456 int wxWindow::GetCharHeight() const
1458 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1460 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1462 int direction
, ascent
, descent
;
1463 XCharStruct overall
;
1464 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1465 &descent
, &overall
);
1467 // return (overall.ascent + overall.descent);
1468 return (ascent
+ descent
);
1471 int wxWindow::GetCharWidth() const
1473 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1475 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1477 int direction
, ascent
, descent
;
1478 XCharStruct overall
;
1479 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1480 &descent
, &overall
);
1482 return overall
.width
;
1485 void wxWindow::GetTextExtent(const wxString
& string
,
1487 int *descent
, int *externalLeading
,
1488 const wxFont
*theFont
) const
1490 wxFont
*fontToUse
= (wxFont
*)theFont
;
1492 fontToUse
= (wxFont
*) & m_font
;
1494 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1496 WXFontStructPtr pFontStruct
= fontToUse
->GetFontStruct(1.0, GetXDisplay());
1498 int direction
, ascent
, descent2
;
1499 XCharStruct overall
;
1500 int slen
= string
.Len();
1504 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
1505 &ascent
, &descent2
, &overall
);
1508 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
1509 &direction
, &ascent
, &descent2
, &overall
);
1512 *x
= (overall
.width
);
1514 *y
= (ascent
+ descent2
);
1516 *descent
= descent2
;
1517 if (externalLeading
)
1518 *externalLeading
= 0;
1522 // ----------------------------------------------------------------------------
1524 // ----------------------------------------------------------------------------
1526 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1528 m_updateRegion
.Union( x
, y
, w
, h
);
1531 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1533 m_needsRefresh
= TRUE
;
1534 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1535 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1537 XExposeEvent dummyEvent
;
1539 GetSize(&width
, &height
);
1541 dummyEvent
.type
= Expose
;
1542 dummyEvent
.display
= display
;
1543 dummyEvent
.send_event
= True
;
1544 dummyEvent
.window
= thisWindow
;
1547 dummyEvent
.x
= rect
->x
;
1548 dummyEvent
.y
= rect
->y
;
1549 dummyEvent
.width
= rect
->width
;
1550 dummyEvent
.height
= rect
->height
;
1556 dummyEvent
.width
= width
;
1557 dummyEvent
.height
= height
;
1559 dummyEvent
.count
= 0;
1563 wxClientDC
dc(this);
1564 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1565 dc
.SetBackground(backgroundBrush
);
1572 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1575 void wxWindow::DoPaint()
1577 //TODO : make a temporary gc so we can do the XCopyArea below
1578 if (m_backingPixmap
&& !m_needsRefresh
)
1582 GC tempGC
= (GC
) dc
.GetBackingGC();
1584 Widget widget
= (Widget
) GetMainWidget();
1589 // We have to test whether it's a wxScrolledWindow (hack!) because
1590 // otherwise we don't know how many pixels have been scrolled. We might
1591 // solve this in the future by defining virtual wxWindow functions to get
1592 // the scroll position in pixels. Or, each kind of scrolled window has to
1593 // implement backing stores itself, using generic wxWindows code.
1594 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1595 if ( scrolledWindow
)
1598 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1604 // TODO: This could be optimized further by only copying the areas in the
1605 // current update region.
1607 // Only blit the part visible in the client area. The backing pixmap
1608 // always starts at 0, 0 but we may be looking at only a portion of it.
1609 wxSize clientArea
= GetClientSize();
1610 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1611 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1613 // Copy whichever is samller, the amount of pixmap we have to copy,
1614 // or the size of the client area.
1615 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1616 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1618 // Make sure we're not negative
1619 toBlitX
= wxMax(0, toBlitX
);
1620 toBlitY
= wxMax(0, toBlitY
);
1625 (Pixmap
) m_backingPixmap
,
1628 scrollPosX
, scrollPosY
, // Start at the scroll position
1629 toBlitX
, toBlitY
, // How much of the pixmap to copy
1635 wxWindowDC
dc(this);
1636 // Set an erase event first
1637 wxEraseEvent
eraseEvent(GetId(), &dc
);
1638 eraseEvent
.SetEventObject(this);
1639 GetEventHandler()->ProcessEvent(eraseEvent
);
1641 wxPaintEvent
event(GetId());
1642 event
.SetEventObject(this);
1643 GetEventHandler()->ProcessEvent(event
);
1645 m_needsRefresh
= FALSE
;
1649 // ----------------------------------------------------------------------------
1651 // ----------------------------------------------------------------------------
1653 // Responds to colour changes: passes event on to children.
1654 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1656 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1659 // Only propagate to non-top-level windows
1660 wxWindow
*win
= node
->GetData();
1661 if ( win
->GetParent() )
1663 wxSysColourChangedEvent event2
;
1664 event
.m_eventObject
= win
;
1665 win
->GetEventHandler()->ProcessEvent(event2
);
1668 node
= node
->GetNext();
1672 void wxWindow::OnInternalIdle()
1674 // This calls the UI-update mechanism (querying windows for
1675 // menu/toolbar/control state information)
1676 if (wxUpdateUIEvent::CanUpdate(this))
1677 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1680 // ----------------------------------------------------------------------------
1682 // ----------------------------------------------------------------------------
1684 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1687 if (!m_acceleratorTable
.Ok())
1690 int count
= m_acceleratorTable
.GetCount();
1691 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1693 for (i
= 0; i
< count
; i
++)
1695 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1696 if (entry
->MatchesEvent(event
))
1698 // Bingo, we have a match. Now find a control that matches the
1699 // entry command id.
1701 // Need to go up to the top of the window hierarchy, since it might
1702 // be e.g. a menu item
1703 wxWindow
* parent
= this;
1704 while ( parent
&& !parent
->IsTopLevel() )
1705 parent
= parent
->GetParent();
1710 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1714 // Try for a menu command
1715 if (frame
->GetMenuBar())
1717 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1720 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1721 commandEvent
.SetEventObject(frame
);
1723 // If ProcessEvent returns TRUE (it was handled), then
1724 // the calling code will skip the event handling.
1725 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1731 // Find a child matching the command id
1732 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1738 // Now we process those kinds of windows that we can.
1739 // For now, only buttons.
1740 if ( wxDynamicCast(child
, wxButton
) )
1742 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1743 commandEvent
.SetEventObject(child
);
1744 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1752 // We didn't match the key event against an accelerator.
1756 // ============================================================================
1757 // Motif-specific stuff from here on
1758 // ============================================================================
1760 // ----------------------------------------------------------------------------
1761 // function which maintain the global hash table mapping Widgets to wxWindows
1762 // ----------------------------------------------------------------------------
1764 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1766 wxWindow
*oldItem
= NULL
;
1767 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1769 wxLogDebug("Widget table clash: new widget is %ld, %s",
1770 (long)w
, win
->GetClassInfo()->GetClassName());
1774 wxWidgetHashTable
->Put((long) w
, win
);
1776 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1777 (WXWidget
)w
, win
, win
->GetClassInfo()->GetClassName());
1782 wxWindow
*wxGetWindowFromTable(Widget w
)
1784 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1787 void wxDeleteWindowFromTable(Widget w
)
1789 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1791 wxWidgetHashTable
->Delete((long)w
);
1794 // ----------------------------------------------------------------------------
1795 // add/remove window from the table
1796 // ----------------------------------------------------------------------------
1798 // Add to hash table, add event handler
1799 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1800 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1802 wxAddWindowToTable((Widget
) mainWidget
, this);
1803 XtAddEventHandler( (Widget
) mainWidget
,
1804 ButtonPressMask
| ButtonReleaseMask
1805 | PointerMotionMask
,
1807 wxPanelItemEventHandler
,
1813 XtOverrideTranslations ((Widget
) mainWidget
,
1814 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1815 XtFree ((char *) ptr
);
1818 // Some widgets have a parent form widget, e.g. wxRadioBox
1821 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1825 XtOverrideTranslations ((Widget
) formWidget
,
1826 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1827 XtFree ((char *) ptr
);
1834 SetSize (x
, y
, width
, height
);
1839 // Remove event handler, remove from hash table
1840 bool wxWindow::DetachWidget(WXWidget widget
)
1842 XtRemoveEventHandler( (Widget
) widget
,
1843 ButtonPressMask
| ButtonReleaseMask
1844 | PointerMotionMask
,
1846 wxPanelItemEventHandler
,
1849 wxDeleteWindowFromTable((Widget
) widget
);
1853 // ----------------------------------------------------------------------------
1854 // Motif-specific accessors
1855 // ----------------------------------------------------------------------------
1857 WXWindow
wxWindow::GetClientXWindow() const
1859 Widget wMain
= (Widget
)GetClientWidget();
1861 return (WXWindow
) XtWindow(wMain
);
1863 return (WXWindow
) 0;
1866 // Get the underlying X window
1867 WXWindow
wxWindow::GetXWindow() const
1869 Widget wMain
= (Widget
)GetMainWidget();
1871 return (WXWindow
) XtWindow(wMain
);
1873 return (WXWindow
) 0;
1876 // Get the underlying X display
1877 WXDisplay
*wxWindow::GetXDisplay() const
1879 Widget wMain
= (Widget
)GetMainWidget();
1881 return (WXDisplay
*) XtDisplay(wMain
);
1883 return (WXDisplay
*) NULL
;
1886 WXWidget
wxWindow::GetMainWidget() const
1889 return m_drawingArea
;
1891 return m_mainWidget
;
1894 WXWidget
wxWindow::GetClientWidget() const
1896 if (m_drawingArea
!= (WXWidget
) 0)
1897 return m_drawingArea
;
1899 return GetMainWidget();
1902 WXWidget
wxWindow::GetTopWidget() const
1904 return GetMainWidget();
1907 WXWidget
wxWindow::GetLabelWidget() const
1909 return GetMainWidget();
1912 // ----------------------------------------------------------------------------
1914 // ----------------------------------------------------------------------------
1916 // All widgets should have this as their resize proc.
1917 // OnSize sent to wxWindow via client data.
1918 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1919 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1921 wxWindow
*win
= wxGetWindowFromTable(w
);
1925 if (win
->PreResize())
1928 win
->GetSize(&width
, &height
);
1929 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1930 sizeEvent
.SetEventObject(win
);
1931 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1935 static void wxCanvasRepaintProc(Widget drawingArea
,
1936 XtPointer clientData
,
1937 XmDrawingAreaCallbackStruct
* cbs
)
1939 if (!wxGetWindowFromTable(drawingArea
))
1942 XEvent
* event
= cbs
->event
;
1943 wxWindow
* win
= (wxWindow
*) clientData
;
1945 switch (event
->type
)
1949 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1950 event
->xexpose
.width
, event
->xexpose
.height
);
1952 if (event
-> xexpose
.count
== 0)
1961 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1962 static void wxCanvasEnterLeave(Widget drawingArea
,
1963 XtPointer
WXUNUSED(clientData
),
1964 XCrossingEvent
* event
)
1966 XmDrawingAreaCallbackStruct cbs
;
1969 ((XCrossingEvent
&) ev
) = *event
;
1971 cbs
.reason
= XmCR_INPUT
;
1974 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1977 // Fix to make it work under Motif 1.0 (!)
1978 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1979 XButtonEvent
*WXUNUSED(event
))
1981 #if XmVersion <= 1000
1982 XmDrawingAreaCallbackStruct cbs
;
1985 ev
= *((XEvent
*) event
);
1986 cbs
.reason
= XmCR_INPUT
;
1989 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
1990 #endif // XmVersion <= 1000
1993 static void wxCanvasInputEvent(Widget drawingArea
,
1994 XtPointer
WXUNUSED(data
),
1995 XmDrawingAreaCallbackStruct
* cbs
)
1997 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
2003 if (cbs
->reason
!= XmCR_INPUT
)
2006 local_event
= *(cbs
->event
); // We must keep a copy!
2008 switch (local_event
.xany
.type
)
2016 wxMouseEvent wxevent
;
2017 if(wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, &local_event
))
2019 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
2025 wxKeyEvent
event (wxEVT_CHAR
);
2026 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2028 // Implement wxFrame::OnCharHook by checking ancestor.
2029 wxWindow
*parent
= canvas
;
2030 while (parent
&& !parent
->IsTopLevel())
2031 parent
= parent
->GetParent();
2035 event
.SetEventType(wxEVT_CHAR_HOOK
);
2036 if (parent
->GetEventHandler()->ProcessEvent(event
))
2040 // For simplicity, OnKeyDown is the same as OnChar
2041 // TODO: filter modifier key presses from OnChar
2042 event
.SetEventType(wxEVT_KEY_DOWN
);
2044 // Only process OnChar if OnKeyDown didn't swallow it
2045 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2047 event
.SetEventType(wxEVT_CHAR
);
2048 canvas
->GetEventHandler()->ProcessEvent (event
);
2055 wxKeyEvent
event (wxEVT_KEY_UP
);
2056 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2058 canvas
->GetEventHandler()->ProcessEvent (event
);
2064 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2066 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2067 event
.SetEventObject(canvas
);
2068 canvas
->GetEventHandler()->ProcessEvent(event
);
2074 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2076 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2077 event
.SetEventObject(canvas
);
2078 canvas
->GetEventHandler()->ProcessEvent(event
);
2087 static void wxPanelItemEventHandler(Widget wid
,
2088 XtPointer
WXUNUSED(client_data
),
2090 Boolean
*continueToDispatch
)
2092 // Widget can be a label or the actual widget.
2094 wxWindow
*window
= wxGetWindowFromTable(wid
);
2097 wxMouseEvent
wxevent(0);
2098 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2100 window
->GetEventHandler()->ProcessEvent(wxevent
);
2104 // TODO: probably the key to allowing default behaviour to happen. Say we
2105 // set a m_doDefault flag to FALSE at the start of this function. Then in
2106 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2107 // TRUE, indicating that default processing can happen. Thus, behaviour can
2108 // appear to be overridden just by adding an event handler and not calling
2109 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2110 // way of handling drawing area events, to simplify things.
2111 *continueToDispatch
= True
;
2114 static void wxScrollBarCallback(Widget scrollbar
,
2115 XtPointer clientData
,
2116 XmScrollBarCallbackStruct
*cbs
)
2118 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2119 wxOrientation orientation
= (wxOrientation
)(int)clientData
;
2121 wxEventType eventType
= wxEVT_NULL
;
2122 switch (cbs
->reason
)
2124 case XmCR_INCREMENT
:
2126 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2129 case XmCR_DECREMENT
:
2131 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2136 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2139 case XmCR_VALUE_CHANGED
:
2141 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2144 case XmCR_PAGE_INCREMENT
:
2146 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2149 case XmCR_PAGE_DECREMENT
:
2151 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2156 eventType
= wxEVT_SCROLLWIN_TOP
;
2159 case XmCR_TO_BOTTOM
:
2161 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2166 // Should never get here
2167 wxFAIL_MSG("Unknown scroll event.");
2172 wxScrollWinEvent
event(eventType
,
2175 event
.SetEventObject( win
);
2176 win
->GetEventHandler()->ProcessEvent(event
);
2179 // For repainting arbitrary windows
2180 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2185 wxWindow
* win
= wxGetWindowFromTable(w
);
2189 switch(event
-> type
)
2193 window
= (Window
) win
-> GetXWindow();
2194 display
= (Display
*) win
-> GetXDisplay();
2196 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2197 event
->xexpose
.width
, event
->xexpose
.height
);
2199 if (event
-> xexpose
.count
== 0)
2209 // ----------------------------------------------------------------------------
2210 // TranslateXXXEvent() functions
2211 // ----------------------------------------------------------------------------
2213 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2214 Widget widget
, XEvent
*xevent
)
2216 switch (xevent
->xany
.type
)
2221 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2222 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2223 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2229 wxEventType eventType
= wxEVT_NULL
;
2231 if (xevent
->xany
.type
== LeaveNotify
)
2233 eventType
= wxEVT_LEAVE_WINDOW
;
2235 if (xevent
->xany
.type
== EnterNotify
)
2237 eventType
= wxEVT_ENTER_WINDOW
;
2239 else if (xevent
->xany
.type
== MotionNotify
)
2241 eventType
= wxEVT_MOTION
;
2243 else if (xevent
->xany
.type
== ButtonPress
)
2245 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2247 if (xevent
->xbutton
.button
== Button1
)
2249 eventType
= wxEVT_LEFT_DOWN
;
2252 else if (xevent
->xbutton
.button
== Button2
)
2254 eventType
= wxEVT_MIDDLE_DOWN
;
2257 else if (xevent
->xbutton
.button
== Button3
)
2259 eventType
= wxEVT_RIGHT_DOWN
;
2263 // check for a double click
2265 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2266 long ts
= wxevent
.GetTimestamp();
2268 int buttonLast
= win
->GetLastClickedButton();
2269 long lastTS
= win
->GetLastClickTime();
2270 if ( buttonLast
&& buttonLast
== button
&&
2271 (ts
- lastTS
) < dclickTime
)
2274 win
->SetLastClick(0, ts
);
2275 if ( eventType
== wxEVT_LEFT_DOWN
)
2276 eventType
= wxEVT_LEFT_DCLICK
;
2277 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2278 eventType
= wxEVT_MIDDLE_DCLICK
;
2279 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2280 eventType
= wxEVT_RIGHT_DCLICK
;
2284 // not fast enough or different button
2285 win
->SetLastClick(button
, ts
);
2288 else if (xevent
->xany
.type
== ButtonRelease
)
2290 if (xevent
->xbutton
.button
== Button1
)
2292 eventType
= wxEVT_LEFT_UP
;
2294 else if (xevent
->xbutton
.button
== Button2
)
2296 eventType
= wxEVT_MIDDLE_UP
;
2298 else if (xevent
->xbutton
.button
== Button3
)
2300 eventType
= wxEVT_RIGHT_UP
;
2310 wxevent
.SetEventType(eventType
);
2313 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2316 win
->GetPosition(&x2
, &y2
);
2318 // The button x/y must be translated to wxWindows
2319 // window space - the widget might be a label or button,
2323 if (widget
!= (Widget
)win
->GetMainWidget())
2329 wxevent
.m_x
= xevent
->xbutton
.x
+ dx
;
2330 wxevent
.m_y
= xevent
->xbutton
.y
+ dy
;
2332 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2333 || (event_left_is_down (xevent
)
2334 && (eventType
!= wxEVT_LEFT_UP
)));
2335 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2336 || (event_middle_is_down (xevent
)
2337 && (eventType
!= wxEVT_MIDDLE_UP
)));
2338 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2339 || (event_right_is_down (xevent
)
2340 && (eventType
!= wxEVT_RIGHT_UP
)));
2342 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2343 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2344 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2345 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2347 wxevent
.SetId(win
->GetId());
2348 wxevent
.SetEventObject(win
);
2356 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2357 Widget
WXUNUSED(widget
), XEvent
*xevent
)
2359 switch (xevent
->xany
.type
)
2367 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2368 int id
= wxCharCodeXToWX (keySym
);
2369 // id may be WXK_xxx code - these are outside ASCII range, so we
2370 // can't just use toupper() on id
2371 if (id
>= 'a' && id
<= 'z')
2374 if (xevent
->xkey
.state
& ShiftMask
)
2375 wxevent
.m_shiftDown
= TRUE
;
2376 if (xevent
->xkey
.state
& ControlMask
)
2377 wxevent
.m_controlDown
= TRUE
;
2378 if (xevent
->xkey
.state
& Mod3Mask
)
2379 wxevent
.m_altDown
= TRUE
;
2380 if (xevent
->xkey
.state
& Mod1Mask
)
2381 wxevent
.m_metaDown
= TRUE
;
2382 wxevent
.SetEventObject(win
);
2383 wxevent
.m_keyCode
= id
;
2384 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2386 wxevent
.m_x
= xevent
->xbutton
.x
;
2387 wxevent
.m_y
= xevent
->xbutton
.y
;
2401 // ----------------------------------------------------------------------------
2403 // ----------------------------------------------------------------------------
2405 #define YAllocColor XAllocColor
2406 XColor g_itemColors
[5];
2407 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2410 static XmColorProc colorProc
;
2412 result
= wxNO_COLORS
;
2416 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2417 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2418 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2419 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2420 if (colorProc
== (XmColorProc
) NULL
)
2422 // Get a ptr to the actual function
2423 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2424 // And set it back to motif.
2425 XmSetColorCalculation (colorProc
);
2427 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2428 &g_itemColors
[wxFORE_INDEX
],
2429 &g_itemColors
[wxSELE_INDEX
],
2430 &g_itemColors
[wxTOPS_INDEX
],
2431 &g_itemColors
[wxBOTS_INDEX
]);
2432 result
= wxBACK_COLORS
;
2436 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2437 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2438 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2439 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2440 if (result
== wxNO_COLORS
)
2441 result
= wxFORE_COLORS
;
2444 Display
*dpy
= display
;
2445 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2449 /* 5 Colours to allocate */
2450 for (int i
= 0; i
< 5; i
++)
2451 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2452 result
= wxNO_COLORS
;
2456 /* Only 1 colour to allocate */
2457 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2458 result
= wxNO_COLORS
;
2464 // Changes the foreground and background colours to be derived from the current
2465 // background colour. To change the foreground colour, you must call
2466 // SetForegroundColour explicitly.
2467 void wxWindow::ChangeBackgroundColour()
2469 WXWidget mainWidget
= GetMainWidget();
2471 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2474 void wxWindow::ChangeForegroundColour()
2476 WXWidget mainWidget
= GetMainWidget();
2478 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2479 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2480 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2483 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2485 if ( !wxWindowBase::SetBackgroundColour(col
) )
2488 ChangeBackgroundColour();
2493 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2495 if ( !wxWindowBase::SetForegroundColour(col
) )
2498 ChangeForegroundColour();
2503 void wxWindow::ChangeFont(bool keepOriginalSize
)
2505 // Note that this causes the widget to be resized back
2506 // to its original size! We therefore have to set the size
2507 // back again. TODO: a better way in Motif?
2508 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2509 if (w
&& m_font
.Ok())
2511 int width
, height
, width1
, height1
;
2512 GetSize(& width
, & height
);
2514 wxDoChangeFont( GetLabelWidget(), m_font
);
2516 GetSize(& width1
, & height1
);
2517 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2519 SetSize(-1, -1, width
, height
);
2524 // ----------------------------------------------------------------------------
2526 // ----------------------------------------------------------------------------
2528 wxWindow
*wxGetActiveWindow()
2531 wxFAIL_MSG("Not implemented");
2536 wxWindow
*wxWindowBase::GetCapture()
2538 return (wxWindow
*)g_captureWindow
;
2542 // Find the wxWindow at the current mouse position, returning the mouse
2544 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2546 return wxFindWindowAtPoint(wxGetMousePosition());
2549 // Get the current mouse position.
2550 wxPoint
wxGetMousePosition()
2552 Display
*display
= wxGlobalDisplay();
2553 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2554 Window rootReturn
, childReturn
;
2555 int rootX
, rootY
, winX
, winY
;
2556 unsigned int maskReturn
;
2558 XQueryPointer (display
,
2562 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2563 return wxPoint(rootX
, rootY
);
2567 // ----------------------------------------------------------------------------
2568 // wxNoOptimize: switch off size optimization
2569 // ----------------------------------------------------------------------------
2571 int wxNoOptimize::ms_count
= 0;