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