Applied patch [ 1197471 ] Scrolling of Widgets within a wxScrollWindow enabled
[wxWidgets.git] / src / x11 / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: windows.cpp
3 // Purpose: wxWindow
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
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 #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"
38 #include "wx/scrolwin.h"
39 #include "wx/scrolbar.h"
40 #include "wx/module.h"
41 #include "wx/menuitem.h"
42 #include "wx/log.h"
43 #include "wx/fontutil.h"
44 #include "wx/univ/renderer.h"
45 #include "wx/hash.h"
46
47 #if wxUSE_DRAG_AND_DROP
48 #include "wx/dnd.h"
49 #endif
50
51 #include "wx/x11/private.h"
52 #include "X11/Xutil.h"
53
54 #if wxUSE_NANOX
55 // For wxGetLocalTime, used by XButtonEventGetTime
56 #include "wx/timer.h"
57 #endif
58
59 #include <string.h>
60
61 // ----------------------------------------------------------------------------
62 // global variables for this module
63 // ----------------------------------------------------------------------------
64
65 static wxWindow* g_captureWindow = NULL;
66 static GC g_eraseGC;
67
68 // ----------------------------------------------------------------------------
69 // macros
70 // ----------------------------------------------------------------------------
71
72 #define event_left_is_down(x) ((x)->xbutton.state & Button1Mask)
73 #define event_middle_is_down(x) ((x)->xbutton.state & Button2Mask)
74 #define event_right_is_down(x) ((x)->xbutton.state & Button3Mask)
75
76 // ----------------------------------------------------------------------------
77 // event tables
78 // ----------------------------------------------------------------------------
79
80 IMPLEMENT_ABSTRACT_CLASS(wxWindowX11, wxWindowBase)
81
82 BEGIN_EVENT_TABLE(wxWindowX11, wxWindowBase)
83 EVT_SYS_COLOUR_CHANGED(wxWindowX11::OnSysColourChanged)
84 END_EVENT_TABLE()
85
86 // ============================================================================
87 // implementation
88 // ============================================================================
89
90 // ----------------------------------------------------------------------------
91 // helper functions
92 // ----------------------------------------------------------------------------
93
94 // ----------------------------------------------------------------------------
95 // constructors
96 // ----------------------------------------------------------------------------
97
98 void wxWindowX11::Init()
99 {
100 // X11-specific
101 m_mainWindow = (WXWindow) 0;
102 m_clientWindow = (WXWindow) 0;
103 m_insertIntoMain = FALSE;
104 m_updateNcArea = FALSE;
105
106 m_winCaptured = FALSE;
107 m_needsInputFocus = FALSE;
108 m_isShown = TRUE;
109 m_lastTS = 0;
110 m_lastButton = 0;
111 }
112
113 // real construction (Init() must have been called before!)
114 bool wxWindowX11::Create(wxWindow *parent, wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119 {
120 wxCHECK_MSG( parent, FALSE, wxT("can't create wxWindow without parent") );
121
122 CreateBase(parent, id, pos, size, style, wxDefaultValidator, name);
123
124 parent->AddChild(this);
125
126 Display *xdisplay = (Display*) wxGlobalDisplay();
127 int xscreen = DefaultScreen( xdisplay );
128 Visual *xvisual = DefaultVisual( xdisplay, xscreen );
129 Colormap cm = DefaultColormap( xdisplay, xscreen );
130
131 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
132 m_backgroundColour.CalcPixel( (WXColormap) cm );
133
134 m_foregroundColour = *wxBLACK;
135 m_foregroundColour.CalcPixel( (WXColormap) cm );
136
137 Window xparent = (Window) parent->GetClientAreaWindow();
138
139 // Add window's own scrollbars to main window, not to client window
140 if (parent->GetInsertIntoMain())
141 {
142 // wxLogDebug( "Inserted into main: %s", GetName().c_str() );
143 xparent = (Window) parent->GetMainWindow();
144 }
145
146 // Size (not including the border) must be nonzero (or a Value error results)!
147 // Note: The Xlib manual doesn't mention this restriction of XCreateWindow.
148 wxSize size2(size);
149 if (size2.x <= 0)
150 size2.x = 20;
151 if (size2.y <= 0)
152 size2.y = 20;
153
154 wxPoint pos2(pos);
155 if (pos2.x == -1)
156 pos2.x = 0;
157 if (pos2.y == -1)
158 pos2.y = 0;
159
160 #if wxUSE_TWO_WINDOWS
161 bool need_two_windows =
162 ((( wxSUNKEN_BORDER | wxRAISED_BORDER | wxSIMPLE_BORDER | wxHSCROLL | wxVSCROLL ) & m_windowStyle) != 0);
163 #else
164 bool need_two_windows = FALSE;
165 #endif
166
167 #if wxUSE_NANOX
168 long xattributes = 0;
169 #else
170 XSetWindowAttributes xattributes;
171 long xattributes_mask = 0;
172
173 xattributes_mask |= CWBackPixel;
174 xattributes.background_pixel = m_backgroundColour.GetPixel();
175
176 xattributes_mask |= CWBorderPixel;
177 xattributes.border_pixel = BlackPixel( xdisplay, xscreen );
178
179 xattributes_mask |= CWEventMask;
180 #endif
181
182 if (need_two_windows)
183 {
184 #if wxUSE_NANOX
185 long backColor, foreColor;
186 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
187 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
188
189 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
190 0, 0, InputOutput, xvisual, backColor, foreColor);
191 XSelectInput( xdisplay, xwindow,
192 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
193 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
194 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
195 PropertyChangeMask );
196
197 #else
198 // Normal X11
199 xattributes.event_mask =
200 ExposureMask | StructureNotifyMask | ColormapChangeMask;
201
202 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
203 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
204
205 #endif
206
207 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
208
209 m_mainWindow = (WXWindow) xwindow;
210 wxAddWindowToTable( xwindow, (wxWindow*) this );
211
212 XMapWindow( xdisplay, xwindow );
213
214 #if !wxUSE_NANOX
215 xattributes.event_mask =
216 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
217 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
218 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
219 PropertyChangeMask | VisibilityChangeMask ;
220
221 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE ))
222 {
223 xattributes_mask |= CWBitGravity;
224 xattributes.bit_gravity = StaticGravity;
225 }
226 #endif
227
228 if (HasFlag( wxSUNKEN_BORDER) || HasFlag( wxRAISED_BORDER))
229 {
230 pos2.x = 2;
231 pos2.y = 2;
232 size2.x -= 4;
233 size2.y -= 4;
234 }
235 else if (HasFlag( wxSIMPLE_BORDER ))
236 {
237 pos2.x = 1;
238 pos2.y = 1;
239 size2.x -= 2;
240 size2.y -= 2;
241 }
242 else
243 {
244 pos2.x = 0;
245 pos2.y = 0;
246 }
247
248 // Make again sure the size is nonzero.
249 if (size2.x <= 0)
250 size2.x = 1;
251 if (size2.y <= 0)
252 size2.y = 1;
253
254 #if wxUSE_NANOX
255 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
256 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
257
258 xwindow = XCreateWindowWithColor( xdisplay, xwindow, pos2.x, pos2.y, size2.x, size2.y,
259 0, 0, InputOutput, xvisual, backColor, foreColor);
260 XSelectInput( xdisplay, xwindow,
261 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
262 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
263 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
264 PropertyChangeMask );
265
266 #else
267 xwindow = XCreateWindow( xdisplay, xwindow, pos2.x, pos2.y, size2.x, size2.y,
268 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
269 #endif
270
271 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
272
273 m_clientWindow = (WXWindow) xwindow;
274 wxAddClientWindowToTable( xwindow, (wxWindow*) this );
275
276 XMapWindow( xdisplay, xwindow );
277 }
278 else
279 {
280 // wxLogDebug( "No two windows needed %s", GetName().c_str() );
281 #if wxUSE_NANOX
282 long backColor, foreColor;
283 backColor = GR_RGB(m_backgroundColour.Red(), m_backgroundColour.Green(), m_backgroundColour.Blue());
284 foreColor = GR_RGB(m_foregroundColour.Red(), m_foregroundColour.Green(), m_foregroundColour.Blue());
285
286 Window xwindow = XCreateWindowWithColor( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
287 0, 0, InputOutput, xvisual, backColor, foreColor);
288 XSelectInput( xdisplay, xwindow,
289 GR_EVENT_MASK_CLOSE_REQ | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
290 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
291 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
292 PropertyChangeMask );
293
294 #else
295 xattributes.event_mask =
296 ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
297 ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask |
298 KeymapStateMask | FocusChangeMask | ColormapChangeMask | StructureNotifyMask |
299 PropertyChangeMask | VisibilityChangeMask ;
300
301 if (!HasFlag( wxFULL_REPAINT_ON_RESIZE ))
302 {
303 xattributes_mask |= CWBitGravity;
304 xattributes.bit_gravity = NorthWestGravity;
305 }
306
307 Window xwindow = XCreateWindow( xdisplay, xparent, pos2.x, pos2.y, size2.x, size2.y,
308 0, DefaultDepth(xdisplay,xscreen), InputOutput, xvisual, xattributes_mask, &xattributes );
309 #endif
310
311 XSetWindowBackgroundPixmap( xdisplay, xwindow, None );
312
313 m_mainWindow = (WXWindow) xwindow;
314 m_clientWindow = m_mainWindow;
315 wxAddWindowToTable( xwindow, (wxWindow*) this );
316
317 XMapWindow( xdisplay, xwindow );
318 }
319
320 // Is a subwindow, so map immediately
321 m_isShown = TRUE;
322
323 // Without this, the cursor may not be restored properly (e.g. in splitter
324 // sample).
325 SetCursor(*wxSTANDARD_CURSOR);
326 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
327
328 // Don't call this, it can have nasty repercussions for composite controls,
329 // for example
330 // SetSize(pos.x, pos.y, size.x, size.y);
331
332 return TRUE;
333 }
334
335 // Destructor
336 wxWindowX11::~wxWindowX11()
337 {
338 SendDestroyEvent();
339
340 if (g_captureWindow == this)
341 g_captureWindow = NULL;
342
343 m_isBeingDeleted = TRUE;
344
345 DestroyChildren();
346
347 if (m_clientWindow != m_mainWindow)
348 {
349 // Destroy the cleint window
350 Window xwindow = (Window) m_clientWindow;
351 wxDeleteClientWindowFromTable( xwindow );
352 XDestroyWindow( wxGlobalDisplay(), xwindow );
353 m_clientWindow = NULL;
354 }
355
356 // Destroy the window
357 Window xwindow = (Window) m_mainWindow;
358 wxDeleteWindowFromTable( xwindow );
359 XDestroyWindow( wxGlobalDisplay(), xwindow );
360 m_mainWindow = NULL;
361 }
362
363 // ---------------------------------------------------------------------------
364 // basic operations
365 // ---------------------------------------------------------------------------
366
367 void wxWindowX11::SetFocus()
368 {
369 Window xwindow = (Window) m_clientWindow;
370
371 wxCHECK_RET( xwindow, wxT("invalid window") );
372
373 wxCHECK_RET( AcceptsFocus(), wxT("set focus on window that doesn't accept the focus") );
374
375 #if 0
376 if (GetName() == "scrollBar")
377 {
378 char *crash = NULL;
379 *crash = 0;
380 }
381 #endif
382
383 if (wxWindowIsVisible(xwindow))
384 {
385 wxLogTrace( _T("focus"), _T("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
386 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
387 XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToNone, CurrentTime );
388 m_needsInputFocus = FALSE;
389 }
390 else
391 {
392 m_needsInputFocus = TRUE;
393 }
394 }
395
396 // Get the window with the focus
397 wxWindow *wxWindowBase::DoFindFocus()
398 {
399 Window xfocus = (Window) 0;
400 int revert = 0;
401
402 XGetInputFocus( wxGlobalDisplay(), &xfocus, &revert);
403 if (xfocus)
404 {
405 wxWindow *win = wxGetWindowFromTable( xfocus );
406 if (!win)
407 {
408 win = wxGetClientWindowFromTable( xfocus );
409 }
410
411 return win;
412 }
413
414 return NULL;
415 }
416
417 // Enabling/disabling handled by event loop, and not sending events
418 // if disabled.
419 bool wxWindowX11::Enable(bool enable)
420 {
421 if ( !wxWindowBase::Enable(enable) )
422 return FALSE;
423
424 return TRUE;
425 }
426
427 bool wxWindowX11::Show(bool show)
428 {
429 wxWindowBase::Show(show);
430
431 Window xwindow = (Window) m_mainWindow;
432 Display *xdisp = wxGlobalDisplay();
433 if (show)
434 {
435 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
436 XMapWindow(xdisp, xwindow);
437 }
438 else
439 {
440 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
441 XUnmapWindow(xdisp, xwindow);
442 }
443
444 return TRUE;
445 }
446
447 // Raise the window to the top of the Z order
448 void wxWindowX11::Raise()
449 {
450 if (m_mainWindow)
451 XRaiseWindow( wxGlobalDisplay(), (Window) m_mainWindow );
452 }
453
454 // Lower the window to the bottom of the Z order
455 void wxWindowX11::Lower()
456 {
457 if (m_mainWindow)
458 XLowerWindow( wxGlobalDisplay(), (Window) m_mainWindow );
459 }
460
461 void wxWindowX11::DoCaptureMouse()
462 {
463 if ((g_captureWindow != NULL) && (g_captureWindow != this))
464 {
465 wxASSERT_MSG(FALSE, wxT("Trying to capture before mouse released."));
466
467 // Core dump now
468 int *tmp = NULL;
469 (*tmp) = 1;
470 return;
471 }
472
473 if (m_winCaptured)
474 return;
475
476 Window xwindow = (Window) m_clientWindow;
477
478 wxCHECK_RET( xwindow, wxT("invalid window") );
479
480 g_captureWindow = (wxWindow*) this;
481
482 if (xwindow)
483 {
484 int res = XGrabPointer(wxGlobalDisplay(), xwindow,
485 FALSE,
486 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
487 GrabModeAsync,
488 GrabModeAsync,
489 None,
490 None, /* cursor */ // TODO: This may need to be set to the cursor of this window
491 CurrentTime );
492
493 if (res != GrabSuccess)
494 {
495 wxString msg;
496 msg.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
497 wxLogDebug(msg);
498 if (res == GrabNotViewable)
499 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
500
501 g_captureWindow = NULL;
502 return;
503 }
504
505 m_winCaptured = TRUE;
506 }
507 }
508
509 void wxWindowX11::DoReleaseMouse()
510 {
511 g_captureWindow = NULL;
512
513 if ( !m_winCaptured )
514 return;
515
516 Window xwindow = (Window) m_clientWindow;
517
518 if (xwindow)
519 {
520 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
521 }
522
523 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
524
525 m_winCaptured = FALSE;
526 }
527
528 bool wxWindowX11::SetFont(const wxFont& font)
529 {
530 if ( !wxWindowBase::SetFont(font) )
531 {
532 // nothing to do
533 return FALSE;
534 }
535
536 return TRUE;
537 }
538
539 bool wxWindowX11::SetCursor(const wxCursor& cursor)
540 {
541 if ( !wxWindowBase::SetCursor(cursor) )
542 {
543 // no change
544 return FALSE;
545 }
546
547 Window xwindow = (Window) m_clientWindow;
548
549 wxCHECK_MSG( xwindow, FALSE, wxT("invalid window") );
550
551 wxCursor cursorToUse;
552 if (m_cursor.Ok())
553 cursorToUse = m_cursor;
554 else
555 cursorToUse = *wxSTANDARD_CURSOR;
556
557 Cursor xcursor = (Cursor) cursorToUse.GetCursor();
558
559 XDefineCursor( wxGlobalDisplay(), xwindow, xcursor );
560
561 return TRUE;
562 }
563
564 // Coordinates relative to the window
565 void wxWindowX11::WarpPointer (int x, int y)
566 {
567 Window xwindow = (Window) m_clientWindow;
568
569 wxCHECK_RET( xwindow, wxT("invalid window") );
570
571 XWarpPointer( wxGlobalDisplay(), None, xwindow, 0, 0, 0, 0, x, y);
572 }
573
574 // Does a physical scroll
575 void wxWindowX11::ScrollWindow(int dx, int dy, const wxRect *rect)
576 {
577 // No scrolling requested.
578 if ((dx == 0) && (dy == 0)) return;
579
580 if (!m_updateRegion.IsEmpty())
581 {
582 m_updateRegion.Offset( dx, dy );
583
584 int cw = 0;
585 int ch = 0;
586 GetSize( &cw, &ch ); // GetClientSize() ??
587 m_updateRegion.Intersect( 0, 0, cw, ch );
588 }
589
590 if (!m_clearRegion.IsEmpty())
591 {
592 m_clearRegion.Offset( dx, dy );
593
594 int cw = 0;
595 int ch = 0;
596 GetSize( &cw, &ch ); // GetClientSize() ??
597 m_clearRegion.Intersect( 0, 0, cw, ch );
598 }
599
600 Window xwindow = (Window) GetClientAreaWindow();
601
602 wxCHECK_RET( xwindow, wxT("invalid window") );
603
604 Display *xdisplay = wxGlobalDisplay();
605
606 GC xgc = XCreateGC( xdisplay, xwindow, 0, NULL );
607 XSetGraphicsExposures( xdisplay, xgc, True );
608
609 int s_x = 0;
610 int s_y = 0;
611 int cw;
612 int ch;
613 if (rect)
614 {
615 s_x = rect->x;
616 s_y = rect->y;
617
618 cw = rect->width;
619 ch = rect->height;
620 }
621 else
622 {
623 s_x = 0;
624 s_y = 0;
625 GetClientSize( &cw, &ch );
626 }
627
628 #if wxUSE_TWO_WINDOWS
629 wxPoint offset( 0,0 );
630 #else
631 wxPoint offset = GetClientAreaOrigin();
632 s_x += offset.x;
633 s_y += offset.y;
634 #endif
635
636 int w = cw - abs(dx);
637 int h = ch - abs(dy);
638
639 if ((h < 0) || (w < 0))
640 {
641 Refresh();
642 }
643 else
644 {
645 wxRect rect;
646 if (dx < 0) rect.x = cw+dx + offset.x; else rect.x = s_x;
647 if (dy < 0) rect.y = ch+dy + offset.y; else rect.y = s_y;
648 if (dy != 0) rect.width = cw; else rect.width = abs(dx);
649 if (dx != 0) rect.height = ch; else rect.height = abs(dy);
650
651 int d_x = s_x;
652 int d_y = s_y;
653
654 if (dx < 0) s_x += -dx;
655 if (dy < 0) s_y += -dy;
656 if (dx > 0) d_x = dx + offset.x;
657 if (dy > 0) d_y = dy + offset.y;
658
659 XCopyArea( xdisplay, xwindow, xwindow, xgc, s_x, s_y, w, h, d_x, d_y );
660
661 // wxLogDebug( "Copy: s_x %d s_y %d w %d h %d d_x %d d_y %d", s_x, s_y, w, h, d_x, d_y );
662
663 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
664
665 m_updateRegion.Union( rect );
666 m_clearRegion.Union( rect );
667 }
668
669 XFreeGC( xdisplay, xgc );
670
671 // Move Clients, but not the scrollbars
672 // FIXME: There may be a better method to move a lot of Windows within X11
673 wxScrollBar *sbH = ((wxWindow *) this)->GetScrollbar( wxHORIZONTAL );
674 wxScrollBar *sbV = ((wxWindow *) this)->GetScrollbar( wxVERTICAL );
675 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
676 while ( node )
677 {
678 // Only propagate to non-top-level windows
679 wxWindow *win = node->GetData();
680 if ( win->GetParent() && win != sbH && win != sbV )
681 {
682 wxPoint pos = win->GetPosition();
683 // Add the delta to the old Position
684 pos.x += dx;
685 pos.y += dy;
686 win->SetPosition(pos);
687 }
688 node = node->GetNext();
689 }
690 }
691
692 // ---------------------------------------------------------------------------
693 // drag and drop
694 // ---------------------------------------------------------------------------
695
696 #if wxUSE_DRAG_AND_DROP
697
698 void wxWindowX11::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
699 {
700 // TODO
701 }
702
703 #endif
704
705 // Old style file-manager drag&drop
706 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept))
707 {
708 // TODO
709 }
710
711 // ----------------------------------------------------------------------------
712 // tooltips
713 // ----------------------------------------------------------------------------
714
715 #if wxUSE_TOOLTIPS
716
717 void wxWindowX11::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
718 {
719 // TODO
720 }
721
722 #endif // wxUSE_TOOLTIPS
723
724 // ---------------------------------------------------------------------------
725 // moving and resizing
726 // ---------------------------------------------------------------------------
727
728 bool wxWindowX11::PreResize()
729 {
730 return TRUE;
731 }
732
733 // Get total size
734 void wxWindowX11::DoGetSize(int *x, int *y) const
735 {
736 Window xwindow = (Window) m_mainWindow;
737
738 wxCHECK_RET( xwindow, wxT("invalid window") );
739
740 //XSync(wxGlobalDisplay(), False);
741
742 XWindowAttributes attr;
743 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
744 wxASSERT(status);
745
746 if (status)
747 {
748 *x = attr.width /* + 2*m_borderSize */ ;
749 *y = attr.height /* + 2*m_borderSize */ ;
750 }
751 }
752
753 void wxWindowX11::DoGetPosition(int *x, int *y) const
754 {
755 Window window = (Window) m_mainWindow;
756 if (window)
757 {
758 //XSync(wxGlobalDisplay(), False);
759 XWindowAttributes attr;
760 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
761 wxASSERT(status);
762
763 if (status)
764 {
765 *x = attr.x;
766 *y = attr.y;
767
768 // We may be faking the client origin. So a window that's really at (0, 30)
769 // may appear (to wxWin apps) to be at (0, 0).
770 if (GetParent())
771 {
772 wxPoint pt(GetParent()->GetClientAreaOrigin());
773 *x -= pt.x;
774 *y -= pt.y;
775 }
776 }
777 }
778 }
779
780 void wxWindowX11::DoScreenToClient(int *x, int *y) const
781 {
782 Display *display = wxGlobalDisplay();
783 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
784 Window thisWindow = (Window) m_clientWindow;
785
786 Window childWindow;
787 int xx = *x;
788 int yy = *y;
789 XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow);
790 }
791
792 void wxWindowX11::DoClientToScreen(int *x, int *y) const
793 {
794 Display *display = wxGlobalDisplay();
795 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
796 Window thisWindow = (Window) m_clientWindow;
797
798 Window childWindow;
799 int xx = *x;
800 int yy = *y;
801 XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow);
802 }
803
804
805 // Get size *available for subwindows* i.e. excluding menu bar etc.
806 void wxWindowX11::DoGetClientSize(int *x, int *y) const
807 {
808 Window window = (Window) m_mainWindow;
809
810 if (window)
811 {
812 XWindowAttributes attr;
813 Status status = XGetWindowAttributes( wxGlobalDisplay(), window, &attr );
814 wxASSERT(status);
815
816 if (status)
817 {
818 *x = attr.width ;
819 *y = attr.height ;
820 }
821 }
822 }
823
824 void wxWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
825 {
826 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
827
828 Window xwindow = (Window) m_mainWindow;
829
830 wxCHECK_RET( xwindow, wxT("invalid window") );
831
832 XWindowAttributes attr;
833 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
834 wxCHECK_RET( status, wxT("invalid window attributes") );
835
836 int new_x = attr.x;
837 int new_y = attr.y;
838 int new_w = attr.width;
839 int new_h = attr.height;
840
841 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
842 {
843 int yy = 0;
844 AdjustForParentClientOrigin( x, yy, sizeFlags);
845 new_x = x;
846 }
847 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
848 {
849 int xx = 0;
850 AdjustForParentClientOrigin( xx, y, sizeFlags);
851 new_y = y;
852 }
853 if (width != -1)
854 {
855 new_w = width;
856 if (new_w <= 0)
857 new_w = 20;
858 }
859 if (height != -1)
860 {
861 new_h = height;
862 if (new_h <= 0)
863 new_h = 20;
864 }
865
866 DoMoveWindow( new_x, new_y, new_w, new_h );
867 }
868
869 void wxWindowX11::DoSetClientSize(int width, int height)
870 {
871 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
872
873 Window xwindow = (Window) m_mainWindow;
874
875 wxCHECK_RET( xwindow, wxT("invalid window") );
876
877 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
878
879 if (m_mainWindow != m_clientWindow)
880 {
881 xwindow = (Window) m_clientWindow;
882
883 wxWindow *window = (wxWindow*) this;
884 wxRenderer *renderer = window->GetRenderer();
885 if (renderer)
886 {
887 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
888 width -= border.x + border.width;
889 height -= border.y + border.height;
890 }
891
892 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
893 }
894 }
895
896 void wxWindowX11::DoMoveWindow(int x, int y, int width, int height)
897 {
898 Window xwindow = (Window) m_mainWindow;
899
900 wxCHECK_RET( xwindow, wxT("invalid window") );
901
902 #if !wxUSE_NANOX
903
904 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, width, height );
905 if (m_mainWindow != m_clientWindow)
906 {
907 xwindow = (Window) m_clientWindow;
908
909 wxWindow *window = (wxWindow*) this;
910 wxRenderer *renderer = window->GetRenderer();
911 if (renderer)
912 {
913 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
914 x = border.x;
915 y = border.y;
916 width -= border.x + border.width;
917 height -= border.y + border.height;
918 }
919 else
920 {
921 x = 0;
922 y = 0;
923 }
924
925 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
926 if (sb && sb->IsShown())
927 {
928 wxSize size = sb->GetSize();
929 height -= size.y;
930 }
931 sb = window->GetScrollbar( wxVERTICAL );
932 if (sb && sb->IsShown())
933 {
934 wxSize size = sb->GetSize();
935 width -= size.x;
936 }
937
938 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, wxMax(1, width), wxMax(1, height) );
939 }
940
941 #else
942
943 XWindowChanges windowChanges;
944 windowChanges.x = x;
945 windowChanges.y = y;
946 windowChanges.width = width;
947 windowChanges.height = height;
948 windowChanges.stack_mode = 0;
949 int valueMask = CWX | CWY | CWWidth | CWHeight;
950
951 XConfigureWindow( wxGlobalDisplay(), xwindow, valueMask, &windowChanges );
952
953 #endif
954 }
955
956 void wxWindowX11::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
957 {
958 m_minWidth = minW;
959 m_minHeight = minH;
960 m_maxWidth = maxW;
961 m_maxHeight = maxH;
962
963 #if !wxUSE_NANOX
964 XSizeHints sizeHints;
965 sizeHints.flags = 0;
966
967 if (minW > -1 && minH > -1)
968 {
969 sizeHints.flags |= PMinSize;
970 sizeHints.min_width = minW;
971 sizeHints.min_height = minH;
972 }
973 if (maxW > -1 && maxH > -1)
974 {
975 sizeHints.flags |= PMaxSize;
976 sizeHints.max_width = maxW;
977 sizeHints.max_height = maxH;
978 }
979 if (incW > -1 && incH > -1)
980 {
981 sizeHints.flags |= PResizeInc;
982 sizeHints.width_inc = incW;
983 sizeHints.height_inc = incH;
984 }
985
986 XSetWMNormalHints(wxGlobalDisplay(), (Window) m_mainWindow, &sizeHints );
987 #endif
988 }
989
990 // ---------------------------------------------------------------------------
991 // text metrics
992 // ---------------------------------------------------------------------------
993
994 int wxWindowX11::GetCharHeight() const
995 {
996 wxCHECK_MSG( m_font.Ok(), 0, wxT("valid window font needed") );
997
998 #if wxUSE_UNICODE
999 // There should be an easier way.
1000 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1001 pango_layout_set_font_description( layout, GetFont().GetNativeFontInfo()->description );
1002 pango_layout_set_text(layout, "H", 1 );
1003 int w,h;
1004 pango_layout_get_pixel_size(layout, &w, &h);
1005 g_object_unref( G_OBJECT( layout ) );
1006
1007 return h;
1008 #else
1009 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, wxGlobalDisplay());
1010
1011 int direction, ascent, descent;
1012 XCharStruct overall;
1013 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1014 &descent, &overall);
1015
1016 // return (overall.ascent + overall.descent);
1017 return (ascent + descent);
1018 #endif
1019 }
1020
1021 int wxWindowX11::GetCharWidth() const
1022 {
1023 wxCHECK_MSG( m_font.Ok(), 0, wxT("valid window font needed") );
1024
1025 #if wxUSE_UNICODE
1026 // There should be an easier way.
1027 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1028 pango_layout_set_font_description( layout, GetFont().GetNativeFontInfo()->description );
1029 pango_layout_set_text(layout, "H", 1 );
1030 int w,h;
1031 pango_layout_get_pixel_size(layout, &w, &h);
1032 g_object_unref( G_OBJECT( layout ) );
1033
1034 return w;
1035 #else
1036 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, wxGlobalDisplay());
1037
1038 int direction, ascent, descent;
1039 XCharStruct overall;
1040 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1041 &descent, &overall);
1042
1043 return overall.width;
1044 #endif
1045 }
1046
1047 void wxWindowX11::GetTextExtent(const wxString& string,
1048 int *x, int *y,
1049 int *descent, int *externalLeading,
1050 const wxFont *theFont) const
1051 {
1052 wxFont fontToUse = m_font;
1053 if (theFont) fontToUse = *theFont;
1054
1055 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
1056
1057 if (string.IsEmpty())
1058 {
1059 if (x) (*x) = 0;
1060 if (y) (*y) = 0;
1061 return;
1062 }
1063
1064 #if wxUSE_UNICODE
1065 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1066
1067 PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
1068 pango_layout_set_font_description(layout, desc);
1069
1070 const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
1071 pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data ));
1072
1073 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
1074
1075
1076 PangoRectangle rect;
1077 pango_layout_line_get_extents(line, NULL, &rect);
1078
1079 if (x) (*x) = (wxCoord) (rect.width / PANGO_SCALE);
1080 if (y) (*y) = (wxCoord) (rect.height / PANGO_SCALE);
1081 if (descent)
1082 {
1083 // Do something about metrics here
1084 (*descent) = 0;
1085 }
1086 if (externalLeading) (*externalLeading) = 0; // ??
1087
1088 g_object_unref( G_OBJECT( layout ) );
1089 #else
1090 WXFontStructPtr pFontStruct = fontToUse.GetFontStruct(1.0, wxGlobalDisplay());
1091
1092 int direction, ascent, descent2;
1093 XCharStruct overall;
1094 int slen = string.Len();
1095
1096 XTextExtents((XFontStruct*) pFontStruct, (char*) string.c_str(), slen,
1097 &direction, &ascent, &descent2, &overall);
1098
1099 if ( x )
1100 *x = (overall.width);
1101 if ( y )
1102 *y = (ascent + descent2);
1103 if (descent)
1104 *descent = descent2;
1105 if (externalLeading)
1106 *externalLeading = 0;
1107 #endif
1108 }
1109
1110 // ----------------------------------------------------------------------------
1111 // painting
1112 // ----------------------------------------------------------------------------
1113
1114 void wxWindowX11::Refresh(bool eraseBack, const wxRect *rect)
1115 {
1116 if (eraseBack)
1117 {
1118 if (rect)
1119 {
1120 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1121 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
1122 }
1123 else
1124 {
1125 int height,width;
1126 GetSize( &width, &height );
1127
1128 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1129 m_clearRegion.Clear();
1130 m_clearRegion.Union( 0, 0, width, height );
1131 }
1132 }
1133
1134 if (rect)
1135 {
1136 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1137 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
1138 }
1139 else
1140 {
1141 int height,width;
1142 GetSize( &width, &height );
1143
1144 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1145 m_updateRegion.Clear();
1146 m_updateRegion.Union( 0, 0, width, height );
1147 }
1148 }
1149
1150 void wxWindowX11::Update()
1151 {
1152 if (m_updateNcArea)
1153 {
1154 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1155 // Send nc paint events.
1156 SendNcPaintEvents();
1157 }
1158
1159 if (!m_updateRegion.IsEmpty())
1160 {
1161 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1162 // Actually send erase events.
1163 SendEraseEvents();
1164
1165 // Actually send paint events.
1166 SendPaintEvents();
1167 }
1168 }
1169
1170 void wxWindowX11::SendEraseEvents()
1171 {
1172 if (m_clearRegion.IsEmpty()) return;
1173
1174 wxClientDC dc( (wxWindow*)this );
1175 dc.SetClippingRegion( m_clearRegion );
1176
1177 wxEraseEvent erase_event( GetId(), &dc );
1178 erase_event.SetEventObject( this );
1179
1180 if (!GetEventHandler()->ProcessEvent(erase_event) )
1181 {
1182 Display *xdisplay = wxGlobalDisplay();
1183 Window xwindow = (Window) GetClientAreaWindow();
1184 XSetForeground( xdisplay, g_eraseGC, m_backgroundColour.GetPixel() );
1185
1186 wxRegionIterator upd( m_clearRegion );
1187 while (upd)
1188 {
1189 XFillRectangle( xdisplay, xwindow, g_eraseGC,
1190 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
1191 upd ++;
1192 }
1193 }
1194
1195 m_clearRegion.Clear();
1196 }
1197
1198 void wxWindowX11::SendPaintEvents()
1199 {
1200 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1201
1202 m_clipPaintRegion = TRUE;
1203
1204 wxPaintEvent paint_event( GetId() );
1205 paint_event.SetEventObject( this );
1206 GetEventHandler()->ProcessEvent( paint_event );
1207
1208 m_updateRegion.Clear();
1209
1210 m_clipPaintRegion = FALSE;
1211 }
1212
1213 void wxWindowX11::SendNcPaintEvents()
1214 {
1215 wxWindow *window = (wxWindow*) this;
1216
1217 // All this for drawing the small square between the scrollbars.
1218 int width = 0;
1219 int height = 0;
1220 int x = 0;
1221 int y = 0;
1222 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
1223 if (sb && sb->IsShown())
1224 {
1225 height = sb->GetSize().y;
1226 y = sb->GetPosition().y;
1227
1228 sb = window->GetScrollbar( wxVERTICAL );
1229 if (sb && sb->IsShown())
1230 {
1231 width = sb->GetSize().x;
1232 x = sb->GetPosition().x;
1233
1234 Display *xdisplay = wxGlobalDisplay();
1235 Window xwindow = (Window) GetMainWindow();
1236 Colormap cm = (Colormap) wxTheApp->GetMainColormap( wxGetDisplay() );
1237 wxColour colour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1238 colour.CalcPixel( (WXColormap) cm );
1239
1240 XSetForeground( xdisplay, g_eraseGC, colour.GetPixel() );
1241
1242 XFillRectangle( xdisplay, xwindow, g_eraseGC, x, y, width, height );
1243 }
1244 }
1245
1246 wxNcPaintEvent nc_paint_event( GetId() );
1247 nc_paint_event.SetEventObject( this );
1248 GetEventHandler()->ProcessEvent( nc_paint_event );
1249
1250 m_updateNcArea = FALSE;
1251 }
1252
1253 // ----------------------------------------------------------------------------
1254 // event handlers
1255 // ----------------------------------------------------------------------------
1256
1257 // Responds to colour changes: passes event on to children.
1258 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent& event)
1259 {
1260 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1261 while ( node )
1262 {
1263 // Only propagate to non-top-level windows
1264 wxWindow *win = node->GetData();
1265 if ( win->GetParent() )
1266 {
1267 wxSysColourChangedEvent event2;
1268 event.SetEventObject(win);
1269 win->GetEventHandler()->ProcessEvent(event2);
1270 }
1271
1272 node = node->GetNext();
1273 }
1274 }
1275
1276 // See handler for InFocus case in app.cpp for details.
1277 wxWindow* g_GettingFocus = NULL;
1278
1279 void wxWindowX11::OnInternalIdle()
1280 {
1281 // Update invalidated regions.
1282 Update();
1283
1284 // This calls the UI-update mechanism (querying windows for
1285 // menu/toolbar/control state information)
1286 if (wxUpdateUIEvent::CanUpdate((wxWindow*) this))
1287 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1288
1289 // Set the input focus if couldn't do it before
1290 if (m_needsInputFocus)
1291 {
1292 #if 0
1293 wxString msg;
1294 msg.Printf("Setting focus for %s from OnInternalIdle\n", GetClassInfo()->GetClassName());
1295 printf(msg.c_str());
1296 #endif
1297 SetFocus();
1298
1299 // If it couldn't set the focus now, there's
1300 // no point in trying again.
1301 m_needsInputFocus = FALSE;
1302 }
1303 g_GettingFocus = NULL;
1304 }
1305
1306 // ----------------------------------------------------------------------------
1307 // function which maintain the global hash table mapping Widgets to wxWidgets
1308 // ----------------------------------------------------------------------------
1309
1310 static bool DoAddWindowToTable(wxWindowHash *hash, Window w, wxWindow *win)
1311 {
1312 if ( !hash->insert(wxWindowHash::value_type(w, win)).second )
1313 {
1314 wxLogDebug( wxT("Widget table clash: new widget is 0x%08x, %s"),
1315 (unsigned int)w, win->GetClassInfo()->GetClassName());
1316 return FALSE;
1317 }
1318
1319 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1320 (unsigned int) w, win, win->GetClassInfo()->GetClassName());
1321
1322 return TRUE;
1323 }
1324
1325 static inline wxWindow *DoGetWindowFromTable(wxWindowHash *hash, Window w)
1326 {
1327 wxWindowHash::iterator i = hash->find(w);
1328 return i == hash->end() ? NULL : i->second;
1329 }
1330
1331 static inline void DoDeleteWindowFromTable(wxWindowHash *hash, Window w)
1332 {
1333 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x deleted"), (unsigned int) w);
1334
1335 hash->erase(w);
1336 }
1337
1338 // ----------------------------------------------------------------------------
1339 // public wrappers
1340 // ----------------------------------------------------------------------------
1341
1342 bool wxAddWindowToTable(Window w, wxWindow *win)
1343 {
1344 return DoAddWindowToTable(wxWidgetHashTable, w, win);
1345 }
1346
1347 wxWindow *wxGetWindowFromTable(Window w)
1348 {
1349 return DoGetWindowFromTable(wxWidgetHashTable, w);
1350 }
1351
1352 void wxDeleteWindowFromTable(Window w)
1353 {
1354 DoDeleteWindowFromTable(wxWidgetHashTable, w);
1355 }
1356
1357 bool wxAddClientWindowToTable(Window w, wxWindow *win)
1358 {
1359 return DoAddWindowToTable(wxClientWidgetHashTable, w, win);
1360 }
1361
1362 wxWindow *wxGetClientWindowFromTable(Window w)
1363 {
1364 return DoGetWindowFromTable(wxClientWidgetHashTable, w);
1365 }
1366
1367 void wxDeleteClientWindowFromTable(Window w)
1368 {
1369 DoDeleteWindowFromTable(wxClientWidgetHashTable, w);
1370 }
1371
1372 // ----------------------------------------------------------------------------
1373 // X11-specific accessors
1374 // ----------------------------------------------------------------------------
1375
1376 WXWindow wxWindowX11::GetMainWindow() const
1377 {
1378 return m_mainWindow;
1379 }
1380
1381 WXWindow wxWindowX11::GetClientAreaWindow() const
1382 {
1383 return m_clientWindow;
1384 }
1385
1386 // ----------------------------------------------------------------------------
1387 // TranslateXXXEvent() functions
1388 // ----------------------------------------------------------------------------
1389
1390 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Window window, XEvent *xevent)
1391 {
1392 switch (XEventGetType(xevent))
1393 {
1394 case EnterNotify:
1395 case LeaveNotify:
1396 case ButtonPress:
1397 case ButtonRelease:
1398 case MotionNotify:
1399 {
1400 wxEventType eventType = wxEVT_NULL;
1401
1402 if (XEventGetType(xevent) == EnterNotify)
1403 {
1404 //if (local_event.xcrossing.mode!=NotifyNormal)
1405 // return ; // Ignore grab events
1406 eventType = wxEVT_ENTER_WINDOW;
1407 // canvas->GetEventHandler()->OnSetFocus();
1408 }
1409 else if (XEventGetType(xevent) == LeaveNotify)
1410 {
1411 //if (local_event.xcrossingr.mode!=NotifyNormal)
1412 // return ; // Ignore grab events
1413 eventType = wxEVT_LEAVE_WINDOW;
1414 // canvas->GetEventHandler()->OnKillFocus();
1415 }
1416 else if (XEventGetType(xevent) == MotionNotify)
1417 {
1418 eventType = wxEVT_MOTION;
1419 }
1420 else if (XEventGetType(xevent) == ButtonPress)
1421 {
1422 wxevent.SetTimestamp(XButtonEventGetTime(xevent));
1423 int button = 0;
1424 if (XButtonEventLChanged(xevent))
1425 {
1426 eventType = wxEVT_LEFT_DOWN;
1427 button = 1;
1428 }
1429 else if (XButtonEventMChanged(xevent))
1430 {
1431 eventType = wxEVT_MIDDLE_DOWN;
1432 button = 2;
1433 }
1434 else if (XButtonEventRChanged(xevent))
1435 {
1436 eventType = wxEVT_RIGHT_DOWN;
1437 button = 3;
1438 }
1439
1440 // check for a double click
1441 // TODO: where can we get this value from?
1442 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1443 long dclickTime = 200;
1444 long ts = wxevent.GetTimestamp();
1445
1446 int buttonLast = win->GetLastClickedButton();
1447 long lastTS = win->GetLastClickTime();
1448 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
1449 {
1450 // I have a dclick
1451 win->SetLastClick(0, ts);
1452 if ( eventType == wxEVT_LEFT_DOWN )
1453 eventType = wxEVT_LEFT_DCLICK;
1454 else if ( eventType == wxEVT_MIDDLE_DOWN )
1455 eventType = wxEVT_MIDDLE_DCLICK;
1456 else if ( eventType == wxEVT_RIGHT_DOWN )
1457 eventType = wxEVT_RIGHT_DCLICK;
1458 }
1459 else
1460 {
1461 // not fast enough or different button
1462 win->SetLastClick(button, ts);
1463 }
1464 }
1465 else if (XEventGetType(xevent) == ButtonRelease)
1466 {
1467 if (XButtonEventLChanged(xevent))
1468 {
1469 eventType = wxEVT_LEFT_UP;
1470 }
1471 else if (XButtonEventMChanged(xevent))
1472 {
1473 eventType = wxEVT_MIDDLE_UP;
1474 }
1475 else if (XButtonEventRChanged(xevent))
1476 {
1477 eventType = wxEVT_RIGHT_UP;
1478 }
1479 else return FALSE;
1480 }
1481 else
1482 {
1483 return FALSE;
1484 }
1485
1486 wxevent.SetEventType(eventType);
1487
1488 wxevent.m_x = XButtonEventGetX(xevent);
1489 wxevent.m_y = XButtonEventGetY(xevent);
1490
1491 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
1492 || (XButtonEventLIsDown(xevent)
1493 && (eventType != wxEVT_LEFT_UP)));
1494 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
1495 || (XButtonEventMIsDown(xevent)
1496 && (eventType != wxEVT_MIDDLE_UP)));
1497 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
1498 || (XButtonEventRIsDown (xevent)
1499 && (eventType != wxEVT_RIGHT_UP)));
1500
1501 wxevent.m_shiftDown = XButtonEventShiftIsDown(xevent);
1502 wxevent.m_controlDown = XButtonEventCtrlIsDown(xevent);
1503 wxevent.m_altDown = XButtonEventAltIsDown(xevent);
1504 wxevent.m_metaDown = XButtonEventMetaIsDown(xevent);
1505
1506 wxevent.SetId(win->GetId());
1507 wxevent.SetEventObject(win);
1508
1509 return TRUE;
1510 }
1511 }
1512 return FALSE;
1513 }
1514
1515 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Window WXUNUSED(win), XEvent *xevent, bool isAscii)
1516 {
1517 switch (XEventGetType(xevent))
1518 {
1519 case KeyPress:
1520 case KeyRelease:
1521 {
1522 char buf[20];
1523
1524 KeySym keySym;
1525 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
1526 int id = wxCharCodeXToWX (keySym);
1527 // id may be WXK_xxx code - these are outside ASCII range, so we
1528 // can't just use toupper() on id.
1529 // Only change this if we want the raw key that was pressed,
1530 // and don't change it if we want an ASCII value.
1531 if (!isAscii && (id >= 'a' && id <= 'z'))
1532 {
1533 id = id + 'A' - 'a';
1534 }
1535
1536 wxevent.m_shiftDown = XKeyEventShiftIsDown(xevent);
1537 wxevent.m_controlDown = XKeyEventCtrlIsDown(xevent);
1538 wxevent.m_altDown = XKeyEventAltIsDown(xevent);
1539 wxevent.m_metaDown = XKeyEventMetaIsDown(xevent);
1540 wxevent.SetEventObject(win);
1541 wxevent.m_keyCode = id;
1542 wxevent.SetTimestamp(XKeyEventGetTime(xevent));
1543
1544 wxevent.m_x = XKeyEventGetX(xevent);
1545 wxevent.m_y = XKeyEventGetY(xevent);
1546
1547 return id > -1;
1548 }
1549 default:
1550 break;
1551 }
1552 return FALSE;
1553 }
1554
1555 // ----------------------------------------------------------------------------
1556 // Colour stuff
1557 // ----------------------------------------------------------------------------
1558
1559 bool wxWindowX11::SetBackgroundColour(const wxColour& col)
1560 {
1561 wxWindowBase::SetBackgroundColour(col);
1562
1563 Display *xdisplay = (Display*) wxGlobalDisplay();
1564 int xscreen = DefaultScreen( xdisplay );
1565 Colormap cm = DefaultColormap( xdisplay, xscreen );
1566
1567 m_backgroundColour.CalcPixel( (WXColormap) cm );
1568
1569 // We don't set the background colour as we paint
1570 // the background ourselves.
1571 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1572
1573 return TRUE;
1574 }
1575
1576 bool wxWindowX11::SetForegroundColour(const wxColour& col)
1577 {
1578 if ( !wxWindowBase::SetForegroundColour(col) )
1579 return FALSE;
1580
1581 return TRUE;
1582 }
1583
1584 // ----------------------------------------------------------------------------
1585 // global functions
1586 // ----------------------------------------------------------------------------
1587
1588 wxWindow *wxGetActiveWindow()
1589 {
1590 // TODO
1591 wxFAIL_MSG(wxT("Not implemented"));
1592 return NULL;
1593 }
1594
1595 /* static */
1596 wxWindow *wxWindowBase::GetCapture()
1597 {
1598 return (wxWindow *)g_captureWindow;
1599 }
1600
1601
1602 // Find the wxWindow at the current mouse position, returning the mouse
1603 // position.
1604 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1605 {
1606 return wxFindWindowAtPoint(wxGetMousePosition());
1607 }
1608
1609 // Get the current mouse position.
1610 wxPoint wxGetMousePosition()
1611 {
1612 #if wxUSE_NANOX
1613 /* TODO */
1614 return wxPoint(0, 0);
1615 #else
1616 Display *display = wxGlobalDisplay();
1617 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
1618 Window rootReturn, childReturn;
1619 int rootX, rootY, winX, winY;
1620 unsigned int maskReturn;
1621
1622 XQueryPointer (display,
1623 rootWindow,
1624 &rootReturn,
1625 &childReturn,
1626 &rootX, &rootY, &winX, &winY, &maskReturn);
1627 return wxPoint(rootX, rootY);
1628 #endif
1629 }
1630
1631
1632 // ----------------------------------------------------------------------------
1633 // wxNoOptimize: switch off size optimization
1634 // ----------------------------------------------------------------------------
1635
1636 int wxNoOptimize::ms_count = 0;
1637
1638
1639 // ----------------------------------------------------------------------------
1640 // wxDCModule
1641 // ----------------------------------------------------------------------------
1642
1643 class wxWinModule : public wxModule
1644 {
1645 public:
1646 bool OnInit();
1647 void OnExit();
1648
1649 private:
1650 DECLARE_DYNAMIC_CLASS(wxWinModule)
1651 };
1652
1653 IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
1654
1655 bool wxWinModule::OnInit()
1656 {
1657 Display *xdisplay = wxGlobalDisplay();
1658 int xscreen = DefaultScreen( xdisplay );
1659 Window xroot = RootWindow( xdisplay, xscreen );
1660 g_eraseGC = XCreateGC( xdisplay, xroot, 0, NULL );
1661 XSetFillStyle( xdisplay, g_eraseGC, FillSolid );
1662
1663 return TRUE;
1664 }
1665
1666 void wxWinModule::OnExit()
1667 {
1668 Display *xdisplay = wxGlobalDisplay();
1669 XFreeGC( xdisplay, g_eraseGC );
1670 }
1671
1672