1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/window.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #define XtDisplay XTDISPLAY
25 #define XtWindow XTWINDOW
26 #define XtScreen XTSCREEN
36 #include "wx/dcclient.h"
37 #include "wx/button.h"
39 #include "wx/settings.h"
40 #include "wx/scrolwin.h"
41 #include "wx/layout.h"
42 #include "wx/menuitem.h"
43 #include "wx/module.h"
46 #include "wx/evtloop.h"
48 #if wxUSE_DRAG_AND_DROP
52 // DoSetSizeIntr and DoMoveWindowIntr
54 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
55 // did not work and/or segfaulted because
56 // 1) wxWindow::Create calls SetSize,
57 // which results in a call to DoSetSize much earlier than in the other ports
58 // 2) if wxWindow::Create is called (wxControl::Create calls it)
59 // then DoSetSize is never called, causing layout problems in composite
63 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
64 // DoSetPosition directly or indirectly from wxWindow::Create
65 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
68 #pragma message disable nosimpint
72 #include <Xm/DrawingA.h>
73 #include <Xm/ScrolledW.h>
74 #include <Xm/ScrollBar.h>
77 #include <Xm/RowColumn.h> // for XmMenuPosition
79 #pragma message enable nosimpint
82 #include "wx/motif/private.h"
86 // ----------------------------------------------------------------------------
87 // global variables for this module
88 // ----------------------------------------------------------------------------
90 extern wxHashTable
*wxWidgetHashTable
;
91 static wxWindow
* g_captureWindow
= NULL
;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 static void wxCanvasRepaintProc(Widget
, XtPointer
, XmDrawingAreaCallbackStruct
* cbs
);
99 static void wxCanvasInputEvent(Widget drawingArea
, XtPointer data
, XmDrawingAreaCallbackStruct
* cbs
);
100 static void wxCanvasMotionEvent(Widget
, XButtonEvent
* event
);
101 static void wxCanvasEnterLeave(Widget drawingArea
, XtPointer clientData
, XCrossingEvent
* event
);
102 static void wxScrollBarCallback(Widget widget
, XtPointer clientData
,
103 XmScrollBarCallbackStruct
*cbs
);
104 static void wxPanelItemEventHandler(Widget wid
,
105 XtPointer client_data
,
107 Boolean
*continueToDispatch
);
112 // Helper function for 16-bit fonts
113 static int str16len(const char *s
)
117 while (s
[0] && s
[1]) {
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
132 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
133 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxWindow
, wxWindowBase
)
141 BEGIN_EVENT_TABLE(wxWindow
, wxWindowBase
)
142 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged
)
145 // ============================================================================
147 // ============================================================================
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
155 Widget w
= (Widget
)widget
;
163 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
165 Widget w
= (Widget
)widget
;
169 // Rationale: a lot of common operations (including but not
170 // limited to moving, resizing and appending items to a listbox)
171 // unmamange the widget, do their work, then manage it again.
172 // This means that, for example adding an item to a listbox will show it,
173 // or that most controls are shown every time they are moved or resized!
174 XtSetMappedWhenManaged( w
, domap
);
176 // if the widget is not unmanaged, it still intercepts
177 // mouse events, even if it is not mapped (and hence invisible)
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 void wxWindow::Init()
199 m_needsRefresh
= true;
200 m_mainWidget
= (WXWidget
) 0;
202 m_winCaptured
= false;
210 m_drawingArea
= (WXWidget
) 0;
215 m_backingPixmap
= (WXPixmap
) 0;
226 // real construction (Init() must have been called before!)
227 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
231 const wxString
& name
)
233 wxCHECK_MSG( parent
, false, "can't create wxWindow without parent" );
235 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
237 parent
->AddChild(this);
239 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
240 m_foregroundColour
= *wxBLACK
;
242 //// TODO: we should probably optimize by only creating a
243 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
244 //// But for now, let's simplify things by always creating the
245 //// drawing area, since otherwise the translations are different.
247 // New translations for getting mouse motion feedback
248 static const String translations
= wxMOTIF_STR(
249 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
250 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
251 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
252 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
253 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
254 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
255 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
256 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
257 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
258 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
259 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
260 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
261 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
262 <Key>: DrawingAreaInput()");
264 XtActionsRec actions
[1];
265 actions
[0].string
= wxMOTIF_STR("wxCanvasMotionEvent");
266 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
267 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
269 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
270 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
272 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
275 xmScrolledWindowWidgetClass
,
276 m_borderWidget
? (Widget
) m_borderWidget
278 XmNresizePolicy
, XmRESIZE_NONE
,
280 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
281 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
285 XtTranslations ptr
= XtParseTranslationTable(translations
);
286 m_drawingArea
= (WXWidget
)XtVaCreateWidget
289 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
290 XmNunitType
, XmPIXELS
,
291 // XmNresizePolicy, XmRESIZE_ANY,
292 XmNresizePolicy
, XmRESIZE_NONE
,
295 XmNtranslations
, ptr
,
298 XtFree((char *) ptr
);
301 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
303 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
304 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
305 XtFree ((char *) ptr
);
309 wxAddWindowToTable((Widget
) m_drawingArea
, this);
310 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
312 // This order is very important in Motif 1.2.1
313 XtRealizeWidget ((Widget
) m_scrolledWindow
);
314 XtRealizeWidget ((Widget
) m_drawingArea
);
315 XtManageChild ((Widget
) m_drawingArea
);
317 ptr
= XtParseTranslationTable("<Configure>: resize()");
318 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
319 XtFree ((char *) ptr
);
321 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
322 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
325 (Widget
)m_drawingArea
,
326 PointerMotionHintMask
| EnterWindowMask
|
327 LeaveWindowMask
| FocusChangeMask
,
329 (XtEventHandler
) wxCanvasEnterLeave
,
333 // Scrolled widget needs to have its colour changed or we get a little blue
334 // square where the scrollbars abutt
335 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
336 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, true);
337 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, true);
339 XmScrolledWindowSetAreas(
340 (Widget
)m_scrolledWindow
,
341 (Widget
) 0, (Widget
) 0,
342 (Widget
) m_drawingArea
);
344 // Without this, the cursor may not be restored properly (e.g. in splitter
346 SetCursor(*wxSTANDARD_CURSOR
);
347 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
348 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, true);
353 wxWindow::~wxWindow()
355 if (g_captureWindow
== this)
356 g_captureWindow
= NULL
;
358 m_isBeingDeleted
= true;
360 // Motif-specific actions first
361 WXWidget wMain
= GetMainWidget();
364 // Removes event handlers
368 // If m_drawingArea, we're a fully-fledged window with drawing area,
369 // scrollbars etc. (what wxCanvas used to be)
372 // Destroy children before destroying self
376 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
378 Widget w
= (Widget
) m_drawingArea
;
379 wxDeleteWindowFromTable(w
);
384 m_drawingArea
= (WXWidget
) 0;
387 // Only if we're _really_ a canvas (not a dialog box/panel)
388 if (m_scrolledWindow
)
390 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
395 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
396 XtUnmanageChild((Widget
) m_hScrollBar
);
400 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
401 XtUnmanageChild((Widget
) m_vScrollBar
);
405 XtDestroyWidget((Widget
) m_hScrollBar
);
407 XtDestroyWidget((Widget
) m_vScrollBar
);
409 UnmanageAndDestroy(m_scrolledWindow
);
413 XtDestroyWidget ((Widget
) m_borderWidget
);
414 m_borderWidget
= (WXWidget
) 0;
417 else // Why wasn't this here before? JACS 8/3/2000
421 // Destroy the window
424 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
425 // Child widgets will get destroyed automatically when a frame
426 // or dialog is destroyed, but before that you may get some memory
427 // leaks and potential layout problems if you delete and then add
430 // GRG, Feb/2000: commented this out when adding support for
431 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
432 // that this call crashed wxMotif under OS/2, so it seems
433 // that leaving it out is the right thing to do.
434 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
435 XtDestroyWidget((Widget
) GetMainWidget());
436 SetMainWidget((WXWidget
) NULL
);
440 // ----------------------------------------------------------------------------
441 // scrollbar management
442 // ----------------------------------------------------------------------------
444 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
445 wxOrientation orientation
,
448 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
450 XtVaCreateManagedWidget( "scrollBarWidget",
451 xmScrollBarWidgetClass
, (Widget
)parent
,
452 XmNorientation
, orient
,
457 XtPointer o
= (XtPointer
)orientation
;
458 XtCallbackProc cb
= (XtCallbackProc
)callback
;
460 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
461 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
462 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
463 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
464 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
465 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
466 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
467 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
473 void wxWindow::CreateScrollbar(wxOrientation orientation
)
475 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
477 XtVaSetValues( (Widget
) m_scrolledWindow
,
478 XmNresizePolicy
, XmRESIZE_NONE
,
481 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
482 // Add scrollbars if required
483 if (orientation
== wxHORIZONTAL
)
485 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
486 (void (*)())wxScrollBarCallback
);
488 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, true);
490 XtRealizeWidget( (Widget
)m_hScrollBar
);
492 XtVaSetValues((Widget
) m_scrolledWindow
,
493 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
496 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
498 else if (orientation
== wxVERTICAL
)
500 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
501 (void (*)())wxScrollBarCallback
);
503 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, true);
505 XtRealizeWidget((Widget
)m_vScrollBar
);
507 XtVaSetValues((Widget
) m_scrolledWindow
,
508 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
511 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
514 XtVaSetValues( (Widget
) m_scrolledWindow
,
515 XmNresizePolicy
, XmRESIZE_ANY
,
519 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
521 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
523 XtVaSetValues((Widget
) m_scrolledWindow
,
524 XmNresizePolicy
, XmRESIZE_NONE
,
526 String stringSB
= orientation
== wxHORIZONTAL
?
527 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
528 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
529 &m_hScrollBar
: &m_vScrollBar
;
533 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
534 XtDestroyWidget( (Widget
)*widgetSB
);
535 *widgetSB
= (WXWidget
)NULL
;
538 XtVaSetValues( (Widget
)m_scrolledWindow
,
539 stringSB
, (Widget
) 0,
542 XtVaSetValues((Widget
) m_scrolledWindow
,
543 XmNresizePolicy
, XmRESIZE_ANY
,
547 // ---------------------------------------------------------------------------
549 // ---------------------------------------------------------------------------
551 void wxWindow::SetFocus()
553 Widget wMain
= (Widget
) GetMainWidget();
554 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
555 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
558 // Get the window with the focus
559 wxWindow
*wxWindowBase::DoFindFocus()
562 // (1) Can there be multiple focussed widgets in an application?
563 // In which case we need to find the top-level window that's
565 // (2) The widget with the focus may not be in the widget table
566 // depending on which widgets I put in the table
567 wxWindow
*winFocus
= (wxWindow
*)NULL
;
568 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
570 node
= node
->GetNext() )
572 wxWindow
*win
= node
->GetData();
574 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
576 if (w
!= (Widget
) NULL
)
578 winFocus
= wxGetWindowFromTable(w
);
587 bool wxWindow::Enable(bool enable
)
589 if ( !wxWindowBase::Enable(enable
) )
592 Widget wMain
= (Widget
)GetMainWidget();
595 XtSetSensitive(wMain
, enable
);
596 XmUpdateDisplay(wMain
);
602 bool wxWindow::Show(bool show
)
604 if ( !wxWindowBase::Show(show
) )
607 if (m_borderWidget
|| m_scrolledWindow
)
609 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
610 // MapOrUnmap(m_drawingArea, show);
614 if ( !MapOrUnmap(GetTopWidget(), show
) )
615 MapOrUnmap(GetMainWidget(), show
);
621 // Raise the window to the top of the Z order
622 void wxWindow::Raise()
624 Widget wTop
= (Widget
) GetTopWidget();
625 Window window
= XtWindow(wTop
);
626 XRaiseWindow(XtDisplay(wTop
), window
);
629 // Lower the window to the bottom of the Z order
630 void wxWindow::Lower()
632 Widget wTop
= (Widget
) GetTopWidget();
633 Window window
= XtWindow(wTop
);
634 XLowerWindow(XtDisplay(wTop
), window
);
637 void wxWindow::SetLabel(const wxString
& label
)
639 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, label
.mb_str(), NULL
);
642 wxString
wxWindow::GetLabel() const
645 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &label
, NULL
);
647 return wxString(label
);
650 void wxWindow::DoCaptureMouse()
652 g_captureWindow
= this;
656 Widget wMain
= (Widget
)GetMainWidget();
658 XtAddGrab(wMain
, True
, False
);
660 m_winCaptured
= true;
663 void wxWindow::DoReleaseMouse()
665 g_captureWindow
= NULL
;
666 if ( !m_winCaptured
)
669 Widget wMain
= (Widget
)GetMainWidget();
673 m_winCaptured
= false;
676 bool wxWindow::SetFont(const wxFont
& font
)
678 if ( !wxWindowBase::SetFont(font
) )
689 bool wxWindow::SetCursor(const wxCursor
& cursor
)
691 if ( !wxWindowBase::SetCursor(cursor
) )
697 // wxASSERT_MSG( m_cursor.Ok(),
698 // wxT("cursor must be valid after call to the base version"));
699 const wxCursor
* cursor2
= NULL
;
701 cursor2
= & m_cursor
;
703 cursor2
= wxSTANDARD_CURSOR
;
705 WXDisplay
*dpy
= GetXDisplay();
706 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
708 Widget w
= (Widget
) GetMainWidget();
709 Window win
= XtWindow(w
);
710 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
715 // Coordinates relative to the window
716 void wxWindow::WarpPointer (int x
, int y
)
718 Widget wClient
= (Widget
)GetClientWidget();
720 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
723 // ---------------------------------------------------------------------------
725 // ---------------------------------------------------------------------------
727 int wxWindow::GetScrollPos(int orient
) const
729 if (orient
== wxHORIZONTAL
)
735 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
739 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
747 // This now returns the whole range, not just the number of positions that we
749 int wxWindow::GetScrollRange(int orient
) const
751 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
752 // CE scintilla windows don't always have these scrollbars
753 // and it tends to pile up a whole bunch of asserts
754 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
758 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
762 int wxWindow::GetScrollThumb(int orient
) const
764 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
765 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
769 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
773 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
775 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
779 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
782 SetInternalScrollPos((wxOrientation
)orient
, pos
);
785 // New function that will replace some of the above.
786 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
787 int range
, bool WXUNUSED(refresh
))
790 GetSize(& oldW
, & oldH
);
794 if (thumbVisible
== 0)
797 if (thumbVisible
> range
)
798 thumbVisible
= range
;
800 // Save the old state to see if it changed
801 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
803 if (orient
== wxHORIZONTAL
)
805 if (thumbVisible
== range
)
808 DestroyScrollbar(wxHORIZONTAL
);
813 CreateScrollbar(wxHORIZONTAL
);
816 if (orient
== wxVERTICAL
)
818 if (thumbVisible
== range
)
821 DestroyScrollbar(wxVERTICAL
);
826 CreateScrollbar(wxVERTICAL
);
829 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
831 if (oldScrollBar
!= newScrollBar
)
833 // This is important! Without it, scrollbars misbehave badly.
834 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
835 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
836 XtRealizeWidget((Widget
) m_scrolledWindow
);
837 XtManageChild((Widget
) m_scrolledWindow
);
842 XtVaSetValues((Widget
) newScrollBar
,
846 XmNsliderSize
, thumbVisible
,
850 SetInternalScrollPos((wxOrientation
)orient
, pos
);
853 GetSize(& newW
, & newH
);
855 // Adjusting scrollbars can resize the canvas accidentally
856 if (newW
!= oldW
|| newH
!= oldH
)
857 SetSize(wxDefaultCoord
, wxDefaultCoord
, oldW
, oldH
);
860 // Does a physical scroll
861 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
866 // Use specified rectangle
867 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
871 // Use whole client area
873 GetClientSize(& w
, & h
);
876 int x1
= (dx
>= 0) ? x
: x
- dx
;
877 int y1
= (dy
>= 0) ? y
: y
- dy
;
878 int w1
= w
- abs(dx
);
879 int h1
= h
- abs(dy
);
880 int x2
= (dx
>= 0) ? x
+ dx
: x
;
881 int y2
= (dy
>= 0) ? y
+ dy
: y
;
885 dc
.SetLogicalFunction (wxCOPY
);
887 Widget widget
= (Widget
) GetMainWidget();
888 Window window
= XtWindow(widget
);
889 Display
* display
= XtDisplay(widget
);
891 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
892 x1
, y1
, w1
, h1
, x2
, y2
);
894 dc
.SetAutoSetting(true);
895 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
896 dc
.SetBrush(brush
); // FIXME: needed?
898 wxWindowList::compatibility_iterator cnode
= m_children
.GetFirst();
901 wxWindow
*child
= cnode
->GetData();
904 child
->GetSize( &sx
, &sy
);
905 wxPoint
pos( child
->GetPosition() );
906 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
907 cnode
= cnode
->GetNext();
910 // We'll add rectangles to the list of update rectangles according to which
911 // bits we've exposed.
916 wxRect
*rect
= new wxRect
;
922 XFillRectangle(display
, window
,
923 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
927 rect
->width
= rect
->width
;
928 rect
->height
= rect
->height
;
930 updateRects
.Append((wxObject
*) rect
);
934 wxRect
*rect
= new wxRect
;
936 rect
->x
= x
+ w
+ dx
;
941 XFillRectangle(display
, window
,
942 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
947 rect
->width
= rect
->width
;
948 rect
->height
= rect
->height
;
950 updateRects
.Append((wxObject
*) rect
);
954 wxRect
*rect
= new wxRect
;
961 XFillRectangle(display
, window
,
962 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
966 rect
->width
= rect
->width
;
967 rect
->height
= rect
->height
;
969 updateRects
.Append((wxObject
*) rect
);
973 wxRect
*rect
= new wxRect
;
976 rect
->y
= y
+ h
+ dy
;
980 XFillRectangle(display
, window
,
981 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
985 rect
->width
= rect
->width
;
986 rect
->height
= rect
->height
;
988 updateRects
.Append((wxObject
*) rect
);
990 dc
.SetBrush(wxNullBrush
);
992 // Now send expose events
994 wxList::compatibility_iterator node
= updateRects
.GetFirst();
997 wxRect
* rect
= (wxRect
*) node
->GetData();
1000 event
.type
= Expose
;
1001 event
.display
= display
;
1002 event
.send_event
= True
;
1003 event
.window
= window
;
1007 event
.width
= rect
->width
;
1008 event
.height
= rect
->height
;
1012 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1014 node
= node
->GetNext();
1018 // Delete the update rects
1019 node
= updateRects
.GetFirst();
1022 wxRect
* rect
= (wxRect
*) node
->GetData();
1024 node
= node
->GetNext();
1027 XmUpdateDisplay((Widget
) GetMainWidget());
1030 // ---------------------------------------------------------------------------
1032 // ---------------------------------------------------------------------------
1034 #if wxUSE_DRAG_AND_DROP
1036 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1043 // Old style file-manager drag&drop
1044 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1049 // ----------------------------------------------------------------------------
1051 // ----------------------------------------------------------------------------
1055 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1060 #endif // wxUSE_TOOLTIPS
1062 // ----------------------------------------------------------------------------
1064 // ----------------------------------------------------------------------------
1068 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
1070 if ( x
== wxDefaultCoord
&& y
== wxDefaultCoord
)
1072 wxPoint mouse
= ScreenToClient(wxGetMousePosition());
1073 x
= mouse
.x
; y
= mouse
.y
;
1076 Widget widget
= (Widget
) GetMainWidget();
1078 /* The menuId field seems to be usused, so we'll use it to
1079 indicate whether a menu is popped up or not:
1080 0: Not currently created as a popup
1081 -1: Created as a popup, but not active
1085 if (menu
->GetParent() && (menu
->GetId() != -1))
1088 if (menu
->GetMainWidget())
1090 menu
->DestroyMenu(true);
1093 menu
->SetId(1); /* Mark as popped-up */
1094 menu
->CreateMenu(NULL
, widget
, menu
, 0);
1095 menu
->SetInvokingWindow(this);
1099 // menu->SetParent(parent);
1100 // parent->children->Append(menu); // Store menu for later deletion
1102 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1110 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1112 wxCanvas *canvas = (wxCanvas *) this;
1113 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1114 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1118 Display
*display
= XtDisplay (widget
);
1119 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1120 Window thisWindow
= XtWindow (widget
);
1122 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1123 &rootX
, &rootY
, &childWindow
);
1125 XButtonPressedEvent event
;
1126 event
.type
= ButtonPress
;
1132 event
.x_root
= rootX
;
1133 event
.y_root
= rootY
;
1135 XmMenuPosition (menuWidget
, &event
);
1136 XtManageChild (menuWidget
);
1138 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1139 // idle-time destroy routine.
1140 // Waiting until this ID changes causes this function to block until
1141 // the menu has been dismissed and the widgets cleaned up.
1142 // In other words, once this routine returns, it is safe to delete
1144 // Ian Brown <ian.brown@printsoft.de>
1146 wxEventLoop evtLoop
;
1148 while (menu
->GetId() == 1)
1150 wxDoEventLoopIteration( evtLoop
);
1158 // ---------------------------------------------------------------------------
1159 // moving and resizing
1160 // ---------------------------------------------------------------------------
1162 bool wxWindow::PreResize()
1168 void wxWindow::DoGetSize(int *x
, int *y
) const
1170 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1171 ( m_borderWidget
? m_borderWidget
:
1172 m_scrolledWindow
? m_scrolledWindow
:
1176 XtVaGetValues( widget
,
1184 void wxWindow::DoGetPosition(int *x
, int *y
) const
1186 Widget widget
= (Widget
)
1188 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1192 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1194 // We may be faking the client origin. So a window that's really at (0, 30)
1195 // may appear (to wxWin apps) to be at (0, 0).
1198 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1199 xx
= (Position
)(xx
- pt
.x
);
1200 yy
= (Position
)(yy
- pt
.y
);
1207 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1209 Widget widget
= (Widget
) GetClientWidget();
1210 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1211 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1212 Window thisWindow
= XtWindow(widget
);
1215 int xx
= x
? *x
: 0;
1216 int yy
= y
? *y
: 0;
1217 XTranslateCoordinates(display
, rootWindow
, thisWindow
,
1218 xx
, yy
, x
? x
: &xx
, y
? y
: &yy
,
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
);
1230 int xx
= x
? *x
: 0;
1231 int yy
= y
? *y
: 0;
1232 XTranslateCoordinates(display
, thisWindow
, rootWindow
,
1233 xx
, yy
, x
? x
: &xx
, y
? y
: &yy
,
1238 // Get size *available for subwindows* i.e. excluding menu bar etc.
1239 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1241 Widget widget
= (Widget
) GetClientWidget();
1243 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1244 if(x
) *x
= xx
; if(y
) *y
= yy
;
1247 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1249 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, false);
1252 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1253 int sizeFlags
, bool fromCtor
)
1255 // A bit of optimization to help sort out the flickers.
1256 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1260 GetSize(& oldW
, & oldH
);
1261 GetPosition(& oldX
, & oldY
);
1264 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1272 wxSize
size(wxDefaultSize
);
1275 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1277 size
= DoGetBestSize();
1288 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1290 if( size
.x
== -1 ) size
= DoGetBestSize();
1299 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1300 || !wxNoOptimize::CanOptimize() )
1313 flags
|= wxMOVE_WIDTH
;
1316 flags
|= wxMOVE_HEIGHT
;
1318 int xx
= x
; int yy
= y
;
1319 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1321 DoMoveWindow( xx
, yy
, width
, height
);
1323 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1328 Widget widget
= (Widget
) GetTopWidget();
1332 bool managed
= XtIsManaged( widget
);
1334 XtUnmanageChild(widget
);
1338 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1340 DoMoveWindow(xx
, yy
, width
, height
);
1343 XtManageChild(widget
);
1347 void wxWindow::DoSetClientSize(int width
, int height
)
1351 Widget drawingArea
= (Widget
) m_drawingArea
;
1353 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1356 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1358 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1360 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1364 Widget widget
= (Widget
) GetTopWidget();
1367 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1369 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1372 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1377 Widget drawingArea
= (Widget
) m_drawingArea
;
1378 Widget borderOrScrolled
= m_borderWidget
?
1379 (Widget
) m_borderWidget
:
1380 (Widget
) m_scrolledWindow
;
1382 bool managed
= XtIsManaged(borderOrScrolled
);
1384 XtUnmanageChild (borderOrScrolled
);
1385 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1387 if (flags
& wxMOVE_X
)
1388 XtVaSetValues (borderOrScrolled
,
1391 if (flags
& wxMOVE_Y
)
1392 XtVaSetValues (borderOrScrolled
,
1396 if (flags
& wxMOVE_WIDTH
)
1400 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1401 short thick
, margin
;
1402 XtVaGetValues ((Widget
) m_borderWidget
,
1403 XmNshadowThickness
, &thick
,
1404 XmNmarginWidth
, &margin
,
1406 w
-= 2 * (thick
+ margin
);
1410 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1413 if (flags
& wxMOVE_HEIGHT
)
1417 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1418 short thick
, margin
;
1419 XtVaGetValues ((Widget
) m_borderWidget
,
1420 XmNshadowThickness
, &thick
,
1421 XmNmarginHeight
, &margin
,
1423 h
-= 2 * (thick
+ margin
);
1427 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1431 XtManageChild (borderOrScrolled
);
1432 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1439 XtVaSetValues((Widget
)GetTopWidget(),
1448 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1450 DoMoveWindowIntr (x
, y
, width
, height
,
1451 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1454 // ---------------------------------------------------------------------------
1456 // ---------------------------------------------------------------------------
1458 int wxWindow::GetCharHeight() const
1460 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1464 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1465 "x", NULL
, &height
, NULL
, NULL
);
1470 int wxWindow::GetCharWidth() const
1472 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1476 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1477 "x", &width
, NULL
, NULL
, NULL
);
1482 void wxWindow::GetTextExtent(const wxString
& string
,
1484 int *descent
, int *externalLeading
,
1485 const wxFont
*theFont
) const
1487 const wxFont
*fontToUse
= theFont
? theFont
: &m_font
;
1489 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1491 if (externalLeading
)
1492 *externalLeading
= 0;
1493 wxGetTextExtent (GetXDisplay(), *fontToUse
, 1.0,
1494 string
, x
, y
, NULL
, descent
);
1497 // ----------------------------------------------------------------------------
1499 // ----------------------------------------------------------------------------
1501 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1503 m_updateRegion
.Union( x
, y
, w
, h
);
1506 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1508 m_needsRefresh
= true;
1509 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1510 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1512 XExposeEvent dummyEvent
;
1514 GetSize(&width
, &height
);
1516 dummyEvent
.type
= Expose
;
1517 dummyEvent
.display
= display
;
1518 dummyEvent
.send_event
= True
;
1519 dummyEvent
.window
= thisWindow
;
1522 dummyEvent
.x
= rect
->x
;
1523 dummyEvent
.y
= rect
->y
;
1524 dummyEvent
.width
= rect
->width
;
1525 dummyEvent
.height
= rect
->height
;
1531 dummyEvent
.width
= width
;
1532 dummyEvent
.height
= height
;
1534 dummyEvent
.count
= 0;
1538 wxClientDC
dc(this);
1539 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1540 dc
.SetBackground(backgroundBrush
);
1547 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1550 void wxWindow::DoPaint()
1552 //TODO : make a temporary gc so we can do the XCopyArea below
1553 if (m_backingPixmap
&& !m_needsRefresh
)
1557 GC tempGC
= (GC
) dc
.GetBackingGC();
1559 Widget widget
= (Widget
) GetMainWidget();
1564 // We have to test whether it's a wxScrolledWindow (hack!) because
1565 // otherwise we don't know how many pixels have been scrolled. We might
1566 // solve this in the future by defining virtual wxWindow functions to get
1567 // the scroll position in pixels. Or, each kind of scrolled window has to
1568 // implement backing stores itself, using generic wxWidgets code.
1569 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1570 if ( scrolledWindow
)
1573 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1579 // TODO: This could be optimized further by only copying the areas in the
1580 // current update region.
1582 // Only blit the part visible in the client area. The backing pixmap
1583 // always starts at 0, 0 but we may be looking at only a portion of it.
1584 wxSize clientArea
= GetClientSize();
1585 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1586 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1588 // Copy whichever is samller, the amount of pixmap we have to copy,
1589 // or the size of the client area.
1590 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1591 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1593 // Make sure we're not negative
1594 toBlitX
= wxMax(0, toBlitX
);
1595 toBlitY
= wxMax(0, toBlitY
);
1600 (Pixmap
) m_backingPixmap
,
1603 scrollPosX
, scrollPosY
, // Start at the scroll position
1604 toBlitX
, toBlitY
, // How much of the pixmap to copy
1610 wxWindowDC
dc(this);
1611 // Set an erase event first
1612 wxEraseEvent
eraseEvent(GetId(), &dc
);
1613 eraseEvent
.SetEventObject(this);
1614 GetEventHandler()->ProcessEvent(eraseEvent
);
1616 wxPaintEvent
event(GetId());
1617 event
.SetEventObject(this);
1618 GetEventHandler()->ProcessEvent(event
);
1620 m_needsRefresh
= false;
1624 // ----------------------------------------------------------------------------
1626 // ----------------------------------------------------------------------------
1628 // Responds to colour changes: passes event on to children.
1629 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1631 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1634 // Only propagate to non-top-level windows
1635 wxWindow
*win
= node
->GetData();
1636 if ( win
->GetParent() )
1638 wxSysColourChangedEvent event2
;
1639 event
.SetEventObject(win
);
1640 win
->GetEventHandler()->ProcessEvent(event2
);
1643 node
= node
->GetNext();
1647 void wxWindow::OnInternalIdle()
1649 // This calls the UI-update mechanism (querying windows for
1650 // menu/toolbar/control state information)
1651 if (wxUpdateUIEvent::CanUpdate(this))
1652 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1655 // ----------------------------------------------------------------------------
1657 // ----------------------------------------------------------------------------
1659 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1662 if (!m_acceleratorTable
.Ok())
1665 int count
= m_acceleratorTable
.GetCount();
1666 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1668 for (i
= 0; i
< count
; i
++)
1670 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1671 if (entry
->MatchesEvent(event
))
1673 // Bingo, we have a match. Now find a control that matches the
1674 // entry command id.
1676 // Need to go up to the top of the window hierarchy, since it might
1677 // be e.g. a menu item
1678 wxWindow
* parent
= this;
1679 while ( parent
&& !parent
->IsTopLevel() )
1680 parent
= parent
->GetParent();
1685 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1689 // Try for a menu command
1690 if (frame
->GetMenuBar())
1692 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1695 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1696 commandEvent
.SetEventObject(frame
);
1698 // If ProcessEvent returns true (it was handled), then
1699 // the calling code will skip the event handling.
1700 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1706 // Find a child matching the command id
1707 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1713 // Now we process those kinds of windows that we can.
1714 // For now, only buttons.
1715 if ( wxDynamicCast(child
, wxButton
) )
1717 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1718 commandEvent
.SetEventObject(child
);
1719 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1727 // We didn't match the key event against an accelerator.
1731 // ============================================================================
1732 // Motif-specific stuff from here on
1733 // ============================================================================
1735 // ----------------------------------------------------------------------------
1736 // function which maintain the global hash table mapping Widgets to wxWidgets
1737 // ----------------------------------------------------------------------------
1739 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1741 const long key
= (long)w
;
1742 if ( wxWidgetHashTable
->Get(key
))
1744 wxLogDebug("Widget table clash: new widget is %ld, %s",
1745 key
, win
->GetClassInfo()->GetClassName());
1749 wxWidgetHashTable
->Put(key
, win
);
1751 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1752 w
, win
, win
->GetClassInfo()->GetClassName());
1757 wxWindow
*wxGetWindowFromTable(Widget w
)
1759 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1762 void wxDeleteWindowFromTable(Widget w
)
1764 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1766 wxWidgetHashTable
->Delete((long)w
);
1769 // ----------------------------------------------------------------------------
1770 // add/remove window from the table
1771 // ----------------------------------------------------------------------------
1773 // Add to hash table, add event handler
1774 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1775 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1777 wxAddWindowToTable((Widget
) mainWidget
, this);
1778 XtAddEventHandler( (Widget
) mainWidget
,
1779 ButtonPressMask
| ButtonReleaseMask
1780 | PointerMotionMask
,
1782 wxPanelItemEventHandler
,
1788 XtOverrideTranslations ((Widget
) mainWidget
,
1789 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1790 XtFree ((char *) ptr
);
1793 // Some widgets have a parent form widget, e.g. wxRadioBox
1796 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1800 XtOverrideTranslations ((Widget
) formWidget
,
1801 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1802 XtFree ((char *) ptr
);
1809 DoSetSize (x
, y
, width
, height
, wxSIZE_USE_EXISTING
);
1814 // Remove event handler, remove from hash table
1815 bool wxWindow::DetachWidget(WXWidget widget
)
1817 XtRemoveEventHandler( (Widget
) widget
,
1818 ButtonPressMask
| ButtonReleaseMask
1819 | PointerMotionMask
,
1821 wxPanelItemEventHandler
,
1824 wxDeleteWindowFromTable((Widget
) widget
);
1828 // ----------------------------------------------------------------------------
1829 // Motif-specific accessors
1830 // ----------------------------------------------------------------------------
1832 WXWindow
wxWindow::GetClientXWindow() const
1834 Widget wMain
= (Widget
)GetClientWidget();
1836 return (WXWindow
) XtWindow(wMain
);
1838 return (WXWindow
) 0;
1841 // Get the underlying X window
1842 WXWindow
wxWindow::GetXWindow() const
1844 Widget wMain
= (Widget
)GetMainWidget();
1846 return (WXWindow
) XtWindow(wMain
);
1848 return (WXWindow
) 0;
1851 // Get the underlying X display
1852 WXDisplay
*wxWindow::GetXDisplay() const
1854 Widget wMain
= (Widget
)GetMainWidget();
1856 return (WXDisplay
*) XtDisplay(wMain
);
1858 return (WXDisplay
*) NULL
;
1861 WXWidget
wxWindow::GetMainWidget() const
1864 return m_drawingArea
;
1866 return m_mainWidget
;
1869 WXWidget
wxWindow::GetClientWidget() const
1871 if (m_drawingArea
!= (WXWidget
) 0)
1872 return m_drawingArea
;
1874 return GetMainWidget();
1877 WXWidget
wxWindow::GetTopWidget() const
1879 return GetMainWidget();
1882 WXWidget
wxWindow::GetLabelWidget() const
1884 return GetMainWidget();
1887 // ----------------------------------------------------------------------------
1889 // ----------------------------------------------------------------------------
1891 // All widgets should have this as their resize proc.
1892 // OnSize sent to wxWindow via client data.
1893 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1894 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1896 wxWindow
*win
= wxGetWindowFromTable(w
);
1900 if (win
->PreResize())
1902 wxSize
newSize(win
->GetSize());
1903 wxSizeEvent
sizeEvent(newSize
, 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 wxCHECK_RET( win
, _T("invalid widget in scrollbar callback") );
2093 wxOrientation orientation
= (wxOrientation
)wxPtrToUInt(clientData
);
2095 wxEventType eventType
= wxEVT_NULL
;
2096 switch (cbs
->reason
)
2098 case XmCR_INCREMENT
:
2100 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2103 case XmCR_DECREMENT
:
2105 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2110 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2113 case XmCR_VALUE_CHANGED
:
2115 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2118 case XmCR_PAGE_INCREMENT
:
2120 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2123 case XmCR_PAGE_DECREMENT
:
2125 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2130 eventType
= wxEVT_SCROLLWIN_TOP
;
2133 case XmCR_TO_BOTTOM
:
2135 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2140 // Should never get here
2141 wxFAIL_MSG("Unknown scroll event.");
2146 wxScrollWinEvent
event(eventType
,
2149 event
.SetEventObject( win
);
2150 win
->GetEventHandler()->ProcessEvent(event
);
2153 // For repainting arbitrary windows
2154 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2156 wxWindow
* win
= wxGetWindowFromTable(w
);
2160 switch ( event
->type
)
2164 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2165 event
->xexpose
.width
, event
->xexpose
.height
);
2167 if ( event
->xexpose
.count
== 0 )
2177 // ----------------------------------------------------------------------------
2178 // TranslateXXXEvent() functions
2179 // ----------------------------------------------------------------------------
2181 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2182 Widget widget
, const XEvent
*xevent
)
2184 switch (xevent
->xany
.type
)
2189 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2190 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2191 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2197 int eventx
= xevent
->xbutton
.x
, eventy
= xevent
->xbutton
.y
;
2199 wxEventType eventType
= wxEVT_NULL
;
2201 if (xevent
->xany
.type
== LeaveNotify
)
2203 eventType
= wxEVT_LEAVE_WINDOW
;
2205 if (xevent
->xany
.type
== EnterNotify
)
2207 eventType
= wxEVT_ENTER_WINDOW
;
2209 else if (xevent
->xany
.type
== MotionNotify
)
2211 eventType
= wxEVT_MOTION
;
2213 if (xevent
->xmotion
.is_hint
== NotifyHint
)
2218 Display
*dpy
= XtDisplay (widget
);
2220 XQueryPointer (dpy
, XtWindow (widget
),
2222 &x_root
, &y_root
, &eventx
, &eventy
, &state
);
2225 else if (xevent
->xany
.type
== ButtonPress
)
2227 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2229 if (xevent
->xbutton
.button
== Button1
)
2231 eventType
= wxEVT_LEFT_DOWN
;
2234 else if (xevent
->xbutton
.button
== Button2
)
2236 eventType
= wxEVT_MIDDLE_DOWN
;
2239 else if (xevent
->xbutton
.button
== Button3
)
2241 eventType
= wxEVT_RIGHT_DOWN
;
2245 // check for a double click
2247 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2248 long ts
= wxevent
.GetTimestamp();
2250 int buttonLast
= win
->GetLastClickedButton();
2251 long lastTS
= win
->GetLastClickTime();
2252 if ( buttonLast
&& buttonLast
== button
&&
2253 (ts
- lastTS
) < dclickTime
)
2256 win
->SetLastClick(0, ts
);
2257 if ( eventType
== wxEVT_LEFT_DOWN
)
2258 eventType
= wxEVT_LEFT_DCLICK
;
2259 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2260 eventType
= wxEVT_MIDDLE_DCLICK
;
2261 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2262 eventType
= wxEVT_RIGHT_DCLICK
;
2266 // not fast enough or different button
2267 win
->SetLastClick(button
, ts
);
2270 else if (xevent
->xany
.type
== ButtonRelease
)
2272 if (xevent
->xbutton
.button
== Button1
)
2274 eventType
= wxEVT_LEFT_UP
;
2276 else if (xevent
->xbutton
.button
== Button2
)
2278 eventType
= wxEVT_MIDDLE_UP
;
2280 else if (xevent
->xbutton
.button
== Button3
)
2282 eventType
= wxEVT_RIGHT_UP
;
2292 wxevent
.SetEventType(eventType
);
2295 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2298 win
->GetPosition(&x2
, &y2
);
2300 // The button x/y must be translated to wxWidgets
2301 // window space - the widget might be a label or button,
2305 if (widget
!= (Widget
)win
->GetMainWidget())
2311 wxevent
.m_x
= eventx
+ dx
;
2312 wxevent
.m_y
= eventy
+ dy
;
2314 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2315 || (event_left_is_down (xevent
)
2316 && (eventType
!= wxEVT_LEFT_UP
)));
2317 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2318 || (event_middle_is_down (xevent
)
2319 && (eventType
!= wxEVT_MIDDLE_UP
)));
2320 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2321 || (event_right_is_down (xevent
)
2322 && (eventType
!= wxEVT_RIGHT_UP
)));
2324 wxevent
.m_shiftDown
= (xevent
->xbutton
.state
& ShiftMask
) == ShiftMask
;
2325 wxevent
.m_controlDown
= (xevent
->xbutton
.state
& ControlMask
) == ControlMask
;
2326 wxevent
.m_altDown
= (xevent
->xbutton
.state
& Mod3Mask
) == Mod3Mask
;
2327 wxevent
.m_metaDown
= (xevent
->xbutton
.state
& Mod1Mask
) == Mod1Mask
;
2329 wxevent
.SetId(win
->GetId());
2330 wxevent
.SetEventObject(win
);
2338 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2339 Widget
WXUNUSED(widget
), const XEvent
*xevent
)
2341 switch (xevent
->xany
.type
)
2349 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2350 int id
= wxCharCodeXToWX (keySym
);
2351 // id may be WXK_xxx code - these are outside ASCII range, so we
2352 // can't just use toupper() on id
2353 if (id
>= 'a' && id
<= 'z')
2356 if (xevent
->xkey
.state
& ShiftMask
)
2357 wxevent
.m_shiftDown
= true;
2358 if (xevent
->xkey
.state
& ControlMask
)
2359 wxevent
.m_controlDown
= true;
2360 if (xevent
->xkey
.state
& Mod3Mask
)
2361 wxevent
.m_altDown
= true;
2362 if (xevent
->xkey
.state
& Mod1Mask
)
2363 wxevent
.m_metaDown
= true;
2364 wxevent
.SetEventObject(win
);
2365 wxevent
.m_keyCode
= id
;
2366 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2368 wxevent
.m_x
= xevent
->xbutton
.x
;
2369 wxevent
.m_y
= xevent
->xbutton
.y
;
2382 // ----------------------------------------------------------------------------
2384 // ----------------------------------------------------------------------------
2386 #define YAllocColor XAllocColor
2387 XColor g_itemColors
[5];
2388 int wxComputeColours (Display
*display
, const wxColour
* back
, const wxColour
* fore
)
2391 static XmColorProc colorProc
;
2393 result
= wxNO_COLORS
;
2397 g_itemColors
[0].red
= (unsigned short)(((long) back
->Red ()) << 8);
2398 g_itemColors
[0].green
= (unsigned short)(((long) back
->Green ()) << 8);
2399 g_itemColors
[0].blue
= (unsigned short)(((long) back
->Blue ()) << 8);
2400 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2401 if (colorProc
== (XmColorProc
) NULL
)
2403 // Get a ptr to the actual function
2404 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2405 // And set it back to motif.
2406 XmSetColorCalculation (colorProc
);
2408 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2409 &g_itemColors
[wxFORE_INDEX
],
2410 &g_itemColors
[wxSELE_INDEX
],
2411 &g_itemColors
[wxTOPS_INDEX
],
2412 &g_itemColors
[wxBOTS_INDEX
]);
2413 result
= wxBACK_COLORS
;
2417 g_itemColors
[wxFORE_INDEX
].red
= (unsigned short)(((long) fore
->Red ()) << 8);
2418 g_itemColors
[wxFORE_INDEX
].green
= (unsigned short)(((long) fore
->Green ()) << 8);
2419 g_itemColors
[wxFORE_INDEX
].blue
= (unsigned short)(((long) fore
->Blue ()) << 8);
2420 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2421 if (result
== wxNO_COLORS
)
2422 result
= wxFORE_COLORS
;
2425 Display
*dpy
= display
;
2426 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2430 /* 5 Colours to allocate */
2431 for (int i
= 0; i
< 5; i
++)
2432 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2433 result
= wxNO_COLORS
;
2437 /* Only 1 colour to allocate */
2438 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2439 result
= wxNO_COLORS
;
2445 // Changes the foreground and background colours to be derived from the current
2446 // background colour. To change the foreground colour, you must call
2447 // SetForegroundColour explicitly.
2448 void wxWindow::ChangeBackgroundColour()
2450 WXWidget mainWidget
= GetMainWidget();
2452 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2453 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2454 wxDoChangeForegroundColour(m_scrolledWindow
, m_backgroundColour
);
2457 void wxWindow::ChangeForegroundColour()
2459 WXWidget mainWidget
= GetMainWidget();
2461 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2462 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2463 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2466 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2468 if ( !wxWindowBase::SetBackgroundColour(col
) )
2471 ChangeBackgroundColour();
2476 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2478 if ( !wxWindowBase::SetForegroundColour(col
) )
2481 ChangeForegroundColour();
2486 void wxWindow::ChangeFont(bool keepOriginalSize
)
2488 // Note that this causes the widget to be resized back
2489 // to its original size! We therefore have to set the size
2490 // back again. TODO: a better way in Motif?
2491 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2492 if (w
&& m_font
.Ok())
2494 int width
, height
, width1
, height1
;
2495 GetSize(& width
, & height
);
2497 wxDoChangeFont( GetLabelWidget(), m_font
);
2499 GetSize(& width1
, & height1
);
2500 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2502 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
2507 // ----------------------------------------------------------------------------
2509 // ----------------------------------------------------------------------------
2511 wxWindow
*wxGetActiveWindow()
2514 wxFAIL_MSG("Not implemented");
2519 wxWindow
*wxWindowBase::GetCapture()
2521 return (wxWindow
*)g_captureWindow
;
2525 // Find the wxWindow at the current mouse position, returning the mouse
2527 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2529 pt
= wxGetMousePosition();
2530 return wxFindWindowAtPoint(pt
);
2533 void wxGetMouseState(int& rootX
, int& rootY
, unsigned& maskReturn
)
2535 Display
*display
= wxGlobalDisplay();
2536 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2537 Window rootReturn
, childReturn
;
2540 XQueryPointer (display
,
2544 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2547 // Get the current mouse position.
2548 wxPoint
wxGetMousePosition()
2553 wxGetMouseState(x
, y
, mask
);
2554 return wxPoint(x
, y
);
2557 wxMouseState
wxGetMouseState()
2563 wxGetMouseState(x
, y
, mask
);
2568 ms
.SetLeftDown(mask
& Button1Mask
);
2569 ms
.SetMiddleDown(mask
& Button2Mask
);
2570 ms
.SetRightDown(mask
& Button3Mask
);
2572 ms
.SetControlDown(mask
& ControlMask
);
2573 ms
.SetShiftDown(mask
& ShiftMask
);
2574 ms
.SetAltDown(mask
& Mod3Mask
);
2575 ms
.SetMetaDown(mask
& Mod1Mask
);
2581 // ----------------------------------------------------------------------------
2582 // wxNoOptimize: switch off size optimization
2583 // ----------------------------------------------------------------------------
2585 int wxNoOptimize::ms_count
= 0;