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