]> git.saurik.com Git - wxWidgets.git/blob - src/motif/window.cpp
Misc. Motif; removed duplicate wxICON; variant compile fix; added wxString form
[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 Widget w = (Widget) GetLabelWidget(); // Usually the main widget
1271 if (w && m_windowFont.Ok())
1272 {
1273 XtVaSetValues (w,
1274 XmNfontList, (XmFontList) m_windowFont.GetFontList(1.0, XtDisplay(w)),
1275 NULL);
1276 }
1277 }
1278
1279 void wxWindow::OnChar(wxKeyEvent& event)
1280 {
1281 if ( event.KeyCode() == WXK_TAB ) {
1282 // propagate the TABs to the parent - it's up to it to decide what
1283 // to do with it
1284 if ( GetParent() ) {
1285 if ( GetParent()->ProcessEvent(event) )
1286 return;
1287 }
1288 }
1289 }
1290
1291 void wxWindow::OnPaint(wxPaintEvent& event)
1292 {
1293 Default();
1294 }
1295
1296 bool wxWindow::IsEnabled() const
1297 {
1298 // TODO. Is this right?
1299 // return XtGetSensitive((Widget) GetMainWidget());
1300 return FALSE;
1301 }
1302
1303 // Dialog support: override these and call
1304 // base class members to add functionality
1305 // that can't be done using validators.
1306 // NOTE: these functions assume that controls
1307 // are direct children of this window, not grandchildren
1308 // or other levels of descendant.
1309
1310 // Transfer values to controls. If returns FALSE,
1311 // it's an application error (pops up a dialog)
1312 bool wxWindow::TransferDataToWindow()
1313 {
1314 wxNode *node = GetChildren()->First();
1315 while ( node )
1316 {
1317 wxWindow *child = (wxWindow *)node->Data();
1318 if ( child->GetValidator() &&
1319 !child->GetValidator()->TransferToWindow() )
1320 {
1321 wxMessageBox("Application Error", "Could not transfer data to window", wxOK|wxICON_EXCLAMATION);
1322 return FALSE;
1323 }
1324
1325 node = node->Next();
1326 }
1327 return TRUE;
1328 }
1329
1330 // Transfer values from controls. If returns FALSE,
1331 // validation failed: don't quit
1332 bool wxWindow::TransferDataFromWindow()
1333 {
1334 wxNode *node = GetChildren()->First();
1335 while ( node )
1336 {
1337 wxWindow *child = (wxWindow *)node->Data();
1338 if ( child->GetValidator() && !child->GetValidator()->TransferFromWindow() )
1339 {
1340 return FALSE;
1341 }
1342
1343 node = node->Next();
1344 }
1345 return TRUE;
1346 }
1347
1348 bool wxWindow::Validate()
1349 {
1350 wxNode *node = GetChildren()->First();
1351 while ( node )
1352 {
1353 wxWindow *child = (wxWindow *)node->Data();
1354 if ( child->GetValidator() && /* child->GetValidator()->Ok() && */ !child->GetValidator()->Validate(this) )
1355 {
1356 return FALSE;
1357 }
1358
1359 node = node->Next();
1360 }
1361 return TRUE;
1362 }
1363
1364 // Get the window with the focus
1365 wxWindow *wxWindow::FindFocus()
1366 {
1367 // TODO
1368 return NULL;
1369 }
1370
1371 void wxWindow::AddChild(wxWindow *child)
1372 {
1373 GetChildren()->Append(child);
1374 child->m_windowParent = this;
1375 }
1376
1377 void wxWindow::RemoveChild(wxWindow *child)
1378 {
1379 if (GetChildren())
1380 GetChildren()->DeleteObject(child);
1381 child->m_windowParent = NULL;
1382 }
1383
1384 void wxWindow::DestroyChildren()
1385 {
1386 if (GetChildren()) {
1387 wxNode *node;
1388 while ((node = GetChildren()->First()) != (wxNode *)NULL) {
1389 wxWindow *child;
1390 if ((child = (wxWindow *)node->Data()) != (wxWindow *)NULL) {
1391 delete child;
1392 if ( GetChildren()->Member(child) )
1393 delete node;
1394 }
1395 } /* while */
1396 }
1397 }
1398
1399 void wxWindow::MakeModal(bool modal)
1400 {
1401 // Disable all other windows
1402 if (this->IsKindOf(CLASSINFO(wxDialog)) || this->IsKindOf(CLASSINFO(wxFrame)))
1403 {
1404 wxNode *node = wxTopLevelWindows.First();
1405 while (node)
1406 {
1407 wxWindow *win = (wxWindow *)node->Data();
1408 if (win != this)
1409 win->Enable(!modal);
1410
1411 node = node->Next();
1412 }
1413 }
1414 }
1415
1416 // If nothing defined for this, try the parent.
1417 // E.g. we may be a button loaded from a resource, with no callback function
1418 // defined.
1419 void wxWindow::OnCommand(wxWindow& win, wxCommandEvent& event)
1420 {
1421 if (GetEventHandler()->ProcessEvent(event) )
1422 return;
1423 if (m_windowParent)
1424 m_windowParent->GetEventHandler()->OnCommand(win, event);
1425 }
1426
1427 void wxWindow::SetConstraints(wxLayoutConstraints *c)
1428 {
1429 if (m_constraints)
1430 {
1431 UnsetConstraints(m_constraints);
1432 delete m_constraints;
1433 }
1434 m_constraints = c;
1435 if (m_constraints)
1436 {
1437 // Make sure other windows know they're part of a 'meaningful relationship'
1438 if (m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this))
1439 m_constraints->left.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1440 if (m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this))
1441 m_constraints->top.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1442 if (m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this))
1443 m_constraints->right.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1444 if (m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this))
1445 m_constraints->bottom.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1446 if (m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this))
1447 m_constraints->width.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1448 if (m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this))
1449 m_constraints->height.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1450 if (m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this))
1451 m_constraints->centreX.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1452 if (m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this))
1453 m_constraints->centreY.GetOtherWindow()->AddConstraintReference((wxWindow *)this);
1454 }
1455 }
1456
1457 // This removes any dangling pointers to this window
1458 // in other windows' constraintsInvolvedIn lists.
1459 void wxWindow::UnsetConstraints(wxLayoutConstraints *c)
1460 {
1461 if (c)
1462 {
1463 if (c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this))
1464 c->left.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1465 if (c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this))
1466 c->top.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1467 if (c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this))
1468 c->right.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1469 if (c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this))
1470 c->bottom.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1471 if (c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this))
1472 c->width.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1473 if (c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this))
1474 c->height.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1475 if (c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this))
1476 c->centreX.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1477 if (c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this))
1478 c->centreY.GetOtherWindow()->RemoveConstraintReference((wxWindow *)this);
1479 }
1480 }
1481
1482 // Back-pointer to other windows we're involved with, so if we delete
1483 // this window, we must delete any constraints we're involved with.
1484 void wxWindow::AddConstraintReference(wxWindow *otherWin)
1485 {
1486 if (!m_constraintsInvolvedIn)
1487 m_constraintsInvolvedIn = new wxList;
1488 if (!m_constraintsInvolvedIn->Member(otherWin))
1489 m_constraintsInvolvedIn->Append(otherWin);
1490 }
1491
1492 // REMOVE back-pointer to other windows we're involved with.
1493 void wxWindow::RemoveConstraintReference(wxWindow *otherWin)
1494 {
1495 if (m_constraintsInvolvedIn)
1496 m_constraintsInvolvedIn->DeleteObject(otherWin);
1497 }
1498
1499 // Reset any constraints that mention this window
1500 void wxWindow::DeleteRelatedConstraints()
1501 {
1502 if (m_constraintsInvolvedIn)
1503 {
1504 wxNode *node = m_constraintsInvolvedIn->First();
1505 while (node)
1506 {
1507 wxWindow *win = (wxWindow *)node->Data();
1508 wxNode *next = node->Next();
1509 wxLayoutConstraints *constr = win->GetConstraints();
1510
1511 // Reset any constraints involving this window
1512 if (constr)
1513 {
1514 constr->left.ResetIfWin((wxWindow *)this);
1515 constr->top.ResetIfWin((wxWindow *)this);
1516 constr->right.ResetIfWin((wxWindow *)this);
1517 constr->bottom.ResetIfWin((wxWindow *)this);
1518 constr->width.ResetIfWin((wxWindow *)this);
1519 constr->height.ResetIfWin((wxWindow *)this);
1520 constr->centreX.ResetIfWin((wxWindow *)this);
1521 constr->centreY.ResetIfWin((wxWindow *)this);
1522 }
1523 delete node;
1524 node = next;
1525 }
1526 delete m_constraintsInvolvedIn;
1527 m_constraintsInvolvedIn = NULL;
1528 }
1529 }
1530
1531 void wxWindow::SetSizer(wxSizer *sizer)
1532 {
1533 m_windowSizer = sizer;
1534 if (sizer)
1535 sizer->SetSizerParent((wxWindow *)this);
1536 }
1537
1538 /*
1539 * New version
1540 */
1541
1542 bool wxWindow::Layout()
1543 {
1544 if (GetConstraints())
1545 {
1546 int w, h;
1547 GetClientSize(&w, &h);
1548 GetConstraints()->width.SetValue(w);
1549 GetConstraints()->height.SetValue(h);
1550 }
1551
1552 // If top level (one sizer), evaluate the sizer's constraints.
1553 if (GetSizer())
1554 {
1555 int noChanges;
1556 GetSizer()->ResetConstraints(); // Mark all constraints as unevaluated
1557 GetSizer()->LayoutPhase1(&noChanges);
1558 GetSizer()->LayoutPhase2(&noChanges);
1559 GetSizer()->SetConstraintSizes(); // Recursively set the real window sizes
1560 return TRUE;
1561 }
1562 else
1563 {
1564 // Otherwise, evaluate child constraints
1565 ResetConstraints(); // Mark all constraints as unevaluated
1566 DoPhase(1); // Just one phase need if no sizers involved
1567 DoPhase(2);
1568 SetConstraintSizes(); // Recursively set the real window sizes
1569 }
1570 return TRUE;
1571 }
1572
1573
1574 // Do a phase of evaluating constraints:
1575 // the default behaviour. wxSizers may do a similar
1576 // thing, but also impose their own 'constraints'
1577 // and order the evaluation differently.
1578 bool wxWindow::LayoutPhase1(int *noChanges)
1579 {
1580 wxLayoutConstraints *constr = GetConstraints();
1581 if (constr)
1582 {
1583 return constr->SatisfyConstraints((wxWindow *)this, noChanges);
1584 }
1585 else
1586 return TRUE;
1587 }
1588
1589 bool wxWindow::LayoutPhase2(int *noChanges)
1590 {
1591 *noChanges = 0;
1592
1593 // Layout children
1594 DoPhase(1);
1595 DoPhase(2);
1596 return TRUE;
1597 }
1598
1599 // Do a phase of evaluating child constraints
1600 bool wxWindow::DoPhase(int phase)
1601 {
1602 int noIterations = 0;
1603 int maxIterations = 500;
1604 int noChanges = 1;
1605 int noFailures = 0;
1606 wxList succeeded;
1607 while ((noChanges > 0) && (noIterations < maxIterations))
1608 {
1609 noChanges = 0;
1610 noFailures = 0;
1611 wxNode *node = GetChildren()->First();
1612 while (node)
1613 {
1614 wxWindow *child = (wxWindow *)node->Data();
1615 if (!child->IsKindOf(CLASSINFO(wxFrame)) && !child->IsKindOf(CLASSINFO(wxDialog)))
1616 {
1617 wxLayoutConstraints *constr = child->GetConstraints();
1618 if (constr)
1619 {
1620 if (succeeded.Member(child))
1621 {
1622 }
1623 else
1624 {
1625 int tempNoChanges = 0;
1626 bool success = ( (phase == 1) ? child->LayoutPhase1(&tempNoChanges) : child->LayoutPhase2(&tempNoChanges) ) ;
1627 noChanges += tempNoChanges;
1628 if (success)
1629 {
1630 succeeded.Append(child);
1631 }
1632 }
1633 }
1634 }
1635 node = node->Next();
1636 }
1637 noIterations ++;
1638 }
1639 return TRUE;
1640 }
1641
1642 void wxWindow::ResetConstraints()
1643 {
1644 wxLayoutConstraints *constr = GetConstraints();
1645 if (constr)
1646 {
1647 constr->left.SetDone(FALSE);
1648 constr->top.SetDone(FALSE);
1649 constr->right.SetDone(FALSE);
1650 constr->bottom.SetDone(FALSE);
1651 constr->width.SetDone(FALSE);
1652 constr->height.SetDone(FALSE);
1653 constr->centreX.SetDone(FALSE);
1654 constr->centreY.SetDone(FALSE);
1655 }
1656 wxNode *node = GetChildren()->First();
1657 while (node)
1658 {
1659 wxWindow *win = (wxWindow *)node->Data();
1660 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
1661 win->ResetConstraints();
1662 node = node->Next();
1663 }
1664 }
1665
1666 // Need to distinguish between setting the 'fake' size for
1667 // windows and sizers, and setting the real values.
1668 void wxWindow::SetConstraintSizes(bool recurse)
1669 {
1670 wxLayoutConstraints *constr = GetConstraints();
1671 if (constr && constr->left.GetDone() && constr->right.GetDone() &&
1672 constr->width.GetDone() && constr->height.GetDone())
1673 {
1674 int x = constr->left.GetValue();
1675 int y = constr->top.GetValue();
1676 int w = constr->width.GetValue();
1677 int h = constr->height.GetValue();
1678
1679 // If we don't want to resize this window, just move it...
1680 if ((constr->width.GetRelationship() != wxAsIs) ||
1681 (constr->height.GetRelationship() != wxAsIs))
1682 {
1683 // Calls Layout() recursively. AAAGH. How can we stop that.
1684 // Simply take Layout() out of non-top level OnSizes.
1685 SizerSetSize(x, y, w, h);
1686 }
1687 else
1688 {
1689 SizerMove(x, y);
1690 }
1691 }
1692 else if (constr)
1693 {
1694 char *windowClass = this->GetClassInfo()->GetClassName();
1695
1696 wxString winName;
1697 if (GetName() == "")
1698 winName = "unnamed";
1699 else
1700 winName = GetName();
1701 wxDebugMsg("Constraint(s) not satisfied for window of type %s, name %s:\n", (const char *)windowClass, (const char *)winName);
1702 if (!constr->left.GetDone())
1703 wxDebugMsg(" unsatisfied 'left' constraint.\n");
1704 if (!constr->right.GetDone())
1705 wxDebugMsg(" unsatisfied 'right' constraint.\n");
1706 if (!constr->width.GetDone())
1707 wxDebugMsg(" unsatisfied 'width' constraint.\n");
1708 if (!constr->height.GetDone())
1709 wxDebugMsg(" unsatisfied 'height' constraint.\n");
1710 wxDebugMsg("Please check constraints: try adding AsIs() constraints.\n");
1711 }
1712
1713 if (recurse)
1714 {
1715 wxNode *node = GetChildren()->First();
1716 while (node)
1717 {
1718 wxWindow *win = (wxWindow *)node->Data();
1719 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)))
1720 win->SetConstraintSizes();
1721 node = node->Next();
1722 }
1723 }
1724 }
1725
1726 // This assumes that all sizers are 'on' the same
1727 // window, i.e. the parent of this window.
1728 void wxWindow::TransformSizerToActual(int *x, int *y) const
1729 {
1730 if (!m_sizerParent || m_sizerParent->IsKindOf(CLASSINFO(wxDialog)) ||
1731 m_sizerParent->IsKindOf(CLASSINFO(wxFrame)) )
1732 return;
1733
1734 int xp, yp;
1735 m_sizerParent->GetPosition(&xp, &yp);
1736 m_sizerParent->TransformSizerToActual(&xp, &yp);
1737 *x += xp;
1738 *y += yp;
1739 }
1740
1741 void wxWindow::SizerSetSize(int x, int y, int w, int h)
1742 {
1743 int xx = x;
1744 int yy = y;
1745 TransformSizerToActual(&xx, &yy);
1746 SetSize(xx, yy, w, h);
1747 }
1748
1749 void wxWindow::SizerMove(int x, int y)
1750 {
1751 int xx = x;
1752 int yy = y;
1753 TransformSizerToActual(&xx, &yy);
1754 Move(xx, yy);
1755 }
1756
1757 // Only set the size/position of the constraint (if any)
1758 void wxWindow::SetSizeConstraint(int x, int y, int w, int h)
1759 {
1760 wxLayoutConstraints *constr = GetConstraints();
1761 if (constr)
1762 {
1763 if (x != -1)
1764 {
1765 constr->left.SetValue(x);
1766 constr->left.SetDone(TRUE);
1767 }
1768 if (y != -1)
1769 {
1770 constr->top.SetValue(y);
1771 constr->top.SetDone(TRUE);
1772 }
1773 if (w != -1)
1774 {
1775 constr->width.SetValue(w);
1776 constr->width.SetDone(TRUE);
1777 }
1778 if (h != -1)
1779 {
1780 constr->height.SetValue(h);
1781 constr->height.SetDone(TRUE);
1782 }
1783 }
1784 }
1785
1786 void wxWindow::MoveConstraint(int x, int y)
1787 {
1788 wxLayoutConstraints *constr = GetConstraints();
1789 if (constr)
1790 {
1791 if (x != -1)
1792 {
1793 constr->left.SetValue(x);
1794 constr->left.SetDone(TRUE);
1795 }
1796 if (y != -1)
1797 {
1798 constr->top.SetValue(y);
1799 constr->top.SetDone(TRUE);
1800 }
1801 }
1802 }
1803
1804 void wxWindow::GetSizeConstraint(int *w, int *h) const
1805 {
1806 wxLayoutConstraints *constr = GetConstraints();
1807 if (constr)
1808 {
1809 *w = constr->width.GetValue();
1810 *h = constr->height.GetValue();
1811 }
1812 else
1813 GetSize(w, h);
1814 }
1815
1816 void wxWindow::GetClientSizeConstraint(int *w, int *h) const
1817 {
1818 wxLayoutConstraints *constr = GetConstraints();
1819 if (constr)
1820 {
1821 *w = constr->width.GetValue();
1822 *h = constr->height.GetValue();
1823 }
1824 else
1825 GetClientSize(w, h);
1826 }
1827
1828 void wxWindow::GetPositionConstraint(int *x, int *y) const
1829 {
1830 wxLayoutConstraints *constr = GetConstraints();
1831 if (constr)
1832 {
1833 *x = constr->left.GetValue();
1834 *y = constr->top.GetValue();
1835 }
1836 else
1837 GetPosition(x, y);
1838 }
1839
1840 bool wxWindow::Close(bool force)
1841 {
1842 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
1843 event.SetEventObject(this);
1844 event.SetForce(force);
1845
1846 return GetEventHandler()->ProcessEvent(event);
1847 }
1848
1849 wxObject* wxWindow::GetChild(int number) const
1850 {
1851 // Return a pointer to the Nth object in the window
1852 if (!GetChildren())
1853 return(NULL) ;
1854 wxNode *node = GetChildren()->First();
1855 int n = number;
1856 while (node && n--)
1857 node = node->Next() ;
1858 if (node)
1859 {
1860 wxObject *obj = (wxObject *)node->Data();
1861 return(obj) ;
1862 }
1863 else
1864 return NULL ;
1865 }
1866
1867 void wxWindow::OnDefaultAction(wxControl *initiatingItem)
1868 {
1869 // Obsolete function
1870 }
1871
1872 void wxWindow::Clear()
1873 {
1874 wxClientDC dc(this);
1875 wxBrush brush(GetBackgroundColour(), wxSOLID);
1876 dc.SetBackground(brush);
1877 dc.Clear();
1878 }
1879
1880 // Fits the panel around the items
1881 void wxWindow::Fit()
1882 {
1883 int maxX = 0;
1884 int maxY = 0;
1885 wxNode *node = GetChildren()->First();
1886 while ( node )
1887 {
1888 wxWindow *win = (wxWindow *)node->Data();
1889 int wx, wy, ww, wh;
1890 win->GetPosition(&wx, &wy);
1891 win->GetSize(&ww, &wh);
1892 if ( wx + ww > maxX )
1893 maxX = wx + ww;
1894 if ( wy + wh > maxY )
1895 maxY = wy + wh;
1896
1897 node = node->Next();
1898 }
1899 SetClientSize(maxX + 5, maxY + 5);
1900 }
1901
1902 void wxWindow::SetValidator(const wxValidator& validator)
1903 {
1904 if ( m_windowValidator )
1905 delete m_windowValidator;
1906 m_windowValidator = validator.Clone();
1907
1908 if ( m_windowValidator )
1909 m_windowValidator->SetWindow(this) ;
1910 }
1911
1912 // Find a window by id or name
1913 wxWindow *wxWindow::FindWindow(long id)
1914 {
1915 if ( GetId() == id)
1916 return this;
1917
1918 wxNode *node = GetChildren()->First();
1919 while ( node )
1920 {
1921 wxWindow *child = (wxWindow *)node->Data();
1922 wxWindow *found = child->FindWindow(id);
1923 if ( found )
1924 return found;
1925 node = node->Next();
1926 }
1927 return NULL;
1928 }
1929
1930 wxWindow *wxWindow::FindWindow(const wxString& name)
1931 {
1932 if ( GetName() == name)
1933 return this;
1934
1935 wxNode *node = GetChildren()->First();
1936 while ( node )
1937 {
1938 wxWindow *child = (wxWindow *)node->Data();
1939 wxWindow *found = child->FindWindow(name);
1940 if ( found )
1941 return found;
1942 node = node->Next();
1943 }
1944 return NULL;
1945 }
1946
1947 void wxWindow::OnIdle(wxIdleEvent& event)
1948 {
1949 /* TODO: you may need to do something like this
1950 * if your GUI doesn't generate enter/leave events
1951
1952 // Check if we need to send a LEAVE event
1953 if (m_mouseInWindow)
1954 {
1955 POINT pt;
1956 ::GetCursorPos(&pt);
1957 if (::WindowFromPoint(pt) != (HWND) GetHWND())
1958 {
1959 // Generate a LEAVE event
1960 m_mouseInWindow = FALSE;
1961 MSWOnMouseLeave(pt.x, pt.y, 0);
1962 }
1963 }
1964 */
1965
1966 // This calls the UI-update mechanism (querying windows for
1967 // menu/toolbar/control state information)
1968 UpdateWindowUI();
1969 }
1970
1971 // Raise the window to the top of the Z order
1972 void wxWindow::Raise()
1973 {
1974 Window window = XtWindow((Widget) GetTopWidget());
1975 XRaiseWindow(XtDisplay((Widget) GetTopWidget()), window);
1976 }
1977
1978 // Lower the window to the bottom of the Z order
1979 void wxWindow::Lower()
1980 {
1981 Window window = XtWindow((Widget) GetTopWidget());
1982 XLowerWindow(XtDisplay((Widget) GetTopWidget()), window);
1983 }
1984
1985 bool wxWindow::AcceptsFocus() const
1986 {
1987 return IsShown() && IsEnabled();
1988 }
1989
1990 // Update region access
1991 wxRegion wxWindow::GetUpdateRegion() const
1992 {
1993 return m_updateRegion;
1994 }
1995
1996 bool wxWindow::IsExposed(int x, int y, int w, int h) const
1997 {
1998 return (m_updateRegion.Contains(x, y, w, h) != wxOutRegion);
1999 }
2000
2001 bool wxWindow::IsExposed(const wxPoint& pt) const
2002 {
2003 return (m_updateRegion.Contains(pt) != wxOutRegion);
2004 }
2005
2006 bool wxWindow::IsExposed(const wxRect& rect) const
2007 {
2008 return (m_updateRegion.Contains(rect) != wxOutRegion);
2009 }
2010
2011 /*
2012 * Allocates control IDs
2013 */
2014
2015 int wxWindow::NewControlId()
2016 {
2017 static int s_controlId = 0;
2018 s_controlId ++;
2019 return s_controlId;
2020 }
2021
2022 void wxWindow::SetAcceleratorTable(const wxAcceleratorTable& accel)
2023 {
2024 m_acceleratorTable = accel;
2025 }
2026
2027 // All widgets should have this as their resize proc.
2028 // OnSize sent to wxWindow via client data.
2029 void wxWidgetResizeProc(Widget w, XConfigureEvent *event, String args[], int *num_args)
2030 {
2031 wxWindow *win = (wxWindow *)wxWidgetHashTable->Get((long)w);
2032 if (!win)
2033 return;
2034
2035 if (win->PreResize())
2036 {
2037 int width, height;
2038 win->GetSize(&width, &height);
2039 wxSizeEvent sizeEvent(wxSize(width, height), win->GetId());
2040 sizeEvent.SetEventObject(win);
2041 win->GetEventHandler()->ProcessEvent(sizeEvent);
2042 }
2043 }
2044
2045 bool wxAddWindowToTable(Widget w, wxWindow *win)
2046 {
2047 wxWindow *oldItem = NULL;
2048 #if DEBUG
2049 // printf("Adding widget %ld, name = %s\n", w, win->GetClassInfo()->GetClassName());
2050 #endif
2051 if ((oldItem = (wxWindow *)wxWidgetHashTable->Get ((long) w)))
2052 {
2053 char buf[300];
2054 sprintf(buf, "Widget table clash: new widget is %ld, %s", (long)w, win->GetClassInfo()->GetClassName());
2055 wxError (buf);
2056 fflush(stderr);
2057 sprintf(buf, "Old widget was %s", oldItem->GetClassInfo()->GetClassName());
2058 wxError (buf);
2059 return FALSE;
2060 }
2061
2062 wxWidgetHashTable->Put ((long) w, win);
2063 return TRUE;
2064 }
2065
2066 wxWindow *wxGetWindowFromTable(Widget w)
2067 {
2068 return (wxWindow *)wxWidgetHashTable->Get ((long) w);
2069 }
2070
2071 void wxDeleteWindowFromTable(Widget w)
2072 {
2073 wxWidgetHashTable->Delete((long)w);
2074 }
2075
2076 // Get the underlying X window and display
2077 WXWindow wxWindow::GetXWindow() const
2078 {
2079 return (WXWindow) XtWindow((Widget) GetMainWidget());
2080 }
2081
2082 WXDisplay *wxWindow::GetXDisplay() const
2083 {
2084 return (WXDisplay*) XtDisplay((Widget) GetMainWidget());
2085 }
2086
2087 WXWidget wxWindow::GetMainWidget() const
2088 {
2089 if (m_drawingArea)
2090 return m_drawingArea;
2091 else
2092 return m_mainWidget;
2093 }
2094
2095 WXWidget wxWindow::GetClientWidget() const
2096 {
2097 if (m_drawingArea != (WXWidget) 0)
2098 return m_drawingArea;
2099 else
2100 return GetMainWidget();
2101 }
2102
2103 WXWidget wxWindow::GetTopWidget() const
2104 {
2105 return GetMainWidget();
2106 }
2107
2108 void wxCanvasRepaintProc (Widget drawingArea, XtPointer clientData,
2109 XmDrawingAreaCallbackStruct * cbs)
2110 {
2111 if (!wxWidgetHashTable->Get ((long) (Widget) drawingArea))
2112 return;
2113
2114 XEvent * event = cbs->event;
2115 wxWindow * canvas = (wxWindow *) clientData;
2116 Display * display = (Display *) canvas->GetXDisplay();
2117 // GC gc = (GC) canvas->GetDC()->gc;
2118
2119 switch (event->type)
2120 {
2121 case Expose:
2122 {
2123 /* TODO
2124 wxCanvasDC* canvasDC = canvas->GetDC();
2125 if (canvasDC)
2126 {
2127 if (canvasDC->onpaint_reg)
2128 XDestroyRegion(canvasDC->onpaint_reg);
2129 canvasDC->onpaint_reg = XCreateRegion();
2130
2131 }
2132 */
2133
2134 int n = canvas->m_updateRects.Number();
2135 XRectangle* xrects = new XRectangle[n];
2136 int i;
2137 for (i = 0; i < canvas->m_updateRects.Number(); i++)
2138 {
2139 wxRect* rect = (wxRect*) canvas->m_updateRects.Nth(i)->Data();
2140 xrects[i].x = rect->x;
2141 xrects[i].y = rect->y;
2142 xrects[i].width = rect->width;
2143 xrects[i].height = rect->height;
2144 /* TODO (?) Actually ignore it I think.
2145 if (canvasDC)
2146 XUnionRectWithRegion(&(xrects[i]), canvasDC->onpaint_reg,
2147 canvasDC->onpaint_reg);
2148 */
2149 }
2150 /* TODO must clip the area being repainted. So we need a gc.
2151 * Alternatively, wxPaintDC must do the clipping
2152 * when it's created.
2153 XSetClipRectangles(display, gc, 0, 0, xrects, n, Unsorted);
2154 */
2155
2156 canvas->DoPaint() ; // xrects, n);
2157 delete[] xrects;
2158
2159 canvas->m_updateRects.Clear();
2160
2161 /*
2162 if (canvasDC)
2163 {
2164 XDestroyRegion(canvasDC->onpaint_reg);
2165 canvasDC->onpaint_reg = NULL;
2166 }
2167
2168 XGCValues gc_val;
2169 gc_val.clip_mask = None;
2170 XChangeGC(display, gc, GCClipMask, &gc_val);
2171 */
2172
2173 break;
2174 }
2175 default:
2176 {
2177 cout << "\n\nNew Event ! is = " << event -> type << "\n";
2178 break;
2179 }
2180 }
2181 }
2182
2183 // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4)
2184 void
2185 wxCanvasEnterLeave (Widget drawingArea, XtPointer clientData, XCrossingEvent * event)
2186 {
2187 XmDrawingAreaCallbackStruct cbs;
2188 XEvent ev;
2189
2190 //if (event->mode!=NotifyNormal)
2191 // return ;
2192
2193 // ev = *((XEvent *) event); // Causes Purify error (copying too many bytes)
2194 ((XCrossingEvent &) ev) = *event;
2195
2196 cbs.reason = XmCR_INPUT;
2197 cbs.event = &ev;
2198
2199 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
2200 }
2201
2202 // Fix to make it work under Motif 1.0 (!)
2203 void wxCanvasMotionEvent (Widget drawingArea, XButtonEvent * event)
2204 {
2205 #if XmVersion<=1000
2206
2207 XmDrawingAreaCallbackStruct cbs;
2208 XEvent ev;
2209
2210 //ev.xbutton = *event;
2211 ev = *((XEvent *) event);
2212 cbs.reason = XmCR_INPUT;
2213 cbs.event = &ev;
2214
2215 wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs);
2216 #endif
2217 }
2218
2219 void wxCanvasInputEvent (Widget drawingArea, XtPointer data, XmDrawingAreaCallbackStruct * cbs)
2220 {
2221 wxWindow *canvas = (wxWindow *) wxWidgetHashTable->Get ((long) (Widget) drawingArea);
2222 XEvent local_event;
2223
2224 if (canvas==NULL)
2225 return ;
2226
2227 if (cbs->reason != XmCR_INPUT)
2228 return;
2229
2230 local_event = *(cbs->event); // We must keep a copy!
2231
2232 switch (local_event.xany.type)
2233 {
2234 case EnterNotify:
2235 case LeaveNotify:
2236 case ButtonPress:
2237 case ButtonRelease:
2238 case MotionNotify:
2239 {
2240 wxEventType eventType = wxEVT_NULL;
2241
2242 if (local_event.xany.type == EnterNotify)
2243 {
2244 //if (local_event.xcrossing.mode!=NotifyNormal)
2245 // return ; // Ignore grab events
2246 eventType = wxEVT_ENTER_WINDOW;
2247 // canvas->GetEventHandler()->OnSetFocus();
2248 }
2249 else if (local_event.xany.type == LeaveNotify)
2250 {
2251 //if (local_event.xcrossing.mode!=NotifyNormal)
2252 // return ; // Ignore grab events
2253 eventType = wxEVT_LEAVE_WINDOW;
2254 // canvas->GetEventHandler()->OnKillFocus();
2255 }
2256 else if (local_event.xany.type == MotionNotify)
2257 {
2258 eventType = wxEVT_MOTION;
2259 if (local_event.xmotion.is_hint == NotifyHint)
2260 {
2261 Window root, child;
2262 Display *dpy = XtDisplay (drawingArea);
2263
2264 XQueryPointer (dpy, XtWindow (drawingArea),
2265 &root, &child,
2266 &local_event.xmotion.x_root,
2267 &local_event.xmotion.y_root,
2268 &local_event.xmotion.x,
2269 &local_event.xmotion.y,
2270 &local_event.xmotion.state);
2271 }
2272 else
2273 {
2274 }
2275 }
2276
2277 else if (local_event.xany.type == ButtonPress)
2278 {
2279 if (local_event.xbutton.button == Button1)
2280 {
2281 eventType = wxEVT_LEFT_DOWN;
2282 canvas->m_button1Pressed = TRUE;
2283 }
2284 else if (local_event.xbutton.button == Button2)
2285 {
2286 eventType = wxEVT_MIDDLE_DOWN;
2287 canvas->m_button2Pressed = TRUE;
2288 }
2289 else if (local_event.xbutton.button == Button3)
2290 {
2291 eventType = wxEVT_RIGHT_DOWN;
2292 canvas->m_button3Pressed = TRUE;
2293 }
2294 }
2295 else if (local_event.xany.type == ButtonRelease)
2296 {
2297 if (local_event.xbutton.button == Button1)
2298 {
2299 eventType = wxEVT_LEFT_UP;
2300 canvas->m_button1Pressed = FALSE;
2301 }
2302 else if (local_event.xbutton.button == Button2)
2303 {
2304 eventType = wxEVT_MIDDLE_UP;
2305 canvas->m_button2Pressed = FALSE;
2306 }
2307 else if (local_event.xbutton.button == Button3)
2308 {
2309 eventType = wxEVT_RIGHT_UP;
2310 canvas->m_button3Pressed = FALSE;
2311 }
2312 }
2313
2314 wxMouseEvent wxevent (eventType);
2315 wxevent.m_eventHandle = (char *) &local_event;
2316
2317 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2318 || (event_left_is_down (&local_event)
2319 && (eventType != wxEVT_LEFT_UP)));
2320 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2321 || (event_middle_is_down (&local_event)
2322 && (eventType != wxEVT_MIDDLE_UP)));
2323 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2324 || (event_right_is_down (&local_event)
2325 && (eventType != wxEVT_RIGHT_UP)));
2326
2327 wxevent.m_shiftDown = local_event.xbutton.state & ShiftMask;
2328 wxevent.m_controlDown = local_event.xbutton.state & ControlMask;
2329 wxevent.m_altDown = local_event.xbutton.state & Mod3Mask;
2330 wxevent.m_metaDown = local_event.xbutton.state & Mod1Mask;
2331 wxevent.SetTimestamp(local_event.xbutton.time);
2332
2333 // Now check if we need to translate this event into a double click
2334 if (TRUE) // canvas->doubleClickAllowed)
2335 {
2336 if (wxevent.ButtonDown())
2337 {
2338 long dclickTime = XtGetMultiClickTime((Display*) wxGetDisplay()) ;
2339
2340 // get button and time-stamp
2341 int button = 0;
2342 if (wxevent.LeftDown()) button = 1;
2343 else if (wxevent.MiddleDown()) button = 2;
2344 else if (wxevent.RightDown()) button = 3;
2345 long ts = wxevent.GetTimestamp();
2346 // check, if single or double click
2347 if (canvas->m_lastButton && canvas->m_lastButton==button && (ts - canvas->m_lastTS) < dclickTime)
2348 {
2349 // I have a dclick
2350 canvas->m_lastButton = 0;
2351 switch ( eventType )
2352 {
2353 case wxEVT_LEFT_DOWN:
2354 wxevent.SetEventType(wxEVT_LEFT_DCLICK);
2355 break;
2356 case wxEVT_MIDDLE_DOWN:
2357 wxevent.SetEventType(wxEVT_MIDDLE_DCLICK);
2358 break;
2359 case wxEVT_RIGHT_DOWN:
2360 wxevent.SetEventType(wxEVT_RIGHT_DCLICK);
2361 break;
2362
2363 default :
2364 break;
2365 }
2366
2367 }
2368 else
2369 {
2370 // not fast enough or different button
2371 canvas->m_lastTS = ts;
2372 canvas->m_lastButton = button;
2373 }
2374 }
2375 }
2376
2377 wxevent.SetId(canvas->GetId());
2378 wxevent.SetEventObject(canvas);
2379 canvas->GetEventHandler()->ProcessEvent (wxevent);
2380 /*
2381 if (eventType == wxEVT_ENTER_WINDOW ||
2382 eventType == wxEVT_LEAVE_WINDOW ||
2383 eventType == wxEVT_MOTION
2384 )
2385 return;
2386 */
2387 break;
2388 }
2389 case KeyPress:
2390 {
2391 KeySym keySym;
2392 // XComposeStatus compose;
2393 // (void) XLookupString ((XKeyEvent *) & local_event, wxBuffer, 20, &keySym, &compose);
2394 (void) XLookupString ((XKeyEvent *) & local_event, wxBuffer, 20, &keySym, NULL);
2395 int id = wxCharCodeXToWX (keySym);
2396
2397 wxKeyEvent event (wxEVT_CHAR);
2398
2399 if (local_event.xkey.state & ShiftMask)
2400 event.m_shiftDown = TRUE;
2401 if (local_event.xkey.state & ControlMask)
2402 event.m_controlDown = TRUE;
2403 if (local_event.xkey.state & Mod3Mask)
2404 event.m_altDown = TRUE;
2405 if (local_event.xkey.state & Mod1Mask)
2406 event.m_metaDown = TRUE;
2407 event.SetEventObject(canvas);
2408 event.m_keyCode = id;
2409 event.SetTimestamp(local_event.xkey.time);
2410
2411 if (id > -1)
2412 {
2413 // Implement wxFrame::OnCharHook by checking ancestor.
2414 wxWindow *parent = canvas->GetParent();
2415 while (parent && !parent->IsKindOf(CLASSINFO(wxFrame)))
2416 parent = parent->GetParent();
2417
2418 if (parent)
2419 {
2420 event.SetEventType(wxEVT_CHAR_HOOK);
2421 if (parent->GetEventHandler()->ProcessEvent(event))
2422 return;
2423 event.SetEventType(wxEVT_CHAR);
2424 }
2425
2426 canvas->GetEventHandler()->ProcessEvent (event);
2427 }
2428 break;
2429 }
2430 case FocusIn:
2431 {
2432 if (local_event.xfocus.detail != NotifyPointer)
2433 {
2434 wxFocusEvent event(wxEVT_SET_FOCUS, canvas->GetId());
2435 event.SetEventObject(canvas);
2436 canvas->GetEventHandler()->ProcessEvent(event);
2437 }
2438 break;
2439 }
2440 case FocusOut:
2441 {
2442 if (local_event.xfocus.detail != NotifyPointer)
2443 {
2444 wxFocusEvent event(wxEVT_KILL_FOCUS, canvas->GetId());
2445 event.SetEventObject(canvas);
2446 canvas->GetEventHandler()->ProcessEvent(event);
2447 }
2448 break;
2449 }
2450 default:
2451 break;
2452 }
2453 }
2454
2455 void wxWindow::DoPaint()
2456 {
2457 //TODO : make a temporary gc so we can do the XCopyArea below
2458 if (0) // m_backingPixmap)
2459 {
2460 /*
2461 Widget drawingArea = (Widget) m_drawingArea;
2462 // int orig = GetDC()->GetLogicalFunction();
2463 // GetDC()->SetLogicalFunction (wxCOPY);
2464
2465 // TODO: it may not be necessary to store m_pixmapOffsetX/Y; we
2466 // should be able to calculate them.
2467 XCopyArea (XtDisplay (drawingArea), m_backingPixmap, XtWindow (drawingArea), GetDC ()->gc,
2468 m_pixmapOffsetX, m_pixmapOffsetY,
2469 m_pixmapWidth, m_pixmapHeight,
2470 0, 0);
2471
2472 // GetDC()->SetLogicalFunction (orig);
2473 */
2474 }
2475 else
2476 {
2477 wxPaintEvent event(GetId());
2478 event.SetEventObject(this);
2479 GetEventHandler()->ProcessEvent(event);
2480 }
2481 }
2482
2483 // SetSize, but as per old wxCanvas (with drawing widget etc.)
2484 void wxWindow::CanvasSetSize (int x, int y, int w, int h, int sizeFlags)
2485 {
2486 Widget drawingArea = (Widget) m_drawingArea;
2487 bool managed = XtIsManaged(m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2488
2489 if (managed)
2490 XtUnmanageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2491 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2492
2493 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2494 {
2495 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2496 XmNx, x, NULL);
2497 }
2498
2499 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2500 {
2501 XtVaSetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow,
2502 XmNy, y, NULL);
2503 }
2504
2505 if (w > -1)
2506 {
2507 if (m_borderWidget)
2508 {
2509 XtVaSetValues ((Widget) m_borderWidget, XmNwidth, w, NULL);
2510 short thick, margin;
2511 XtVaGetValues ((Widget) m_borderWidget,
2512 XmNshadowThickness, &thick,
2513 XmNmarginWidth, &margin,
2514 NULL);
2515 w -= 2 * (thick + margin);
2516 }
2517
2518 XtVaSetValues ((Widget) m_scrolledWindow, XmNwidth, w, NULL);
2519
2520 Dimension spacing;
2521 Widget sbar;
2522 XtVaGetValues ((Widget) m_scrolledWindow,
2523 XmNspacing, &spacing,
2524 XmNverticalScrollBar, &sbar,
2525 NULL);
2526 Dimension wsbar;
2527 if (sbar)
2528 XtVaGetValues (sbar, XmNwidth, &wsbar, NULL);
2529 else
2530 wsbar = 0;
2531
2532 w -= (spacing + wsbar);
2533
2534 XtVaSetValues ((Widget) m_drawingArea, XmNwidth, w, NULL);
2535 }
2536 if (h > -1)
2537 {
2538 if (m_borderWidget)
2539 {
2540 XtVaSetValues ((Widget) m_borderWidget, XmNheight, h, NULL);
2541 short thick, margin;
2542 XtVaGetValues ((Widget) m_borderWidget,
2543 XmNshadowThickness, &thick,
2544 XmNmarginHeight, &margin,
2545 NULL);
2546 h -= 2 * (thick + margin);
2547 }
2548
2549 XtVaSetValues ((Widget) m_scrolledWindow, XmNheight, h, NULL);
2550
2551 Dimension spacing;
2552 Widget sbar;
2553 XtVaGetValues ((Widget) m_scrolledWindow,
2554 XmNspacing, &spacing,
2555 XmNhorizontalScrollBar, &sbar,
2556 NULL);
2557 Dimension wsbar;
2558 if (sbar)
2559 XtVaGetValues (sbar, XmNheight, &wsbar, NULL);
2560 else
2561 wsbar = 0;
2562
2563 h -= (spacing + wsbar);
2564
2565 XtVaSetValues ((Widget) m_drawingArea, XmNheight, h, NULL);
2566 }
2567 if (managed)
2568 XtManageChild (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow);
2569 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2570
2571 int ww, hh;
2572 GetClientSize (&ww, &hh);
2573 wxSizeEvent sizeEvent(wxSize(ww, hh), GetId());
2574 sizeEvent.SetEventObject(this);
2575
2576 GetEventHandler()->ProcessEvent(sizeEvent);
2577 }
2578
2579 void wxWindow::CanvasSetClientSize (int w, int h)
2580 {
2581 Widget drawingArea = (Widget) m_drawingArea;
2582
2583 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL);
2584
2585 if (w > -1)
2586 XtVaSetValues ((Widget) m_drawingArea, XmNwidth, w, NULL);
2587 if (h > -1)
2588 XtVaSetValues ((Widget) m_drawingArea, XmNheight, h, NULL);
2589 /* TODO: is this necessary?
2590 allowRepainting = FALSE;
2591
2592 XSync (XtDisplay (drawingArea), FALSE);
2593 XEvent event;
2594 while (XtAppPending (wxTheApp->appContext))
2595 {
2596 XFlush (XtDisplay (drawingArea));
2597 XtAppNextEvent (wxTheApp->appContext, &event);
2598 XtDispatchEvent (&event);
2599 }
2600 */
2601
2602 XtVaSetValues((Widget) m_drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL);
2603
2604 /* TODO
2605 allowRepainting = TRUE;
2606 DoRefresh ();
2607 */
2608
2609 wxSizeEvent sizeEvent(wxSize(w, h), GetId());
2610 sizeEvent.SetEventObject(this);
2611
2612 GetEventHandler()->ProcessEvent(sizeEvent);
2613 }
2614
2615 void wxWindow::CanvasGetClientSize (int *w, int *h) const
2616 {
2617 // Must return the same thing that was set via SetClientSize
2618 Dimension xx, yy;
2619 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2620 *w = xx;
2621 *h = yy;
2622 }
2623
2624 void wxWindow::CanvasGetSize (int *w, int *h) const
2625 {
2626 Dimension xx, yy;
2627 if ((Widget) m_borderWidget)
2628 XtVaGetValues ((Widget) m_borderWidget, XmNwidth, &xx, XmNheight, &yy, NULL);
2629 else if ((Widget) m_scrolledWindow)
2630 XtVaGetValues ((Widget) m_scrolledWindow, XmNwidth, &xx, XmNheight, &yy, NULL);
2631 else
2632 XtVaGetValues ((Widget) m_drawingArea, XmNwidth, &xx, XmNheight, &yy, NULL);
2633
2634 *w = xx;
2635 *h = yy;
2636 }
2637
2638 void wxWindow::CanvasGetPosition (int *x, int *y) const
2639 {
2640 Position xx, yy;
2641 XtVaGetValues (m_borderWidget ? (Widget) m_borderWidget : (Widget) m_scrolledWindow, XmNx, &xx, XmNy, &yy, NULL);
2642 *x = xx;
2643 *y = yy;
2644 }
2645
2646 // Add to hash table, add event handler
2647 bool wxWindow::AttachWidget (wxWindow* parent, WXWidget mainWidget,
2648 WXWidget formWidget, int x, int y, int width, int height)
2649 {
2650 wxAddWindowToTable((Widget) mainWidget, this);
2651 if (CanAddEventHandler())
2652 {
2653 XtAddEventHandler((Widget) mainWidget,
2654 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
2655 False,
2656 wxPanelItemEventHandler,
2657 (XtPointer) this);
2658 }
2659
2660 if (!formWidget)
2661 {
2662 XtTranslations ptr;
2663 XtOverrideTranslations ((Widget) mainWidget,
2664 ptr = XtParseTranslationTable ("<Configure>: resize()"));
2665 XtFree ((char *) ptr);
2666 }
2667
2668 // Some widgets have a parent form widget, e.g. wxRadioBox
2669 if (formWidget)
2670 {
2671 if (!wxAddWindowToTable((Widget) formWidget, this))
2672 return FALSE;
2673
2674 XtTranslations ptr;
2675 XtOverrideTranslations ((Widget) formWidget,
2676 ptr = XtParseTranslationTable ("<Configure>: resize()"));
2677 XtFree ((char *) ptr);
2678 }
2679
2680 if (x == -1)
2681 x = 0;
2682 if (y == -1)
2683 y = 0;
2684 SetSize (x, y, width, height);
2685
2686 return TRUE;
2687 }
2688
2689 // Remove event handler, remove from hash table
2690 bool wxWindow::DetachWidget(WXWidget widget)
2691 {
2692 if (CanAddEventHandler())
2693 {
2694 XtRemoveEventHandler((Widget) widget,
2695 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
2696 False,
2697 wxPanelItemEventHandler,
2698 (XtPointer)this);
2699 }
2700
2701 wxDeleteWindowFromTable((Widget) widget);
2702 return TRUE;
2703 }
2704
2705 void wxPanelItemEventHandler (Widget wid,
2706 XtPointer client_data,
2707 XEvent* event,
2708 Boolean *continueToDispatch)
2709 {
2710 // Widget can be a label or the actual widget.
2711
2712 wxWindow *window = (wxWindow *)wxWidgetHashTable->Get((long)wid);
2713 if (window)
2714 {
2715 wxMouseEvent wxevent(0);
2716 if (wxTranslateMouseEvent(wxevent, window, wid, event))
2717 {
2718 window->GetEventHandler()->ProcessEvent(wxevent);
2719 }
2720 }
2721 // TODO: probably the key to allowing default behaviour
2722 // to happen.
2723 // Say we set a m_doDefault flag to FALSE at the start of this
2724 // function. Then in e.g. wxWindow::OnMouseEvent we can
2725 // call Default() which sets this flag to TRUE, indicating
2726 // that default processing can happen. Thus, behaviour can appear
2727 // to be overridden just by adding an event handler and not calling
2728 // wxWindow::OnWhatever.
2729 // ALSO, maybe we can use this instead of the current way of handling
2730 // drawing area events, to simplify things.
2731 *continueToDispatch = True;
2732 }
2733
2734 void wxScrollBarCallback(Widget scrollbar, XtPointer clientData,
2735 XmScaleCallbackStruct *cbs)
2736 {
2737 Widget scrolledWindow = XtParent (scrollbar);
2738 wxWindow *win = (wxWindow *) wxWidgetHashTable->Get ((long) scrolledWindow);
2739 int orientation = (int) clientData;
2740
2741 wxEventType eventType = wxEVT_NULL;
2742 switch (cbs->reason)
2743 {
2744 case XmCR_INCREMENT:
2745 {
2746 eventType = wxEVT_SCROLL_LINEDOWN;
2747 break;
2748 }
2749 case XmCR_DECREMENT:
2750 {
2751 eventType = wxEVT_SCROLL_LINEUP;
2752 break;
2753 }
2754 case XmCR_DRAG:
2755 {
2756 eventType = wxEVT_SCROLL_THUMBTRACK;
2757 break;
2758 }
2759 case XmCR_VALUE_CHANGED:
2760 {
2761 // TODO: Should this be intercepted too, or will it cause
2762 // duplicate events?
2763 eventType = wxEVT_SCROLL_THUMBTRACK;
2764 break;
2765 }
2766 case XmCR_PAGE_INCREMENT:
2767 {
2768 eventType = wxEVT_SCROLL_PAGEDOWN;
2769 break;
2770 }
2771 case XmCR_PAGE_DECREMENT:
2772 {
2773 eventType = wxEVT_SCROLL_PAGEUP;
2774 break;
2775 }
2776 case XmCR_TO_TOP:
2777 {
2778 eventType = wxEVT_SCROLL_TOP;
2779 break;
2780 }
2781 case XmCR_TO_BOTTOM:
2782 {
2783 eventType = wxEVT_SCROLL_BOTTOM;
2784 break;
2785 }
2786 default:
2787 {
2788 // Should never get here
2789 wxFAIL_MSG("Unknown scroll event.");
2790 break;
2791 }
2792 }
2793
2794 wxScrollEvent event(eventType, win->GetId());
2795 event.SetEventObject(win);
2796 event.SetPosition(cbs->value);
2797 event.SetOrientation( (orientation == XmHORIZONTAL) ? wxHORIZONTAL : wxVERTICAL );
2798
2799 win->GetEventHandler()->ProcessEvent(event);
2800 }
2801
2802 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Widget widget, XEvent *xevent)
2803 {
2804 switch (xevent->xany.type)
2805 {
2806 case EnterNotify:
2807 case LeaveNotify:
2808 case ButtonPress:
2809 case ButtonRelease:
2810 case MotionNotify:
2811 {
2812 wxEventType eventType = wxEVT_NULL;
2813
2814 if (xevent->xany.type == LeaveNotify)
2815 {
2816 win->m_button1Pressed = FALSE;
2817 win->m_button2Pressed = FALSE;
2818 win->m_button3Pressed = FALSE;
2819 return FALSE;
2820 }
2821 else if (xevent->xany.type == MotionNotify)
2822 {
2823 eventType = wxEVT_MOTION;
2824 }
2825 else if (xevent->xany.type == ButtonPress)
2826 {
2827 if (xevent->xbutton.button == Button1)
2828 {
2829 eventType = wxEVT_LEFT_DOWN;
2830 win->m_button1Pressed = TRUE;
2831 }
2832 else if (xevent->xbutton.button == Button2)
2833 {
2834 eventType = wxEVT_MIDDLE_DOWN;
2835 win->m_button2Pressed = TRUE;
2836 }
2837 else if (xevent->xbutton.button == Button3)
2838 {
2839 eventType = wxEVT_RIGHT_DOWN;
2840 win->m_button3Pressed = TRUE;
2841 }
2842 }
2843 else if (xevent->xany.type == ButtonRelease)
2844 {
2845 if (xevent->xbutton.button == Button1)
2846 {
2847 eventType = wxEVT_LEFT_UP;
2848 win->m_button1Pressed = FALSE;
2849 }
2850 else if (xevent->xbutton.button == Button2)
2851 {
2852 eventType = wxEVT_MIDDLE_UP;
2853 win->m_button2Pressed = FALSE;
2854 }
2855 else if (xevent->xbutton.button == Button3)
2856 {
2857 eventType = wxEVT_RIGHT_UP;
2858 win->m_button3Pressed = FALSE;
2859 }
2860 else return FALSE;
2861 }
2862 else return FALSE;
2863
2864 wxevent.m_eventHandle = (char *)xevent;
2865 wxevent.SetEventType(eventType);
2866
2867 Position x1, y1;
2868 XtVaGetValues(widget, XmNx, &x1, XmNy, &y1, NULL);
2869
2870 int x2, y2;
2871 win->GetPosition(&x2, &y2);
2872
2873 // The button x/y must be translated to wxWindows
2874 // window space - the widget might be a label or button,
2875 // within a form.
2876 int dx = 0;
2877 int dy = 0;
2878 if (widget != (Widget)win->GetMainWidget())
2879 {
2880 dx = x1;
2881 dy = y1;
2882 }
2883
2884 wxevent.m_x = xevent->xbutton.x + dx;
2885 wxevent.m_y = xevent->xbutton.y + dy;
2886
2887 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
2888 || (event_left_is_down (xevent)
2889 && (eventType != wxEVT_LEFT_UP)));
2890 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
2891 || (event_middle_is_down (xevent)
2892 && (eventType != wxEVT_MIDDLE_UP)));
2893 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
2894 || (event_right_is_down (xevent)
2895 && (eventType != wxEVT_RIGHT_UP)));
2896
2897 wxevent.m_shiftDown = xevent->xbutton.state & ShiftMask;
2898 wxevent.m_controlDown = xevent->xbutton.state & ControlMask;
2899 return TRUE;
2900 }
2901 }
2902 return FALSE;
2903 }
2904
2905 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Widget widget, XEvent *xevent)
2906 {
2907 switch (xevent->xany.type)
2908 {
2909 case KeyPress:
2910 {
2911 char buf[20];
2912
2913 KeySym keySym;
2914 // XComposeStatus compose;
2915 // (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, &compose);
2916 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
2917 int id = wxCharCodeXToWX (keySym);
2918
2919 if (xevent->xkey.state & ShiftMask)
2920 wxevent.m_shiftDown = TRUE;
2921 if (xevent->xkey.state & ControlMask)
2922 wxevent.m_controlDown = TRUE;
2923 if (xevent->xkey.state & Mod3Mask)
2924 wxevent.m_altDown = TRUE;
2925 if (xevent->xkey.state & Mod1Mask)
2926 wxevent.m_metaDown = TRUE;
2927 wxevent.SetEventObject(win);
2928 wxevent.m_keyCode = id;
2929 wxevent.SetTimestamp(xevent->xkey.time);
2930
2931 wxevent.m_x = xevent->xbutton.x;
2932 wxevent.m_y = xevent->xbutton.y;
2933
2934 if (id > -1)
2935 return TRUE;
2936 else
2937 return FALSE;
2938 break;
2939 }
2940 default:
2941 break;
2942 }
2943 return FALSE;
2944 }
2945
2946 // TODO From wxWin 1.68. What does it do exactly?
2947 #define YAllocColor XAllocColor
2948
2949 XColor itemColors[5];
2950 int wxComputeColors (Display *display, wxColour * back, wxColour * fore)
2951 {
2952 int result;
2953 static XmColorProc colorProc;
2954
2955 result = wxNO_COLORS;
2956
2957 if (back)
2958 {
2959 itemColors[0].red = (((long) back->Red ()) << 8);
2960 itemColors[0].green = (((long) back->Green ()) << 8);
2961 itemColors[0].blue = (((long) back->Blue ()) << 8);
2962 itemColors[0].flags = DoRed | DoGreen | DoBlue;
2963 if (colorProc == (XmColorProc) NULL)
2964 {
2965 // Get a ptr to the actual function
2966 colorProc = XmSetColorCalculation ((XmColorProc) NULL);
2967 // And set it back to motif.
2968 XmSetColorCalculation (colorProc);
2969 }
2970 (*colorProc) (&itemColors[wxBACK_INDEX],
2971 &itemColors[wxFORE_INDEX],
2972 &itemColors[wxSELE_INDEX],
2973 &itemColors[wxTOPS_INDEX],
2974 &itemColors[wxBOTS_INDEX]);
2975 result = wxBACK_COLORS;
2976 }
2977 if (fore)
2978 {
2979 itemColors[wxFORE_INDEX].red = (((long) fore->Red ()) << 8);
2980 itemColors[wxFORE_INDEX].green = (((long) fore->Green ()) << 8);
2981 itemColors[wxFORE_INDEX].blue = (((long) fore->Blue ()) << 8);
2982 itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue;
2983 if (result == wxNO_COLORS)
2984 result = wxFORE_COLORS;
2985 }
2986
2987 Display *dpy = display;
2988 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
2989
2990 if (back)
2991 {
2992 /* 5 Colours to allocate */
2993 for (int i = 0; i < 5; i++)
2994 if (!YAllocColor (dpy, cmap, &itemColors[i]))
2995 result = wxNO_COLORS;
2996 }
2997 else if (fore)
2998 {
2999 /* Only 1 colour to allocate */
3000 if (!YAllocColor (dpy, cmap, &itemColors[wxFORE_INDEX]))
3001 result = wxNO_COLORS;
3002 }
3003
3004 return (result);
3005
3006 }
3007
3008 void wxWindow::ChangeColour(WXWidget widget)
3009 {
3010 // TODO
3011 #if 0
3012 int change;
3013
3014 // TODO: how to determine whether we can change this item's colours?
3015 // We used to have wxUSER_COLOURS. Now perhaps we assume we always
3016 // can change it.
3017 // if (!(parent->GetWindowStyleFlag() & wxUSER_COLOURS))
3018 // return;
3019
3020 change = wxComputeColors (XtDisplay((Widget)widget), panel->GetBackgroundColour(),
3021 panel->GetLabelColour());
3022 if (change == wxBACK_COLORS)
3023 XtVaSetValues ((Widget) widget,
3024 XmNbackground, itemColors[wxBACK_INDEX].pixel,
3025 XmNtopShadowColor, itemColors[wxTOPS_INDEX].pixel,
3026 XmNbottomShadowColor, itemColors[wxBOTS_INDEX].pixel,
3027 XmNforeground, itemColors[wxFORE_INDEX].pixel,
3028 NULL);
3029 else if (change == wxFORE_COLORS)
3030 XtVaSetValues (formWidget,
3031 XmNforeground, itemColors[wxFORE_INDEX].pixel,
3032 NULL);
3033
3034 change = wxComputeColors (XtDisplay((Widget)formWidget), GetBackgroundColour(), GetLabelColour());
3035 if (change == wxBACK_COLORS)
3036 XtVaSetValues (labelWidget,
3037 XmNbackground, itemColors[wxBACK_INDEX].pixel,
3038 XmNtopShadowColor, itemColors[wxTOPS_INDEX].pixel,
3039 XmNbottomShadowColor, itemColors[wxBOTS_INDEX].pixel,
3040 XmNarmColor, itemColors[wxSELE_INDEX].pixel,
3041 XmNforeground, itemColors[wxFORE_INDEX].pixel,
3042 NULL);
3043 else if (change == wxFORE_COLORS)
3044 XtVaSetValues (labelWidget,
3045 XmNforeground, itemColors[wxFORE_INDEX].pixel,
3046 NULL);
3047 #endif
3048 }
3049
3050 void wxWindow::ChangeFont(WXWidget widget)
3051 {
3052 if (widget && GetFont() && GetFont()->Ok())
3053 {
3054 XmFontList fontList = (XmFontList) GetFont()->GetFontList(1.0, GetXDisplay());
3055 if (fontList)
3056 XtVaSetValues ((Widget) widget,
3057 XmNfontList, fontList,
3058 NULL);
3059 }
3060 }