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