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