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