]> git.saurik.com Git - wxWidgets.git/blame - src/motif/window.cpp
Added wxGenericValidator & doc
[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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "window.h"
14#endif
15
16#include "wx/setup.h"
17#include "wx/menu.h"
18#include "wx/dc.h"
19#include "wx/dcclient.h"
20#include "wx/utils.h"
21#include "wx/app.h"
22#include "wx/panel.h"
23#include "wx/layout.h"
24#include "wx/dialog.h"
25#include "wx/listbox.h"
26#include "wx/button.h"
27#include "wx/settings.h"
28#include "wx/msgdlg.h"
29#include "wx/frame.h"
30
31#include "wx/menuitem.h"
32#include "wx/log.h"
33
47d67540 34#if wxUSE_DRAG_AND_DROP
4bb6408c
JS
35#include "wx/dnd.h"
36#endif
37
38#include <Xm/Xm.h>
50414e24
JS
39
40#include <Xm/DrawingA.h>
41#include <Xm/ScrolledW.h>
42#include <Xm/ScrollBar.h>
43#include <Xm/Frame.h>
44#include <Xm/Label.h>
45
4bb6408c
JS
46#include "wx/motif/private.h"
47
48#include <string.h>
49
50414e24
JS
50#define SCROLL_MARGIN 4
51void wxCanvasRepaintProc (Widget, XtPointer, XmDrawingAreaCallbackStruct * cbs);
52void wxCanvasInputEvent (Widget drawingArea, XtPointer data, XmDrawingAreaCallbackStruct * cbs);
53void wxCanvasMotionEvent (Widget, XButtonEvent * event);
54void wxCanvasEnterLeave (Widget drawingArea, XtPointer clientData, XCrossingEvent * event);
8aa04e8b 55static void wxScrollBarCallback(Widget widget, XtPointer clientData,
2d120f83 56 XmScaleCallbackStruct *cbs);
02e8b2f9
JS
57void wxPanelItemEventHandler (Widget wid,
58 XtPointer client_data,
59 XEvent* event,
60 Boolean *continueToDispatch);
50414e24
JS
61
62#define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
63#define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
64#define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
65
4bb6408c
JS
66extern wxList wxPendingDelete;
67
68#if !USE_SHARED_LIBRARY
69IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxEvtHandler)
70
71BEGIN_EVENT_TABLE(wxWindow, wxEvtHandler)
2d120f83
JS
72EVT_CHAR(wxWindow::OnChar)
73EVT_KEY_DOWN(wxWindow::OnKeyDown)
74EVT_KEY_UP(wxWindow::OnKeyUp)
75EVT_ERASE_BACKGROUND(wxWindow::OnEraseBackground)
76EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged)
77EVT_INIT_DIALOG(wxWindow::OnInitDialog)
78EVT_IDLE(wxWindow::OnIdle)
4bb6408c
JS
79END_EVENT_TABLE()
80
81#endif
82
83
84// Constructor
85wxWindow::wxWindow()
86{
87 // Generic
88 m_windowId = 0;
89 m_windowStyle = 0;
90 m_windowParent = NULL;
91 m_windowEventHandler = this;
92 m_windowName = "";
93 m_windowCursor = *wxSTANDARD_CURSOR;
94 m_children = new wxList;
95 m_constraints = NULL;
96 m_constraintsInvolvedIn = NULL;
97 m_windowSizer = NULL;
98 m_sizerParent = NULL;
99 m_autoLayout = FALSE;
100 m_windowValidator = NULL;
101 m_defaultItem = NULL;
102 m_returnCode = 0;
103 m_caretWidth = 0; m_caretHeight = 0;
104 m_caretEnabled = FALSE;
105 m_caretShown = FALSE;
fd71308f
JS
106 // m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) ; ;
107 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE) ;
4bb6408c 108 m_foregroundColour = *wxBLACK;
2d120f83 109
47d67540 110#if wxUSE_DRAG_AND_DROP
4bb6408c
JS
111 m_pDropTarget = NULL;
112#endif
3dd4e4e0
JS
113 m_clientObject = (wxClientData*) NULL;
114 m_clientData = NULL;
2d120f83 115
50414e24 116 /// Motif-specific
4bb6408c
JS
117 m_mainWidget = (WXWidget) 0;
118 m_button1Pressed = FALSE;
119 m_button2Pressed = FALSE;
120 m_button3Pressed = FALSE;
121 m_winCaptured = FALSE;
122 m_isShown = TRUE;
50414e24
JS
123 m_hScrollBar = (WXWidget) 0;
124 m_vScrollBar = (WXWidget) 0;
125 m_borderWidget = (WXWidget) 0;
126 m_scrolledWindow = (WXWidget) 0;
127 m_drawingArea = (WXWidget) 0;
128 m_hScroll = FALSE;
129 m_vScroll = FALSE;
50414e24
JS
130 m_backingPixmap = (WXPixmap) 0;
131 m_pixmapWidth = 0;
132 m_pixmapHeight = 0;
133 m_pixmapOffsetX = 0;
134 m_pixmapOffsetY = 0;
135 m_lastTS = 0;
136 m_lastButton = 0;
02e8b2f9 137 m_canAddEventHandler = FALSE;
47bc1060
JS
138 m_scrollPosX = 0;
139 m_scrollPosY = 0;
4bb6408c
JS
140}
141
142// Destructor
143wxWindow::~wxWindow()
144{
f97c9854 145 //// Motif-specific
2d120f83 146
f97c9854
JS
147 if (GetMainWidget())
148 DetachWidget(GetMainWidget()); // Removes event handlers
2d120f83 149
f97c9854
JS
150 // If m_drawingArea, we're a fully-fledged window with drawing area, scrollbars etc. (what wxCanvas used to be)
151 if (m_drawingArea)
152 {
153 // Destroy children before destroying self
154 DestroyChildren();
2d120f83 155
f97c9854 156 if (m_backingPixmap)
2d120f83
JS
157 XFreePixmap (XtDisplay ((Widget) GetMainWidget()), (Pixmap) m_backingPixmap);
158
f97c9854
JS
159 Widget w = (Widget) m_drawingArea;
160 wxDeleteWindowFromTable(w);
2d120f83 161
f97c9854
JS
162 if (w)
163 XtDestroyWidget(w);
164 m_mainWidget = (WXWidget) 0;
2d120f83 165
f97c9854
JS
166 // Only if we're _really_ a canvas (not a dialog box/panel)
167 if (m_scrolledWindow)
168 {
169 wxDeleteWindowFromTable((Widget) m_scrolledWindow);
170 }
2d120f83 171
f97c9854
JS
172 if (m_hScrollBar)
173 {
174 XtUnmanageChild ((Widget) m_hScrollBar);
175 XtDestroyWidget ((Widget) m_hScrollBar);
176 }
177 if (m_vScrollBar)
178 {
179 XtUnmanageChild ((Widget) m_vScrollBar);
180 XtDestroyWidget ((Widget) m_vScrollBar);
181 }
182 if (m_scrolledWindow)
183 {
184 XtUnmanageChild ((Widget) m_scrolledWindow);
185 XtDestroyWidget ((Widget) m_scrolledWindow);
186 }
2d120f83 187
f97c9854
JS
188 if (m_borderWidget)
189 {
190 XtDestroyWidget ((Widget) m_borderWidget);
191 m_borderWidget = (WXWidget) 0;
192 }
50414e24 193 }
2d120f83 194
f97c9854 195 //// Generic stuff
2d120f83
JS
196
197 // Have to delete constraints/sizer FIRST otherwise
198 // sizers may try to look at deleted windows as they
199 // delete themselves.
47d67540 200#if wxUSE_CONSTRAINTS
4bb6408c
JS
201 DeleteRelatedConstraints();
202 if (m_constraints)
203 {
204 // This removes any dangling pointers to this window
205 // in other windows' constraintsInvolvedIn lists.
206 UnsetConstraints(m_constraints);
207 delete m_constraints;
208 m_constraints = NULL;
209 }
210 if (m_windowSizer)
211 {
212 delete m_windowSizer;
213 m_windowSizer = NULL;
214 }
215 // If this is a child of a sizer, remove self from parent
216 if (m_sizerParent)
217 m_sizerParent->RemoveChild((wxWindow *)this);
218#endif
2d120f83 219
4bb6408c
JS
220 if (m_windowParent)
221 m_windowParent->RemoveChild(this);
2d120f83 222
4bb6408c 223 DestroyChildren();
2d120f83 224
4bb6408c
JS
225 // Destroy the window
226 if (GetMainWidget())
227 {
2d120f83
JS
228 wxDeleteWindowFromTable((Widget) GetMainWidget());
229 XtDestroyWidget((Widget) GetMainWidget());
230 SetMainWidget((WXWidget) NULL);
4bb6408c 231 }
2d120f83 232
4bb6408c
JS
233 delete m_children;
234 m_children = NULL;
2d120f83 235
4bb6408c
JS
236 // Just in case the window has been Closed, but
237 // we're then deleting immediately: don't leave
238 // dangling pointers.
239 wxPendingDelete.DeleteObject(this);
2d120f83 240
3dd4e4e0
JS
241 if ( m_windowValidator ) delete m_windowValidator;
242 if (m_clientObject) delete m_clientObject;
2d120f83 243
55acd85e 244 ClearUpdateRects();
4bb6408c
JS
245}
246
247// Destroy the window (delayed, if a managed window)
248bool wxWindow::Destroy()
249{
250 delete this;
251 return TRUE;
252}
253
254// Constructor
255bool wxWindow::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
256 const wxPoint& pos,
257 const wxSize& size,
258 long style,
259 const wxString& name)
4bb6408c
JS
260{
261 // Generic
262 m_windowId = 0;
263 m_windowStyle = 0;
264 m_windowParent = NULL;
265 m_windowEventHandler = this;
266 m_windowName = "";
267 m_windowCursor = *wxSTANDARD_CURSOR;
268 m_constraints = NULL;
269 m_constraintsInvolvedIn = NULL;
270 m_windowSizer = NULL;
271 m_sizerParent = NULL;
272 m_autoLayout = FALSE;
273 m_windowValidator = NULL;
47d67540 274#if wxUSE_DRAG_AND_DROP
4bb6408c
JS
275 m_pDropTarget = NULL;
276#endif
4bb6408c
JS
277 m_caretWidth = 0; m_caretHeight = 0;
278 m_caretEnabled = FALSE;
279 m_caretShown = FALSE;
280 m_minSizeX = -1;
281 m_minSizeY = -1;
282 m_maxSizeX = -1;
283 m_maxSizeY = -1;
284 m_defaultItem = NULL;
285 m_windowParent = NULL;
3dd4e4e0
JS
286 m_clientObject = (wxClientData*) NULL;
287 m_clientData = NULL;
2d120f83 288
50414e24 289 // Motif-specific
02e8b2f9 290 m_canAddEventHandler = FALSE;
50414e24
JS
291 m_mainWidget = (WXWidget) 0;
292 m_button1Pressed = FALSE;
293 m_button2Pressed = FALSE;
294 m_button3Pressed = FALSE;
295 m_winCaptured = FALSE;
296 m_isShown = TRUE;
297 m_hScrollBar = (WXWidget) 0;
298 m_vScrollBar = (WXWidget) 0;
299 m_borderWidget = (WXWidget) 0;
300 m_scrolledWindow = (WXWidget) 0;
301 m_drawingArea = (WXWidget) 0;
302 m_hScroll = FALSE;
303 m_vScroll = FALSE;
50414e24
JS
304 m_backingPixmap = (WXPixmap) 0;
305 m_pixmapWidth = 0;
306 m_pixmapHeight = 0;
307 m_pixmapOffsetX = 0;
308 m_pixmapOffsetY = 0;
47bc1060
JS
309 m_scrollPosX = 0;
310 m_scrollPosY = 0;
2d120f83 311
4bb6408c
JS
312 if (!parent)
313 return FALSE;
2d120f83 314
4bb6408c 315 if (parent) parent->AddChild(this);
2d120f83 316
4bb6408c 317 m_returnCode = 0;
2d120f83 318
4bb6408c 319 SetName(name);
2d120f83 320
4bb6408c 321 if ( id == -1 )
2d120f83 322 m_windowId = (int)NewControlId();
4bb6408c 323 else
2d120f83
JS
324 m_windowId = id;
325
fd71308f
JS
326 // m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW) ; ;
327 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE) ;
4bb6408c 328 m_foregroundColour = *wxBLACK;
2d120f83 329
4bb6408c 330 m_windowStyle = style;
2d120f83 331
4bb6408c 332 if ( id == -1 )
2d120f83 333 m_windowId = (int)NewControlId();
4bb6408c 334 else
2d120f83
JS
335 m_windowId = id;
336
50414e24
JS
337 //// TODO: we should probably optimize by only creating a
338 //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL).
339 //// But for now, let's simplify things by always creating the
340 //// drawing area, since otherwise the translations are different.
2d120f83
JS
341
342 // New translations for getting mouse motion feedback
343 String translations =
344 "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
345<Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
346<Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
347<BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\
348<Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\
349<Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\
350<Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\
351<Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
352<Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
353<Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\
354<Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\
355<EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
356<LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\
357<Key>: DrawingAreaInput()";
358
359 XtActionsRec actions[1];
360 actions[0].string = "wxCanvasMotionEvent";
361 actions[0].proc = (XtActionProc) wxCanvasMotionEvent;
362 XtAppAddActions ((XtAppContext) wxTheApp->GetAppContext(), actions, 1);
363
364 Widget parentWidget = (Widget) parent->GetClientWidget();
365 if (style & wxBORDER)
366 m_borderWidget = (WXWidget) XtVaCreateManagedWidget ("canvasBorder",
367 xmFrameWidgetClass, parentWidget,
368 XmNshadowType, XmSHADOW_IN,
369 NULL);
370
371 m_scrolledWindow = (WXWidget) XtVaCreateManagedWidget ("scrolledWindow",
372 xmScrolledWindowWidgetClass, m_borderWidget ? (Widget) m_borderWidget : parentWidget,
373 XmNresizePolicy, XmRESIZE_NONE,
374 XmNspacing, 0,
375 XmNscrollingPolicy, XmAPPLICATION_DEFINED,
376 // XmNscrollBarDisplayPolicy, XmAS_NEEDED,
377 NULL);
378
379 XtTranslations ptr;
380 m_drawingArea = (WXWidget) XtVaCreateWidget ((char*) (const char*) name,
381 xmDrawingAreaWidgetClass, (Widget) m_scrolledWindow,
382 XmNunitType, XmPIXELS,
383 // XmNresizePolicy, XmRESIZE_ANY,
384 XmNresizePolicy, XmRESIZE_NONE,
385 XmNmarginHeight, 0,
386 XmNmarginWidth, 0,
387 XmNtranslations, ptr = XtParseTranslationTable (translations),
388 NULL);
389 /*
390 if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS)
391 {
392 XtFree ((char *) ptr);
393 ptr = XtParseTranslationTable ("<Key>: DrawingAreaInput()");
394 XtOverrideTranslations ((Widget) m_drawingArea, ptr);
395 XtFree ((char *) ptr);
396 }
397 */
398
399 wxAddWindowToTable((Widget) m_drawingArea, this);
400 wxAddWindowToTable((Widget) m_scrolledWindow, this);
401
402 /*
403 * This order is very important in Motif 1.2.1
404 *
405 */
406
407 XtRealizeWidget ((Widget) m_scrolledWindow);
408 XtRealizeWidget ((Widget) m_drawingArea);
409 XtManageChild ((Widget) m_drawingArea);
410
411 XtOverrideTranslations ((Widget) m_drawingArea,
412 ptr = XtParseTranslationTable ("<Configure>: resize()"));
50414e24 413 XtFree ((char *) ptr);
2d120f83
JS
414
415 XtAddCallback ((Widget) m_drawingArea, XmNexposeCallback, (XtCallbackProc) wxCanvasRepaintProc, (XtPointer) this);
416 XtAddCallback ((Widget) m_drawingArea, XmNinputCallback, (XtCallbackProc) wxCanvasInputEvent, (XtPointer) this);
417
418 /* TODO?
419 display = XtDisplay (scrolledWindow);
420 xwindow = XtWindow (drawingArea);
421 */
422
423 XtAddEventHandler ((Widget) m_drawingArea, PointerMotionHintMask | EnterWindowMask | LeaveWindowMask | FocusChangeMask,
424 False, (XtEventHandler) wxCanvasEnterLeave, (XtPointer) this);
425
426 // Scrolled widget needs to have its colour changed or we get
427 // a little blue square where the scrollbars abutt
428 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
429 DoChangeBackgroundColour(m_scrolledWindow, backgroundColour, TRUE);
430 DoChangeBackgroundColour(m_drawingArea, backgroundColour, TRUE);
431
432 XmScrolledWindowSetAreas ((Widget) m_scrolledWindow, (Widget) 0, (Widget) 0, (Widget) m_drawingArea);
433
434 /*
435 if (m_hScrollBar)
436 XtRealizeWidget ((Widget) m_hScrollBar);
437 if (m_vScrollBar)
438 XtRealizeWidget ((Widget) m_vScrollBar);
439 */
440
441 // Without this, the cursor may not be restored properly
442 // (e.g. in splitter sample).
443 SetCursor(*wxSTANDARD_CURSOR);
444 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
445 SetSize(pos.x, pos.y, size.x, size.y);
446
447 return TRUE;
88150e60
JS
448}
449
450// Helper function
451void wxWindow::CreateScrollbar(int orientation)
452{
2d120f83
JS
453 if (!m_drawingArea)
454 return;
455
456 XtVaSetValues((Widget) m_scrolledWindow, XmNresizePolicy, XmRESIZE_NONE, NULL);
457
458 // Add scrollbars if required
459 if (orientation == wxHORIZONTAL)
460 {
461 Widget hScrollBar = XtVaCreateManagedWidget ("hsb",
462 xmScrollBarWidgetClass, (Widget) m_scrolledWindow,
463 XmNorientation, XmHORIZONTAL,
464 NULL);
465 // XtAddCallback (hScrollBar, XmNvalueChangedCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
466 XtAddCallback (hScrollBar, XmNdragCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
467 XtAddCallback (hScrollBar, XmNincrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
468 XtAddCallback (hScrollBar, XmNdecrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
469 XtAddCallback (hScrollBar, XmNpageIncrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
470 XtAddCallback (hScrollBar, XmNpageDecrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
471 XtAddCallback (hScrollBar, XmNtoTopCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
472 XtAddCallback (hScrollBar, XmNtoBottomCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmHORIZONTAL);
473
474 XtVaSetValues (hScrollBar,
475 XmNincrement, 1,
476 XmNvalue, 0,
477 NULL);
478
479 m_hScrollBar = (WXWidget) hScrollBar;
480
481 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
482 DoChangeBackgroundColour(m_hScrollBar, backgroundColour, TRUE);
483
484 XtRealizeWidget(hScrollBar);
485
486 XtVaSetValues((Widget) m_scrolledWindow,
487 XmNhorizontalScrollBar, (Widget) m_hScrollBar,
488 NULL);
489
490 m_hScroll = TRUE;
491 }
492
493 if (orientation == wxVERTICAL)
494 {
495 Widget vScrollBar = XtVaCreateManagedWidget ("vsb",
496 xmScrollBarWidgetClass, (Widget) m_scrolledWindow,
497 XmNorientation, XmVERTICAL,
498 NULL);
499 // XtAddCallback (vScrollBar, XmNvalueChangedCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
500 XtAddCallback (vScrollBar, XmNdragCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
501 XtAddCallback (vScrollBar, XmNincrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
502 XtAddCallback (vScrollBar, XmNdecrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
503 XtAddCallback (vScrollBar, XmNpageIncrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
504 XtAddCallback (vScrollBar, XmNpageDecrementCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
505 XtAddCallback (vScrollBar, XmNtoTopCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
506 XtAddCallback (vScrollBar, XmNtoBottomCallback, (XtCallbackProc) wxScrollBarCallback, (XtPointer) XmVERTICAL);
507
508 XtVaSetValues (vScrollBar,
509 XmNincrement, 1,
510 XmNvalue, 0,
511 NULL);
512
513 m_vScrollBar = (WXWidget) vScrollBar;
514 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
515 DoChangeBackgroundColour(m_vScrollBar, backgroundColour, TRUE);
516
517 XtRealizeWidget(vScrollBar);
518
519 XtVaSetValues((Widget) m_scrolledWindow,
520 XmNverticalScrollBar, (Widget) m_vScrollBar,
521 NULL);
522
523 m_vScroll = TRUE;
524 }
525
526 XtVaSetValues((Widget) m_scrolledWindow, XmNresizePolicy, XmRESIZE_ANY, NULL);
88150e60
JS
527}
528void wxWindow::DestroyScrollbar(int orientation)
529{
2d120f83
JS
530 if (!m_drawingArea)
531 return;
532
533 XtVaSetValues((Widget) m_scrolledWindow, XmNresizePolicy, XmRESIZE_NONE, NULL);
534 // Add scrollbars if required
535 if (orientation == wxHORIZONTAL)
536 {
537 if (m_hScrollBar)
538 {
539 XtDestroyWidget((Widget) m_hScrollBar);
540 }
541 m_hScrollBar = (WXWidget) 0;
542 m_hScroll = FALSE;
543
544 XtVaSetValues((Widget) m_scrolledWindow,
545 XmNhorizontalScrollBar, (Widget) 0,
546 NULL);
547
548 }
549
550 if (orientation == wxVERTICAL)
551 {
552 if (m_vScrollBar)
553 {
554 XtDestroyWidget((Widget) m_vScrollBar);
555 }
556 m_vScrollBar = (WXWidget) 0;
557 m_vScroll = TRUE;
558
559 XtVaSetValues((Widget) m_scrolledWindow,
560 XmNverticalScrollBar, (Widget) 0,
561 NULL);
562
563 }
564 XtVaSetValues((Widget) m_scrolledWindow, XmNresizePolicy, XmRESIZE_ANY, NULL);
4bb6408c
JS
565}
566
567void wxWindow::SetFocus()
568{
2d120f83
JS
569 XmProcessTraversal((Widget) GetMainWidget(), XmTRAVERSE_CURRENT);
570 XmProcessTraversal((Widget) GetMainWidget(), XmTRAVERSE_CURRENT);
4bb6408c
JS
571}
572
573void wxWindow::Enable(bool enable)
574{
2d120f83
JS
575 if (GetMainWidget())
576 {
577 XtSetSensitive((Widget) GetMainWidget(), enable);
578 XmUpdateDisplay((Widget) GetMainWidget());
579 }
4bb6408c
JS
580}
581
582void wxWindow::CaptureMouse()
583{
2d120f83
JS
584 if (m_winCaptured)
585 return;
4bb6408c 586
2d120f83
JS
587 if (GetMainWidget())
588 XtAddGrab((Widget) GetMainWidget(), TRUE, FALSE);
589
590 m_winCaptured = TRUE;
4bb6408c
JS
591}
592
593void wxWindow::ReleaseMouse()
594{
2d120f83
JS
595 if (!m_winCaptured)
596 return;
4bb6408c 597
2d120f83
JS
598 if (GetMainWidget())
599 XtRemoveGrab((Widget) GetMainWidget());
600 m_winCaptured = FALSE;
4bb6408c
JS
601}
602
603// Push/pop event handler (i.e. allow a chain of event handlers
604// be searched)
605void wxWindow::PushEventHandler(wxEvtHandler *handler)
606{
2d120f83
JS
607 handler->SetNextHandler(GetEventHandler());
608 SetEventHandler(handler);
4bb6408c
JS
609}
610
611wxEvtHandler *wxWindow::PopEventHandler(bool deleteHandler)
612{
2d120f83
JS
613 if ( GetEventHandler() )
614 {
615 wxEvtHandler *handlerA = GetEventHandler();
616 wxEvtHandler *handlerB = handlerA->GetNextHandler();
617 handlerA->SetNextHandler(NULL);
618 SetEventHandler(handlerB);
619 if ( deleteHandler )
620 {
621 delete handlerA;
622 return NULL;
623 }
624 else
625 return handlerA;
626 }
627 else
628 return NULL;
4bb6408c
JS
629}
630
47d67540 631#if wxUSE_DRAG_AND_DROP
4bb6408c
JS
632
633void wxWindow::SetDropTarget(wxDropTarget *pDropTarget)
634{
2d120f83
JS
635 if ( m_pDropTarget != 0 ) {
636 delete m_pDropTarget;
637 }
638
639 m_pDropTarget = pDropTarget;
640 if ( m_pDropTarget != 0 )
641 {
642 // TODO
643 }
4bb6408c
JS
644}
645
646#endif
647
648// Old style file-manager drag&drop
649void wxWindow::DragAcceptFiles(bool accept)
650{
651 // TODO
652}
653
654// Get total size
655void wxWindow::GetSize(int *x, int *y) const
656{
2d120f83
JS
657 if (m_drawingArea)
658 {
659 CanvasGetSize(x, y);
660 return;
661 }
662
663 Widget widget = (Widget) GetTopWidget();
664 Dimension xx, yy;
665 XtVaGetValues(widget, XmNwidth, &xx, XmNheight, &yy, NULL);
666 *x = xx; *y = yy;
4bb6408c
JS
667}
668
669void wxWindow::GetPosition(int *x, int *y) const
670{
a4294b78
JS
671 if (m_drawingArea)
672 {
673 CanvasGetPosition(x, y);
674 return;
675 }
676 Widget widget = (Widget) GetTopWidget();
677 Position xx, yy;
678 XtVaGetValues(widget, XmNx, &xx, XmNy, &yy, NULL);
2d120f83 679
a4294b78
JS
680 // We may be faking the client origin.
681 // So a window that's really at (0, 30) may appear
682 // (to wxWin apps) to be at (0, 0).
683 if (GetParent())
684 {
685 wxPoint pt(GetParent()->GetClientAreaOrigin());
686 xx -= pt.x;
687 yy -= pt.y;
688 }
2d120f83 689
a4294b78 690 *x = xx; *y = yy;
4bb6408c
JS
691}
692
693void wxWindow::ScreenToClient(int *x, int *y) const
694{
2d120f83
JS
695 Widget widget = (Widget) GetClientWidget();
696 Display *display = XtDisplay((Widget) GetMainWidget());
697 Window rootWindow = RootWindowOfScreen(XtScreen(widget));
698 Window thisWindow = XtWindow(widget);
699
700 Window childWindow;
701 int xx = *x;
702 int yy = *y;
703 XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow);
4bb6408c
JS
704}
705
706void wxWindow::ClientToScreen(int *x, int *y) const
707{
2d120f83
JS
708 Widget widget = (Widget) GetClientWidget();
709 Display *display = XtDisplay(widget);
710 Window rootWindow = RootWindowOfScreen(XtScreen(widget));
711 Window thisWindow = XtWindow(widget);
712
713 Window childWindow;
714 int xx = *x;
715 int yy = *y;
716 XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow);
4bb6408c
JS
717}
718
719void wxWindow::SetCursor(const wxCursor& cursor)
720{
2d120f83
JS
721 m_windowCursor = cursor;
722 if (m_windowCursor.Ok())
723 {
724 WXDisplay *dpy = GetXDisplay();
725 WXCursor x_cursor = ((wxCursor&)cursor).GetXCursor(dpy);
726
727 Widget w = (Widget) GetMainWidget();
728 Window win = XtWindow(w);
729 XDefineCursor((Display*) dpy, win, (Cursor) x_cursor);
730 }
4bb6408c
JS
731}
732
733
734// Get size *available for subwindows* i.e. excluding menu bar etc.
735void wxWindow::GetClientSize(int *x, int *y) const
736{
2d120f83
JS
737 Widget widget = (Widget) GetClientWidget();
738 Dimension xx, yy;
739 XtVaGetValues(widget, XmNwidth, &xx, XmNheight, &yy, NULL);
740 *x = xx; *y = yy;
4bb6408c
JS
741}
742
743void wxWindow::SetSize(int x, int y, int width, int height, int sizeFlags)
744{
2d120f83
JS
745 // A bit of optimization to help sort out the flickers.
746 int oldX, oldY, oldW, oldH;
747 GetSize(& oldW, & oldH);
748 GetPosition(& oldX, & oldY);
749
750 bool useOldPos = FALSE;
751 bool useOldSize = FALSE;
752
753 if ((x == -1) && (x == -1) && ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0))
754 useOldPos = TRUE;
755 else if (x == oldX && y == oldY)
756 useOldPos = TRUE;
757
758 if ((width == -1) && (height == -1))
759 useOldSize = TRUE;
760 else if (width == oldW && height == oldH)
761 useOldSize = TRUE;
762
763 if (!wxNoOptimize::CanOptimize())
764 {
765 useOldSize = FALSE; useOldPos = FALSE;
766 }
767
768 if (useOldPos && useOldSize)
769 return;
770
771 if (m_drawingArea)
772 {
773 CanvasSetSize(x, y, width, height, sizeFlags);
774 return;
775 }
776 Widget widget = (Widget) GetTopWidget();
777 if (!widget)
778 return;
779
780 bool managed = XtIsManaged( widget );
781 if (managed)
782 XtUnmanageChild(widget);
783
784 int xx = x; int yy = y;
785 AdjustForParentClientOrigin(xx, yy, sizeFlags);
786
787 if (!useOldPos)
788 {
789 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
790 XtVaSetValues(widget, XmNx, xx, NULL);
791 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
792 XtVaSetValues(widget, XmNy, yy, NULL);
793 }
794 if (!useOldSize)
795 {
796 if (width > -1)
797 XtVaSetValues(widget, XmNwidth, width, NULL);
798 if (height > -1)
799 XtVaSetValues(widget, XmNheight, height, NULL);
800 }
801
802 if (managed)
803 XtManageChild(widget);
804
805 // How about this bit. Maybe we don't need to generate size events
806 // all the time -- they'll be generated when the window is sized anyway.
807 /*
808 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
809 sizeEvent.SetEventObject(this);
810
811 GetEventHandler()->ProcessEvent(sizeEvent);
812 */
813}
7fe7d506 814
2d120f83
JS
815void wxWindow::SetClientSize(int width, int height)
816{
817 if (m_drawingArea)
818 {
819 CanvasSetClientSize(width, height);
820 return;
821 }
822
823 Widget widget = (Widget) GetTopWidget();
824
825 if (width > -1)
826 XtVaSetValues(widget, XmNwidth, width, NULL);
827 if (height > -1)
828 XtVaSetValues(widget, XmNheight, height, NULL);
829
830 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
831 sizeEvent.SetEventObject(this);
832
833 GetEventHandler()->ProcessEvent(sizeEvent);
834}
4bb6408c
JS
835
836// For implementation purposes - sometimes decorations make the client area
837// smaller
838wxPoint wxWindow::GetClientAreaOrigin() const
839{
840 return wxPoint(0, 0);
841}
842
843// Makes an adjustment to the window position (for example, a frame that has
844// a toolbar that it manages itself).
845void wxWindow::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags)
846{
847 if (((sizeFlags & wxSIZE_NO_ADJUSTMENTS) == 0) && GetParent())
848 {
849 wxPoint pt(GetParent()->GetClientAreaOrigin());
850 x += pt.x; y += pt.y;
851 }
852}
853
854bool wxWindow::Show(bool show)
855{
50414e24
JS
856 if (show)
857 {
2d120f83
JS
858 if (m_borderWidget || m_scrolledWindow)
859 {
860 if (m_drawingArea)
861 XtMapWidget((Widget) m_drawingArea);
862 XtMapWidget(m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
863 }
864 else
865 {
866 WXWidget topWidget = GetTopWidget();
867 if (GetTopWidget())
868 XtMapWidget((Widget) GetTopWidget());
869 else if (GetMainWidget())
870 XtMapWidget((Widget) GetMainWidget());
871 }
50414e24
JS
872 }
873 else
874 {
2d120f83
JS
875 if (m_borderWidget || m_scrolledWindow)
876 {
877 if (m_drawingArea)
878 XtUnmapWidget((Widget) m_drawingArea);
879 XtUnmapWidget(m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
880 }
881 else
882 {
883 if (GetTopWidget())
884 XtUnmapWidget((Widget) GetTopWidget());
885 else if (GetMainWidget())
886 XtUnmapWidget((Widget) GetMainWidget());
887 }
50414e24 888 }
2d120f83
JS
889
890 /*
4bb6408c
JS
891 Window xwin = (Window) GetXWindow();
892 Display *xdisp = (Display*) GetXDisplay();
893 if (show)
2d120f83 894 XMapWindow(xdisp, xwin);
4bb6408c 895 else
2d120f83
JS
896 XUnmapWindow(xdisp, xwin);
897 */
898
4bb6408c 899 m_isShown = show;
2d120f83 900
4bb6408c
JS
901 return TRUE;
902}
903
904bool wxWindow::IsShown() const
905{
906 return m_isShown;
907}
908
909int wxWindow::GetCharHeight() const
910{
2d120f83
JS
911 if (!m_windowFont.Ok())
912 return 0;
913
914 WXFontStructPtr pFontStruct = m_windowFont.GetFontStruct(1.0, GetXDisplay());
915
916 int direction, ascent, descent;
917 XCharStruct overall;
918 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
47bc1060 919 &descent, &overall);
2d120f83
JS
920 // return (overall.ascent + overall.descent);
921 return (ascent + descent);
4bb6408c
JS
922}
923
924int wxWindow::GetCharWidth() const
925{
2d120f83
JS
926 if (!m_windowFont.Ok())
927 return 0;
928
929 WXFontStructPtr pFontStruct = m_windowFont.GetFontStruct(1.0, GetXDisplay());
930
931 int direction, ascent, descent;
932 XCharStruct overall;
933 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
47bc1060 934 &descent, &overall);
2d120f83 935 return overall.width;
47bc1060
JS
936}
937
938/* Helper function for 16-bit fonts */
939static int str16len(const char *s)
940{
2d120f83
JS
941 int count = 0;
942
943 while (s[0] && s[1]) {
944 count++;
945 s += 2;
946 }
947
948 return count;
4bb6408c
JS
949}
950
951void wxWindow::GetTextExtent(const wxString& string, int *x, int *y,
2d120f83 952 int *descent, int *externalLeading, const wxFont *theFont, bool use16) const
4bb6408c 953{
2d120f83
JS
954 wxFont *fontToUse = (wxFont *)theFont;
955 if (!fontToUse)
956 fontToUse = (wxFont *) & m_windowFont;
957
958 if (!fontToUse->Ok())
959 return;
960
961 WXFontStructPtr pFontStruct = theFont->GetFontStruct(1.0, GetXDisplay());
962
963 int direction, ascent, descent2;
964 XCharStruct overall;
965 int slen;
966
967 if (use16) slen = str16len(string); else slen = strlen(string);
968
969 if (use16)
970 XTextExtents16((XFontStruct*) pFontStruct, (XChar2b *) (char*) (const char*) string, slen, &direction,
971 &ascent, &descent2, &overall);
972 else
973 XTextExtents((XFontStruct*) pFontStruct, (char*) (const char*) string, slen, &direction,
974 &ascent, &descent2, &overall);
975
976 *x = (overall.width);
977 *y = (ascent + descent2);
978 if (descent)
979 *descent = descent2;
980 if (externalLeading)
981 *externalLeading = 0;
4bb6408c
JS
982}
983
16e93305 984void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
4bb6408c
JS
985{
986 Display *display = XtDisplay((Widget) GetMainWidget());
987 Window thisWindow = XtWindow((Widget) GetMainWidget());
2d120f83 988
4bb6408c
JS
989 XExposeEvent dummyEvent;
990 int width, height;
991 GetSize(&width, &height);
2d120f83 992
4bb6408c
JS
993 dummyEvent.type = Expose;
994 dummyEvent.display = display;
995 dummyEvent.send_event = True;
996 dummyEvent.window = thisWindow;
997 if (rect)
998 {
2d120f83
JS
999 dummyEvent.x = rect->x;
1000 dummyEvent.y = rect->y;
1001 dummyEvent.width = rect->width;
1002 dummyEvent.height = rect->height;
4bb6408c
JS
1003 }
1004 else
1005 {
2d120f83
JS
1006 dummyEvent.x = 0;
1007 dummyEvent.y = 0;
1008 dummyEvent.width = width;
1009 dummyEvent.height = height;
4bb6408c
JS
1010 }
1011 dummyEvent.count = 0;
2d120f83 1012
4bb6408c
JS
1013 if (eraseBack)
1014 {
1015 wxClientDC dc(this);
e97f20a0
JS
1016 wxBrush backgroundBrush(GetBackgroundColour(), wxSOLID);
1017 dc.SetBackground(backgroundBrush);
a367b9b3 1018 if (rect)
2d120f83 1019 dc.Clear(*rect);
a367b9b3 1020 else
2d120f83 1021 dc.Clear();
4bb6408c 1022 }
2d120f83 1023
4bb6408c
JS
1024 XSendEvent(display, thisWindow, False, ExposureMask, (XEvent *)&dummyEvent);
1025}
1026
1027// Responds to colour changes: passes event on to children.
1028void wxWindow::OnSysColourChanged(wxSysColourChangedEvent& event)
1029{
c0ed460c 1030 wxNode *node = GetChildren().First();
4bb6408c
JS
1031 while ( node )
1032 {
1033 // Only propagate to non-top-level windows
1034 wxWindow *win = (wxWindow *)node->Data();
1035 if ( win->GetParent() )
1036 {
1037 wxSysColourChangedEvent event2;
1038 event.m_eventObject = win;
1039 win->GetEventHandler()->ProcessEvent(event2);
1040 }
2d120f83 1041
4bb6408c
JS
1042 node = node->Next();
1043 }
1044}
1045
1046// This can be called by the app (or wxWindows) to do default processing for the current
1047// event. Save message/event info in wxWindow so they can be used in this function.
1048long wxWindow::Default()
1049{
1050 // TODO
1051 return 0;
1052}
1053
1054void wxWindow::InitDialog()
1055{
2d120f83
JS
1056 wxInitDialogEvent event(GetId());
1057 event.SetEventObject( this );
1058 GetEventHandler()->ProcessEvent(event);
4bb6408c
JS
1059}
1060
1061// Default init dialog behaviour is to transfer data to window
1062void wxWindow::OnInitDialog(wxInitDialogEvent& event)
1063{
2d120f83 1064 TransferDataToWindow();
4bb6408c
JS
1065}
1066
1067// Caret manipulation
1068void wxWindow::CreateCaret(int w, int h)
1069{
2d120f83
JS
1070 m_caretWidth = w;
1071 m_caretHeight = h;
1072 m_caretEnabled = TRUE;
4bb6408c
JS
1073}
1074
1075void wxWindow::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
1076{
1077 // TODO
1078}
1079
1080void wxWindow::ShowCaret(bool show)
1081{
1082 // TODO
1083}
1084
1085void wxWindow::DestroyCaret()
1086{
1087 // TODO
1088 m_caretEnabled = FALSE;
1089}
1090
1091void wxWindow::SetCaretPos(int x, int y)
1092{
1093 // TODO
1094}
1095
1096void wxWindow::GetCaretPos(int *x, int *y) const
1097{
1098 // TODO
1099}
1100
1101wxWindow *wxGetActiveWindow()
1102{
1103 // TODO
1104 return NULL;
1105}
1106
1107void wxWindow::SetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
1108{
1109 m_minSizeX = minW;
1110 m_minSizeY = minH;
1111 m_maxSizeX = maxW;
1112 m_maxSizeY = maxH;
2d120f83 1113
4bb6408c 1114 if (!this->IsKindOf(CLASSINFO(wxFrame)))
2d120f83
JS
1115 return;
1116
4bb6408c 1117 wxFrame *frame = (wxFrame *)this;
47bc1060 1118 Widget widget = (Widget) frame->GetShellWidget();
2d120f83 1119
4bb6408c 1120 if (minW > -1)
2d120f83 1121 XtVaSetValues(widget, XmNminWidth, minW, NULL);
4bb6408c 1122 if (minH > -1)
2d120f83 1123 XtVaSetValues(widget, XmNminHeight, minH, NULL);
4bb6408c 1124 if (maxW > -1)
2d120f83 1125 XtVaSetValues(widget, XmNmaxWidth, maxW, NULL);
4bb6408c 1126 if (maxH > -1)
2d120f83 1127 XtVaSetValues(widget, XmNmaxHeight, maxH, NULL);
4bb6408c 1128 if (incW > -1)
2d120f83 1129 XtVaSetValues(widget, XmNwidthInc, incW, NULL);
4bb6408c 1130 if (incH > -1)
2d120f83 1131 XtVaSetValues(widget, XmNheightInc, incH, NULL);
4bb6408c
JS
1132}
1133
1134void wxWindow::Centre(int direction)
1135{
2d120f83
JS
1136 int x, y, width, height, panel_width, panel_height, new_x, new_y;
1137
1138 wxWindow *father = (wxWindow *)GetParent();
1139 if (!father)
1140 return;
1141
1142 father->GetClientSize(&panel_width, &panel_height);
1143 GetSize(&width, &height);
1144 GetPosition(&x, &y);
1145
1146 new_x = -1;
1147 new_y = -1;
1148
1149 if (direction & wxHORIZONTAL)
1150 new_x = (int)((panel_width - width)/2);
1151
1152 if (direction & wxVERTICAL)
1153 new_y = (int)((panel_height - height)/2);
1154
1155 SetSize(new_x, new_y, -1, -1);
1156
4bb6408c
JS
1157}
1158
1159// Coordinates relative to the window
50414e24 1160void wxWindow::WarpPointer (int x, int y)
4bb6408c 1161{
2d120f83 1162 XWarpPointer (XtDisplay((Widget) GetClientWidget()), None, XtWindow((Widget) GetClientWidget()), 0, 0, 0, 0, x, y);
4bb6408c
JS
1163}
1164
1165void wxWindow::OnEraseBackground(wxEraseEvent& event)
1166{
1167 // TODO
1168 Default();
1169}
1170
1171int wxWindow::GetScrollPos(int orient) const
1172{
47bc1060
JS
1173 if (orient == wxHORIZONTAL)
1174 return m_scrollPosX;
1175 else
1176 return m_scrollPosY;
2d120f83
JS
1177 /*
1178 Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar);
1179 if (scrollBar)
1180 {
47bc1060
JS
1181 int pos;
1182 XtVaGetValues(scrollBar,
2d120f83 1183 XmNvalue, &pos, NULL);
47bc1060 1184 return pos;
2d120f83
JS
1185 }
1186 else
47bc1060 1187 return 0;
2d120f83 1188 */
4bb6408c
JS
1189}
1190
1191// This now returns the whole range, not just the number
1192// of positions that we can scroll.
1193int wxWindow::GetScrollRange(int orient) const
1194{
2d120f83
JS
1195 Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar);
1196 if (scrollBar)
1197 {
47bc1060
JS
1198 int range;
1199 XtVaGetValues(scrollBar,
1200 XmNmaximum, &range, NULL);
1201 return range;
1202 }
1203 else
1204 return 0;
4bb6408c
JS
1205}
1206
1207int wxWindow::GetScrollThumb(int orient) const
1208{
2d120f83
JS
1209 Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar);
1210 if (scrollBar)
1211 {
47bc1060
JS
1212 int thumb;
1213 XtVaGetValues(scrollBar,
1214 XmNsliderSize, &thumb, NULL);
1215 return thumb;
1216 }
1217 else
1218 return 0;
4bb6408c
JS
1219}
1220
47bc1060 1221void wxWindow::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
4bb6408c 1222{
2d120f83
JS
1223 Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar);
1224 if (scrollBar)
1225 {
1226 XtVaSetValues (scrollBar,
1227 XmNvalue, pos,
1228 NULL);
1229 }
1230 if (orient == wxHORIZONTAL)
1231 m_scrollPosX = pos;
1232 else
1233 m_scrollPosY = pos;
1234
4bb6408c
JS
1235}
1236
1237// New function that will replace some of the above.
1238void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible,
2d120f83 1239 int range, bool WXUNUSED(refresh))
4bb6408c 1240{
88150e60
JS
1241 int oldW, oldH;
1242 GetSize(& oldW, & oldH);
2d120f83 1243
47bc1060 1244 if (range == 0)
2d120f83 1245 range = 1;
47bc1060 1246 if (thumbVisible == 0)
2d120f83
JS
1247 thumbVisible = 1;
1248
28ab302b 1249 if (thumbVisible > range)
2d120f83
JS
1250 thumbVisible = range;
1251
88150e60
JS
1252 // Save the old state to see if it changed
1253 WXWidget oldScrollBar = ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar );
2d120f83 1254
47bc1060 1255 if (orient == wxHORIZONTAL)
28ab302b 1256 {
88150e60
JS
1257 if (thumbVisible == range)
1258 {
1259 if (m_hScrollBar)
2d120f83
JS
1260 DestroyScrollbar(wxHORIZONTAL);
1261 }
28ab302b 1262 else
88150e60
JS
1263 {
1264 if (!m_hScrollBar)
2d120f83 1265 CreateScrollbar(wxHORIZONTAL);
88150e60 1266 }
28ab302b 1267 }
88150e60 1268 if (orient == wxVERTICAL)
28ab302b 1269 {
88150e60
JS
1270 if (thumbVisible == range)
1271 {
1272 if (m_vScrollBar)
2d120f83
JS
1273 DestroyScrollbar(wxVERTICAL);
1274 }
28ab302b 1275 else
88150e60
JS
1276 {
1277 if (!m_vScrollBar)
2d120f83 1278 CreateScrollbar(wxVERTICAL);
88150e60 1279 }
28ab302b 1280 }
88150e60 1281 WXWidget newScrollBar = ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar );
2d120f83 1282
88150e60 1283 if (oldScrollBar != newScrollBar)
28ab302b 1284 {
2d120f83
JS
1285 // This is important! Without it, scrollbars misbehave
1286 // badly.
1287 XtUnrealizeWidget((Widget) m_scrolledWindow);
1288 XmScrolledWindowSetAreas ((Widget) m_scrolledWindow, (Widget) m_hScrollBar, (Widget) m_vScrollBar, (Widget) m_drawingArea);
1289 XtRealizeWidget((Widget) m_scrolledWindow);
1290 XtManageChild((Widget) m_scrolledWindow);
28ab302b 1291 }
2d120f83 1292
88150e60 1293 if (newScrollBar)
2d120f83
JS
1294 XtVaSetValues((Widget) newScrollBar,
1295 XmNvalue, pos,
1296 XmNminimum, 0,
1297 XmNmaximum, range,
1298 XmNsliderSize, thumbVisible,
1299 NULL);
1300
88150e60
JS
1301 if (orient == wxHORIZONTAL)
1302 m_scrollPosX = pos;
28ab302b 1303 else
88150e60 1304 m_scrollPosY = pos;
2d120f83 1305
88150e60
JS
1306 int newW, newH;
1307 GetSize(& newW, & newH);
2d120f83 1308
88150e60
JS
1309 // Adjusting scrollbars can resize the canvas accidentally
1310 if (newW != oldW || newH != oldH)
1311 SetSize(-1, -1, oldW, oldH);
4bb6408c
JS
1312}
1313
1314// Does a physical scroll
16e93305 1315void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
4bb6408c 1316{
2d120f83
JS
1317 // cerr << "Scrolling. delta = " << dx << ", " << dy << endl;
1318 int x, y, w, h;
1319 if (rect)
1320 {
1321 // Use specified rectangle
1322 x = rect->x; y = rect->y; w = rect->width; h = rect->height;
1323 }
1324 else
1325 {
1326 // Use whole client area
1327 x = 0; y = 0;
1328 GetClientSize(& w, & h);
1329 }
1330
1331 int x1 = (dx >= 0) ? x : x - dx ;
1332 int y1 = (dy >= 0) ? y : y - dy;
1333 int w1 = w - abs(dx);
1334 int h1 = h - abs(dy);
1335 int x2 = (dx >= 0) ? x + dx : x;
1336 int y2 = (dy >= 0) ? y + dy : y;
1337
1338 wxClientDC dc(this);
1339
1340 dc.SetLogicalFunction (wxCOPY);
1341
1342 Widget widget = (Widget) GetMainWidget();
1343 Window window = XtWindow(widget);
1344 Display* display = XtDisplay(widget);
1345
1346 XCopyArea(display, window,
1347 window, (GC) dc.GetGC(),
1348 x1, y1,
1349 w1, h1,
1350 x2, y2);
1351
1352 dc.SetAutoSetting(TRUE);
1353 wxBrush brush(GetBackgroundColour(), wxSOLID);
1354 dc.SetBrush(brush); // ??
1355
1356 // We'll add rectangles to the list of update rectangles
1357 // according to which bits we've exposed.
1358 wxList updateRects;
1359
1360 if (dx > 0)
1361 {
1362 wxRect *rect = new wxRect;
1363 rect->x = x;
1364 rect->y = y;
1365 rect->width = dx;
1366 rect->height = h;
1367
1368 XFillRectangle(display, window,
1369 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
1370
1371 rect->x = rect->x;
1372 rect->y = rect->y;
1373 rect->width = rect->width;
1374 rect->height = rect->height;
1375
1376 updateRects.Append((wxObject*) rect);
1377 }
1378 else if (dx < 0)
1379 {
1380 wxRect *rect = new wxRect;
1381
1382 rect->x = x + w + dx;
1383 rect->y = y;
1384 rect->width = -dx;
1385 rect->height = h;
1386
1387 XFillRectangle(display, window,
1388 (GC) dc.GetGC(), rect->x, rect->y, rect->width,
1389 rect->height);
1390
1391 rect->x = rect->x;
1392 rect->y = rect->y;
1393 rect->width = rect->width;
1394 rect->height = rect->height;
1395
1396 updateRects.Append((wxObject*) rect);
1397 }
1398 if (dy > 0)
1399 {
1400 wxRect *rect = new wxRect;
1401
1402 rect->x = x;
1403 rect->y = y;
1404 rect->width = w;
1405 rect->height = dy;
1406
1407 XFillRectangle(display, window,
1408 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
1409
1410 rect->x = rect->x;
1411 rect->y = rect->y;
1412 rect->width = rect->width;
1413 rect->height = rect->height;
1414
1415 updateRects.Append((wxObject*) rect);
1416 }
1417 else if (dy < 0)
1418 {
1419 wxRect *rect = new wxRect;
1420
1421 rect->x = x;
1422 rect->y = y + h + dy;
1423 rect->width = w;
1424 rect->height = -dy;
1425
1426 XFillRectangle(display, window,
1427 (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height);
1428
1429 rect->x = rect->x;
1430 rect->y = rect->y;
1431 rect->width = rect->width;
1432 rect->height = rect->height;
1433
1434 updateRects.Append((wxObject*) rect);
1435 }
1436 dc.SetBrush(wxNullBrush);
1437
1438 // Now send expose events
1439
1440 wxNode* node = updateRects.First();
1441 while (node)
1442 {
1443 wxRect* rect = (wxRect*) node->Data();
1444 XExposeEvent event;
1445
1446 event.type = Expose;
1447 event.display = display;
1448 event.send_event = True;
1449 event.window = window;
1450
1451 event.x = rect->x;
1452 event.y = rect->y;
1453 event.width = rect->width;
1454 event.height = rect->height;
1455
1456 event.count = 0;
1457
1458 XSendEvent(display, window, False, ExposureMask, (XEvent *)&event);
1459
1460 node = node->Next();
1461
1462 }
1463
1464 // Delete the update rects
1465 node = updateRects.First();
1466 while (node)
1467 {
1468 wxRect* rect = (wxRect*) node->Data();
1469 delete rect;
1470 node = node->Next();
1471 }
1472
4bb6408c
JS
1473}
1474
4bb6408c
JS
1475void wxWindow::OnChar(wxKeyEvent& event)
1476{
4ce81a75 1477/* ??
2d120f83
JS
1478if ( event.KeyCode() == WXK_TAB ) {
1479// propagate the TABs to the parent - it's up to it to decide what
1480// to do with it
1481if ( GetParent() ) {
1482if ( GetParent()->ProcessEvent(event) )
1483return;
1484}
1485}
1486 */
4ce81a75
JS
1487}
1488
1489void wxWindow::OnKeyDown(wxKeyEvent& event)
1490{
1491 Default();
1492}
1493
1494void wxWindow::OnKeyUp(wxKeyEvent& event)
1495{
1496 Default();
4bb6408c
JS
1497}
1498
1499void wxWindow::OnPaint(wxPaintEvent& event)
1500{
2d120f83 1501 Default();
4bb6408c
JS
1502}
1503
1504bool wxWindow::IsEnabled() const
1505{
47bc1060
JS
1506 // TODO. Is this right?
1507 // return XtGetSensitive((Widget) GetMainWidget());
4bb6408c
JS
1508 return FALSE;
1509}
1510
1511// Dialog support: override these and call
1512// base class members to add functionality
1513// that can't be done using validators.
1514// NOTE: these functions assume that controls
1515// are direct children of this window, not grandchildren
1516// or other levels of descendant.
1517
1518// Transfer values to controls. If returns FALSE,
1519// it's an application error (pops up a dialog)
1520bool wxWindow::TransferDataToWindow()
1521{
2d120f83
JS
1522 wxNode *node = GetChildren().First();
1523 while ( node )
1524 {
1525 wxWindow *child = (wxWindow *)node->Data();
1526 if ( child->GetValidator() &&
1527 !child->GetValidator()->TransferToWindow() )
1528 {
1529 wxLogError("Could not transfer data to window.");
1530 return FALSE;
1531 }
1532
1533 node = node->Next();
1534 }
1535 return TRUE;
4bb6408c
JS
1536}
1537
1538// Transfer values from controls. If returns FALSE,
1539// validation failed: don't quit
1540bool wxWindow::TransferDataFromWindow()
1541{
2d120f83
JS
1542 wxNode *node = GetChildren().First();
1543 while ( node )
1544 {
1545 wxWindow *child = (wxWindow *)node->Data();
1546 if ( child->GetValidator() && !child->GetValidator()->TransferFromWindow() )
1547 {
1548 return FALSE;
1549 }
1550
1551 node = node->Next();
1552 }
1553 return TRUE;
4bb6408c
JS
1554}
1555
1556bool wxWindow::Validate()
1557{
2d120f83
JS
1558 wxNode *node = GetChildren().First();
1559 while ( node )
1560 {
1561 wxWindow *child = (wxWindow *)node->Data();
1562 if ( child->GetValidator() && /* child->GetValidator()->Ok() && */ !child->GetValidator()->Validate(this) )
1563 {
1564 return FALSE;
1565 }
1566
1567 node = node->Next();
1568 }
1569 return TRUE;
4bb6408c
JS
1570}
1571
1572// Get the window with the focus
1573wxWindow *wxWindow::FindFocus()
1574{
1575 // TODO
1576 return NULL;
1577}
1578
1579void wxWindow::AddChild(wxWindow *child)
1580{
c0ed460c 1581 GetChildren().Append(child);
4bb6408c
JS
1582 child->m_windowParent = this;
1583}
1584
1585void wxWindow::RemoveChild(wxWindow *child)
1586{
c0ed460c 1587 GetChildren().DeleteObject(child);
4bb6408c
JS
1588 child->m_windowParent = NULL;
1589}
1590
4fabb575
JS
1591// Reparents this window to have the new parent.
1592bool wxWindow::Reparent(wxWindow* WXUNUSED(parent))
1593{
1594 // For now, we indicate that this isn't implemented.
1595 return FALSE;
1596}
1597
4bb6408c
JS
1598void wxWindow::DestroyChildren()
1599{
c0ed460c 1600 wxNode *node = GetChildren().First();
02800301
JS
1601 while (node)
1602 {
2d120f83
JS
1603 wxNode* next = node->Next();
1604 wxWindow* child = (wxWindow*) node->Data();
1605 delete child;
1606 node = next;
02800301 1607 }
c0ed460c 1608 GetChildren().Clear();
02800301 1609#if 0
4bb6408c 1610 wxNode *node;
c0ed460c 1611 while ((node = GetChildren().First()) != (wxNode *)NULL) {
2d120f83
JS
1612 wxWindow *child;
1613 if ((child = (wxWindow *)node->Data()) != (wxWindow *)NULL) {
1614 delete child;
1615 if ( GetChildren().Member(child) )
1616 delete node;
1617 }
4bb6408c 1618 } /* while */
02800301 1619#endif
4bb6408c
JS
1620}
1621
1622void wxWindow::MakeModal(bool modal)
1623{
2d120f83
JS
1624 // Disable all other windows
1625 if (this->IsKindOf(CLASSINFO(wxDialog)) || this->IsKindOf(CLASSINFO(wxFrame)))
4bb6408c 1626 {
2d120f83
JS
1627 wxNode *node = wxTopLevelWindows.First();
1628 while (node)
1629 {
1630 wxWindow *win = (wxWindow *)node->Data();
1631 if (win != this)
1632 win->Enable(!modal);
1633
1634 node = node->Next();
1635 }
4bb6408c 1636 }
4bb6408c
JS
1637}
1638
02e8b2f9
JS
1639// If nothing defined for this, try the parent.
1640// E.g. we may be a button loaded from a resource, with no callback function
1641// defined.
1642void wxWindow::OnCommand(wxWindow& win, wxCommandEvent& event)
1643{
2d120f83
JS
1644 if (GetEventHandler()->ProcessEvent(event) )
1645 return;
1646 if (m_windowParent)
1647 m_windowParent->GetEventHandler()->OnCommand(win, event);
02e8b2f9
JS
1648}
1649
4bb6408c
JS
1650void wxWindow::SetConstraints(wxLayoutConstraints *c)
1651{
2d120f83
JS
1652 if (m_constraints)
1653 {
1654 UnsetConstraints(m_constraints);
1655 delete m_constraints;
1656 }
1657 m_constraints = c;
1658 if (m_constraints)
1659 {
1660 // Make sure other windows know they're part of a 'meaningful relationship'
1661 if (m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this))
1662 m_constraints->left.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1663 if (m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this))
1664 m_constraints->top.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1665 if (m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this))
1666 m_constraints->right.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1667 if (m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this))
1668 m_constraints->bottom.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1669 if (m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this))
1670 m_constraints->width.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1671 if (m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this))
1672 m_constraints->height.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1673 if (m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this))
1674 m_constraints->centreX.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1675 if (m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this))
1676 m_constraints->centreY.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1677 }
4bb6408c
JS
1678}
1679
1680// This removes any dangling pointers to this window
1681// in other windows' constraintsInvolvedIn lists.
1682void wxWindow::UnsetConstraints(wxLayoutConstraints *c)
1683{
2d120f83
JS
1684 if (c)
1685 {
1686 if (c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this))
1687 c->left.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1688 if (c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this))
1689 c->top.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1690 if (c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this))
1691 c->right.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1692 if (c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this))
1693 c->bottom.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1694 if (c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this))
1695 c->width.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1696 if (c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this))
1697 c->height.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1698 if (c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this))
1699 c->centreX.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1700 if (c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this))
1701 c->centreY.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1702 }
4bb6408c
JS
1703}
1704
1705// Back-pointer to other windows we're involved with, so if we delete
1706// this window, we must delete any constraints we're involved with.
1707void wxWindow::AddConstraintReference(wxWindow *otherWin)
1708{
2d120f83
JS
1709 if (!m_constraintsInvolvedIn)
1710 m_constraintsInvolvedIn = new wxList;
1711 if (!m_constraintsInvolvedIn->Member(otherWin))
1712 m_constraintsInvolvedIn->Append(otherWin);
4bb6408c
JS
1713}
1714
1715// REMOVE back-pointer to other windows we're involved with.
1716void wxWindow::RemoveConstraintReference(wxWindow *otherWin)
1717{
2d120f83
JS
1718 if (m_constraintsInvolvedIn)
1719 m_constraintsInvolvedIn->DeleteObject(otherWin);
4bb6408c
JS
1720}
1721
1722// Reset any constraints that mention this window
1723void wxWindow::DeleteRelatedConstraints()
1724{
2d120f83 1725 if (m_constraintsInvolvedIn)
4bb6408c 1726 {
2d120f83
JS
1727 wxNode *node = m_constraintsInvolvedIn->First();
1728 while (node)
1729 {
1730 wxWindow *win = (wxWindow *)node->Data();
1731 wxNode *next = node->Next();
1732 wxLayoutConstraints *constr = win->GetConstraints();
1733
1734 // Reset any constraints involving this window
1735 if (constr)
1736 {
1737 constr->left.ResetIfWin((wxWindow *)this);
1738 constr->top.ResetIfWin((wxWindow *)this);
1739 constr->right.ResetIfWin((wxWindow *)this);
1740 constr->bottom.ResetIfWin((wxWindow *)this);
1741 constr->width.ResetIfWin((wxWindow *)this);
1742 constr->height.ResetIfWin((wxWindow *)this);
1743 constr->centreX.ResetIfWin((wxWindow *)this);
1744 constr->centreY.ResetIfWin((wxWindow *)this);
1745 }
1746 delete node;
1747 node = next;
1748 }
1749 delete m_constraintsInvolvedIn;
1750 m_constraintsInvolvedIn = NULL;
4bb6408c 1751 }
4bb6408c
JS
1752}
1753
1754void wxWindow::SetSizer(wxSizer *sizer)
1755{
2d120f83
JS
1756 m_windowSizer = sizer;
1757 if (sizer)
1758 sizer->SetSizerParent((wxWindow *)this);
4bb6408c
JS
1759}
1760
1761/*
2d120f83
JS
1762* New version
1763*/
4bb6408c
JS
1764
1765bool wxWindow::Layout()
1766{
2d120f83
JS
1767 if (GetConstraints())
1768 {
1769 int w, h;
1770 GetClientSize(&w, &h);
1771 GetConstraints()->width.SetValue(w);
1772 GetConstraints()->height.SetValue(h);
1773 }
1774
1775 // If top level (one sizer), evaluate the sizer's constraints.
1776 if (GetSizer())
1777 {
1778 int noChanges;
1779 GetSizer()->ResetConstraints(); // Mark all constraints as unevaluated
1780 GetSizer()->LayoutPhase1(&noChanges);
1781 GetSizer()->LayoutPhase2(&noChanges);
1782 GetSizer()->SetConstraintSizes(); // Recursively set the real window sizes
1783 return TRUE;
1784 }
1785 else
1786 {
1787 // Otherwise, evaluate child constraints
1788 ResetConstraints(); // Mark all constraints as unevaluated
1789 DoPhase(1); // Just one phase need if no sizers involved
1790 DoPhase(2);
1791 SetConstraintSizes(); // Recursively set the real window sizes
1792 }
1793 return TRUE;
4bb6408c
JS
1794}
1795
1796
1797// Do a phase of evaluating constraints:
1798// the default behaviour. wxSizers may do a similar
1799// thing, but also impose their own 'constraints'
1800// and order the evaluation differently.
1801bool wxWindow::LayoutPhase1(int *noChanges)
1802{
2d120f83
JS
1803 wxLayoutConstraints *constr = GetConstraints();
1804 if (constr)
1805 {
1806 return constr->SatisfyConstraints((wxWindow *)this, noChanges);
1807 }
1808 else
1809 return TRUE;
4bb6408c
JS
1810}
1811
1812bool wxWindow::LayoutPhase2(int *noChanges)
1813{
2d120f83
JS
1814 *noChanges = 0;
1815
1816 // Layout children
1817 DoPhase(1);
1818 DoPhase(2);
1819 return TRUE;
4bb6408c
JS
1820}
1821
1822// Do a phase of evaluating child constraints
1823bool wxWindow::DoPhase(int phase)
1824{
2d120f83
JS
1825 int noIterations = 0;
1826 int maxIterations = 500;
1827 int noChanges = 1;
1828 int noFailures = 0;
1829 wxList succeeded;
1830 while ((noChanges > 0) && (noIterations < maxIterations))
4bb6408c 1831 {
2d120f83
JS
1832 noChanges = 0;
1833 noFailures = 0;
1834 wxNode *node = GetChildren().First();
1835 while (node)
4bb6408c 1836 {
2d120f83
JS
1837 wxWindow *child = (wxWindow *)node->Data();
1838 if (!child->IsKindOf(CLASSINFO(wxFrame)) && !child->IsKindOf(CLASSINFO(wxDialog)))
4bb6408c 1839 {
2d120f83
JS
1840 wxLayoutConstraints *constr = child->GetConstraints();
1841 if (constr)
1842 {
1843 if (succeeded.Member(child))
1844 {
1845 }
1846 else
1847 {
1848 int tempNoChanges = 0;
1849 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
1850 noChanges += tempNoChanges;
1851 if (success)
1852 {
1853 succeeded.Append(child);
1854 }
1855 }
1856 }
4bb6408c 1857 }
2d120f83 1858 node = node->Next();
4bb6408c 1859 }
2d120f83 1860 noIterations ++;
4bb6408c 1861 }
2d120f83 1862 return TRUE;
4bb6408c
JS
1863}
1864
1865void wxWindow::ResetConstraints()
1866{
2d120f83
JS
1867 wxLayoutConstraints *constr = GetConstraints();
1868 if (constr)
1869 {
1870 constr->left.SetDone(FALSE);
1871 constr->top.SetDone(FALSE);
1872 constr->right.SetDone(FALSE);
1873 constr->bottom.SetDone(FALSE);
1874 constr->width.SetDone(FALSE);
1875 constr->height.SetDone(FALSE);
1876 constr->centreX.SetDone(FALSE);
1877 constr->centreY.SetDone(FALSE);
1878 }
1879 wxNode *node = GetChildren().First();
1880 while (node)
1881 {
1882 wxWindow *win = (wxWindow *)node->Data();
1883 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
1884 win->ResetConstraints();
1885 node = node->Next();
1886 }
4bb6408c
JS
1887}
1888
1889// Need to distinguish between setting the 'fake' size for
1890// windows and sizers, and setting the real values.
1891void wxWindow::SetConstraintSizes(bool recurse)
1892{
2d120f83
JS
1893 wxLayoutConstraints *constr = GetConstraints();
1894 if (constr && constr->left.GetDone() && constr->right.GetDone() &&
1895 constr->width.GetDone() && constr->height.GetDone())
4bb6408c 1896 {
2d120f83
JS
1897 int x = constr->left.GetValue();
1898 int y = constr->top.GetValue();
1899 int w = constr->width.GetValue();
1900 int h = constr->height.GetValue();
1901
1902 // If we don't want to resize this window, just move it...
1903 if ((constr->width.GetRelationship() != wxAsIs) ||
1904 (constr->height.GetRelationship() != wxAsIs))
1905 {
1906 // Calls Layout() recursively. AAAGH. How can we stop that.
1907 // Simply take Layout() out of non-top level OnSizes.
1908 SizerSetSize(x, y, w, h);
1909 }
1910 else
1911 {
1912 SizerMove(x, y);
1913 }
4bb6408c 1914 }
2d120f83 1915 else if (constr)
4bb6408c 1916 {
2d120f83
JS
1917 char *windowClass = this->GetClassInfo()->GetClassName();
1918
1919 wxString winName;
1920 if (GetName() == "")
1921 winName = "unnamed";
1922 else
1923 winName = GetName();
1924 wxDebugMsg("Constraint(s) not satisfied for window of type %s, name %s:\n", (const char *)windowClass, (const char *)winName);
1925 if (!constr->left.GetDone())
1926 wxDebugMsg(" unsatisfied 'left' constraint.\n");
1927 if (!constr->right.GetDone())
1928 wxDebugMsg(" unsatisfied 'right' constraint.\n");
1929 if (!constr->width.GetDone())
1930 wxDebugMsg(" unsatisfied 'width' constraint.\n");
1931 if (!constr->height.GetDone())
1932 wxDebugMsg(" unsatisfied 'height' constraint.\n");
1933 wxDebugMsg("Please check constraints: try adding AsIs() constraints.\n");
4bb6408c 1934 }
2d120f83
JS
1935
1936 if (recurse)
4bb6408c 1937 {
2d120f83
JS
1938 wxNode *node = GetChildren().First();
1939 while (node)
1940 {
1941 wxWindow *win = (wxWindow *)node->Data();
1942 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
1943 win->SetConstraintSizes();
1944 node = node->Next();
1945 }
4bb6408c 1946 }
4bb6408c
JS
1947}
1948
1949// This assumes that all sizers are 'on' the same
1950// window, i.e. the parent of this window.
1951void wxWindow::TransformSizerToActual(int *x, int *y) const
1952{
2d120f83
JS
1953 if (!m_sizerParent || m_sizerParent->IsKindOf(CLASSINFO(wxDialog)) ||
1954 m_sizerParent->IsKindOf(CLASSINFO(wxFrame)) )
1955 return;
4bb6408c 1956
2d120f83
JS
1957 int xp, yp;
1958 m_sizerParent->GetPosition(&xp, &yp);
1959 m_sizerParent->TransformSizerToActual(&xp, &yp);
1960 *x += xp;
1961 *y += yp;
4bb6408c
JS
1962}
1963
1964void wxWindow::SizerSetSize(int x, int y, int w, int h)
1965{
2d120f83
JS
1966 int xx = x;
1967 int yy = y;
1968 TransformSizerToActual(&xx, &yy);
1969 SetSize(xx, yy, w, h);
4bb6408c
JS
1970}
1971
1972void wxWindow::SizerMove(int x, int y)
1973{
2d120f83
JS
1974 int xx = x;
1975 int yy = y;
1976 TransformSizerToActual(&xx, &yy);
1977 Move(xx, yy);
4bb6408c
JS
1978}
1979
1980// Only set the size/position of the constraint (if any)
1981void wxWindow::SetSizeConstraint(int x, int y, int w, int h)
1982{
2d120f83
JS
1983 wxLayoutConstraints *constr = GetConstraints();
1984 if (constr)
4bb6408c 1985 {
2d120f83
JS
1986 if (x != -1)
1987 {
1988 constr->left.SetValue(x);
1989 constr->left.SetDone(TRUE);
1990 }
1991 if (y != -1)
1992 {
1993 constr->top.SetValue(y);
1994 constr->top.SetDone(TRUE);
1995 }
1996 if (w != -1)
1997 {
1998 constr->width.SetValue(w);
1999 constr->width.SetDone(TRUE);
2000 }
2001 if (h != -1)
2002 {
2003 constr->height.SetValue(h);
2004 constr->height.SetDone(TRUE);
2005 }
4bb6408c 2006 }
4bb6408c
JS
2007}
2008
2009void wxWindow::MoveConstraint(int x, int y)
2010{
2d120f83
JS
2011 wxLayoutConstraints *constr = GetConstraints();
2012 if (constr)
4bb6408c 2013 {
2d120f83
JS
2014 if (x != -1)
2015 {
2016 constr->left.SetValue(x);
2017 constr->left.SetDone(TRUE);
2018 }
2019 if (y != -1)
2020 {
2021 constr->top.SetValue(y);
2022 constr->top.SetDone(TRUE);
2023 }
4bb6408c 2024 }
4bb6408c
JS
2025}
2026
2027void wxWindow::GetSizeConstraint(int *w, int *h) const
2028{
2d120f83
JS
2029 wxLayoutConstraints *constr = GetConstraints();
2030 if (constr)
2031 {
2032 *w = constr->width.GetValue();
2033 *h = constr->height.GetValue();
2034 }
2035 else
2036 GetSize(w, h);
4bb6408c
JS
2037}
2038
2039void wxWindow::GetClientSizeConstraint(int *w, int *h) const
2040{
2d120f83
JS
2041 wxLayoutConstraints *constr = GetConstraints();
2042 if (constr)
2043 {
2044 *w = constr->width.GetValue();
2045 *h = constr->height.GetValue();
2046 }
2047 else
2048 GetClientSize(w, h);
4bb6408c
JS
2049}
2050
2051void wxWindow::GetPositionConstraint(int *x, int *y) const
2052{
2d120f83
JS
2053 wxLayoutConstraints *constr = GetConstraints();
2054 if (constr)
2055 {
2056 *x = constr->left.GetValue();
2057 *y = constr->top.GetValue();
2058 }
2059 else
2060 GetPosition(x, y);
4bb6408c
JS
2061}
2062
2063bool wxWindow::Close(bool force)
2064{
2d120f83
JS
2065 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
2066 event.SetEventObject(this);
e3065973 2067#if WXWIN_COMPATIBILITY
2d120f83 2068 event.SetForce(force);
e3065973
JS
2069#endif
2070 event.SetCanVeto(!force);
2d120f83
JS
2071
2072 return GetEventHandler()->ProcessEvent(event);
4bb6408c
JS
2073}
2074
2075wxObject* wxWindow::GetChild(int number) const
2076{
2d120f83
JS
2077 // Return a pointer to the Nth object in the window
2078 wxNode *node = GetChildren().First();
2079 int n = number;
2080 while (node && n--)
2081 node = node->Next() ;
2082 if (node)
2083 {
2084 wxObject *obj = (wxObject *)node->Data();
2085 return(obj) ;
2086 }
2087 else
2088 return NULL ;
4bb6408c
JS
2089}
2090
2091void wxWindow::OnDefaultAction(wxControl *initiatingItem)
2092{
2093 // Obsolete function
2094}
2095
2096void wxWindow::Clear()
2097{
e97f20a0 2098 wxClientDC dc(this);
4bb6408c
JS
2099 wxBrush brush(GetBackgroundColour(), wxSOLID);
2100 dc.SetBackground(brush);
2101 dc.Clear();
2102}
2103
2104// Fits the panel around the items
2105void wxWindow::Fit()
2106{
2d120f83
JS
2107 int maxX = 0;
2108 int maxY = 0;
2109 wxNode *node = GetChildren().First();
2110 while ( node )
2111 {
2112 wxWindow *win = (wxWindow *)node->Data();
2113 int wx, wy, ww, wh;
2114 win->GetPosition(&wx, &wy);
2115 win->GetSize(&ww, &wh);
2116 if ( wx + ww > maxX )
2117 maxX = wx + ww;
2118 if ( wy + wh > maxY )
2119 maxY = wy + wh;
2120
2121 node = node->Next();
2122 }
2123 SetClientSize(maxX + 5, maxY + 5);
4bb6408c
JS
2124}
2125
2126void wxWindow::SetValidator(const wxValidator& validator)
2127{
2d120f83
JS
2128 if ( m_windowValidator )
2129 delete m_windowValidator;
2130 m_windowValidator = validator.Clone();
2131
2132 if ( m_windowValidator )
2133 m_windowValidator->SetWindow(this) ;
3dd4e4e0
JS
2134}
2135
2136void wxWindow::SetClientObject( wxClientData *data )
2137{
2d120f83
JS
2138 if (m_clientObject) delete m_clientObject;
2139 m_clientObject = data;
3dd4e4e0
JS
2140}
2141
2142wxClientData *wxWindow::GetClientObject()
2143{
2d120f83 2144 return m_clientObject;
3dd4e4e0
JS
2145}
2146
2147void wxWindow::SetClientData( void *data )
2148{
2d120f83 2149 m_clientData = data;
3dd4e4e0
JS
2150}
2151
2152void *wxWindow::GetClientData()
2153{
2d120f83 2154 return m_clientData;
4bb6408c
JS
2155}
2156
2157// Find a window by id or name
2158wxWindow *wxWindow::FindWindow(long id)
2159{
2d120f83
JS
2160 if ( GetId() == id)
2161 return this;
2162
2163 wxNode *node = GetChildren().First();
2164 while ( node )
2165 {
2166 wxWindow *child = (wxWindow *)node->Data();
2167 wxWindow *found = child->FindWindow(id);
2168 if ( found )
2169 return found;
2170 node = node->Next();
2171 }
2172 return NULL;
4bb6408c
JS
2173}
2174
2175wxWindow *wxWindow::FindWindow(const wxString& name)
2176{
2d120f83
JS
2177 if ( GetName() == name)
2178 return this;
2179
2180 wxNode *node = GetChildren().First();
2181 while ( node )
2182 {
2183 wxWindow *child = (wxWindow *)node->Data();
2184 wxWindow *found = child->FindWindow(name);
2185 if ( found )
2186 return found;
2187 node = node->Next();
2188 }
2189 return NULL;
4bb6408c
JS
2190}
2191
2192void wxWindow::OnIdle(wxIdleEvent& event)
2193{
4bb6408c
JS
2194 // This calls the UI-update mechanism (querying windows for
2195 // menu/toolbar/control state information)
0d57be45 2196 UpdateWindowUI();
4bb6408c
JS
2197}
2198
2199// Raise the window to the top of the Z order
2200void wxWindow::Raise()
2201{
02e8b2f9
JS
2202 Window window = XtWindow((Widget) GetTopWidget());
2203 XRaiseWindow(XtDisplay((Widget) GetTopWidget()), window);
4bb6408c
JS
2204}
2205
2206// Lower the window to the bottom of the Z order
2207void wxWindow::Lower()
2208{
02e8b2f9
JS
2209 Window window = XtWindow((Widget) GetTopWidget());
2210 XLowerWindow(XtDisplay((Widget) GetTopWidget()), window);
4bb6408c
JS
2211}
2212
2213bool wxWindow::AcceptsFocus() const
2214{
2d120f83 2215 return IsShown() && IsEnabled();
4bb6408c
JS
2216}
2217
2218// Update region access
55acd85e 2219wxRegion& wxWindow::GetUpdateRegion() const
4bb6408c 2220{
55acd85e 2221 return (wxRegion&) m_updateRegion;
4bb6408c
JS
2222}
2223
2224bool wxWindow::IsExposed(int x, int y, int w, int h) const
2225{
2226 return (m_updateRegion.Contains(x, y, w, h) != wxOutRegion);
2227}
2228
2229bool wxWindow::IsExposed(const wxPoint& pt) const
2230{
2231 return (m_updateRegion.Contains(pt) != wxOutRegion);
2232}
2233
2234bool wxWindow::IsExposed(const wxRect& rect) const
2235{
2236 return (m_updateRegion.Contains(rect) != wxOutRegion);
2237}
2238
2239/*
2d120f83
JS
2240* Allocates control IDs
2241*/
4bb6408c
JS
2242
2243int wxWindow::NewControlId()
2244{
2245 static int s_controlId = 0;
2246 s_controlId ++;
2247 return s_controlId;
2248}
2249
2250void wxWindow::SetAcceleratorTable(const wxAcceleratorTable& accel)
2251{
2252 m_acceleratorTable = accel;
2253}
2254
2255// All widgets should have this as their resize proc.
2256// OnSize sent to wxWindow via client data.
2257void wxWidgetResizeProc(Widget w, XConfigureEvent *event, String args[], int *num_args)
2258{
2259 wxWindow *win = (wxWindow *)wxWidgetHashTable->Get((long)w);
2260 if (!win)
2261 return;
2d120f83 2262
4bb6408c
JS
2263 if (win->PreResize())
2264 {
2265 int width, height;
2266 win->GetSize(&width, &height);
2267 wxSizeEvent sizeEvent(wxSize(width, height), win->GetId());
2268 sizeEvent.SetEventObject(win);
2269 win->GetEventHandler()->ProcessEvent(sizeEvent);
2270 }
2271}
2272
2273bool wxAddWindowToTable(Widget w, wxWindow *win)
2274{
2d120f83
JS
2275 wxWindow *oldItem = NULL;
2276 // printf("Adding widget %ld, name = %s\n", w, win->GetClassInfo()->GetClassName());
2277 if ((oldItem = (wxWindow *)wxWidgetHashTable->Get ((long) w)))
2278 {
2279 wxLogError("Widget table clash: new widget is %ld, %s", (long)w, win->GetClassInfo()->GetClassName());
2280 return FALSE;
2281 }
2282
2283 wxWidgetHashTable->Put ((long) w, win);
2284 return TRUE;
4bb6408c
JS
2285}
2286
2287wxWindow *wxGetWindowFromTable(Widget w)
2288{
2d120f83 2289 return (wxWindow *)wxWidgetHashTable->Get ((long) w);
4bb6408c
JS
2290}
2291
2292void wxDeleteWindowFromTable(Widget w)
2293{
2d120f83 2294 wxWidgetHashTable->Delete((long)w);
4bb6408c 2295}
2d120f83 2296
4bb6408c
JS
2297// Get the underlying X window and display
2298WXWindow wxWindow::GetXWindow() const
2299{
50414e24 2300 return (WXWindow) XtWindow((Widget) GetMainWidget());
4bb6408c
JS
2301}
2302
2303WXDisplay *wxWindow::GetXDisplay() const
2304{
50414e24 2305 return (WXDisplay*) XtDisplay((Widget) GetMainWidget());
4bb6408c
JS
2306}
2307
2308WXWidget wxWindow::GetMainWidget() const
2309{
50414e24 2310 if (m_drawingArea)
2d120f83 2311 return m_drawingArea;
50414e24 2312 else
2d120f83 2313 return m_mainWidget;
50414e24
JS
2314}
2315
2316WXWidget wxWindow::GetClientWidget() const
2317{
2318 if (m_drawingArea != (WXWidget) 0)
2319 return m_drawingArea;
2320 else
02e8b2f9
JS
2321 return GetMainWidget();
2322}
2323
2324WXWidget wxWindow::GetTopWidget() const
2325{
2326 return GetMainWidget();
4bb6408c
JS
2327}
2328
50414e24 2329void wxCanvasRepaintProc (Widget drawingArea, XtPointer clientData,
2d120f83 2330 XmDrawingAreaCallbackStruct * cbs)
50414e24
JS
2331{
2332 if (!wxWidgetHashTable->Get ((long) (Widget) drawingArea))
2d120f83
JS
2333 return;
2334
50414e24 2335 XEvent * event = cbs->event;
55acd85e
JS
2336 wxWindow * win = (wxWindow *) clientData;
2337 Display * display = (Display *) win->GetXDisplay();
2d120f83 2338
50414e24
JS
2339 switch (event->type)
2340 {
2d120f83 2341 case Expose:
55acd85e
JS
2342 {
2343 wxRect* rect = new wxRect(event->xexpose.x, event->xexpose.y,
2d120f83
JS
2344 event->xexpose.width, event->xexpose.height);
2345 /*
2346 cout << "Expose proc. wxRect: " << rect->x << ", " << rect->y << ", ";
2347 cout << rect->width << ", " << rect->height << "\n\n";
2348 */
2349
55acd85e 2350 win->m_updateRects.Append((wxObject*) rect);
2d120f83 2351
55acd85e
JS
2352 if (event -> xexpose.count == 0)
2353 {
2354 wxPaintEvent event(win->GetId());
2355 event.SetEventObject(win);
2356 win->GetEventHandler()->ProcessEvent(event);
2d120f83 2357
55acd85e
JS
2358 win->ClearUpdateRects();
2359 }
2360 break;
2361 }
2d120f83 2362 default:
55acd85e 2363 {
2d120f83
JS
2364 cout << "\n\nNew Event ! is = " << event -> type << "\n";
2365 break;
55acd85e 2366 }
50414e24 2367 }
50414e24
JS
2368}
2369
2370// Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
2371void
2372wxCanvasEnterLeave (Widget drawingArea, XtPointer clientData, XCrossingEvent * event)
2373{
2d120f83
JS
2374 XmDrawingAreaCallbackStruct cbs;
2375 XEvent ev;
2376
2377 //if (event->mode!=NotifyNormal)
2378 // return ;
2379
2380 // ev = *((XEvent *) event); // Causes Purify error (copying too many bytes)
2381 ((XCrossingEvent &) ev) = *event;
2382
2383 cbs.reason = XmCR_INPUT;
2384 cbs.event = &ev;
2385
2386 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
50414e24
JS
2387}
2388
2389// Fix to make it work under Motif 1.0 (!)
2390void wxCanvasMotionEvent (Widget drawingArea, XButtonEvent * event)
2391{
2392#if XmVersion<=1000
2d120f83
JS
2393
2394 XmDrawingAreaCallbackStruct cbs;
2395 XEvent ev;
2396
2397 //ev.xbutton = *event;
2398 ev = *((XEvent *) event);
2399 cbs.reason = XmCR_INPUT;
2400 cbs.event = &ev;
2401
2402 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
50414e24
JS
2403#endif
2404}
2405
2406void wxCanvasInputEvent (Widget drawingArea, XtPointer data, XmDrawingAreaCallbackStruct * cbs)
2407{
2d120f83
JS
2408 wxWindow *canvas = (wxWindow *) wxWidgetHashTable->Get ((long) (Widget) drawingArea);
2409 XEvent local_event;
2410
2411 if (canvas==NULL)
2412 return ;
2413
2414 if (cbs->reason != XmCR_INPUT)
2415 return;
2416
2417 local_event = *(cbs->event); // We must keep a copy!
2418
f57fe24c
JS
2419 /*
2420 switch (local_event.xany.type)
2d120f83
JS
2421 {
2422 case EnterNotify:
2423 cout << "EnterNotify\n";
2424 break;
2425 case LeaveNotify:
2426 cout << "LeaveNotify\n";
2427 break;
2428 case ButtonPress:
2429 cout << "ButtonPress\n";
2430 break;
2431 case ButtonRelease:
2432 cout << "ButtonRelease\n";
2433 break;
2434 case MotionNotify:
2435 cout << "MotionNotify\n";
2436 break;
2437 default:
2438 cout << "Something else\n";
2439 break;
f57fe24c 2440 }
2d120f83
JS
2441 */
2442
2443 switch (local_event.xany.type)
50414e24
JS
2444 {
2445 case EnterNotify:
2446 case LeaveNotify:
2447 case ButtonPress:
2448 case ButtonRelease:
2449 case MotionNotify:
2d120f83
JS
2450 {
2451 wxEventType eventType = wxEVT_NULL;
2452
2453 if (local_event.xany.type == EnterNotify)
2454 {
2455 //if (local_event.xcrossing.mode!=NotifyNormal)
2456 // return ; // Ignore grab events
2457 eventType = wxEVT_ENTER_WINDOW;
2458 // canvas->GetEventHandler()->OnSetFocus();
2459 }
2460 else if (local_event.xany.type == LeaveNotify)
2461 {
2462 //if (local_event.xcrossing.mode!=NotifyNormal)
2463 // return ; // Ignore grab events
2464 eventType = wxEVT_LEAVE_WINDOW;
2465 // canvas->GetEventHandler()->OnKillFocus();
2466 }
2467 else if (local_event.xany.type == MotionNotify)
2468 {
2469 eventType = wxEVT_MOTION;
2470 if (local_event.xmotion.is_hint == NotifyHint)
2471 {
2472 Window root, child;
2473 Display *dpy = XtDisplay (drawingArea);
2474
2475 XQueryPointer (dpy, XtWindow (drawingArea),
2476 &root, &child,
2477 &local_event.xmotion.x_root,
2478 &local_event.xmotion.y_root,
2479 &local_event.xmotion.x,
2480 &local_event.xmotion.y,
2481 &local_event.xmotion.state);
2482 }
2483 else
2484 {
2485 }
2486 }
2487
2488 else if (local_event.xany.type == ButtonPress)
2489 {
2490 if (local_event.xbutton.button == Button1)
2491 {
2492 eventType = wxEVT_LEFT_DOWN;
2493 canvas->m_button1Pressed = TRUE;
2494 }
2495 else if (local_event.xbutton.button == Button2)
2496 {
2497 eventType = wxEVT_MIDDLE_DOWN;
2498 canvas->m_button2Pressed = TRUE;
2499 }
2500 else if (local_event.xbutton.button == Button3)
2501 {
2502 eventType = wxEVT_RIGHT_DOWN;
2503 canvas->m_button3Pressed = TRUE;
2504 }
2505 }
2506 else if (local_event.xany.type == ButtonRelease)
2507 {
2508 if (local_event.xbutton.button == Button1)
2509 {
2510 eventType = wxEVT_LEFT_UP;
2511 canvas->m_button1Pressed = FALSE;
2512 }
2513 else if (local_event.xbutton.button == Button2)
2514 {
2515 eventType = wxEVT_MIDDLE_UP;
2516 canvas->m_button2Pressed = FALSE;
2517 }
2518 else if (local_event.xbutton.button == Button3)
2519 {
2520 eventType = wxEVT_RIGHT_UP;
2521 canvas->m_button3Pressed = FALSE;
2522 }
2523 }
2524
2525 wxMouseEvent wxevent (eventType);
2526 wxevent.m_eventHandle = (char *) &local_event;
2527
2528 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2529 || (event_left_is_down (&local_event)
2530 && (eventType != wxEVT_LEFT_UP)));
2531 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2532 || (event_middle_is_down (&local_event)
2533 && (eventType != wxEVT_MIDDLE_UP)));
2534 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2535 || (event_right_is_down (&local_event)
2536 && (eventType != wxEVT_RIGHT_UP)));
2537
2538 wxevent.m_shiftDown = local_event.xbutton.state & ShiftMask;
2539 wxevent.m_controlDown = local_event.xbutton.state & ControlMask;
2540 wxevent.m_altDown = local_event.xbutton.state & Mod3Mask;
2541 wxevent.m_metaDown = local_event.xbutton.state & Mod1Mask;
2542 wxevent.SetTimestamp(local_event.xbutton.time);
2543
2544 // Now check if we need to translate this event into a double click
2545 if (TRUE) // canvas->doubleClickAllowed)
2546 {
2547 if (wxevent.ButtonDown())
2548 {
2549 long dclickTime = XtGetMultiClickTime((Display*) wxGetDisplay()) ;
2550
2551 // get button and time-stamp
2552 int button = 0;
2553 if (wxevent.LeftDown()) button = 1;
2554 else if (wxevent.MiddleDown()) button = 2;
2555 else if (wxevent.RightDown()) button = 3;
2556 long ts = wxevent.GetTimestamp();
2557 // check, if single or double click
2558 if (canvas->m_lastButton && canvas->m_lastButton==button && (ts - canvas->m_lastTS) < dclickTime)
2559 {
2560 // I have a dclick
2561 canvas->m_lastButton = 0;
2562 switch ( eventType )
2563 {
2564 case wxEVT_LEFT_DOWN:
2565 wxevent.SetEventType(wxEVT_LEFT_DCLICK);
2566 break;
2567 case wxEVT_MIDDLE_DOWN:
2568 wxevent.SetEventType(wxEVT_MIDDLE_DCLICK);
2569 break;
2570 case wxEVT_RIGHT_DOWN:
2571 wxevent.SetEventType(wxEVT_RIGHT_DCLICK);
2572 break;
2573
2574 default :
2575 break;
2576 }
2577
2578 }
2579 else
2580 {
2581 // not fast enough or different button
2582 canvas->m_lastTS = ts;
2583 canvas->m_lastButton = button;
2584 }
2585 }
2586 }
2587
2588 wxevent.SetId(canvas->GetId());
2589 wxevent.SetEventObject(canvas);
2590 wxevent.m_x = local_event.xbutton.x;
2591 wxevent.m_y = local_event.xbutton.y;
2592 canvas->GetEventHandler()->ProcessEvent (wxevent);
2593 /*
2594 if (eventType == wxEVT_ENTER_WINDOW ||
2595 eventType == wxEVT_LEAVE_WINDOW ||
2596 eventType == wxEVT_MOTION
2597 )
2598 return;
2599 */
2600 break;
50414e24
JS
2601 }
2602 case KeyPress:
50414e24 2603 {
2d120f83
JS
2604 KeySym keySym;
2605 // XComposeStatus compose;
2606 // (void) XLookupString ((XKeyEvent *) & local_event, wxBuffer, 20, &keySym, &compose);
2607 (void) XLookupString ((XKeyEvent *) & local_event, wxBuffer, 20, &keySym, NULL);
2608 int id = wxCharCodeXToWX (keySym);
2609
2610 wxEventType eventType = wxEVT_CHAR;
50414e24 2611
2d120f83
JS
2612 // TODO: Is this the correct criterion for wxEVT_KEY_DOWN down versus wxEVT_CHAR?
2613 if (id > WXK_START) // Non-ASCII values
2614 eventType = wxEVT_KEY_DOWN;
2615
2616 wxKeyEvent event (eventType);
2617
2618 if (local_event.xkey.state & ShiftMask)
2619 event.m_shiftDown = TRUE;
2620 if (local_event.xkey.state & ControlMask)
2621 event.m_controlDown = TRUE;
2622 if (local_event.xkey.state & Mod3Mask)
2623 event.m_altDown = TRUE;
2624 if (local_event.xkey.state & Mod1Mask)
2625 event.m_metaDown = TRUE;
2626 event.SetEventObject(canvas);
2627 event.m_keyCode = id;
2628 event.SetTimestamp(local_event.xkey.time);
2629
2630 if (id > -1)
2631 {
2632 // Implement wxFrame::OnCharHook by checking ancestor.
2633 wxWindow *parent = canvas->GetParent();
2634 while (parent && !parent->IsKindOf(CLASSINFO(wxFrame)))
2635 parent = parent->GetParent();
2636
2637 if (parent)
2638 {
2639 event.SetEventType(wxEVT_CHAR_HOOK);
2640 if (parent->GetEventHandler()->ProcessEvent(event))
2641 return;
2642 event.SetEventType(wxEVT_CHAR);
2643 }
2644
2645 canvas->GetEventHandler()->ProcessEvent (event);
2646 }
2647 break;
2648 }
2649 case KeyRelease:
2650 {
2651 KeySym keySym;
2652 (void) XLookupString ((XKeyEvent *) & local_event, wxBuffer, 20, &keySym, NULL);
2653 int id = wxCharCodeXToWX (keySym);
2654
2655 wxKeyEvent event (wxEVT_KEY_UP);
2656
2657 if (local_event.xkey.state & ShiftMask)
2658 event.m_shiftDown = TRUE;
2659 if (local_event.xkey.state & ControlMask)
2660 event.m_controlDown = TRUE;
2661 if (local_event.xkey.state & Mod3Mask)
2662 event.m_altDown = TRUE;
2663 if (local_event.xkey.state & Mod1Mask)
2664 event.m_metaDown = TRUE;
2665 event.SetEventObject(canvas);
2666 event.m_keyCode = id;
2667 event.SetTimestamp(local_event.xkey.time);
2668
2669 if (id > -1)
2670 {
2671 canvas->GetEventHandler()->ProcessEvent (event);
2672 }
2673 break;
50414e24 2674 }
50414e24 2675 case FocusIn:
2d120f83
JS
2676 {
2677 if (local_event.xfocus.detail != NotifyPointer)
2678 {
2679 wxFocusEvent event(wxEVT_SET_FOCUS, canvas->GetId());
2680 event.SetEventObject(canvas);
2681 canvas->GetEventHandler()->ProcessEvent(event);
2682 }
2683 break;
50414e24 2684 }
50414e24 2685 case FocusOut:
2d120f83
JS
2686 {
2687 if (local_event.xfocus.detail != NotifyPointer)
2688 {
2689 wxFocusEvent event(wxEVT_KILL_FOCUS, canvas->GetId());
2690 event.SetEventObject(canvas);
2691 canvas->GetEventHandler()->ProcessEvent(event);
2692 }
2693 break;
2694 }
50414e24 2695 default:
2d120f83 2696 break;
50414e24
JS
2697 }
2698}
2699
2700void wxWindow::DoPaint()
2701{
2d120f83
JS
2702 //TODO : make a temporary gc so we can do the XCopyArea below
2703 if (0) // m_backingPixmap)
50414e24 2704 {
2d120f83
JS
2705 /*
2706 Widget drawingArea = (Widget) m_drawingArea;
2707 // int orig = GetDC()->GetLogicalFunction();
2708 // GetDC()->SetLogicalFunction (wxCOPY);
2709
50414e24
JS
2710 // TODO: it may not be necessary to store m_pixmapOffsetX/Y; we
2711 // should be able to calculate them.
2712 XCopyArea (XtDisplay (drawingArea), m_backingPixmap, XtWindow (drawingArea), GetDC ()->gc,
2d120f83
JS
2713 m_pixmapOffsetX, m_pixmapOffsetY,
2714 m_pixmapWidth, m_pixmapHeight,
2715 0, 0);
2716
2717 // GetDC()->SetLogicalFunction (orig);
2718 */
50414e24 2719 }
2d120f83 2720 else
50414e24
JS
2721 {
2722 wxPaintEvent event(GetId());
2723 event.SetEventObject(this);
2724 GetEventHandler()->ProcessEvent(event);
2725 }
2726}
2727
2728// SetSize, but as per old wxCanvas (with drawing widget etc.)
2729void wxWindow::CanvasSetSize (int x, int y, int w, int h, int sizeFlags)
2730{
2d120f83
JS
2731 // A bit of optimization to help sort out the flickers.
2732 int oldX, oldY, oldW, oldH;
2733 GetSize(& oldW, & oldH);
2734 GetPosition(& oldX, & oldY);
2735
2736 bool useOldPos = FALSE;
2737 bool useOldSize = FALSE;
2738
2739 if ((x == -1) && (x == -1) && ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0))
2740 useOldPos = TRUE;
2741 else if (x == oldX && y == oldY)
2742 useOldPos = TRUE;
2743
2744 if ((w == -1) && (h == -1))
2745 useOldSize = TRUE;
2746 else if (w == oldW && h == oldH)
2747 useOldSize = TRUE;
2748
2749 if (!wxNoOptimize::CanOptimize())
50414e24 2750 {
2d120f83 2751 useOldSize = FALSE; useOldPos = FALSE;
50414e24 2752 }
2d120f83
JS
2753
2754 if (useOldPos && useOldSize)
2755 return;
2756
2757 Widget drawingArea = (Widget) m_drawingArea;
2758 bool managed = XtIsManaged(m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2759
2760 if (managed)
2761 XtUnmanageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2762 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2763
2764 int xx = x; int yy = y;
2765 AdjustForParentClientOrigin(xx, yy, sizeFlags);
2766
2767 if (!useOldPos)
50414e24 2768 {
2d120f83
JS
2769 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2770 {
2771 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2772 XmNx, xx, NULL);
2773 }
2774
2775 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2776 {
2777 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2778 XmNy, yy, NULL);
2779 }
50414e24 2780 }
2d120f83
JS
2781
2782 if (!useOldSize)
50414e24 2783 {
2d120f83
JS
2784
2785 if (w > -1)
2786 {
2787 if (m_borderWidget)
2788 {
2789 XtVaSetValues ((Widget) m_borderWidget, XmNwidth, w, NULL);
2790 short thick, margin;
2791 XtVaGetValues ((Widget) m_borderWidget,
2792 XmNshadowThickness, &thick,
2793 XmNmarginWidth, &margin,
2794 NULL);
2795 w -= 2 * (thick + margin);
2796 }
2797
2798 XtVaSetValues ((Widget) m_scrolledWindow, XmNwidth, w, NULL);
2799
2800 Dimension spacing;
2801 Widget sbar;
2802 XtVaGetValues ((Widget) m_scrolledWindow,
2803 XmNspacing, &spacing,
2804 XmNverticalScrollBar, &sbar,
2805 NULL);
2806 Dimension wsbar;
2807 if (sbar)
2808 XtVaGetValues (sbar, XmNwidth, &wsbar, NULL);
2809 else
2810 wsbar = 0;
2811
2812 w -= (spacing + wsbar);
2813
2814 // XtVaSetValues ((Widget) m_drawingArea, XmNwidth, w, NULL);
2815 }
2816 if (h > -1)
2817 {
2818 if (m_borderWidget)
2819 {
2820 XtVaSetValues ((Widget) m_borderWidget, XmNheight, h, NULL);
2821 short thick, margin;
2822 XtVaGetValues ((Widget) m_borderWidget,
2823 XmNshadowThickness, &thick,
2824 XmNmarginHeight, &margin,
2825 NULL);
2826 h -= 2 * (thick + margin);
2827 }
2828
2829 XtVaSetValues ((Widget) m_scrolledWindow, XmNheight, h, NULL);
2830
2831 Dimension spacing;
2832 Widget sbar;
2833 XtVaGetValues ((Widget) m_scrolledWindow,
2834 XmNspacing, &spacing,
2835 XmNhorizontalScrollBar, &sbar,
2836 NULL);
2837 Dimension wsbar;
2838 if (sbar)
2839 XtVaGetValues (sbar, XmNheight, &wsbar, NULL);
2840 else
2841 wsbar = 0;
2842
2843 h -= (spacing + wsbar);
2844
2845 // XtVaSetValues ((Widget) m_drawingArea, XmNheight, h, NULL);
2846
2847 }
50414e24 2848 }
2d120f83
JS
2849
2850 if (managed)
2851 XtManageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2852 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2853
2854 /*
2855 int ww, hh;
2856 GetClientSize (&ww, &hh);
2857 wxSizeEvent sizeEvent(wxSize(ww, hh), GetId());
2858 sizeEvent.SetEventObject(this);
2859
2860 GetEventHandler()->ProcessEvent(sizeEvent);
2861 */
2862
50414e24
JS
2863}
2864
2865void wxWindow::CanvasSetClientSize (int w, int h)
2866{
2d120f83
JS
2867 Widget drawingArea = (Widget) m_drawingArea;
2868
2869 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2870
2871 if (w > -1)
2872 XtVaSetValues ((Widget) m_drawingArea, XmNwidth, w, NULL);
2873 if (h > -1)
2874 XtVaSetValues ((Widget) m_drawingArea, XmNheight, h, NULL);
2875 /* TODO: is this necessary?
2876 allowRepainting = FALSE;
2877
2878 XSync (XtDisplay (drawingArea), FALSE);
2879 XEvent event;
2880 while (XtAppPending (wxTheApp->appContext))
2881 {
2882 XFlush (XtDisplay (drawingArea));
2883 XtAppNextEvent (wxTheApp->appContext, &event);
2884 XtDispatchEvent (&event);
2885 }
2886 */
2887
2888 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2889
2890 /* TODO
2891 allowRepainting = TRUE;
2892 DoRefresh ();
2893 */
2894
2895 /*
2896 wxSizeEvent sizeEvent(wxSize(w, h), GetId());
2897 sizeEvent.SetEventObject(this);
2898
2899 GetEventHandler()->ProcessEvent(sizeEvent);
50414e24 2900 */
50414e24
JS
2901}
2902
2903void wxWindow::CanvasGetClientSize (int *w, int *h) const
2904{
2d120f83
JS
2905 // Must return the same thing that was set via SetClientSize
2906 Dimension xx, yy;
2907 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2908 *w = xx;
2909 *h = yy;
50414e24
JS
2910}
2911
2912void wxWindow::CanvasGetSize (int *w, int *h) const
2913{
2d120f83
JS
2914 Dimension xx, yy;
2915 if ((Widget) m_borderWidget)
2916 XtVaGetValues ((Widget) m_borderWidget, XmNwidth, &xx, XmNheight, &yy, NULL);
2917 else if ((Widget) m_scrolledWindow)
2918 XtVaGetValues ((Widget) m_scrolledWindow, XmNwidth, &xx, XmNheight, &yy, NULL);
2919 else
2920 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2921
2922 *w = xx;
2923 *h = yy;
50414e24
JS
2924}
2925
2926void wxWindow::CanvasGetPosition (int *x, int *y) const
2927{
a4294b78
JS
2928 Position xx, yy;
2929 XtVaGetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow, XmNx, &xx, XmNy, &yy, NULL);
2d120f83 2930
a4294b78
JS
2931 // We may be faking the client origin.
2932 // So a window that's really at (0, 30) may appear
2933 // (to wxWin apps) to be at (0, 0).
2934 if (GetParent())
2935 {
2936 wxPoint pt(GetParent()->GetClientAreaOrigin());
2937 xx -= pt.x;
2938 yy -= pt.y;
2939 }
2d120f83 2940
a4294b78
JS
2941 *x = xx;
2942 *y = yy;
50414e24 2943}
02e8b2f9
JS
2944
2945// Add to hash table, add event handler
2946bool wxWindow::AttachWidget (wxWindow* parent, WXWidget mainWidget,
2d120f83 2947 WXWidget formWidget, int x, int y, int width, int height)
02e8b2f9
JS
2948{
2949 wxAddWindowToTable((Widget) mainWidget, this);
2950 if (CanAddEventHandler())
2951 {
2952 XtAddEventHandler((Widget) mainWidget,
2d120f83
JS
2953 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
2954 False,
2955 wxPanelItemEventHandler,
2956 (XtPointer) this);
02e8b2f9 2957 }
2d120f83 2958
02e8b2f9
JS
2959 if (!formWidget)
2960 {
2961 XtTranslations ptr;
2962 XtOverrideTranslations ((Widget) mainWidget,
2d120f83 2963 ptr = XtParseTranslationTable ("<Configure>: resize()"));
02e8b2f9
JS
2964 XtFree ((char *) ptr);
2965 }
2d120f83 2966
02e8b2f9
JS
2967 // Some widgets have a parent form widget, e.g. wxRadioBox
2968 if (formWidget)
2969 {
2d120f83
JS
2970 if (!wxAddWindowToTable((Widget) formWidget, this))
2971 return FALSE;
2972
2973 XtTranslations ptr;
2974 XtOverrideTranslations ((Widget) formWidget,
2975 ptr = XtParseTranslationTable ("<Configure>: resize()"));
2976 XtFree ((char *) ptr);
02e8b2f9 2977 }
2d120f83 2978
02e8b2f9 2979 if (x == -1)
2d120f83 2980 x = 0;
02e8b2f9 2981 if (y == -1)
2d120f83 2982 y = 0;
02e8b2f9 2983 SetSize (x, y, width, height);
2d120f83 2984
02e8b2f9
JS
2985 return TRUE;
2986}
2987
2988// Remove event handler, remove from hash table
2989bool wxWindow::DetachWidget(WXWidget widget)
2990{
2991 if (CanAddEventHandler())
2992 {
2d120f83
JS
2993 XtRemoveEventHandler((Widget) widget,
2994 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
2995 False,
2996 wxPanelItemEventHandler,
2997 (XtPointer)this);
02e8b2f9 2998 }
2d120f83 2999
02e8b2f9
JS
3000 wxDeleteWindowFromTable((Widget) widget);
3001 return TRUE;
3002}
3003
3004void wxPanelItemEventHandler (Widget wid,
3005 XtPointer client_data,
3006 XEvent* event,
3007 Boolean *continueToDispatch)
3008{
2d120f83
JS
3009 // Widget can be a label or the actual widget.
3010
3011 wxWindow *window = (wxWindow *)wxWidgetHashTable->Get((long)wid);
3012 if (window)
02e8b2f9 3013 {
2d120f83
JS
3014 wxMouseEvent wxevent(0);
3015 if (wxTranslateMouseEvent(wxevent, window, wid, event))
3016 {
3017 window->GetEventHandler()->ProcessEvent(wxevent);
3018 }
02e8b2f9 3019 }
2d120f83
JS
3020 // TODO: probably the key to allowing default behaviour
3021 // to happen.
3022 // Say we set a m_doDefault flag to FALSE at the start of this
3023 // function. Then in e.g. wxWindow::OnMouseEvent we can
3024 // call Default() which sets this flag to TRUE, indicating
3025 // that default processing can happen. Thus, behaviour can appear
3026 // to be overridden just by adding an event handler and not calling
3027 // wxWindow::OnWhatever.
3028 // ALSO, maybe we can use this instead of the current way of handling
3029 // drawing area events, to simplify things.
3030 *continueToDispatch = True;
02e8b2f9
JS
3031}
3032
8aa04e8b 3033static void wxScrollBarCallback(Widget scrollbar, XtPointer clientData,
2d120f83 3034 XmScaleCallbackStruct *cbs)
47bc1060
JS
3035{
3036 Widget scrolledWindow = XtParent (scrollbar);
3037 wxWindow *win = (wxWindow *) wxWidgetHashTable->Get ((long) scrolledWindow);
3038 int orientation = (int) clientData;
2d120f83 3039
47bc1060
JS
3040 wxEventType eventType = wxEVT_NULL;
3041 switch (cbs->reason)
3042 {
2d120f83 3043 case XmCR_INCREMENT:
47bc1060
JS
3044 {
3045 eventType = wxEVT_SCROLL_LINEDOWN;
3046 break;
3047 }
2d120f83 3048 case XmCR_DECREMENT:
47bc1060
JS
3049 {
3050 eventType = wxEVT_SCROLL_LINEUP;
3051 break;
3052 }
2d120f83 3053 case XmCR_DRAG:
47bc1060
JS
3054 {
3055 eventType = wxEVT_SCROLL_THUMBTRACK;
3056 break;
3057 }
2d120f83 3058 case XmCR_VALUE_CHANGED:
47bc1060
JS
3059 {
3060 // TODO: Should this be intercepted too, or will it cause
3061 // duplicate events?
3062 eventType = wxEVT_SCROLL_THUMBTRACK;
3063 break;
3064 }
2d120f83 3065 case XmCR_PAGE_INCREMENT:
47bc1060
JS
3066 {
3067 eventType = wxEVT_SCROLL_PAGEDOWN;
3068 break;
3069 }
2d120f83 3070 case XmCR_PAGE_DECREMENT:
47bc1060
JS
3071 {
3072 eventType = wxEVT_SCROLL_PAGEUP;
3073 break;
3074 }
2d120f83 3075 case XmCR_TO_TOP:
47bc1060
JS
3076 {
3077 eventType = wxEVT_SCROLL_TOP;
3078 break;
3079 }
2d120f83 3080 case XmCR_TO_BOTTOM:
47bc1060
JS
3081 {
3082 eventType = wxEVT_SCROLL_BOTTOM;
3083 break;
3084 }
2d120f83 3085 default:
47bc1060
JS
3086 {
3087 // Should never get here
3088 wxFAIL_MSG("Unknown scroll event.");
3089 break;
3090 }
3091 }
2d120f83 3092
47bc1060
JS
3093 wxScrollEvent event(eventType, win->GetId());
3094 event.SetEventObject(win);
3095 event.SetPosition(cbs->value);
3096 event.SetOrientation( (orientation == XmHORIZONTAL) ? wxHORIZONTAL : wxVERTICAL );
2d120f83 3097
47bc1060
JS
3098 win->GetEventHandler()->ProcessEvent(event);
3099}
3100
02e8b2f9
JS
3101bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Widget widget, XEvent *xevent)
3102{
2d120f83
JS
3103 switch (xevent->xany.type)
3104 {
02e8b2f9
JS
3105 case EnterNotify:
3106 case LeaveNotify:
3107 case ButtonPress:
3108 case ButtonRelease:
3109 case MotionNotify:
02e8b2f9 3110 {
2d120f83
JS
3111 wxEventType eventType = wxEVT_NULL;
3112
3113 if (xevent->xany.type == LeaveNotify)
3114 {
3115 win->m_button1Pressed = FALSE;
3116 win->m_button2Pressed = FALSE;
3117 win->m_button3Pressed = FALSE;
3118 return FALSE;
3119 }
3120 else if (xevent->xany.type == MotionNotify)
3121 {
3122 eventType = wxEVT_MOTION;
3123 }
3124 else if (xevent->xany.type == ButtonPress)
3125 {
3126 if (xevent->xbutton.button == Button1)
3127 {
3128 eventType = wxEVT_LEFT_DOWN;
3129 win->m_button1Pressed = TRUE;
3130 }
3131 else if (xevent->xbutton.button == Button2)
3132 {
3133 eventType = wxEVT_MIDDLE_DOWN;
3134 win->m_button2Pressed = TRUE;
3135 }
3136 else if (xevent->xbutton.button == Button3)
3137 {
3138 eventType = wxEVT_RIGHT_DOWN;
3139 win->m_button3Pressed = TRUE;
3140 }
3141 }
3142 else if (xevent->xany.type == ButtonRelease)
3143 {
3144 if (xevent->xbutton.button == Button1)
3145 {
3146 eventType = wxEVT_LEFT_UP;
3147 win->m_button1Pressed = FALSE;
3148 }
3149 else if (xevent->xbutton.button == Button2)
3150 {
3151 eventType = wxEVT_MIDDLE_UP;
3152 win->m_button2Pressed = FALSE;
3153 }
3154 else if (xevent->xbutton.button == Button3)
3155 {
3156 eventType = wxEVT_RIGHT_UP;
3157 win->m_button3Pressed = FALSE;
3158 }
3159 else return FALSE;
3160 }
3161 else return FALSE;
3162
3163 wxevent.m_eventHandle = (char *)xevent;
3164 wxevent.SetEventType(eventType);
3165
3166 Position x1, y1;
3167 XtVaGetValues(widget, XmNx, &x1, XmNy, &y1, NULL);
3168
3169 int x2, y2;
3170 win->GetPosition(&x2, &y2);
3171
3172 // The button x/y must be translated to wxWindows
3173 // window space - the widget might be a label or button,
3174 // within a form.
3175 int dx = 0;
3176 int dy = 0;
3177 if (widget != (Widget)win->GetMainWidget())
3178 {
3179 dx = x1;
3180 dy = y1;
3181 }
3182
3183 wxevent.m_x = xevent->xbutton.x + dx;
3184 wxevent.m_y = xevent->xbutton.y + dy;
3185
3186 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
3187 || (event_left_is_down (xevent)
3188 && (eventType != wxEVT_LEFT_UP)));
3189 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
3190 || (event_middle_is_down (xevent)
3191 && (eventType != wxEVT_MIDDLE_UP)));
3192 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
3193 || (event_right_is_down (xevent)
3194 && (eventType != wxEVT_RIGHT_UP)));
3195
3196 wxevent.m_shiftDown = xevent->xbutton.state & ShiftMask;
3197 wxevent.m_controlDown = xevent->xbutton.state & ControlMask;
3198 return TRUE;
3199 }
02e8b2f9 3200 }
2d120f83 3201 return FALSE;
02e8b2f9
JS
3202}
3203
3204bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Widget widget, XEvent *xevent)
3205{
2d120f83
JS
3206 switch (xevent->xany.type)
3207 {
02e8b2f9 3208 case KeyPress:
2d120f83
JS
3209 {
3210 char buf[20];
3211
3212 KeySym keySym;
3213 // XComposeStatus compose;
3214 // (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, &compose);
3215 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
3216 int id = wxCharCodeXToWX (keySym);
3217
3218 if (xevent->xkey.state & ShiftMask)
3219 wxevent.m_shiftDown = TRUE;
3220 if (xevent->xkey.state & ControlMask)
3221 wxevent.m_controlDown = TRUE;
3222 if (xevent->xkey.state & Mod3Mask)
3223 wxevent.m_altDown = TRUE;
3224 if (xevent->xkey.state & Mod1Mask)
3225 wxevent.m_metaDown = TRUE;
3226 wxevent.SetEventObject(win);
3227 wxevent.m_keyCode = id;
3228 wxevent.SetTimestamp(xevent->xkey.time);
3229
3230 wxevent.m_x = xevent->xbutton.x;
3231 wxevent.m_y = xevent->xbutton.y;
3232
3233 if (id > -1)
3234 return TRUE;
3235 else
3236 return FALSE;
3237 break;
3238 }
02e8b2f9 3239 default:
2d120f83
JS
3240 break;
3241 }
3242 return FALSE;
02e8b2f9
JS
3243}
3244
02e8b2f9 3245#define YAllocColor XAllocColor
0d57be45
JS
3246XColor g_itemColors[5];
3247int wxComputeColours (Display *display, wxColour * back, wxColour * fore)
02e8b2f9 3248{
2d120f83
JS
3249 int result;
3250 static XmColorProc colorProc;
3251
3252 result = wxNO_COLORS;
3253
3254 if (back)
02e8b2f9 3255 {
2d120f83
JS
3256 g_itemColors[0].red = (((long) back->Red ()) << 8);
3257 g_itemColors[0].green = (((long) back->Green ()) << 8);
3258 g_itemColors[0].blue = (((long) back->Blue ()) << 8);
3259 g_itemColors[0].flags = DoRed | DoGreen | DoBlue;
3260 if (colorProc == (XmColorProc) NULL)
3261 {
3262 // Get a ptr to the actual function
3263 colorProc = XmSetColorCalculation ((XmColorProc) NULL);
3264 // And set it back to motif.
3265 XmSetColorCalculation (colorProc);
3266 }
3267 (*colorProc) (&g_itemColors[wxBACK_INDEX],
3268 &g_itemColors[wxFORE_INDEX],
3269 &g_itemColors[wxSELE_INDEX],
3270 &g_itemColors[wxTOPS_INDEX],
3271 &g_itemColors[wxBOTS_INDEX]);
3272 result = wxBACK_COLORS;
02e8b2f9 3273 }
2d120f83 3274 if (fore)
02e8b2f9 3275 {
2d120f83
JS
3276 g_itemColors[wxFORE_INDEX].red = (((long) fore->Red ()) << 8);
3277 g_itemColors[wxFORE_INDEX].green = (((long) fore->Green ()) << 8);
3278 g_itemColors[wxFORE_INDEX].blue = (((long) fore->Blue ()) << 8);
3279 g_itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue;
3280 if (result == wxNO_COLORS)
3281 result = wxFORE_COLORS;
02e8b2f9 3282 }
2d120f83
JS
3283
3284 Display *dpy = display;
3285 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
3286
3287 if (back)
02e8b2f9 3288 {
2d120f83
JS
3289 /* 5 Colours to allocate */
3290 for (int i = 0; i < 5; i++)
3291 if (!YAllocColor (dpy, cmap, &g_itemColors[i]))
3292 result = wxNO_COLORS;
02e8b2f9 3293 }
2d120f83 3294 else if (fore)
02e8b2f9 3295 {
2d120f83
JS
3296 /* Only 1 colour to allocate */
3297 if (!YAllocColor (dpy, cmap, &g_itemColors[wxFORE_INDEX]))
3298 result = wxNO_COLORS;
02e8b2f9 3299 }
2d120f83
JS
3300
3301 return (result);
3302
02e8b2f9
JS
3303}
3304
0d57be45
JS
3305// Changes the foreground and background colours to be derived
3306// from the current background colour.
3307// To change the foreground colour, you must call SetForegroundColour
3308// explicitly.
3309void wxWindow::ChangeBackgroundColour()
02e8b2f9 3310{
0d57be45
JS
3311 if (GetMainWidget())
3312 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
2d120f83 3313
b412f9be
JS
3314 // This not necessary
3315#if 0
2d120f83 3316
88150e60 3317 if (m_scrolledWindow && (GetMainWidget() != m_scrolledWindow))
b412f9be 3318 {
88150e60 3319 DoChangeBackgroundColour(m_scrolledWindow, m_backgroundColour);
b412f9be
JS
3320 // Have to set the scrollbar colours back since
3321 // the scrolled window seemed to change them
3322 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE) ;
2d120f83 3323
b412f9be 3324 if (m_hScrollBar)
2d120f83 3325 DoChangeBackgroundColour(m_hScrollBar, backgroundColour);
b412f9be 3326 if (m_vScrollBar)
2d120f83 3327 DoChangeBackgroundColour(m_vScrollBar, backgroundColour);
b412f9be
JS
3328 }
3329#endif
0d57be45 3330}
02e8b2f9 3331
0d57be45
JS
3332void wxWindow::ChangeForegroundColour()
3333{
3334 if (GetMainWidget())
3335 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
88150e60
JS
3336 if (m_scrolledWindow && (GetMainWidget() != m_scrolledWindow))
3337 DoChangeForegroundColour(m_scrolledWindow, m_foregroundColour);
0d57be45 3338}
02e8b2f9 3339
0d57be45
JS
3340// Change a widget's foreground and background colours.
3341
0d57be45
JS
3342void wxWindow::DoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
3343{
2d120f83
JS
3344 // When should we specify the foreground, if it's calculated
3345 // by wxComputeColours?
3346 // Solution: say we start with the default (computed) foreground colour.
3347 // If we call SetForegroundColour explicitly for a control or window,
3348 // then the foreground is changed.
3349 // Therefore SetBackgroundColour computes the foreground colour, and
3350 // SetForegroundColour changes the foreground colour. The ordering is
3351 // important.
3352
3353 XtVaSetValues ((Widget) widget,
3354 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
3355 NULL);
0d57be45
JS
3356}
3357
3358void wxWindow::DoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
3359{
2d120f83
JS
3360 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
3361 (wxColour*) NULL);
3362
0d57be45 3363 XtVaSetValues ((Widget) widget,
2d120f83
JS
3364 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
3365 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
3366 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
3367 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
3368 NULL);
3369
3370 if (changeArmColour)
3371 XtVaSetValues ((Widget) widget,
3372 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
3373 NULL);
02e8b2f9
JS
3374}
3375
0d57be45 3376void wxWindow::SetBackgroundColour(const wxColour& col)
02e8b2f9 3377{
0d57be45
JS
3378 m_backgroundColour = col;
3379 ChangeBackgroundColour();
3380}
3381
3382void wxWindow::SetForegroundColour(const wxColour& col)
3383{
3384 m_foregroundColour = col;
3385 ChangeForegroundColour();
3386}
3387
4b5f3fe6 3388void wxWindow::ChangeFont(bool keepOriginalSize)
0d57be45
JS
3389{
3390 // Note that this causes the widget to be resized back
3391 // to its original size! We therefore have to set the size
3392 // back again. TODO: a better way in Motif?
0d57be45
JS
3393 Widget w = (Widget) GetLabelWidget(); // Usually the main widget
3394 if (w && m_windowFont.Ok())
3395 {
3396 int width, height, width1, height1;
3397 GetSize(& width, & height);
2d120f83
JS
3398
3399 // lesstif 0.87 hangs here
32c69f2c 3400#ifndef LESSTIF_VERSION
0d57be45 3401 XtVaSetValues (w,
2d120f83
JS
3402 XmNfontList, (XmFontList) m_windowFont.GetFontList(1.0, XtDisplay(w)),
3403 NULL);
32c69f2c 3404#endif
2d120f83 3405
0d57be45 3406 GetSize(& width1, & height1);
4b5f3fe6 3407 if (keepOriginalSize && (width != width1 || height != height1))
0d57be45
JS
3408 {
3409 SetSize(-1, -1, width, height);
3410 }
3411 }
02e8b2f9 3412}
0d57be45
JS
3413
3414void wxWindow::SetFont(const wxFont& font)
3415{
3416 m_windowFont = font;
3417 ChangeFont();
3418}
3419
1f112209
JS
3420void wxWindow::SetToolTip(const wxString& tooltip)
3421{
3422 // TODO
3423}
3424
55acd85e
JS
3425void wxWindow::ClearUpdateRects()
3426{
3427 wxNode* node = m_updateRects.First();
3428 while (node)
3429 {
3430 wxRect* rect = (wxRect*) node->Data();
3431 delete rect;
3432 node = node->Next();
3433 }
3434 m_updateRects.Clear();
3435}
8aa04e8b
JS
3436
3437bool wxWindow::ProcessAccelerator(wxKeyEvent& event)
3438{
3439 if (!m_acceleratorTable.Ok())
3440 return FALSE;
2d120f83 3441
8aa04e8b
JS
3442 int count = m_acceleratorTable.GetCount();
3443 wxAcceleratorEntry* entries = m_acceleratorTable.GetEntries();
3444 int i;
3445 for (i = 0; i < count; i++)
3446 {
3447 wxAcceleratorEntry* entry = & (entries[i]);
3448 if (entry->MatchesEvent(event))
3449 {
3450 // Bingo, we have a match. Now find a control
3451 // that matches the entry command id.
2d120f83 3452
8aa04e8b
JS
3453 // Need to go up to the top of the window hierarchy,
3454 // since it might be e.g. a menu item
3455 wxWindow* parent = this;
3456 while (parent && !parent->IsKindOf(CLASSINFO(wxFrame)) && !parent->IsKindOf(CLASSINFO(wxDialog)))
3457 parent = parent->GetParent();
2d120f83 3458
8aa04e8b
JS
3459 if (!parent)
3460 return FALSE;
2d120f83 3461
8aa04e8b
JS
3462 if (parent->IsKindOf(CLASSINFO(wxFrame)))
3463 {
3464 // Try for a menu command
3465 wxFrame* frame = (wxFrame*) parent;
3466 if (frame->GetMenuBar())
3467 {
3468 wxMenuItem* item = frame->GetMenuBar()->FindItemForId(entry->GetCommand());
3469 if (item)
3470 {
3471 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, entry->GetCommand());
3472 commandEvent.SetEventObject(frame);
2d120f83 3473
8aa04e8b
JS
3474 // If ProcessEvent returns TRUE (it was handled), then
3475 // the calling code will skip the event handling.
3476 return frame->GetEventHandler()->ProcessEvent(commandEvent);
3477 }
3478 }
3479 }
2d120f83 3480
8aa04e8b
JS
3481 // Find a child matching the command id
3482 wxWindow* child = parent->FindWindow(entry->GetCommand());
2d120f83 3483
8aa04e8b
JS
3484 // No such child
3485 if (!child)
3486 return FALSE;
2d120f83 3487
8aa04e8b
JS
3488 // Now we process those kinds of windows that we can.
3489 // For now, only buttons.
3490 if (child->IsKindOf(CLASSINFO(wxButton)))
3491 {
3492 wxCommandEvent commandEvent (wxEVT_COMMAND_BUTTON_CLICKED, child->GetId());
3493 commandEvent.SetEventObject(child);
3494 return child->GetEventHandler()->ProcessEvent(commandEvent);
3495 }
2d120f83 3496
8aa04e8b
JS
3497 return FALSE;
3498 } // matches event
3499 }// for
2d120f83 3500
8aa04e8b
JS
3501 // We didn't match the key event against an accelerator.
3502 return FALSE;
3503}
3504
88150e60 3505/*
2d120f83
JS
3506* wxNoOptimize: switch off size optimization
3507*/
88150e60
JS
3508
3509int wxNoOptimize::m_count = 0;
3510
3511wxNoOptimize::wxNoOptimize()
3512{
2d120f83 3513 m_count ++;
88150e60
JS
3514}
3515
3516wxNoOptimize::~wxNoOptimize()
3517{
2d120f83 3518 m_count --;
88150e60
JS
3519}
3520
3521bool wxNoOptimize::CanOptimize()
3522{
2d120f83 3523 return (m_count == 0);
88150e60
JS
3524}
3525