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