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