1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "window.h"
25 #define XtDisplay XTDISPLAY
26 #define XtWindow XTWINDOW
27 #define XtScreen XTSCREEN
33 #include "wx/dcclient.h"
36 #include "wx/layout.h"
37 #include "wx/button.h"
38 #include "wx/settings.h"
40 #include "wx/scrolwin.h"
41 #include "wx/module.h"
42 #include "wx/menuitem.h"
44 #include "wx/evtloop.h"
47 #if wxUSE_DRAG_AND_DROP
51 // DoSetSizeIntr and DoMoveWindowIntr
53 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
54 // did nott work and/or segfaulted because
55 // 1) wxWindow::Create calls SetSize,
56 // which results in a call to DoSetSize much earlier than in the other ports
57 // 2) if wxWindow::Create is called (wxControl::Create calls it)
58 // then DoSetSize is never called, causing layout problems in composite
62 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
63 // DoSetPosition directly or indirectly from wxWindow::Create
64 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
67 #pragma message disable nosimpint
71 #include <Xm/DrawingA.h>
72 #include <Xm/ScrolledW.h>
73 #include <Xm/ScrollBar.h>
76 #include <Xm/RowColumn.h> // for XmMenuPosition
78 #pragma message enable nosimpint
81 #include "wx/motif/private.h"
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 static const int SCROLL_MARGIN
= 4;
91 // ----------------------------------------------------------------------------
92 // global variables for this module
93 // ----------------------------------------------------------------------------
95 extern wxHashTable
*wxWidgetHashTable
;
96 static wxWindow
* g_captureWindow
= NULL
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 static void wxCanvasRepaintProc(Widget
, XtPointer
, XmDrawingAreaCallbackStruct
* cbs
);
104 static void wxCanvasInputEvent(Widget drawingArea
, XtPointer data
, XmDrawingAreaCallbackStruct
* cbs
);
105 static void wxCanvasMotionEvent(Widget
, XButtonEvent
* event
);
106 static void wxCanvasEnterLeave(Widget drawingArea
, XtPointer clientData
, XCrossingEvent
* event
);
107 static void wxScrollBarCallback(Widget widget
, XtPointer clientData
,
108 XmScrollBarCallbackStruct
*cbs
);
109 static void wxPanelItemEventHandler(Widget wid
,
110 XtPointer client_data
,
112 Boolean
*continueToDispatch
);
117 // Helper function for 16-bit fonts
118 static int str16len(const char *s
)
122 while (s
[0] && s
[1]) {
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
137 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
138 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxWindow
, wxWindowBase
)
146 BEGIN_EVENT_TABLE(wxWindow
, wxWindowBase
)
147 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged
)
150 // ============================================================================
152 // ============================================================================
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
160 Widget w
= (Widget
)widget
;
168 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
170 Widget w
= (Widget
)widget
;
174 // Rationale: a lot of common operations (including but not
175 // limited to moving, resizing and appending items to a listbox)
176 // unmamange the widget, do their work, then manage it again.
177 // This means that, for example adding an item to a listbox will show it,
178 // or that most controls are shown every time they are moved or resized!
179 XtSetMappedWhenManaged( w
, domap
);
181 // if the widget is not unmanaged, it still intercepts
182 // mouse events, even if it is not mapped (and hence invisible)
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 void wxWindow::Init()
203 // generic initializations first
207 m_needsRefresh
= TRUE
;
208 m_mainWidget
= (WXWidget
) 0;
210 m_winCaptured
= FALSE
;
213 m_isBeingDeleted
= FALSE
;
219 m_drawingArea
= (WXWidget
) 0;
224 m_backingPixmap
= (WXPixmap
) 0;
235 // real construction (Init() must have been called before!)
236 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
240 const wxString
& name
)
242 wxCHECK_MSG( parent
, FALSE
, "can't create wxWindow without parent" );
244 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
246 parent
->AddChild(this);
248 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
249 m_foregroundColour
= *wxBLACK
;
251 //// TODO: we should probably optimize by only creating a
252 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
253 //// But for now, let's simplify things by always creating the
254 //// drawing area, since otherwise the translations are different.
256 // New translations for getting mouse motion feedback
257 static const String translations
=
258 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
259 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
260 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
261 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
262 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
263 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
264 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
265 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
266 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
267 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
268 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
269 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
270 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
271 <Key>: DrawingAreaInput()";
273 XtActionsRec actions
[1];
274 actions
[0].string
= "wxCanvasMotionEvent";
275 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
276 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
278 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
279 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
281 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
284 xmScrolledWindowWidgetClass
,
285 m_borderWidget
? (Widget
) m_borderWidget
287 XmNresizePolicy
, XmRESIZE_NONE
,
289 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
290 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
294 XtTranslations ptr
= XtParseTranslationTable(translations
);
295 m_drawingArea
= (WXWidget
)XtVaCreateWidget
298 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
299 XmNunitType
, XmPIXELS
,
300 // XmNresizePolicy, XmRESIZE_ANY,
301 XmNresizePolicy
, XmRESIZE_NONE
,
304 XmNtranslations
, ptr
,
307 XtFree((char *) ptr
);
310 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
312 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
313 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
314 XtFree ((char *) ptr
);
318 wxAddWindowToTable((Widget
) m_drawingArea
, this);
319 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
321 // This order is very important in Motif 1.2.1
322 XtRealizeWidget ((Widget
) m_scrolledWindow
);
323 XtRealizeWidget ((Widget
) m_drawingArea
);
324 XtManageChild ((Widget
) m_drawingArea
);
326 ptr
= XtParseTranslationTable("<Configure>: resize()");
327 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
328 XtFree ((char *) ptr
);
330 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
331 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
334 (Widget
)m_drawingArea
,
335 PointerMotionHintMask
| EnterWindowMask
|
336 LeaveWindowMask
| FocusChangeMask
,
338 (XtEventHandler
) wxCanvasEnterLeave
,
342 // Scrolled widget needs to have its colour changed or we get a little blue
343 // square where the scrollbars abutt
344 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
345 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, TRUE
);
346 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, TRUE
);
348 XmScrolledWindowSetAreas(
349 (Widget
)m_scrolledWindow
,
350 (Widget
) 0, (Widget
) 0,
351 (Widget
) m_drawingArea
);
353 // Without this, the cursor may not be restored properly (e.g. in splitter
355 SetCursor(*wxSTANDARD_CURSOR
);
356 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
357 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, TRUE
);
362 wxWindow::~wxWindow()
364 if (g_captureWindow
== this)
365 g_captureWindow
= NULL
;
367 m_isBeingDeleted
= TRUE
;
369 // Motif-specific actions first
370 WXWidget wMain
= GetMainWidget();
373 // Removes event handlers
377 // If m_drawingArea, we're a fully-fledged window with drawing area,
378 // scrollbars etc. (what wxCanvas used to be)
381 // Destroy children before destroying self
385 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
387 Widget w
= (Widget
) m_drawingArea
;
388 wxDeleteWindowFromTable(w
);
393 m_drawingArea
= (WXWidget
) 0;
396 // Only if we're _really_ a canvas (not a dialog box/panel)
397 if (m_scrolledWindow
)
399 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
404 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
405 XtUnmanageChild((Widget
) m_hScrollBar
);
409 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
410 XtUnmanageChild((Widget
) m_vScrollBar
);
414 XtDestroyWidget((Widget
) m_hScrollBar
);
416 XtDestroyWidget((Widget
) m_vScrollBar
);
418 UnmanageAndDestroy(m_scrolledWindow
);
422 XtDestroyWidget ((Widget
) m_borderWidget
);
423 m_borderWidget
= (WXWidget
) 0;
426 else // Why wasn't this here before? JACS 8/3/2000
430 // Destroy the window
433 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
434 // Child widgets will get destroyed automatically when a frame
435 // or dialog is destroyed, but before that you may get some memory
436 // leaks and potential layout problems if you delete and then add
439 // GRG, Feb/2000: commented this out when adding support for
440 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
441 // that this call crashed wxMotif under OS/2, so it seems
442 // that leaving it out is the right thing to do.
443 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
444 XtDestroyWidget((Widget
) GetMainWidget());
445 SetMainWidget((WXWidget
) NULL
);
449 // ----------------------------------------------------------------------------
450 // scrollbar management
451 // ----------------------------------------------------------------------------
453 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
454 wxOrientation orientation
,
457 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
459 XtVaCreateManagedWidget( "scrollBarWidget",
460 xmScrollBarWidgetClass
, (Widget
)parent
,
461 XmNorientation
, orient
,
466 XtPointer o
= (XtPointer
)orientation
;
467 XtCallbackProc cb
= (XtCallbackProc
)callback
;
469 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
470 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
471 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
472 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
473 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
474 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
475 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
476 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
482 void wxWindow::CreateScrollbar(wxOrientation orientation
)
484 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
486 XtVaSetValues( (Widget
) m_scrolledWindow
,
487 XmNresizePolicy
, XmRESIZE_NONE
,
490 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
491 // Add scrollbars if required
492 if (orientation
== wxHORIZONTAL
)
494 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
495 (void (*)())wxScrollBarCallback
);
497 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, TRUE
);
499 XtRealizeWidget( (Widget
)m_hScrollBar
);
501 XtVaSetValues((Widget
) m_scrolledWindow
,
502 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
505 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
507 else if (orientation
== wxVERTICAL
)
509 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
510 (void (*)())wxScrollBarCallback
);
512 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, TRUE
);
514 XtRealizeWidget((Widget
)m_vScrollBar
);
516 XtVaSetValues((Widget
) m_scrolledWindow
,
517 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
520 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
523 XtVaSetValues( (Widget
) m_scrolledWindow
,
524 XmNresizePolicy
, XmRESIZE_ANY
,
528 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
530 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
532 XtVaSetValues((Widget
) m_scrolledWindow
,
533 XmNresizePolicy
, XmRESIZE_NONE
,
535 String stringSB
= orientation
== wxHORIZONTAL
?
536 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
537 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
538 &m_hScrollBar
: &m_vScrollBar
;
542 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
543 XtDestroyWidget( (Widget
)*widgetSB
);
544 *widgetSB
= (WXWidget
)NULL
;
547 XtVaSetValues( (Widget
)m_scrolledWindow
,
548 stringSB
, (Widget
) 0,
551 XtVaSetValues((Widget
) m_scrolledWindow
,
552 XmNresizePolicy
, XmRESIZE_ANY
,
556 // ---------------------------------------------------------------------------
558 // ---------------------------------------------------------------------------
560 void wxWindow::SetFocus()
562 Widget wMain
= (Widget
) GetMainWidget();
563 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
564 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
567 // Get the window with the focus
568 wxWindow
*wxWindowBase::FindFocus()
571 // (1) Can there be multiple focussed widgets in an application?
572 // In which case we need to find the top-level window that's
574 // (2) The widget with the focus may not be in the widget table
575 // depending on which widgets I put in the table
576 wxWindow
*winFocus
= (wxWindow
*)NULL
;
577 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
579 node
= node
->GetNext() )
581 wxWindow
*win
= node
->GetData();
583 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
585 if (w
!= (Widget
) NULL
)
587 winFocus
= wxGetWindowFromTable(w
);
596 bool wxWindow::Enable(bool enable
)
598 if ( !wxWindowBase::Enable(enable
) )
601 Widget wMain
= (Widget
)GetMainWidget();
604 XtSetSensitive(wMain
, enable
);
605 XmUpdateDisplay(wMain
);
611 bool wxWindow::Show(bool show
)
613 if ( !wxWindowBase::Show(show
) )
616 if (m_borderWidget
|| m_scrolledWindow
)
618 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
619 // MapOrUnmap(m_drawingArea, show);
623 if ( !MapOrUnmap(GetTopWidget(), show
) )
624 MapOrUnmap(GetMainWidget(), show
);
630 // Raise the window to the top of the Z order
631 void wxWindow::Raise()
633 Widget wTop
= (Widget
) GetTopWidget();
634 Window window
= XtWindow(wTop
);
635 XRaiseWindow(XtDisplay(wTop
), window
);
638 // Lower the window to the bottom of the Z order
639 void wxWindow::Lower()
641 Widget wTop
= (Widget
) GetTopWidget();
642 Window window
= XtWindow(wTop
);
643 XLowerWindow(XtDisplay(wTop
), window
);
646 void wxWindow::SetTitle(const wxString
& title
)
648 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
651 wxString
wxWindow::GetTitle() const
654 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
656 return wxString(title
);
659 void wxWindow::DoCaptureMouse()
661 g_captureWindow
= this;
665 Widget wMain
= (Widget
)GetMainWidget();
667 XtAddGrab(wMain
, TRUE
, FALSE
);
669 m_winCaptured
= TRUE
;
672 void wxWindow::DoReleaseMouse()
674 g_captureWindow
= NULL
;
675 if ( !m_winCaptured
)
678 Widget wMain
= (Widget
)GetMainWidget();
682 m_winCaptured
= FALSE
;
685 bool wxWindow::SetFont(const wxFont
& font
)
687 if ( !wxWindowBase::SetFont(font
) )
698 bool wxWindow::SetCursor(const wxCursor
& cursor
)
700 if ( !wxWindowBase::SetCursor(cursor
) )
706 // wxASSERT_MSG( m_cursor.Ok(),
707 // wxT("cursor must be valid after call to the base version"));
708 wxCursor
* cursor2
= NULL
;
710 cursor2
= & m_cursor
;
712 cursor2
= wxSTANDARD_CURSOR
;
714 WXDisplay
*dpy
= GetXDisplay();
715 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
717 Widget w
= (Widget
) GetMainWidget();
718 Window win
= XtWindow(w
);
719 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
724 // Coordinates relative to the window
725 void wxWindow::WarpPointer (int x
, int y
)
727 Widget wClient
= (Widget
)GetClientWidget();
729 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
732 // ---------------------------------------------------------------------------
734 // ---------------------------------------------------------------------------
736 int wxWindow::GetScrollPos(int orient
) const
738 if (orient
== wxHORIZONTAL
)
744 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
748 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
756 // This now returns the whole range, not just the number of positions that we
758 int wxWindow::GetScrollRange(int orient
) const
760 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
761 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
765 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
769 int wxWindow::GetScrollThumb(int orient
) const
771 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
772 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
775 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
779 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
781 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
785 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
788 SetInternalScrollPos((wxOrientation
)orient
, pos
);
791 // New function that will replace some of the above.
792 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
793 int range
, bool WXUNUSED(refresh
))
796 GetSize(& oldW
, & oldH
);
800 if (thumbVisible
== 0)
803 if (thumbVisible
> range
)
804 thumbVisible
= range
;
806 // Save the old state to see if it changed
807 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
809 if (orient
== wxHORIZONTAL
)
811 if (thumbVisible
== range
)
814 DestroyScrollbar(wxHORIZONTAL
);
819 CreateScrollbar(wxHORIZONTAL
);
822 if (orient
== wxVERTICAL
)
824 if (thumbVisible
== range
)
827 DestroyScrollbar(wxVERTICAL
);
832 CreateScrollbar(wxVERTICAL
);
835 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
837 if (oldScrollBar
!= newScrollBar
)
839 // This is important! Without it, scrollbars misbehave badly.
840 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
841 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
842 XtRealizeWidget((Widget
) m_scrolledWindow
);
843 XtManageChild((Widget
) m_scrolledWindow
);
848 XtVaSetValues((Widget
) newScrollBar
,
852 XmNsliderSize
, thumbVisible
,
856 SetInternalScrollPos((wxOrientation
)orient
, pos
);
859 GetSize(& newW
, & newH
);
861 // Adjusting scrollbars can resize the canvas accidentally
862 if (newW
!= oldW
|| newH
!= oldH
)
863 SetSize(-1, -1, oldW
, oldH
);
866 // Does a physical scroll
867 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
872 // Use specified rectangle
873 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
877 // Use whole client area
879 GetClientSize(& w
, & h
);
882 int x1
= (dx
>= 0) ? x
: x
- dx
;
883 int y1
= (dy
>= 0) ? y
: y
- dy
;
884 int w1
= w
- abs(dx
);
885 int h1
= h
- abs(dy
);
886 int x2
= (dx
>= 0) ? x
+ dx
: x
;
887 int y2
= (dy
>= 0) ? y
+ dy
: y
;
891 dc
.SetLogicalFunction (wxCOPY
);
893 Widget widget
= (Widget
) GetMainWidget();
894 Window window
= XtWindow(widget
);
895 Display
* display
= XtDisplay(widget
);
897 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
898 x1
, y1
, w1
, h1
, x2
, y2
);
900 dc
.SetAutoSetting(TRUE
);
901 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
902 dc
.SetBrush(brush
); // FIXME: needed?
904 wxWindowList::compatibility_iterator cnode
= m_children
.GetFirst();
907 wxWindow
*child
= cnode
->GetData();
910 child
->GetSize( &sx
, &sy
);
911 wxPoint
pos( child
->GetPosition() );
912 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
913 cnode
= cnode
->GetNext();
916 // We'll add rectangles to the list of update rectangles according to which
917 // bits we've exposed.
922 wxRect
*rect
= new wxRect
;
928 XFillRectangle(display
, window
,
929 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
933 rect
->width
= rect
->width
;
934 rect
->height
= rect
->height
;
936 updateRects
.Append((wxObject
*) rect
);
940 wxRect
*rect
= new wxRect
;
942 rect
->x
= x
+ w
+ dx
;
947 XFillRectangle(display
, window
,
948 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
953 rect
->width
= rect
->width
;
954 rect
->height
= rect
->height
;
956 updateRects
.Append((wxObject
*) rect
);
960 wxRect
*rect
= new wxRect
;
967 XFillRectangle(display
, window
,
968 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
972 rect
->width
= rect
->width
;
973 rect
->height
= rect
->height
;
975 updateRects
.Append((wxObject
*) rect
);
979 wxRect
*rect
= new wxRect
;
982 rect
->y
= y
+ h
+ dy
;
986 XFillRectangle(display
, window
,
987 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
991 rect
->width
= rect
->width
;
992 rect
->height
= rect
->height
;
994 updateRects
.Append((wxObject
*) rect
);
996 dc
.SetBrush(wxNullBrush
);
998 // Now send expose events
1000 wxList::compatibility_iterator node
= updateRects
.GetFirst();
1003 wxRect
* rect
= (wxRect
*) node
->GetData();
1006 event
.type
= Expose
;
1007 event
.display
= display
;
1008 event
.send_event
= True
;
1009 event
.window
= window
;
1013 event
.width
= rect
->width
;
1014 event
.height
= rect
->height
;
1018 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1020 node
= node
->GetNext();
1024 // Delete the update rects
1025 node
= updateRects
.GetFirst();
1028 wxRect
* rect
= (wxRect
*) node
->GetData();
1030 node
= node
->GetNext();
1033 XmUpdateDisplay((Widget
) GetMainWidget());
1036 // ---------------------------------------------------------------------------
1038 // ---------------------------------------------------------------------------
1040 #if wxUSE_DRAG_AND_DROP
1042 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1049 // Old style file-manager drag&drop
1050 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1055 // ----------------------------------------------------------------------------
1057 // ----------------------------------------------------------------------------
1061 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1066 #endif // wxUSE_TOOLTIPS
1068 // ----------------------------------------------------------------------------
1070 // ----------------------------------------------------------------------------
1074 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int 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
);
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());
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
);
1217 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1220 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1222 Widget widget
= (Widget
) GetClientWidget();
1223 Display
*display
= XtDisplay(widget
);
1224 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1225 Window thisWindow
= XtWindow(widget
);
1230 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1234 // Get size *available for subwindows* i.e. excluding menu bar etc.
1235 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1237 Widget widget
= (Widget
) GetClientWidget();
1239 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1240 if(x
) *x
= xx
; if(y
) *y
= yy
;
1243 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1245 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, FALSE
);
1248 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1249 int sizeFlags
, bool fromCtor
)
1251 // A bit of optimization to help sort out the flickers.
1252 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1255 GetSize(& oldW
, & oldH
);
1256 GetPosition(& oldX
, & oldY
);
1259 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1267 wxSize
size(-1, -1);
1270 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1272 size
= DoGetBestSize();
1283 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1285 if( size
.x
== -1 ) size
= DoGetBestSize();
1294 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1295 || !wxNoOptimize::CanOptimize() )
1301 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1304 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1308 flags
|= wxMOVE_WIDTH
;
1311 flags
|= wxMOVE_HEIGHT
;
1313 int xx
= x
; int yy
= y
;
1314 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1316 DoMoveWindow( xx
, yy
, width
, height
);
1318 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1323 Widget widget
= (Widget
) GetTopWidget();
1327 bool managed
= XtIsManaged( widget
);
1329 XtUnmanageChild(widget
);
1333 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1335 DoMoveWindow(xx
, yy
, width
, height
);
1338 XtManageChild(widget
);
1342 void wxWindow::DoSetClientSize(int width
, int height
)
1346 Widget drawingArea
= (Widget
) m_drawingArea
;
1348 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1351 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1353 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1355 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1359 Widget widget
= (Widget
) GetTopWidget();
1362 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1364 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1367 // For implementation purposes - sometimes decorations make the client area
1369 wxPoint
wxWindow::GetClientAreaOrigin() const
1371 return wxPoint(0, 0);
1374 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1379 Widget drawingArea
= (Widget
) m_drawingArea
;
1380 Widget borderOrScrolled
= m_borderWidget
?
1381 (Widget
) m_borderWidget
:
1382 (Widget
) m_scrolledWindow
;
1384 bool managed
= XtIsManaged(borderOrScrolled
);
1386 XtUnmanageChild (borderOrScrolled
);
1387 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1389 if (flags
& wxMOVE_X
)
1390 XtVaSetValues (borderOrScrolled
,
1393 if (flags
& wxMOVE_Y
)
1394 XtVaSetValues (borderOrScrolled
,
1398 if (flags
& wxMOVE_WIDTH
)
1402 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1403 short thick
, margin
;
1404 XtVaGetValues ((Widget
) m_borderWidget
,
1405 XmNshadowThickness
, &thick
,
1406 XmNmarginWidth
, &margin
,
1408 w
-= 2 * (thick
+ margin
);
1412 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1415 if (flags
& wxMOVE_HEIGHT
)
1419 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1420 short thick
, margin
;
1421 XtVaGetValues ((Widget
) m_borderWidget
,
1422 XmNshadowThickness
, &thick
,
1423 XmNmarginHeight
, &margin
,
1425 h
-= 2 * (thick
+ margin
);
1429 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1433 XtManageChild (borderOrScrolled
);
1434 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1441 XtVaSetValues((Widget
)GetTopWidget(),
1450 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1452 DoMoveWindowIntr (x
, y
, width
, height
,
1453 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1456 // ---------------------------------------------------------------------------
1458 // ---------------------------------------------------------------------------
1460 int wxWindow::GetCharHeight() const
1462 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1464 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1466 int direction
, ascent
, descent
;
1467 XCharStruct overall
;
1468 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1469 &descent
, &overall
);
1471 // return (overall.ascent + overall.descent);
1472 return (ascent
+ descent
);
1475 int wxWindow::GetCharWidth() const
1477 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1479 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1481 int direction
, ascent
, descent
;
1482 XCharStruct overall
;
1483 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1484 &descent
, &overall
);
1486 return overall
.width
;
1489 void wxWindow::GetTextExtent(const wxString
& string
,
1491 int *descent
, int *externalLeading
,
1492 const wxFont
*theFont
) const
1494 wxFont
*fontToUse
= (wxFont
*)theFont
;
1496 fontToUse
= (wxFont
*) & m_font
;
1498 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1500 WXFontStructPtr pFontStruct
= fontToUse
->GetFontStruct(1.0, GetXDisplay());
1502 int direction
, ascent
, descent2
;
1503 XCharStruct overall
;
1504 int slen
= string
.Len();
1508 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
1509 &ascent
, &descent2
, &overall
);
1512 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
1513 &direction
, &ascent
, &descent2
, &overall
);
1516 *x
= (overall
.width
);
1518 *y
= (ascent
+ descent2
);
1520 *descent
= descent2
;
1521 if (externalLeading
)
1522 *externalLeading
= 0;
1526 // ----------------------------------------------------------------------------
1528 // ----------------------------------------------------------------------------
1530 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1532 m_updateRegion
.Union( x
, y
, w
, h
);
1535 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1537 m_needsRefresh
= TRUE
;
1538 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1539 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1541 XExposeEvent dummyEvent
;
1543 GetSize(&width
, &height
);
1545 dummyEvent
.type
= Expose
;
1546 dummyEvent
.display
= display
;
1547 dummyEvent
.send_event
= True
;
1548 dummyEvent
.window
= thisWindow
;
1551 dummyEvent
.x
= rect
->x
;
1552 dummyEvent
.y
= rect
->y
;
1553 dummyEvent
.width
= rect
->width
;
1554 dummyEvent
.height
= rect
->height
;
1560 dummyEvent
.width
= width
;
1561 dummyEvent
.height
= height
;
1563 dummyEvent
.count
= 0;
1567 wxClientDC
dc(this);
1568 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1569 dc
.SetBackground(backgroundBrush
);
1576 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1579 void wxWindow::DoPaint()
1581 //TODO : make a temporary gc so we can do the XCopyArea below
1582 if (m_backingPixmap
&& !m_needsRefresh
)
1586 GC tempGC
= (GC
) dc
.GetBackingGC();
1588 Widget widget
= (Widget
) GetMainWidget();
1593 // We have to test whether it's a wxScrolledWindow (hack!) because
1594 // otherwise we don't know how many pixels have been scrolled. We might
1595 // solve this in the future by defining virtual wxWindow functions to get
1596 // the scroll position in pixels. Or, each kind of scrolled window has to
1597 // implement backing stores itself, using generic wxWindows code.
1598 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1599 if ( scrolledWindow
)
1602 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1608 // TODO: This could be optimized further by only copying the areas in the
1609 // current update region.
1611 // Only blit the part visible in the client area. The backing pixmap
1612 // always starts at 0, 0 but we may be looking at only a portion of it.
1613 wxSize clientArea
= GetClientSize();
1614 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1615 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1617 // Copy whichever is samller, the amount of pixmap we have to copy,
1618 // or the size of the client area.
1619 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1620 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1622 // Make sure we're not negative
1623 toBlitX
= wxMax(0, toBlitX
);
1624 toBlitY
= wxMax(0, toBlitY
);
1629 (Pixmap
) m_backingPixmap
,
1632 scrollPosX
, scrollPosY
, // Start at the scroll position
1633 toBlitX
, toBlitY
, // How much of the pixmap to copy
1639 wxWindowDC
dc(this);
1640 // Set an erase event first
1641 wxEraseEvent
eraseEvent(GetId(), &dc
);
1642 eraseEvent
.SetEventObject(this);
1643 GetEventHandler()->ProcessEvent(eraseEvent
);
1645 wxPaintEvent
event(GetId());
1646 event
.SetEventObject(this);
1647 GetEventHandler()->ProcessEvent(event
);
1649 m_needsRefresh
= FALSE
;
1653 // ----------------------------------------------------------------------------
1655 // ----------------------------------------------------------------------------
1657 // Responds to colour changes: passes event on to children.
1658 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1660 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1663 // Only propagate to non-top-level windows
1664 wxWindow
*win
= node
->GetData();
1665 if ( win
->GetParent() )
1667 wxSysColourChangedEvent event2
;
1668 event
.m_eventObject
= win
;
1669 win
->GetEventHandler()->ProcessEvent(event2
);
1672 node
= node
->GetNext();
1676 void wxWindow::OnInternalIdle()
1678 // This calls the UI-update mechanism (querying windows for
1679 // menu/toolbar/control state information)
1680 if (wxUpdateUIEvent::CanUpdate(this))
1681 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1684 // ----------------------------------------------------------------------------
1686 // ----------------------------------------------------------------------------
1688 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1691 if (!m_acceleratorTable
.Ok())
1694 int count
= m_acceleratorTable
.GetCount();
1695 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1697 for (i
= 0; i
< count
; i
++)
1699 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1700 if (entry
->MatchesEvent(event
))
1702 // Bingo, we have a match. Now find a control that matches the
1703 // entry command id.
1705 // Need to go up to the top of the window hierarchy, since it might
1706 // be e.g. a menu item
1707 wxWindow
* parent
= this;
1708 while ( parent
&& !parent
->IsTopLevel() )
1709 parent
= parent
->GetParent();
1714 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1718 // Try for a menu command
1719 if (frame
->GetMenuBar())
1721 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1724 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1725 commandEvent
.SetEventObject(frame
);
1727 // If ProcessEvent returns TRUE (it was handled), then
1728 // the calling code will skip the event handling.
1729 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1735 // Find a child matching the command id
1736 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1742 // Now we process those kinds of windows that we can.
1743 // For now, only buttons.
1744 if ( wxDynamicCast(child
, wxButton
) )
1746 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1747 commandEvent
.SetEventObject(child
);
1748 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1756 // We didn't match the key event against an accelerator.
1760 // ============================================================================
1761 // Motif-specific stuff from here on
1762 // ============================================================================
1764 // ----------------------------------------------------------------------------
1765 // function which maintain the global hash table mapping Widgets to wxWindows
1766 // ----------------------------------------------------------------------------
1768 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1770 wxWindow
*oldItem
= NULL
;
1771 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1773 wxLogDebug("Widget table clash: new widget is %ld, %s",
1774 (long)w
, win
->GetClassInfo()->GetClassName());
1778 wxWidgetHashTable
->Put((long) w
, win
);
1780 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1781 (WXWidget
)w
, win
, win
->GetClassInfo()->GetClassName());
1786 wxWindow
*wxGetWindowFromTable(Widget w
)
1788 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1791 void wxDeleteWindowFromTable(Widget w
)
1793 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1795 wxWidgetHashTable
->Delete((long)w
);
1798 // ----------------------------------------------------------------------------
1799 // add/remove window from the table
1800 // ----------------------------------------------------------------------------
1802 // Add to hash table, add event handler
1803 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1804 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1806 wxAddWindowToTable((Widget
) mainWidget
, this);
1807 XtAddEventHandler( (Widget
) mainWidget
,
1808 ButtonPressMask
| ButtonReleaseMask
1809 | PointerMotionMask
,
1811 wxPanelItemEventHandler
,
1817 XtOverrideTranslations ((Widget
) mainWidget
,
1818 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1819 XtFree ((char *) ptr
);
1822 // Some widgets have a parent form widget, e.g. wxRadioBox
1825 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1829 XtOverrideTranslations ((Widget
) formWidget
,
1830 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1831 XtFree ((char *) ptr
);
1838 SetSize (x
, y
, width
, height
);
1843 // Remove event handler, remove from hash table
1844 bool wxWindow::DetachWidget(WXWidget widget
)
1846 XtRemoveEventHandler( (Widget
) widget
,
1847 ButtonPressMask
| ButtonReleaseMask
1848 | PointerMotionMask
,
1850 wxPanelItemEventHandler
,
1853 wxDeleteWindowFromTable((Widget
) widget
);
1857 // ----------------------------------------------------------------------------
1858 // Motif-specific accessors
1859 // ----------------------------------------------------------------------------
1861 WXWindow
wxWindow::GetClientXWindow() const
1863 Widget wMain
= (Widget
)GetClientWidget();
1865 return (WXWindow
) XtWindow(wMain
);
1867 return (WXWindow
) 0;
1870 // Get the underlying X window
1871 WXWindow
wxWindow::GetXWindow() const
1873 Widget wMain
= (Widget
)GetMainWidget();
1875 return (WXWindow
) XtWindow(wMain
);
1877 return (WXWindow
) 0;
1880 // Get the underlying X display
1881 WXDisplay
*wxWindow::GetXDisplay() const
1883 Widget wMain
= (Widget
)GetMainWidget();
1885 return (WXDisplay
*) XtDisplay(wMain
);
1887 return (WXDisplay
*) NULL
;
1890 WXWidget
wxWindow::GetMainWidget() const
1893 return m_drawingArea
;
1895 return m_mainWidget
;
1898 WXWidget
wxWindow::GetClientWidget() const
1900 if (m_drawingArea
!= (WXWidget
) 0)
1901 return m_drawingArea
;
1903 return GetMainWidget();
1906 WXWidget
wxWindow::GetTopWidget() const
1908 return GetMainWidget();
1911 WXWidget
wxWindow::GetLabelWidget() const
1913 return GetMainWidget();
1916 // ----------------------------------------------------------------------------
1918 // ----------------------------------------------------------------------------
1920 // All widgets should have this as their resize proc.
1921 // OnSize sent to wxWindow via client data.
1922 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1923 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1925 wxWindow
*win
= wxGetWindowFromTable(w
);
1929 if (win
->PreResize())
1932 win
->GetSize(&width
, &height
);
1933 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1934 sizeEvent
.SetEventObject(win
);
1935 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1939 static void wxCanvasRepaintProc(Widget drawingArea
,
1940 XtPointer clientData
,
1941 XmDrawingAreaCallbackStruct
* cbs
)
1943 if (!wxGetWindowFromTable(drawingArea
))
1946 XEvent
* event
= cbs
->event
;
1947 wxWindow
* win
= (wxWindow
*) clientData
;
1949 switch (event
->type
)
1953 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1954 event
->xexpose
.width
, event
->xexpose
.height
);
1956 if (event
-> xexpose
.count
== 0)
1965 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1966 static void wxCanvasEnterLeave(Widget drawingArea
,
1967 XtPointer
WXUNUSED(clientData
),
1968 XCrossingEvent
* event
)
1970 XmDrawingAreaCallbackStruct cbs
;
1973 ((XCrossingEvent
&) ev
) = *event
;
1975 cbs
.reason
= XmCR_INPUT
;
1978 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1981 // Fix to make it work under Motif 1.0 (!)
1982 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1983 XButtonEvent
*WXUNUSED(event
))
1985 #if XmVersion <= 1000
1986 XmDrawingAreaCallbackStruct cbs
;
1989 ev
= *((XEvent
*) event
);
1990 cbs
.reason
= XmCR_INPUT
;
1993 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
1994 #endif // XmVersion <= 1000
1997 static void wxCanvasInputEvent(Widget drawingArea
,
1998 XtPointer
WXUNUSED(data
),
1999 XmDrawingAreaCallbackStruct
* cbs
)
2001 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
2007 if (cbs
->reason
!= XmCR_INPUT
)
2010 local_event
= *(cbs
->event
); // We must keep a copy!
2012 switch (local_event
.xany
.type
)
2020 wxMouseEvent wxevent
;
2021 if(wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, &local_event
))
2023 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
2029 wxKeyEvent
event (wxEVT_CHAR
);
2030 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2032 // Implement wxFrame::OnCharHook by checking ancestor.
2033 wxWindow
*parent
= canvas
;
2034 while (parent
&& !parent
->IsTopLevel())
2035 parent
= parent
->GetParent();
2039 event
.SetEventType(wxEVT_CHAR_HOOK
);
2040 if (parent
->GetEventHandler()->ProcessEvent(event
))
2044 // For simplicity, OnKeyDown is the same as OnChar
2045 // TODO: filter modifier key presses from OnChar
2046 event
.SetEventType(wxEVT_KEY_DOWN
);
2048 // Only process OnChar if OnKeyDown didn't swallow it
2049 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2051 event
.SetEventType(wxEVT_CHAR
);
2052 canvas
->GetEventHandler()->ProcessEvent (event
);
2059 wxKeyEvent
event (wxEVT_KEY_UP
);
2060 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2062 canvas
->GetEventHandler()->ProcessEvent (event
);
2068 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2070 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2071 event
.SetEventObject(canvas
);
2072 canvas
->GetEventHandler()->ProcessEvent(event
);
2078 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2080 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2081 event
.SetEventObject(canvas
);
2082 canvas
->GetEventHandler()->ProcessEvent(event
);
2091 static void wxPanelItemEventHandler(Widget wid
,
2092 XtPointer
WXUNUSED(client_data
),
2094 Boolean
*continueToDispatch
)
2096 // Widget can be a label or the actual widget.
2098 wxWindow
*window
= wxGetWindowFromTable(wid
);
2101 wxMouseEvent
wxevent(0);
2102 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2104 window
->GetEventHandler()->ProcessEvent(wxevent
);
2108 // TODO: probably the key to allowing default behaviour to happen. Say we
2109 // set a m_doDefault flag to FALSE at the start of this function. Then in
2110 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2111 // TRUE, indicating that default processing can happen. Thus, behaviour can
2112 // appear to be overridden just by adding an event handler and not calling
2113 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2114 // way of handling drawing area events, to simplify things.
2115 *continueToDispatch
= True
;
2118 static void wxScrollBarCallback(Widget scrollbar
,
2119 XtPointer clientData
,
2120 XmScrollBarCallbackStruct
*cbs
)
2122 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2123 wxOrientation orientation
= (wxOrientation
)(int)clientData
;
2125 wxEventType eventType
= wxEVT_NULL
;
2126 switch (cbs
->reason
)
2128 case XmCR_INCREMENT
:
2130 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2133 case XmCR_DECREMENT
:
2135 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2140 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2143 case XmCR_VALUE_CHANGED
:
2145 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2148 case XmCR_PAGE_INCREMENT
:
2150 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2153 case XmCR_PAGE_DECREMENT
:
2155 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2160 eventType
= wxEVT_SCROLLWIN_TOP
;
2163 case XmCR_TO_BOTTOM
:
2165 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2170 // Should never get here
2171 wxFAIL_MSG("Unknown scroll event.");
2176 wxScrollWinEvent
event(eventType
,
2179 event
.SetEventObject( win
);
2180 win
->GetEventHandler()->ProcessEvent(event
);
2183 // For repainting arbitrary windows
2184 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2189 wxWindow
* win
= wxGetWindowFromTable(w
);
2193 switch(event
-> type
)
2197 window
= (Window
) win
-> GetXWindow();
2198 display
= (Display
*) win
-> GetXDisplay();
2200 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2201 event
->xexpose
.width
, event
->xexpose
.height
);
2203 if (event
-> xexpose
.count
== 0)
2213 // ----------------------------------------------------------------------------
2214 // TranslateXXXEvent() functions
2215 // ----------------------------------------------------------------------------
2217 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2218 Widget widget
, XEvent
*xevent
)
2220 switch (xevent
->xany
.type
)
2225 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2226 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2227 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2233 wxEventType eventType
= wxEVT_NULL
;
2235 if (xevent
->xany
.type
== LeaveNotify
)
2237 eventType
= wxEVT_LEAVE_WINDOW
;
2239 if (xevent
->xany
.type
== EnterNotify
)
2241 eventType
= wxEVT_ENTER_WINDOW
;
2243 else if (xevent
->xany
.type
== MotionNotify
)
2245 eventType
= wxEVT_MOTION
;
2247 else if (xevent
->xany
.type
== ButtonPress
)
2249 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2251 if (xevent
->xbutton
.button
== Button1
)
2253 eventType
= wxEVT_LEFT_DOWN
;
2256 else if (xevent
->xbutton
.button
== Button2
)
2258 eventType
= wxEVT_MIDDLE_DOWN
;
2261 else if (xevent
->xbutton
.button
== Button3
)
2263 eventType
= wxEVT_RIGHT_DOWN
;
2267 // check for a double click
2269 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2270 long ts
= wxevent
.GetTimestamp();
2272 int buttonLast
= win
->GetLastClickedButton();
2273 long lastTS
= win
->GetLastClickTime();
2274 if ( buttonLast
&& buttonLast
== button
&&
2275 (ts
- lastTS
) < dclickTime
)
2278 win
->SetLastClick(0, ts
);
2279 if ( eventType
== wxEVT_LEFT_DOWN
)
2280 eventType
= wxEVT_LEFT_DCLICK
;
2281 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2282 eventType
= wxEVT_MIDDLE_DCLICK
;
2283 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2284 eventType
= wxEVT_RIGHT_DCLICK
;
2288 // not fast enough or different button
2289 win
->SetLastClick(button
, ts
);
2292 else if (xevent
->xany
.type
== ButtonRelease
)
2294 if (xevent
->xbutton
.button
== Button1
)
2296 eventType
= wxEVT_LEFT_UP
;
2298 else if (xevent
->xbutton
.button
== Button2
)
2300 eventType
= wxEVT_MIDDLE_UP
;
2302 else if (xevent
->xbutton
.button
== Button3
)
2304 eventType
= wxEVT_RIGHT_UP
;
2314 wxevent
.SetEventType(eventType
);
2317 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2320 win
->GetPosition(&x2
, &y2
);
2322 // The button x/y must be translated to wxWindows
2323 // window space - the widget might be a label or button,
2327 if (widget
!= (Widget
)win
->GetMainWidget())
2333 wxevent
.m_x
= xevent
->xbutton
.x
+ dx
;
2334 wxevent
.m_y
= xevent
->xbutton
.y
+ dy
;
2336 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2337 || (event_left_is_down (xevent
)
2338 && (eventType
!= wxEVT_LEFT_UP
)));
2339 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2340 || (event_middle_is_down (xevent
)
2341 && (eventType
!= wxEVT_MIDDLE_UP
)));
2342 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2343 || (event_right_is_down (xevent
)
2344 && (eventType
!= wxEVT_RIGHT_UP
)));
2346 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2347 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2348 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2349 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2351 wxevent
.SetId(win
->GetId());
2352 wxevent
.SetEventObject(win
);
2360 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2361 Widget
WXUNUSED(widget
), XEvent
*xevent
)
2363 switch (xevent
->xany
.type
)
2371 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2372 int id
= wxCharCodeXToWX (keySym
);
2373 // id may be WXK_xxx code - these are outside ASCII range, so we
2374 // can't just use toupper() on id
2375 if (id
>= 'a' && id
<= 'z')
2378 if (xevent
->xkey
.state
& ShiftMask
)
2379 wxevent
.m_shiftDown
= TRUE
;
2380 if (xevent
->xkey
.state
& ControlMask
)
2381 wxevent
.m_controlDown
= TRUE
;
2382 if (xevent
->xkey
.state
& Mod3Mask
)
2383 wxevent
.m_altDown
= TRUE
;
2384 if (xevent
->xkey
.state
& Mod1Mask
)
2385 wxevent
.m_metaDown
= TRUE
;
2386 wxevent
.SetEventObject(win
);
2387 wxevent
.m_keyCode
= id
;
2388 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2390 wxevent
.m_x
= xevent
->xbutton
.x
;
2391 wxevent
.m_y
= xevent
->xbutton
.y
;
2405 // ----------------------------------------------------------------------------
2407 // ----------------------------------------------------------------------------
2409 #define YAllocColor XAllocColor
2410 XColor g_itemColors
[5];
2411 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2414 static XmColorProc colorProc
;
2416 result
= wxNO_COLORS
;
2420 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2421 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2422 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2423 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2424 if (colorProc
== (XmColorProc
) NULL
)
2426 // Get a ptr to the actual function
2427 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2428 // And set it back to motif.
2429 XmSetColorCalculation (colorProc
);
2431 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2432 &g_itemColors
[wxFORE_INDEX
],
2433 &g_itemColors
[wxSELE_INDEX
],
2434 &g_itemColors
[wxTOPS_INDEX
],
2435 &g_itemColors
[wxBOTS_INDEX
]);
2436 result
= wxBACK_COLORS
;
2440 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2441 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2442 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2443 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2444 if (result
== wxNO_COLORS
)
2445 result
= wxFORE_COLORS
;
2448 Display
*dpy
= display
;
2449 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2453 /* 5 Colours to allocate */
2454 for (int i
= 0; i
< 5; i
++)
2455 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2456 result
= wxNO_COLORS
;
2460 /* Only 1 colour to allocate */
2461 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2462 result
= wxNO_COLORS
;
2468 // Changes the foreground and background colours to be derived from the current
2469 // background colour. To change the foreground colour, you must call
2470 // SetForegroundColour explicitly.
2471 void wxWindow::ChangeBackgroundColour()
2473 WXWidget mainWidget
= GetMainWidget();
2475 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2478 void wxWindow::ChangeForegroundColour()
2480 WXWidget mainWidget
= GetMainWidget();
2482 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2483 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2484 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2487 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2489 if ( !wxWindowBase::SetBackgroundColour(col
) )
2492 ChangeBackgroundColour();
2497 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2499 if ( !wxWindowBase::SetForegroundColour(col
) )
2502 ChangeForegroundColour();
2507 void wxWindow::ChangeFont(bool keepOriginalSize
)
2509 // Note that this causes the widget to be resized back
2510 // to its original size! We therefore have to set the size
2511 // back again. TODO: a better way in Motif?
2512 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2513 if (w
&& m_font
.Ok())
2515 int width
, height
, width1
, height1
;
2516 GetSize(& width
, & height
);
2518 wxDoChangeFont( GetLabelWidget(), m_font
);
2520 GetSize(& width1
, & height1
);
2521 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2523 SetSize(-1, -1, width
, height
);
2528 // ----------------------------------------------------------------------------
2530 // ----------------------------------------------------------------------------
2532 wxWindow
*wxGetActiveWindow()
2535 wxFAIL_MSG("Not implemented");
2540 wxWindow
*wxWindowBase::GetCapture()
2542 return (wxWindow
*)g_captureWindow
;
2546 // Find the wxWindow at the current mouse position, returning the mouse
2548 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2550 return wxFindWindowAtPoint(wxGetMousePosition());
2553 // Get the current mouse position.
2554 wxPoint
wxGetMousePosition()
2556 Display
*display
= wxGlobalDisplay();
2557 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2558 Window rootReturn
, childReturn
;
2559 int rootX
, rootY
, winX
, winY
;
2560 unsigned int maskReturn
;
2562 XQueryPointer (display
,
2566 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2567 return wxPoint(rootX
, rootY
);
2571 // ----------------------------------------------------------------------------
2572 // wxNoOptimize: switch off size optimization
2573 // ----------------------------------------------------------------------------
2575 int wxNoOptimize::ms_count
= 0;