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