1. added wxEvtHandler::SafelyProcessEvent() and wxWindow::HandleWindowEvent() to...
[wxWidgets.git] / src / motif / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/window.cpp
3 // Purpose: wxWindow
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __VMS
24 #define XtDisplay XTDISPLAY
25 #define XtWindow XTWINDOW
26 #define XtScreen XTSCREEN
27 #endif
28
29 #ifndef WX_PRECOMP
30 #include "wx/hash.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/utils.h"
34 #include "wx/frame.h"
35 #include "wx/dc.h"
36 #include "wx/dcclient.h"
37 #include "wx/button.h"
38 #include "wx/menu.h"
39 #include "wx/settings.h"
40 #include "wx/scrolwin.h"
41 #include "wx/layout.h"
42 #include "wx/menuitem.h"
43 #include "wx/module.h"
44 #endif
45
46 #include "wx/evtloop.h"
47
48 #if wxUSE_DRAG_AND_DROP
49 #include "wx/dnd.h"
50 #endif
51
52 // DoSetSizeIntr and DoMoveWindowIntr
53 // PROBLEM:
54 // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl
55 // did not work and/or segfaulted because
56 // 1) wxWindow::Create calls SetSize,
57 // which results in a call to DoSetSize much earlier than in the other ports
58 // 2) if wxWindow::Create is called (wxControl::Create calls it)
59 // then DoSetSize is never called, causing layout problems in composite
60 // controls
61 //
62 // SOLUTION:
63 // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition,
64 // DoSetPosition directly or indirectly from wxWindow::Create
65 // 2) call DoMoveWindow from DoSetSize, allowing controls to override it
66
67 #ifdef __VMS__
68 #pragma message disable nosimpint
69 #endif
70 #include <Xm/Xm.h>
71
72 #include <Xm/DrawingA.h>
73 #include <Xm/ScrolledW.h>
74 #include <Xm/ScrollBar.h>
75 #include <Xm/Frame.h>
76 #include <Xm/Label.h>
77 #include <Xm/RowColumn.h> // for XmMenuPosition
78 #ifdef __VMS__
79 #pragma message enable nosimpint
80 #endif
81
82 #include "wx/motif/private.h"
83
84 #include <string.h>
85
86 // ----------------------------------------------------------------------------
87 // global variables for this module
88 // ----------------------------------------------------------------------------
89
90 extern wxHashTable *wxWidgetHashTable;
91 static wxWindow* g_captureWindow = NULL;
92
93
94 // ----------------------------------------------------------------------------
95 // private functions
96 // ----------------------------------------------------------------------------
97
98 static void wxCanvasRepaintProc(Widget, XtPointer, XmDrawingAreaCallbackStruct * cbs);
99 static void wxCanvasInputEvent(Widget drawingArea, XtPointer data, XmDrawingAreaCallbackStruct * cbs);
100 static void wxCanvasMotionEvent(Widget, XButtonEvent * event);
101 static void wxCanvasEnterLeave(Widget drawingArea, XtPointer clientData, XCrossingEvent * event);
102 static void wxScrollBarCallback(Widget widget, XtPointer clientData,
103 XmScrollBarCallbackStruct *cbs);
104 static void wxPanelItemEventHandler(Widget wid,
105 XtPointer client_data,
106 XEvent* event,
107 Boolean *continueToDispatch);
108
109 // unused for now
110 #if 0
111
112 // Helper function for 16-bit fonts
113 static int str16len(const char *s)
114 {
115 int count = 0;
116
117 while (s[0] && s[1]) {
118 count++;
119 s += 2;
120 }
121
122 return count;
123 }
124
125 #endif // 0
126
127 // ----------------------------------------------------------------------------
128 // macros
129 // ----------------------------------------------------------------------------
130
131 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
132 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
133 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
134
135 // ----------------------------------------------------------------------------
136 // event tables
137 // ----------------------------------------------------------------------------
138
139 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
140
141 BEGIN_EVENT_TABLE(wxWindow, wxWindowBase)
142 EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged)
143 END_EVENT_TABLE()
144
145 // ============================================================================
146 // implementation
147 // ============================================================================
148
149 // ----------------------------------------------------------------------------
150 // helper functions
151 // ----------------------------------------------------------------------------
152
153 void wxWindow::UnmanageAndDestroy(WXWidget widget)
154 {
155 Widget w = (Widget)widget;
156 if ( w )
157 {
158 XtUnmanageChild(w);
159 XtDestroyWidget(w);
160 }
161 }
162
163 bool wxWindow::MapOrUnmap(WXWidget widget, bool domap)
164 {
165 Widget w = (Widget)widget;
166 if ( !w )
167 return false;
168
169 // Rationale: a lot of common operations (including but not
170 // limited to moving, resizing and appending items to a listbox)
171 // unmamange the widget, do their work, then manage it again.
172 // This means that, for example adding an item to a listbox will show it,
173 // or that most controls are shown every time they are moved or resized!
174 XtSetMappedWhenManaged( w, domap );
175
176 // if the widget is not unmanaged, it still intercepts
177 // mouse events, even if it is not mapped (and hence invisible)
178 if ( domap )
179 {
180 XtManageChild(w);
181 // XtMapWidget(w);
182 }
183 else
184 {
185 XtUnmanageChild(w);
186 // XtUnmapWidget(w);
187 }
188
189 return true;
190 }
191
192 // ----------------------------------------------------------------------------
193 // constructors
194 // ----------------------------------------------------------------------------
195
196 void wxWindow::Init()
197 {
198 // Motif-specific
199 m_needsRefresh = true;
200 m_mainWidget = (WXWidget) 0;
201
202 m_winCaptured = false;
203
204 m_isShown = true;
205
206 m_hScrollBar =
207 m_vScrollBar =
208 m_borderWidget =
209 m_scrolledWindow =
210 m_drawingArea = (WXWidget) 0;
211
212 m_scrollPosX =
213 m_scrollPosY = 0;
214
215 m_backingPixmap = (WXPixmap) 0;
216 m_pixmapWidth =
217 m_pixmapHeight = 0;
218
219 m_pixmapOffsetX =
220 m_pixmapOffsetY = 0;
221
222 m_lastTS = 0;
223 m_lastButton = 0;
224 }
225
226 // real construction (Init() must have been called before!)
227 bool wxWindow::Create(wxWindow *parent, wxWindowID id,
228 const wxPoint& pos,
229 const wxSize& size,
230 long style,
231 const wxString& name)
232 {
233 // Get default border
234 wxBorder border = GetBorder(style);
235 style &= ~wxBORDER_MASK;
236 style |= border;
237
238 wxCHECK_MSG( parent, false, "can't create wxWindow without parent" );
239
240 CreateBase(parent, id, pos, size, style, wxDefaultValidator, name);
241
242 parent->AddChild(this);
243 PreCreation();
244
245 //// TODO: we should probably optimize by only creating a
246 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
247 //// But for now, let's simplify things by always creating the
248 //// drawing area, since otherwise the translations are different.
249
250 // New translations for getting mouse motion feedback
251 static const String translations = wxMOTIF_STR(
252 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
253 <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
254 <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
255 <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
256 <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
257 <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
258 <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
259 <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
260 <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
261 <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
262 <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
263 <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
264 <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
265 <Key>: DrawingAreaInput()");
266
267 XtActionsRec actions[1];
268 actions[0].string = wxMOTIF_STR("wxCanvasMotionEvent");
269 actions[0].proc = (XtActionProc) wxCanvasMotionEvent;
270 XtAppAddActions ((XtAppContext) wxTheApp->GetAppContext(), actions, 1);
271
272 Widget parentWidget = (Widget) parent->GetClientWidget();
273 m_borderWidget = wxCreateBorderWidget( (WXWidget)parentWidget, style );
274
275 m_scrolledWindow = (WXWidget)XtVaCreateManagedWidget
276 (
277 "scrolledWindow",
278 xmScrolledWindowWidgetClass,
279 m_borderWidget ? (Widget) m_borderWidget
280 : parentWidget,
281 XmNresizePolicy, XmRESIZE_NONE,
282 XmNspacing, 0,
283 XmNscrollingPolicy, XmAPPLICATION_DEFINED,
284 //XmNscrollBarDisplayPolicy, XmAS_NEEDED,
285 NULL
286 );
287
288 XtTranslations ptr = XtParseTranslationTable(translations);
289 m_drawingArea = (WXWidget)XtVaCreateWidget
290 (
291 name,
292 xmDrawingAreaWidgetClass, (Widget) m_scrolledWindow,
293 XmNunitType, XmPIXELS,
294 // XmNresizePolicy, XmRESIZE_ANY,
295 XmNresizePolicy, XmRESIZE_NONE,
296 XmNmarginHeight, 0,
297 XmNmarginWidth, 0,
298 XmNtranslations, ptr,
299 NULL
300 );
301 XtFree((char *) ptr);
302
303 #if 0
304 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS)
305 {
306 ptr = XtParseTranslationTable ("<Key>: DrawingAreaInput()");
307 XtOverrideTranslations ((Widget) m_drawingArea, ptr);
308 XtFree ((char *) ptr);
309 }
310 #endif // 0
311
312 wxAddWindowToTable((Widget) m_drawingArea, this);
313 wxAddWindowToTable((Widget) m_scrolledWindow, this);
314
315 // This order is very important in Motif 1.2.1
316 XtRealizeWidget ((Widget) m_scrolledWindow);
317 XtRealizeWidget ((Widget) m_drawingArea);
318 XtManageChild ((Widget) m_drawingArea);
319
320 ptr = XtParseTranslationTable("<Configure>: resize()");
321 XtOverrideTranslations((Widget) m_drawingArea, ptr);
322 XtFree ((char *) ptr);
323
324 XtAddCallback ((Widget) m_drawingArea, XmNexposeCallback, (XtCallbackProc) wxCanvasRepaintProc, (XtPointer) this);
325 XtAddCallback ((Widget) m_drawingArea, XmNinputCallback, (XtCallbackProc) wxCanvasInputEvent, (XtPointer) this);
326
327 XtAddEventHandler(
328 (Widget)m_drawingArea,
329 PointerMotionHintMask | EnterWindowMask |
330 LeaveWindowMask | FocusChangeMask,
331 False,
332 (XtEventHandler) wxCanvasEnterLeave,
333 (XtPointer) this
334 );
335
336 XmScrolledWindowSetAreas(
337 (Widget)m_scrolledWindow,
338 (Widget) 0, (Widget) 0,
339 (Widget) m_drawingArea);
340
341 PostCreation();
342
343 // Without this, the cursor may not be restored properly (e.g. in splitter
344 // sample).
345 SetCursor(*wxSTANDARD_CURSOR);
346 DoSetSizeIntr(pos.x, pos.y, size.x,size.y, wxSIZE_AUTO, true);
347 return true;
348 }
349
350 // Destructor
351 wxWindow::~wxWindow()
352 {
353 if (g_captureWindow == this)
354 g_captureWindow = NULL;
355
356 m_isBeingDeleted = true;
357
358 // Motif-specific actions first
359 WXWidget wMain = GetMainWidget();
360 if ( wMain )
361 {
362 // Removes event handlers
363 DetachWidget(wMain);
364 }
365
366 // If m_drawingArea, we're a fully-fledged window with drawing area,
367 // scrollbars etc. (what wxCanvas used to be)
368 if ( m_drawingArea )
369 {
370 // Destroy children before destroying self
371 DestroyChildren();
372
373 if (m_backingPixmap)
374 XFreePixmap (XtDisplay ((Widget) GetMainWidget()), (Pixmap) m_backingPixmap);
375
376 Widget w = (Widget) m_drawingArea;
377 wxDeleteWindowFromTable(w);
378
379 if (w)
380 {
381 XtDestroyWidget(w);
382 m_drawingArea = (WXWidget) 0;
383 }
384
385 // Only if we're _really_ a canvas (not a dialog box/panel)
386 if (m_scrolledWindow)
387 {
388 wxDeleteWindowFromTable((Widget) m_scrolledWindow);
389 }
390
391 if (m_hScrollBar)
392 {
393 wxDeleteWindowFromTable((Widget) m_hScrollBar);
394 XtUnmanageChild((Widget) m_hScrollBar);
395 }
396 if (m_vScrollBar)
397 {
398 wxDeleteWindowFromTable((Widget) m_vScrollBar);
399 XtUnmanageChild((Widget) m_vScrollBar);
400 }
401
402 if (m_hScrollBar)
403 XtDestroyWidget((Widget) m_hScrollBar);
404 if (m_vScrollBar)
405 XtDestroyWidget((Widget) m_vScrollBar);
406
407 UnmanageAndDestroy(m_scrolledWindow);
408
409 if (m_borderWidget)
410 {
411 XtDestroyWidget ((Widget) m_borderWidget);
412 m_borderWidget = (WXWidget) 0;
413 }
414 }
415 else // Why wasn't this here before? JACS 8/3/2000
416 DestroyChildren();
417
418
419 // Destroy the window
420 if (GetMainWidget())
421 {
422 // If this line (XtDestroyWidget) causes a crash, you may comment it out.
423 // Child widgets will get destroyed automatically when a frame
424 // or dialog is destroyed, but before that you may get some memory
425 // leaks and potential layout problems if you delete and then add
426 // child windows.
427
428 // GRG, Feb/2000: commented this out when adding support for
429 // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported
430 // that this call crashed wxMotif under OS/2, so it seems
431 // that leaving it out is the right thing to do.
432 // SN, Feb/2000: newgrid/griddemo shows why it is needed :-(
433 XtDestroyWidget((Widget) GetMainWidget());
434 SetMainWidget((WXWidget) NULL);
435 }
436 }
437
438 // ----------------------------------------------------------------------------
439 // scrollbar management
440 // ----------------------------------------------------------------------------
441
442 WXWidget wxWindow::DoCreateScrollBar(WXWidget parent,
443 wxOrientation orientation,
444 void (*callback)())
445 {
446 int orient = ( orientation & wxHORIZONTAL ) ? XmHORIZONTAL : XmVERTICAL;
447 Widget sb =
448 XtVaCreateManagedWidget( "scrollBarWidget",
449 xmScrollBarWidgetClass, (Widget)parent,
450 XmNorientation, orient,
451 XmNincrement, 1,
452 XmNvalue, 0,
453 NULL );
454
455 XtPointer o = (XtPointer)orientation;
456 XtCallbackProc cb = (XtCallbackProc)callback;
457
458 XtAddCallback( sb, XmNvalueChangedCallback, cb, o );
459 XtAddCallback( sb, XmNdragCallback, cb, o );
460 XtAddCallback( sb, XmNincrementCallback, cb, o );
461 XtAddCallback( sb, XmNdecrementCallback, cb, o );
462 XtAddCallback( sb, XmNpageIncrementCallback, cb, o );
463 XtAddCallback( sb, XmNpageDecrementCallback, cb, o );
464 XtAddCallback( sb, XmNtoTopCallback, cb, o );
465 XtAddCallback( sb, XmNtoBottomCallback, cb, o );
466
467 return (WXWidget)sb;
468 }
469
470 // Helper function
471 void wxWindow::CreateScrollbar(wxOrientation orientation)
472 {
473 wxCHECK_RET( m_drawingArea, "this window can't have scrollbars" );
474
475 XtVaSetValues( (Widget) m_scrolledWindow,
476 XmNresizePolicy, XmRESIZE_NONE,
477 NULL );
478
479 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
480 // Add scrollbars if required
481 if (orientation == wxHORIZONTAL)
482 {
483 m_hScrollBar = DoCreateScrollBar( m_scrolledWindow, wxHORIZONTAL,
484 (void (*)())wxScrollBarCallback );
485
486 wxDoChangeBackgroundColour(m_hScrollBar, backgroundColour, true);
487
488 XtRealizeWidget( (Widget)m_hScrollBar );
489
490 XtVaSetValues((Widget) m_scrolledWindow,
491 XmNhorizontalScrollBar, (Widget) m_hScrollBar,
492 NULL);
493
494 wxAddWindowToTable( (Widget)m_hScrollBar, this );
495 }
496 else if (orientation == wxVERTICAL)
497 {
498 m_vScrollBar = DoCreateScrollBar( m_scrolledWindow, wxVERTICAL,
499 (void (*)())wxScrollBarCallback );
500
501 wxDoChangeBackgroundColour(m_vScrollBar, backgroundColour, true);
502
503 XtRealizeWidget((Widget)m_vScrollBar);
504
505 XtVaSetValues((Widget) m_scrolledWindow,
506 XmNverticalScrollBar, (Widget) m_vScrollBar,
507 NULL);
508
509 wxAddWindowToTable( (Widget)m_vScrollBar, this );
510 }
511
512 XtVaSetValues( (Widget) m_scrolledWindow,
513 XmNresizePolicy, XmRESIZE_ANY,
514 NULL );
515 }
516
517 void wxWindow::DestroyScrollbar(wxOrientation orientation)
518 {
519 wxCHECK_RET( m_drawingArea, "this window can't have scrollbars" );
520
521 XtVaSetValues((Widget) m_scrolledWindow,
522 XmNresizePolicy, XmRESIZE_NONE,
523 NULL);
524 String stringSB = orientation == wxHORIZONTAL ?
525 XmNhorizontalScrollBar : XmNverticalScrollBar;
526 WXWidget* widgetSB = orientation == wxHORIZONTAL ?
527 &m_hScrollBar : &m_vScrollBar;
528
529 if( *widgetSB )
530 {
531 wxDeleteWindowFromTable( (Widget)*widgetSB );
532 XtDestroyWidget( (Widget)*widgetSB );
533 *widgetSB = (WXWidget)NULL;
534 }
535
536 XtVaSetValues( (Widget)m_scrolledWindow,
537 stringSB, (Widget) 0,
538 NULL );
539
540 XtVaSetValues((Widget) m_scrolledWindow,
541 XmNresizePolicy, XmRESIZE_ANY,
542 NULL);
543 }
544
545 // ---------------------------------------------------------------------------
546 // basic operations
547 // ---------------------------------------------------------------------------
548
549 void wxWindow::SetFocus()
550 {
551 Widget wMain = (Widget) GetMainWidget();
552 XmProcessTraversal(wMain, XmTRAVERSE_CURRENT);
553 XmProcessTraversal((Widget) GetMainWidget(), XmTRAVERSE_CURRENT);
554 }
555
556 // Get the window with the focus
557 wxWindow *wxWindowBase::DoFindFocus()
558 {
559 // TODO Problems:
560 // (1) Can there be multiple focussed widgets in an application?
561 // In which case we need to find the top-level window that's
562 // currently active.
563 // (2) The widget with the focus may not be in the widget table
564 // depending on which widgets I put in the table
565 wxWindow *winFocus = (wxWindow *)NULL;
566 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
567 node;
568 node = node->GetNext() )
569 {
570 wxWindow *win = node->GetData();
571
572 Widget w = XmGetFocusWidget ((Widget) win->GetTopWidget());
573
574 if (w != (Widget) NULL)
575 {
576 winFocus = wxGetWindowFromTable(w);
577 if ( winFocus )
578 break;
579 }
580 }
581
582 return winFocus;
583 }
584
585 bool wxWindow::Enable(bool enable)
586 {
587 if ( !wxWindowBase::Enable(enable) )
588 return false;
589
590 Widget wMain = (Widget)GetMainWidget();
591 if ( wMain )
592 {
593 XtSetSensitive(wMain, enable);
594 XmUpdateDisplay(wMain);
595 }
596
597 return true;
598 }
599
600 bool wxWindow::Show(bool show)
601 {
602 if ( !wxWindowBase::Show(show) )
603 return false;
604
605 if (m_borderWidget || m_scrolledWindow)
606 {
607 MapOrUnmap(m_borderWidget ? m_borderWidget : m_scrolledWindow, show);
608 // MapOrUnmap(m_drawingArea, show);
609 }
610 else
611 {
612 if ( !MapOrUnmap(GetTopWidget(), show) )
613 MapOrUnmap(GetMainWidget(), show);
614 }
615
616 return true;
617 }
618
619 // Raise the window to the top of the Z order
620 void wxWindow::Raise()
621 {
622 Widget wTop = (Widget) GetTopWidget();
623 Window window = XtWindow(wTop);
624 XRaiseWindow(XtDisplay(wTop), window);
625 }
626
627 // Lower the window to the bottom of the Z order
628 void wxWindow::Lower()
629 {
630 Widget wTop = (Widget) GetTopWidget();
631 Window window = XtWindow(wTop);
632 XLowerWindow(XtDisplay(wTop), window);
633 }
634
635 void wxWindow::SetLabel(const wxString& label)
636 {
637 XtVaSetValues((Widget)GetMainWidget(), XmNtitle,
638 (const char*)label.mb_str(), NULL);
639 }
640
641 wxString wxWindow::GetLabel() const
642 {
643 char *label = NULL;
644 XtVaGetValues((Widget)GetMainWidget(), XmNtitle, &label, NULL);
645
646 return wxString(label);
647 }
648
649 void wxWindow::DoCaptureMouse()
650 {
651 g_captureWindow = this;
652 if ( m_winCaptured )
653 return;
654
655 Widget wMain = (Widget)GetMainWidget();
656 if ( wMain )
657 XtAddGrab(wMain, True, False);
658
659 m_winCaptured = true;
660 }
661
662 void wxWindow::DoReleaseMouse()
663 {
664 g_captureWindow = NULL;
665 if ( !m_winCaptured )
666 return;
667
668 Widget wMain = (Widget)GetMainWidget();
669 if ( wMain )
670 XtRemoveGrab(wMain);
671
672 m_winCaptured = false;
673 }
674
675 bool wxWindow::SetFont(const wxFont& font)
676 {
677 if ( !wxWindowBase::SetFont(font) )
678 {
679 // nothing to do
680 return false;
681 }
682
683 ChangeFont();
684
685 return true;
686 }
687
688 bool wxWindow::SetCursor(const wxCursor& cursor)
689 {
690 if ( !wxWindowBase::SetCursor(cursor) )
691 {
692 // no change
693 return false;
694 }
695
696 // wxASSERT_MSG( m_cursor.Ok(),
697 // wxT("cursor must be valid after call to the base version"));
698 const wxCursor* cursor2 = NULL;
699 if (m_cursor.Ok())
700 cursor2 = & m_cursor;
701 else
702 cursor2 = wxSTANDARD_CURSOR;
703
704 WXDisplay *dpy = GetXDisplay();
705 WXCursor x_cursor = cursor2->GetXCursor(dpy);
706
707 Widget w = (Widget) GetMainWidget();
708 Window win = XtWindow(w);
709 XDefineCursor((Display*) dpy, win, (Cursor) x_cursor);
710
711 return true;
712 }
713
714 // Coordinates relative to the window
715 void wxWindow::WarpPointer (int x, int y)
716 {
717 Widget wClient = (Widget)GetClientWidget();
718
719 XWarpPointer(XtDisplay(wClient), None, XtWindow(wClient), 0, 0, 0, 0, x, y);
720 }
721
722 // ---------------------------------------------------------------------------
723 // scrolling stuff
724 // ---------------------------------------------------------------------------
725
726 int wxWindow::GetScrollPos(int orient) const
727 {
728 if (orient == wxHORIZONTAL)
729 return m_scrollPosX;
730 else
731 return m_scrollPosY;
732
733 #if 0
734 Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar);
735 if (scrollBar)
736 {
737 int pos;
738 XtVaGetValues(scrollBar, XmNvalue, &pos, NULL);
739 return pos;
740 }
741 else
742 return 0;
743 #endif // 0
744 }
745
746 // This now returns the whole range, not just the number of positions that we
747 // can scroll.
748 int wxWindow::GetScrollRange(int orient) const
749 {
750 Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient);
751 // CE scintilla windows don't always have these scrollbars
752 // and it tends to pile up a whole bunch of asserts
753 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
754
755 int range = 0;
756 if (scrollBar)
757 XtVaGetValues(scrollBar, XmNmaximum, &range, NULL);
758 return range;
759 }
760
761 int wxWindow::GetScrollThumb(int orient) const
762 {
763 Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient);
764 //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" );
765
766 int thumb = 0;
767 if (scrollBar)
768 XtVaGetValues(scrollBar, XmNsliderSize, &thumb, NULL);
769 return thumb;
770 }
771
772 void wxWindow::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
773 {
774 Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient);
775
776 if ( scrollBar )
777 {
778 XtVaSetValues (scrollBar, XmNvalue, pos, NULL);
779 }
780
781 SetInternalScrollPos((wxOrientation)orient, pos);
782 }
783
784 // New function that will replace some of the above.
785 void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
786 int range, bool WXUNUSED(refresh))
787 {
788 int oldW, oldH;
789 GetSize(& oldW, & oldH);
790
791 if (range == 0)
792 range = 1;
793 if (thumbVisible == 0)
794 thumbVisible = 1;
795
796 if (thumbVisible > range)
797 thumbVisible = range;
798
799 // Save the old state to see if it changed
800 WXWidget oldScrollBar = GetScrollbar((wxOrientation)orient);
801
802 if (orient == wxHORIZONTAL)
803 {
804 if (thumbVisible == range)
805 {
806 if (m_hScrollBar)
807 DestroyScrollbar(wxHORIZONTAL);
808 }
809 else
810 {
811 if (!m_hScrollBar)
812 CreateScrollbar(wxHORIZONTAL);
813 }
814 }
815 if (orient == wxVERTICAL)
816 {
817 if (thumbVisible == range)
818 {
819 if (m_vScrollBar)
820 DestroyScrollbar(wxVERTICAL);
821 }
822 else
823 {
824 if (!m_vScrollBar)
825 CreateScrollbar(wxVERTICAL);
826 }
827 }
828 WXWidget newScrollBar = GetScrollbar((wxOrientation)orient);
829
830 if (oldScrollBar != newScrollBar)
831 {
832 // This is important! Without it, scrollbars misbehave badly.
833 XtUnrealizeWidget((Widget) m_scrolledWindow);
834 XmScrolledWindowSetAreas ((Widget) m_scrolledWindow, (Widget) m_hScrollBar, (Widget) m_vScrollBar, (Widget) m_drawingArea);
835 XtRealizeWidget((Widget) m_scrolledWindow);
836 XtManageChild((Widget) m_scrolledWindow);
837 }
838
839 if (newScrollBar)
840 {
841 XtVaSetValues((Widget) newScrollBar,
842 XmNvalue, pos,
843 XmNminimum, 0,
844 XmNmaximum, range,
845 XmNsliderSize, thumbVisible,
846 NULL);
847 }
848
849 SetInternalScrollPos((wxOrientation)orient, pos);
850
851 int newW, newH;
852 GetSize(& newW, & newH);
853
854 // Adjusting scrollbars can resize the canvas accidentally
855 if (newW != oldW || newH != oldH)
856 SetSize(wxDefaultCoord, wxDefaultCoord, oldW, oldH);
857 }
858
859 // Does a physical scroll
860 void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
861 {
862 int x, y, w, h;
863 if (rect)
864 {
865 // Use specified rectangle
866 x = rect->x; y = rect->y; w = rect->width; h = rect->height;
867 }
868 else
869 {
870 // Use whole client area
871 x = 0; y = 0;
872 GetClientSize(& w, & h);
873 }
874
875 int x1 = (dx >= 0) ? x : x - dx;
876 int y1 = (dy >= 0) ? y : y - dy;
877 int w1 = w - abs(dx);
878 int h1 = h - abs(dy);
879 int x2 = (dx >= 0) ? x + dx : x;
880 int y2 = (dy >= 0) ? y + dy : y;
881
882 wxClientDC dc(this);
883
884 dc.SetLogicalFunction (wxCOPY);
885
886 Widget widget = (Widget) GetMainWidget();
887 Window window = XtWindow(widget);
888 Display* display = XtDisplay(widget);
889
890 XCopyArea(display, window, window, (GC) dc.GetGC(),
891 x1, y1, w1, h1, x2, y2);
892
893 dc.SetAutoSetting(true);
894 wxBrush brush(GetBackgroundColour(), wxSOLID);
895 dc.SetBrush(brush); // FIXME: needed?
896
897 wxWindowList::compatibility_iterator cnode = m_children.GetFirst();
898 while (cnode)
899 {
900 wxWindow *child = cnode->GetData();
901 int sx = 0;
902 int sy = 0;
903 child->GetSize( &sx, &sy );
904 wxPoint pos( child->GetPosition() );
905 child->SetSize( pos.x + dx, pos.y + dy, sx, sy, wxSIZE_ALLOW_MINUS_ONE );
906 cnode = cnode->GetNext();
907 }
908
909 // We'll add rectangles to the list of update rectangles according to which
910 // bits we've exposed.
911 wxList updateRects;
912
913 if (dx > 0)
914 {
915 wxRect *rect = new wxRect;
916 rect->x = x;
917 rect->y = y;
918 rect->width = dx;
919 rect->height = h;
920
921 XFillRectangle(display, window,
922 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
923
924 rect->x = rect->x;
925 rect->y = rect->y;
926 rect->width = rect->width;
927 rect->height = rect->height;
928
929 updateRects.Append((wxObject*) rect);
930 }
931 else if (dx < 0)
932 {
933 wxRect *rect = new wxRect;
934
935 rect->x = x + w + dx;
936 rect->y = y;
937 rect->width = -dx;
938 rect->height = h;
939
940 XFillRectangle(display, window,
941 (GC) dc.GetGC(), rect->x, rect->y, rect->width,
942 rect->height);
943
944 rect->x = rect->x;
945 rect->y = rect->y;
946 rect->width = rect->width;
947 rect->height = rect->height;
948
949 updateRects.Append((wxObject*) rect);
950 }
951 if (dy > 0)
952 {
953 wxRect *rect = new wxRect;
954
955 rect->x = x;
956 rect->y = y;
957 rect->width = w;
958 rect->height = dy;
959
960 XFillRectangle(display, window,
961 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
962
963 rect->x = rect->x;
964 rect->y = rect->y;
965 rect->width = rect->width;
966 rect->height = rect->height;
967
968 updateRects.Append((wxObject*) rect);
969 }
970 else if (dy < 0)
971 {
972 wxRect *rect = new wxRect;
973
974 rect->x = x;
975 rect->y = y + h + dy;
976 rect->width = w;
977 rect->height = -dy;
978
979 XFillRectangle(display, window,
980 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
981
982 rect->x = rect->x;
983 rect->y = rect->y;
984 rect->width = rect->width;
985 rect->height = rect->height;
986
987 updateRects.Append((wxObject*) rect);
988 }
989 dc.SetBrush(wxNullBrush);
990
991 // Now send expose events
992
993 wxList::compatibility_iterator node = updateRects.GetFirst();
994 while (node)
995 {
996 wxRect* rect = (wxRect*) node->GetData();
997 XExposeEvent event;
998
999 event.type = Expose;
1000 event.display = display;
1001 event.send_event = True;
1002 event.window = window;
1003
1004 event.x = rect->x;
1005 event.y = rect->y;
1006 event.width = rect->width;
1007 event.height = rect->height;
1008
1009 event.count = 0;
1010
1011 XSendEvent(display, window, False, ExposureMask, (XEvent *)&event);
1012
1013 node = node->GetNext();
1014
1015 }
1016
1017 // Delete the update rects
1018 node = updateRects.GetFirst();
1019 while (node)
1020 {
1021 wxRect* rect = (wxRect*) node->GetData();
1022 delete rect;
1023 node = node->GetNext();
1024 }
1025
1026 XmUpdateDisplay((Widget) GetMainWidget());
1027 }
1028
1029 // ---------------------------------------------------------------------------
1030 // drag and drop
1031 // ---------------------------------------------------------------------------
1032
1033 #if wxUSE_DRAG_AND_DROP
1034
1035 void wxWindow::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
1036 {
1037 // TODO
1038 }
1039
1040 #endif
1041
1042 // Old style file-manager drag&drop
1043 void wxWindow::DragAcceptFiles(bool WXUNUSED(accept))
1044 {
1045 // TODO
1046 }
1047
1048 // ----------------------------------------------------------------------------
1049 // tooltips
1050 // ----------------------------------------------------------------------------
1051
1052 #if wxUSE_TOOLTIPS
1053
1054 void wxWindow::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
1055 {
1056 // TODO
1057 }
1058
1059 #endif // wxUSE_TOOLTIPS
1060
1061 // ----------------------------------------------------------------------------
1062 // popup menus
1063 // ----------------------------------------------------------------------------
1064
1065 #if wxUSE_MENUS
1066
1067 bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y)
1068 {
1069 if ( x == wxDefaultCoord && y == wxDefaultCoord )
1070 {
1071 wxPoint mouse = ScreenToClient(wxGetMousePosition());
1072 x = mouse.x; y = mouse.y;
1073 }
1074
1075 Widget widget = (Widget) GetMainWidget();
1076
1077 /* The menuId field seems to be usused, so we'll use it to
1078 indicate whether a menu is popped up or not:
1079 0: Not currently created as a popup
1080 -1: Created as a popup, but not active
1081 1: Active popup.
1082 */
1083
1084 if (menu->GetParent() && (menu->GetId() != -1))
1085 return false;
1086
1087 if (menu->GetMainWidget())
1088 {
1089 menu->DestroyMenu(true);
1090 }
1091
1092 menu->SetId(1); /* Mark as popped-up */
1093 menu->CreateMenu(NULL, widget, menu, 0);
1094 menu->SetInvokingWindow(this);
1095
1096 menu->UpdateUI();
1097
1098 // menu->SetParent(parent);
1099 // parent->children->Append(menu); // Store menu for later deletion
1100
1101 Widget menuWidget = (Widget) menu->GetMainWidget();
1102
1103 int rootX = 0;
1104 int rootY = 0;
1105
1106 int deviceX = x;
1107 int deviceY = y;
1108 /*
1109 if (this->IsKindOf(CLASSINFO(wxCanvas)))
1110 {
1111 wxCanvas *canvas = (wxCanvas *) this;
1112 deviceX = canvas->GetDC ()->LogicalToDeviceX (x);
1113 deviceY = canvas->GetDC ()->LogicalToDeviceY (y);
1114 }
1115 */
1116
1117 Display *display = XtDisplay (widget);
1118 Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget));
1119 Window thisWindow = XtWindow (widget);
1120 Window childWindow;
1121 XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY,
1122 &rootX, &rootY, &childWindow);
1123
1124 XButtonPressedEvent event;
1125 event.type = ButtonPress;
1126 event.button = 1;
1127
1128 event.x = deviceX;
1129 event.y = deviceY;
1130
1131 event.x_root = rootX;
1132 event.y_root = rootY;
1133
1134 XmMenuPosition (menuWidget, &event);
1135 XtManageChild (menuWidget);
1136
1137 // The ID of a pop-up menu is 1 when active, and is set to 0 by the
1138 // idle-time destroy routine.
1139 // Waiting until this ID changes causes this function to block until
1140 // the menu has been dismissed and the widgets cleaned up.
1141 // In other words, once this routine returns, it is safe to delete
1142 // the menu object.
1143 // Ian Brown <ian.brown@printsoft.de>
1144
1145 wxEventLoop evtLoop;
1146
1147 while (menu->GetId() == 1)
1148 {
1149 wxDoEventLoopIteration( evtLoop );
1150 }
1151
1152 return true;
1153 }
1154
1155 #endif
1156
1157 // ---------------------------------------------------------------------------
1158 // moving and resizing
1159 // ---------------------------------------------------------------------------
1160
1161 bool wxWindow::PreResize()
1162 {
1163 return true;
1164 }
1165
1166 // Get total size
1167 void wxWindow::DoGetSize(int *x, int *y) const
1168 {
1169 Widget widget = (Widget)( !m_drawingArea ? GetTopWidget() :
1170 ( m_borderWidget ? m_borderWidget :
1171 m_scrolledWindow ? m_scrolledWindow :
1172 m_drawingArea ) );
1173 Dimension xx, yy;
1174
1175 if (widget)
1176 XtVaGetValues( widget,
1177 XmNwidth, &xx,
1178 XmNheight, &yy,
1179 NULL );
1180 if(x) *x = widget ? xx : -1;
1181 if(y) *y = widget ? yy : -1;
1182 }
1183
1184 void wxWindow::DoGetPosition(int *x, int *y) const
1185 {
1186 Widget widget = (Widget)
1187 ( m_drawingArea ?
1188 ( m_borderWidget ? m_borderWidget : m_scrolledWindow ) :
1189 GetTopWidget() );
1190
1191 Position xx, yy;
1192 XtVaGetValues(widget, XmNx, &xx, XmNy, &yy, NULL);
1193
1194 // We may be faking the client origin. So a window that's really at (0, 30)
1195 // may appear (to wxWin apps) to be at (0, 0).
1196 if (GetParent())
1197 {
1198 wxPoint pt(GetParent()->GetClientAreaOrigin());
1199 xx = (Position)(xx - pt.x);
1200 yy = (Position)(yy - pt.y);
1201 }
1202
1203 if(x) *x = xx;
1204 if(y) *y = yy;
1205 }
1206
1207 void wxWindow::DoScreenToClient(int *x, int *y) const
1208 {
1209 Widget widget = (Widget) GetClientWidget();
1210 Display *display = XtDisplay((Widget) GetMainWidget());
1211 Window rootWindow = RootWindowOfScreen(XtScreen(widget));
1212 Window thisWindow = XtWindow(widget);
1213
1214 Window childWindow;
1215 int xx = x ? *x : 0;
1216 int yy = y ? *y : 0;
1217 XTranslateCoordinates(display, rootWindow, thisWindow,
1218 xx, yy, x ? x : &xx, y ? y : &yy,
1219 &childWindow);
1220 }
1221
1222 void wxWindow::DoClientToScreen(int *x, int *y) const
1223 {
1224 Widget widget = (Widget) GetClientWidget();
1225 Display *display = XtDisplay(widget);
1226 Window rootWindow = RootWindowOfScreen(XtScreen(widget));
1227 Window thisWindow = XtWindow(widget);
1228
1229 Window childWindow;
1230 int xx = x ? *x : 0;
1231 int yy = y ? *y : 0;
1232 XTranslateCoordinates(display, thisWindow, rootWindow,
1233 xx, yy, x ? x : &xx, y ? y : &yy,
1234 &childWindow);
1235 }
1236
1237
1238 // Get size *available for subwindows* i.e. excluding menu bar etc.
1239 void wxWindow::DoGetClientSize(int *x, int *y) const
1240 {
1241 Widget widget = (Widget) GetClientWidget();
1242 Dimension xx, yy;
1243 XtVaGetValues(widget, XmNwidth, &xx, XmNheight, &yy, NULL);
1244 if(x) *x = xx; if(y) *y = yy;
1245 }
1246
1247 void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1248 {
1249 DoSetSizeIntr(x, y, width, height, sizeFlags, false);
1250 }
1251
1252 void wxWindow::DoSetSizeIntr(int x, int y, int width, int height,
1253 int sizeFlags, bool fromCtor)
1254 {
1255 // A bit of optimization to help sort out the flickers.
1256 int oldX = -1, oldY = -1, oldW = -1, oldH = -1;
1257
1258 if( !fromCtor )
1259 {
1260 GetSize(& oldW, & oldH);
1261 GetPosition(& oldX, & oldY);
1262 }
1263
1264 if (x == -1)
1265 x = oldX;
1266 if (x == -1)
1267 x = oldY;
1268
1269 if ( !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1270 {
1271 if ( width == -1 )
1272 width = oldW;
1273 if ( height == -1 )
1274 height = oldH;
1275 }
1276
1277 wxSize size(wxDefaultSize);
1278 if ( width <= 0 )
1279 {
1280 if ( ( sizeFlags & wxSIZE_AUTO_WIDTH ) && !fromCtor )
1281 {
1282 size = DoGetBestSize();
1283 width = size.x;
1284 }
1285 else
1286 {
1287 width = oldW;
1288 }
1289 }
1290
1291 if ( height == -1 )
1292 {
1293 if( ( sizeFlags & wxSIZE_AUTO_HEIGHT ) && !fromCtor )
1294 {
1295 if( size.x == -1 ) size = DoGetBestSize();
1296 height = size.y;
1297 }
1298 else
1299 {
1300 height = oldH;
1301 }
1302 }
1303
1304 if ( x != oldX || y != oldY || width != oldW || height != oldH
1305 || !wxNoOptimize::CanOptimize() )
1306 {
1307 if (m_drawingArea)
1308 {
1309 int flags = 0;
1310
1311 if (x != oldX)
1312 flags |= wxMOVE_X;
1313
1314 if (y != oldY)
1315 flags |= wxMOVE_Y;
1316
1317 if (width > 0)
1318 flags |= wxMOVE_WIDTH;
1319
1320 if (height > 0)
1321 flags |= wxMOVE_HEIGHT;
1322
1323 int xx = x; int yy = y;
1324 AdjustForParentClientOrigin(xx, yy, sizeFlags);
1325 if( !fromCtor )
1326 DoMoveWindow( xx, yy, width, height );
1327 else
1328 DoMoveWindowIntr( xx, yy, width, height, flags );
1329
1330 return;
1331 }
1332
1333 Widget widget = (Widget) GetTopWidget();
1334 if (!widget)
1335 return;
1336
1337 bool managed = XtIsManaged( widget );
1338 if (managed)
1339 XtUnmanageChild(widget);
1340
1341 int xx = x;
1342 int yy = y;
1343 AdjustForParentClientOrigin(xx, yy, sizeFlags);
1344
1345 DoMoveWindow(xx, yy, width, height);
1346
1347 if (managed)
1348 XtManageChild(widget);
1349 }
1350 }
1351
1352 void wxWindow::DoSetClientSize(int width, int height)
1353 {
1354 if (m_drawingArea)
1355 {
1356 Widget drawingArea = (Widget) m_drawingArea;
1357
1358 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
1359
1360 if (width > -1)
1361 XtVaSetValues(drawingArea, XmNwidth, width, NULL);
1362 if (height > -1)
1363 XtVaSetValues(drawingArea, XmNheight, height, NULL);
1364
1365 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
1366 return;
1367 }
1368
1369 Widget widget = (Widget) GetTopWidget();
1370
1371 if (width > -1)
1372 XtVaSetValues(widget, XmNwidth, width, NULL);
1373 if (height > -1)
1374 XtVaSetValues(widget, XmNheight, height, NULL);
1375 }
1376
1377 void wxWindow::DoMoveWindowIntr(int xx, int yy, int w, int h,
1378 int flags)
1379 {
1380 if (m_drawingArea)
1381 {
1382 Widget drawingArea = (Widget) m_drawingArea;
1383 Widget borderOrScrolled = m_borderWidget ?
1384 (Widget) m_borderWidget :
1385 (Widget) m_scrolledWindow;
1386
1387 bool managed = XtIsManaged(borderOrScrolled);
1388 if (managed)
1389 XtUnmanageChild (borderOrScrolled);
1390 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
1391
1392 if (flags & wxMOVE_X)
1393 XtVaSetValues (borderOrScrolled,
1394 XmNx, xx,
1395 NULL);
1396 if (flags & wxMOVE_Y)
1397 XtVaSetValues (borderOrScrolled,
1398 XmNy, yy,
1399 NULL);
1400
1401 if (flags & wxMOVE_WIDTH)
1402 {
1403 if (m_borderWidget)
1404 {
1405 XtVaSetValues ((Widget) m_borderWidget, XmNwidth, w, NULL);
1406 short thick, margin;
1407 XtVaGetValues ((Widget) m_borderWidget,
1408 XmNshadowThickness, &thick,
1409 XmNmarginWidth, &margin,
1410 NULL);
1411 w -= 2 * (thick + margin);
1412 }
1413
1414 if( w < 1 ) w = 1;
1415 XtVaSetValues ((Widget) m_scrolledWindow, XmNwidth, w, NULL);
1416 }
1417
1418 if (flags & wxMOVE_HEIGHT)
1419 {
1420 if (m_borderWidget)
1421 {
1422 XtVaSetValues ((Widget) m_borderWidget, XmNheight, h, NULL);
1423 short thick, margin;
1424 XtVaGetValues ((Widget) m_borderWidget,
1425 XmNshadowThickness, &thick,
1426 XmNmarginHeight, &margin,
1427 NULL);
1428 h -= 2 * (thick + margin);
1429 }
1430
1431 if( h < 1 ) h = 1;
1432 XtVaSetValues ((Widget) m_scrolledWindow, XmNheight, h, NULL);
1433 }
1434
1435 if (managed)
1436 XtManageChild (borderOrScrolled);
1437 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
1438 }
1439 else
1440 {
1441 if( w < 1 ) w = 1;
1442 if( h < 1 ) h = 1;
1443
1444 XtVaSetValues((Widget)GetTopWidget(),
1445 XmNx, xx,
1446 XmNy, yy,
1447 XmNwidth, w,
1448 XmNheight, h,
1449 NULL);
1450 }
1451 }
1452
1453 void wxWindow::DoMoveWindow(int x, int y, int width, int height)
1454 {
1455 DoMoveWindowIntr (x, y, width, height,
1456 wxMOVE_X|wxMOVE_Y|wxMOVE_WIDTH|wxMOVE_HEIGHT);
1457 }
1458
1459 // ---------------------------------------------------------------------------
1460 // text metrics
1461 // ---------------------------------------------------------------------------
1462
1463 int wxWindow::GetCharHeight() const
1464 {
1465 int height;
1466
1467 if (m_font.Ok())
1468 wxGetTextExtent (GetXDisplay(), m_font, 1.0,
1469 "x", NULL, &height, NULL, NULL);
1470 else
1471 wxGetTextExtent (this, "x", NULL, &height, NULL, NULL);
1472
1473 return height;
1474 }
1475
1476 int wxWindow::GetCharWidth() const
1477 {
1478 int width;
1479
1480 if (m_font.Ok())
1481 wxGetTextExtent (GetXDisplay(), m_font, 1.0,
1482 "x", &width, NULL, NULL, NULL);
1483 else
1484 wxGetTextExtent (this, "x", &width, NULL, NULL, NULL);
1485
1486 return width;
1487 }
1488
1489 void wxWindow::GetTextExtent(const wxString& string,
1490 int *x, int *y,
1491 int *descent, int *externalLeading,
1492 const wxFont *theFont) const
1493 {
1494 const wxFont *fontToUse = theFont ? theFont : &m_font;
1495
1496 if (externalLeading)
1497 *externalLeading = 0;
1498 if (fontToUse->Ok())
1499 wxGetTextExtent (GetXDisplay(), *fontToUse, 1.0,
1500 string, x, y, NULL, descent);
1501 else
1502 wxGetTextExtent (this, string, x, y, NULL, descent);
1503 }
1504
1505 // ----------------------------------------------------------------------------
1506 // painting
1507 // ----------------------------------------------------------------------------
1508
1509 void wxWindow::AddUpdateRect(int x, int y, int w, int h)
1510 {
1511 m_updateRegion.Union( x, y, w, h );
1512 }
1513
1514 void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
1515 {
1516 Widget widget = (Widget) GetMainWidget();
1517 if (!widget)
1518 return;
1519 m_needsRefresh = true;
1520 Display *display = XtDisplay(widget);
1521 Window thisWindow = XtWindow(widget);
1522
1523 XExposeEvent dummyEvent;
1524 int width, height;
1525 GetSize(&width, &height);
1526
1527 dummyEvent.type = Expose;
1528 dummyEvent.display = display;
1529 dummyEvent.send_event = True;
1530 dummyEvent.window = thisWindow;
1531 if (rect)
1532 {
1533 dummyEvent.x = rect->x;
1534 dummyEvent.y = rect->y;
1535 dummyEvent.width = rect->width;
1536 dummyEvent.height = rect->height;
1537 }
1538 else
1539 {
1540 dummyEvent.x = 0;
1541 dummyEvent.y = 0;
1542 dummyEvent.width = width;
1543 dummyEvent.height = height;
1544 }
1545 dummyEvent.count = 0;
1546
1547 if (eraseBack)
1548 {
1549 wxClientDC dc(this);
1550 wxBrush backgroundBrush(GetBackgroundColour(), wxSOLID);
1551 dc.SetBackground(backgroundBrush);
1552 if (rect)
1553 dc.Clear(*rect);
1554 else
1555 dc.Clear();
1556 }
1557
1558 XSendEvent(display, thisWindow, False, ExposureMask, (XEvent *)&dummyEvent);
1559 }
1560
1561 void wxWindow::DoPaint()
1562 {
1563 //TODO : make a temporary gc so we can do the XCopyArea below
1564 if (m_backingPixmap && !m_needsRefresh)
1565 {
1566 wxPaintDC dc(this);
1567
1568 GC tempGC = (GC) dc.GetBackingGC();
1569
1570 Widget widget = (Widget) GetMainWidget();
1571
1572 int scrollPosX = 0;
1573 int scrollPosY = 0;
1574
1575 // We have to test whether it's a wxScrolledWindow (hack!) because
1576 // otherwise we don't know how many pixels have been scrolled. We might
1577 // solve this in the future by defining virtual wxWindow functions to get
1578 // the scroll position in pixels. Or, each kind of scrolled window has to
1579 // implement backing stores itself, using generic wxWidgets code.
1580 wxScrolledWindow* scrolledWindow = wxDynamicCast(this, wxScrolledWindow);
1581 if ( scrolledWindow )
1582 {
1583 int x, y;
1584 scrolledWindow->CalcScrolledPosition(0, 0, &x, &y);
1585
1586 scrollPosX = - x;
1587 scrollPosY = - y;
1588 }
1589
1590 // TODO: This could be optimized further by only copying the areas in the
1591 // current update region.
1592
1593 // Only blit the part visible in the client area. The backing pixmap
1594 // always starts at 0, 0 but we may be looking at only a portion of it.
1595 wxSize clientArea = GetClientSize();
1596 int toBlitX = m_pixmapWidth - scrollPosX;
1597 int toBlitY = m_pixmapHeight - scrollPosY;
1598
1599 // Copy whichever is samller, the amount of pixmap we have to copy,
1600 // or the size of the client area.
1601 toBlitX = wxMin(toBlitX, clientArea.x);
1602 toBlitY = wxMin(toBlitY, clientArea.y);
1603
1604 // Make sure we're not negative
1605 toBlitX = wxMax(0, toBlitX);
1606 toBlitY = wxMax(0, toBlitY);
1607
1608 XCopyArea
1609 (
1610 XtDisplay(widget),
1611 (Pixmap) m_backingPixmap,
1612 XtWindow (widget),
1613 tempGC,
1614 scrollPosX, scrollPosY, // Start at the scroll position
1615 toBlitX, toBlitY, // How much of the pixmap to copy
1616 0, 0 // Destination
1617 );
1618 }
1619 else
1620 {
1621 wxWindowDC dc(this);
1622 // Set an erase event first
1623 wxEraseEvent eraseEvent(GetId(), &dc);
1624 eraseEvent.SetEventObject(this);
1625 HandleWindowEvent(eraseEvent);
1626
1627 wxPaintEvent event(GetId());
1628 event.SetEventObject(this);
1629 HandleWindowEvent(event);
1630
1631 m_needsRefresh = false;
1632 }
1633 }
1634
1635 // ----------------------------------------------------------------------------
1636 // event handlers
1637 // ----------------------------------------------------------------------------
1638
1639 // Responds to colour changes: passes event on to children.
1640 void wxWindow::OnSysColourChanged(wxSysColourChangedEvent& event)
1641 {
1642 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1643 while ( node )
1644 {
1645 // Only propagate to non-top-level windows
1646 wxWindow *win = node->GetData();
1647 if ( win->GetParent() )
1648 {
1649 wxSysColourChangedEvent event2;
1650 event.SetEventObject(win);
1651 win->HandleWindowEvent(event2);
1652 }
1653
1654 node = node->GetNext();
1655 }
1656 }
1657
1658 void wxWindow::OnInternalIdle()
1659 {
1660 // This calls the UI-update mechanism (querying windows for
1661 // menu/toolbar/control state information)
1662 if (wxUpdateUIEvent::CanUpdate(this) && IsShown())
1663 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1664 }
1665
1666 // ----------------------------------------------------------------------------
1667 // accelerators
1668 // ----------------------------------------------------------------------------
1669
1670 bool wxWindow::ProcessAccelerator(wxKeyEvent& event)
1671 {
1672 #if wxUSE_ACCEL
1673 if (!m_acceleratorTable.Ok())
1674 return false;
1675
1676 int count = m_acceleratorTable.GetCount();
1677 wxAcceleratorEntry* entries = m_acceleratorTable.GetEntries();
1678 int i;
1679 for (i = 0; i < count; i++)
1680 {
1681 wxAcceleratorEntry* entry = & (entries[i]);
1682 if (entry->MatchesEvent(event))
1683 {
1684 // Bingo, we have a match. Now find a control that matches the
1685 // entry command id.
1686
1687 // Need to go up to the top of the window hierarchy, since it might
1688 // be e.g. a menu item
1689 wxWindow* parent = this;
1690 while ( parent && !parent->IsTopLevel() )
1691 parent = parent->GetParent();
1692
1693 if (!parent)
1694 return false;
1695
1696 wxFrame* frame = wxDynamicCast(parent, wxFrame);
1697 if ( frame )
1698 {
1699 #if wxUSE_MENUS
1700 // Try for a menu command
1701 if (frame->GetMenuBar())
1702 {
1703 wxMenuItem* item = frame->GetMenuBar()->FindItem(entry->GetCommand());
1704 if (item)
1705 {
1706 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, entry->GetCommand());
1707 commandEvent.SetEventObject(frame);
1708
1709 // If ProcessEvent returns true (it was handled), then
1710 // the calling code will skip the event handling.
1711 return frame->HandleWindowEvent(commandEvent);
1712 }
1713 }
1714 #endif
1715 }
1716
1717 // Find a child matching the command id
1718 wxWindow* child = parent->FindWindow(entry->GetCommand());
1719
1720 // No such child
1721 if (!child)
1722 return false;
1723
1724 // Now we process those kinds of windows that we can.
1725 // For now, only buttons.
1726 if ( wxDynamicCast(child, wxButton) )
1727 {
1728 wxCommandEvent commandEvent (wxEVT_COMMAND_BUTTON_CLICKED, child->GetId());
1729 commandEvent.SetEventObject(child);
1730 return child->HandleWindowEvent(commandEvent);
1731 }
1732
1733 return false;
1734 } // matches event
1735 }// for
1736 #endif
1737
1738 // We didn't match the key event against an accelerator.
1739 return false;
1740 }
1741
1742 // ============================================================================
1743 // Motif-specific stuff from here on
1744 // ============================================================================
1745
1746 // ----------------------------------------------------------------------------
1747 // function which maintain the global hash table mapping Widgets to wxWidgets
1748 // ----------------------------------------------------------------------------
1749
1750 bool wxAddWindowToTable(Widget w, wxWindow *win)
1751 {
1752 const long key = (long)w;
1753 if ( wxWidgetHashTable->Get(key))
1754 {
1755 wxLogDebug("Widget table clash: new widget is %ld, %s",
1756 key, win->GetClassInfo()->GetClassName());
1757 return false;
1758 }
1759
1760 wxWidgetHashTable->Put(key, win);
1761
1762 wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)",
1763 w, win, win->GetClassInfo()->GetClassName());
1764
1765 return true;
1766 }
1767
1768 wxWindow *wxGetWindowFromTable(Widget w)
1769 {
1770 return (wxWindow *)wxWidgetHashTable->Get((long) w);
1771 }
1772
1773 void wxDeleteWindowFromTable(Widget w)
1774 {
1775 wxLogTrace("widget", "Widget 0x%p", (WXWidget)w);
1776
1777 wxWidgetHashTable->Delete((long)w);
1778 }
1779
1780 // ----------------------------------------------------------------------------
1781 // add/remove window from the table
1782 // ----------------------------------------------------------------------------
1783
1784 // Add to hash table, add event handler
1785 bool wxWindow::AttachWidget (wxWindow* WXUNUSED(parent), WXWidget mainWidget,
1786 WXWidget formWidget, int x, int y, int width, int height)
1787 {
1788 wxAddWindowToTable((Widget) mainWidget, this);
1789 XtAddEventHandler( (Widget) mainWidget,
1790 ButtonPressMask | ButtonReleaseMask
1791 | PointerMotionMask,
1792 False,
1793 wxPanelItemEventHandler,
1794 (XtPointer) this);
1795
1796 if (!formWidget)
1797 {
1798 XtTranslations ptr;
1799 XtOverrideTranslations ((Widget) mainWidget,
1800 ptr = XtParseTranslationTable ("<Configure>: resize()"));
1801 XtFree ((char *) ptr);
1802 }
1803
1804 // Some widgets have a parent form widget, e.g. wxRadioBox
1805 if (formWidget)
1806 {
1807 if (!wxAddWindowToTable((Widget) formWidget, this))
1808 return false;
1809
1810 XtTranslations ptr;
1811 XtOverrideTranslations ((Widget) formWidget,
1812 ptr = XtParseTranslationTable ("<Configure>: resize()"));
1813 XtFree ((char *) ptr);
1814 }
1815
1816 if (x == -1)
1817 x = 0;
1818 if (y == -1)
1819 y = 0;
1820 DoSetSize (x, y, width, height, wxSIZE_USE_EXISTING);
1821
1822 return true;
1823 }
1824
1825 // Remove event handler, remove from hash table
1826 bool wxWindow::DetachWidget(WXWidget widget)
1827 {
1828 XtRemoveEventHandler( (Widget) widget,
1829 ButtonPressMask | ButtonReleaseMask
1830 | PointerMotionMask,
1831 False,
1832 wxPanelItemEventHandler,
1833 (XtPointer)this);
1834
1835 wxDeleteWindowFromTable((Widget) widget);
1836 return true;
1837 }
1838
1839 // ----------------------------------------------------------------------------
1840 // Motif-specific accessors
1841 // ----------------------------------------------------------------------------
1842
1843 WXWindow wxWindow::GetClientXWindow() const
1844 {
1845 Widget wMain = (Widget)GetClientWidget();
1846 if ( wMain )
1847 return (WXWindow) XtWindow(wMain);
1848 else
1849 return (WXWindow) 0;
1850 }
1851
1852 // Get the underlying X window
1853 WXWindow wxWindow::GetXWindow() const
1854 {
1855 Widget wMain = (Widget)GetMainWidget();
1856 if ( wMain )
1857 return (WXWindow) XtWindow(wMain);
1858 else
1859 return (WXWindow) 0;
1860 }
1861
1862 // Get the underlying X display
1863 WXDisplay *wxWindow::GetXDisplay() const
1864 {
1865 Widget wMain = (Widget)GetMainWidget();
1866 if ( wMain )
1867 return (WXDisplay*) XtDisplay(wMain);
1868 else
1869 return (WXDisplay*) NULL;
1870 }
1871
1872 WXWidget wxWindow::GetMainWidget() const
1873 {
1874 if (m_drawingArea)
1875 return m_drawingArea;
1876 else
1877 return m_mainWidget;
1878 }
1879
1880 WXWidget wxWindow::GetClientWidget() const
1881 {
1882 if (m_drawingArea != (WXWidget) 0)
1883 return m_drawingArea;
1884 else
1885 return GetMainWidget();
1886 }
1887
1888 WXWidget wxWindow::GetTopWidget() const
1889 {
1890 return GetMainWidget();
1891 }
1892
1893 WXWidget wxWindow::GetLabelWidget() const
1894 {
1895 return GetMainWidget();
1896 }
1897
1898 // ----------------------------------------------------------------------------
1899 // Motif callbacks
1900 // ----------------------------------------------------------------------------
1901
1902 // All widgets should have this as their resize proc.
1903 // OnSize sent to wxWindow via client data.
1904 void wxWidgetResizeProc(Widget w, XConfigureEvent *WXUNUSED(event),
1905 String WXUNUSED(args)[], int *WXUNUSED(num_args))
1906 {
1907 wxWindow *win = wxGetWindowFromTable(w);
1908 if (!win)
1909 return;
1910
1911 if (win->PreResize())
1912 {
1913 wxSize newSize(win->GetSize());
1914 wxSizeEvent sizeEvent(newSize, win->GetId());
1915 sizeEvent.SetEventObject(win);
1916 win->HandleWindowEvent(sizeEvent);
1917 }
1918 }
1919
1920 static void wxCanvasRepaintProc(Widget drawingArea,
1921 XtPointer clientData,
1922 XmDrawingAreaCallbackStruct * cbs)
1923 {
1924 if (!wxGetWindowFromTable(drawingArea))
1925 return;
1926
1927 XEvent * event = cbs->event;
1928 wxWindow * win = (wxWindow *) clientData;
1929
1930 switch (event->type)
1931 {
1932 case Expose:
1933 {
1934 win->AddUpdateRect(event->xexpose.x, event->xexpose.y,
1935 event->xexpose.width, event->xexpose.height);
1936
1937 if (event -> xexpose.count == 0)
1938 {
1939 win->DoPaint();
1940 }
1941 break;
1942 }
1943 }
1944 }
1945
1946 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
1947 static void wxCanvasEnterLeave(Widget drawingArea,
1948 XtPointer WXUNUSED(clientData),
1949 XCrossingEvent * event)
1950 {
1951 XmDrawingAreaCallbackStruct cbs;
1952 XEvent ev;
1953
1954 ((XCrossingEvent &) ev) = *event;
1955
1956 cbs.reason = XmCR_INPUT;
1957 cbs.event = &ev;
1958
1959 wxCanvasInputEvent(drawingArea, (XtPointer) NULL, &cbs);
1960 }
1961
1962 // Fix to make it work under Motif 1.0 (!)
1963 static void wxCanvasMotionEvent (Widget WXUNUSED(drawingArea),
1964 XButtonEvent *WXUNUSED(event))
1965 {
1966 #if XmVersion <= 1000
1967 XmDrawingAreaCallbackStruct cbs;
1968 XEvent ev;
1969
1970 ev = *((XEvent *) event);
1971 cbs.reason = XmCR_INPUT;
1972 cbs.event = &ev;
1973
1974 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
1975 #endif // XmVersion <= 1000
1976 }
1977
1978 static void wxCanvasInputEvent(Widget drawingArea,
1979 XtPointer WXUNUSED(data),
1980 XmDrawingAreaCallbackStruct * cbs)
1981 {
1982 wxWindow *canvas = wxGetWindowFromTable(drawingArea);
1983 XEvent* xevent = cbs->event;
1984
1985 if (canvas==NULL)
1986 return;
1987
1988 if (cbs->reason != XmCR_INPUT)
1989 return;
1990
1991 switch (xevent->xany.type)
1992 {
1993 case EnterNotify:
1994 case LeaveNotify:
1995 case ButtonPress:
1996 case ButtonRelease:
1997 case MotionNotify:
1998 {
1999 wxMouseEvent wxevent(0);
2000 if (wxTranslateMouseEvent(wxevent, canvas, drawingArea, xevent))
2001 {
2002 canvas->HandleWindowEvent(wxevent);
2003 }
2004 break;
2005 }
2006 case KeyPress:
2007 {
2008 wxKeyEvent event (wxEVT_CHAR);
2009 if (wxTranslateKeyEvent (event, canvas, (Widget) 0, xevent))
2010 {
2011 // Implement wxFrame::OnCharHook by checking ancestor.
2012 wxWindow *parent = canvas;
2013 while (parent && !parent->IsTopLevel())
2014 parent = parent->GetParent();
2015
2016 if (parent)
2017 {
2018 event.SetEventType(wxEVT_CHAR_HOOK);
2019 if (parent->HandleWindowEvent(event))
2020 return;
2021 }
2022
2023 // For simplicity, OnKeyDown is the same as OnChar
2024 // TODO: filter modifier key presses from OnChar
2025 event.SetEventType(wxEVT_KEY_DOWN);
2026
2027 // Only process OnChar if OnKeyDown didn't swallow it
2028 if (!canvas->HandleWindowEvent (event))
2029 {
2030 event.SetEventType(wxEVT_CHAR);
2031 canvas->HandleWindowEvent (event);
2032 }
2033 }
2034 break;
2035 }
2036 case KeyRelease:
2037 {
2038 wxKeyEvent event (wxEVT_KEY_UP);
2039 if (wxTranslateKeyEvent (event, canvas, (Widget) 0, xevent))
2040 {
2041 canvas->HandleWindowEvent (event);
2042 }
2043 break;
2044 }
2045 case FocusIn:
2046 {
2047 if (xevent->xfocus.detail != NotifyPointer)
2048 {
2049 wxFocusEvent event(wxEVT_SET_FOCUS, canvas->GetId());
2050 event.SetEventObject(canvas);
2051 canvas->HandleWindowEvent(event);
2052 }
2053 break;
2054 }
2055 case FocusOut:
2056 {
2057 if (xevent->xfocus.detail != NotifyPointer)
2058 {
2059 wxFocusEvent event(wxEVT_KILL_FOCUS, canvas->GetId());
2060 event.SetEventObject(canvas);
2061 canvas->HandleWindowEvent(event);
2062 }
2063 break;
2064 }
2065 default:
2066 break;
2067 }
2068 }
2069
2070 static void wxPanelItemEventHandler(Widget wid,
2071 XtPointer WXUNUSED(client_data),
2072 XEvent* event,
2073 Boolean *continueToDispatch)
2074 {
2075 // Widget can be a label or the actual widget.
2076
2077 wxWindow *window = wxGetWindowFromTable(wid);
2078 if (window)
2079 {
2080 wxMouseEvent wxevent(0);
2081 if (wxTranslateMouseEvent(wxevent, window, wid, event))
2082 {
2083 window->HandleWindowEvent(wxevent);
2084 }
2085 }
2086
2087 // TODO: probably the key to allowing default behaviour to happen. Say we
2088 // set a m_doDefault flag to false at the start of this function. Then in
2089 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2090 // true, indicating that default processing can happen. Thus, behaviour can
2091 // appear to be overridden just by adding an event handler and not calling
2092 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2093 // way of handling drawing area events, to simplify things.
2094 *continueToDispatch = True;
2095 }
2096
2097 static void wxScrollBarCallback(Widget scrollbar,
2098 XtPointer clientData,
2099 XmScrollBarCallbackStruct *cbs)
2100 {
2101 wxWindow *win = wxGetWindowFromTable(scrollbar);
2102 wxCHECK_RET( win, _T("invalid widget in scrollbar callback") );
2103
2104 wxOrientation orientation = (wxOrientation)wxPtrToUInt(clientData);
2105
2106 wxEventType eventType = wxEVT_NULL;
2107 switch (cbs->reason)
2108 {
2109 case XmCR_INCREMENT:
2110 {
2111 eventType = wxEVT_SCROLLWIN_LINEDOWN;
2112 break;
2113 }
2114 case XmCR_DECREMENT:
2115 {
2116 eventType = wxEVT_SCROLLWIN_LINEUP;
2117 break;
2118 }
2119 case XmCR_DRAG:
2120 {
2121 eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2122 break;
2123 }
2124 case XmCR_VALUE_CHANGED:
2125 {
2126 eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2127 break;
2128 }
2129 case XmCR_PAGE_INCREMENT:
2130 {
2131 eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2132 break;
2133 }
2134 case XmCR_PAGE_DECREMENT:
2135 {
2136 eventType = wxEVT_SCROLLWIN_PAGEUP;
2137 break;
2138 }
2139 case XmCR_TO_TOP:
2140 {
2141 eventType = wxEVT_SCROLLWIN_TOP;
2142 break;
2143 }
2144 case XmCR_TO_BOTTOM:
2145 {
2146 eventType = wxEVT_SCROLLWIN_BOTTOM;
2147 break;
2148 }
2149 default:
2150 {
2151 // Should never get here
2152 wxFAIL_MSG("Unknown scroll event.");
2153 break;
2154 }
2155 }
2156
2157 wxScrollWinEvent event(eventType,
2158 cbs->value,
2159 orientation);
2160 event.SetEventObject( win );
2161 win->HandleWindowEvent(event);
2162 }
2163
2164 // For repainting arbitrary windows
2165 void wxUniversalRepaintProc(Widget w, XtPointer WXUNUSED(c_data), XEvent *event, char *)
2166 {
2167 wxWindow* win = wxGetWindowFromTable(w);
2168 if (!win)
2169 return;
2170
2171 switch ( event->type )
2172 {
2173 case Expose:
2174 {
2175 win->AddUpdateRect(event->xexpose.x, event->xexpose.y,
2176 event->xexpose.width, event->xexpose.height);
2177
2178 if ( event->xexpose.count == 0 )
2179 {
2180 win->DoPaint();
2181 }
2182
2183 break;
2184 }
2185 }
2186 }
2187
2188 // ----------------------------------------------------------------------------
2189 // TranslateXXXEvent() functions
2190 // ----------------------------------------------------------------------------
2191
2192 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win,
2193 Widget widget, const XEvent *xevent)
2194 {
2195 switch (xevent->xany.type)
2196 {
2197 case EnterNotify:
2198 case LeaveNotify:
2199 #if 0
2200 fprintf(stderr, "Widget 0x%p <-> window %p (%s), %s\n",
2201 (WXWidget)widget, win, win->GetClassInfo()->GetClassName(),
2202 (xevent->xany.type == EnterNotify ? "ENTER" : "LEAVE"));
2203 #endif
2204 case ButtonPress:
2205 case ButtonRelease:
2206 case MotionNotify:
2207 {
2208 int eventx = xevent->xbutton.x, eventy = xevent->xbutton.y;
2209
2210 wxEventType eventType = wxEVT_NULL;
2211
2212 if (xevent->xany.type == LeaveNotify)
2213 {
2214 eventType = wxEVT_LEAVE_WINDOW;
2215 }
2216 if (xevent->xany.type == EnterNotify)
2217 {
2218 eventType = wxEVT_ENTER_WINDOW;
2219 }
2220 else if (xevent->xany.type == MotionNotify)
2221 {
2222 eventType = wxEVT_MOTION;
2223
2224 if (xevent->xmotion.is_hint == NotifyHint)
2225 {
2226 Window root, child;
2227 int x_root, y_root;
2228 unsigned int state;
2229 Display *dpy = XtDisplay (widget);
2230
2231 XQueryPointer (dpy, XtWindow (widget),
2232 &root, &child,
2233 &x_root, &y_root, &eventx, &eventy, &state);
2234 }
2235 }
2236 else if (xevent->xany.type == ButtonPress)
2237 {
2238 wxevent.SetTimestamp(xevent->xbutton.time);
2239 int button = 0;
2240 if (xevent->xbutton.button == Button1)
2241 {
2242 eventType = wxEVT_LEFT_DOWN;
2243 button = 1;
2244 }
2245 else if (xevent->xbutton.button == Button2)
2246 {
2247 eventType = wxEVT_MIDDLE_DOWN;
2248 button = 2;
2249 }
2250 else if (xevent->xbutton.button == Button3)
2251 {
2252 eventType = wxEVT_RIGHT_DOWN;
2253 button = 3;
2254 }
2255
2256 // check for a double click
2257 //
2258 long dclickTime = XtGetMultiClickTime(xevent->xany.display);
2259 long ts = wxevent.GetTimestamp();
2260
2261 int buttonLast = win->GetLastClickedButton();
2262 long lastTS = win->GetLastClickTime();
2263 if ( buttonLast && buttonLast == button &&
2264 (ts - lastTS) < dclickTime )
2265 {
2266 // I have a dclick
2267 win->SetLastClick(0, ts);
2268 if ( eventType == wxEVT_LEFT_DOWN )
2269 eventType = wxEVT_LEFT_DCLICK;
2270 else if ( eventType == wxEVT_MIDDLE_DOWN )
2271 eventType = wxEVT_MIDDLE_DCLICK;
2272 else if ( eventType == wxEVT_RIGHT_DOWN )
2273 eventType = wxEVT_RIGHT_DCLICK;
2274 }
2275 else
2276 {
2277 // not fast enough or different button
2278 win->SetLastClick(button, ts);
2279 }
2280 }
2281 else if (xevent->xany.type == ButtonRelease)
2282 {
2283 if (xevent->xbutton.button == Button1)
2284 {
2285 eventType = wxEVT_LEFT_UP;
2286 }
2287 else if (xevent->xbutton.button == Button2)
2288 {
2289 eventType = wxEVT_MIDDLE_UP;
2290 }
2291 else if (xevent->xbutton.button == Button3)
2292 {
2293 eventType = wxEVT_RIGHT_UP;
2294 }
2295 else
2296 return false;
2297 }
2298 else
2299 {
2300 return false;
2301 }
2302
2303 wxevent.SetEventType(eventType);
2304
2305 Position x1, y1;
2306 XtVaGetValues(widget, XmNx, &x1, XmNy, &y1, NULL);
2307
2308 int x2, y2;
2309 win->GetPosition(&x2, &y2);
2310
2311 // The button x/y must be translated to wxWidgets
2312 // window space - the widget might be a label or button,
2313 // within a form.
2314 int dx = 0;
2315 int dy = 0;
2316 if (widget != (Widget)win->GetMainWidget())
2317 {
2318 dx = x1;
2319 dy = y1;
2320 }
2321
2322 wxevent.m_x = eventx + dx;
2323 wxevent.m_y = eventy + dy;
2324
2325 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2326 || (event_left_is_down (xevent)
2327 && (eventType != wxEVT_LEFT_UP)));
2328 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2329 || (event_middle_is_down (xevent)
2330 && (eventType != wxEVT_MIDDLE_UP)));
2331 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2332 || (event_right_is_down (xevent)
2333 && (eventType != wxEVT_RIGHT_UP)));
2334
2335 wxevent.m_shiftDown = (xevent->xbutton.state & ShiftMask) == ShiftMask;
2336 wxevent.m_controlDown = (xevent->xbutton.state & ControlMask) == ControlMask;
2337 wxevent.m_altDown = (xevent->xbutton.state & Mod3Mask) == Mod3Mask;
2338 wxevent.m_metaDown = (xevent->xbutton.state & Mod1Mask) == Mod1Mask;
2339
2340 wxevent.SetId(win->GetId());
2341 wxevent.SetEventObject(win);
2342
2343 return true;
2344 }
2345 }
2346 return false;
2347 }
2348
2349 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win,
2350 Widget WXUNUSED(widget), const XEvent *xevent)
2351 {
2352 switch (xevent->xany.type)
2353 {
2354 case KeyPress:
2355 case KeyRelease:
2356 {
2357 char buf[20];
2358
2359 KeySym keySym;
2360 (void) XLookupString((XKeyEvent *)xevent, buf, 20, &keySym, NULL);
2361 int id = wxCharCodeXToWX (keySym);
2362 // id may be WXK_xxx code - these are outside ASCII range, so we
2363 // can't just use toupper() on id
2364 if (id >= 'a' && id <= 'z')
2365 id = toupper(id);
2366
2367 if (xevent->xkey.state & ShiftMask)
2368 wxevent.m_shiftDown = true;
2369 if (xevent->xkey.state & ControlMask)
2370 wxevent.m_controlDown = true;
2371 if (xevent->xkey.state & Mod3Mask)
2372 wxevent.m_altDown = true;
2373 if (xevent->xkey.state & Mod1Mask)
2374 wxevent.m_metaDown = true;
2375 wxevent.SetEventObject(win);
2376 wxevent.m_keyCode = id;
2377 wxevent.SetTimestamp(xevent->xkey.time);
2378
2379 wxevent.m_x = xevent->xbutton.x;
2380 wxevent.m_y = xevent->xbutton.y;
2381
2382 if (id > -1)
2383 return true;
2384
2385 return false;
2386 }
2387 default:
2388 break;
2389 }
2390 return false;
2391 }
2392
2393 // ----------------------------------------------------------------------------
2394 // Colour stuff
2395 // ----------------------------------------------------------------------------
2396
2397 #define YAllocColor XAllocColor
2398 XColor g_itemColors[5];
2399 int wxComputeColours (Display *display, const wxColour * back, const wxColour * fore)
2400 {
2401 int result;
2402 static XmColorProc colorProc;
2403
2404 result = wxNO_COLORS;
2405
2406 if (back)
2407 {
2408 g_itemColors[0].red = (unsigned short)(((long) back->Red ()) << 8);
2409 g_itemColors[0].green = (unsigned short)(((long) back->Green ()) << 8);
2410 g_itemColors[0].blue = (unsigned short)(((long) back->Blue ()) << 8);
2411 g_itemColors[0].flags = DoRed | DoGreen | DoBlue;
2412 if (colorProc == (XmColorProc) NULL)
2413 {
2414 // Get a ptr to the actual function
2415 colorProc = XmSetColorCalculation ((XmColorProc) NULL);
2416 // And set it back to motif.
2417 XmSetColorCalculation (colorProc);
2418 }
2419 (*colorProc) (&g_itemColors[wxBACK_INDEX],
2420 &g_itemColors[wxFORE_INDEX],
2421 &g_itemColors[wxSELE_INDEX],
2422 &g_itemColors[wxTOPS_INDEX],
2423 &g_itemColors[wxBOTS_INDEX]);
2424 result = wxBACK_COLORS;
2425 }
2426 if (fore)
2427 {
2428 g_itemColors[wxFORE_INDEX].red = (unsigned short)(((long) fore->Red ()) << 8);
2429 g_itemColors[wxFORE_INDEX].green = (unsigned short)(((long) fore->Green ()) << 8);
2430 g_itemColors[wxFORE_INDEX].blue = (unsigned short)(((long) fore->Blue ()) << 8);
2431 g_itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue;
2432 if (result == wxNO_COLORS)
2433 result = wxFORE_COLORS;
2434 }
2435
2436 Display *dpy = display;
2437 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
2438
2439 if (back)
2440 {
2441 /* 5 Colours to allocate */
2442 for (int i = 0; i < 5; i++)
2443 if (!YAllocColor (dpy, cmap, &g_itemColors[i]))
2444 result = wxNO_COLORS;
2445 }
2446 else if (fore)
2447 {
2448 /* Only 1 colour to allocate */
2449 if (!YAllocColor (dpy, cmap, &g_itemColors[wxFORE_INDEX]))
2450 result = wxNO_COLORS;
2451 }
2452
2453 return result;
2454 }
2455
2456 // Changes the foreground and background colours to be derived from the current
2457 // background colour. To change the foreground colour, you must call
2458 // SetForegroundColour explicitly.
2459 void wxWindow::ChangeBackgroundColour()
2460 {
2461 WXWidget mainWidget = GetMainWidget();
2462 if ( mainWidget )
2463 wxDoChangeBackgroundColour(mainWidget, m_backgroundColour);
2464 if ( m_scrolledWindow && mainWidget != m_scrolledWindow )
2465 wxDoChangeForegroundColour(m_scrolledWindow, m_backgroundColour);
2466 }
2467
2468 void wxWindow::ChangeForegroundColour()
2469 {
2470 WXWidget mainWidget = GetMainWidget();
2471 if ( mainWidget )
2472 wxDoChangeForegroundColour(mainWidget, m_foregroundColour);
2473 if ( m_scrolledWindow && mainWidget != m_scrolledWindow )
2474 wxDoChangeForegroundColour(m_scrolledWindow, m_foregroundColour);
2475 }
2476
2477 bool wxWindow::SetBackgroundColour(const wxColour& col)
2478 {
2479 if ( !wxWindowBase::SetBackgroundColour(col) )
2480 return false;
2481
2482 ChangeBackgroundColour();
2483
2484 return true;
2485 }
2486
2487 bool wxWindow::SetForegroundColour(const wxColour& col)
2488 {
2489 if ( !wxWindowBase::SetForegroundColour(col) )
2490 return false;
2491
2492 ChangeForegroundColour();
2493
2494 return true;
2495 }
2496
2497 void wxWindow::ChangeFont(bool keepOriginalSize)
2498 {
2499 // Note that this causes the widget to be resized back
2500 // to its original size! We therefore have to set the size
2501 // back again. TODO: a better way in Motif?
2502 Widget w = (Widget) GetLabelWidget(); // Usually the main widget
2503 if (w && m_font.Ok())
2504 {
2505 int width, height, width1, height1;
2506 GetSize(& width, & height);
2507
2508 wxDoChangeFont( w, m_font );
2509
2510 GetSize(& width1, & height1);
2511 if (keepOriginalSize && (width != width1 || height != height1))
2512 {
2513 SetSize(wxDefaultCoord, wxDefaultCoord, width, height);
2514 }
2515 }
2516 }
2517
2518 // Post-creation
2519 void wxWindow::PostCreation()
2520 {
2521 ChangeFont();
2522 ChangeForegroundColour();
2523 ChangeBackgroundColour();
2524 }
2525
2526 // Pre-creation
2527 void wxWindow::PreCreation()
2528 {
2529 InheritAttributes();
2530 }
2531
2532 // ----------------------------------------------------------------------------
2533 // global functions
2534 // ----------------------------------------------------------------------------
2535
2536 wxWindow *wxGetActiveWindow()
2537 {
2538 // TODO
2539 wxFAIL_MSG("Not implemented");
2540 return NULL;
2541 }
2542
2543 /* static */
2544 wxWindow *wxWindowBase::GetCapture()
2545 {
2546 return (wxWindow *)g_captureWindow;
2547 }
2548
2549
2550 // Find the wxWindow at the current mouse position, returning the mouse
2551 // position.
2552 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
2553 {
2554 pt = wxGetMousePosition();
2555 return wxFindWindowAtPoint(pt);
2556 }
2557
2558 void wxGetMouseState(int& rootX, int& rootY, unsigned& maskReturn)
2559 {
2560 Display *display = wxGlobalDisplay();
2561 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
2562 Window rootReturn, childReturn;
2563 int winX, winY;
2564
2565 XQueryPointer (display,
2566 rootWindow,
2567 &rootReturn,
2568 &childReturn,
2569 &rootX, &rootY, &winX, &winY, &maskReturn);
2570 }
2571
2572 // Get the current mouse position.
2573 wxPoint wxGetMousePosition()
2574 {
2575 int x, y;
2576 unsigned mask;
2577
2578 wxGetMouseState(x, y, mask);
2579 return wxPoint(x, y);
2580 }
2581
2582 wxMouseState wxGetMouseState()
2583 {
2584 wxMouseState ms;
2585 int x, y;
2586 unsigned mask;
2587
2588 wxGetMouseState(x, y, mask);
2589
2590 ms.SetX(x);
2591 ms.SetY(y);
2592
2593 ms.SetLeftDown(mask & Button1Mask);
2594 ms.SetMiddleDown(mask & Button2Mask);
2595 ms.SetRightDown(mask & Button3Mask);
2596
2597 ms.SetControlDown(mask & ControlMask);
2598 ms.SetShiftDown(mask & ShiftMask);
2599 ms.SetAltDown(mask & Mod3Mask);
2600 ms.SetMetaDown(mask & Mod1Mask);
2601
2602 return ms;
2603 }
2604
2605
2606 #if wxMOTIF_NEW_FONT_HANDLING
2607
2608 #include <Xm/XmP.h>
2609
2610 void wxGetTextExtent(const wxWindow* window, const wxString& str,
2611 int* width, int* height, int* ascent, int* descent)
2612 {
2613 Arg args[2];
2614 int count = 0;
2615 XmRendition rendition = NULL;
2616 XmRenderTable table = NULL;
2617 Widget w = (Widget) window->GetLabelWidget();
2618
2619 XtVaGetValues( w, XmNrenderTable, &table, NULL );
2620 if (table == NULL)
2621 table = XmeGetDefaultRenderTable(w, XmTEXT_RENDER_TABLE);
2622
2623 rendition = XmRenderTableGetRendition( table, "" );
2624 XtSetArg( args[count], XmNfont, 0 ); ++count;
2625 XtSetArg( args[count], XmNfontType, 0 ); ++count;
2626 XmRenditionRetrieve( rendition, args, count );
2627
2628 if (args[1].value == XmFONT_IS_FONTSET)
2629 {
2630 XRectangle ink, logical;
2631 WXFontSet fset = (WXFontSet) args[0].value;
2632
2633 XmbTextExtents( (XFontSet)fset, str.c_str(), str.length(),
2634 &ink, &logical);
2635
2636 if( width ) *width = logical.width;
2637 if( height ) *height = logical.height;
2638 if( ascent ) *ascent = -logical.y;
2639 if( descent ) *descent = logical.height + logical.y;
2640 }
2641 else
2642 {
2643 int direction, ascent2, descent2;
2644 XCharStruct overall;
2645 XFontStruct* fontStruct;
2646
2647 XmeRenderTableGetDefaultFont( table, &fontStruct );
2648 XTextExtents(fontStruct, (const char*)str.c_str(), str.length(),
2649 &direction, &ascent2, &descent2, &overall);
2650
2651 if ( width ) *width = overall.width;
2652 if ( height ) *height = ascent2 + descent2;
2653 if ( descent ) *descent = descent2;
2654 if ( ascent ) *ascent = ascent2;
2655 }
2656 }
2657
2658 #else // if !wxMOTIF_NEW_FONT_HANDLING
2659
2660 void wxGetTextExtent(const wxWindow* window, const wxString& str,
2661 int* width, int* height, int* ascent, int* descent)
2662 {
2663 XmFontList list = NULL;
2664 XmFontContext cxt;
2665 XmFontType type;
2666 Widget w = (Widget) window->GetLabelWidget();
2667
2668 XtVaGetValues( w, XmNfontList, &list, NULL );
2669 XmFontListInitFontContext( &cxt, list );
2670
2671 XmFontListEntry entry = XmFontListNextEntry( cxt );
2672 XmFontListFreeFontContext( cxt );
2673 XtPointer thing = XmFontListEntryGetFont( entry, &type );
2674
2675 if (type == XmFONT_IS_FONTSET)
2676 {
2677 XRectangle ink, logical;
2678
2679 XmbTextExtents( (XFontSet)thing, str.c_str(), str.length(),
2680 &ink, &logical);
2681
2682 if( width ) *width = logical.width;
2683 if( height ) *height = logical.height;
2684 if( ascent ) *ascent = -logical.y;
2685 if( descent ) *descent = logical.height + logical.y;
2686 }
2687 else
2688 {
2689 int direction, ascent2, descent2;
2690 XCharStruct overall;
2691
2692 XTextExtents( (XFontStruct*)thing, (char*)(const char*)str.c_str(), str.length(),
2693 &direction, &ascent2, &descent2, &overall);
2694
2695 if ( width ) *width = overall.width;
2696 if ( height ) *height = ascent2 + descent2;
2697 if ( descent ) *descent = descent2;
2698 if ( ascent ) *ascent = ascent2;
2699 }
2700 }
2701
2702 #endif // !wxMOTIF_NEW_FONT_HANDLING
2703
2704 // ----------------------------------------------------------------------------
2705 // wxNoOptimize: switch off size optimization
2706 // ----------------------------------------------------------------------------
2707
2708 int wxNoOptimize::ms_count = 0;