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