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