1 /////////////////////////////////////////////////////////////////////////////
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
32 #include "wx/dcclient.h"
35 #include "wx/layout.h"
36 #include "wx/button.h"
37 #include "wx/settings.h"
39 #include "wx/scrolwin.h"
40 #include "wx/module.h"
41 #include "wx/menuitem.h"
43 #include "wx/evtloop.h"
46 #if wxUSE_DRAG_AND_DROP
50 // DoSetSizeIntr and DoMoveWindowIntr
52 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
53 // did not work and/or segfaulted because
54 // 1) wxWindow::Create calls SetSize,
55 // which results in a call to DoSetSize much earlier than in the other ports
56 // 2) if wxWindow::Create is called (wxControl::Create calls it)
57 // then DoSetSize is never called, causing layout problems in composite
61 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
62 // DoSetPosition directly or indirectly from wxWindow::Create
63 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
66 #pragma message disable nosimpint
70 #include <Xm/DrawingA.h>
71 #include <Xm/ScrolledW.h>
72 #include <Xm/ScrollBar.h>
75 #include <Xm/RowColumn.h> // for XmMenuPosition
77 #pragma message enable nosimpint
80 #include "wx/motif/private.h"
84 // ----------------------------------------------------------------------------
85 // global variables for this module
86 // ----------------------------------------------------------------------------
88 extern wxHashTable
*wxWidgetHashTable
;
89 static wxWindow
* g_captureWindow
= NULL
;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 static void wxCanvasRepaintProc(Widget
, XtPointer
, XmDrawingAreaCallbackStruct
* cbs
);
97 static void wxCanvasInputEvent(Widget drawingArea
, XtPointer data
, XmDrawingAreaCallbackStruct
* cbs
);
98 static void wxCanvasMotionEvent(Widget
, XButtonEvent
* event
);
99 static void wxCanvasEnterLeave(Widget drawingArea
, XtPointer clientData
, XCrossingEvent
* event
);
100 static void wxScrollBarCallback(Widget widget
, XtPointer clientData
,
101 XmScrollBarCallbackStruct
*cbs
);
102 static void wxPanelItemEventHandler(Widget wid
,
103 XtPointer client_data
,
105 Boolean
*continueToDispatch
);
110 // Helper function for 16-bit fonts
111 static int str16len(const char *s
)
115 while (s
[0] && s
[1]) {
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
130 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
131 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 IMPLEMENT_DYNAMIC_CLASS(wxWindow
, wxWindowBase
)
139 BEGIN_EVENT_TABLE(wxWindow
, wxWindowBase
)
140 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged
)
143 // ============================================================================
145 // ============================================================================
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
153 Widget w
= (Widget
)widget
;
161 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
163 Widget w
= (Widget
)widget
;
167 // Rationale: a lot of common operations (including but not
168 // limited to moving, resizing and appending items to a listbox)
169 // unmamange the widget, do their work, then manage it again.
170 // This means that, for example adding an item to a listbox will show it,
171 // or that most controls are shown every time they are moved or resized!
172 XtSetMappedWhenManaged( w
, domap
);
174 // if the widget is not unmanaged, it still intercepts
175 // mouse events, even if it is not mapped (and hence invisible)
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxWindow::Init()
197 m_needsRefresh
= true;
198 m_mainWidget
= (WXWidget
) 0;
200 m_winCaptured
= false;
208 m_drawingArea
= (WXWidget
) 0;
213 m_backingPixmap
= (WXPixmap
) 0;
224 // real construction (Init() must have been called before!)
225 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
229 const wxString
& name
)
231 wxCHECK_MSG( parent
, false, "can't create wxWindow without parent" );
233 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
235 parent
->AddChild(this);
237 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
238 m_foregroundColour
= *wxBLACK
;
240 //// TODO: we should probably optimize by only creating a
241 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
242 //// But for now, let's simplify things by always creating the
243 //// drawing area, since otherwise the translations are different.
245 // New translations for getting mouse motion feedback
246 static const String translations
= wxMOTIF_STR(
247 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
248 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
249 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
250 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
251 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
252 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
253 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
254 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
255 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
256 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
257 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
258 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
259 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
260 <Key>: DrawingAreaInput()");
262 XtActionsRec actions
[1];
263 actions
[0].string
= wxMOTIF_STR("wxCanvasMotionEvent");
264 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
265 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
267 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
268 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
270 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
273 xmScrolledWindowWidgetClass
,
274 m_borderWidget
? (Widget
) m_borderWidget
276 XmNresizePolicy
, XmRESIZE_NONE
,
278 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
279 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
283 XtTranslations ptr
= XtParseTranslationTable(translations
);
284 m_drawingArea
= (WXWidget
)XtVaCreateWidget
287 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
288 XmNunitType
, XmPIXELS
,
289 // XmNresizePolicy, XmRESIZE_ANY,
290 XmNresizePolicy
, XmRESIZE_NONE
,
293 XmNtranslations
, ptr
,
296 XtFree((char *) ptr
);
299 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
301 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
302 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
303 XtFree ((char *) ptr
);
307 wxAddWindowToTable((Widget
) m_drawingArea
, this);
308 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
310 // This order is very important in Motif 1.2.1
311 XtRealizeWidget ((Widget
) m_scrolledWindow
);
312 XtRealizeWidget ((Widget
) m_drawingArea
);
313 XtManageChild ((Widget
) m_drawingArea
);
315 ptr
= XtParseTranslationTable("<Configure>: resize()");
316 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
317 XtFree ((char *) ptr
);
319 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
320 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
323 (Widget
)m_drawingArea
,
324 PointerMotionHintMask
| EnterWindowMask
|
325 LeaveWindowMask
| FocusChangeMask
,
327 (XtEventHandler
) wxCanvasEnterLeave
,
331 // Scrolled widget needs to have its colour changed or we get a little blue
332 // square where the scrollbars abutt
333 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
334 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, true);
335 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, true);
337 XmScrolledWindowSetAreas(
338 (Widget
)m_scrolledWindow
,
339 (Widget
) 0, (Widget
) 0,
340 (Widget
) m_drawingArea
);
342 // Without this, the cursor may not be restored properly (e.g. in splitter
344 SetCursor(*wxSTANDARD_CURSOR
);
345 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
346 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, true);
351 wxWindow::~wxWindow()
353 if (g_captureWindow
== this)
354 g_captureWindow
= NULL
;
356 m_isBeingDeleted
= true;
358 // Motif-specific actions first
359 WXWidget wMain
= GetMainWidget();
362 // Removes event handlers
366 // If m_drawingArea, we're a fully-fledged window with drawing area,
367 // scrollbars etc. (what wxCanvas used to be)
370 // Destroy children before destroying self
374 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
376 Widget w
= (Widget
) m_drawingArea
;
377 wxDeleteWindowFromTable(w
);
382 m_drawingArea
= (WXWidget
) 0;
385 // Only if we're _really_ a canvas (not a dialog box/panel)
386 if (m_scrolledWindow
)
388 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
393 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
394 XtUnmanageChild((Widget
) m_hScrollBar
);
398 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
399 XtUnmanageChild((Widget
) m_vScrollBar
);
403 XtDestroyWidget((Widget
) m_hScrollBar
);
405 XtDestroyWidget((Widget
) m_vScrollBar
);
407 UnmanageAndDestroy(m_scrolledWindow
);
411 XtDestroyWidget ((Widget
) m_borderWidget
);
412 m_borderWidget
= (WXWidget
) 0;
415 else // Why wasn't this here before? JACS 8/3/2000
419 // Destroy the window
422 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
423 // Child widgets will get destroyed automatically when a frame
424 // or dialog is destroyed, but before that you may get some memory
425 // leaks and potential layout problems if you delete and then add
428 // GRG, Feb/2000: commented this out when adding support for
429 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
430 // that this call crashed wxMotif under OS/2, so it seems
431 // that leaving it out is the right thing to do.
432 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
433 XtDestroyWidget((Widget
) GetMainWidget());
434 SetMainWidget((WXWidget
) NULL
);
438 // ----------------------------------------------------------------------------
439 // scrollbar management
440 // ----------------------------------------------------------------------------
442 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
443 wxOrientation orientation
,
446 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
448 XtVaCreateManagedWidget( "scrollBarWidget",
449 xmScrollBarWidgetClass
, (Widget
)parent
,
450 XmNorientation
, orient
,
455 XtPointer o
= (XtPointer
)orientation
;
456 XtCallbackProc cb
= (XtCallbackProc
)callback
;
458 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
459 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
460 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
461 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
462 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
463 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
464 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
465 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
471 void wxWindow::CreateScrollbar(wxOrientation orientation
)
473 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
475 XtVaSetValues( (Widget
) m_scrolledWindow
,
476 XmNresizePolicy
, XmRESIZE_NONE
,
479 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
480 // Add scrollbars if required
481 if (orientation
== wxHORIZONTAL
)
483 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
484 (void (*)())wxScrollBarCallback
);
486 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, true);
488 XtRealizeWidget( (Widget
)m_hScrollBar
);
490 XtVaSetValues((Widget
) m_scrolledWindow
,
491 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
494 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
496 else if (orientation
== wxVERTICAL
)
498 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
499 (void (*)())wxScrollBarCallback
);
501 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, true);
503 XtRealizeWidget((Widget
)m_vScrollBar
);
505 XtVaSetValues((Widget
) m_scrolledWindow
,
506 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
509 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
512 XtVaSetValues( (Widget
) m_scrolledWindow
,
513 XmNresizePolicy
, XmRESIZE_ANY
,
517 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
519 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
521 XtVaSetValues((Widget
) m_scrolledWindow
,
522 XmNresizePolicy
, XmRESIZE_NONE
,
524 String stringSB
= orientation
== wxHORIZONTAL
?
525 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
526 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
527 &m_hScrollBar
: &m_vScrollBar
;
531 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
532 XtDestroyWidget( (Widget
)*widgetSB
);
533 *widgetSB
= (WXWidget
)NULL
;
536 XtVaSetValues( (Widget
)m_scrolledWindow
,
537 stringSB
, (Widget
) 0,
540 XtVaSetValues((Widget
) m_scrolledWindow
,
541 XmNresizePolicy
, XmRESIZE_ANY
,
545 // ---------------------------------------------------------------------------
547 // ---------------------------------------------------------------------------
549 void wxWindow::SetFocus()
551 Widget wMain
= (Widget
) GetMainWidget();
552 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
553 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
556 // Get the window with the focus
557 wxWindow
*wxWindowBase::DoFindFocus()
560 // (1) Can there be multiple focussed widgets in an application?
561 // In which case we need to find the top-level window that's
563 // (2) The widget with the focus may not be in the widget table
564 // depending on which widgets I put in the table
565 wxWindow
*winFocus
= (wxWindow
*)NULL
;
566 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
568 node
= node
->GetNext() )
570 wxWindow
*win
= node
->GetData();
572 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
574 if (w
!= (Widget
) NULL
)
576 winFocus
= wxGetWindowFromTable(w
);
585 bool wxWindow::Enable(bool enable
)
587 if ( !wxWindowBase::Enable(enable
) )
590 Widget wMain
= (Widget
)GetMainWidget();
593 XtSetSensitive(wMain
, enable
);
594 XmUpdateDisplay(wMain
);
600 bool wxWindow::Show(bool show
)
602 if ( !wxWindowBase::Show(show
) )
605 if (m_borderWidget
|| m_scrolledWindow
)
607 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
608 // MapOrUnmap(m_drawingArea, show);
612 if ( !MapOrUnmap(GetTopWidget(), show
) )
613 MapOrUnmap(GetMainWidget(), show
);
619 // Raise the window to the top of the Z order
620 void wxWindow::Raise()
622 Widget wTop
= (Widget
) GetTopWidget();
623 Window window
= XtWindow(wTop
);
624 XRaiseWindow(XtDisplay(wTop
), window
);
627 // Lower the window to the bottom of the Z order
628 void wxWindow::Lower()
630 Widget wTop
= (Widget
) GetTopWidget();
631 Window window
= XtWindow(wTop
);
632 XLowerWindow(XtDisplay(wTop
), window
);
635 void wxWindow::SetTitle(const wxString
& title
)
637 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
640 wxString
wxWindow::GetTitle() const
643 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
645 return wxString(title
);
648 void wxWindow::DoCaptureMouse()
650 g_captureWindow
= this;
654 Widget wMain
= (Widget
)GetMainWidget();
656 XtAddGrab(wMain
, True
, False
);
658 m_winCaptured
= true;
661 void wxWindow::DoReleaseMouse()
663 g_captureWindow
= NULL
;
664 if ( !m_winCaptured
)
667 Widget wMain
= (Widget
)GetMainWidget();
671 m_winCaptured
= false;
674 bool wxWindow::SetFont(const wxFont
& font
)
676 if ( !wxWindowBase::SetFont(font
) )
687 bool wxWindow::SetCursor(const wxCursor
& cursor
)
689 if ( !wxWindowBase::SetCursor(cursor
) )
695 // wxASSERT_MSG( m_cursor.Ok(),
696 // wxT("cursor must be valid after call to the base version"));
697 wxCursor
* cursor2
= NULL
;
699 cursor2
= & m_cursor
;
701 cursor2
= wxSTANDARD_CURSOR
;
703 WXDisplay
*dpy
= GetXDisplay();
704 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
706 Widget w
= (Widget
) GetMainWidget();
707 Window win
= XtWindow(w
);
708 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
713 // Coordinates relative to the window
714 void wxWindow::WarpPointer (int x
, int y
)
716 Widget wClient
= (Widget
)GetClientWidget();
718 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
721 // ---------------------------------------------------------------------------
723 // ---------------------------------------------------------------------------
725 int wxWindow::GetScrollPos(int orient
) const
727 if (orient
== wxHORIZONTAL
)
733 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
737 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
745 // This now returns the whole range, not just the number of positions that we
747 int wxWindow::GetScrollRange(int orient
) const
749 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
750 // CE scintilla windows don't always have these scrollbars
751 // and it tends to pile up a whole bunch of asserts
752 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
756 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
760 int wxWindow::GetScrollThumb(int orient
) const
762 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
763 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
767 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
771 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
773 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
777 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
780 SetInternalScrollPos((wxOrientation
)orient
, pos
);
783 // New function that will replace some of the above.
784 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
785 int range
, bool WXUNUSED(refresh
))
788 GetSize(& oldW
, & oldH
);
792 if (thumbVisible
== 0)
795 if (thumbVisible
> range
)
796 thumbVisible
= range
;
798 // Save the old state to see if it changed
799 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
801 if (orient
== wxHORIZONTAL
)
803 if (thumbVisible
== range
)
806 DestroyScrollbar(wxHORIZONTAL
);
811 CreateScrollbar(wxHORIZONTAL
);
814 if (orient
== wxVERTICAL
)
816 if (thumbVisible
== range
)
819 DestroyScrollbar(wxVERTICAL
);
824 CreateScrollbar(wxVERTICAL
);
827 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
829 if (oldScrollBar
!= newScrollBar
)
831 // This is important! Without it, scrollbars misbehave badly.
832 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
833 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
834 XtRealizeWidget((Widget
) m_scrolledWindow
);
835 XtManageChild((Widget
) m_scrolledWindow
);
840 XtVaSetValues((Widget
) newScrollBar
,
844 XmNsliderSize
, thumbVisible
,
848 SetInternalScrollPos((wxOrientation
)orient
, pos
);
851 GetSize(& newW
, & newH
);
853 // Adjusting scrollbars can resize the canvas accidentally
854 if (newW
!= oldW
|| newH
!= oldH
)
855 SetSize(-1, -1, oldW
, oldH
);
858 // Does a physical scroll
859 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
864 // Use specified rectangle
865 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
869 // Use whole client area
871 GetClientSize(& w
, & h
);
874 int x1
= (dx
>= 0) ? x
: x
- dx
;
875 int y1
= (dy
>= 0) ? y
: y
- dy
;
876 int w1
= w
- abs(dx
);
877 int h1
= h
- abs(dy
);
878 int x2
= (dx
>= 0) ? x
+ dx
: x
;
879 int y2
= (dy
>= 0) ? y
+ dy
: y
;
883 dc
.SetLogicalFunction (wxCOPY
);
885 Widget widget
= (Widget
) GetMainWidget();
886 Window window
= XtWindow(widget
);
887 Display
* display
= XtDisplay(widget
);
889 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
890 x1
, y1
, w1
, h1
, x2
, y2
);
892 dc
.SetAutoSetting(true);
893 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
894 dc
.SetBrush(brush
); // FIXME: needed?
896 wxWindowList::compatibility_iterator cnode
= m_children
.GetFirst();
899 wxWindow
*child
= cnode
->GetData();
902 child
->GetSize( &sx
, &sy
);
903 wxPoint
pos( child
->GetPosition() );
904 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
905 cnode
= cnode
->GetNext();
908 // We'll add rectangles to the list of update rectangles according to which
909 // bits we've exposed.
914 wxRect
*rect
= new wxRect
;
920 XFillRectangle(display
, window
,
921 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
925 rect
->width
= rect
->width
;
926 rect
->height
= rect
->height
;
928 updateRects
.Append((wxObject
*) rect
);
932 wxRect
*rect
= new wxRect
;
934 rect
->x
= x
+ w
+ dx
;
939 XFillRectangle(display
, window
,
940 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
945 rect
->width
= rect
->width
;
946 rect
->height
= rect
->height
;
948 updateRects
.Append((wxObject
*) rect
);
952 wxRect
*rect
= new wxRect
;
959 XFillRectangle(display
, window
,
960 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
964 rect
->width
= rect
->width
;
965 rect
->height
= rect
->height
;
967 updateRects
.Append((wxObject
*) rect
);
971 wxRect
*rect
= new wxRect
;
974 rect
->y
= y
+ h
+ dy
;
978 XFillRectangle(display
, window
,
979 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
983 rect
->width
= rect
->width
;
984 rect
->height
= rect
->height
;
986 updateRects
.Append((wxObject
*) rect
);
988 dc
.SetBrush(wxNullBrush
);
990 // Now send expose events
992 wxList::compatibility_iterator node
= updateRects
.GetFirst();
995 wxRect
* rect
= (wxRect
*) node
->GetData();
999 event
.display
= display
;
1000 event
.send_event
= True
;
1001 event
.window
= window
;
1005 event
.width
= rect
->width
;
1006 event
.height
= rect
->height
;
1010 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1012 node
= node
->GetNext();
1016 // Delete the update rects
1017 node
= updateRects
.GetFirst();
1020 wxRect
* rect
= (wxRect
*) node
->GetData();
1022 node
= node
->GetNext();
1025 XmUpdateDisplay((Widget
) GetMainWidget());
1028 // ---------------------------------------------------------------------------
1030 // ---------------------------------------------------------------------------
1032 #if wxUSE_DRAG_AND_DROP
1034 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1041 // Old style file-manager drag&drop
1042 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1047 // ----------------------------------------------------------------------------
1049 // ----------------------------------------------------------------------------
1053 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1058 #endif // wxUSE_TOOLTIPS
1060 // ----------------------------------------------------------------------------
1062 // ----------------------------------------------------------------------------
1066 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
1068 if ( x
== -1 && y
== -1 )
1070 wxPoint mouse
= ScreenToClient(wxGetMousePosition());
1071 x
= mouse
.x
; y
= mouse
.y
;
1074 Widget widget
= (Widget
) GetMainWidget();
1076 /* The menuId field seems to be usused, so we'll use it to
1077 indicate whether a menu is popped up or not:
1078 0: Not currently created as a popup
1079 -1: Created as a popup, but not active
1083 if (menu
->GetParent() && (menu
->GetId() != -1))
1086 if (menu
->GetMainWidget())
1088 menu
->DestroyMenu(true);
1091 menu
->SetId(1); /* Mark as popped-up */
1092 menu
->CreateMenu(NULL
, widget
, menu
, 0);
1093 menu
->SetInvokingWindow(this);
1097 // menu->SetParent(parent);
1098 // parent->children->Append(menu); // Store menu for later deletion
1100 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1108 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1110 wxCanvas *canvas = (wxCanvas *) this;
1111 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1112 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1116 Display
*display
= XtDisplay (widget
);
1117 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1118 Window thisWindow
= XtWindow (widget
);
1120 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1121 &rootX
, &rootY
, &childWindow
);
1123 XButtonPressedEvent event
;
1124 event
.type
= ButtonPress
;
1130 event
.x_root
= rootX
;
1131 event
.y_root
= rootY
;
1133 XmMenuPosition (menuWidget
, &event
);
1134 XtManageChild (menuWidget
);
1136 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1137 // idle-time destroy routine.
1138 // Waiting until this ID changes causes this function to block until
1139 // the menu has been dismissed and the widgets cleaned up.
1140 // In other words, once this routine returns, it is safe to delete
1142 // Ian Brown <ian.brown@printsoft.de>
1144 wxEventLoop evtLoop
;
1146 while (menu
->GetId() == 1)
1148 wxDoEventLoopIteration( evtLoop
);
1156 // ---------------------------------------------------------------------------
1157 // moving and resizing
1158 // ---------------------------------------------------------------------------
1160 bool wxWindow::PreResize()
1166 void wxWindow::DoGetSize(int *x
, int *y
) const
1168 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1169 ( m_borderWidget
? m_borderWidget
:
1170 m_scrolledWindow
? m_scrolledWindow
:
1174 XtVaGetValues( widget
,
1182 void wxWindow::DoGetPosition(int *x
, int *y
) const
1184 Widget widget
= (Widget
)
1186 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1190 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1192 // We may be faking the client origin. So a window that's really at (0, 30)
1193 // may appear (to wxWin apps) to be at (0, 0).
1196 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1205 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1207 Widget widget
= (Widget
) GetClientWidget();
1208 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1209 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1210 Window thisWindow
= XtWindow(widget
);
1215 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1218 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1220 Widget widget
= (Widget
) GetClientWidget();
1221 Display
*display
= XtDisplay(widget
);
1222 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1223 Window thisWindow
= XtWindow(widget
);
1228 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1232 // Get size *available for subwindows* i.e. excluding menu bar etc.
1233 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1235 Widget widget
= (Widget
) GetClientWidget();
1237 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1238 if(x
) *x
= xx
; if(y
) *y
= yy
;
1241 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1243 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, false);
1246 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1247 int sizeFlags
, bool fromCtor
)
1249 // A bit of optimization to help sort out the flickers.
1250 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1253 GetSize(& oldW
, & oldH
);
1254 GetPosition(& oldX
, & oldY
);
1257 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1265 wxSize
size(-1, -1);
1268 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1270 size
= DoGetBestSize();
1281 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1283 if( size
.x
== -1 ) size
= DoGetBestSize();
1292 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1293 || !wxNoOptimize::CanOptimize() )
1299 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1302 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1306 flags
|= wxMOVE_WIDTH
;
1309 flags
|= wxMOVE_HEIGHT
;
1311 int xx
= x
; int yy
= y
;
1312 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1314 DoMoveWindow( xx
, yy
, width
, height
);
1316 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1321 Widget widget
= (Widget
) GetTopWidget();
1325 bool managed
= XtIsManaged( widget
);
1327 XtUnmanageChild(widget
);
1331 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1333 DoMoveWindow(xx
, yy
, width
, height
);
1336 XtManageChild(widget
);
1340 void wxWindow::DoSetClientSize(int width
, int height
)
1344 Widget drawingArea
= (Widget
) m_drawingArea
;
1346 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1349 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1351 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1353 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1357 Widget widget
= (Widget
) GetTopWidget();
1360 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1362 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1365 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1370 Widget drawingArea
= (Widget
) m_drawingArea
;
1371 Widget borderOrScrolled
= m_borderWidget
?
1372 (Widget
) m_borderWidget
:
1373 (Widget
) m_scrolledWindow
;
1375 bool managed
= XtIsManaged(borderOrScrolled
);
1377 XtUnmanageChild (borderOrScrolled
);
1378 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1380 if (flags
& wxMOVE_X
)
1381 XtVaSetValues (borderOrScrolled
,
1384 if (flags
& wxMOVE_Y
)
1385 XtVaSetValues (borderOrScrolled
,
1389 if (flags
& wxMOVE_WIDTH
)
1393 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1394 short thick
, margin
;
1395 XtVaGetValues ((Widget
) m_borderWidget
,
1396 XmNshadowThickness
, &thick
,
1397 XmNmarginWidth
, &margin
,
1399 w
-= 2 * (thick
+ margin
);
1403 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1406 if (flags
& wxMOVE_HEIGHT
)
1410 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1411 short thick
, margin
;
1412 XtVaGetValues ((Widget
) m_borderWidget
,
1413 XmNshadowThickness
, &thick
,
1414 XmNmarginHeight
, &margin
,
1416 h
-= 2 * (thick
+ margin
);
1420 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1424 XtManageChild (borderOrScrolled
);
1425 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1429 if( xx
< 0 ) xx
= 0;
1430 if( yy
< 0 ) yy
= 0;
1434 XtVaSetValues((Widget
)GetTopWidget(),
1443 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1445 DoMoveWindowIntr (x
, y
, width
, height
,
1446 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1449 // ---------------------------------------------------------------------------
1451 // ---------------------------------------------------------------------------
1453 int wxWindow::GetCharHeight() const
1455 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1459 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1460 "x", NULL
, &height
, NULL
, NULL
);
1465 int wxWindow::GetCharWidth() const
1467 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1471 wxGetTextExtent (GetXDisplay(), m_font
, 1.0,
1472 "x", &width
, NULL
, NULL
, NULL
);
1477 void wxWindow::GetTextExtent(const wxString
& string
,
1479 int *descent
, int *externalLeading
,
1480 const wxFont
*theFont
) const
1482 const wxFont
*fontToUse
= theFont
? theFont
: &m_font
;
1484 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1486 if (externalLeading
)
1487 *externalLeading
= 0;
1488 wxGetTextExtent (GetXDisplay(), *fontToUse
, 1.0,
1489 string
, x
, y
, NULL
, descent
);
1492 // ----------------------------------------------------------------------------
1494 // ----------------------------------------------------------------------------
1496 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1498 m_updateRegion
.Union( x
, y
, w
, h
);
1501 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1503 m_needsRefresh
= true;
1504 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1505 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1507 XExposeEvent dummyEvent
;
1509 GetSize(&width
, &height
);
1511 dummyEvent
.type
= Expose
;
1512 dummyEvent
.display
= display
;
1513 dummyEvent
.send_event
= True
;
1514 dummyEvent
.window
= thisWindow
;
1517 dummyEvent
.x
= rect
->x
;
1518 dummyEvent
.y
= rect
->y
;
1519 dummyEvent
.width
= rect
->width
;
1520 dummyEvent
.height
= rect
->height
;
1526 dummyEvent
.width
= width
;
1527 dummyEvent
.height
= height
;
1529 dummyEvent
.count
= 0;
1533 wxClientDC
dc(this);
1534 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1535 dc
.SetBackground(backgroundBrush
);
1542 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1545 void wxWindow::DoPaint()
1547 //TODO : make a temporary gc so we can do the XCopyArea below
1548 if (m_backingPixmap
&& !m_needsRefresh
)
1552 GC tempGC
= (GC
) dc
.GetBackingGC();
1554 Widget widget
= (Widget
) GetMainWidget();
1559 // We have to test whether it's a wxScrolledWindow (hack!) because
1560 // otherwise we don't know how many pixels have been scrolled. We might
1561 // solve this in the future by defining virtual wxWindow functions to get
1562 // the scroll position in pixels. Or, each kind of scrolled window has to
1563 // implement backing stores itself, using generic wxWidgets code.
1564 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1565 if ( scrolledWindow
)
1568 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1574 // TODO: This could be optimized further by only copying the areas in the
1575 // current update region.
1577 // Only blit the part visible in the client area. The backing pixmap
1578 // always starts at 0, 0 but we may be looking at only a portion of it.
1579 wxSize clientArea
= GetClientSize();
1580 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1581 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1583 // Copy whichever is samller, the amount of pixmap we have to copy,
1584 // or the size of the client area.
1585 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1586 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1588 // Make sure we're not negative
1589 toBlitX
= wxMax(0, toBlitX
);
1590 toBlitY
= wxMax(0, toBlitY
);
1595 (Pixmap
) m_backingPixmap
,
1598 scrollPosX
, scrollPosY
, // Start at the scroll position
1599 toBlitX
, toBlitY
, // How much of the pixmap to copy
1605 wxWindowDC
dc(this);
1606 // Set an erase event first
1607 wxEraseEvent
eraseEvent(GetId(), &dc
);
1608 eraseEvent
.SetEventObject(this);
1609 GetEventHandler()->ProcessEvent(eraseEvent
);
1611 wxPaintEvent
event(GetId());
1612 event
.SetEventObject(this);
1613 GetEventHandler()->ProcessEvent(event
);
1615 m_needsRefresh
= false;
1619 // ----------------------------------------------------------------------------
1621 // ----------------------------------------------------------------------------
1623 // Responds to colour changes: passes event on to children.
1624 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1626 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1629 // Only propagate to non-top-level windows
1630 wxWindow
*win
= node
->GetData();
1631 if ( win
->GetParent() )
1633 wxSysColourChangedEvent event2
;
1634 event
.SetEventObject(win
);
1635 win
->GetEventHandler()->ProcessEvent(event2
);
1638 node
= node
->GetNext();
1642 void wxWindow::OnInternalIdle()
1644 // This calls the UI-update mechanism (querying windows for
1645 // menu/toolbar/control state information)
1646 if (wxUpdateUIEvent::CanUpdate(this))
1647 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1650 // ----------------------------------------------------------------------------
1652 // ----------------------------------------------------------------------------
1654 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1657 if (!m_acceleratorTable
.Ok())
1660 int count
= m_acceleratorTable
.GetCount();
1661 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1663 for (i
= 0; i
< count
; i
++)
1665 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1666 if (entry
->MatchesEvent(event
))
1668 // Bingo, we have a match. Now find a control that matches the
1669 // entry command id.
1671 // Need to go up to the top of the window hierarchy, since it might
1672 // be e.g. a menu item
1673 wxWindow
* parent
= this;
1674 while ( parent
&& !parent
->IsTopLevel() )
1675 parent
= parent
->GetParent();
1680 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1684 // Try for a menu command
1685 if (frame
->GetMenuBar())
1687 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1690 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1691 commandEvent
.SetEventObject(frame
);
1693 // If ProcessEvent returns true (it was handled), then
1694 // the calling code will skip the event handling.
1695 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1701 // Find a child matching the command id
1702 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1708 // Now we process those kinds of windows that we can.
1709 // For now, only buttons.
1710 if ( wxDynamicCast(child
, wxButton
) )
1712 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1713 commandEvent
.SetEventObject(child
);
1714 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1722 // We didn't match the key event against an accelerator.
1726 // ============================================================================
1727 // Motif-specific stuff from here on
1728 // ============================================================================
1730 // ----------------------------------------------------------------------------
1731 // function which maintain the global hash table mapping Widgets to wxWidgets
1732 // ----------------------------------------------------------------------------
1734 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1736 const long key
= (long)w
;
1737 if ( wxWidgetHashTable
->Get(key
))
1739 wxLogDebug("Widget table clash: new widget is %ld, %s",
1740 key
, win
->GetClassInfo()->GetClassName());
1744 wxWidgetHashTable
->Put(key
, win
);
1746 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1747 w
, win
, win
->GetClassInfo()->GetClassName());
1752 wxWindow
*wxGetWindowFromTable(Widget w
)
1754 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1757 void wxDeleteWindowFromTable(Widget w
)
1759 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1761 wxWidgetHashTable
->Delete((long)w
);
1764 // ----------------------------------------------------------------------------
1765 // add/remove window from the table
1766 // ----------------------------------------------------------------------------
1768 // Add to hash table, add event handler
1769 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1770 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1772 wxAddWindowToTable((Widget
) mainWidget
, this);
1773 XtAddEventHandler( (Widget
) mainWidget
,
1774 ButtonPressMask
| ButtonReleaseMask
1775 | PointerMotionMask
,
1777 wxPanelItemEventHandler
,
1783 XtOverrideTranslations ((Widget
) mainWidget
,
1784 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1785 XtFree ((char *) ptr
);
1788 // Some widgets have a parent form widget, e.g. wxRadioBox
1791 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1795 XtOverrideTranslations ((Widget
) formWidget
,
1796 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1797 XtFree ((char *) ptr
);
1804 DoSetSize (x
, y
, width
, height
, wxSIZE_USE_EXISTING
);
1809 // Remove event handler, remove from hash table
1810 bool wxWindow::DetachWidget(WXWidget widget
)
1812 XtRemoveEventHandler( (Widget
) widget
,
1813 ButtonPressMask
| ButtonReleaseMask
1814 | PointerMotionMask
,
1816 wxPanelItemEventHandler
,
1819 wxDeleteWindowFromTable((Widget
) widget
);
1823 // ----------------------------------------------------------------------------
1824 // Motif-specific accessors
1825 // ----------------------------------------------------------------------------
1827 WXWindow
wxWindow::GetClientXWindow() const
1829 Widget wMain
= (Widget
)GetClientWidget();
1831 return (WXWindow
) XtWindow(wMain
);
1833 return (WXWindow
) 0;
1836 // Get the underlying X window
1837 WXWindow
wxWindow::GetXWindow() const
1839 Widget wMain
= (Widget
)GetMainWidget();
1841 return (WXWindow
) XtWindow(wMain
);
1843 return (WXWindow
) 0;
1846 // Get the underlying X display
1847 WXDisplay
*wxWindow::GetXDisplay() const
1849 Widget wMain
= (Widget
)GetMainWidget();
1851 return (WXDisplay
*) XtDisplay(wMain
);
1853 return (WXDisplay
*) NULL
;
1856 WXWidget
wxWindow::GetMainWidget() const
1859 return m_drawingArea
;
1861 return m_mainWidget
;
1864 WXWidget
wxWindow::GetClientWidget() const
1866 if (m_drawingArea
!= (WXWidget
) 0)
1867 return m_drawingArea
;
1869 return GetMainWidget();
1872 WXWidget
wxWindow::GetTopWidget() const
1874 return GetMainWidget();
1877 WXWidget
wxWindow::GetLabelWidget() const
1879 return GetMainWidget();
1882 // ----------------------------------------------------------------------------
1884 // ----------------------------------------------------------------------------
1886 // All widgets should have this as their resize proc.
1887 // OnSize sent to wxWindow via client data.
1888 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1889 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1891 wxWindow
*win
= wxGetWindowFromTable(w
);
1895 if (win
->PreResize())
1898 win
->GetSize(&width
, &height
);
1899 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1900 sizeEvent
.SetEventObject(win
);
1901 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1905 static void wxCanvasRepaintProc(Widget drawingArea
,
1906 XtPointer clientData
,
1907 XmDrawingAreaCallbackStruct
* cbs
)
1909 if (!wxGetWindowFromTable(drawingArea
))
1912 XEvent
* event
= cbs
->event
;
1913 wxWindow
* win
= (wxWindow
*) clientData
;
1915 switch (event
->type
)
1919 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1920 event
->xexpose
.width
, event
->xexpose
.height
);
1922 if (event
-> xexpose
.count
== 0)
1931 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1932 static void wxCanvasEnterLeave(Widget drawingArea
,
1933 XtPointer
WXUNUSED(clientData
),
1934 XCrossingEvent
* event
)
1936 XmDrawingAreaCallbackStruct cbs
;
1939 ((XCrossingEvent
&) ev
) = *event
;
1941 cbs
.reason
= XmCR_INPUT
;
1944 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1947 // Fix to make it work under Motif 1.0 (!)
1948 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1949 XButtonEvent
*WXUNUSED(event
))
1951 #if XmVersion <= 1000
1952 XmDrawingAreaCallbackStruct cbs
;
1955 ev
= *((XEvent
*) event
);
1956 cbs
.reason
= XmCR_INPUT
;
1959 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
1960 #endif // XmVersion <= 1000
1963 static void wxCanvasInputEvent(Widget drawingArea
,
1964 XtPointer
WXUNUSED(data
),
1965 XmDrawingAreaCallbackStruct
* cbs
)
1967 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
1968 XEvent
* xevent
= cbs
->event
;
1973 if (cbs
->reason
!= XmCR_INPUT
)
1976 switch (xevent
->xany
.type
)
1984 wxMouseEvent
wxevent(0);
1985 if (wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, xevent
))
1987 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
1993 wxKeyEvent
event (wxEVT_CHAR
);
1994 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, xevent
))
1996 // Implement wxFrame::OnCharHook by checking ancestor.
1997 wxWindow
*parent
= canvas
;
1998 while (parent
&& !parent
->IsTopLevel())
1999 parent
= parent
->GetParent();
2003 event
.SetEventType(wxEVT_CHAR_HOOK
);
2004 if (parent
->GetEventHandler()->ProcessEvent(event
))
2008 // For simplicity, OnKeyDown is the same as OnChar
2009 // TODO: filter modifier key presses from OnChar
2010 event
.SetEventType(wxEVT_KEY_DOWN
);
2012 // Only process OnChar if OnKeyDown didn't swallow it
2013 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2015 event
.SetEventType(wxEVT_CHAR
);
2016 canvas
->GetEventHandler()->ProcessEvent (event
);
2023 wxKeyEvent
event (wxEVT_KEY_UP
);
2024 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, xevent
))
2026 canvas
->GetEventHandler()->ProcessEvent (event
);
2032 if (xevent
->xfocus
.detail
!= NotifyPointer
)
2034 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2035 event
.SetEventObject(canvas
);
2036 canvas
->GetEventHandler()->ProcessEvent(event
);
2042 if (xevent
->xfocus
.detail
!= NotifyPointer
)
2044 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2045 event
.SetEventObject(canvas
);
2046 canvas
->GetEventHandler()->ProcessEvent(event
);
2055 static void wxPanelItemEventHandler(Widget wid
,
2056 XtPointer
WXUNUSED(client_data
),
2058 Boolean
*continueToDispatch
)
2060 // Widget can be a label or the actual widget.
2062 wxWindow
*window
= wxGetWindowFromTable(wid
);
2065 wxMouseEvent
wxevent(0);
2066 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2068 window
->GetEventHandler()->ProcessEvent(wxevent
);
2072 // TODO: probably the key to allowing default behaviour to happen. Say we
2073 // set a m_doDefault flag to false at the start of this function. Then in
2074 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2075 // true, indicating that default processing can happen. Thus, behaviour can
2076 // appear to be overridden just by adding an event handler and not calling
2077 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2078 // way of handling drawing area events, to simplify things.
2079 *continueToDispatch
= True
;
2082 static void wxScrollBarCallback(Widget scrollbar
,
2083 XtPointer clientData
,
2084 XmScrollBarCallbackStruct
*cbs
)
2086 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2087 wxOrientation orientation
= (wxOrientation
)wxPtrToUInt(clientData
);
2089 wxEventType eventType
= wxEVT_NULL
;
2090 switch (cbs
->reason
)
2092 case XmCR_INCREMENT
:
2094 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2097 case XmCR_DECREMENT
:
2099 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2104 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2107 case XmCR_VALUE_CHANGED
:
2109 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2112 case XmCR_PAGE_INCREMENT
:
2114 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2117 case XmCR_PAGE_DECREMENT
:
2119 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2124 eventType
= wxEVT_SCROLLWIN_TOP
;
2127 case XmCR_TO_BOTTOM
:
2129 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2134 // Should never get here
2135 wxFAIL_MSG("Unknown scroll event.");
2140 wxScrollWinEvent
event(eventType
,
2143 event
.SetEventObject( win
);
2144 win
->GetEventHandler()->ProcessEvent(event
);
2147 // For repainting arbitrary windows
2148 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2150 wxWindow
* win
= wxGetWindowFromTable(w
);
2154 switch ( event
->type
)
2158 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2159 event
->xexpose
.width
, event
->xexpose
.height
);
2161 if ( event
->xexpose
.count
== 0 )
2171 // ----------------------------------------------------------------------------
2172 // TranslateXXXEvent() functions
2173 // ----------------------------------------------------------------------------
2175 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2176 Widget widget
, const XEvent
*xevent
)
2178 switch (xevent
->xany
.type
)
2183 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2184 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2185 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2191 int eventx
= xevent
->xbutton
.x
, eventy
= xevent
->xbutton
.y
;
2193 wxEventType eventType
= wxEVT_NULL
;
2195 if (xevent
->xany
.type
== LeaveNotify
)
2197 eventType
= wxEVT_LEAVE_WINDOW
;
2199 if (xevent
->xany
.type
== EnterNotify
)
2201 eventType
= wxEVT_ENTER_WINDOW
;
2203 else if (xevent
->xany
.type
== MotionNotify
)
2205 eventType
= wxEVT_MOTION
;
2207 if (xevent
->xmotion
.is_hint
== NotifyHint
)
2212 Display
*dpy
= XtDisplay (widget
);
2214 XQueryPointer (dpy
, XtWindow (widget
),
2216 &x_root
, &y_root
, &eventx
, &eventy
, &state
);
2219 else if (xevent
->xany
.type
== ButtonPress
)
2221 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2223 if (xevent
->xbutton
.button
== Button1
)
2225 eventType
= wxEVT_LEFT_DOWN
;
2228 else if (xevent
->xbutton
.button
== Button2
)
2230 eventType
= wxEVT_MIDDLE_DOWN
;
2233 else if (xevent
->xbutton
.button
== Button3
)
2235 eventType
= wxEVT_RIGHT_DOWN
;
2239 // check for a double click
2241 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2242 long ts
= wxevent
.GetTimestamp();
2244 int buttonLast
= win
->GetLastClickedButton();
2245 long lastTS
= win
->GetLastClickTime();
2246 if ( buttonLast
&& buttonLast
== button
&&
2247 (ts
- lastTS
) < dclickTime
)
2250 win
->SetLastClick(0, ts
);
2251 if ( eventType
== wxEVT_LEFT_DOWN
)
2252 eventType
= wxEVT_LEFT_DCLICK
;
2253 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2254 eventType
= wxEVT_MIDDLE_DCLICK
;
2255 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2256 eventType
= wxEVT_RIGHT_DCLICK
;
2260 // not fast enough or different button
2261 win
->SetLastClick(button
, ts
);
2264 else if (xevent
->xany
.type
== ButtonRelease
)
2266 if (xevent
->xbutton
.button
== Button1
)
2268 eventType
= wxEVT_LEFT_UP
;
2270 else if (xevent
->xbutton
.button
== Button2
)
2272 eventType
= wxEVT_MIDDLE_UP
;
2274 else if (xevent
->xbutton
.button
== Button3
)
2276 eventType
= wxEVT_RIGHT_UP
;
2286 wxevent
.SetEventType(eventType
);
2289 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2292 win
->GetPosition(&x2
, &y2
);
2294 // The button x/y must be translated to wxWidgets
2295 // window space - the widget might be a label or button,
2299 if (widget
!= (Widget
)win
->GetMainWidget())
2305 wxevent
.m_x
= eventx
+ dx
;
2306 wxevent
.m_y
= eventy
+ dy
;
2308 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2309 || (event_left_is_down (xevent
)
2310 && (eventType
!= wxEVT_LEFT_UP
)));
2311 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2312 || (event_middle_is_down (xevent
)
2313 && (eventType
!= wxEVT_MIDDLE_UP
)));
2314 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2315 || (event_right_is_down (xevent
)
2316 && (eventType
!= wxEVT_RIGHT_UP
)));
2318 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2319 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2320 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2321 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2323 wxevent
.SetId(win
->GetId());
2324 wxevent
.SetEventObject(win
);
2332 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2333 Widget
WXUNUSED(widget
), const XEvent
*xevent
)
2335 switch (xevent
->xany
.type
)
2343 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2344 int id
= wxCharCodeXToWX (keySym
);
2345 // id may be WXK_xxx code - these are outside ASCII range, so we
2346 // can't just use toupper() on id
2347 if (id
>= 'a' && id
<= 'z')
2350 if (xevent
->xkey
.state
& ShiftMask
)
2351 wxevent
.m_shiftDown
= true;
2352 if (xevent
->xkey
.state
& ControlMask
)
2353 wxevent
.m_controlDown
= true;
2354 if (xevent
->xkey
.state
& Mod3Mask
)
2355 wxevent
.m_altDown
= true;
2356 if (xevent
->xkey
.state
& Mod1Mask
)
2357 wxevent
.m_metaDown
= true;
2358 wxevent
.SetEventObject(win
);
2359 wxevent
.m_keyCode
= id
;
2360 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2362 wxevent
.m_x
= xevent
->xbutton
.x
;
2363 wxevent
.m_y
= xevent
->xbutton
.y
;
2377 // ----------------------------------------------------------------------------
2379 // ----------------------------------------------------------------------------
2381 #define YAllocColor XAllocColor
2382 XColor g_itemColors
[5];
2383 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2386 static XmColorProc colorProc
;
2388 result
= wxNO_COLORS
;
2392 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2393 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2394 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2395 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2396 if (colorProc
== (XmColorProc
) NULL
)
2398 // Get a ptr to the actual function
2399 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2400 // And set it back to motif.
2401 XmSetColorCalculation (colorProc
);
2403 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2404 &g_itemColors
[wxFORE_INDEX
],
2405 &g_itemColors
[wxSELE_INDEX
],
2406 &g_itemColors
[wxTOPS_INDEX
],
2407 &g_itemColors
[wxBOTS_INDEX
]);
2408 result
= wxBACK_COLORS
;
2412 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2413 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2414 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2415 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2416 if (result
== wxNO_COLORS
)
2417 result
= wxFORE_COLORS
;
2420 Display
*dpy
= display
;
2421 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2425 /* 5 Colours to allocate */
2426 for (int i
= 0; i
< 5; i
++)
2427 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2428 result
= wxNO_COLORS
;
2432 /* Only 1 colour to allocate */
2433 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2434 result
= wxNO_COLORS
;
2440 // Changes the foreground and background colours to be derived from the current
2441 // background colour. To change the foreground colour, you must call
2442 // SetForegroundColour explicitly.
2443 void wxWindow::ChangeBackgroundColour()
2445 WXWidget mainWidget
= GetMainWidget();
2447 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2448 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2449 wxDoChangeForegroundColour(m_scrolledWindow
, m_backgroundColour
);
2452 void wxWindow::ChangeForegroundColour()
2454 WXWidget mainWidget
= GetMainWidget();
2456 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2457 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2458 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2461 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2463 if ( !wxWindowBase::SetBackgroundColour(col
) )
2466 ChangeBackgroundColour();
2471 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2473 if ( !wxWindowBase::SetForegroundColour(col
) )
2476 ChangeForegroundColour();
2481 void wxWindow::ChangeFont(bool keepOriginalSize
)
2483 // Note that this causes the widget to be resized back
2484 // to its original size! We therefore have to set the size
2485 // back again. TODO: a better way in Motif?
2486 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2487 if (w
&& m_font
.Ok())
2489 int width
, height
, width1
, height1
;
2490 GetSize(& width
, & height
);
2492 wxDoChangeFont( GetLabelWidget(), m_font
);
2494 GetSize(& width1
, & height1
);
2495 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2497 SetSize(-1, -1, width
, height
);
2502 // ----------------------------------------------------------------------------
2504 // ----------------------------------------------------------------------------
2506 wxWindow
*wxGetActiveWindow()
2509 wxFAIL_MSG("Not implemented");
2514 wxWindow
*wxWindowBase::GetCapture()
2516 return (wxWindow
*)g_captureWindow
;
2520 // Find the wxWindow at the current mouse position, returning the mouse
2522 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2524 pt
= wxGetMousePosition();
2525 return wxFindWindowAtPoint(pt
);
2528 // Get the current mouse position.
2529 wxPoint
wxGetMousePosition()
2531 Display
*display
= wxGlobalDisplay();
2532 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2533 Window rootReturn
, childReturn
;
2534 int rootX
, rootY
, winX
, winY
;
2535 unsigned int maskReturn
;
2537 XQueryPointer (display
,
2541 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2542 return wxPoint(rootX
, rootY
);
2546 // ----------------------------------------------------------------------------
2547 // wxNoOptimize: switch off size optimization
2548 // ----------------------------------------------------------------------------
2550 int wxNoOptimize::ms_count
= 0;