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