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