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