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