1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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
378 m_parent
->RemoveChild( this );
380 // If m_drawingArea, we're a fully-fledged window with drawing area,
381 // scrollbars etc. (what wxCanvas used to be)
384 // Destroy children before destroying self
388 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
390 Widget w
= (Widget
) m_drawingArea
;
391 wxDeleteWindowFromTable(w
);
396 m_drawingArea
= (WXWidget
) 0;
399 // Only if we're _really_ a canvas (not a dialog box/panel)
400 if (m_scrolledWindow
)
402 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
407 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
408 XtUnmanageChild((Widget
) m_hScrollBar
);
412 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
413 XtUnmanageChild((Widget
) m_vScrollBar
);
417 XtDestroyWidget((Widget
) m_hScrollBar
);
419 XtDestroyWidget((Widget
) m_vScrollBar
);
421 UnmanageAndDestroy(m_scrolledWindow
);
425 XtDestroyWidget ((Widget
) m_borderWidget
);
426 m_borderWidget
= (WXWidget
) 0;
429 else // Why wasn't this here before? JACS 8/3/2000
433 // Destroy the window
436 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
437 // Child widgets will get destroyed automatically when a frame
438 // or dialog is destroyed, but before that you may get some memory
439 // leaks and potential layout problems if you delete and then add
442 // GRG, Feb/2000: commented this out when adding support for
443 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
444 // that this call crashed wxMotif under OS/2, so it seems
445 // that leaving it out is the right thing to do.
446 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
447 XtDestroyWidget((Widget
) GetMainWidget());
448 SetMainWidget((WXWidget
) NULL
);
452 // ----------------------------------------------------------------------------
453 // scrollbar management
454 // ----------------------------------------------------------------------------
456 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
457 wxOrientation orientation
,
460 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
462 XtVaCreateManagedWidget( "scrollBarWidget",
463 xmScrollBarWidgetClass
, (Widget
)parent
,
464 XmNorientation
, orient
,
469 XtPointer o
= (XtPointer
)orientation
;
470 XtCallbackProc cb
= (XtCallbackProc
)callback
;
472 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
473 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
474 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
475 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
476 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
477 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
478 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
479 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
485 void wxWindow::CreateScrollbar(wxOrientation orientation
)
487 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
489 XtVaSetValues( (Widget
) m_scrolledWindow
,
490 XmNresizePolicy
, XmRESIZE_NONE
,
493 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
494 // Add scrollbars if required
495 if (orientation
== wxHORIZONTAL
)
497 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
498 (void (*)())wxScrollBarCallback
);
500 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, TRUE
);
502 XtRealizeWidget( (Widget
)m_hScrollBar
);
504 XtVaSetValues((Widget
) m_scrolledWindow
,
505 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
508 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
510 else if (orientation
== wxVERTICAL
)
512 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
513 (void (*)())wxScrollBarCallback
);
515 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, TRUE
);
517 XtRealizeWidget((Widget
)m_vScrollBar
);
519 XtVaSetValues((Widget
) m_scrolledWindow
,
520 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
523 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
526 XtVaSetValues( (Widget
) m_scrolledWindow
,
527 XmNresizePolicy
, XmRESIZE_ANY
,
531 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
533 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
535 XtVaSetValues((Widget
) m_scrolledWindow
,
536 XmNresizePolicy
, XmRESIZE_NONE
,
538 String stringSB
= orientation
== wxHORIZONTAL
?
539 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
540 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
541 &m_hScrollBar
: &m_vScrollBar
;
545 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
546 XtDestroyWidget( (Widget
)*widgetSB
);
547 *widgetSB
= (WXWidget
)NULL
;
550 XtVaSetValues( (Widget
)m_scrolledWindow
,
551 stringSB
, (Widget
) 0,
554 XtVaSetValues((Widget
) m_scrolledWindow
,
555 XmNresizePolicy
, XmRESIZE_ANY
,
559 // ---------------------------------------------------------------------------
561 // ---------------------------------------------------------------------------
563 void wxWindow::SetFocus()
565 Widget wMain
= (Widget
) GetMainWidget();
566 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
567 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
570 // Get the window with the focus
571 wxWindow
*wxWindowBase::FindFocus()
574 // (1) Can there be multiple focussed widgets in an application?
575 // In which case we need to find the top-level window that's
577 // (2) The widget with the focus may not be in the widget table
578 // depending on which widgets I put in the table
579 wxWindow
*winFocus
= (wxWindow
*)NULL
;
580 for ( wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst();
582 node
= node
->GetNext() )
584 wxWindow
*win
= node
->GetData();
586 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
588 if (w
!= (Widget
) NULL
)
590 winFocus
= wxGetWindowFromTable(w
);
599 bool wxWindow::Enable(bool enable
)
601 if ( !wxWindowBase::Enable(enable
) )
604 Widget wMain
= (Widget
)GetMainWidget();
607 XtSetSensitive(wMain
, enable
);
608 XmUpdateDisplay(wMain
);
614 bool wxWindow::Show(bool show
)
616 if ( !wxWindowBase::Show(show
) )
619 if (m_borderWidget
|| m_scrolledWindow
)
621 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
622 // MapOrUnmap(m_drawingArea, show);
626 if ( !MapOrUnmap(GetTopWidget(), show
) )
627 MapOrUnmap(GetMainWidget(), show
);
633 // Raise the window to the top of the Z order
634 void wxWindow::Raise()
636 Widget wTop
= (Widget
) GetTopWidget();
637 Window window
= XtWindow(wTop
);
638 XRaiseWindow(XtDisplay(wTop
), window
);
641 // Lower the window to the bottom of the Z order
642 void wxWindow::Lower()
644 Widget wTop
= (Widget
) GetTopWidget();
645 Window window
= XtWindow(wTop
);
646 XLowerWindow(XtDisplay(wTop
), window
);
649 void wxWindow::SetTitle(const wxString
& title
)
651 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
654 wxString
wxWindow::GetTitle() const
657 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
659 return wxString(title
);
662 void wxWindow::DoCaptureMouse()
664 g_captureWindow
= this;
668 Widget wMain
= (Widget
)GetMainWidget();
670 XtAddGrab(wMain
, TRUE
, FALSE
);
672 m_winCaptured
= TRUE
;
675 void wxWindow::DoReleaseMouse()
677 g_captureWindow
= NULL
;
678 if ( !m_winCaptured
)
681 Widget wMain
= (Widget
)GetMainWidget();
685 m_winCaptured
= FALSE
;
688 bool wxWindow::SetFont(const wxFont
& font
)
690 if ( !wxWindowBase::SetFont(font
) )
701 bool wxWindow::SetCursor(const wxCursor
& cursor
)
703 if ( !wxWindowBase::SetCursor(cursor
) )
709 // wxASSERT_MSG( m_cursor.Ok(),
710 // wxT("cursor must be valid after call to the base version"));
711 wxCursor
* cursor2
= NULL
;
713 cursor2
= & m_cursor
;
715 cursor2
= wxSTANDARD_CURSOR
;
717 WXDisplay
*dpy
= GetXDisplay();
718 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
720 Widget w
= (Widget
) GetMainWidget();
721 Window win
= XtWindow(w
);
722 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
727 // Coordinates relative to the window
728 void wxWindow::WarpPointer (int x
, int y
)
730 Widget wClient
= (Widget
)GetClientWidget();
732 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
735 // ---------------------------------------------------------------------------
737 // ---------------------------------------------------------------------------
739 int wxWindow::GetScrollPos(int orient
) const
741 if (orient
== wxHORIZONTAL
)
747 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
751 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
759 // This now returns the whole range, not just the number of positions that we
761 int wxWindow::GetScrollRange(int orient
) const
763 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
764 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
768 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
772 int wxWindow::GetScrollThumb(int orient
) const
774 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
775 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
778 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
782 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
784 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
788 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
791 SetInternalScrollPos((wxOrientation
)orient
, pos
);
794 // New function that will replace some of the above.
795 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
796 int range
, bool WXUNUSED(refresh
))
799 GetSize(& oldW
, & oldH
);
803 if (thumbVisible
== 0)
806 if (thumbVisible
> range
)
807 thumbVisible
= range
;
809 // Save the old state to see if it changed
810 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
812 if (orient
== wxHORIZONTAL
)
814 if (thumbVisible
== range
)
817 DestroyScrollbar(wxHORIZONTAL
);
822 CreateScrollbar(wxHORIZONTAL
);
825 if (orient
== wxVERTICAL
)
827 if (thumbVisible
== range
)
830 DestroyScrollbar(wxVERTICAL
);
835 CreateScrollbar(wxVERTICAL
);
838 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
840 if (oldScrollBar
!= newScrollBar
)
842 // This is important! Without it, scrollbars misbehave badly.
843 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
844 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
845 XtRealizeWidget((Widget
) m_scrolledWindow
);
846 XtManageChild((Widget
) m_scrolledWindow
);
851 XtVaSetValues((Widget
) newScrollBar
,
855 XmNsliderSize
, thumbVisible
,
859 SetInternalScrollPos((wxOrientation
)orient
, pos
);
862 GetSize(& newW
, & newH
);
864 // Adjusting scrollbars can resize the canvas accidentally
865 if (newW
!= oldW
|| newH
!= oldH
)
866 SetSize(-1, -1, oldW
, oldH
);
869 // Does a physical scroll
870 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
875 // Use specified rectangle
876 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
880 // Use whole client area
882 GetClientSize(& w
, & h
);
885 int x1
= (dx
>= 0) ? x
: x
- dx
;
886 int y1
= (dy
>= 0) ? y
: y
- dy
;
887 int w1
= w
- abs(dx
);
888 int h1
= h
- abs(dy
);
889 int x2
= (dx
>= 0) ? x
+ dx
: x
;
890 int y2
= (dy
>= 0) ? y
+ dy
: y
;
894 dc
.SetLogicalFunction (wxCOPY
);
896 Widget widget
= (Widget
) GetMainWidget();
897 Window window
= XtWindow(widget
);
898 Display
* display
= XtDisplay(widget
);
900 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
901 x1
, y1
, w1
, h1
, x2
, y2
);
903 dc
.SetAutoSetting(TRUE
);
904 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
905 dc
.SetBrush(brush
); // FIXME: needed?
907 wxWindowList::compatibility_iterator cnode
= m_children
.GetFirst();
910 wxWindow
*child
= cnode
->GetData();
913 child
->GetSize( &sx
, &sy
);
914 wxPoint
pos( child
->GetPosition() );
915 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
916 cnode
= cnode
->GetNext();
919 // We'll add rectangles to the list of update rectangles according to which
920 // bits we've exposed.
925 wxRect
*rect
= new wxRect
;
931 XFillRectangle(display
, window
,
932 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
936 rect
->width
= rect
->width
;
937 rect
->height
= rect
->height
;
939 updateRects
.Append((wxObject
*) rect
);
943 wxRect
*rect
= new wxRect
;
945 rect
->x
= x
+ w
+ dx
;
950 XFillRectangle(display
, window
,
951 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
956 rect
->width
= rect
->width
;
957 rect
->height
= rect
->height
;
959 updateRects
.Append((wxObject
*) rect
);
963 wxRect
*rect
= new wxRect
;
970 XFillRectangle(display
, window
,
971 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
975 rect
->width
= rect
->width
;
976 rect
->height
= rect
->height
;
978 updateRects
.Append((wxObject
*) rect
);
982 wxRect
*rect
= new wxRect
;
985 rect
->y
= y
+ h
+ dy
;
989 XFillRectangle(display
, window
,
990 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
994 rect
->width
= rect
->width
;
995 rect
->height
= rect
->height
;
997 updateRects
.Append((wxObject
*) rect
);
999 dc
.SetBrush(wxNullBrush
);
1001 // Now send expose events
1003 wxList::compatibility_iterator node
= updateRects
.GetFirst();
1006 wxRect
* rect
= (wxRect
*) node
->GetData();
1009 event
.type
= Expose
;
1010 event
.display
= display
;
1011 event
.send_event
= True
;
1012 event
.window
= window
;
1016 event
.width
= rect
->width
;
1017 event
.height
= rect
->height
;
1021 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1023 node
= node
->GetNext();
1027 // Delete the update rects
1028 node
= updateRects
.GetFirst();
1031 wxRect
* rect
= (wxRect
*) node
->GetData();
1033 node
= node
->GetNext();
1036 XmUpdateDisplay((Widget
) GetMainWidget());
1039 // ---------------------------------------------------------------------------
1041 // ---------------------------------------------------------------------------
1043 #if wxUSE_DRAG_AND_DROP
1045 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1052 // Old style file-manager drag&drop
1053 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1058 // ----------------------------------------------------------------------------
1060 // ----------------------------------------------------------------------------
1064 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1069 #endif // wxUSE_TOOLTIPS
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1077 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
1079 Widget widget
= (Widget
) GetMainWidget();
1081 /* The menuId field seems to be usused, so we'll use it to
1082 indicate whether a menu is popped up or not:
1083 0: Not currently created as a popup
1084 -1: Created as a popup, but not active
1088 if (menu
->GetParent() && (menu
->GetId() != -1))
1091 if (menu
->GetMainWidget())
1093 menu
->DestroyMenu(TRUE
);
1096 menu
->SetId(1); /* Mark as popped-up */
1097 menu
->CreateMenu(NULL
, widget
, menu
);
1098 menu
->SetInvokingWindow(this);
1102 // menu->SetParent(parent);
1103 // parent->children->Append(menu); // Store menu for later deletion
1105 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1113 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1115 wxCanvas *canvas = (wxCanvas *) this;
1116 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1117 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1121 Display
*display
= XtDisplay (widget
);
1122 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1123 Window thisWindow
= XtWindow (widget
);
1125 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1126 &rootX
, &rootY
, &childWindow
);
1128 XButtonPressedEvent event
;
1129 event
.type
= ButtonPress
;
1135 event
.x_root
= rootX
;
1136 event
.y_root
= rootY
;
1138 XmMenuPosition (menuWidget
, &event
);
1139 XtManageChild (menuWidget
);
1141 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1142 // idle-time destroy routine.
1143 // Waiting until this ID changes causes this function to block until
1144 // the menu has been dismissed and the widgets cleaned up.
1145 // In other words, once this routine returns, it is safe to delete
1147 // Ian Brown <ian.brown@printsoft.de>
1149 wxEventLoop evtLoop
;
1151 while (menu
->GetId() == 1)
1153 wxDoEventLoopIteration( evtLoop
);
1161 // ---------------------------------------------------------------------------
1162 // moving and resizing
1163 // ---------------------------------------------------------------------------
1165 bool wxWindow::PreResize()
1171 void wxWindow::DoGetSize(int *x
, int *y
) const
1173 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1174 ( m_borderWidget
? m_borderWidget
:
1175 m_scrolledWindow
? m_scrolledWindow
:
1179 XtVaGetValues( widget
,
1187 void wxWindow::DoGetPosition(int *x
, int *y
) const
1189 Widget widget
= (Widget
)
1191 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1195 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1197 // We may be faking the client origin. So a window that's really at (0, 30)
1198 // may appear (to wxWin apps) to be at (0, 0).
1201 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1210 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1212 Widget widget
= (Widget
) GetClientWidget();
1213 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1214 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1215 Window thisWindow
= XtWindow(widget
);
1220 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1223 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1225 Widget widget
= (Widget
) GetClientWidget();
1226 Display
*display
= XtDisplay(widget
);
1227 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1228 Window thisWindow
= XtWindow(widget
);
1233 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1237 // Get size *available for subwindows* i.e. excluding menu bar etc.
1238 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1240 Widget widget
= (Widget
) GetClientWidget();
1242 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1243 if(x
) *x
= xx
; if(y
) *y
= yy
;
1246 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1248 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, FALSE
);
1251 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1252 int sizeFlags
, bool fromCtor
)
1254 // A bit of optimization to help sort out the flickers.
1255 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1258 GetSize(& oldW
, & oldH
);
1259 GetPosition(& oldX
, & oldY
);
1262 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1270 wxSize
size(-1, -1);
1273 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1275 size
= DoGetBestSize();
1286 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1288 if( size
.x
== -1 ) size
= DoGetBestSize();
1297 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1298 || !wxNoOptimize::CanOptimize() )
1304 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1307 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1311 flags
|= wxMOVE_WIDTH
;
1314 flags
|= wxMOVE_HEIGHT
;
1316 int xx
= x
; int yy
= y
;
1317 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1319 DoMoveWindow( xx
, yy
, width
, height
);
1321 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1326 Widget widget
= (Widget
) GetTopWidget();
1330 bool managed
= XtIsManaged( widget
);
1332 XtUnmanageChild(widget
);
1336 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1338 DoMoveWindow(xx
, yy
, width
, height
);
1341 XtManageChild(widget
);
1345 void wxWindow::DoSetClientSize(int width
, int height
)
1349 Widget drawingArea
= (Widget
) m_drawingArea
;
1351 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1354 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1356 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1358 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1362 Widget widget
= (Widget
) GetTopWidget();
1365 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1367 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1370 // For implementation purposes - sometimes decorations make the client area
1372 wxPoint
wxWindow::GetClientAreaOrigin() const
1374 return wxPoint(0, 0);
1377 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1382 Widget drawingArea
= (Widget
) m_drawingArea
;
1383 Widget borderOrScrolled
= m_borderWidget
?
1384 (Widget
) m_borderWidget
:
1385 (Widget
) m_scrolledWindow
;
1387 bool managed
= XtIsManaged(borderOrScrolled
);
1389 XtUnmanageChild (borderOrScrolled
);
1390 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1392 if (flags
& wxMOVE_X
)
1393 XtVaSetValues (borderOrScrolled
,
1396 if (flags
& wxMOVE_Y
)
1397 XtVaSetValues (borderOrScrolled
,
1401 if (flags
& wxMOVE_WIDTH
)
1405 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1406 short thick
, margin
;
1407 XtVaGetValues ((Widget
) m_borderWidget
,
1408 XmNshadowThickness
, &thick
,
1409 XmNmarginWidth
, &margin
,
1411 w
-= 2 * (thick
+ margin
);
1414 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1417 if (flags
& wxMOVE_HEIGHT
)
1421 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1422 short thick
, margin
;
1423 XtVaGetValues ((Widget
) m_borderWidget
,
1424 XmNshadowThickness
, &thick
,
1425 XmNmarginHeight
, &margin
,
1427 h
-= 2 * (thick
+ margin
);
1430 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1434 XtManageChild (borderOrScrolled
);
1435 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1442 XtVaSetValues((Widget
)GetTopWidget(),
1451 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1453 DoMoveWindowIntr (x
, y
, width
, height
,
1454 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1457 // ---------------------------------------------------------------------------
1459 // ---------------------------------------------------------------------------
1461 int wxWindow::GetCharHeight() const
1463 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1465 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1467 int direction
, ascent
, descent
;
1468 XCharStruct overall
;
1469 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1470 &descent
, &overall
);
1472 // return (overall.ascent + overall.descent);
1473 return (ascent
+ descent
);
1476 int wxWindow::GetCharWidth() const
1478 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1480 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1482 int direction
, ascent
, descent
;
1483 XCharStruct overall
;
1484 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1485 &descent
, &overall
);
1487 return overall
.width
;
1490 void wxWindow::GetTextExtent(const wxString
& string
,
1492 int *descent
, int *externalLeading
,
1493 const wxFont
*theFont
) const
1495 wxFont
*fontToUse
= (wxFont
*)theFont
;
1497 fontToUse
= (wxFont
*) & m_font
;
1499 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1501 WXFontStructPtr pFontStruct
= fontToUse
->GetFontStruct(1.0, GetXDisplay());
1503 int direction
, ascent
, descent2
;
1504 XCharStruct overall
;
1505 int slen
= string
.Len();
1509 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
1510 &ascent
, &descent2
, &overall
);
1513 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
1514 &direction
, &ascent
, &descent2
, &overall
);
1517 *x
= (overall
.width
);
1519 *y
= (ascent
+ descent2
);
1521 *descent
= descent2
;
1522 if (externalLeading
)
1523 *externalLeading
= 0;
1527 // ----------------------------------------------------------------------------
1529 // ----------------------------------------------------------------------------
1531 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1533 m_updateRegion
.Union( x
, y
, w
, h
);
1536 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1538 m_needsRefresh
= TRUE
;
1539 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1540 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1542 XExposeEvent dummyEvent
;
1544 GetSize(&width
, &height
);
1546 dummyEvent
.type
= Expose
;
1547 dummyEvent
.display
= display
;
1548 dummyEvent
.send_event
= True
;
1549 dummyEvent
.window
= thisWindow
;
1552 dummyEvent
.x
= rect
->x
;
1553 dummyEvent
.y
= rect
->y
;
1554 dummyEvent
.width
= rect
->width
;
1555 dummyEvent
.height
= rect
->height
;
1561 dummyEvent
.width
= width
;
1562 dummyEvent
.height
= height
;
1564 dummyEvent
.count
= 0;
1568 wxClientDC
dc(this);
1569 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1570 dc
.SetBackground(backgroundBrush
);
1577 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1580 void wxWindow::Clear()
1582 wxClientDC
dc(this);
1583 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
1584 dc
.SetBackground(brush
);
1588 void wxWindow::DoPaint()
1590 //TODO : make a temporary gc so we can do the XCopyArea below
1591 if (m_backingPixmap
&& !m_needsRefresh
)
1595 GC tempGC
= (GC
) dc
.GetBackingGC();
1597 Widget widget
= (Widget
) GetMainWidget();
1602 // We have to test whether it's a wxScrolledWindow (hack!) because
1603 // otherwise we don't know how many pixels have been scrolled. We might
1604 // solve this in the future by defining virtual wxWindow functions to get
1605 // the scroll position in pixels. Or, each kind of scrolled window has to
1606 // implement backing stores itself, using generic wxWindows code.
1607 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1608 if ( scrolledWindow
)
1611 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1617 // TODO: This could be optimized further by only copying the areas in the
1618 // current update region.
1620 // Only blit the part visible in the client area. The backing pixmap
1621 // always starts at 0, 0 but we may be looking at only a portion of it.
1622 wxSize clientArea
= GetClientSize();
1623 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1624 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1626 // Copy whichever is samller, the amount of pixmap we have to copy,
1627 // or the size of the client area.
1628 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1629 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1631 // Make sure we're not negative
1632 toBlitX
= wxMax(0, toBlitX
);
1633 toBlitY
= wxMax(0, toBlitY
);
1638 (Pixmap
) m_backingPixmap
,
1641 scrollPosX
, scrollPosY
, // Start at the scroll position
1642 toBlitX
, toBlitY
, // How much of the pixmap to copy
1648 wxWindowDC
dc(this);
1649 // Set an erase event first
1650 wxEraseEvent
eraseEvent(GetId(), &dc
);
1651 eraseEvent
.SetEventObject(this);
1652 GetEventHandler()->ProcessEvent(eraseEvent
);
1654 wxPaintEvent
event(GetId());
1655 event
.SetEventObject(this);
1656 GetEventHandler()->ProcessEvent(event
);
1658 m_needsRefresh
= FALSE
;
1662 // ----------------------------------------------------------------------------
1664 // ----------------------------------------------------------------------------
1666 // Responds to colour changes: passes event on to children.
1667 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1669 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
1672 // Only propagate to non-top-level windows
1673 wxWindow
*win
= node
->GetData();
1674 if ( win
->GetParent() )
1676 wxSysColourChangedEvent event2
;
1677 event
.m_eventObject
= win
;
1678 win
->GetEventHandler()->ProcessEvent(event2
);
1681 node
= node
->GetNext();
1685 void wxWindow::OnInternalIdle()
1687 // This calls the UI-update mechanism (querying windows for
1688 // menu/toolbar/control state information)
1689 if (wxUpdateUIEvent::CanUpdate(this))
1690 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1693 // ----------------------------------------------------------------------------
1695 // ----------------------------------------------------------------------------
1697 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1700 if (!m_acceleratorTable
.Ok())
1703 int count
= m_acceleratorTable
.GetCount();
1704 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1706 for (i
= 0; i
< count
; i
++)
1708 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1709 if (entry
->MatchesEvent(event
))
1711 // Bingo, we have a match. Now find a control that matches the
1712 // entry command id.
1714 // Need to go up to the top of the window hierarchy, since it might
1715 // be e.g. a menu item
1716 wxWindow
* parent
= this;
1717 while ( parent
&& !parent
->IsTopLevel() )
1718 parent
= parent
->GetParent();
1723 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1727 // Try for a menu command
1728 if (frame
->GetMenuBar())
1730 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1733 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1734 commandEvent
.SetEventObject(frame
);
1736 // If ProcessEvent returns TRUE (it was handled), then
1737 // the calling code will skip the event handling.
1738 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1744 // Find a child matching the command id
1745 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1751 // Now we process those kinds of windows that we can.
1752 // For now, only buttons.
1753 if ( wxDynamicCast(child
, wxButton
) )
1755 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1756 commandEvent
.SetEventObject(child
);
1757 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1765 // We didn't match the key event against an accelerator.
1769 // ============================================================================
1770 // Motif-specific stuff from here on
1771 // ============================================================================
1773 // ----------------------------------------------------------------------------
1774 // function which maintain the global hash table mapping Widgets to wxWindows
1775 // ----------------------------------------------------------------------------
1777 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1779 wxWindow
*oldItem
= NULL
;
1780 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1782 wxLogDebug("Widget table clash: new widget is %ld, %s",
1783 (long)w
, win
->GetClassInfo()->GetClassName());
1787 wxWidgetHashTable
->Put((long) w
, win
);
1789 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1790 (WXWidget
)w
, win
, win
->GetClassInfo()->GetClassName());
1795 wxWindow
*wxGetWindowFromTable(Widget w
)
1797 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1800 void wxDeleteWindowFromTable(Widget w
)
1802 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1804 wxWidgetHashTable
->Delete((long)w
);
1807 // ----------------------------------------------------------------------------
1808 // add/remove window from the table
1809 // ----------------------------------------------------------------------------
1811 // Add to hash table, add event handler
1812 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1813 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1815 wxAddWindowToTable((Widget
) mainWidget
, this);
1816 XtAddEventHandler( (Widget
) mainWidget
,
1817 ButtonPressMask
| ButtonReleaseMask
1818 | PointerMotionMask
,
1820 wxPanelItemEventHandler
,
1826 XtOverrideTranslations ((Widget
) mainWidget
,
1827 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1828 XtFree ((char *) ptr
);
1831 // Some widgets have a parent form widget, e.g. wxRadioBox
1834 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1838 XtOverrideTranslations ((Widget
) formWidget
,
1839 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1840 XtFree ((char *) ptr
);
1847 SetSize (x
, y
, width
, height
);
1852 // Remove event handler, remove from hash table
1853 bool wxWindow::DetachWidget(WXWidget widget
)
1855 XtRemoveEventHandler( (Widget
) widget
,
1856 ButtonPressMask
| ButtonReleaseMask
1857 | PointerMotionMask
,
1859 wxPanelItemEventHandler
,
1862 wxDeleteWindowFromTable((Widget
) widget
);
1866 // ----------------------------------------------------------------------------
1867 // Motif-specific accessors
1868 // ----------------------------------------------------------------------------
1870 WXWindow
wxWindow::GetClientXWindow() const
1872 Widget wMain
= (Widget
)GetClientWidget();
1874 return (WXWindow
) XtWindow(wMain
);
1876 return (WXWindow
) 0;
1879 // Get the underlying X window
1880 WXWindow
wxWindow::GetXWindow() const
1882 Widget wMain
= (Widget
)GetMainWidget();
1884 return (WXWindow
) XtWindow(wMain
);
1886 return (WXWindow
) 0;
1889 // Get the underlying X display
1890 WXDisplay
*wxWindow::GetXDisplay() const
1892 Widget wMain
= (Widget
)GetMainWidget();
1894 return (WXDisplay
*) XtDisplay(wMain
);
1896 return (WXDisplay
*) NULL
;
1899 WXWidget
wxWindow::GetMainWidget() const
1902 return m_drawingArea
;
1904 return m_mainWidget
;
1907 WXWidget
wxWindow::GetClientWidget() const
1909 if (m_drawingArea
!= (WXWidget
) 0)
1910 return m_drawingArea
;
1912 return GetMainWidget();
1915 WXWidget
wxWindow::GetTopWidget() const
1917 return GetMainWidget();
1920 WXWidget
wxWindow::GetLabelWidget() const
1922 return GetMainWidget();
1925 // ----------------------------------------------------------------------------
1927 // ----------------------------------------------------------------------------
1929 // All widgets should have this as their resize proc.
1930 // OnSize sent to wxWindow via client data.
1931 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1932 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1934 wxWindow
*win
= wxGetWindowFromTable(w
);
1938 if (win
->PreResize())
1941 win
->GetSize(&width
, &height
);
1942 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1943 sizeEvent
.SetEventObject(win
);
1944 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1948 static void wxCanvasRepaintProc(Widget drawingArea
,
1949 XtPointer clientData
,
1950 XmDrawingAreaCallbackStruct
* cbs
)
1952 if (!wxGetWindowFromTable(drawingArea
))
1955 XEvent
* event
= cbs
->event
;
1956 wxWindow
* win
= (wxWindow
*) clientData
;
1958 switch (event
->type
)
1962 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1963 event
->xexpose
.width
, event
->xexpose
.height
);
1965 if (event
-> xexpose
.count
== 0)
1974 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1975 static void wxCanvasEnterLeave(Widget drawingArea
,
1976 XtPointer
WXUNUSED(clientData
),
1977 XCrossingEvent
* event
)
1979 XmDrawingAreaCallbackStruct cbs
;
1982 ((XCrossingEvent
&) ev
) = *event
;
1984 cbs
.reason
= XmCR_INPUT
;
1987 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1990 // Fix to make it work under Motif 1.0 (!)
1991 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1992 XButtonEvent
*WXUNUSED(event
))
1994 #if XmVersion <= 1000
1995 XmDrawingAreaCallbackStruct cbs
;
1998 ev
= *((XEvent
*) event
);
1999 cbs
.reason
= XmCR_INPUT
;
2002 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
2003 #endif // XmVersion <= 1000
2006 static void wxCanvasInputEvent(Widget drawingArea
,
2007 XtPointer
WXUNUSED(data
),
2008 XmDrawingAreaCallbackStruct
* cbs
)
2010 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
2016 if (cbs
->reason
!= XmCR_INPUT
)
2019 local_event
= *(cbs
->event
); // We must keep a copy!
2021 switch (local_event
.xany
.type
)
2029 wxMouseEvent wxevent
;
2030 if(wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, &local_event
))
2032 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
2038 wxKeyEvent
event (wxEVT_CHAR
);
2039 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2041 // Implement wxFrame::OnCharHook by checking ancestor.
2042 wxWindow
*parent
= canvas
;
2043 while (parent
&& !parent
->IsTopLevel())
2044 parent
= parent
->GetParent();
2048 event
.SetEventType(wxEVT_CHAR_HOOK
);
2049 if (parent
->GetEventHandler()->ProcessEvent(event
))
2053 // For simplicity, OnKeyDown is the same as OnChar
2054 // TODO: filter modifier key presses from OnChar
2055 event
.SetEventType(wxEVT_KEY_DOWN
);
2057 // Only process OnChar if OnKeyDown didn't swallow it
2058 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2060 event
.SetEventType(wxEVT_CHAR
);
2061 canvas
->GetEventHandler()->ProcessEvent (event
);
2068 wxKeyEvent
event (wxEVT_KEY_UP
);
2069 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2071 canvas
->GetEventHandler()->ProcessEvent (event
);
2077 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2079 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2080 event
.SetEventObject(canvas
);
2081 canvas
->GetEventHandler()->ProcessEvent(event
);
2087 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2089 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2090 event
.SetEventObject(canvas
);
2091 canvas
->GetEventHandler()->ProcessEvent(event
);
2100 static void wxPanelItemEventHandler(Widget wid
,
2101 XtPointer
WXUNUSED(client_data
),
2103 Boolean
*continueToDispatch
)
2105 // Widget can be a label or the actual widget.
2107 wxWindow
*window
= wxGetWindowFromTable(wid
);
2110 wxMouseEvent
wxevent(0);
2111 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2113 window
->GetEventHandler()->ProcessEvent(wxevent
);
2117 // TODO: probably the key to allowing default behaviour to happen. Say we
2118 // set a m_doDefault flag to FALSE at the start of this function. Then in
2119 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2120 // TRUE, indicating that default processing can happen. Thus, behaviour can
2121 // appear to be overridden just by adding an event handler and not calling
2122 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2123 // way of handling drawing area events, to simplify things.
2124 *continueToDispatch
= True
;
2127 static void wxScrollBarCallback(Widget scrollbar
,
2128 XtPointer clientData
,
2129 XmScrollBarCallbackStruct
*cbs
)
2131 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2132 wxOrientation orientation
= (wxOrientation
)(int)clientData
;
2134 wxEventType eventType
= wxEVT_NULL
;
2135 switch (cbs
->reason
)
2137 case XmCR_INCREMENT
:
2139 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2142 case XmCR_DECREMENT
:
2144 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2149 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2152 case XmCR_VALUE_CHANGED
:
2154 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2157 case XmCR_PAGE_INCREMENT
:
2159 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2162 case XmCR_PAGE_DECREMENT
:
2164 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2169 eventType
= wxEVT_SCROLLWIN_TOP
;
2172 case XmCR_TO_BOTTOM
:
2174 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2179 // Should never get here
2180 wxFAIL_MSG("Unknown scroll event.");
2185 wxScrollWinEvent
event(eventType
,
2188 event
.SetEventObject( win
);
2189 win
->GetEventHandler()->ProcessEvent(event
);
2192 // For repainting arbitrary windows
2193 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2198 wxWindow
* win
= wxGetWindowFromTable(w
);
2202 switch(event
-> type
)
2206 window
= (Window
) win
-> GetXWindow();
2207 display
= (Display
*) win
-> GetXDisplay();
2209 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2210 event
->xexpose
.width
, event
->xexpose
.height
);
2212 if (event
-> xexpose
.count
== 0)
2222 // ----------------------------------------------------------------------------
2223 // TranslateXXXEvent() functions
2224 // ----------------------------------------------------------------------------
2226 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2227 Widget widget
, XEvent
*xevent
)
2229 switch (xevent
->xany
.type
)
2234 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2235 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2236 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2242 wxEventType eventType
= wxEVT_NULL
;
2244 if (xevent
->xany
.type
== LeaveNotify
)
2246 eventType
= wxEVT_LEAVE_WINDOW
;
2248 if (xevent
->xany
.type
== EnterNotify
)
2250 eventType
= wxEVT_ENTER_WINDOW
;
2252 else if (xevent
->xany
.type
== MotionNotify
)
2254 eventType
= wxEVT_MOTION
;
2256 else if (xevent
->xany
.type
== ButtonPress
)
2258 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2260 if (xevent
->xbutton
.button
== Button1
)
2262 eventType
= wxEVT_LEFT_DOWN
;
2265 else if (xevent
->xbutton
.button
== Button2
)
2267 eventType
= wxEVT_MIDDLE_DOWN
;
2270 else if (xevent
->xbutton
.button
== Button3
)
2272 eventType
= wxEVT_RIGHT_DOWN
;
2276 // check for a double click
2278 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2279 long ts
= wxevent
.GetTimestamp();
2281 int buttonLast
= win
->GetLastClickedButton();
2282 long lastTS
= win
->GetLastClickTime();
2283 if ( buttonLast
&& buttonLast
== button
&&
2284 (ts
- lastTS
) < dclickTime
)
2287 win
->SetLastClick(0, ts
);
2288 if ( eventType
== wxEVT_LEFT_DOWN
)
2289 eventType
= wxEVT_LEFT_DCLICK
;
2290 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2291 eventType
= wxEVT_MIDDLE_DCLICK
;
2292 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2293 eventType
= wxEVT_RIGHT_DCLICK
;
2297 // not fast enough or different button
2298 win
->SetLastClick(button
, ts
);
2301 else if (xevent
->xany
.type
== ButtonRelease
)
2303 if (xevent
->xbutton
.button
== Button1
)
2305 eventType
= wxEVT_LEFT_UP
;
2307 else if (xevent
->xbutton
.button
== Button2
)
2309 eventType
= wxEVT_MIDDLE_UP
;
2311 else if (xevent
->xbutton
.button
== Button3
)
2313 eventType
= wxEVT_RIGHT_UP
;
2323 wxevent
.SetEventType(eventType
);
2326 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2329 win
->GetPosition(&x2
, &y2
);
2331 // The button x/y must be translated to wxWindows
2332 // window space - the widget might be a label or button,
2336 if (widget
!= (Widget
)win
->GetMainWidget())
2342 wxevent
.m_x
= xevent
->xbutton
.x
+ dx
;
2343 wxevent
.m_y
= xevent
->xbutton
.y
+ dy
;
2345 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2346 || (event_left_is_down (xevent
)
2347 && (eventType
!= wxEVT_LEFT_UP
)));
2348 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2349 || (event_middle_is_down (xevent
)
2350 && (eventType
!= wxEVT_MIDDLE_UP
)));
2351 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2352 || (event_right_is_down (xevent
)
2353 && (eventType
!= wxEVT_RIGHT_UP
)));
2355 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2356 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2357 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2358 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2360 wxevent
.SetId(win
->GetId());
2361 wxevent
.SetEventObject(win
);
2369 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2370 Widget
WXUNUSED(widget
), XEvent
*xevent
)
2372 switch (xevent
->xany
.type
)
2380 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2381 int id
= wxCharCodeXToWX (keySym
);
2382 // id may be WXK_xxx code - these are outside ASCII range, so we
2383 // can't just use toupper() on id
2384 if (id
>= 'a' && id
<= 'z')
2387 if (xevent
->xkey
.state
& ShiftMask
)
2388 wxevent
.m_shiftDown
= TRUE
;
2389 if (xevent
->xkey
.state
& ControlMask
)
2390 wxevent
.m_controlDown
= TRUE
;
2391 if (xevent
->xkey
.state
& Mod3Mask
)
2392 wxevent
.m_altDown
= TRUE
;
2393 if (xevent
->xkey
.state
& Mod1Mask
)
2394 wxevent
.m_metaDown
= TRUE
;
2395 wxevent
.SetEventObject(win
);
2396 wxevent
.m_keyCode
= id
;
2397 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2399 wxevent
.m_x
= xevent
->xbutton
.x
;
2400 wxevent
.m_y
= xevent
->xbutton
.y
;
2414 // ----------------------------------------------------------------------------
2416 // ----------------------------------------------------------------------------
2418 #define YAllocColor XAllocColor
2419 XColor g_itemColors
[5];
2420 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2423 static XmColorProc colorProc
;
2425 result
= wxNO_COLORS
;
2429 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2430 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2431 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2432 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2433 if (colorProc
== (XmColorProc
) NULL
)
2435 // Get a ptr to the actual function
2436 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2437 // And set it back to motif.
2438 XmSetColorCalculation (colorProc
);
2440 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2441 &g_itemColors
[wxFORE_INDEX
],
2442 &g_itemColors
[wxSELE_INDEX
],
2443 &g_itemColors
[wxTOPS_INDEX
],
2444 &g_itemColors
[wxBOTS_INDEX
]);
2445 result
= wxBACK_COLORS
;
2449 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2450 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2451 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2452 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2453 if (result
== wxNO_COLORS
)
2454 result
= wxFORE_COLORS
;
2457 Display
*dpy
= display
;
2458 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2462 /* 5 Colours to allocate */
2463 for (int i
= 0; i
< 5; i
++)
2464 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2465 result
= wxNO_COLORS
;
2469 /* Only 1 colour to allocate */
2470 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2471 result
= wxNO_COLORS
;
2477 // Changes the foreground and background colours to be derived from the current
2478 // background colour. To change the foreground colour, you must call
2479 // SetForegroundColour explicitly.
2480 void wxWindow::ChangeBackgroundColour()
2482 WXWidget mainWidget
= GetMainWidget();
2484 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2487 void wxWindow::ChangeForegroundColour()
2489 WXWidget mainWidget
= GetMainWidget();
2491 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2492 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2493 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2496 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2498 if ( !wxWindowBase::SetBackgroundColour(col
) )
2501 ChangeBackgroundColour();
2506 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2508 if ( !wxWindowBase::SetForegroundColour(col
) )
2511 ChangeForegroundColour();
2516 void wxWindow::ChangeFont(bool keepOriginalSize
)
2518 // Note that this causes the widget to be resized back
2519 // to its original size! We therefore have to set the size
2520 // back again. TODO: a better way in Motif?
2521 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2522 if (w
&& m_font
.Ok())
2524 int width
, height
, width1
, height1
;
2525 GetSize(& width
, & height
);
2527 wxDoChangeFont( GetLabelWidget(), m_font
);
2529 GetSize(& width1
, & height1
);
2530 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2532 SetSize(-1, -1, width
, height
);
2537 // ----------------------------------------------------------------------------
2539 // ----------------------------------------------------------------------------
2541 wxWindow
*wxGetActiveWindow()
2544 wxFAIL_MSG("Not implemented");
2549 wxWindow
*wxWindowBase::GetCapture()
2551 return (wxWindow
*)g_captureWindow
;
2555 // Find the wxWindow at the current mouse position, returning the mouse
2557 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2559 return wxFindWindowAtPoint(wxGetMousePosition());
2562 // Get the current mouse position.
2563 wxPoint
wxGetMousePosition()
2565 Display
*display
= wxGlobalDisplay();
2566 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2567 Window rootReturn
, childReturn
;
2568 int rootX
, rootY
, winX
, winY
;
2569 unsigned int maskReturn
;
2571 XQueryPointer (display
,
2575 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2576 return wxPoint(rootX
, rootY
);
2580 // ----------------------------------------------------------------------------
2581 // wxNoOptimize: switch off size optimization
2582 // ----------------------------------------------------------------------------
2584 int wxNoOptimize::ms_count
= 0;