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