]>
Commit | Line | Data |
---|---|---|
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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "window.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __VMS | |
28 | #define XtDisplay XTDISPLAY | |
29 | #define XtWindow XTWINDOW | |
30 | #define XtScreen XTSCREEN | |
31 | #endif | |
32 | ||
33 | #include "wx/setup.h" | |
34 | #include "wx/menu.h" | |
35 | #include "wx/dc.h" | |
36 | #include "wx/dcclient.h" | |
37 | #include "wx/utils.h" | |
38 | #include "wx/app.h" | |
39 | #include "wx/layout.h" | |
40 | #include "wx/button.h" | |
41 | #include "wx/settings.h" | |
42 | #include "wx/frame.h" | |
43 | #include "wx/scrolwin.h" | |
44 | #include "wx/module.h" | |
45 | #include "wx/menuitem.h" | |
46 | #include "wx/log.h" | |
47 | #include "wx/evtloop.h" | |
48 | #include "wx/hash.h" | |
49 | ||
50 | #if wxUSE_DRAG_AND_DROP | |
51 | #include "wx/dnd.h" | |
52 | #endif | |
53 | ||
54 | // DoSetSizeIntr and DoMoveWindowIntr | |
55 | // PROBLEM: | |
56 | // under Motif composite controls (such as wxCalendarCtrl or generic wxSpinCtrl | |
57 | // did not work and/or segfaulted because | |
58 | // 1) wxWindow::Create calls SetSize, | |
59 | // which results in a call to DoSetSize much earlier than in the other ports | |
60 | // 2) if wxWindow::Create is called (wxControl::Create calls it) | |
61 | // then DoSetSize is never called, causing layout problems in composite | |
62 | // controls | |
63 | // | |
64 | // SOLUTION: | |
65 | // 1) don't call SetSize, DoSetSize, DoMoveWindow, DoGetPosition, | |
66 | // DoSetPosition directly or indirectly from wxWindow::Create | |
67 | // 2) call DoMoveWindow from DoSetSize, allowing controls to override it | |
68 | ||
69 | #ifdef __VMS__ | |
70 | #pragma message disable nosimpint | |
71 | #endif | |
72 | #include <Xm/Xm.h> | |
73 | ||
74 | #include <Xm/DrawingA.h> | |
75 | #include <Xm/ScrolledW.h> | |
76 | #include <Xm/ScrollBar.h> | |
77 | #include <Xm/Frame.h> | |
78 | #include <Xm/Label.h> | |
79 | #include <Xm/RowColumn.h> // for XmMenuPosition | |
80 | #ifdef __VMS__ | |
81 | #pragma message enable nosimpint | |
82 | #endif | |
83 | ||
84 | #include "wx/motif/private.h" | |
85 | ||
86 | #include <string.h> | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // constants | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | static const int SCROLL_MARGIN = 4; | |
93 | ||
94 | // ---------------------------------------------------------------------------- | |
95 | // global variables for this module | |
96 | // ---------------------------------------------------------------------------- | |
97 | ||
98 | extern wxHashTable *wxWidgetHashTable; | |
99 | static wxWindow* g_captureWindow = NULL; | |
100 | ||
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // private functions | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | static void wxCanvasRepaintProc(Widget, XtPointer, XmDrawingAreaCallbackStruct * cbs); | |
107 | static void wxCanvasInputEvent(Widget drawingArea, XtPointer data, XmDrawingAreaCallbackStruct * cbs); | |
108 | static void wxCanvasMotionEvent(Widget, XButtonEvent * event); | |
109 | static void wxCanvasEnterLeave(Widget drawingArea, XtPointer clientData, XCrossingEvent * event); | |
110 | static void wxScrollBarCallback(Widget widget, XtPointer clientData, | |
111 | XmScrollBarCallbackStruct *cbs); | |
112 | static void wxPanelItemEventHandler(Widget wid, | |
113 | XtPointer client_data, | |
114 | XEvent* event, | |
115 | Boolean *continueToDispatch); | |
116 | ||
117 | // unused for now | |
118 | #if 0 | |
119 | ||
120 | // Helper function for 16-bit fonts | |
121 | static int str16len(const char *s) | |
122 | { | |
123 | int count = 0; | |
124 | ||
125 | while (s[0] && s[1]) { | |
126 | count++; | |
127 | s += 2; | |
128 | } | |
129 | ||
130 | return count; | |
131 | } | |
132 | ||
133 | #endif // 0 | |
134 | ||
135 | // ---------------------------------------------------------------------------- | |
136 | // macros | |
137 | // ---------------------------------------------------------------------------- | |
138 | ||
139 | #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask) | |
140 | #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask) | |
141 | #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask) | |
142 | ||
143 | // ---------------------------------------------------------------------------- | |
144 | // event tables | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
147 | IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase) | |
148 | ||
149 | BEGIN_EVENT_TABLE(wxWindow, wxWindowBase) | |
150 | EVT_SYS_COLOUR_CHANGED(wxWindow::OnSysColourChanged) | |
151 | END_EVENT_TABLE() | |
152 | ||
153 | // ============================================================================ | |
154 | // implementation | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // helper functions | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | void wxWindow::UnmanageAndDestroy(WXWidget widget) | |
162 | { | |
163 | Widget w = (Widget)widget; | |
164 | if ( w ) | |
165 | { | |
166 | XtUnmanageChild(w); | |
167 | XtDestroyWidget(w); | |
168 | } | |
169 | } | |
170 | ||
171 | bool wxWindow::MapOrUnmap(WXWidget widget, bool domap) | |
172 | { | |
173 | Widget w = (Widget)widget; | |
174 | if ( !w ) | |
175 | return false; | |
176 | ||
177 | // Rationale: a lot of common operations (including but not | |
178 | // limited to moving, resizing and appending items to a listbox) | |
179 | // unmamange the widget, do their work, then manage it again. | |
180 | // This means that, for example adding an item to a listbox will show it, | |
181 | // or that most controls are shown every time they are moved or resized! | |
182 | XtSetMappedWhenManaged( w, domap ); | |
183 | ||
184 | // if the widget is not unmanaged, it still intercepts | |
185 | // mouse events, even if it is not mapped (and hence invisible) | |
186 | if ( domap ) | |
187 | { | |
188 | XtManageChild(w); | |
189 | // XtMapWidget(w); | |
190 | } | |
191 | else | |
192 | { | |
193 | XtUnmanageChild(w); | |
194 | // XtUnmapWidget(w); | |
195 | } | |
196 | ||
197 | return true; | |
198 | } | |
199 | ||
200 | // ---------------------------------------------------------------------------- | |
201 | // constructors | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | void wxWindow::Init() | |
205 | { | |
206 | // Motif-specific | |
207 | m_needsRefresh = true; | |
208 | m_mainWidget = (WXWidget) 0; | |
209 | ||
210 | m_winCaptured = false; | |
211 | ||
212 | m_isShown = true; | |
213 | ||
214 | m_hScrollBar = | |
215 | m_vScrollBar = | |
216 | m_borderWidget = | |
217 | m_scrolledWindow = | |
218 | m_drawingArea = (WXWidget) 0; | |
219 | ||
220 | m_scrollPosX = | |
221 | m_scrollPosY = 0; | |
222 | ||
223 | m_backingPixmap = (WXPixmap) 0; | |
224 | m_pixmapWidth = | |
225 | m_pixmapHeight = 0; | |
226 | ||
227 | m_pixmapOffsetX = | |
228 | m_pixmapOffsetY = 0; | |
229 | ||
230 | m_lastTS = 0; | |
231 | m_lastButton = 0; | |
232 | } | |
233 | ||
234 | // real construction (Init() must have been called before!) | |
235 | bool wxWindow::Create(wxWindow *parent, wxWindowID id, | |
236 | const wxPoint& pos, | |
237 | const wxSize& size, | |
238 | long style, | |
239 | const wxString& name) | |
240 | { | |
241 | wxCHECK_MSG( parent, false, "can't create wxWindow without parent" ); | |
242 | ||
243 | CreateBase(parent, id, pos, size, style, wxDefaultValidator, name); | |
244 | ||
245 | parent->AddChild(this); | |
246 | ||
247 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
248 | m_foregroundColour = *wxBLACK; | |
249 | ||
250 | //// TODO: we should probably optimize by only creating a | |
251 | //// a drawing area if we have one or more scrollbars (wxVSCROLL/wxHSCROLL). | |
252 | //// But for now, let's simplify things by always creating the | |
253 | //// drawing area, since otherwise the translations are different. | |
254 | ||
255 | // New translations for getting mouse motion feedback | |
256 | static const String translations = wxMOTIF_STR( | |
257 | "<Btn1Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\ | |
258 | <Btn2Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\ | |
259 | <Btn3Motion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\ | |
260 | <BtnMotion>: wxCanvasMotionEvent() DrawingAreaInput() ManagerGadgetButtonMotion()\n\ | |
261 | <Btn1Down>: DrawingAreaInput() ManagerGadgetArm()\n\ | |
262 | <Btn2Down>: DrawingAreaInput() ManagerGadgetArm()\n\ | |
263 | <Btn3Down>: DrawingAreaInput() ManagerGadgetArm()\n\ | |
264 | <Btn1Up>: DrawingAreaInput() ManagerGadgetActivate()\n\ | |
265 | <Btn2Up>: DrawingAreaInput() ManagerGadgetActivate()\n\ | |
266 | <Btn3Up>: DrawingAreaInput() ManagerGadgetActivate()\n\ | |
267 | <Motion>: wxCanvasMotionEvent() DrawingAreaInput()\n\ | |
268 | <EnterWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\ | |
269 | <LeaveWindow>: wxCanvasMotionEvent() DrawingAreaInput()\n\ | |
270 | <Key>: DrawingAreaInput()"); | |
271 | ||
272 | XtActionsRec actions[1]; | |
273 | actions[0].string = wxMOTIF_STR("wxCanvasMotionEvent"); | |
274 | actions[0].proc = (XtActionProc) wxCanvasMotionEvent; | |
275 | XtAppAddActions ((XtAppContext) wxTheApp->GetAppContext(), actions, 1); | |
276 | ||
277 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
278 | m_borderWidget = wxCreateBorderWidget( (WXWidget)parentWidget, style ); | |
279 | ||
280 | m_scrolledWindow = (WXWidget)XtVaCreateManagedWidget | |
281 | ( | |
282 | "scrolledWindow", | |
283 | xmScrolledWindowWidgetClass, | |
284 | m_borderWidget ? (Widget) m_borderWidget | |
285 | : parentWidget, | |
286 | XmNresizePolicy, XmRESIZE_NONE, | |
287 | XmNspacing, 0, | |
288 | XmNscrollingPolicy, XmAPPLICATION_DEFINED, | |
289 | //XmNscrollBarDisplayPolicy, XmAS_NEEDED, | |
290 | NULL | |
291 | ); | |
292 | ||
293 | XtTranslations ptr = XtParseTranslationTable(translations); | |
294 | m_drawingArea = (WXWidget)XtVaCreateWidget | |
295 | ( | |
296 | name, | |
297 | xmDrawingAreaWidgetClass, (Widget) m_scrolledWindow, | |
298 | XmNunitType, XmPIXELS, | |
299 | // XmNresizePolicy, XmRESIZE_ANY, | |
300 | XmNresizePolicy, XmRESIZE_NONE, | |
301 | XmNmarginHeight, 0, | |
302 | XmNmarginWidth, 0, | |
303 | XmNtranslations, ptr, | |
304 | NULL | |
305 | ); | |
306 | XtFree((char *) ptr); | |
307 | ||
308 | #if 0 | |
309 | if (GetWindowStyleFlag() & wxOVERRIDE_KEY_TRANSLATIONS) | |
310 | { | |
311 | ptr = XtParseTranslationTable ("<Key>: DrawingAreaInput()"); | |
312 | XtOverrideTranslations ((Widget) m_drawingArea, ptr); | |
313 | XtFree ((char *) ptr); | |
314 | } | |
315 | #endif // 0 | |
316 | ||
317 | wxAddWindowToTable((Widget) m_drawingArea, this); | |
318 | wxAddWindowToTable((Widget) m_scrolledWindow, this); | |
319 | ||
320 | // This order is very important in Motif 1.2.1 | |
321 | XtRealizeWidget ((Widget) m_scrolledWindow); | |
322 | XtRealizeWidget ((Widget) m_drawingArea); | |
323 | XtManageChild ((Widget) m_drawingArea); | |
324 | ||
325 | ptr = XtParseTranslationTable("<Configure>: resize()"); | |
326 | XtOverrideTranslations((Widget) m_drawingArea, ptr); | |
327 | XtFree ((char *) ptr); | |
328 | ||
329 | XtAddCallback ((Widget) m_drawingArea, XmNexposeCallback, (XtCallbackProc) wxCanvasRepaintProc, (XtPointer) this); | |
330 | XtAddCallback ((Widget) m_drawingArea, XmNinputCallback, (XtCallbackProc) wxCanvasInputEvent, (XtPointer) this); | |
331 | ||
332 | XtAddEventHandler( | |
333 | (Widget)m_drawingArea, | |
334 | PointerMotionHintMask | EnterWindowMask | | |
335 | LeaveWindowMask | FocusChangeMask, | |
336 | False, | |
337 | (XtEventHandler) wxCanvasEnterLeave, | |
338 | (XtPointer) this | |
339 | ); | |
340 | ||
341 | // Scrolled widget needs to have its colour changed or we get a little blue | |
342 | // square where the scrollbars abutt | |
343 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
344 | wxDoChangeBackgroundColour(m_scrolledWindow, backgroundColour, true); | |
345 | wxDoChangeBackgroundColour(m_drawingArea, backgroundColour, true); | |
346 | ||
347 | XmScrolledWindowSetAreas( | |
348 | (Widget)m_scrolledWindow, | |
349 | (Widget) 0, (Widget) 0, | |
350 | (Widget) m_drawingArea); | |
351 | ||
352 | // Without this, the cursor may not be restored properly (e.g. in splitter | |
353 | // sample). | |
354 | SetCursor(*wxSTANDARD_CURSOR); | |
355 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
356 | DoSetSizeIntr(pos.x, pos.y, size.x,size.y, wxSIZE_AUTO, true); | |
357 | return true; | |
358 | } | |
359 | ||
360 | // Destructor | |
361 | wxWindow::~wxWindow() | |
362 | { | |
363 | if (g_captureWindow == this) | |
364 | g_captureWindow = NULL; | |
365 | ||
366 | m_isBeingDeleted = true; | |
367 | ||
368 | // Motif-specific actions first | |
369 | WXWidget wMain = GetMainWidget(); | |
370 | if ( wMain ) | |
371 | { | |
372 | // Removes event handlers | |
373 | DetachWidget(wMain); | |
374 | } | |
375 | ||
376 | // If m_drawingArea, we're a fully-fledged window with drawing area, | |
377 | // scrollbars etc. (what wxCanvas used to be) | |
378 | if ( m_drawingArea ) | |
379 | { | |
380 | // Destroy children before destroying self | |
381 | DestroyChildren(); | |
382 | ||
383 | if (m_backingPixmap) | |
384 | XFreePixmap (XtDisplay ((Widget) GetMainWidget()), (Pixmap) m_backingPixmap); | |
385 | ||
386 | Widget w = (Widget) m_drawingArea; | |
387 | wxDeleteWindowFromTable(w); | |
388 | ||
389 | if (w) | |
390 | { | |
391 | XtDestroyWidget(w); | |
392 | m_drawingArea = (WXWidget) 0; | |
393 | } | |
394 | ||
395 | // Only if we're _really_ a canvas (not a dialog box/panel) | |
396 | if (m_scrolledWindow) | |
397 | { | |
398 | wxDeleteWindowFromTable((Widget) m_scrolledWindow); | |
399 | } | |
400 | ||
401 | if (m_hScrollBar) | |
402 | { | |
403 | wxDeleteWindowFromTable((Widget) m_hScrollBar); | |
404 | XtUnmanageChild((Widget) m_hScrollBar); | |
405 | } | |
406 | if (m_vScrollBar) | |
407 | { | |
408 | wxDeleteWindowFromTable((Widget) m_vScrollBar); | |
409 | XtUnmanageChild((Widget) m_vScrollBar); | |
410 | } | |
411 | ||
412 | if (m_hScrollBar) | |
413 | XtDestroyWidget((Widget) m_hScrollBar); | |
414 | if (m_vScrollBar) | |
415 | XtDestroyWidget((Widget) m_vScrollBar); | |
416 | ||
417 | UnmanageAndDestroy(m_scrolledWindow); | |
418 | ||
419 | if (m_borderWidget) | |
420 | { | |
421 | XtDestroyWidget ((Widget) m_borderWidget); | |
422 | m_borderWidget = (WXWidget) 0; | |
423 | } | |
424 | } | |
425 | else // Why wasn't this here before? JACS 8/3/2000 | |
426 | DestroyChildren(); | |
427 | ||
428 | ||
429 | // Destroy the window | |
430 | if (GetMainWidget()) | |
431 | { | |
432 | // If this line (XtDestroyWidget) causes a crash, you may comment it out. | |
433 | // Child widgets will get destroyed automatically when a frame | |
434 | // or dialog is destroyed, but before that you may get some memory | |
435 | // leaks and potential layout problems if you delete and then add | |
436 | // child windows. | |
437 | ||
438 | // GRG, Feb/2000: commented this out when adding support for | |
439 | // wxSCROLL[WIN]_THUMBRELEASE events. Also it was reported | |
440 | // that this call crashed wxMotif under OS/2, so it seems | |
441 | // that leaving it out is the right thing to do. | |
442 | // SN, Feb/2000: newgrid/griddemo shows why it is needed :-( | |
443 | XtDestroyWidget((Widget) GetMainWidget()); | |
444 | SetMainWidget((WXWidget) NULL); | |
445 | } | |
446 | } | |
447 | ||
448 | // ---------------------------------------------------------------------------- | |
449 | // scrollbar management | |
450 | // ---------------------------------------------------------------------------- | |
451 | ||
452 | WXWidget wxWindow::DoCreateScrollBar(WXWidget parent, | |
453 | wxOrientation orientation, | |
454 | void (*callback)()) | |
455 | { | |
456 | int orient = ( orientation & wxHORIZONTAL ) ? XmHORIZONTAL : XmVERTICAL; | |
457 | Widget sb = | |
458 | XtVaCreateManagedWidget( "scrollBarWidget", | |
459 | xmScrollBarWidgetClass, (Widget)parent, | |
460 | XmNorientation, orient, | |
461 | XmNincrement, 1, | |
462 | XmNvalue, 0, | |
463 | NULL ); | |
464 | ||
465 | XtPointer o = (XtPointer)orientation; | |
466 | XtCallbackProc cb = (XtCallbackProc)callback; | |
467 | ||
468 | XtAddCallback( sb, XmNvalueChangedCallback, cb, o ); | |
469 | XtAddCallback( sb, XmNdragCallback, cb, o ); | |
470 | XtAddCallback( sb, XmNincrementCallback, cb, o ); | |
471 | XtAddCallback( sb, XmNdecrementCallback, cb, o ); | |
472 | XtAddCallback( sb, XmNpageIncrementCallback, cb, o ); | |
473 | XtAddCallback( sb, XmNpageDecrementCallback, cb, o ); | |
474 | XtAddCallback( sb, XmNtoTopCallback, cb, o ); | |
475 | XtAddCallback( sb, XmNtoBottomCallback, cb, o ); | |
476 | ||
477 | return (WXWidget)sb; | |
478 | } | |
479 | ||
480 | // Helper function | |
481 | void wxWindow::CreateScrollbar(wxOrientation orientation) | |
482 | { | |
483 | wxCHECK_RET( m_drawingArea, "this window can't have scrollbars" ); | |
484 | ||
485 | XtVaSetValues( (Widget) m_scrolledWindow, | |
486 | XmNresizePolicy, XmRESIZE_NONE, | |
487 | NULL ); | |
488 | ||
489 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
490 | // Add scrollbars if required | |
491 | if (orientation == wxHORIZONTAL) | |
492 | { | |
493 | m_hScrollBar = DoCreateScrollBar( m_scrolledWindow, wxHORIZONTAL, | |
494 | (void (*)())wxScrollBarCallback ); | |
495 | ||
496 | wxDoChangeBackgroundColour(m_hScrollBar, backgroundColour, true); | |
497 | ||
498 | XtRealizeWidget( (Widget)m_hScrollBar ); | |
499 | ||
500 | XtVaSetValues((Widget) m_scrolledWindow, | |
501 | XmNhorizontalScrollBar, (Widget) m_hScrollBar, | |
502 | NULL); | |
503 | ||
504 | wxAddWindowToTable( (Widget)m_hScrollBar, this ); | |
505 | } | |
506 | else if (orientation == wxVERTICAL) | |
507 | { | |
508 | m_vScrollBar = DoCreateScrollBar( m_scrolledWindow, wxVERTICAL, | |
509 | (void (*)())wxScrollBarCallback ); | |
510 | ||
511 | wxDoChangeBackgroundColour(m_vScrollBar, backgroundColour, true); | |
512 | ||
513 | XtRealizeWidget((Widget)m_vScrollBar); | |
514 | ||
515 | XtVaSetValues((Widget) m_scrolledWindow, | |
516 | XmNverticalScrollBar, (Widget) m_vScrollBar, | |
517 | NULL); | |
518 | ||
519 | wxAddWindowToTable( (Widget)m_vScrollBar, this ); | |
520 | } | |
521 | ||
522 | XtVaSetValues( (Widget) m_scrolledWindow, | |
523 | XmNresizePolicy, XmRESIZE_ANY, | |
524 | NULL ); | |
525 | } | |
526 | ||
527 | void wxWindow::DestroyScrollbar(wxOrientation orientation) | |
528 | { | |
529 | wxCHECK_RET( m_drawingArea, "this window can't have scrollbars" ); | |
530 | ||
531 | XtVaSetValues((Widget) m_scrolledWindow, | |
532 | XmNresizePolicy, XmRESIZE_NONE, | |
533 | NULL); | |
534 | String stringSB = orientation == wxHORIZONTAL ? | |
535 | XmNhorizontalScrollBar : XmNverticalScrollBar; | |
536 | WXWidget* widgetSB = orientation == wxHORIZONTAL ? | |
537 | &m_hScrollBar : &m_vScrollBar; | |
538 | ||
539 | if( *widgetSB ) | |
540 | { | |
541 | wxDeleteWindowFromTable( (Widget)*widgetSB ); | |
542 | XtDestroyWidget( (Widget)*widgetSB ); | |
543 | *widgetSB = (WXWidget)NULL; | |
544 | } | |
545 | ||
546 | XtVaSetValues( (Widget)m_scrolledWindow, | |
547 | stringSB, (Widget) 0, | |
548 | NULL ); | |
549 | ||
550 | XtVaSetValues((Widget) m_scrolledWindow, | |
551 | XmNresizePolicy, XmRESIZE_ANY, | |
552 | NULL); | |
553 | } | |
554 | ||
555 | // --------------------------------------------------------------------------- | |
556 | // basic operations | |
557 | // --------------------------------------------------------------------------- | |
558 | ||
559 | void wxWindow::SetFocus() | |
560 | { | |
561 | Widget wMain = (Widget) GetMainWidget(); | |
562 | XmProcessTraversal(wMain, XmTRAVERSE_CURRENT); | |
563 | XmProcessTraversal((Widget) GetMainWidget(), XmTRAVERSE_CURRENT); | |
564 | } | |
565 | ||
566 | // Get the window with the focus | |
567 | wxWindow *wxWindowBase::DoFindFocus() | |
568 | { | |
569 | // TODO Problems: | |
570 | // (1) Can there be multiple focussed widgets in an application? | |
571 | // In which case we need to find the top-level window that's | |
572 | // currently active. | |
573 | // (2) The widget with the focus may not be in the widget table | |
574 | // depending on which widgets I put in the table | |
575 | wxWindow *winFocus = (wxWindow *)NULL; | |
576 | for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst(); | |
577 | node; | |
578 | node = node->GetNext() ) | |
579 | { | |
580 | wxWindow *win = node->GetData(); | |
581 | ||
582 | Widget w = XmGetFocusWidget ((Widget) win->GetTopWidget()); | |
583 | ||
584 | if (w != (Widget) NULL) | |
585 | { | |
586 | winFocus = wxGetWindowFromTable(w); | |
587 | if ( winFocus ) | |
588 | break; | |
589 | } | |
590 | } | |
591 | ||
592 | return winFocus; | |
593 | } | |
594 | ||
595 | bool wxWindow::Enable(bool enable) | |
596 | { | |
597 | if ( !wxWindowBase::Enable(enable) ) | |
598 | return false; | |
599 | ||
600 | Widget wMain = (Widget)GetMainWidget(); | |
601 | if ( wMain ) | |
602 | { | |
603 | XtSetSensitive(wMain, enable); | |
604 | XmUpdateDisplay(wMain); | |
605 | } | |
606 | ||
607 | return true; | |
608 | } | |
609 | ||
610 | bool wxWindow::Show(bool show) | |
611 | { | |
612 | if ( !wxWindowBase::Show(show) ) | |
613 | return false; | |
614 | ||
615 | if (m_borderWidget || m_scrolledWindow) | |
616 | { | |
617 | MapOrUnmap(m_borderWidget ? m_borderWidget : m_scrolledWindow, show); | |
618 | // MapOrUnmap(m_drawingArea, show); | |
619 | } | |
620 | else | |
621 | { | |
622 | if ( !MapOrUnmap(GetTopWidget(), show) ) | |
623 | MapOrUnmap(GetMainWidget(), show); | |
624 | } | |
625 | ||
626 | return true; | |
627 | } | |
628 | ||
629 | // Raise the window to the top of the Z order | |
630 | void wxWindow::Raise() | |
631 | { | |
632 | Widget wTop = (Widget) GetTopWidget(); | |
633 | Window window = XtWindow(wTop); | |
634 | XRaiseWindow(XtDisplay(wTop), window); | |
635 | } | |
636 | ||
637 | // Lower the window to the bottom of the Z order | |
638 | void wxWindow::Lower() | |
639 | { | |
640 | Widget wTop = (Widget) GetTopWidget(); | |
641 | Window window = XtWindow(wTop); | |
642 | XLowerWindow(XtDisplay(wTop), window); | |
643 | } | |
644 | ||
645 | void wxWindow::SetTitle(const wxString& title) | |
646 | { | |
647 | XtVaSetValues((Widget)GetMainWidget(), XmNtitle, title.c_str(), NULL); | |
648 | } | |
649 | ||
650 | wxString wxWindow::GetTitle() const | |
651 | { | |
652 | char *title; | |
653 | XtVaGetValues((Widget)GetMainWidget(), XmNtitle, &title, NULL); | |
654 | ||
655 | return wxString(title); | |
656 | } | |
657 | ||
658 | void wxWindow::DoCaptureMouse() | |
659 | { | |
660 | g_captureWindow = this; | |
661 | if ( m_winCaptured ) | |
662 | return; | |
663 | ||
664 | Widget wMain = (Widget)GetMainWidget(); | |
665 | if ( wMain ) | |
666 | XtAddGrab(wMain, True, False); | |
667 | ||
668 | m_winCaptured = true; | |
669 | } | |
670 | ||
671 | void wxWindow::DoReleaseMouse() | |
672 | { | |
673 | g_captureWindow = NULL; | |
674 | if ( !m_winCaptured ) | |
675 | return; | |
676 | ||
677 | Widget wMain = (Widget)GetMainWidget(); | |
678 | if ( wMain ) | |
679 | XtRemoveGrab(wMain); | |
680 | ||
681 | m_winCaptured = false; | |
682 | } | |
683 | ||
684 | bool wxWindow::SetFont(const wxFont& font) | |
685 | { | |
686 | if ( !wxWindowBase::SetFont(font) ) | |
687 | { | |
688 | // nothing to do | |
689 | return false; | |
690 | } | |
691 | ||
692 | ChangeFont(); | |
693 | ||
694 | return true; | |
695 | } | |
696 | ||
697 | bool wxWindow::SetCursor(const wxCursor& cursor) | |
698 | { | |
699 | if ( !wxWindowBase::SetCursor(cursor) ) | |
700 | { | |
701 | // no change | |
702 | return false; | |
703 | } | |
704 | ||
705 | // wxASSERT_MSG( m_cursor.Ok(), | |
706 | // wxT("cursor must be valid after call to the base version")); | |
707 | wxCursor* cursor2 = NULL; | |
708 | if (m_cursor.Ok()) | |
709 | cursor2 = & m_cursor; | |
710 | else | |
711 | cursor2 = wxSTANDARD_CURSOR; | |
712 | ||
713 | WXDisplay *dpy = GetXDisplay(); | |
714 | WXCursor x_cursor = cursor2->GetXCursor(dpy); | |
715 | ||
716 | Widget w = (Widget) GetMainWidget(); | |
717 | Window win = XtWindow(w); | |
718 | XDefineCursor((Display*) dpy, win, (Cursor) x_cursor); | |
719 | ||
720 | return true; | |
721 | } | |
722 | ||
723 | // Coordinates relative to the window | |
724 | void wxWindow::WarpPointer (int x, int y) | |
725 | { | |
726 | Widget wClient = (Widget)GetClientWidget(); | |
727 | ||
728 | XWarpPointer(XtDisplay(wClient), None, XtWindow(wClient), 0, 0, 0, 0, x, y); | |
729 | } | |
730 | ||
731 | // --------------------------------------------------------------------------- | |
732 | // scrolling stuff | |
733 | // --------------------------------------------------------------------------- | |
734 | ||
735 | int wxWindow::GetScrollPos(int orient) const | |
736 | { | |
737 | if (orient == wxHORIZONTAL) | |
738 | return m_scrollPosX; | |
739 | else | |
740 | return m_scrollPosY; | |
741 | ||
742 | #if 0 | |
743 | Widget scrollBar = (Widget) ((orient == wxHORIZONTAL) ? m_hScrollBar : m_vScrollBar); | |
744 | if (scrollBar) | |
745 | { | |
746 | int pos; | |
747 | XtVaGetValues(scrollBar, XmNvalue, &pos, NULL); | |
748 | return pos; | |
749 | } | |
750 | else | |
751 | return 0; | |
752 | #endif // 0 | |
753 | } | |
754 | ||
755 | // This now returns the whole range, not just the number of positions that we | |
756 | // can scroll. | |
757 | int wxWindow::GetScrollRange(int orient) const | |
758 | { | |
759 | Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient); | |
760 | // CE scintilla windows don't always have these scrollbars | |
761 | // and it tends to pile up a whole bunch of asserts | |
762 | //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" ); | |
763 | ||
764 | int range = 0; | |
765 | if (scrollBar) | |
766 | XtVaGetValues(scrollBar, XmNmaximum, &range, NULL); | |
767 | return range; | |
768 | } | |
769 | ||
770 | int wxWindow::GetScrollThumb(int orient) const | |
771 | { | |
772 | Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient); | |
773 | //wxCHECK_MSG( scrollBar, 0, "no such scrollbar" ); | |
774 | ||
775 | int thumb = 0; | |
776 | if (scrollBar) | |
777 | XtVaGetValues(scrollBar, XmNsliderSize, &thumb, NULL); | |
778 | return thumb; | |
779 | } | |
780 | ||
781 | void wxWindow::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh)) | |
782 | { | |
783 | Widget scrollBar = (Widget)GetScrollbar((wxOrientation)orient); | |
784 | ||
785 | if ( scrollBar ) | |
786 | { | |
787 | XtVaSetValues (scrollBar, XmNvalue, pos, NULL); | |
788 | } | |
789 | ||
790 | SetInternalScrollPos((wxOrientation)orient, pos); | |
791 | } | |
792 | ||
793 | // New function that will replace some of the above. | |
794 | void wxWindow::SetScrollbar(int orient, int pos, int thumbVisible, | |
795 | int range, bool WXUNUSED(refresh)) | |
796 | { | |
797 | int oldW, oldH; | |
798 | GetSize(& oldW, & oldH); | |
799 | ||
800 | if (range == 0) | |
801 | range = 1; | |
802 | if (thumbVisible == 0) | |
803 | thumbVisible = 1; | |
804 | ||
805 | if (thumbVisible > range) | |
806 | thumbVisible = range; | |
807 | ||
808 | // Save the old state to see if it changed | |
809 | WXWidget oldScrollBar = GetScrollbar((wxOrientation)orient); | |
810 | ||
811 | if (orient == wxHORIZONTAL) | |
812 | { | |
813 | if (thumbVisible == range) | |
814 | { | |
815 | if (m_hScrollBar) | |
816 | DestroyScrollbar(wxHORIZONTAL); | |
817 | } | |
818 | else | |
819 | { | |
820 | if (!m_hScrollBar) | |
821 | CreateScrollbar(wxHORIZONTAL); | |
822 | } | |
823 | } | |
824 | if (orient == wxVERTICAL) | |
825 | { | |
826 | if (thumbVisible == range) | |
827 | { | |
828 | if (m_vScrollBar) | |
829 | DestroyScrollbar(wxVERTICAL); | |
830 | } | |
831 | else | |
832 | { | |
833 | if (!m_vScrollBar) | |
834 | CreateScrollbar(wxVERTICAL); | |
835 | } | |
836 | } | |
837 | WXWidget newScrollBar = GetScrollbar((wxOrientation)orient); | |
838 | ||
839 | if (oldScrollBar != newScrollBar) | |
840 | { | |
841 | // This is important! Without it, scrollbars misbehave badly. | |
842 | XtUnrealizeWidget((Widget) m_scrolledWindow); | |
843 | XmScrolledWindowSetAreas ((Widget) m_scrolledWindow, (Widget) m_hScrollBar, (Widget) m_vScrollBar, (Widget) m_drawingArea); | |
844 | XtRealizeWidget((Widget) m_scrolledWindow); | |
845 | XtManageChild((Widget) m_scrolledWindow); | |
846 | } | |
847 | ||
848 | if (newScrollBar) | |
849 | { | |
850 | XtVaSetValues((Widget) newScrollBar, | |
851 | XmNvalue, pos, | |
852 | XmNminimum, 0, | |
853 | XmNmaximum, range, | |
854 | XmNsliderSize, thumbVisible, | |
855 | NULL); | |
856 | } | |
857 | ||
858 | SetInternalScrollPos((wxOrientation)orient, pos); | |
859 | ||
860 | int newW, newH; | |
861 | GetSize(& newW, & newH); | |
862 | ||
863 | // Adjusting scrollbars can resize the canvas accidentally | |
864 | if (newW != oldW || newH != oldH) | |
865 | SetSize(-1, -1, oldW, oldH); | |
866 | } | |
867 | ||
868 | // Does a physical scroll | |
869 | void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect) | |
870 | { | |
871 | int x, y, w, h; | |
872 | if (rect) | |
873 | { | |
874 | // Use specified rectangle | |
875 | x = rect->x; y = rect->y; w = rect->width; h = rect->height; | |
876 | } | |
877 | else | |
878 | { | |
879 | // Use whole client area | |
880 | x = 0; y = 0; | |
881 | GetClientSize(& w, & h); | |
882 | } | |
883 | ||
884 | int x1 = (dx >= 0) ? x : x - dx; | |
885 | int y1 = (dy >= 0) ? y : y - dy; | |
886 | int w1 = w - abs(dx); | |
887 | int h1 = h - abs(dy); | |
888 | int x2 = (dx >= 0) ? x + dx : x; | |
889 | int y2 = (dy >= 0) ? y + dy : y; | |
890 | ||
891 | wxClientDC dc(this); | |
892 | ||
893 | dc.SetLogicalFunction (wxCOPY); | |
894 | ||
895 | Widget widget = (Widget) GetMainWidget(); | |
896 | Window window = XtWindow(widget); | |
897 | Display* display = XtDisplay(widget); | |
898 | ||
899 | XCopyArea(display, window, window, (GC) dc.GetGC(), | |
900 | x1, y1, w1, h1, x2, y2); | |
901 | ||
902 | dc.SetAutoSetting(true); | |
903 | wxBrush brush(GetBackgroundColour(), wxSOLID); | |
904 | dc.SetBrush(brush); // FIXME: needed? | |
905 | ||
906 | wxWindowList::compatibility_iterator cnode = m_children.GetFirst(); | |
907 | while (cnode) | |
908 | { | |
909 | wxWindow *child = cnode->GetData(); | |
910 | int sx = 0; | |
911 | int sy = 0; | |
912 | child->GetSize( &sx, &sy ); | |
913 | wxPoint pos( child->GetPosition() ); | |
914 | child->SetSize( pos.x + dx, pos.y + dy, sx, sy, wxSIZE_ALLOW_MINUS_ONE ); | |
915 | cnode = cnode->GetNext(); | |
916 | } | |
917 | ||
918 | // We'll add rectangles to the list of update rectangles according to which | |
919 | // bits we've exposed. | |
920 | wxList updateRects; | |
921 | ||
922 | if (dx > 0) | |
923 | { | |
924 | wxRect *rect = new wxRect; | |
925 | rect->x = x; | |
926 | rect->y = y; | |
927 | rect->width = dx; | |
928 | rect->height = h; | |
929 | ||
930 | XFillRectangle(display, window, | |
931 | (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height); | |
932 | ||
933 | rect->x = rect->x; | |
934 | rect->y = rect->y; | |
935 | rect->width = rect->width; | |
936 | rect->height = rect->height; | |
937 | ||
938 | updateRects.Append((wxObject*) rect); | |
939 | } | |
940 | else if (dx < 0) | |
941 | { | |
942 | wxRect *rect = new wxRect; | |
943 | ||
944 | rect->x = x + w + dx; | |
945 | rect->y = y; | |
946 | rect->width = -dx; | |
947 | rect->height = h; | |
948 | ||
949 | XFillRectangle(display, window, | |
950 | (GC) dc.GetGC(), rect->x, rect->y, rect->width, | |
951 | rect->height); | |
952 | ||
953 | rect->x = rect->x; | |
954 | rect->y = rect->y; | |
955 | rect->width = rect->width; | |
956 | rect->height = rect->height; | |
957 | ||
958 | updateRects.Append((wxObject*) rect); | |
959 | } | |
960 | if (dy > 0) | |
961 | { | |
962 | wxRect *rect = new wxRect; | |
963 | ||
964 | rect->x = x; | |
965 | rect->y = y; | |
966 | rect->width = w; | |
967 | rect->height = dy; | |
968 | ||
969 | XFillRectangle(display, window, | |
970 | (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height); | |
971 | ||
972 | rect->x = rect->x; | |
973 | rect->y = rect->y; | |
974 | rect->width = rect->width; | |
975 | rect->height = rect->height; | |
976 | ||
977 | updateRects.Append((wxObject*) rect); | |
978 | } | |
979 | else if (dy < 0) | |
980 | { | |
981 | wxRect *rect = new wxRect; | |
982 | ||
983 | rect->x = x; | |
984 | rect->y = y + h + dy; | |
985 | rect->width = w; | |
986 | rect->height = -dy; | |
987 | ||
988 | XFillRectangle(display, window, | |
989 | (GC) dc.GetGC(), rect->x, rect->y, rect->width, rect->height); | |
990 | ||
991 | rect->x = rect->x; | |
992 | rect->y = rect->y; | |
993 | rect->width = rect->width; | |
994 | rect->height = rect->height; | |
995 | ||
996 | updateRects.Append((wxObject*) rect); | |
997 | } | |
998 | dc.SetBrush(wxNullBrush); | |
999 | ||
1000 | // Now send expose events | |
1001 | ||
1002 | wxList::compatibility_iterator node = updateRects.GetFirst(); | |
1003 | while (node) | |
1004 | { | |
1005 | wxRect* rect = (wxRect*) node->GetData(); | |
1006 | XExposeEvent event; | |
1007 | ||
1008 | event.type = Expose; | |
1009 | event.display = display; | |
1010 | event.send_event = True; | |
1011 | event.window = window; | |
1012 | ||
1013 | event.x = rect->x; | |
1014 | event.y = rect->y; | |
1015 | event.width = rect->width; | |
1016 | event.height = rect->height; | |
1017 | ||
1018 | event.count = 0; | |
1019 | ||
1020 | XSendEvent(display, window, False, ExposureMask, (XEvent *)&event); | |
1021 | ||
1022 | node = node->GetNext(); | |
1023 | ||
1024 | } | |
1025 | ||
1026 | // Delete the update rects | |
1027 | node = updateRects.GetFirst(); | |
1028 | while (node) | |
1029 | { | |
1030 | wxRect* rect = (wxRect*) node->GetData(); | |
1031 | delete rect; | |
1032 | node = node->GetNext(); | |
1033 | } | |
1034 | ||
1035 | XmUpdateDisplay((Widget) GetMainWidget()); | |
1036 | } | |
1037 | ||
1038 | // --------------------------------------------------------------------------- | |
1039 | // drag and drop | |
1040 | // --------------------------------------------------------------------------- | |
1041 | ||
1042 | #if wxUSE_DRAG_AND_DROP | |
1043 | ||
1044 | void wxWindow::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget)) | |
1045 | { | |
1046 | // TODO | |
1047 | } | |
1048 | ||
1049 | #endif | |
1050 | ||
1051 | // Old style file-manager drag&drop | |
1052 | void wxWindow::DragAcceptFiles(bool WXUNUSED(accept)) | |
1053 | { | |
1054 | // TODO | |
1055 | } | |
1056 | ||
1057 | // ---------------------------------------------------------------------------- | |
1058 | // tooltips | |
1059 | // ---------------------------------------------------------------------------- | |
1060 | ||
1061 | #if wxUSE_TOOLTIPS | |
1062 | ||
1063 | void wxWindow::DoSetToolTip(wxToolTip * WXUNUSED(tooltip)) | |
1064 | { | |
1065 | // TODO | |
1066 | } | |
1067 | ||
1068 | #endif // wxUSE_TOOLTIPS | |
1069 | ||
1070 | // ---------------------------------------------------------------------------- | |
1071 | // popup menus | |
1072 | // ---------------------------------------------------------------------------- | |
1073 | ||
1074 | #if wxUSE_MENUS | |
1075 | ||
1076 | bool wxWindow::DoPopupMenu(wxMenu *menu, int x, int y) | |
1077 | { | |
1078 | if ( x == -1 && y == -1 ) | |
1079 | { | |
1080 | wxPoint mouse = ScreenToClient(wxGetMousePosition()); | |
1081 | x = mouse.x; y = mouse.y; | |
1082 | } | |
1083 | ||
1084 | Widget widget = (Widget) GetMainWidget(); | |
1085 | ||
1086 | /* The menuId field seems to be usused, so we'll use it to | |
1087 | indicate whether a menu is popped up or not: | |
1088 | 0: Not currently created as a popup | |
1089 | -1: Created as a popup, but not active | |
1090 | 1: Active popup. | |
1091 | */ | |
1092 | ||
1093 | if (menu->GetParent() && (menu->GetId() != -1)) | |
1094 | return false; | |
1095 | ||
1096 | if (menu->GetMainWidget()) | |
1097 | { | |
1098 | menu->DestroyMenu(true); | |
1099 | } | |
1100 | ||
1101 | menu->SetId(1); /* Mark as popped-up */ | |
1102 | menu->CreateMenu(NULL, widget, menu); | |
1103 | menu->SetInvokingWindow(this); | |
1104 | ||
1105 | menu->UpdateUI(); | |
1106 | ||
1107 | // menu->SetParent(parent); | |
1108 | // parent->children->Append(menu); // Store menu for later deletion | |
1109 | ||
1110 | Widget menuWidget = (Widget) menu->GetMainWidget(); | |
1111 | ||
1112 | int rootX = 0; | |
1113 | int rootY = 0; | |
1114 | ||
1115 | int deviceX = x; | |
1116 | int deviceY = y; | |
1117 | /* | |
1118 | if (this->IsKindOf(CLASSINFO(wxCanvas))) | |
1119 | { | |
1120 | wxCanvas *canvas = (wxCanvas *) this; | |
1121 | deviceX = canvas->GetDC ()->LogicalToDeviceX (x); | |
1122 | deviceY = canvas->GetDC ()->LogicalToDeviceY (y); | |
1123 | } | |
1124 | */ | |
1125 | ||
1126 | Display *display = XtDisplay (widget); | |
1127 | Window rootWindow = RootWindowOfScreen (XtScreen((Widget)widget)); | |
1128 | Window thisWindow = XtWindow (widget); | |
1129 | Window childWindow; | |
1130 | XTranslateCoordinates (display, thisWindow, rootWindow, (int) deviceX, (int) deviceY, | |
1131 | &rootX, &rootY, &childWindow); | |
1132 | ||
1133 | XButtonPressedEvent event; | |
1134 | event.type = ButtonPress; | |
1135 | event.button = 1; | |
1136 | ||
1137 | event.x = deviceX; | |
1138 | event.y = deviceY; | |
1139 | ||
1140 | event.x_root = rootX; | |
1141 | event.y_root = rootY; | |
1142 | ||
1143 | XmMenuPosition (menuWidget, &event); | |
1144 | XtManageChild (menuWidget); | |
1145 | ||
1146 | // The ID of a pop-up menu is 1 when active, and is set to 0 by the | |
1147 | // idle-time destroy routine. | |
1148 | // Waiting until this ID changes causes this function to block until | |
1149 | // the menu has been dismissed and the widgets cleaned up. | |
1150 | // In other words, once this routine returns, it is safe to delete | |
1151 | // the menu object. | |
1152 | // Ian Brown <ian.brown@printsoft.de> | |
1153 | ||
1154 | wxEventLoop evtLoop; | |
1155 | ||
1156 | while (menu->GetId() == 1) | |
1157 | { | |
1158 | wxDoEventLoopIteration( evtLoop ); | |
1159 | } | |
1160 | ||
1161 | return true; | |
1162 | } | |
1163 | ||
1164 | #endif | |
1165 | ||
1166 | // --------------------------------------------------------------------------- | |
1167 | // moving and resizing | |
1168 | // --------------------------------------------------------------------------- | |
1169 | ||
1170 | bool wxWindow::PreResize() | |
1171 | { | |
1172 | return true; | |
1173 | } | |
1174 | ||
1175 | // Get total size | |
1176 | void wxWindow::DoGetSize(int *x, int *y) const | |
1177 | { | |
1178 | Widget widget = (Widget)( !m_drawingArea ? GetTopWidget() : | |
1179 | ( m_borderWidget ? m_borderWidget : | |
1180 | m_scrolledWindow ? m_scrolledWindow : | |
1181 | m_drawingArea ) ); | |
1182 | Dimension xx, yy; | |
1183 | ||
1184 | XtVaGetValues( widget, | |
1185 | XmNwidth, &xx, | |
1186 | XmNheight, &yy, | |
1187 | NULL ); | |
1188 | if(x) *x = xx; | |
1189 | if(y) *y = yy; | |
1190 | } | |
1191 | ||
1192 | void wxWindow::DoGetPosition(int *x, int *y) const | |
1193 | { | |
1194 | Widget widget = (Widget) | |
1195 | ( m_drawingArea ? | |
1196 | ( m_borderWidget ? m_borderWidget : m_scrolledWindow ) : | |
1197 | GetTopWidget() ); | |
1198 | ||
1199 | Position xx, yy; | |
1200 | XtVaGetValues(widget, XmNx, &xx, XmNy, &yy, NULL); | |
1201 | ||
1202 | // We may be faking the client origin. So a window that's really at (0, 30) | |
1203 | // may appear (to wxWin apps) to be at (0, 0). | |
1204 | if (GetParent()) | |
1205 | { | |
1206 | wxPoint pt(GetParent()->GetClientAreaOrigin()); | |
1207 | xx -= pt.x; | |
1208 | yy -= pt.y; | |
1209 | } | |
1210 | ||
1211 | if(x) *x = xx; | |
1212 | if(y) *y = yy; | |
1213 | } | |
1214 | ||
1215 | void wxWindow::DoScreenToClient(int *x, int *y) const | |
1216 | { | |
1217 | Widget widget = (Widget) GetClientWidget(); | |
1218 | Display *display = XtDisplay((Widget) GetMainWidget()); | |
1219 | Window rootWindow = RootWindowOfScreen(XtScreen(widget)); | |
1220 | Window thisWindow = XtWindow(widget); | |
1221 | ||
1222 | Window childWindow; | |
1223 | int xx = *x; | |
1224 | int yy = *y; | |
1225 | XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow); | |
1226 | } | |
1227 | ||
1228 | void wxWindow::DoClientToScreen(int *x, int *y) const | |
1229 | { | |
1230 | Widget widget = (Widget) GetClientWidget(); | |
1231 | Display *display = XtDisplay(widget); | |
1232 | Window rootWindow = RootWindowOfScreen(XtScreen(widget)); | |
1233 | Window thisWindow = XtWindow(widget); | |
1234 | ||
1235 | Window childWindow; | |
1236 | int xx = *x; | |
1237 | int yy = *y; | |
1238 | XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow); | |
1239 | } | |
1240 | ||
1241 | ||
1242 | // Get size *available for subwindows* i.e. excluding menu bar etc. | |
1243 | void wxWindow::DoGetClientSize(int *x, int *y) const | |
1244 | { | |
1245 | Widget widget = (Widget) GetClientWidget(); | |
1246 | Dimension xx, yy; | |
1247 | XtVaGetValues(widget, XmNwidth, &xx, XmNheight, &yy, NULL); | |
1248 | if(x) *x = xx; if(y) *y = yy; | |
1249 | } | |
1250 | ||
1251 | void wxWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
1252 | { | |
1253 | DoSetSizeIntr(x, y, width, height, sizeFlags, false); | |
1254 | } | |
1255 | ||
1256 | void wxWindow::DoSetSizeIntr(int x, int y, int width, int height, | |
1257 | int sizeFlags, bool fromCtor) | |
1258 | { | |
1259 | // A bit of optimization to help sort out the flickers. | |
1260 | int oldX = -1, oldY = -1, oldW = -1, oldH = -1; | |
1261 | if( !fromCtor ) | |
1262 | { | |
1263 | GetSize(& oldW, & oldH); | |
1264 | GetPosition(& oldX, & oldY); | |
1265 | } | |
1266 | ||
1267 | if ( !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) ) | |
1268 | { | |
1269 | if ( x == -1 ) | |
1270 | x = oldX; | |
1271 | if ( y == -1 ) | |
1272 | y = oldY; | |
1273 | } | |
1274 | ||
1275 | wxSize size(-1, -1); | |
1276 | if ( width <= 0 ) | |
1277 | { | |
1278 | if ( ( sizeFlags & wxSIZE_AUTO_WIDTH ) && !fromCtor ) | |
1279 | { | |
1280 | size = DoGetBestSize(); | |
1281 | width = size.x; | |
1282 | } | |
1283 | else | |
1284 | { | |
1285 | width = oldW; | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | if ( height == -1 ) | |
1290 | { | |
1291 | if( ( sizeFlags & wxSIZE_AUTO_HEIGHT ) && !fromCtor ) | |
1292 | { | |
1293 | if( size.x == -1 ) size = DoGetBestSize(); | |
1294 | height = size.y; | |
1295 | } | |
1296 | else | |
1297 | { | |
1298 | height = oldH; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | if ( x != oldX || y != oldY || width != oldW || height != oldH | |
1303 | || !wxNoOptimize::CanOptimize() ) | |
1304 | { | |
1305 | if (m_drawingArea) | |
1306 | { | |
1307 | int flags = 0; | |
1308 | ||
1309 | if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
1310 | flags |= wxMOVE_X; | |
1311 | ||
1312 | if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
1313 | flags |= wxMOVE_Y; | |
1314 | ||
1315 | if (width > 0) | |
1316 | flags |= wxMOVE_WIDTH; | |
1317 | ||
1318 | if (height > 0) | |
1319 | flags |= wxMOVE_HEIGHT; | |
1320 | ||
1321 | int xx = x; int yy = y; | |
1322 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
1323 | if( !fromCtor ) | |
1324 | DoMoveWindow( xx, yy, width, height ); | |
1325 | else | |
1326 | DoMoveWindowIntr( xx, yy, width, height, flags ); | |
1327 | ||
1328 | return; | |
1329 | } | |
1330 | ||
1331 | Widget widget = (Widget) GetTopWidget(); | |
1332 | if (!widget) | |
1333 | return; | |
1334 | ||
1335 | bool managed = XtIsManaged( widget ); | |
1336 | if (managed) | |
1337 | XtUnmanageChild(widget); | |
1338 | ||
1339 | int xx = x; | |
1340 | int yy = y; | |
1341 | AdjustForParentClientOrigin(xx, yy, sizeFlags); | |
1342 | ||
1343 | DoMoveWindow(xx, yy, width, height); | |
1344 | ||
1345 | if (managed) | |
1346 | XtManageChild(widget); | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | void wxWindow::DoSetClientSize(int width, int height) | |
1351 | { | |
1352 | if (m_drawingArea) | |
1353 | { | |
1354 | Widget drawingArea = (Widget) m_drawingArea; | |
1355 | ||
1356 | XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL); | |
1357 | ||
1358 | if (width > -1) | |
1359 | XtVaSetValues(drawingArea, XmNwidth, width, NULL); | |
1360 | if (height > -1) | |
1361 | XtVaSetValues(drawingArea, XmNheight, height, NULL); | |
1362 | ||
1363 | XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL); | |
1364 | return; | |
1365 | } | |
1366 | ||
1367 | Widget widget = (Widget) GetTopWidget(); | |
1368 | ||
1369 | if (width > -1) | |
1370 | XtVaSetValues(widget, XmNwidth, width, NULL); | |
1371 | if (height > -1) | |
1372 | XtVaSetValues(widget, XmNheight, height, NULL); | |
1373 | } | |
1374 | ||
1375 | void wxWindow::DoMoveWindowIntr(int xx, int yy, int w, int h, | |
1376 | int flags) | |
1377 | { | |
1378 | if (m_drawingArea) | |
1379 | { | |
1380 | Widget drawingArea = (Widget) m_drawingArea; | |
1381 | Widget borderOrScrolled = m_borderWidget ? | |
1382 | (Widget) m_borderWidget : | |
1383 | (Widget) m_scrolledWindow; | |
1384 | ||
1385 | bool managed = XtIsManaged(borderOrScrolled); | |
1386 | if (managed) | |
1387 | XtUnmanageChild (borderOrScrolled); | |
1388 | XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_ANY, NULL); | |
1389 | ||
1390 | if (flags & wxMOVE_X) | |
1391 | XtVaSetValues (borderOrScrolled, | |
1392 | XmNx, xx, | |
1393 | NULL); | |
1394 | if (flags & wxMOVE_Y) | |
1395 | XtVaSetValues (borderOrScrolled, | |
1396 | XmNy, yy, | |
1397 | NULL); | |
1398 | ||
1399 | if (flags & wxMOVE_WIDTH) | |
1400 | { | |
1401 | if (m_borderWidget) | |
1402 | { | |
1403 | XtVaSetValues ((Widget) m_borderWidget, XmNwidth, w, NULL); | |
1404 | short thick, margin; | |
1405 | XtVaGetValues ((Widget) m_borderWidget, | |
1406 | XmNshadowThickness, &thick, | |
1407 | XmNmarginWidth, &margin, | |
1408 | NULL); | |
1409 | w -= 2 * (thick + margin); | |
1410 | } | |
1411 | ||
1412 | if( w < 1 ) w = 1; | |
1413 | XtVaSetValues ((Widget) m_scrolledWindow, XmNwidth, w, NULL); | |
1414 | } | |
1415 | ||
1416 | if (flags & wxMOVE_HEIGHT) | |
1417 | { | |
1418 | if (m_borderWidget) | |
1419 | { | |
1420 | XtVaSetValues ((Widget) m_borderWidget, XmNheight, h, NULL); | |
1421 | short thick, margin; | |
1422 | XtVaGetValues ((Widget) m_borderWidget, | |
1423 | XmNshadowThickness, &thick, | |
1424 | XmNmarginHeight, &margin, | |
1425 | NULL); | |
1426 | h -= 2 * (thick + margin); | |
1427 | } | |
1428 | ||
1429 | if( h < 1 ) h = 1; | |
1430 | XtVaSetValues ((Widget) m_scrolledWindow, XmNheight, h, NULL); | |
1431 | } | |
1432 | ||
1433 | if (managed) | |
1434 | XtManageChild (borderOrScrolled); | |
1435 | XtVaSetValues(drawingArea, XmNresizePolicy, XmRESIZE_NONE, NULL); | |
1436 | } | |
1437 | else | |
1438 | { | |
1439 | if( xx < 0 ) xx = 0; | |
1440 | if( yy < 0 ) yy = 0; | |
1441 | if( w < 1 ) w = 1; | |
1442 | if( h < 1 ) h = 1; | |
1443 | ||
1444 | XtVaSetValues((Widget)GetTopWidget(), | |
1445 | XmNx, xx, | |
1446 | XmNy, yy, | |
1447 | XmNwidth, w, | |
1448 | XmNheight, h, | |
1449 | NULL); | |
1450 | } | |
1451 | } | |
1452 | ||
1453 | void wxWindow::DoMoveWindow(int x, int y, int width, int height) | |
1454 | { | |
1455 | DoMoveWindowIntr (x, y, width, height, | |
1456 | wxMOVE_X|wxMOVE_Y|wxMOVE_WIDTH|wxMOVE_HEIGHT); | |
1457 | } | |
1458 | ||
1459 | // --------------------------------------------------------------------------- | |
1460 | // text metrics | |
1461 | // --------------------------------------------------------------------------- | |
1462 | ||
1463 | int wxWindow::GetCharHeight() const | |
1464 | { | |
1465 | wxCHECK_MSG( m_font.Ok(), 0, "valid window font needed" ); | |
1466 | ||
1467 | int height; | |
1468 | ||
1469 | wxGetTextExtent (GetXDisplay(), m_font, 1.0, | |
1470 | "x", NULL, &height, NULL, NULL); | |
1471 | ||
1472 | return height; | |
1473 | } | |
1474 | ||
1475 | int wxWindow::GetCharWidth() const | |
1476 | { | |
1477 | wxCHECK_MSG( m_font.Ok(), 0, "valid window font needed" ); | |
1478 | ||
1479 | int width; | |
1480 | ||
1481 | wxGetTextExtent (GetXDisplay(), m_font, 1.0, | |
1482 | "x", &width, NULL, NULL, NULL); | |
1483 | ||
1484 | return width; | |
1485 | } | |
1486 | ||
1487 | void wxWindow::GetTextExtent(const wxString& string, | |
1488 | int *x, int *y, | |
1489 | int *descent, int *externalLeading, | |
1490 | const wxFont *theFont) const | |
1491 | { | |
1492 | const wxFont *fontToUse = theFont ? theFont : &m_font; | |
1493 | ||
1494 | wxCHECK_RET( fontToUse->Ok(), "valid window font needed" ); | |
1495 | ||
1496 | if (externalLeading) | |
1497 | *externalLeading = 0; | |
1498 | wxGetTextExtent (GetXDisplay(), *fontToUse, 1.0, | |
1499 | string, x, y, NULL, descent); | |
1500 | } | |
1501 | ||
1502 | // ---------------------------------------------------------------------------- | |
1503 | // painting | |
1504 | // ---------------------------------------------------------------------------- | |
1505 | ||
1506 | void wxWindow::AddUpdateRect(int x, int y, int w, int h) | |
1507 | { | |
1508 | m_updateRegion.Union( x, y, w, h ); | |
1509 | } | |
1510 | ||
1511 | void wxWindow::Refresh(bool eraseBack, const wxRect *rect) | |
1512 | { | |
1513 | m_needsRefresh = true; | |
1514 | Display *display = XtDisplay((Widget) GetMainWidget()); | |
1515 | Window thisWindow = XtWindow((Widget) GetMainWidget()); | |
1516 | ||
1517 | XExposeEvent dummyEvent; | |
1518 | int width, height; | |
1519 | GetSize(&width, &height); | |
1520 | ||
1521 | dummyEvent.type = Expose; | |
1522 | dummyEvent.display = display; | |
1523 | dummyEvent.send_event = True; | |
1524 | dummyEvent.window = thisWindow; | |
1525 | if (rect) | |
1526 | { | |
1527 | dummyEvent.x = rect->x; | |
1528 | dummyEvent.y = rect->y; | |
1529 | dummyEvent.width = rect->width; | |
1530 | dummyEvent.height = rect->height; | |
1531 | } | |
1532 | else | |
1533 | { | |
1534 | dummyEvent.x = 0; | |
1535 | dummyEvent.y = 0; | |
1536 | dummyEvent.width = width; | |
1537 | dummyEvent.height = height; | |
1538 | } | |
1539 | dummyEvent.count = 0; | |
1540 | ||
1541 | if (eraseBack) | |
1542 | { | |
1543 | wxClientDC dc(this); | |
1544 | wxBrush backgroundBrush(GetBackgroundColour(), wxSOLID); | |
1545 | dc.SetBackground(backgroundBrush); | |
1546 | if (rect) | |
1547 | dc.Clear(*rect); | |
1548 | else | |
1549 | dc.Clear(); | |
1550 | } | |
1551 | ||
1552 | XSendEvent(display, thisWindow, False, ExposureMask, (XEvent *)&dummyEvent); | |
1553 | } | |
1554 | ||
1555 | void wxWindow::DoPaint() | |
1556 | { | |
1557 | //TODO : make a temporary gc so we can do the XCopyArea below | |
1558 | if (m_backingPixmap && !m_needsRefresh) | |
1559 | { | |
1560 | wxPaintDC dc(this); | |
1561 | ||
1562 | GC tempGC = (GC) dc.GetBackingGC(); | |
1563 | ||
1564 | Widget widget = (Widget) GetMainWidget(); | |
1565 | ||
1566 | int scrollPosX = 0; | |
1567 | int scrollPosY = 0; | |
1568 | ||
1569 | // We have to test whether it's a wxScrolledWindow (hack!) because | |
1570 | // otherwise we don't know how many pixels have been scrolled. We might | |
1571 | // solve this in the future by defining virtual wxWindow functions to get | |
1572 | // the scroll position in pixels. Or, each kind of scrolled window has to | |
1573 | // implement backing stores itself, using generic wxWidgets code. | |
1574 | wxScrolledWindow* scrolledWindow = wxDynamicCast(this, wxScrolledWindow); | |
1575 | if ( scrolledWindow ) | |
1576 | { | |
1577 | int x, y; | |
1578 | scrolledWindow->CalcScrolledPosition(0, 0, &x, &y); | |
1579 | ||
1580 | scrollPosX = - x; | |
1581 | scrollPosY = - y; | |
1582 | } | |
1583 | ||
1584 | // TODO: This could be optimized further by only copying the areas in the | |
1585 | // current update region. | |
1586 | ||
1587 | // Only blit the part visible in the client area. The backing pixmap | |
1588 | // always starts at 0, 0 but we may be looking at only a portion of it. | |
1589 | wxSize clientArea = GetClientSize(); | |
1590 | int toBlitX = m_pixmapWidth - scrollPosX; | |
1591 | int toBlitY = m_pixmapHeight - scrollPosY; | |
1592 | ||
1593 | // Copy whichever is samller, the amount of pixmap we have to copy, | |
1594 | // or the size of the client area. | |
1595 | toBlitX = wxMin(toBlitX, clientArea.x); | |
1596 | toBlitY = wxMin(toBlitY, clientArea.y); | |
1597 | ||
1598 | // Make sure we're not negative | |
1599 | toBlitX = wxMax(0, toBlitX); | |
1600 | toBlitY = wxMax(0, toBlitY); | |
1601 | ||
1602 | XCopyArea | |
1603 | ( | |
1604 | XtDisplay(widget), | |
1605 | (Pixmap) m_backingPixmap, | |
1606 | XtWindow (widget), | |
1607 | tempGC, | |
1608 | scrollPosX, scrollPosY, // Start at the scroll position | |
1609 | toBlitX, toBlitY, // How much of the pixmap to copy | |
1610 | 0, 0 // Destination | |
1611 | ); | |
1612 | } | |
1613 | else | |
1614 | { | |
1615 | wxWindowDC dc(this); | |
1616 | // Set an erase event first | |
1617 | wxEraseEvent eraseEvent(GetId(), &dc); | |
1618 | eraseEvent.SetEventObject(this); | |
1619 | GetEventHandler()->ProcessEvent(eraseEvent); | |
1620 | ||
1621 | wxPaintEvent event(GetId()); | |
1622 | event.SetEventObject(this); | |
1623 | GetEventHandler()->ProcessEvent(event); | |
1624 | ||
1625 | m_needsRefresh = false; | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | // ---------------------------------------------------------------------------- | |
1630 | // event handlers | |
1631 | // ---------------------------------------------------------------------------- | |
1632 | ||
1633 | // Responds to colour changes: passes event on to children. | |
1634 | void wxWindow::OnSysColourChanged(wxSysColourChangedEvent& event) | |
1635 | { | |
1636 | wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); | |
1637 | while ( node ) | |
1638 | { | |
1639 | // Only propagate to non-top-level windows | |
1640 | wxWindow *win = node->GetData(); | |
1641 | if ( win->GetParent() ) | |
1642 | { | |
1643 | wxSysColourChangedEvent event2; | |
1644 | event.SetEventObject(win); | |
1645 | win->GetEventHandler()->ProcessEvent(event2); | |
1646 | } | |
1647 | ||
1648 | node = node->GetNext(); | |
1649 | } | |
1650 | } | |
1651 | ||
1652 | void wxWindow::OnInternalIdle() | |
1653 | { | |
1654 | // This calls the UI-update mechanism (querying windows for | |
1655 | // menu/toolbar/control state information) | |
1656 | if (wxUpdateUIEvent::CanUpdate(this)) | |
1657 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
1658 | } | |
1659 | ||
1660 | // ---------------------------------------------------------------------------- | |
1661 | // accelerators | |
1662 | // ---------------------------------------------------------------------------- | |
1663 | ||
1664 | bool wxWindow::ProcessAccelerator(wxKeyEvent& event) | |
1665 | { | |
1666 | #if wxUSE_ACCEL | |
1667 | if (!m_acceleratorTable.Ok()) | |
1668 | return false; | |
1669 | ||
1670 | int count = m_acceleratorTable.GetCount(); | |
1671 | wxAcceleratorEntry* entries = m_acceleratorTable.GetEntries(); | |
1672 | int i; | |
1673 | for (i = 0; i < count; i++) | |
1674 | { | |
1675 | wxAcceleratorEntry* entry = & (entries[i]); | |
1676 | if (entry->MatchesEvent(event)) | |
1677 | { | |
1678 | // Bingo, we have a match. Now find a control that matches the | |
1679 | // entry command id. | |
1680 | ||
1681 | // Need to go up to the top of the window hierarchy, since it might | |
1682 | // be e.g. a menu item | |
1683 | wxWindow* parent = this; | |
1684 | while ( parent && !parent->IsTopLevel() ) | |
1685 | parent = parent->GetParent(); | |
1686 | ||
1687 | if (!parent) | |
1688 | return false; | |
1689 | ||
1690 | wxFrame* frame = wxDynamicCast(parent, wxFrame); | |
1691 | if ( frame ) | |
1692 | { | |
1693 | #if wxUSE_MENUS | |
1694 | // Try for a menu command | |
1695 | if (frame->GetMenuBar()) | |
1696 | { | |
1697 | wxMenuItem* item = frame->GetMenuBar()->FindItem(entry->GetCommand()); | |
1698 | if (item) | |
1699 | { | |
1700 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, entry->GetCommand()); | |
1701 | commandEvent.SetEventObject(frame); | |
1702 | ||
1703 | // If ProcessEvent returns true (it was handled), then | |
1704 | // the calling code will skip the event handling. | |
1705 | return frame->GetEventHandler()->ProcessEvent(commandEvent); | |
1706 | } | |
1707 | } | |
1708 | #endif | |
1709 | } | |
1710 | ||
1711 | // Find a child matching the command id | |
1712 | wxWindow* child = parent->FindWindow(entry->GetCommand()); | |
1713 | ||
1714 | // No such child | |
1715 | if (!child) | |
1716 | return false; | |
1717 | ||
1718 | // Now we process those kinds of windows that we can. | |
1719 | // For now, only buttons. | |
1720 | if ( wxDynamicCast(child, wxButton) ) | |
1721 | { | |
1722 | wxCommandEvent commandEvent (wxEVT_COMMAND_BUTTON_CLICKED, child->GetId()); | |
1723 | commandEvent.SetEventObject(child); | |
1724 | return child->GetEventHandler()->ProcessEvent(commandEvent); | |
1725 | } | |
1726 | ||
1727 | return false; | |
1728 | } // matches event | |
1729 | }// for | |
1730 | #endif | |
1731 | ||
1732 | // We didn't match the key event against an accelerator. | |
1733 | return false; | |
1734 | } | |
1735 | ||
1736 | // ============================================================================ | |
1737 | // Motif-specific stuff from here on | |
1738 | // ============================================================================ | |
1739 | ||
1740 | // ---------------------------------------------------------------------------- | |
1741 | // function which maintain the global hash table mapping Widgets to wxWidgets | |
1742 | // ---------------------------------------------------------------------------- | |
1743 | ||
1744 | bool wxAddWindowToTable(Widget w, wxWindow *win) | |
1745 | { | |
1746 | wxWindow *oldItem = NULL; | |
1747 | if ((oldItem = (wxWindow *)wxWidgetHashTable->Get ((long) w))) | |
1748 | { | |
1749 | wxLogDebug("Widget table clash: new widget is %ld, %s", | |
1750 | (long)w, win->GetClassInfo()->GetClassName()); | |
1751 | return false; | |
1752 | } | |
1753 | ||
1754 | wxWidgetHashTable->Put((long) w, win); | |
1755 | ||
1756 | wxLogTrace("widget", "Widget 0x%p <-> window %p (%s)", | |
1757 | (WXWidget)w, win, win->GetClassInfo()->GetClassName()); | |
1758 | ||
1759 | return true; | |
1760 | } | |
1761 | ||
1762 | wxWindow *wxGetWindowFromTable(Widget w) | |
1763 | { | |
1764 | return (wxWindow *)wxWidgetHashTable->Get((long) w); | |
1765 | } | |
1766 | ||
1767 | void wxDeleteWindowFromTable(Widget w) | |
1768 | { | |
1769 | wxLogTrace("widget", "Widget 0x%p", (WXWidget)w); | |
1770 | ||
1771 | wxWidgetHashTable->Delete((long)w); | |
1772 | } | |
1773 | ||
1774 | // ---------------------------------------------------------------------------- | |
1775 | // add/remove window from the table | |
1776 | // ---------------------------------------------------------------------------- | |
1777 | ||
1778 | // Add to hash table, add event handler | |
1779 | bool wxWindow::AttachWidget (wxWindow* WXUNUSED(parent), WXWidget mainWidget, | |
1780 | WXWidget formWidget, int x, int y, int width, int height) | |
1781 | { | |
1782 | wxAddWindowToTable((Widget) mainWidget, this); | |
1783 | XtAddEventHandler( (Widget) mainWidget, | |
1784 | ButtonPressMask | ButtonReleaseMask | |
1785 | | PointerMotionMask, | |
1786 | False, | |
1787 | wxPanelItemEventHandler, | |
1788 | (XtPointer) this); | |
1789 | ||
1790 | if (!formWidget) | |
1791 | { | |
1792 | XtTranslations ptr; | |
1793 | XtOverrideTranslations ((Widget) mainWidget, | |
1794 | ptr = XtParseTranslationTable ("<Configure>: resize()")); | |
1795 | XtFree ((char *) ptr); | |
1796 | } | |
1797 | ||
1798 | // Some widgets have a parent form widget, e.g. wxRadioBox | |
1799 | if (formWidget) | |
1800 | { | |
1801 | if (!wxAddWindowToTable((Widget) formWidget, this)) | |
1802 | return false; | |
1803 | ||
1804 | XtTranslations ptr; | |
1805 | XtOverrideTranslations ((Widget) formWidget, | |
1806 | ptr = XtParseTranslationTable ("<Configure>: resize()")); | |
1807 | XtFree ((char *) ptr); | |
1808 | } | |
1809 | ||
1810 | if (x == -1) | |
1811 | x = 0; | |
1812 | if (y == -1) | |
1813 | y = 0; | |
1814 | DoSetSize (x, y, width, height, wxSIZE_USE_EXISTING); | |
1815 | ||
1816 | return true; | |
1817 | } | |
1818 | ||
1819 | // Remove event handler, remove from hash table | |
1820 | bool wxWindow::DetachWidget(WXWidget widget) | |
1821 | { | |
1822 | XtRemoveEventHandler( (Widget) widget, | |
1823 | ButtonPressMask | ButtonReleaseMask | |
1824 | | PointerMotionMask, | |
1825 | False, | |
1826 | wxPanelItemEventHandler, | |
1827 | (XtPointer)this); | |
1828 | ||
1829 | wxDeleteWindowFromTable((Widget) widget); | |
1830 | return true; | |
1831 | } | |
1832 | ||
1833 | // ---------------------------------------------------------------------------- | |
1834 | // Motif-specific accessors | |
1835 | // ---------------------------------------------------------------------------- | |
1836 | ||
1837 | WXWindow wxWindow::GetClientXWindow() const | |
1838 | { | |
1839 | Widget wMain = (Widget)GetClientWidget(); | |
1840 | if ( wMain ) | |
1841 | return (WXWindow) XtWindow(wMain); | |
1842 | else | |
1843 | return (WXWindow) 0; | |
1844 | } | |
1845 | ||
1846 | // Get the underlying X window | |
1847 | WXWindow wxWindow::GetXWindow() const | |
1848 | { | |
1849 | Widget wMain = (Widget)GetMainWidget(); | |
1850 | if ( wMain ) | |
1851 | return (WXWindow) XtWindow(wMain); | |
1852 | else | |
1853 | return (WXWindow) 0; | |
1854 | } | |
1855 | ||
1856 | // Get the underlying X display | |
1857 | WXDisplay *wxWindow::GetXDisplay() const | |
1858 | { | |
1859 | Widget wMain = (Widget)GetMainWidget(); | |
1860 | if ( wMain ) | |
1861 | return (WXDisplay*) XtDisplay(wMain); | |
1862 | else | |
1863 | return (WXDisplay*) NULL; | |
1864 | } | |
1865 | ||
1866 | WXWidget wxWindow::GetMainWidget() const | |
1867 | { | |
1868 | if (m_drawingArea) | |
1869 | return m_drawingArea; | |
1870 | else | |
1871 | return m_mainWidget; | |
1872 | } | |
1873 | ||
1874 | WXWidget wxWindow::GetClientWidget() const | |
1875 | { | |
1876 | if (m_drawingArea != (WXWidget) 0) | |
1877 | return m_drawingArea; | |
1878 | else | |
1879 | return GetMainWidget(); | |
1880 | } | |
1881 | ||
1882 | WXWidget wxWindow::GetTopWidget() const | |
1883 | { | |
1884 | return GetMainWidget(); | |
1885 | } | |
1886 | ||
1887 | WXWidget wxWindow::GetLabelWidget() const | |
1888 | { | |
1889 | return GetMainWidget(); | |
1890 | } | |
1891 | ||
1892 | // ---------------------------------------------------------------------------- | |
1893 | // Motif callbacks | |
1894 | // ---------------------------------------------------------------------------- | |
1895 | ||
1896 | // All widgets should have this as their resize proc. | |
1897 | // OnSize sent to wxWindow via client data. | |
1898 | void wxWidgetResizeProc(Widget w, XConfigureEvent *WXUNUSED(event), | |
1899 | String WXUNUSED(args)[], int *WXUNUSED(num_args)) | |
1900 | { | |
1901 | wxWindow *win = wxGetWindowFromTable(w); | |
1902 | if (!win) | |
1903 | return; | |
1904 | ||
1905 | if (win->PreResize()) | |
1906 | { | |
1907 | int width, height; | |
1908 | win->GetSize(&width, &height); | |
1909 | wxSizeEvent sizeEvent(wxSize(width, height), win->GetId()); | |
1910 | sizeEvent.SetEventObject(win); | |
1911 | win->GetEventHandler()->ProcessEvent(sizeEvent); | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | static void wxCanvasRepaintProc(Widget drawingArea, | |
1916 | XtPointer clientData, | |
1917 | XmDrawingAreaCallbackStruct * cbs) | |
1918 | { | |
1919 | if (!wxGetWindowFromTable(drawingArea)) | |
1920 | return; | |
1921 | ||
1922 | XEvent * event = cbs->event; | |
1923 | wxWindow * win = (wxWindow *) clientData; | |
1924 | ||
1925 | switch (event->type) | |
1926 | { | |
1927 | case Expose: | |
1928 | { | |
1929 | win->AddUpdateRect(event->xexpose.x, event->xexpose.y, | |
1930 | event->xexpose.width, event->xexpose.height); | |
1931 | ||
1932 | if (event -> xexpose.count == 0) | |
1933 | { | |
1934 | win->DoPaint(); | |
1935 | } | |
1936 | break; | |
1937 | } | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | // Unable to deal with Enter/Leave without a separate EventHandler (Motif 1.1.4) | |
1942 | static void wxCanvasEnterLeave(Widget drawingArea, | |
1943 | XtPointer WXUNUSED(clientData), | |
1944 | XCrossingEvent * event) | |
1945 | { | |
1946 | XmDrawingAreaCallbackStruct cbs; | |
1947 | XEvent ev; | |
1948 | ||
1949 | ((XCrossingEvent &) ev) = *event; | |
1950 | ||
1951 | cbs.reason = XmCR_INPUT; | |
1952 | cbs.event = &ev; | |
1953 | ||
1954 | wxCanvasInputEvent(drawingArea, (XtPointer) NULL, &cbs); | |
1955 | } | |
1956 | ||
1957 | // Fix to make it work under Motif 1.0 (!) | |
1958 | static void wxCanvasMotionEvent (Widget WXUNUSED(drawingArea), | |
1959 | XButtonEvent *WXUNUSED(event)) | |
1960 | { | |
1961 | #if XmVersion <= 1000 | |
1962 | XmDrawingAreaCallbackStruct cbs; | |
1963 | XEvent ev; | |
1964 | ||
1965 | ev = *((XEvent *) event); | |
1966 | cbs.reason = XmCR_INPUT; | |
1967 | cbs.event = &ev; | |
1968 | ||
1969 | wxCanvasInputEvent (drawingArea, (XtPointer) NULL, &cbs); | |
1970 | #endif // XmVersion <= 1000 | |
1971 | } | |
1972 | ||
1973 | static void wxCanvasInputEvent(Widget drawingArea, | |
1974 | XtPointer WXUNUSED(data), | |
1975 | XmDrawingAreaCallbackStruct * cbs) | |
1976 | { | |
1977 | wxWindow *canvas = wxGetWindowFromTable(drawingArea); | |
1978 | XEvent* xevent = cbs->event; | |
1979 | ||
1980 | if (canvas==NULL) | |
1981 | return; | |
1982 | ||
1983 | if (cbs->reason != XmCR_INPUT) | |
1984 | return; | |
1985 | ||
1986 | switch (xevent->xany.type) | |
1987 | { | |
1988 | case EnterNotify: | |
1989 | case LeaveNotify: | |
1990 | case ButtonPress: | |
1991 | case ButtonRelease: | |
1992 | case MotionNotify: | |
1993 | { | |
1994 | wxMouseEvent wxevent(0); | |
1995 | if (wxTranslateMouseEvent(wxevent, canvas, drawingArea, xevent)) | |
1996 | { | |
1997 | canvas->GetEventHandler()->ProcessEvent(wxevent); | |
1998 | } | |
1999 | break; | |
2000 | } | |
2001 | case KeyPress: | |
2002 | { | |
2003 | wxKeyEvent event (wxEVT_CHAR); | |
2004 | if (wxTranslateKeyEvent (event, canvas, (Widget) 0, xevent)) | |
2005 | { | |
2006 | // Implement wxFrame::OnCharHook by checking ancestor. | |
2007 | wxWindow *parent = canvas; | |
2008 | while (parent && !parent->IsTopLevel()) | |
2009 | parent = parent->GetParent(); | |
2010 | ||
2011 | if (parent) | |
2012 | { | |
2013 | event.SetEventType(wxEVT_CHAR_HOOK); | |
2014 | if (parent->GetEventHandler()->ProcessEvent(event)) | |
2015 | return; | |
2016 | } | |
2017 | ||
2018 | // For simplicity, OnKeyDown is the same as OnChar | |
2019 | // TODO: filter modifier key presses from OnChar | |
2020 | event.SetEventType(wxEVT_KEY_DOWN); | |
2021 | ||
2022 | // Only process OnChar if OnKeyDown didn't swallow it | |
2023 | if (!canvas->GetEventHandler()->ProcessEvent (event)) | |
2024 | { | |
2025 | event.SetEventType(wxEVT_CHAR); | |
2026 | canvas->GetEventHandler()->ProcessEvent (event); | |
2027 | } | |
2028 | } | |
2029 | break; | |
2030 | } | |
2031 | case KeyRelease: | |
2032 | { | |
2033 | wxKeyEvent event (wxEVT_KEY_UP); | |
2034 | if (wxTranslateKeyEvent (event, canvas, (Widget) 0, xevent)) | |
2035 | { | |
2036 | canvas->GetEventHandler()->ProcessEvent (event); | |
2037 | } | |
2038 | break; | |
2039 | } | |
2040 | case FocusIn: | |
2041 | { | |
2042 | if (xevent->xfocus.detail != NotifyPointer) | |
2043 | { | |
2044 | wxFocusEvent event(wxEVT_SET_FOCUS, canvas->GetId()); | |
2045 | event.SetEventObject(canvas); | |
2046 | canvas->GetEventHandler()->ProcessEvent(event); | |
2047 | } | |
2048 | break; | |
2049 | } | |
2050 | case FocusOut: | |
2051 | { | |
2052 | if (xevent->xfocus.detail != NotifyPointer) | |
2053 | { | |
2054 | wxFocusEvent event(wxEVT_KILL_FOCUS, canvas->GetId()); | |
2055 | event.SetEventObject(canvas); | |
2056 | canvas->GetEventHandler()->ProcessEvent(event); | |
2057 | } | |
2058 | break; | |
2059 | } | |
2060 | default: | |
2061 | break; | |
2062 | } | |
2063 | } | |
2064 | ||
2065 | static void wxPanelItemEventHandler(Widget wid, | |
2066 | XtPointer WXUNUSED(client_data), | |
2067 | XEvent* event, | |
2068 | Boolean *continueToDispatch) | |
2069 | { | |
2070 | // Widget can be a label or the actual widget. | |
2071 | ||
2072 | wxWindow *window = wxGetWindowFromTable(wid); | |
2073 | if (window) | |
2074 | { | |
2075 | wxMouseEvent wxevent(0); | |
2076 | if (wxTranslateMouseEvent(wxevent, window, wid, event)) | |
2077 | { | |
2078 | window->GetEventHandler()->ProcessEvent(wxevent); | |
2079 | } | |
2080 | } | |
2081 | ||
2082 | // TODO: probably the key to allowing default behaviour to happen. Say we | |
2083 | // set a m_doDefault flag to false at the start of this function. Then in | |
2084 | // e.g. wxWindow::OnMouseEvent we can call Default() which sets this flag to | |
2085 | // true, indicating that default processing can happen. Thus, behaviour can | |
2086 | // appear to be overridden just by adding an event handler and not calling | |
2087 | // wxWindow::OnWhatever. ALSO, maybe we can use this instead of the current | |
2088 | // way of handling drawing area events, to simplify things. | |
2089 | *continueToDispatch = True; | |
2090 | } | |
2091 | ||
2092 | static void wxScrollBarCallback(Widget scrollbar, | |
2093 | XtPointer clientData, | |
2094 | XmScrollBarCallbackStruct *cbs) | |
2095 | { | |
2096 | wxWindow *win = wxGetWindowFromTable(scrollbar); | |
2097 | wxOrientation orientation = (wxOrientation)(int)clientData; | |
2098 | ||
2099 | wxEventType eventType = wxEVT_NULL; | |
2100 | switch (cbs->reason) | |
2101 | { | |
2102 | case XmCR_INCREMENT: | |
2103 | { | |
2104 | eventType = wxEVT_SCROLLWIN_LINEDOWN; | |
2105 | break; | |
2106 | } | |
2107 | case XmCR_DECREMENT: | |
2108 | { | |
2109 | eventType = wxEVT_SCROLLWIN_LINEUP; | |
2110 | break; | |
2111 | } | |
2112 | case XmCR_DRAG: | |
2113 | { | |
2114 | eventType = wxEVT_SCROLLWIN_THUMBTRACK; | |
2115 | break; | |
2116 | } | |
2117 | case XmCR_VALUE_CHANGED: | |
2118 | { | |
2119 | eventType = wxEVT_SCROLLWIN_THUMBRELEASE; | |
2120 | break; | |
2121 | } | |
2122 | case XmCR_PAGE_INCREMENT: | |
2123 | { | |
2124 | eventType = wxEVT_SCROLLWIN_PAGEDOWN; | |
2125 | break; | |
2126 | } | |
2127 | case XmCR_PAGE_DECREMENT: | |
2128 | { | |
2129 | eventType = wxEVT_SCROLLWIN_PAGEUP; | |
2130 | break; | |
2131 | } | |
2132 | case XmCR_TO_TOP: | |
2133 | { | |
2134 | eventType = wxEVT_SCROLLWIN_TOP; | |
2135 | break; | |
2136 | } | |
2137 | case XmCR_TO_BOTTOM: | |
2138 | { | |
2139 | eventType = wxEVT_SCROLLWIN_BOTTOM; | |
2140 | break; | |
2141 | } | |
2142 | default: | |
2143 | { | |
2144 | // Should never get here | |
2145 | wxFAIL_MSG("Unknown scroll event."); | |
2146 | break; | |
2147 | } | |
2148 | } | |
2149 | ||
2150 | wxScrollWinEvent event(eventType, | |
2151 | cbs->value, | |
2152 | orientation); | |
2153 | event.SetEventObject( win ); | |
2154 | win->GetEventHandler()->ProcessEvent(event); | |
2155 | } | |
2156 | ||
2157 | // For repainting arbitrary windows | |
2158 | void wxUniversalRepaintProc(Widget w, XtPointer WXUNUSED(c_data), XEvent *event, char *) | |
2159 | { | |
2160 | Window window; | |
2161 | Display *display; | |
2162 | ||
2163 | wxWindow* win = wxGetWindowFromTable(w); | |
2164 | if (!win) | |
2165 | return; | |
2166 | ||
2167 | switch(event -> type) | |
2168 | { | |
2169 | case Expose: | |
2170 | { | |
2171 | window = (Window) win -> GetXWindow(); | |
2172 | display = (Display *) win -> GetXDisplay(); | |
2173 | ||
2174 | win->AddUpdateRect(event->xexpose.x, event->xexpose.y, | |
2175 | event->xexpose.width, event->xexpose.height); | |
2176 | ||
2177 | if (event -> xexpose.count == 0) | |
2178 | { | |
2179 | win->DoPaint(); | |
2180 | } | |
2181 | ||
2182 | break; | |
2183 | } | |
2184 | } | |
2185 | } | |
2186 | ||
2187 | // ---------------------------------------------------------------------------- | |
2188 | // TranslateXXXEvent() functions | |
2189 | // ---------------------------------------------------------------------------- | |
2190 | ||
2191 | bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, | |
2192 | Widget widget, const XEvent *xevent) | |
2193 | { | |
2194 | switch (xevent->xany.type) | |
2195 | { | |
2196 | case EnterNotify: | |
2197 | case LeaveNotify: | |
2198 | #if 0 | |
2199 | fprintf(stderr, "Widget 0x%p <-> window %p (%s), %s\n", | |
2200 | (WXWidget)widget, win, win->GetClassInfo()->GetClassName(), | |
2201 | (xevent->xany.type == EnterNotify ? "ENTER" : "LEAVE")); | |
2202 | #endif | |
2203 | case ButtonPress: | |
2204 | case ButtonRelease: | |
2205 | case MotionNotify: | |
2206 | { | |
2207 | int eventx = xevent->xbutton.x, eventy = xevent->xbutton.y; | |
2208 | ||
2209 | wxEventType eventType = wxEVT_NULL; | |
2210 | ||
2211 | if (xevent->xany.type == LeaveNotify) | |
2212 | { | |
2213 | eventType = wxEVT_LEAVE_WINDOW; | |
2214 | } | |
2215 | if (xevent->xany.type == EnterNotify) | |
2216 | { | |
2217 | eventType = wxEVT_ENTER_WINDOW; | |
2218 | } | |
2219 | else if (xevent->xany.type == MotionNotify) | |
2220 | { | |
2221 | eventType = wxEVT_MOTION; | |
2222 | ||
2223 | if (xevent->xmotion.is_hint == NotifyHint) | |
2224 | { | |
2225 | Window root, child; | |
2226 | int x_root, y_root; | |
2227 | unsigned int state; | |
2228 | Display *dpy = XtDisplay (widget); | |
2229 | ||
2230 | XQueryPointer (dpy, XtWindow (widget), | |
2231 | &root, &child, | |
2232 | &x_root, &y_root, &eventx, &eventy, &state); | |
2233 | } | |
2234 | } | |
2235 | else if (xevent->xany.type == ButtonPress) | |
2236 | { | |
2237 | wxevent.SetTimestamp(xevent->xbutton.time); | |
2238 | int button = 0; | |
2239 | if (xevent->xbutton.button == Button1) | |
2240 | { | |
2241 | eventType = wxEVT_LEFT_DOWN; | |
2242 | button = 1; | |
2243 | } | |
2244 | else if (xevent->xbutton.button == Button2) | |
2245 | { | |
2246 | eventType = wxEVT_MIDDLE_DOWN; | |
2247 | button = 2; | |
2248 | } | |
2249 | else if (xevent->xbutton.button == Button3) | |
2250 | { | |
2251 | eventType = wxEVT_RIGHT_DOWN; | |
2252 | button = 3; | |
2253 | } | |
2254 | ||
2255 | // check for a double click | |
2256 | // | |
2257 | long dclickTime = XtGetMultiClickTime(xevent->xany.display); | |
2258 | long ts = wxevent.GetTimestamp(); | |
2259 | ||
2260 | int buttonLast = win->GetLastClickedButton(); | |
2261 | long lastTS = win->GetLastClickTime(); | |
2262 | if ( buttonLast && buttonLast == button && | |
2263 | (ts - lastTS) < dclickTime ) | |
2264 | { | |
2265 | // I have a dclick | |
2266 | win->SetLastClick(0, ts); | |
2267 | if ( eventType == wxEVT_LEFT_DOWN ) | |
2268 | eventType = wxEVT_LEFT_DCLICK; | |
2269 | else if ( eventType == wxEVT_MIDDLE_DOWN ) | |
2270 | eventType = wxEVT_MIDDLE_DCLICK; | |
2271 | else if ( eventType == wxEVT_RIGHT_DOWN ) | |
2272 | eventType = wxEVT_RIGHT_DCLICK; | |
2273 | } | |
2274 | else | |
2275 | { | |
2276 | // not fast enough or different button | |
2277 | win->SetLastClick(button, ts); | |
2278 | } | |
2279 | } | |
2280 | else if (xevent->xany.type == ButtonRelease) | |
2281 | { | |
2282 | if (xevent->xbutton.button == Button1) | |
2283 | { | |
2284 | eventType = wxEVT_LEFT_UP; | |
2285 | } | |
2286 | else if (xevent->xbutton.button == Button2) | |
2287 | { | |
2288 | eventType = wxEVT_MIDDLE_UP; | |
2289 | } | |
2290 | else if (xevent->xbutton.button == Button3) | |
2291 | { | |
2292 | eventType = wxEVT_RIGHT_UP; | |
2293 | } | |
2294 | else | |
2295 | return false; | |
2296 | } | |
2297 | else | |
2298 | { | |
2299 | return false; | |
2300 | } | |
2301 | ||
2302 | wxevent.SetEventType(eventType); | |
2303 | ||
2304 | Position x1, y1; | |
2305 | XtVaGetValues(widget, XmNx, &x1, XmNy, &y1, NULL); | |
2306 | ||
2307 | int x2, y2; | |
2308 | win->GetPosition(&x2, &y2); | |
2309 | ||
2310 | // The button x/y must be translated to wxWidgets | |
2311 | // window space - the widget might be a label or button, | |
2312 | // within a form. | |
2313 | int dx = 0; | |
2314 | int dy = 0; | |
2315 | if (widget != (Widget)win->GetMainWidget()) | |
2316 | { | |
2317 | dx = x1; | |
2318 | dy = y1; | |
2319 | } | |
2320 | ||
2321 | wxevent.m_x = eventx + dx; | |
2322 | wxevent.m_y = eventy + dy; | |
2323 | ||
2324 | wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN) | |
2325 | || (event_left_is_down (xevent) | |
2326 | && (eventType != wxEVT_LEFT_UP))); | |
2327 | wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN) | |
2328 | || (event_middle_is_down (xevent) | |
2329 | && (eventType != wxEVT_MIDDLE_UP))); | |
2330 | wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN) | |
2331 | || (event_right_is_down (xevent) | |
2332 | && (eventType != wxEVT_RIGHT_UP))); | |
2333 | ||
2334 | wxevent.m_shiftDown = xevent->xbutton.state & ShiftMask; | |
2335 | wxevent.m_controlDown = xevent->xbutton.state & ControlMask; | |
2336 | wxevent.m_altDown = xevent->xbutton.state & Mod3Mask; | |
2337 | wxevent.m_metaDown = xevent->xbutton.state & Mod1Mask; | |
2338 | ||
2339 | wxevent.SetId(win->GetId()); | |
2340 | wxevent.SetEventObject(win); | |
2341 | ||
2342 | return true; | |
2343 | } | |
2344 | } | |
2345 | return false; | |
2346 | } | |
2347 | ||
2348 | bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, | |
2349 | Widget WXUNUSED(widget), const XEvent *xevent) | |
2350 | { | |
2351 | switch (xevent->xany.type) | |
2352 | { | |
2353 | case KeyPress: | |
2354 | case KeyRelease: | |
2355 | { | |
2356 | char buf[20]; | |
2357 | ||
2358 | KeySym keySym; | |
2359 | (void) XLookupString((XKeyEvent *)xevent, buf, 20, &keySym, NULL); | |
2360 | int id = wxCharCodeXToWX (keySym); | |
2361 | // id may be WXK_xxx code - these are outside ASCII range, so we | |
2362 | // can't just use toupper() on id | |
2363 | if (id >= 'a' && id <= 'z') | |
2364 | id = toupper(id); | |
2365 | ||
2366 | if (xevent->xkey.state & ShiftMask) | |
2367 | wxevent.m_shiftDown = true; | |
2368 | if (xevent->xkey.state & ControlMask) | |
2369 | wxevent.m_controlDown = true; | |
2370 | if (xevent->xkey.state & Mod3Mask) | |
2371 | wxevent.m_altDown = true; | |
2372 | if (xevent->xkey.state & Mod1Mask) | |
2373 | wxevent.m_metaDown = true; | |
2374 | wxevent.SetEventObject(win); | |
2375 | wxevent.m_keyCode = id; | |
2376 | wxevent.SetTimestamp(xevent->xkey.time); | |
2377 | ||
2378 | wxevent.m_x = xevent->xbutton.x; | |
2379 | wxevent.m_y = xevent->xbutton.y; | |
2380 | ||
2381 | if (id > -1) | |
2382 | return true; | |
2383 | else | |
2384 | return false; | |
2385 | break; | |
2386 | } | |
2387 | default: | |
2388 | break; | |
2389 | } | |
2390 | return false; | |
2391 | } | |
2392 | ||
2393 | // ---------------------------------------------------------------------------- | |
2394 | // Colour stuff | |
2395 | // ---------------------------------------------------------------------------- | |
2396 | ||
2397 | #define YAllocColor XAllocColor | |
2398 | XColor g_itemColors[5]; | |
2399 | int wxComputeColours (Display *display, wxColour * back, wxColour * fore) | |
2400 | { | |
2401 | int result; | |
2402 | static XmColorProc colorProc; | |
2403 | ||
2404 | result = wxNO_COLORS; | |
2405 | ||
2406 | if (back) | |
2407 | { | |
2408 | g_itemColors[0].red = (((long) back->Red ()) << 8); | |
2409 | g_itemColors[0].green = (((long) back->Green ()) << 8); | |
2410 | g_itemColors[0].blue = (((long) back->Blue ()) << 8); | |
2411 | g_itemColors[0].flags = DoRed | DoGreen | DoBlue; | |
2412 | if (colorProc == (XmColorProc) NULL) | |
2413 | { | |
2414 | // Get a ptr to the actual function | |
2415 | colorProc = XmSetColorCalculation ((XmColorProc) NULL); | |
2416 | // And set it back to motif. | |
2417 | XmSetColorCalculation (colorProc); | |
2418 | } | |
2419 | (*colorProc) (&g_itemColors[wxBACK_INDEX], | |
2420 | &g_itemColors[wxFORE_INDEX], | |
2421 | &g_itemColors[wxSELE_INDEX], | |
2422 | &g_itemColors[wxTOPS_INDEX], | |
2423 | &g_itemColors[wxBOTS_INDEX]); | |
2424 | result = wxBACK_COLORS; | |
2425 | } | |
2426 | if (fore) | |
2427 | { | |
2428 | g_itemColors[wxFORE_INDEX].red = (((long) fore->Red ()) << 8); | |
2429 | g_itemColors[wxFORE_INDEX].green = (((long) fore->Green ()) << 8); | |
2430 | g_itemColors[wxFORE_INDEX].blue = (((long) fore->Blue ()) << 8); | |
2431 | g_itemColors[wxFORE_INDEX].flags = DoRed | DoGreen | DoBlue; | |
2432 | if (result == wxNO_COLORS) | |
2433 | result = wxFORE_COLORS; | |
2434 | } | |
2435 | ||
2436 | Display *dpy = display; | |
2437 | Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy); | |
2438 | ||
2439 | if (back) | |
2440 | { | |
2441 | /* 5 Colours to allocate */ | |
2442 | for (int i = 0; i < 5; i++) | |
2443 | if (!YAllocColor (dpy, cmap, &g_itemColors[i])) | |
2444 | result = wxNO_COLORS; | |
2445 | } | |
2446 | else if (fore) | |
2447 | { | |
2448 | /* Only 1 colour to allocate */ | |
2449 | if (!YAllocColor (dpy, cmap, &g_itemColors[wxFORE_INDEX])) | |
2450 | result = wxNO_COLORS; | |
2451 | } | |
2452 | ||
2453 | return result; | |
2454 | } | |
2455 | ||
2456 | // Changes the foreground and background colours to be derived from the current | |
2457 | // background colour. To change the foreground colour, you must call | |
2458 | // SetForegroundColour explicitly. | |
2459 | void wxWindow::ChangeBackgroundColour() | |
2460 | { | |
2461 | WXWidget mainWidget = GetMainWidget(); | |
2462 | if ( mainWidget ) | |
2463 | wxDoChangeBackgroundColour(mainWidget, m_backgroundColour); | |
2464 | if ( m_scrolledWindow && mainWidget != m_scrolledWindow ) | |
2465 | wxDoChangeForegroundColour(m_scrolledWindow, m_backgroundColour); | |
2466 | } | |
2467 | ||
2468 | void wxWindow::ChangeForegroundColour() | |
2469 | { | |
2470 | WXWidget mainWidget = GetMainWidget(); | |
2471 | if ( mainWidget ) | |
2472 | wxDoChangeForegroundColour(mainWidget, m_foregroundColour); | |
2473 | if ( m_scrolledWindow && mainWidget != m_scrolledWindow ) | |
2474 | wxDoChangeForegroundColour(m_scrolledWindow, m_foregroundColour); | |
2475 | } | |
2476 | ||
2477 | bool wxWindow::SetBackgroundColour(const wxColour& col) | |
2478 | { | |
2479 | if ( !wxWindowBase::SetBackgroundColour(col) ) | |
2480 | return false; | |
2481 | ||
2482 | ChangeBackgroundColour(); | |
2483 | ||
2484 | return true; | |
2485 | } | |
2486 | ||
2487 | bool wxWindow::SetForegroundColour(const wxColour& col) | |
2488 | { | |
2489 | if ( !wxWindowBase::SetForegroundColour(col) ) | |
2490 | return false; | |
2491 | ||
2492 | ChangeForegroundColour(); | |
2493 | ||
2494 | return true; | |
2495 | } | |
2496 | ||
2497 | void wxWindow::ChangeFont(bool keepOriginalSize) | |
2498 | { | |
2499 | // Note that this causes the widget to be resized back | |
2500 | // to its original size! We therefore have to set the size | |
2501 | // back again. TODO: a better way in Motif? | |
2502 | Widget w = (Widget) GetLabelWidget(); // Usually the main widget | |
2503 | if (w && m_font.Ok()) | |
2504 | { | |
2505 | int width, height, width1, height1; | |
2506 | GetSize(& width, & height); | |
2507 | ||
2508 | wxDoChangeFont( GetLabelWidget(), m_font ); | |
2509 | ||
2510 | GetSize(& width1, & height1); | |
2511 | if (keepOriginalSize && (width != width1 || height != height1)) | |
2512 | { | |
2513 | SetSize(-1, -1, width, height); | |
2514 | } | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | // ---------------------------------------------------------------------------- | |
2519 | // global functions | |
2520 | // ---------------------------------------------------------------------------- | |
2521 | ||
2522 | wxWindow *wxGetActiveWindow() | |
2523 | { | |
2524 | // TODO | |
2525 | wxFAIL_MSG("Not implemented"); | |
2526 | return NULL; | |
2527 | } | |
2528 | ||
2529 | /* static */ | |
2530 | wxWindow *wxWindowBase::GetCapture() | |
2531 | { | |
2532 | return (wxWindow *)g_captureWindow; | |
2533 | } | |
2534 | ||
2535 | ||
2536 | // Find the wxWindow at the current mouse position, returning the mouse | |
2537 | // position. | |
2538 | wxWindow* wxFindWindowAtPointer(wxPoint& pt) | |
2539 | { | |
2540 | return wxFindWindowAtPoint(wxGetMousePosition()); | |
2541 | } | |
2542 | ||
2543 | // Get the current mouse position. | |
2544 | wxPoint wxGetMousePosition() | |
2545 | { | |
2546 | Display *display = wxGlobalDisplay(); | |
2547 | Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display)); | |
2548 | Window rootReturn, childReturn; | |
2549 | int rootX, rootY, winX, winY; | |
2550 | unsigned int maskReturn; | |
2551 | ||
2552 | XQueryPointer (display, | |
2553 | rootWindow, | |
2554 | &rootReturn, | |
2555 | &childReturn, | |
2556 | &rootX, &rootY, &winX, &winY, &maskReturn); | |
2557 | return wxPoint(rootX, rootY); | |
2558 | } | |
2559 | ||
2560 | ||
2561 | // ---------------------------------------------------------------------------- | |
2562 | // wxNoOptimize: switch off size optimization | |
2563 | // ---------------------------------------------------------------------------- | |
2564 | ||
2565 | int wxNoOptimize::ms_count = 0; | |
2566 |