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