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