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