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