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