]> git.saurik.com Git - wxWidgets.git/blob - src/motif/window.cpp
Fix wxSocket under wxMotif.
[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),
2001 XButtonEvent *WXUNUSED(event))
2002 {
2003 #if XmVersion <= 1000
2004 XmDrawingAreaCallbackStruct cbs;
2005 XEvent ev;
2006
2007 ev = *((XEvent *) event);
2008 cbs.reason = XmCR_INPUT;
2009 cbs.event = &ev;
2010
2011 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
2012 #endif // XmVersion <= 1000
2013 }
2014
2015 static void wxCanvasInputEvent(Widget drawingArea,
2016 XtPointer WXUNUSED(data),
2017 XmDrawingAreaCallbackStruct * cbs)
2018 {
2019 wxWindow *canvas = wxGetWindowFromTable(drawingArea);
2020 XEvent local_event;
2021
2022 if (canvas==NULL)
2023 return;
2024
2025 if (cbs->reason != XmCR_INPUT)
2026 return;
2027
2028 local_event = *(cbs->event); // We must keep a copy!
2029
2030 switch (local_event.xany.type)
2031 {
2032 case EnterNotify:
2033 case LeaveNotify:
2034 case ButtonPress:
2035 case ButtonRelease:
2036 case MotionNotify:
2037 {
2038 // FIXME: most of this mouse event code is more or less
2039 // duplicated in wxTranslateMouseEvent
2040 //
2041 wxEventType eventType = wxEVT_NULL;
2042
2043 if (local_event.xany.type == EnterNotify)
2044 {
2045 //if (local_event.xcrossing.mode!=NotifyNormal)
2046 // return ; // Ignore grab events
2047 eventType = wxEVT_ENTER_WINDOW;
2048 // canvas->GetEventHandler()->OnSetFocus();
2049 }
2050 else if (local_event.xany.type == LeaveNotify)
2051 {
2052 //if (local_event.xcrossingr.mode!=NotifyNormal)
2053 // return ; // Ignore grab events
2054 eventType = wxEVT_LEAVE_WINDOW;
2055 // canvas->GetEventHandler()->OnKillFocus();
2056 }
2057 else if (local_event.xany.type == MotionNotify)
2058 {
2059 eventType = wxEVT_MOTION;
2060 }
2061
2062 else if (local_event.xany.type == ButtonPress)
2063 {
2064 if (local_event.xbutton.button == Button1)
2065 {
2066 eventType = wxEVT_LEFT_DOWN;
2067 canvas->SetButton1(TRUE);
2068 }
2069 else if (local_event.xbutton.button == Button2)
2070 {
2071 eventType = wxEVT_MIDDLE_DOWN;
2072 canvas->SetButton2(TRUE);
2073 }
2074 else if (local_event.xbutton.button == Button3)
2075 {
2076 eventType = wxEVT_RIGHT_DOWN;
2077 canvas->SetButton3(TRUE);
2078 }
2079 }
2080 else if (local_event.xany.type == ButtonRelease)
2081 {
2082 if (local_event.xbutton.button == Button1)
2083 {
2084 eventType = wxEVT_LEFT_UP;
2085 canvas->SetButton1(FALSE);
2086 }
2087 else if (local_event.xbutton.button == Button2)
2088 {
2089 eventType = wxEVT_MIDDLE_UP;
2090 canvas->SetButton2(FALSE);
2091 }
2092 else if (local_event.xbutton.button == Button3)
2093 {
2094 eventType = wxEVT_RIGHT_UP;
2095 canvas->SetButton3(FALSE);
2096 }
2097 }
2098
2099 wxMouseEvent wxevent (eventType);
2100
2101 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2102 || (event_left_is_down (&local_event)
2103 && (eventType != wxEVT_LEFT_UP)));
2104 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2105 || (event_middle_is_down (&local_event)
2106 && (eventType != wxEVT_MIDDLE_UP)));
2107 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2108 || (event_right_is_down (&local_event)
2109 && (eventType != wxEVT_RIGHT_UP)));
2110
2111 wxevent.m_shiftDown = local_event.xbutton.state & ShiftMask;
2112 wxevent.m_controlDown = local_event.xbutton.state & ControlMask;
2113 wxevent.m_altDown = local_event.xbutton.state & Mod3Mask;
2114 wxevent.m_metaDown = local_event.xbutton.state & Mod1Mask;
2115 wxevent.SetTimestamp(local_event.xbutton.time);
2116
2117 if ( eventType == wxEVT_MOTION )
2118 {
2119 if (local_event.xmotion.is_hint == NotifyHint)
2120 {
2121 Window root, child;
2122 Display *dpy = XtDisplay (drawingArea);
2123
2124 XQueryPointer (dpy, XtWindow (drawingArea),
2125 &root, &child,
2126 &local_event.xmotion.x_root,
2127 &local_event.xmotion.y_root,
2128 &local_event.xmotion.x,
2129 &local_event.xmotion.y,
2130 &local_event.xmotion.state);
2131 }
2132 else
2133 {
2134 }
2135 }
2136
2137 // Now check if we need to translate this event into a double click
2138 if (TRUE) // canvas->doubleClickAllowed)
2139 {
2140 if (wxevent.ButtonDown())
2141 {
2142 long dclickTime = XtGetMultiClickTime((Display*) wxGetDisplay());
2143
2144 // get button and time-stamp
2145 int button = 0;
2146 if (wxevent.LeftDown())
2147 button = 1;
2148 else if (wxevent.MiddleDown())
2149 button = 2;
2150 else if (wxevent.RightDown())
2151 button = 3;
2152 long ts = wxevent.GetTimestamp();
2153
2154 // check, if single or double click
2155 int buttonLast = canvas->GetLastClickedButton();
2156 long lastTS = canvas->GetLastClickTime();
2157 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
2158 {
2159 // I have a dclick
2160 canvas->SetLastClick(0, ts);
2161
2162 wxEventType typeDouble;
2163 if ( eventType == wxEVT_LEFT_DOWN )
2164 typeDouble = wxEVT_LEFT_DCLICK;
2165 else if ( eventType == wxEVT_MIDDLE_DOWN )
2166 typeDouble = wxEVT_MIDDLE_DCLICK;
2167 else if ( eventType == wxEVT_RIGHT_DOWN )
2168 typeDouble = wxEVT_RIGHT_DCLICK;
2169 else
2170 typeDouble = wxEVT_NULL;
2171
2172 if ( typeDouble != wxEVT_NULL )
2173 {
2174 wxevent.SetEventType(typeDouble);
2175 }
2176 }
2177 else
2178 {
2179 // not fast enough or different button
2180 canvas->SetLastClick(button, ts);
2181 }
2182 }
2183 }
2184
2185 wxevent.SetId(canvas->GetId());
2186 wxevent.SetEventObject(canvas);
2187 wxevent.m_x = local_event.xbutton.x;
2188 wxevent.m_y = local_event.xbutton.y;
2189 canvas->GetEventHandler()->ProcessEvent (wxevent);
2190 #if 0
2191 if (eventType == wxEVT_ENTER_WINDOW ||
2192 eventType == wxEVT_LEAVE_WINDOW ||
2193 eventType == wxEVT_MOTION
2194 )
2195 return;
2196 #endif // 0
2197 break;
2198 }
2199 case KeyPress:
2200 {
2201 wxKeyEvent event (wxEVT_CHAR);
2202 if (wxTranslateKeyEvent (event, canvas, (Widget) 0, &local_event))
2203 {
2204 // Implement wxFrame::OnCharHook by checking ancestor.
2205 wxWindow *parent = canvas->GetParent();
2206 while (parent && !parent->IsKindOf(CLASSINFO(wxFrame)))
2207 parent = parent->GetParent();
2208
2209 if (parent)
2210 {
2211 event.SetEventType(wxEVT_CHAR_HOOK);
2212 if (parent->GetEventHandler()->ProcessEvent(event))
2213 return;
2214 }
2215
2216 // For simplicity, OnKeyDown is the same as OnChar
2217 // TODO: filter modifier key presses from OnChar
2218 event.SetEventType(wxEVT_KEY_DOWN);
2219
2220 // Only process OnChar if OnKeyDown didn't swallow it
2221 if (!canvas->GetEventHandler()->ProcessEvent (event))
2222 {
2223 event.SetEventType(wxEVT_CHAR);
2224 canvas->GetEventHandler()->ProcessEvent (event);
2225 }
2226 }
2227 break;
2228 }
2229 case KeyRelease:
2230 {
2231 wxKeyEvent event (wxEVT_KEY_UP);
2232 if (wxTranslateKeyEvent (event, canvas, (Widget) 0, &local_event))
2233 {
2234 canvas->GetEventHandler()->ProcessEvent (event);
2235 }
2236 break;
2237 }
2238 case FocusIn:
2239 {
2240 if (local_event.xfocus.detail != NotifyPointer)
2241 {
2242 wxFocusEvent event(wxEVT_SET_FOCUS, canvas->GetId());
2243 event.SetEventObject(canvas);
2244 canvas->GetEventHandler()->ProcessEvent(event);
2245 }
2246 break;
2247 }
2248 case FocusOut:
2249 {
2250 if (local_event.xfocus.detail != NotifyPointer)
2251 {
2252 wxFocusEvent event(wxEVT_KILL_FOCUS, canvas->GetId());
2253 event.SetEventObject(canvas);
2254 canvas->GetEventHandler()->ProcessEvent(event);
2255 }
2256 break;
2257 }
2258 default:
2259 break;
2260 }
2261 }
2262
2263 static void wxPanelItemEventHandler(Widget wid,
2264 XtPointer WXUNUSED(client_data),
2265 XEvent* event,
2266 Boolean *continueToDispatch)
2267 {
2268 // Widget can be a label or the actual widget.
2269
2270 wxWindow *window = wxGetWindowFromTable(wid);
2271 if (window)
2272 {
2273 wxMouseEvent wxevent(0);
2274 if (wxTranslateMouseEvent(wxevent, window, wid, event))
2275 {
2276 window->GetEventHandler()->ProcessEvent(wxevent);
2277 }
2278 }
2279
2280 // TODO: probably the key to allowing default behaviour to happen. Say we
2281 // set a m_doDefault flag to FALSE at the start of this function. Then in
2282 // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to
2283 // TRUE, indicating that default processing can happen. Thus, behaviour can
2284 // appear to be overridden just by adding an event handler and not calling
2285 // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current
2286 // way of handling drawing area events, to simplify things.
2287 *continueToDispatch = True;
2288 }
2289
2290 static void wxScrollBarCallback(Widget scrollbar,
2291 XtPointer clientData,
2292 XmScrollBarCallbackStruct *cbs)
2293 {
2294 wxWindow *win = wxGetWindowFromTable(scrollbar);
2295 int orientation = (int) clientData;
2296
2297 wxEventType eventType = wxEVT_NULL;
2298 switch (cbs->reason)
2299 {
2300 case XmCR_INCREMENT:
2301 {
2302 eventType = wxEVT_SCROLLWIN_LINEDOWN;
2303 break;
2304 }
2305 case XmCR_DECREMENT:
2306 {
2307 eventType = wxEVT_SCROLLWIN_LINEUP;
2308 break;
2309 }
2310 case XmCR_DRAG:
2311 {
2312 eventType = wxEVT_SCROLLWIN_THUMBTRACK;
2313 break;
2314 }
2315 case XmCR_VALUE_CHANGED:
2316 {
2317 eventType = wxEVT_SCROLLWIN_THUMBRELEASE;
2318 break;
2319 }
2320 case XmCR_PAGE_INCREMENT:
2321 {
2322 eventType = wxEVT_SCROLLWIN_PAGEDOWN;
2323 break;
2324 }
2325 case XmCR_PAGE_DECREMENT:
2326 {
2327 eventType = wxEVT_SCROLLWIN_PAGEUP;
2328 break;
2329 }
2330 case XmCR_TO_TOP:
2331 {
2332 eventType = wxEVT_SCROLLWIN_TOP;
2333 break;
2334 }
2335 case XmCR_TO_BOTTOM:
2336 {
2337 eventType = wxEVT_SCROLLWIN_BOTTOM;
2338 break;
2339 }
2340 default:
2341 {
2342 // Should never get here
2343 wxFAIL_MSG("Unknown scroll event.");
2344 break;
2345 }
2346 }
2347
2348 wxScrollWinEvent event(eventType,
2349 cbs->value,
2350 ((orientation == XmHORIZONTAL) ?
2351 wxHORIZONTAL : wxVERTICAL));
2352 event.SetEventObject( win );
2353 win->GetEventHandler()->ProcessEvent(event);
2354 }
2355
2356 // For repainting arbitrary windows
2357 void wxUniversalRepaintProc(Widget w, XtPointer WXUNUSED(c_data), XEvent *event, char *)
2358 {
2359 Window window;
2360 Display *display;
2361
2362 wxWindow* win = wxGetWindowFromTable(w);
2363 if (!win)
2364 return;
2365
2366 switch(event -> type)
2367 {
2368 case Expose:
2369 {
2370 window = (Window) win -> GetXWindow();
2371 display = (Display *) win -> GetXDisplay();
2372
2373 if (event -> xexpose.count == 0)
2374 {
2375 win->DoPaint();
2376
2377 win->ClearUpdateRects();
2378 }
2379 else
2380 {
2381 win->AddUpdateRect(event->xexpose.x, event->xexpose.y,
2382 event->xexpose.width, event->xexpose.height);
2383 }
2384
2385 break;
2386 }
2387 }
2388 }
2389
2390 // ----------------------------------------------------------------------------
2391 // CanvaseXXXSize() functions
2392 // ----------------------------------------------------------------------------
2393
2394 void wxWindow::CanvasSetSize(int x, int y, int w, int h, int sizeFlags)
2395 {
2396 CanvasSetSizeIntr(x, y, w, h, sizeFlags, FALSE);
2397 }
2398
2399 // SetSize, but as per old wxCanvas (with drawing widget etc.)
2400 void wxWindow::CanvasSetSizeIntr(int x, int y, int w, int h, int sizeFlags,
2401 bool fromCtor)
2402 {
2403 // A bit of optimization to help sort out the flickers.
2404 int oldX = -1, oldY = -1, oldW = -1, oldH = -1;
2405 // see the top of the file, near DoSetSizeIntr
2406 if( !fromCtor )
2407 {
2408 GetSize(& oldW, & oldH);
2409 GetPosition(& oldX, & oldY);
2410 }
2411
2412 bool useOldPos = FALSE;
2413 bool useOldSize = FALSE;
2414
2415 if ((x == -1) && (x == -1) && ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0))
2416 useOldPos = TRUE;
2417 else if (x == oldX && y == oldY)
2418 useOldPos = TRUE;
2419
2420 if ((w == -1) && (h == -1))
2421 useOldSize = TRUE;
2422 else if (w == oldW && h == oldH)
2423 useOldSize = TRUE;
2424
2425 if (!wxNoOptimize::CanOptimize())
2426 {
2427 useOldSize = FALSE; useOldPos = FALSE;
2428 }
2429
2430 if (useOldPos && useOldSize)
2431 return;
2432
2433 Widget drawingArea = (Widget) m_drawingArea;
2434 bool managed = XtIsManaged(m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2435
2436 if (managed)
2437 XtUnmanageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2438 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2439
2440 int xx = x; int yy = y;
2441 AdjustForParentClientOrigin(xx, yy, sizeFlags);
2442
2443 if (!useOldPos)
2444 {
2445 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2446 {
2447 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2448 XmNx, xx, NULL);
2449 }
2450
2451 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2452 {
2453 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2454 XmNy, yy, NULL);
2455 }
2456 }
2457
2458 if (!useOldSize)
2459 {
2460
2461 if (w > -1)
2462 {
2463 if (m_borderWidget)
2464 {
2465 XtVaSetValues ((Widget) m_borderWidget, XmNwidth, w, NULL);
2466 short thick, margin;
2467 XtVaGetValues ((Widget) m_borderWidget,
2468 XmNshadowThickness, &thick,
2469 XmNmarginWidth, &margin,
2470 NULL);
2471 w -= 2 * (thick + margin);
2472 }
2473
2474 XtVaSetValues ((Widget) m_scrolledWindow, XmNwidth, w, NULL);
2475
2476 Dimension spacing;
2477 Widget sbar;
2478 XtVaGetValues ((Widget) m_scrolledWindow,
2479 XmNspacing, &spacing,
2480 XmNverticalScrollBar, &sbar,
2481 NULL);
2482 Dimension wsbar;
2483 if (sbar)
2484 XtVaGetValues (sbar, XmNwidth, &wsbar, NULL);
2485 else
2486 wsbar = 0;
2487
2488 w -= (spacing + wsbar);
2489
2490 #if 0
2491 XtVaSetValues(drawingArea, XmNwidth, w, NULL);
2492 #endif // 0
2493 }
2494 if (h > -1)
2495 {
2496 if (m_borderWidget)
2497 {
2498 XtVaSetValues ((Widget) m_borderWidget, XmNheight, h, NULL);
2499 short thick, margin;
2500 XtVaGetValues ((Widget) m_borderWidget,
2501 XmNshadowThickness, &thick,
2502 XmNmarginHeight, &margin,
2503 NULL);
2504 h -= 2 * (thick + margin);
2505 }
2506
2507 XtVaSetValues ((Widget) m_scrolledWindow, XmNheight, h, NULL);
2508
2509 Dimension spacing;
2510 Widget sbar;
2511 XtVaGetValues ((Widget) m_scrolledWindow,
2512 XmNspacing, &spacing,
2513 XmNhorizontalScrollBar, &sbar,
2514 NULL);
2515 Dimension wsbar;
2516 if (sbar)
2517 XtVaGetValues (sbar, XmNheight, &wsbar, NULL);
2518 else
2519 wsbar = 0;
2520
2521 h -= (spacing + wsbar);
2522
2523 #if 0
2524 XtVaSetValues(drawingArea, XmNheight, h, NULL);
2525 #endif // 0
2526 }
2527 }
2528
2529 if (managed)
2530 XtManageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2531 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2532
2533 #if 0
2534 int ww, hh;
2535 GetClientSize (&ww, &hh);
2536 wxSizeEvent sizeEvent(wxSize(ww, hh), GetId());
2537 sizeEvent.SetEventObject(this);
2538
2539 GetEventHandler()->ProcessEvent(sizeEvent);
2540 #endif // 0
2541 }
2542
2543 void wxWindow::CanvasSetClientSize (int w, int h)
2544 {
2545 Widget drawingArea = (Widget) m_drawingArea;
2546
2547 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2548
2549 if (w > -1)
2550 XtVaSetValues(drawingArea, XmNwidth, w, NULL);
2551 if (h > -1)
2552 XtVaSetValues(drawingArea, XmNheight, h, NULL);
2553
2554 #if 0
2555 // TODO: is this necessary?
2556 allowRepainting = FALSE;
2557
2558 XSync (XtDisplay (drawingArea), FALSE);
2559 XEvent event;
2560 while (XtAppPending (wxTheApp->appContext))
2561 {
2562 XFlush (XtDisplay (drawingArea));
2563 XtAppNextEvent (wxTheApp->appContext, &event);
2564 XtDispatchEvent (&event);
2565 }
2566 #endif // 0
2567
2568 XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2569
2570 #if 0
2571 allowRepainting = TRUE;
2572 DoRefresh ();
2573
2574 wxSizeEvent sizeEvent(wxSize(w, h), GetId());
2575 sizeEvent.SetEventObject(this);
2576
2577 GetEventHandler()->ProcessEvent(sizeEvent);
2578 #endif // 0
2579 }
2580
2581 void wxWindow::CanvasGetClientSize (int *w, int *h) const
2582 {
2583 // Must return the same thing that was set via SetClientSize
2584 Dimension xx, yy;
2585 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2586 *w = xx;
2587 *h = yy;
2588 }
2589
2590 void wxWindow::CanvasGetSize (int *w, int *h) const
2591 {
2592 Dimension xx, yy;
2593 if ((Widget) m_borderWidget)
2594 XtVaGetValues ((Widget) m_borderWidget, XmNwidth, &xx, XmNheight, &yy, NULL);
2595 else if ((Widget) m_scrolledWindow)
2596 XtVaGetValues ((Widget) m_scrolledWindow, XmNwidth, &xx, XmNheight, &yy, NULL);
2597 else
2598 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2599
2600 *w = xx;
2601 *h = yy;
2602 }
2603
2604 void wxWindow::CanvasGetPosition (int *x, int *y) const
2605 {
2606 Position xx, yy;
2607 XtVaGetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow, XmNx, &xx, XmNy, &yy, NULL);
2608
2609 // We may be faking the client origin.
2610 // So a window that's really at (0, 30) may appear
2611 // (to wxWin apps) to be at (0, 0).
2612 if (GetParent())
2613 {
2614 wxPoint pt(GetParent()->GetClientAreaOrigin());
2615 xx -= pt.x;
2616 yy -= pt.y;
2617 }
2618
2619 *x = xx;
2620 *y = yy;
2621 }
2622
2623 // ----------------------------------------------------------------------------
2624 // TranslateXXXEvent() functions
2625 // ----------------------------------------------------------------------------
2626
2627 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Widget widget, XEvent *xevent)
2628 {
2629 switch (xevent->xany.type)
2630 {
2631 case EnterNotify: // never received here - yes ? MB
2632 case LeaveNotify: // never received here - yes ? MB
2633 case ButtonPress:
2634 case ButtonRelease:
2635 case MotionNotify:
2636 {
2637 wxEventType eventType = wxEVT_NULL;
2638
2639 // FIXME: this is never true I think - MB
2640 //
2641 if (xevent->xany.type == LeaveNotify)
2642 {
2643 win->SetButton1(FALSE);
2644 win->SetButton2(FALSE);
2645 win->SetButton3(FALSE);
2646 return FALSE;
2647 }
2648 else if (xevent->xany.type == MotionNotify)
2649 {
2650 eventType = wxEVT_MOTION;
2651 }
2652 else if (xevent->xany.type == ButtonPress)
2653 {
2654 wxevent.SetTimestamp(xevent->xbutton.time);
2655 int button = 0;
2656 if (xevent->xbutton.button == Button1)
2657 {
2658 eventType = wxEVT_LEFT_DOWN;
2659 win->SetButton1(TRUE);
2660 button = 1;
2661 }
2662 else if (xevent->xbutton.button == Button2)
2663 {
2664 eventType = wxEVT_MIDDLE_DOWN;
2665 win->SetButton2(TRUE);
2666 button = 2;
2667 }
2668 else if (xevent->xbutton.button == Button3)
2669 {
2670 eventType = wxEVT_RIGHT_DOWN;
2671 win->SetButton3(TRUE);
2672 button = 3;
2673 }
2674
2675 // check for a double click
2676 //
2677 long dclickTime = XtGetMultiClickTime((Display*) wxGetDisplay());
2678 long ts = wxevent.GetTimestamp();
2679
2680 int buttonLast = win->GetLastClickedButton();
2681 long lastTS = win->GetLastClickTime();
2682 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
2683 {
2684 // I have a dclick
2685 win->SetLastClick(0, ts);
2686 if ( eventType == wxEVT_LEFT_DOWN )
2687 eventType = wxEVT_LEFT_DCLICK;
2688 else if ( eventType == wxEVT_MIDDLE_DOWN )
2689 eventType = wxEVT_MIDDLE_DCLICK;
2690 else if ( eventType == wxEVT_RIGHT_DOWN )
2691 eventType = wxEVT_RIGHT_DCLICK;
2692 }
2693 else
2694 {
2695 // not fast enough or different button
2696 win->SetLastClick(button, ts);
2697 }
2698 }
2699 else if (xevent->xany.type == ButtonRelease)
2700 {
2701 if (xevent->xbutton.button == Button1)
2702 {
2703 eventType = wxEVT_LEFT_UP;
2704 win->SetButton1(FALSE);
2705 }
2706 else if (xevent->xbutton.button == Button2)
2707 {
2708 eventType = wxEVT_MIDDLE_UP;
2709 win->SetButton2(FALSE);
2710 }
2711 else if (xevent->xbutton.button == Button3)
2712 {
2713 eventType = wxEVT_RIGHT_UP;
2714 win->SetButton3(FALSE);
2715 }
2716 else return FALSE;
2717 }
2718 else
2719 {
2720 return FALSE;
2721 }
2722
2723 wxevent.SetEventType(eventType);
2724
2725 Position x1, y1;
2726 XtVaGetValues(widget, XmNx, &x1, XmNy, &y1, NULL);
2727
2728 int x2, y2;
2729 win->GetPosition(&x2, &y2);
2730
2731 // The button x/y must be translated to wxWindows
2732 // window space - the widget might be a label or button,
2733 // within a form.
2734 int dx = 0;
2735 int dy = 0;
2736 if (widget != (Widget)win->GetMainWidget())
2737 {
2738 dx = x1;
2739 dy = y1;
2740 }
2741
2742 wxevent.m_x = xevent->xbutton.x + dx;
2743 wxevent.m_y = xevent->xbutton.y + dy;
2744
2745 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2746 || (event_left_is_down (xevent)
2747 && (eventType != wxEVT_LEFT_UP)));
2748 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2749 || (event_middle_is_down (xevent)
2750 && (eventType != wxEVT_MIDDLE_UP)));
2751 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2752 || (event_right_is_down (xevent)
2753 && (eventType != wxEVT_RIGHT_UP)));
2754
2755 wxevent.m_shiftDown = xevent->xbutton.state & ShiftMask;
2756 wxevent.m_controlDown = xevent->xbutton.state & ControlMask;
2757 wxevent.m_altDown = xevent->xbutton.state & Mod3Mask;
2758 wxevent.m_metaDown = xevent->xbutton.state & Mod1Mask;
2759
2760 wxevent.SetId(win->GetId());
2761 wxevent.SetEventObject(win);
2762
2763 return TRUE;
2764 }
2765 }
2766 return FALSE;
2767 }
2768
2769 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Widget WXUNUSED(widget), XEvent *xevent)
2770 {
2771 switch (xevent->xany.type)
2772 {
2773 case KeyPress:
2774 case KeyRelease:
2775 {
2776 char buf[20];
2777
2778 KeySym keySym;
2779 #if 0
2780 XComposeStatus compose;
2781 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, &compose);
2782 #endif // 0
2783 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
2784 int id = wxCharCodeXToWX (keySym);
2785 // id may be WXK_xxx code - these are outside ASCII range, so we
2786 // can't just use toupper() on id
2787 if (id >= 'a' && id <= 'z')
2788 id = toupper(id);
2789
2790 if (xevent->xkey.state & ShiftMask)
2791 wxevent.m_shiftDown = TRUE;
2792 if (xevent->xkey.state & ControlMask)
2793 wxevent.m_controlDown = TRUE;
2794 if (xevent->xkey.state & Mod3Mask)
2795 wxevent.m_altDown = TRUE;
2796 if (xevent->xkey.state & Mod1Mask)
2797 wxevent.m_metaDown = TRUE;
2798 wxevent.SetEventObject(win);
2799 wxevent.m_keyCode = id;
2800 wxevent.SetTimestamp(xevent->xkey.time);
2801
2802 wxevent.m_x = xevent->xbutton.x;
2803 wxevent.m_y = xevent->xbutton.y;
2804
2805 if (id > -1)
2806 return TRUE;
2807 else
2808 return FALSE;
2809 break;
2810 }
2811 default:
2812 break;
2813 }
2814 return FALSE;
2815 }
2816
2817 // ----------------------------------------------------------------------------
2818 // Colour stuff
2819 // ----------------------------------------------------------------------------
2820
2821 #define YAllocColor XAllocColor
2822 XColor g_itemColors[5];
2823 int wxComputeColours (Display *display, wxColour * back, wxColour * fore)
2824 {
2825 int result;
2826 static XmColorProc colorProc;
2827
2828 result = wxNO_COLORS;
2829
2830 if (back)
2831 {
2832 g_itemColors[0].red = (((long) back->Red ()) << 8);
2833 g_itemColors[0].green = (((long) back->Green ()) << 8);
2834 g_itemColors[0].blue = (((long) back->Blue ()) << 8);
2835 g_itemColors[0].flags = DoRed | DoGreen | DoBlue;
2836 if (colorProc == (XmColorProc) NULL)
2837 {
2838 // Get a ptr to the actual function
2839 colorProc = XmSetColorCalculation ((XmColorProc) NULL);
2840 // And set it back to motif.
2841 XmSetColorCalculation (colorProc);
2842 }
2843 (*colorProc) (&g_itemColors[wxBACK_INDEX],
2844 &g_itemColors[wxFORE_INDEX],
2845 &g_itemColors[wxSELE_INDEX],
2846 &g_itemColors[wxTOPS_INDEX],
2847 &g_itemColors[wxBOTS_INDEX]);
2848 result = wxBACK_COLORS;
2849 }
2850 if (fore)
2851 {
2852 g_itemColors[wxFORE_INDEX].red = (((long) fore->Red ()) << 8);
2853 g_itemColors[wxFORE_INDEX].green = (((long) fore->Green ()) << 8);
2854 g_itemColors[wxFORE_INDEX].blue = (((long) fore->Blue ()) << 8);
2855 g_itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue;
2856 if (result == wxNO_COLORS)
2857 result = wxFORE_COLORS;
2858 }
2859
2860 Display *dpy = display;
2861 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
2862
2863 if (back)
2864 {
2865 /* 5 Colours to allocate */
2866 for (int i = 0; i < 5; i++)
2867 if (!YAllocColor (dpy, cmap, &g_itemColors[i]))
2868 result = wxNO_COLORS;
2869 }
2870 else if (fore)
2871 {
2872 /* Only 1 colour to allocate */
2873 if (!YAllocColor (dpy, cmap, &g_itemColors[wxFORE_INDEX]))
2874 result = wxNO_COLORS;
2875 }
2876
2877 return (result);
2878
2879 }
2880
2881 // Changes the foreground and background colours to be derived from the current
2882 // background colour. To change the foreground colour, you must call
2883 // SetForegroundColour explicitly.
2884 void wxWindow::ChangeBackgroundColour()
2885 {
2886 WXWidget mainWidget = GetMainWidget();
2887 if ( mainWidget )
2888 DoChangeBackgroundColour(mainWidget, m_backgroundColour);
2889
2890 // This not necessary
2891 #if 0
2892
2893 if (m_scrolledWindow && (GetMainWidget() != m_scrolledWindow))
2894 {
2895 DoChangeBackgroundColour(m_scrolledWindow, m_backgroundColour);
2896 // Have to set the scrollbar colours back since
2897 // the scrolled window seemed to change them
2898 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
2899
2900 if (m_hScrollBar)
2901 DoChangeBackgroundColour(m_hScrollBar, backgroundColour);
2902 if (m_vScrollBar)
2903 DoChangeBackgroundColour(m_vScrollBar, backgroundColour);
2904 }
2905 #endif
2906 }
2907
2908 void wxWindow::ChangeForegroundColour()
2909 {
2910 WXWidget mainWidget = GetMainWidget();
2911 if ( mainWidget )
2912 DoChangeForegroundColour(mainWidget, m_foregroundColour);
2913 if ( m_scrolledWindow && mainWidget != m_scrolledWindow )
2914 DoChangeForegroundColour(m_scrolledWindow, m_foregroundColour);
2915 }
2916
2917 // Change a widget's foreground and background colours.
2918 void wxWindow::DoChangeForegroundColour(WXWidget widget,
2919 wxColour& foregroundColour)
2920 {
2921 wxDoChangeForegroundColour( widget, foregroundColour );
2922 }
2923
2924 void wxWindow::DoChangeBackgroundColour(WXWidget widget,
2925 wxColour& backgroundColour,
2926 bool changeArmColour)
2927 {
2928 wxDoChangeBackgroundColour( widget, backgroundColour, changeArmColour );
2929 }
2930
2931 bool wxWindow::SetBackgroundColour(const wxColour& col)
2932 {
2933 if ( !wxWindowBase::SetBackgroundColour(col) )
2934 return FALSE;
2935
2936 ChangeBackgroundColour();
2937
2938 return TRUE;
2939 }
2940
2941 bool wxWindow::SetForegroundColour(const wxColour& col)
2942 {
2943 if ( !wxWindowBase::SetForegroundColour(col) )
2944 return FALSE;
2945
2946 ChangeForegroundColour();
2947
2948 return TRUE;
2949 }
2950
2951 void wxWindow::ChangeFont(bool keepOriginalSize)
2952 {
2953 // Note that this causes the widget to be resized back
2954 // to its original size! We therefore have to set the size
2955 // back again. TODO: a better way in Motif?
2956 Widget w = (Widget) GetLabelWidget(); // Usually the main widget
2957 if (w && m_font.Ok())
2958 {
2959 int width, height, width1, height1;
2960 GetSize(& width, & height);
2961
2962 wxDoChangeFont( GetLabelWidget(), m_font );
2963
2964 GetSize(& width1, & height1);
2965 if (keepOriginalSize && (width != width1 || height != height1))
2966 {
2967 SetSize(-1, -1, width, height);
2968 }
2969 }
2970 }
2971
2972 // ----------------------------------------------------------------------------
2973 // global functions
2974 // ----------------------------------------------------------------------------
2975
2976 wxWindow *wxGetActiveWindow()
2977 {
2978 // TODO
2979 wxFAIL_MSG("Not implemented");
2980 return NULL;
2981 }
2982
2983 /* static */
2984 wxWindow *wxWindowBase::GetCapture()
2985 {
2986 return (wxWindow *)g_captureWindow;
2987 }
2988
2989
2990 // Find the wxWindow at the current mouse position, returning the mouse
2991 // position.
2992 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
2993 {
2994 return wxFindWindowAtPoint(wxGetMousePosition());
2995 }
2996
2997 // Get the current mouse position.
2998 wxPoint wxGetMousePosition()
2999 {
3000 Display *display = (Display*) wxGetDisplay();
3001 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
3002 Window rootReturn, childReturn;
3003 int rootX, rootY, winX, winY;
3004 unsigned int maskReturn;
3005
3006 XQueryPointer (display,
3007 rootWindow,
3008 &rootReturn,
3009 &childReturn,
3010 &rootX, &rootY, &winX, &winY, &maskReturn);
3011 return wxPoint(rootX, rootY);
3012 }
3013
3014
3015 // ----------------------------------------------------------------------------
3016 // wxNoOptimize: switch off size optimization
3017 // ----------------------------------------------------------------------------
3018
3019 int wxNoOptimize::ms_count = 0;
3020