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