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
)
148 EVT_IDLE(wxWindow::OnIdle
)
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void wxWindow::UnmanageAndDestroy(WXWidget widget
)
161 Widget w
= (Widget
)widget
;
169 bool wxWindow::MapOrUnmap(WXWidget widget
, bool domap
)
171 Widget w
= (Widget
)widget
;
175 // Rationale: a lot of common operations (including but not
176 // limited to moving, resizing and appending items to a listbox)
177 // unmamange the widget, do their work, then manage it again.
178 // This means that, for example adding an item to a listbox will show it,
179 // or that most controls are shown every time they are moved or resized!
180 XtSetMappedWhenManaged( w
, domap
);
182 // if the widget is not unmanaged, it still intercepts
183 // mouse events, even if it is not mapped (and hence invisible)
198 // ----------------------------------------------------------------------------
200 // ----------------------------------------------------------------------------
202 void wxWindow::Init()
204 // generic initializations first
208 m_needsRefresh
= TRUE
;
209 m_mainWidget
= (WXWidget
) 0;
211 m_winCaptured
= FALSE
;
214 m_isBeingDeleted
= FALSE
;
220 m_drawingArea
= (WXWidget
) 0;
225 m_backingPixmap
= (WXPixmap
) 0;
236 // real construction (Init() must have been called before!)
237 bool wxWindow::Create(wxWindow
*parent
, wxWindowID id
,
241 const wxString
& name
)
243 wxCHECK_MSG( parent
, FALSE
, "can't create wxWindow without parent" );
245 CreateBase(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
247 parent
->AddChild(this);
249 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
250 m_foregroundColour
= *wxBLACK
;
252 //// TODO: we should probably optimize by only creating a
253 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
254 //// But for now, let's simplify things by always creating the
255 //// drawing area, since otherwise the translations are different.
257 // New translations for getting mouse motion feedback
258 static const String translations
=
259 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
260 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
261 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
262 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
263 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
264 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
265 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
266 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
267 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
268 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
269 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
270 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
271 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
272 <Key>: DrawingAreaInput()";
274 XtActionsRec actions
[1];
275 actions
[0].string
= "wxCanvasMotionEvent";
276 actions
[0].proc
= (XtActionProc
) wxCanvasMotionEvent
;
277 XtAppAddActions ((XtAppContext
) wxTheApp
->GetAppContext(), actions
, 1);
279 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
280 m_borderWidget
= wxCreateBorderWidget( (WXWidget
)parentWidget
, style
);
282 m_scrolledWindow
= (WXWidget
)XtVaCreateManagedWidget
285 xmScrolledWindowWidgetClass
,
286 m_borderWidget
? (Widget
) m_borderWidget
288 XmNresizePolicy
, XmRESIZE_NONE
,
290 XmNscrollingPolicy
, XmAPPLICATION_DEFINED
,
291 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
295 XtTranslations ptr
= XtParseTranslationTable(translations
);
296 m_drawingArea
= (WXWidget
)XtVaCreateWidget
299 xmDrawingAreaWidgetClass
, (Widget
) m_scrolledWindow
,
300 XmNunitType
, XmPIXELS
,
301 // XmNresizePolicy, XmRESIZE_ANY,
302 XmNresizePolicy
, XmRESIZE_NONE
,
305 XmNtranslations
, ptr
,
308 XtFree((char *) ptr
);
311 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS
)
313 ptr
= XtParseTranslationTable ("<Key>: DrawingAreaInput()");
314 XtOverrideTranslations ((Widget
) m_drawingArea
, ptr
);
315 XtFree ((char *) ptr
);
319 wxAddWindowToTable((Widget
) m_drawingArea
, this);
320 wxAddWindowToTable((Widget
) m_scrolledWindow
, this);
322 // This order is very important in Motif 1.2.1
323 XtRealizeWidget ((Widget
) m_scrolledWindow
);
324 XtRealizeWidget ((Widget
) m_drawingArea
);
325 XtManageChild ((Widget
) m_drawingArea
);
327 ptr
= XtParseTranslationTable("<Configure>: resize()");
328 XtOverrideTranslations((Widget
) m_drawingArea
, ptr
);
329 XtFree ((char *) ptr
);
331 XtAddCallback ((Widget
) m_drawingArea
, XmNexposeCallback
, (XtCallbackProc
) wxCanvasRepaintProc
, (XtPointer
) this);
332 XtAddCallback ((Widget
) m_drawingArea
, XmNinputCallback
, (XtCallbackProc
) wxCanvasInputEvent
, (XtPointer
) this);
335 (Widget
)m_drawingArea
,
336 PointerMotionHintMask
| EnterWindowMask
|
337 LeaveWindowMask
| FocusChangeMask
,
339 (XtEventHandler
) wxCanvasEnterLeave
,
343 // Scrolled widget needs to have its colour changed or we get a little blue
344 // square where the scrollbars abutt
345 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
346 wxDoChangeBackgroundColour(m_scrolledWindow
, backgroundColour
, TRUE
);
347 wxDoChangeBackgroundColour(m_drawingArea
, backgroundColour
, TRUE
);
349 XmScrolledWindowSetAreas(
350 (Widget
)m_scrolledWindow
,
351 (Widget
) 0, (Widget
) 0,
352 (Widget
) m_drawingArea
);
354 // Without this, the cursor may not be restored properly (e.g. in splitter
356 SetCursor(*wxSTANDARD_CURSOR
);
357 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
358 DoSetSizeIntr(pos
.x
, pos
.y
, size
.x
,size
.y
, wxSIZE_AUTO
, TRUE
);
363 wxWindow::~wxWindow()
365 if (g_captureWindow
== this)
366 g_captureWindow
= NULL
;
368 m_isBeingDeleted
= TRUE
;
370 // Motif-specific actions first
371 WXWidget wMain
= GetMainWidget();
374 // Removes event handlers
379 m_parent
->RemoveChild( this );
381 // If m_drawingArea, we're a fully-fledged window with drawing area,
382 // scrollbars etc. (what wxCanvas used to be)
385 // Destroy children before destroying self
389 XFreePixmap (XtDisplay ((Widget
) GetMainWidget()), (Pixmap
) m_backingPixmap
);
391 Widget w
= (Widget
) m_drawingArea
;
392 wxDeleteWindowFromTable(w
);
397 m_drawingArea
= (WXWidget
) 0;
400 // Only if we're _really_ a canvas (not a dialog box/panel)
401 if (m_scrolledWindow
)
403 wxDeleteWindowFromTable((Widget
) m_scrolledWindow
);
408 wxDeleteWindowFromTable((Widget
) m_hScrollBar
);
409 XtUnmanageChild((Widget
) m_hScrollBar
);
413 wxDeleteWindowFromTable((Widget
) m_vScrollBar
);
414 XtUnmanageChild((Widget
) m_vScrollBar
);
418 XtDestroyWidget((Widget
) m_hScrollBar
);
420 XtDestroyWidget((Widget
) m_vScrollBar
);
422 UnmanageAndDestroy(m_scrolledWindow
);
426 XtDestroyWidget ((Widget
) m_borderWidget
);
427 m_borderWidget
= (WXWidget
) 0;
430 else // Why wasn't this here before? JACS 8/3/2000
434 // Destroy the window
437 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
438 // Child widgets will get destroyed automatically when a frame
439 // or dialog is destroyed, but before that you may get some memory
440 // leaks and potential layout problems if you delete and then add
443 // GRG, Feb/2000: commented this out when adding support for
444 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
445 // that this call crashed wxMotif under OS/2, so it seems
446 // that leaving it out is the right thing to do.
447 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
448 XtDestroyWidget((Widget
) GetMainWidget());
449 SetMainWidget((WXWidget
) NULL
);
453 // ----------------------------------------------------------------------------
454 // scrollbar management
455 // ----------------------------------------------------------------------------
457 WXWidget
wxWindow::DoCreateScrollBar(WXWidget parent
,
458 wxOrientation orientation
,
461 int orient
= ( orientation
& wxHORIZONTAL
) ? XmHORIZONTAL
: XmVERTICAL
;
463 XtVaCreateManagedWidget( "scrollBarWidget",
464 xmScrollBarWidgetClass
, (Widget
)parent
,
465 XmNorientation
, orient
,
470 XtPointer o
= (XtPointer
)orientation
;
471 XtCallbackProc cb
= (XtCallbackProc
)callback
;
473 XtAddCallback( sb
, XmNvalueChangedCallback
, cb
, o
);
474 XtAddCallback( sb
, XmNdragCallback
, cb
, o
);
475 XtAddCallback( sb
, XmNincrementCallback
, cb
, o
);
476 XtAddCallback( sb
, XmNdecrementCallback
, cb
, o
);
477 XtAddCallback( sb
, XmNpageIncrementCallback
, cb
, o
);
478 XtAddCallback( sb
, XmNpageDecrementCallback
, cb
, o
);
479 XtAddCallback( sb
, XmNtoTopCallback
, cb
, o
);
480 XtAddCallback( sb
, XmNtoBottomCallback
, cb
, o
);
486 void wxWindow::CreateScrollbar(wxOrientation orientation
)
488 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
490 XtVaSetValues( (Widget
) m_scrolledWindow
,
491 XmNresizePolicy
, XmRESIZE_NONE
,
494 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
495 // Add scrollbars if required
496 if (orientation
== wxHORIZONTAL
)
498 m_hScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxHORIZONTAL
,
499 (void (*)())wxScrollBarCallback
);
501 wxDoChangeBackgroundColour(m_hScrollBar
, backgroundColour
, TRUE
);
503 XtRealizeWidget( (Widget
)m_hScrollBar
);
505 XtVaSetValues((Widget
) m_scrolledWindow
,
506 XmNhorizontalScrollBar
, (Widget
) m_hScrollBar
,
509 wxAddWindowToTable( (Widget
)m_hScrollBar
, this );
511 else if (orientation
== wxVERTICAL
)
513 m_vScrollBar
= DoCreateScrollBar( m_scrolledWindow
, wxVERTICAL
,
514 (void (*)())wxScrollBarCallback
);
516 wxDoChangeBackgroundColour(m_vScrollBar
, backgroundColour
, TRUE
);
518 XtRealizeWidget((Widget
)m_vScrollBar
);
520 XtVaSetValues((Widget
) m_scrolledWindow
,
521 XmNverticalScrollBar
, (Widget
) m_vScrollBar
,
524 wxAddWindowToTable( (Widget
)m_vScrollBar
, this );
527 XtVaSetValues( (Widget
) m_scrolledWindow
,
528 XmNresizePolicy
, XmRESIZE_ANY
,
532 void wxWindow::DestroyScrollbar(wxOrientation orientation
)
534 wxCHECK_RET( m_drawingArea
, "this window can't have scrollbars" );
536 XtVaSetValues((Widget
) m_scrolledWindow
,
537 XmNresizePolicy
, XmRESIZE_NONE
,
539 String stringSB
= orientation
== wxHORIZONTAL
?
540 XmNhorizontalScrollBar
: XmNverticalScrollBar
;
541 WXWidget
* widgetSB
= orientation
== wxHORIZONTAL
?
542 &m_hScrollBar
: &m_vScrollBar
;
546 wxDeleteWindowFromTable( (Widget
)*widgetSB
);
547 XtDestroyWidget( (Widget
)*widgetSB
);
548 *widgetSB
= (WXWidget
)NULL
;
551 XtVaSetValues( (Widget
)m_scrolledWindow
,
552 stringSB
, (Widget
) 0,
555 XtVaSetValues((Widget
) m_scrolledWindow
,
556 XmNresizePolicy
, XmRESIZE_ANY
,
560 // ---------------------------------------------------------------------------
562 // ---------------------------------------------------------------------------
564 void wxWindow::SetFocus()
566 Widget wMain
= (Widget
) GetMainWidget();
567 XmProcessTraversal(wMain
, XmTRAVERSE_CURRENT
);
568 XmProcessTraversal((Widget
) GetMainWidget(), XmTRAVERSE_CURRENT
);
571 // Get the window with the focus
572 wxWindow
*wxWindowBase::FindFocus()
575 // (1) Can there be multiple focussed widgets in an application?
576 // In which case we need to find the top-level window that's
578 // (2) The widget with the focus may not be in the widget table
579 // depending on which widgets I put in the table
580 wxWindow
*winFocus
= (wxWindow
*)NULL
;
581 for ( wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
583 node
= node
->GetNext() )
585 wxWindow
*win
= node
->GetData();
587 Widget w
= XmGetFocusWidget ((Widget
) win
->GetTopWidget());
589 if (w
!= (Widget
) NULL
)
591 winFocus
= wxGetWindowFromTable(w
);
600 bool wxWindow::Enable(bool enable
)
602 if ( !wxWindowBase::Enable(enable
) )
605 Widget wMain
= (Widget
)GetMainWidget();
608 XtSetSensitive(wMain
, enable
);
609 XmUpdateDisplay(wMain
);
615 bool wxWindow::Show(bool show
)
617 if ( !wxWindowBase::Show(show
) )
620 if (m_borderWidget
|| m_scrolledWindow
)
622 MapOrUnmap(m_borderWidget
? m_borderWidget
: m_scrolledWindow
, show
);
623 // MapOrUnmap(m_drawingArea, show);
627 if ( !MapOrUnmap(GetTopWidget(), show
) )
628 MapOrUnmap(GetMainWidget(), show
);
634 // Raise the window to the top of the Z order
635 void wxWindow::Raise()
637 Widget wTop
= (Widget
) GetTopWidget();
638 Window window
= XtWindow(wTop
);
639 XRaiseWindow(XtDisplay(wTop
), window
);
642 // Lower the window to the bottom of the Z order
643 void wxWindow::Lower()
645 Widget wTop
= (Widget
) GetTopWidget();
646 Window window
= XtWindow(wTop
);
647 XLowerWindow(XtDisplay(wTop
), window
);
650 void wxWindow::SetTitle(const wxString
& title
)
652 XtVaSetValues((Widget
)GetMainWidget(), XmNtitle
, title
.c_str(), NULL
);
655 wxString
wxWindow::GetTitle() const
658 XtVaGetValues((Widget
)GetMainWidget(), XmNtitle
, &title
, NULL
);
660 return wxString(title
);
663 void wxWindow::DoCaptureMouse()
665 g_captureWindow
= this;
669 Widget wMain
= (Widget
)GetMainWidget();
671 XtAddGrab(wMain
, TRUE
, FALSE
);
673 m_winCaptured
= TRUE
;
676 void wxWindow::DoReleaseMouse()
678 g_captureWindow
= NULL
;
679 if ( !m_winCaptured
)
682 Widget wMain
= (Widget
)GetMainWidget();
686 m_winCaptured
= FALSE
;
689 bool wxWindow::SetFont(const wxFont
& font
)
691 if ( !wxWindowBase::SetFont(font
) )
702 bool wxWindow::SetCursor(const wxCursor
& cursor
)
704 if ( !wxWindowBase::SetCursor(cursor
) )
710 // wxASSERT_MSG( m_cursor.Ok(),
711 // wxT("cursor must be valid after call to the base version"));
712 wxCursor
* cursor2
= NULL
;
714 cursor2
= & m_cursor
;
716 cursor2
= wxSTANDARD_CURSOR
;
718 WXDisplay
*dpy
= GetXDisplay();
719 WXCursor x_cursor
= cursor2
->GetXCursor(dpy
);
721 Widget w
= (Widget
) GetMainWidget();
722 Window win
= XtWindow(w
);
723 XDefineCursor((Display
*) dpy
, win
, (Cursor
) x_cursor
);
728 // Coordinates relative to the window
729 void wxWindow::WarpPointer (int x
, int y
)
731 Widget wClient
= (Widget
)GetClientWidget();
733 XWarpPointer(XtDisplay(wClient
), None
, XtWindow(wClient
), 0, 0, 0, 0, x
, y
);
736 // ---------------------------------------------------------------------------
738 // ---------------------------------------------------------------------------
740 int wxWindow::GetScrollPos(int orient
) const
742 if (orient
== wxHORIZONTAL
)
748 Widget scrollBar
= (Widget
) ((orient
== wxHORIZONTAL
) ? m_hScrollBar
: m_vScrollBar
);
752 XtVaGetValues(scrollBar
, XmNvalue
, &pos
, NULL
);
760 // This now returns the whole range, not just the number of positions that we
762 int wxWindow::GetScrollRange(int orient
) const
764 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
765 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
769 XtVaGetValues(scrollBar
, XmNmaximum
, &range
, NULL
);
773 int wxWindow::GetScrollThumb(int orient
) const
775 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
776 wxCHECK_MSG( scrollBar
, 0, "no such scrollbar" );
779 XtVaGetValues(scrollBar
, XmNsliderSize
, &thumb
, NULL
);
783 void wxWindow::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
785 Widget scrollBar
= (Widget
)GetScrollbar((wxOrientation
)orient
);
789 XtVaSetValues (scrollBar
, XmNvalue
, pos
, NULL
);
792 SetInternalScrollPos((wxOrientation
)orient
, pos
);
795 // New function that will replace some of the above.
796 void wxWindow::SetScrollbar(int orient
, int pos
, int thumbVisible
,
797 int range
, bool WXUNUSED(refresh
))
800 GetSize(& oldW
, & oldH
);
804 if (thumbVisible
== 0)
807 if (thumbVisible
> range
)
808 thumbVisible
= range
;
810 // Save the old state to see if it changed
811 WXWidget oldScrollBar
= GetScrollbar((wxOrientation
)orient
);
813 if (orient
== wxHORIZONTAL
)
815 if (thumbVisible
== range
)
818 DestroyScrollbar(wxHORIZONTAL
);
823 CreateScrollbar(wxHORIZONTAL
);
826 if (orient
== wxVERTICAL
)
828 if (thumbVisible
== range
)
831 DestroyScrollbar(wxVERTICAL
);
836 CreateScrollbar(wxVERTICAL
);
839 WXWidget newScrollBar
= GetScrollbar((wxOrientation
)orient
);
841 if (oldScrollBar
!= newScrollBar
)
843 // This is important! Without it, scrollbars misbehave badly.
844 XtUnrealizeWidget((Widget
) m_scrolledWindow
);
845 XmScrolledWindowSetAreas ((Widget
) m_scrolledWindow
, (Widget
) m_hScrollBar
, (Widget
) m_vScrollBar
, (Widget
) m_drawingArea
);
846 XtRealizeWidget((Widget
) m_scrolledWindow
);
847 XtManageChild((Widget
) m_scrolledWindow
);
852 XtVaSetValues((Widget
) newScrollBar
,
856 XmNsliderSize
, thumbVisible
,
860 SetInternalScrollPos((wxOrientation
)orient
, pos
);
863 GetSize(& newW
, & newH
);
865 // Adjusting scrollbars can resize the canvas accidentally
866 if (newW
!= oldW
|| newH
!= oldH
)
867 SetSize(-1, -1, oldW
, oldH
);
870 // Does a physical scroll
871 void wxWindow::ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
876 // Use specified rectangle
877 x
= rect
->x
; y
= rect
->y
; w
= rect
->width
; h
= rect
->height
;
881 // Use whole client area
883 GetClientSize(& w
, & h
);
886 int x1
= (dx
>= 0) ? x
: x
- dx
;
887 int y1
= (dy
>= 0) ? y
: y
- dy
;
888 int w1
= w
- abs(dx
);
889 int h1
= h
- abs(dy
);
890 int x2
= (dx
>= 0) ? x
+ dx
: x
;
891 int y2
= (dy
>= 0) ? y
+ dy
: y
;
895 dc
.SetLogicalFunction (wxCOPY
);
897 Widget widget
= (Widget
) GetMainWidget();
898 Window window
= XtWindow(widget
);
899 Display
* display
= XtDisplay(widget
);
901 XCopyArea(display
, window
, window
, (GC
) dc
.GetGC(),
902 x1
, y1
, w1
, h1
, x2
, y2
);
904 dc
.SetAutoSetting(TRUE
);
905 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
906 dc
.SetBrush(brush
); // FIXME: needed?
908 wxWindowList::Node
*cnode
= m_children
.GetFirst();
911 wxWindow
*child
= cnode
->GetData();
914 child
->GetSize( &sx
, &sy
);
915 wxPoint
pos( child
->GetPosition() );
916 child
->SetSize( pos
.x
+ dx
, pos
.y
+ dy
, sx
, sy
, wxSIZE_ALLOW_MINUS_ONE
);
917 cnode
= cnode
->GetNext();
920 // We'll add rectangles to the list of update rectangles according to which
921 // bits we've exposed.
926 wxRect
*rect
= new wxRect
;
932 XFillRectangle(display
, window
,
933 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
937 rect
->width
= rect
->width
;
938 rect
->height
= rect
->height
;
940 updateRects
.Append((wxObject
*) rect
);
944 wxRect
*rect
= new wxRect
;
946 rect
->x
= x
+ w
+ dx
;
951 XFillRectangle(display
, window
,
952 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
,
957 rect
->width
= rect
->width
;
958 rect
->height
= rect
->height
;
960 updateRects
.Append((wxObject
*) rect
);
964 wxRect
*rect
= new wxRect
;
971 XFillRectangle(display
, window
,
972 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
976 rect
->width
= rect
->width
;
977 rect
->height
= rect
->height
;
979 updateRects
.Append((wxObject
*) rect
);
983 wxRect
*rect
= new wxRect
;
986 rect
->y
= y
+ h
+ dy
;
990 XFillRectangle(display
, window
,
991 (GC
) dc
.GetGC(), rect
->x
, rect
->y
, rect
->width
, rect
->height
);
995 rect
->width
= rect
->width
;
996 rect
->height
= rect
->height
;
998 updateRects
.Append((wxObject
*) rect
);
1000 dc
.SetBrush(wxNullBrush
);
1002 // Now send expose events
1004 wxList::Node
* node
= updateRects
.GetFirst();
1007 wxRect
* rect
= (wxRect
*) node
->GetData();
1010 event
.type
= Expose
;
1011 event
.display
= display
;
1012 event
.send_event
= True
;
1013 event
.window
= window
;
1017 event
.width
= rect
->width
;
1018 event
.height
= rect
->height
;
1022 XSendEvent(display
, window
, False
, ExposureMask
, (XEvent
*)&event
);
1024 node
= node
->GetNext();
1028 // Delete the update rects
1029 node
= updateRects
.GetFirst();
1032 wxRect
* rect
= (wxRect
*) node
->GetData();
1034 node
= node
->GetNext();
1037 XmUpdateDisplay((Widget
) GetMainWidget());
1040 // ---------------------------------------------------------------------------
1042 // ---------------------------------------------------------------------------
1044 #if wxUSE_DRAG_AND_DROP
1046 void wxWindow::SetDropTarget(wxDropTarget
* WXUNUSED(pDropTarget
))
1053 // Old style file-manager drag&drop
1054 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept
))
1059 // ----------------------------------------------------------------------------
1061 // ----------------------------------------------------------------------------
1065 void wxWindow::DoSetToolTip(wxToolTip
* WXUNUSED(tooltip
))
1070 #endif // wxUSE_TOOLTIPS
1072 // ----------------------------------------------------------------------------
1074 // ----------------------------------------------------------------------------
1078 bool wxWindow::DoPopupMenu(wxMenu
*menu
, int x
, int y
)
1080 Widget widget
= (Widget
) GetMainWidget();
1082 /* The menuId field seems to be usused, so we'll use it to
1083 indicate whether a menu is popped up or not:
1084 0: Not currently created as a popup
1085 -1: Created as a popup, but not active
1089 if (menu
->GetParent() && (menu
->GetId() != -1))
1092 if (menu
->GetMainWidget())
1094 menu
->DestroyMenu(TRUE
);
1097 menu
->SetId(1); /* Mark as popped-up */
1098 menu
->CreateMenu(NULL
, widget
, menu
);
1099 menu
->SetInvokingWindow(this);
1103 // menu->SetParent(parent);
1104 // parent->children->Append(menu); // Store menu for later deletion
1106 Widget menuWidget
= (Widget
) menu
->GetMainWidget();
1114 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1116 wxCanvas *canvas = (wxCanvas *) this;
1117 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1118 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1122 Display
*display
= XtDisplay (widget
);
1123 Window rootWindow
= RootWindowOfScreen (XtScreen((Widget
)widget
));
1124 Window thisWindow
= XtWindow (widget
);
1126 XTranslateCoordinates (display
, thisWindow
, rootWindow
, (int) deviceX
, (int) deviceY
,
1127 &rootX
, &rootY
, &childWindow
);
1129 XButtonPressedEvent event
;
1130 event
.type
= ButtonPress
;
1136 event
.x_root
= rootX
;
1137 event
.y_root
= rootY
;
1139 XmMenuPosition (menuWidget
, &event
);
1140 XtManageChild (menuWidget
);
1142 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1143 // idle-time destroy routine.
1144 // Waiting until this ID changes causes this function to block until
1145 // the menu has been dismissed and the widgets cleaned up.
1146 // In other words, once this routine returns, it is safe to delete
1148 // Ian Brown <ian.brown@printsoft.de>
1150 wxEventLoop evtLoop
;
1152 while (menu
->GetId() == 1)
1154 wxDoEventLoopIteration( evtLoop
);
1162 // ---------------------------------------------------------------------------
1163 // moving and resizing
1164 // ---------------------------------------------------------------------------
1166 bool wxWindow::PreResize()
1172 void wxWindow::DoGetSize(int *x
, int *y
) const
1174 Widget widget
= (Widget
)( !m_drawingArea
? GetTopWidget() :
1175 ( m_borderWidget
? m_borderWidget
:
1176 m_scrolledWindow
? m_scrolledWindow
:
1180 XtVaGetValues( widget
,
1188 void wxWindow::DoGetPosition(int *x
, int *y
) const
1190 Widget widget
= (Widget
)
1192 ( m_borderWidget
? m_borderWidget
: m_scrolledWindow
) :
1196 XtVaGetValues(widget
, XmNx
, &xx
, XmNy
, &yy
, NULL
);
1198 // We may be faking the client origin. So a window that's really at (0, 30)
1199 // may appear (to wxWin apps) to be at (0, 0).
1202 wxPoint
pt(GetParent()->GetClientAreaOrigin());
1211 void wxWindow::DoScreenToClient(int *x
, int *y
) const
1213 Widget widget
= (Widget
) GetClientWidget();
1214 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1215 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1216 Window thisWindow
= XtWindow(widget
);
1221 XTranslateCoordinates(display
, rootWindow
, thisWindow
, xx
, yy
, x
, y
, &childWindow
);
1224 void wxWindow::DoClientToScreen(int *x
, int *y
) const
1226 Widget widget
= (Widget
) GetClientWidget();
1227 Display
*display
= XtDisplay(widget
);
1228 Window rootWindow
= RootWindowOfScreen(XtScreen(widget
));
1229 Window thisWindow
= XtWindow(widget
);
1234 XTranslateCoordinates(display
, thisWindow
, rootWindow
, xx
, yy
, x
, y
, &childWindow
);
1238 // Get size *available for subwindows* i.e. excluding menu bar etc.
1239 void wxWindow::DoGetClientSize(int *x
, int *y
) const
1241 Widget widget
= (Widget
) GetClientWidget();
1243 XtVaGetValues(widget
, XmNwidth
, &xx
, XmNheight
, &yy
, NULL
);
1244 if(x
) *x
= xx
; if(y
) *y
= yy
;
1247 void wxWindow::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
1249 DoSetSizeIntr(x
, y
, width
, height
, sizeFlags
, FALSE
);
1252 void wxWindow::DoSetSizeIntr(int x
, int y
, int width
, int height
,
1253 int sizeFlags
, bool fromCtor
)
1255 // A bit of optimization to help sort out the flickers.
1256 int oldX
= -1, oldY
= -1, oldW
= -1, oldH
= -1;
1259 GetSize(& oldW
, & oldH
);
1260 GetPosition(& oldX
, & oldY
);
1263 if ( !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) )
1271 wxSize
size(-1, -1);
1274 if ( ( sizeFlags
& wxSIZE_AUTO_WIDTH
) && !fromCtor
)
1276 size
= DoGetBestSize();
1287 if( ( sizeFlags
& wxSIZE_AUTO_HEIGHT
) && !fromCtor
)
1289 if( size
.x
== -1 ) size
= DoGetBestSize();
1298 if ( x
!= oldX
|| y
!= oldY
|| width
!= oldW
|| height
!= oldH
1299 || !wxNoOptimize::CanOptimize() )
1305 if (x
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1308 if (y
> -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
1312 flags
|= wxMOVE_WIDTH
;
1315 flags
|= wxMOVE_HEIGHT
;
1317 int xx
= x
; int yy
= y
;
1318 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1320 DoMoveWindow( xx
, yy
, width
, height
);
1322 DoMoveWindowIntr( xx
, yy
, width
, height
, flags
);
1327 Widget widget
= (Widget
) GetTopWidget();
1331 bool managed
= XtIsManaged( widget
);
1333 XtUnmanageChild(widget
);
1337 AdjustForParentClientOrigin(xx
, yy
, sizeFlags
);
1339 DoMoveWindow(xx
, yy
, width
, height
);
1342 XtManageChild(widget
);
1346 void wxWindow::DoSetClientSize(int width
, int height
)
1350 Widget drawingArea
= (Widget
) m_drawingArea
;
1352 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1355 XtVaSetValues(drawingArea
, XmNwidth
, width
, NULL
);
1357 XtVaSetValues(drawingArea
, XmNheight
, height
, NULL
);
1359 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1363 Widget widget
= (Widget
) GetTopWidget();
1366 XtVaSetValues(widget
, XmNwidth
, width
, NULL
);
1368 XtVaSetValues(widget
, XmNheight
, height
, NULL
);
1371 // For implementation purposes - sometimes decorations make the client area
1373 wxPoint
wxWindow::GetClientAreaOrigin() const
1375 return wxPoint(0, 0);
1378 void wxWindow::DoMoveWindowIntr(int xx
, int yy
, int w
, int h
,
1383 Widget drawingArea
= (Widget
) m_drawingArea
;
1384 Widget borderOrScrolled
= m_borderWidget
?
1385 (Widget
) m_borderWidget
:
1386 (Widget
) m_scrolledWindow
;
1388 bool managed
= XtIsManaged(borderOrScrolled
);
1390 XtUnmanageChild (borderOrScrolled
);
1391 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
1393 if (flags
& wxMOVE_X
)
1394 XtVaSetValues (borderOrScrolled
,
1397 if (flags
& wxMOVE_Y
)
1398 XtVaSetValues (borderOrScrolled
,
1402 if (flags
& wxMOVE_WIDTH
)
1406 XtVaSetValues ((Widget
) m_borderWidget
, XmNwidth
, w
, NULL
);
1407 short thick
, margin
;
1408 XtVaGetValues ((Widget
) m_borderWidget
,
1409 XmNshadowThickness
, &thick
,
1410 XmNmarginWidth
, &margin
,
1412 w
-= 2 * (thick
+ margin
);
1415 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNwidth
, w
, NULL
);
1418 if (flags
& wxMOVE_HEIGHT
)
1422 XtVaSetValues ((Widget
) m_borderWidget
, XmNheight
, h
, NULL
);
1423 short thick
, margin
;
1424 XtVaGetValues ((Widget
) m_borderWidget
,
1425 XmNshadowThickness
, &thick
,
1426 XmNmarginHeight
, &margin
,
1428 h
-= 2 * (thick
+ margin
);
1431 XtVaSetValues ((Widget
) m_scrolledWindow
, XmNheight
, h
, NULL
);
1435 XtManageChild (borderOrScrolled
);
1436 XtVaSetValues(drawingArea
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
1443 XtVaSetValues((Widget
)GetTopWidget(),
1452 void wxWindow::DoMoveWindow(int x
, int y
, int width
, int height
)
1454 DoMoveWindowIntr (x
, y
, width
, height
,
1455 wxMOVE_X
|wxMOVE_Y
|wxMOVE_WIDTH
|wxMOVE_HEIGHT
);
1458 // ---------------------------------------------------------------------------
1460 // ---------------------------------------------------------------------------
1462 int wxWindow::GetCharHeight() const
1464 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1466 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1468 int direction
, ascent
, descent
;
1469 XCharStruct overall
;
1470 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1471 &descent
, &overall
);
1473 // return (overall.ascent + overall.descent);
1474 return (ascent
+ descent
);
1477 int wxWindow::GetCharWidth() const
1479 wxCHECK_MSG( m_font
.Ok(), 0, "valid window font needed" );
1481 WXFontStructPtr pFontStruct
= m_font
.GetFontStruct(1.0, GetXDisplay());
1483 int direction
, ascent
, descent
;
1484 XCharStruct overall
;
1485 XTextExtents ((XFontStruct
*) pFontStruct
, "x", 1, &direction
, &ascent
,
1486 &descent
, &overall
);
1488 return overall
.width
;
1491 void wxWindow::GetTextExtent(const wxString
& string
,
1493 int *descent
, int *externalLeading
,
1494 const wxFont
*theFont
) const
1496 wxFont
*fontToUse
= (wxFont
*)theFont
;
1498 fontToUse
= (wxFont
*) & m_font
;
1500 wxCHECK_RET( fontToUse
->Ok(), "valid window font needed" );
1502 WXFontStructPtr pFontStruct
= fontToUse
->GetFontStruct(1.0, GetXDisplay());
1504 int direction
, ascent
, descent2
;
1505 XCharStruct overall
;
1506 int slen
= string
.Len();
1510 XTextExtents16((XFontStruct
*) pFontStruct
, (XChar2b
*) (char*) (const char*) string
, slen
, &direction
,
1511 &ascent
, &descent2
, &overall
);
1514 XTextExtents((XFontStruct
*) pFontStruct
, string
, slen
,
1515 &direction
, &ascent
, &descent2
, &overall
);
1518 *x
= (overall
.width
);
1520 *y
= (ascent
+ descent2
);
1522 *descent
= descent2
;
1523 if (externalLeading
)
1524 *externalLeading
= 0;
1528 // ----------------------------------------------------------------------------
1530 // ----------------------------------------------------------------------------
1532 void wxWindow::AddUpdateRect(int x
, int y
, int w
, int h
)
1534 m_updateRegion
.Union( x
, y
, w
, h
);
1537 void wxWindow::Refresh(bool eraseBack
, const wxRect
*rect
)
1539 m_needsRefresh
= TRUE
;
1540 Display
*display
= XtDisplay((Widget
) GetMainWidget());
1541 Window thisWindow
= XtWindow((Widget
) GetMainWidget());
1543 XExposeEvent dummyEvent
;
1545 GetSize(&width
, &height
);
1547 dummyEvent
.type
= Expose
;
1548 dummyEvent
.display
= display
;
1549 dummyEvent
.send_event
= True
;
1550 dummyEvent
.window
= thisWindow
;
1553 dummyEvent
.x
= rect
->x
;
1554 dummyEvent
.y
= rect
->y
;
1555 dummyEvent
.width
= rect
->width
;
1556 dummyEvent
.height
= rect
->height
;
1562 dummyEvent
.width
= width
;
1563 dummyEvent
.height
= height
;
1565 dummyEvent
.count
= 0;
1569 wxClientDC
dc(this);
1570 wxBrush
backgroundBrush(GetBackgroundColour(), wxSOLID
);
1571 dc
.SetBackground(backgroundBrush
);
1578 XSendEvent(display
, thisWindow
, False
, ExposureMask
, (XEvent
*)&dummyEvent
);
1581 void wxWindow::Clear()
1583 wxClientDC
dc(this);
1584 wxBrush
brush(GetBackgroundColour(), wxSOLID
);
1585 dc
.SetBackground(brush
);
1589 void wxWindow::DoPaint()
1591 //TODO : make a temporary gc so we can do the XCopyArea below
1592 if (m_backingPixmap
&& !m_needsRefresh
)
1596 GC tempGC
= (GC
) dc
.GetBackingGC();
1598 Widget widget
= (Widget
) GetMainWidget();
1603 // We have to test whether it's a wxScrolledWindow (hack!) because
1604 // otherwise we don't know how many pixels have been scrolled. We might
1605 // solve this in the future by defining virtual wxWindow functions to get
1606 // the scroll position in pixels. Or, each kind of scrolled window has to
1607 // implement backing stores itself, using generic wxWindows code.
1608 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(this, wxScrolledWindow
);
1609 if ( scrolledWindow
)
1612 scrolledWindow
->CalcScrolledPosition(0, 0, &x
, &y
);
1618 // TODO: This could be optimized further by only copying the areas in the
1619 // current update region.
1621 // Only blit the part visible in the client area. The backing pixmap
1622 // always starts at 0, 0 but we may be looking at only a portion of it.
1623 wxSize clientArea
= GetClientSize();
1624 int toBlitX
= m_pixmapWidth
- scrollPosX
;
1625 int toBlitY
= m_pixmapHeight
- scrollPosY
;
1627 // Copy whichever is samller, the amount of pixmap we have to copy,
1628 // or the size of the client area.
1629 toBlitX
= wxMin(toBlitX
, clientArea
.x
);
1630 toBlitY
= wxMin(toBlitY
, clientArea
.y
);
1632 // Make sure we're not negative
1633 toBlitX
= wxMax(0, toBlitX
);
1634 toBlitY
= wxMax(0, toBlitY
);
1639 (Pixmap
) m_backingPixmap
,
1642 scrollPosX
, scrollPosY
, // Start at the scroll position
1643 toBlitX
, toBlitY
, // How much of the pixmap to copy
1649 wxWindowDC
dc(this);
1650 // Set an erase event first
1651 wxEraseEvent
eraseEvent(GetId(), &dc
);
1652 eraseEvent
.SetEventObject(this);
1653 GetEventHandler()->ProcessEvent(eraseEvent
);
1655 wxPaintEvent
event(GetId());
1656 event
.SetEventObject(this);
1657 GetEventHandler()->ProcessEvent(event
);
1659 m_needsRefresh
= FALSE
;
1663 // ----------------------------------------------------------------------------
1665 // ----------------------------------------------------------------------------
1667 // Responds to colour changes: passes event on to children.
1668 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent
& event
)
1670 wxWindowList::Node
*node
= GetChildren().GetFirst();
1673 // Only propagate to non-top-level windows
1674 wxWindow
*win
= node
->GetData();
1675 if ( win
->GetParent() )
1677 wxSysColourChangedEvent event2
;
1678 event
.m_eventObject
= win
;
1679 win
->GetEventHandler()->ProcessEvent(event2
);
1682 node
= node
->GetNext();
1686 void wxWindow::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1688 // This calls the UI-update mechanism (querying windows for
1689 // menu/toolbar/control state information)
1690 if (wxUpdateUIEvent::CanUpdate())
1694 // ----------------------------------------------------------------------------
1696 // ----------------------------------------------------------------------------
1698 bool wxWindow::ProcessAccelerator(wxKeyEvent
& event
)
1701 if (!m_acceleratorTable
.Ok())
1704 int count
= m_acceleratorTable
.GetCount();
1705 wxAcceleratorEntry
* entries
= m_acceleratorTable
.GetEntries();
1707 for (i
= 0; i
< count
; i
++)
1709 wxAcceleratorEntry
* entry
= & (entries
[i
]);
1710 if (entry
->MatchesEvent(event
))
1712 // Bingo, we have a match. Now find a control that matches the
1713 // entry command id.
1715 // Need to go up to the top of the window hierarchy, since it might
1716 // be e.g. a menu item
1717 wxWindow
* parent
= this;
1718 while ( parent
&& !parent
->IsTopLevel() )
1719 parent
= parent
->GetParent();
1724 wxFrame
* frame
= wxDynamicCast(parent
, wxFrame
);
1728 // Try for a menu command
1729 if (frame
->GetMenuBar())
1731 wxMenuItem
* item
= frame
->GetMenuBar()->FindItem(entry
->GetCommand());
1734 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, entry
->GetCommand());
1735 commandEvent
.SetEventObject(frame
);
1737 // If ProcessEvent returns TRUE (it was handled), then
1738 // the calling code will skip the event handling.
1739 return frame
->GetEventHandler()->ProcessEvent(commandEvent
);
1745 // Find a child matching the command id
1746 wxWindow
* child
= parent
->FindWindow(entry
->GetCommand());
1752 // Now we process those kinds of windows that we can.
1753 // For now, only buttons.
1754 if ( wxDynamicCast(child
, wxButton
) )
1756 wxCommandEvent
commandEvent (wxEVT_COMMAND_BUTTON_CLICKED
, child
->GetId());
1757 commandEvent
.SetEventObject(child
);
1758 return child
->GetEventHandler()->ProcessEvent(commandEvent
);
1766 // We didn't match the key event against an accelerator.
1770 // ============================================================================
1771 // Motif-specific stuff from here on
1772 // ============================================================================
1774 // ----------------------------------------------------------------------------
1775 // function which maintain the global hash table mapping Widgets to wxWindows
1776 // ----------------------------------------------------------------------------
1778 bool wxAddWindowToTable(Widget w
, wxWindow
*win
)
1780 wxWindow
*oldItem
= NULL
;
1781 if ((oldItem
= (wxWindow
*)wxWidgetHashTable
->Get ((long) w
)))
1783 wxLogDebug("Widget table clash: new widget is %ld, %s",
1784 (long)w
, win
->GetClassInfo()->GetClassName());
1788 wxWidgetHashTable
->Put((long) w
, win
);
1790 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1791 (WXWidget
)w
, win
, win
->GetClassInfo()->GetClassName());
1796 wxWindow
*wxGetWindowFromTable(Widget w
)
1798 return (wxWindow
*)wxWidgetHashTable
->Get((long) w
);
1801 void wxDeleteWindowFromTable(Widget w
)
1803 wxLogTrace("widget", "Widget 0x%p", (WXWidget
)w
);
1805 wxWidgetHashTable
->Delete((long)w
);
1808 // ----------------------------------------------------------------------------
1809 // add/remove window from the table
1810 // ----------------------------------------------------------------------------
1812 // Add to hash table, add event handler
1813 bool wxWindow::AttachWidget (wxWindow
* WXUNUSED(parent
), WXWidget mainWidget
,
1814 WXWidget formWidget
, int x
, int y
, int width
, int height
)
1816 wxAddWindowToTable((Widget
) mainWidget
, this);
1817 XtAddEventHandler( (Widget
) mainWidget
,
1818 ButtonPressMask
| ButtonReleaseMask
1819 | PointerMotionMask
,
1821 wxPanelItemEventHandler
,
1827 XtOverrideTranslations ((Widget
) mainWidget
,
1828 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1829 XtFree ((char *) ptr
);
1832 // Some widgets have a parent form widget, e.g. wxRadioBox
1835 if (!wxAddWindowToTable((Widget
) formWidget
, this))
1839 XtOverrideTranslations ((Widget
) formWidget
,
1840 ptr
= XtParseTranslationTable ("<Configure>: resize()"));
1841 XtFree ((char *) ptr
);
1848 SetSize (x
, y
, width
, height
);
1853 // Remove event handler, remove from hash table
1854 bool wxWindow::DetachWidget(WXWidget widget
)
1856 XtRemoveEventHandler( (Widget
) widget
,
1857 ButtonPressMask
| ButtonReleaseMask
1858 | PointerMotionMask
,
1860 wxPanelItemEventHandler
,
1863 wxDeleteWindowFromTable((Widget
) widget
);
1867 // ----------------------------------------------------------------------------
1868 // Motif-specific accessors
1869 // ----------------------------------------------------------------------------
1871 // Get the underlying X window
1872 WXWindow
wxWindow::GetXWindow() const
1874 Widget wMain
= (Widget
)GetMainWidget();
1876 return (WXWindow
) XtWindow(wMain
);
1878 return (WXWindow
) 0;
1881 // Get the underlying X display
1882 WXDisplay
*wxWindow::GetXDisplay() const
1884 Widget wMain
= (Widget
)GetMainWidget();
1886 return (WXDisplay
*) XtDisplay(wMain
);
1888 return (WXDisplay
*) NULL
;
1891 WXWidget
wxWindow::GetMainWidget() const
1894 return m_drawingArea
;
1896 return m_mainWidget
;
1899 WXWidget
wxWindow::GetClientWidget() const
1901 if (m_drawingArea
!= (WXWidget
) 0)
1902 return m_drawingArea
;
1904 return GetMainWidget();
1907 WXWidget
wxWindow::GetTopWidget() const
1909 return GetMainWidget();
1912 WXWidget
wxWindow::GetLabelWidget() const
1914 return GetMainWidget();
1917 // ----------------------------------------------------------------------------
1919 // ----------------------------------------------------------------------------
1921 // All widgets should have this as their resize proc.
1922 // OnSize sent to wxWindow via client data.
1923 void wxWidgetResizeProc(Widget w
, XConfigureEvent
*WXUNUSED(event
),
1924 String
WXUNUSED(args
)[], int *WXUNUSED(num_args
))
1926 wxWindow
*win
= wxGetWindowFromTable(w
);
1930 if (win
->PreResize())
1933 win
->GetSize(&width
, &height
);
1934 wxSizeEvent
sizeEvent(wxSize(width
, height
), win
->GetId());
1935 sizeEvent
.SetEventObject(win
);
1936 win
->GetEventHandler()->ProcessEvent(sizeEvent
);
1940 static void wxCanvasRepaintProc(Widget drawingArea
,
1941 XtPointer clientData
,
1942 XmDrawingAreaCallbackStruct
* cbs
)
1944 if (!wxGetWindowFromTable(drawingArea
))
1947 XEvent
* event
= cbs
->event
;
1948 wxWindow
* win
= (wxWindow
*) clientData
;
1950 switch (event
->type
)
1954 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
1955 event
->xexpose
.width
, event
->xexpose
.height
);
1957 if (event
-> xexpose
.count
== 0)
1966 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1967 static void wxCanvasEnterLeave(Widget drawingArea
,
1968 XtPointer
WXUNUSED(clientData
),
1969 XCrossingEvent
* event
)
1971 XmDrawingAreaCallbackStruct cbs
;
1974 ((XCrossingEvent
&) ev
) = *event
;
1976 cbs
.reason
= XmCR_INPUT
;
1979 wxCanvasInputEvent(drawingArea
, (XtPointer
) NULL
, &cbs
);
1982 // Fix to make it work under Motif 1.0 (!)
1983 static void wxCanvasMotionEvent (Widget
WXUNUSED(drawingArea
),
1984 XButtonEvent
*WXUNUSED(event
))
1986 #if XmVersion <= 1000
1987 XmDrawingAreaCallbackStruct cbs
;
1990 ev
= *((XEvent
*) event
);
1991 cbs
.reason
= XmCR_INPUT
;
1994 wxCanvasInputEvent (drawingArea
, (XtPointer
) NULL
, &cbs
);
1995 #endif // XmVersion <= 1000
1998 static void wxCanvasInputEvent(Widget drawingArea
,
1999 XtPointer
WXUNUSED(data
),
2000 XmDrawingAreaCallbackStruct
* cbs
)
2002 wxWindow
*canvas
= wxGetWindowFromTable(drawingArea
);
2008 if (cbs
->reason
!= XmCR_INPUT
)
2011 local_event
= *(cbs
->event
); // We must keep a copy!
2013 switch (local_event
.xany
.type
)
2021 wxMouseEvent wxevent
;
2022 if(wxTranslateMouseEvent(wxevent
, canvas
, drawingArea
, &local_event
))
2024 canvas
->GetEventHandler()->ProcessEvent(wxevent
);
2030 wxKeyEvent
event (wxEVT_CHAR
);
2031 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2033 // Implement wxFrame::OnCharHook by checking ancestor.
2034 wxWindow
*parent
= canvas
;
2035 while (parent
&& !parent
->IsTopLevel())
2036 parent
= parent
->GetParent();
2040 event
.SetEventType(wxEVT_CHAR_HOOK
);
2041 if (parent
->GetEventHandler()->ProcessEvent(event
))
2045 // For simplicity, OnKeyDown is the same as OnChar
2046 // TODO: filter modifier key presses from OnChar
2047 event
.SetEventType(wxEVT_KEY_DOWN
);
2049 // Only process OnChar if OnKeyDown didn't swallow it
2050 if (!canvas
->GetEventHandler()->ProcessEvent (event
))
2052 event
.SetEventType(wxEVT_CHAR
);
2053 canvas
->GetEventHandler()->ProcessEvent (event
);
2060 wxKeyEvent
event (wxEVT_KEY_UP
);
2061 if (wxTranslateKeyEvent (event
, canvas
, (Widget
) 0, &local_event
))
2063 canvas
->GetEventHandler()->ProcessEvent (event
);
2069 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2071 wxFocusEvent
event(wxEVT_SET_FOCUS
, canvas
->GetId());
2072 event
.SetEventObject(canvas
);
2073 canvas
->GetEventHandler()->ProcessEvent(event
);
2079 if (local_event
.xfocus
.detail
!= NotifyPointer
)
2081 wxFocusEvent
event(wxEVT_KILL_FOCUS
, canvas
->GetId());
2082 event
.SetEventObject(canvas
);
2083 canvas
->GetEventHandler()->ProcessEvent(event
);
2092 static void wxPanelItemEventHandler(Widget wid
,
2093 XtPointer
WXUNUSED(client_data
),
2095 Boolean
*continueToDispatch
)
2097 // Widget can be a label or the actual widget.
2099 wxWindow
*window
= wxGetWindowFromTable(wid
);
2102 wxMouseEvent
wxevent(0);
2103 if (wxTranslateMouseEvent(wxevent
, window
, wid
, event
))
2105 window
->GetEventHandler()->ProcessEvent(wxevent
);
2109 // TODO: probably the key to allowing default behaviour to happen. Say we
2110 // set a m_doDefault flag to FALSE at the start of this function. Then in
2111 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2112 // TRUE, indicating that default processing can happen. Thus, behaviour can
2113 // appear to be overridden just by adding an event handler and not calling
2114 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2115 // way of handling drawing area events, to simplify things.
2116 *continueToDispatch
= True
;
2119 static void wxScrollBarCallback(Widget scrollbar
,
2120 XtPointer clientData
,
2121 XmScrollBarCallbackStruct
*cbs
)
2123 wxWindow
*win
= wxGetWindowFromTable(scrollbar
);
2124 wxOrientation orientation
= (wxOrientation
)(int)clientData
;
2126 wxEventType eventType
= wxEVT_NULL
;
2127 switch (cbs
->reason
)
2129 case XmCR_INCREMENT
:
2131 eventType
= wxEVT_SCROLLWIN_LINEDOWN
;
2134 case XmCR_DECREMENT
:
2136 eventType
= wxEVT_SCROLLWIN_LINEUP
;
2141 eventType
= wxEVT_SCROLLWIN_THUMBTRACK
;
2144 case XmCR_VALUE_CHANGED
:
2146 eventType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
2149 case XmCR_PAGE_INCREMENT
:
2151 eventType
= wxEVT_SCROLLWIN_PAGEDOWN
;
2154 case XmCR_PAGE_DECREMENT
:
2156 eventType
= wxEVT_SCROLLWIN_PAGEUP
;
2161 eventType
= wxEVT_SCROLLWIN_TOP
;
2164 case XmCR_TO_BOTTOM
:
2166 eventType
= wxEVT_SCROLLWIN_BOTTOM
;
2171 // Should never get here
2172 wxFAIL_MSG("Unknown scroll event.");
2177 wxScrollWinEvent
event(eventType
,
2180 event
.SetEventObject( win
);
2181 win
->GetEventHandler()->ProcessEvent(event
);
2184 // For repainting arbitrary windows
2185 void wxUniversalRepaintProc(Widget w
, XtPointer
WXUNUSED(c_data
), XEvent
*event
, char *)
2190 wxWindow
* win
= wxGetWindowFromTable(w
);
2194 switch(event
-> type
)
2198 window
= (Window
) win
-> GetXWindow();
2199 display
= (Display
*) win
-> GetXDisplay();
2201 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
2202 event
->xexpose
.width
, event
->xexpose
.height
);
2204 if (event
-> xexpose
.count
== 0)
2214 // ----------------------------------------------------------------------------
2215 // TranslateXXXEvent() functions
2216 // ----------------------------------------------------------------------------
2218 bool wxTranslateMouseEvent(wxMouseEvent
& wxevent
, wxWindow
*win
,
2219 Widget widget
, XEvent
*xevent
)
2221 switch (xevent
->xany
.type
)
2226 fprintf(stderr
, "Widget 0x%p <-> window %p (%s), %s\n",
2227 (WXWidget
)widget
, win
, win
->GetClassInfo()->GetClassName(),
2228 (xevent
->xany
.type
== EnterNotify
? "ENTER" : "LEAVE"));
2234 wxEventType eventType
= wxEVT_NULL
;
2236 if (xevent
->xany
.type
== LeaveNotify
)
2238 eventType
= wxEVT_LEAVE_WINDOW
;
2240 if (xevent
->xany
.type
== EnterNotify
)
2242 eventType
= wxEVT_ENTER_WINDOW
;
2244 else if (xevent
->xany
.type
== MotionNotify
)
2246 eventType
= wxEVT_MOTION
;
2248 else if (xevent
->xany
.type
== ButtonPress
)
2250 wxevent
.SetTimestamp(xevent
->xbutton
.time
);
2252 if (xevent
->xbutton
.button
== Button1
)
2254 eventType
= wxEVT_LEFT_DOWN
;
2257 else if (xevent
->xbutton
.button
== Button2
)
2259 eventType
= wxEVT_MIDDLE_DOWN
;
2262 else if (xevent
->xbutton
.button
== Button3
)
2264 eventType
= wxEVT_RIGHT_DOWN
;
2268 // check for a double click
2270 long dclickTime
= XtGetMultiClickTime(xevent
->xany
.display
);
2271 long ts
= wxevent
.GetTimestamp();
2273 int buttonLast
= win
->GetLastClickedButton();
2274 long lastTS
= win
->GetLastClickTime();
2275 if ( buttonLast
&& buttonLast
== button
&&
2276 (ts
- lastTS
) < dclickTime
)
2279 win
->SetLastClick(0, ts
);
2280 if ( eventType
== wxEVT_LEFT_DOWN
)
2281 eventType
= wxEVT_LEFT_DCLICK
;
2282 else if ( eventType
== wxEVT_MIDDLE_DOWN
)
2283 eventType
= wxEVT_MIDDLE_DCLICK
;
2284 else if ( eventType
== wxEVT_RIGHT_DOWN
)
2285 eventType
= wxEVT_RIGHT_DCLICK
;
2289 // not fast enough or different button
2290 win
->SetLastClick(button
, ts
);
2293 else if (xevent
->xany
.type
== ButtonRelease
)
2295 if (xevent
->xbutton
.button
== Button1
)
2297 eventType
= wxEVT_LEFT_UP
;
2299 else if (xevent
->xbutton
.button
== Button2
)
2301 eventType
= wxEVT_MIDDLE_UP
;
2303 else if (xevent
->xbutton
.button
== Button3
)
2305 eventType
= wxEVT_RIGHT_UP
;
2315 wxevent
.SetEventType(eventType
);
2318 XtVaGetValues(widget
, XmNx
, &x1
, XmNy
, &y1
, NULL
);
2321 win
->GetPosition(&x2
, &y2
);
2323 // The button x/y must be translated to wxWindows
2324 // window space - the widget might be a label or button,
2328 if (widget
!= (Widget
)win
->GetMainWidget())
2334 wxevent
.m_x
= xevent
->xbutton
.x
+ dx
;
2335 wxevent
.m_y
= xevent
->xbutton
.y
+ dy
;
2337 wxevent
.m_leftDown
= ((eventType
== wxEVT_LEFT_DOWN
)
2338 || (event_left_is_down (xevent
)
2339 && (eventType
!= wxEVT_LEFT_UP
)));
2340 wxevent
.m_middleDown
= ((eventType
== wxEVT_MIDDLE_DOWN
)
2341 || (event_middle_is_down (xevent
)
2342 && (eventType
!= wxEVT_MIDDLE_UP
)));
2343 wxevent
.m_rightDown
= ((eventType
== wxEVT_RIGHT_DOWN
)
2344 || (event_right_is_down (xevent
)
2345 && (eventType
!= wxEVT_RIGHT_UP
)));
2347 wxevent
.m_shiftDown
= xevent
->xbutton
.state
& ShiftMask
;
2348 wxevent
.m_controlDown
= xevent
->xbutton
.state
& ControlMask
;
2349 wxevent
.m_altDown
= xevent
->xbutton
.state
& Mod3Mask
;
2350 wxevent
.m_metaDown
= xevent
->xbutton
.state
& Mod1Mask
;
2352 wxevent
.SetId(win
->GetId());
2353 wxevent
.SetEventObject(win
);
2361 bool wxTranslateKeyEvent(wxKeyEvent
& wxevent
, wxWindow
*win
,
2362 Widget
WXUNUSED(widget
), XEvent
*xevent
)
2364 switch (xevent
->xany
.type
)
2372 (void) XLookupString((XKeyEvent
*)xevent
, buf
, 20, &keySym
, NULL
);
2373 int id
= wxCharCodeXToWX (keySym
);
2374 // id may be WXK_xxx code - these are outside ASCII range, so we
2375 // can't just use toupper() on id
2376 if (id
>= 'a' && id
<= 'z')
2379 if (xevent
->xkey
.state
& ShiftMask
)
2380 wxevent
.m_shiftDown
= TRUE
;
2381 if (xevent
->xkey
.state
& ControlMask
)
2382 wxevent
.m_controlDown
= TRUE
;
2383 if (xevent
->xkey
.state
& Mod3Mask
)
2384 wxevent
.m_altDown
= TRUE
;
2385 if (xevent
->xkey
.state
& Mod1Mask
)
2386 wxevent
.m_metaDown
= TRUE
;
2387 wxevent
.SetEventObject(win
);
2388 wxevent
.m_keyCode
= id
;
2389 wxevent
.SetTimestamp(xevent
->xkey
.time
);
2391 wxevent
.m_x
= xevent
->xbutton
.x
;
2392 wxevent
.m_y
= xevent
->xbutton
.y
;
2406 // ----------------------------------------------------------------------------
2408 // ----------------------------------------------------------------------------
2410 #define YAllocColor XAllocColor
2411 XColor g_itemColors
[5];
2412 int wxComputeColours (Display
*display
, wxColour
* back
, wxColour
* fore
)
2415 static XmColorProc colorProc
;
2417 result
= wxNO_COLORS
;
2421 g_itemColors
[0].red
= (((long) back
->Red ()) << 8);
2422 g_itemColors
[0].green
= (((long) back
->Green ()) << 8);
2423 g_itemColors
[0].blue
= (((long) back
->Blue ()) << 8);
2424 g_itemColors
[0].flags
= DoRed
| DoGreen
| DoBlue
;
2425 if (colorProc
== (XmColorProc
) NULL
)
2427 // Get a ptr to the actual function
2428 colorProc
= XmSetColorCalculation ((XmColorProc
) NULL
);
2429 // And set it back to motif.
2430 XmSetColorCalculation (colorProc
);
2432 (*colorProc
) (&g_itemColors
[wxBACK_INDEX
],
2433 &g_itemColors
[wxFORE_INDEX
],
2434 &g_itemColors
[wxSELE_INDEX
],
2435 &g_itemColors
[wxTOPS_INDEX
],
2436 &g_itemColors
[wxBOTS_INDEX
]);
2437 result
= wxBACK_COLORS
;
2441 g_itemColors
[wxFORE_INDEX
].red
= (((long) fore
->Red ()) << 8);
2442 g_itemColors
[wxFORE_INDEX
].green
= (((long) fore
->Green ()) << 8);
2443 g_itemColors
[wxFORE_INDEX
].blue
= (((long) fore
->Blue ()) << 8);
2444 g_itemColors
[wxFORE_INDEX
].flags
= DoRed
| DoGreen
| DoBlue
;
2445 if (result
== wxNO_COLORS
)
2446 result
= wxFORE_COLORS
;
2449 Display
*dpy
= display
;
2450 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
2454 /* 5 Colours to allocate */
2455 for (int i
= 0; i
< 5; i
++)
2456 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[i
]))
2457 result
= wxNO_COLORS
;
2461 /* Only 1 colour to allocate */
2462 if (!YAllocColor (dpy
, cmap
, &g_itemColors
[wxFORE_INDEX
]))
2463 result
= wxNO_COLORS
;
2469 // Changes the foreground and background colours to be derived from the current
2470 // background colour. To change the foreground colour, you must call
2471 // SetForegroundColour explicitly.
2472 void wxWindow::ChangeBackgroundColour()
2474 WXWidget mainWidget
= GetMainWidget();
2476 wxDoChangeBackgroundColour(mainWidget
, m_backgroundColour
);
2479 void wxWindow::ChangeForegroundColour()
2481 WXWidget mainWidget
= GetMainWidget();
2483 wxDoChangeForegroundColour(mainWidget
, m_foregroundColour
);
2484 if ( m_scrolledWindow
&& mainWidget
!= m_scrolledWindow
)
2485 wxDoChangeForegroundColour(m_scrolledWindow
, m_foregroundColour
);
2488 bool wxWindow::SetBackgroundColour(const wxColour
& col
)
2490 if ( !wxWindowBase::SetBackgroundColour(col
) )
2493 ChangeBackgroundColour();
2498 bool wxWindow::SetForegroundColour(const wxColour
& col
)
2500 if ( !wxWindowBase::SetForegroundColour(col
) )
2503 ChangeForegroundColour();
2508 void wxWindow::ChangeFont(bool keepOriginalSize
)
2510 // Note that this causes the widget to be resized back
2511 // to its original size! We therefore have to set the size
2512 // back again. TODO: a better way in Motif?
2513 Widget w
= (Widget
) GetLabelWidget(); // Usually the main widget
2514 if (w
&& m_font
.Ok())
2516 int width
, height
, width1
, height1
;
2517 GetSize(& width
, & height
);
2519 wxDoChangeFont( GetLabelWidget(), m_font
);
2521 GetSize(& width1
, & height1
);
2522 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
2524 SetSize(-1, -1, width
, height
);
2529 // ----------------------------------------------------------------------------
2531 // ----------------------------------------------------------------------------
2533 wxWindow
*wxGetActiveWindow()
2536 wxFAIL_MSG("Not implemented");
2541 wxWindow
*wxWindowBase::GetCapture()
2543 return (wxWindow
*)g_captureWindow
;
2547 // Find the wxWindow at the current mouse position, returning the mouse
2549 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
2551 return wxFindWindowAtPoint(wxGetMousePosition());
2554 // Get the current mouse position.
2555 wxPoint
wxGetMousePosition()
2557 Display
*display
= wxGlobalDisplay();
2558 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
2559 Window rootReturn
, childReturn
;
2560 int rootX
, rootY
, winX
, winY
;
2561 unsigned int maskReturn
;
2563 XQueryPointer (display
,
2567 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
2568 return wxPoint(rootX
, rootY
);
2572 // ----------------------------------------------------------------------------
2573 // wxNoOptimize: switch off size optimization
2574 // ----------------------------------------------------------------------------
2576 int wxNoOptimize::ms_count
= 0;