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