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