]> git.saurik.com Git - wxWidgets.git/blob - src/x11/window.cpp
Don't assert if the window doesn't accept the focus.
[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 // Don't assert; we might be trying to set the focus for a panel
374 // with only static controls, so the panel returns false from AcceptsFocus.
375 // The app should be not be expected to deal with this.
376 if (!AcceptsFocus())
377 return;
378
379 #if 0
380 if (GetName() == "scrollBar")
381 {
382 char *crash = NULL;
383 *crash = 0;
384 }
385 #endif
386
387 if (wxWindowIsVisible(xwindow))
388 {
389 wxLogTrace( _T("focus"), _T("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
390 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
391 XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToNone, CurrentTime );
392 m_needsInputFocus = FALSE;
393 }
394 else
395 {
396 m_needsInputFocus = TRUE;
397 }
398 }
399
400 // Get the window with the focus
401 wxWindow *wxWindowBase::DoFindFocus()
402 {
403 Window xfocus = (Window) 0;
404 int revert = 0;
405
406 XGetInputFocus( wxGlobalDisplay(), &xfocus, &revert);
407 if (xfocus)
408 {
409 wxWindow *win = wxGetWindowFromTable( xfocus );
410 if (!win)
411 {
412 win = wxGetClientWindowFromTable( xfocus );
413 }
414
415 return win;
416 }
417
418 return NULL;
419 }
420
421 // Enabling/disabling handled by event loop, and not sending events
422 // if disabled.
423 bool wxWindowX11::Enable(bool enable)
424 {
425 if ( !wxWindowBase::Enable(enable) )
426 return FALSE;
427
428 return TRUE;
429 }
430
431 bool wxWindowX11::Show(bool show)
432 {
433 wxWindowBase::Show(show);
434
435 Window xwindow = (Window) m_mainWindow;
436 Display *xdisp = wxGlobalDisplay();
437 if (show)
438 {
439 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
440 XMapWindow(xdisp, xwindow);
441 }
442 else
443 {
444 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
445 XUnmapWindow(xdisp, xwindow);
446 }
447
448 return TRUE;
449 }
450
451 // Raise the window to the top of the Z order
452 void wxWindowX11::Raise()
453 {
454 if (m_mainWindow)
455 XRaiseWindow( wxGlobalDisplay(), (Window) m_mainWindow );
456 }
457
458 // Lower the window to the bottom of the Z order
459 void wxWindowX11::Lower()
460 {
461 if (m_mainWindow)
462 XLowerWindow( wxGlobalDisplay(), (Window) m_mainWindow );
463 }
464
465 void wxWindowX11::DoCaptureMouse()
466 {
467 if ((g_captureWindow != NULL) && (g_captureWindow != this))
468 {
469 wxASSERT_MSG(FALSE, wxT("Trying to capture before mouse released."));
470
471 // Core dump now
472 int *tmp = NULL;
473 (*tmp) = 1;
474 return;
475 }
476
477 if (m_winCaptured)
478 return;
479
480 Window xwindow = (Window) m_clientWindow;
481
482 wxCHECK_RET( xwindow, wxT("invalid window") );
483
484 g_captureWindow = (wxWindow*) this;
485
486 if (xwindow)
487 {
488 int res = XGrabPointer(wxGlobalDisplay(), xwindow,
489 FALSE,
490 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
491 GrabModeAsync,
492 GrabModeAsync,
493 None,
494 None, /* cursor */ // TODO: This may need to be set to the cursor of this window
495 CurrentTime );
496
497 if (res != GrabSuccess)
498 {
499 wxString msg;
500 msg.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
501 wxLogDebug(msg);
502 if (res == GrabNotViewable)
503 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
504
505 g_captureWindow = NULL;
506 return;
507 }
508
509 m_winCaptured = TRUE;
510 }
511 }
512
513 void wxWindowX11::DoReleaseMouse()
514 {
515 g_captureWindow = NULL;
516
517 if ( !m_winCaptured )
518 return;
519
520 Window xwindow = (Window) m_clientWindow;
521
522 if (xwindow)
523 {
524 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
525 }
526
527 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
528
529 m_winCaptured = FALSE;
530 }
531
532 bool wxWindowX11::SetFont(const wxFont& font)
533 {
534 if ( !wxWindowBase::SetFont(font) )
535 {
536 // nothing to do
537 return FALSE;
538 }
539
540 return TRUE;
541 }
542
543 bool wxWindowX11::SetCursor(const wxCursor& cursor)
544 {
545 if ( !wxWindowBase::SetCursor(cursor) )
546 {
547 // no change
548 return FALSE;
549 }
550
551 Window xwindow = (Window) m_clientWindow;
552
553 wxCHECK_MSG( xwindow, FALSE, wxT("invalid window") );
554
555 wxCursor cursorToUse;
556 if (m_cursor.Ok())
557 cursorToUse = m_cursor;
558 else
559 cursorToUse = *wxSTANDARD_CURSOR;
560
561 Cursor xcursor = (Cursor) cursorToUse.GetCursor();
562
563 XDefineCursor( wxGlobalDisplay(), xwindow, xcursor );
564
565 return TRUE;
566 }
567
568 // Coordinates relative to the window
569 void wxWindowX11::WarpPointer (int x, int y)
570 {
571 Window xwindow = (Window) m_clientWindow;
572
573 wxCHECK_RET( xwindow, wxT("invalid window") );
574
575 XWarpPointer( wxGlobalDisplay(), None, xwindow, 0, 0, 0, 0, x, y);
576 }
577
578 // Does a physical scroll
579 void wxWindowX11::ScrollWindow(int dx, int dy, const wxRect *rect)
580 {
581 // No scrolling requested.
582 if ((dx == 0) && (dy == 0)) return;
583
584 if (!m_updateRegion.IsEmpty())
585 {
586 m_updateRegion.Offset( dx, dy );
587
588 int cw = 0;
589 int ch = 0;
590 GetSize( &cw, &ch ); // GetClientSize() ??
591 m_updateRegion.Intersect( 0, 0, cw, ch );
592 }
593
594 if (!m_clearRegion.IsEmpty())
595 {
596 m_clearRegion.Offset( dx, dy );
597
598 int cw = 0;
599 int ch = 0;
600 GetSize( &cw, &ch ); // GetClientSize() ??
601 m_clearRegion.Intersect( 0, 0, cw, ch );
602 }
603
604 Window xwindow = (Window) GetClientAreaWindow();
605
606 wxCHECK_RET( xwindow, wxT("invalid window") );
607
608 Display *xdisplay = wxGlobalDisplay();
609
610 GC xgc = XCreateGC( xdisplay, xwindow, 0, NULL );
611 XSetGraphicsExposures( xdisplay, xgc, True );
612
613 int s_x = 0;
614 int s_y = 0;
615 int cw;
616 int ch;
617 if (rect)
618 {
619 s_x = rect->x;
620 s_y = rect->y;
621
622 cw = rect->width;
623 ch = rect->height;
624 }
625 else
626 {
627 s_x = 0;
628 s_y = 0;
629 GetClientSize( &cw, &ch );
630 }
631
632 #if wxUSE_TWO_WINDOWS
633 wxPoint offset( 0,0 );
634 #else
635 wxPoint offset = GetClientAreaOrigin();
636 s_x += offset.x;
637 s_y += offset.y;
638 #endif
639
640 int w = cw - abs(dx);
641 int h = ch - abs(dy);
642
643 if ((h < 0) || (w < 0))
644 {
645 Refresh();
646 }
647 else
648 {
649 wxRect rect;
650 if (dx < 0) rect.x = cw+dx + offset.x; else rect.x = s_x;
651 if (dy < 0) rect.y = ch+dy + offset.y; else rect.y = s_y;
652 if (dy != 0) rect.width = cw; else rect.width = abs(dx);
653 if (dx != 0) rect.height = ch; else rect.height = abs(dy);
654
655 int d_x = s_x;
656 int d_y = s_y;
657
658 if (dx < 0) s_x += -dx;
659 if (dy < 0) s_y += -dy;
660 if (dx > 0) d_x = dx + offset.x;
661 if (dy > 0) d_y = dy + offset.y;
662
663 XCopyArea( xdisplay, xwindow, xwindow, xgc, s_x, s_y, w, h, d_x, d_y );
664
665 // 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 );
666
667 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
668
669 m_updateRegion.Union( rect );
670 m_clearRegion.Union( rect );
671 }
672
673 XFreeGC( xdisplay, xgc );
674
675 // Move Clients, but not the scrollbars
676 // FIXME: There may be a better method to move a lot of Windows within X11
677 wxScrollBar *sbH = ((wxWindow *) this)->GetScrollbar( wxHORIZONTAL );
678 wxScrollBar *sbV = ((wxWindow *) this)->GetScrollbar( wxVERTICAL );
679 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
680 while ( node )
681 {
682 // Only propagate to non-top-level windows
683 wxWindow *win = node->GetData();
684 if ( win->GetParent() && win != sbH && win != sbV )
685 {
686 wxPoint pos = win->GetPosition();
687 // Add the delta to the old Position
688 pos.x += dx;
689 pos.y += dy;
690 win->SetPosition(pos);
691 }
692 node = node->GetNext();
693 }
694 }
695
696 // ---------------------------------------------------------------------------
697 // drag and drop
698 // ---------------------------------------------------------------------------
699
700 #if wxUSE_DRAG_AND_DROP
701
702 void wxWindowX11::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
703 {
704 // TODO
705 }
706
707 #endif
708
709 // Old style file-manager drag&drop
710 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept))
711 {
712 // TODO
713 }
714
715 // ----------------------------------------------------------------------------
716 // tooltips
717 // ----------------------------------------------------------------------------
718
719 #if wxUSE_TOOLTIPS
720
721 void wxWindowX11::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
722 {
723 // TODO
724 }
725
726 #endif // wxUSE_TOOLTIPS
727
728 // ---------------------------------------------------------------------------
729 // moving and resizing
730 // ---------------------------------------------------------------------------
731
732 bool wxWindowX11::PreResize()
733 {
734 return TRUE;
735 }
736
737 // Get total size
738 void wxWindowX11::DoGetSize(int *x, int *y) const
739 {
740 Window xwindow = (Window) m_mainWindow;
741
742 wxCHECK_RET( xwindow, wxT("invalid window") );
743
744 //XSync(wxGlobalDisplay(), False);
745
746 XWindowAttributes attr;
747 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
748 wxASSERT(status);
749
750 if (status)
751 {
752 *x = attr.width /* + 2*m_borderSize */ ;
753 *y = attr.height /* + 2*m_borderSize */ ;
754 }
755 }
756
757 void wxWindowX11::DoGetPosition(int *x, int *y) const
758 {
759 Window window = (Window) m_mainWindow;
760 if (window)
761 {
762 //XSync(wxGlobalDisplay(), False);
763 XWindowAttributes attr;
764 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
765 wxASSERT(status);
766
767 if (status)
768 {
769 *x = attr.x;
770 *y = attr.y;
771
772 // We may be faking the client origin. So a window that's really at (0, 30)
773 // may appear (to wxWin apps) to be at (0, 0).
774 if (GetParent())
775 {
776 wxPoint pt(GetParent()->GetClientAreaOrigin());
777 *x -= pt.x;
778 *y -= pt.y;
779 }
780 }
781 }
782 }
783
784 void wxWindowX11::DoScreenToClient(int *x, int *y) const
785 {
786 Display *display = wxGlobalDisplay();
787 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
788 Window thisWindow = (Window) m_clientWindow;
789
790 Window childWindow;
791 int xx = *x;
792 int yy = *y;
793 XTranslateCoordinates(display, rootWindow, thisWindow, xx, yy, x, y, &childWindow);
794 }
795
796 void wxWindowX11::DoClientToScreen(int *x, int *y) const
797 {
798 Display *display = wxGlobalDisplay();
799 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
800 Window thisWindow = (Window) m_clientWindow;
801
802 Window childWindow;
803 int xx = *x;
804 int yy = *y;
805 XTranslateCoordinates(display, thisWindow, rootWindow, xx, yy, x, y, &childWindow);
806 }
807
808
809 // Get size *available for subwindows* i.e. excluding menu bar etc.
810 void wxWindowX11::DoGetClientSize(int *x, int *y) const
811 {
812 Window window = (Window) m_mainWindow;
813
814 if (window)
815 {
816 XWindowAttributes attr;
817 Status status = XGetWindowAttributes( wxGlobalDisplay(), window, &attr );
818 wxASSERT(status);
819
820 if (status)
821 {
822 *x = attr.width ;
823 *y = attr.height ;
824 }
825 }
826 }
827
828 void wxWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
829 {
830 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
831
832 Window xwindow = (Window) m_mainWindow;
833
834 wxCHECK_RET( xwindow, wxT("invalid window") );
835
836 XWindowAttributes attr;
837 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
838 wxCHECK_RET( status, wxT("invalid window attributes") );
839
840 int new_x = attr.x;
841 int new_y = attr.y;
842 int new_w = attr.width;
843 int new_h = attr.height;
844
845 if (x != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
846 {
847 int yy = 0;
848 AdjustForParentClientOrigin( x, yy, sizeFlags);
849 new_x = x;
850 }
851 if (y != -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
852 {
853 int xx = 0;
854 AdjustForParentClientOrigin( xx, y, sizeFlags);
855 new_y = y;
856 }
857 if (width != -1)
858 {
859 new_w = width;
860 if (new_w <= 0)
861 new_w = 20;
862 }
863 if (height != -1)
864 {
865 new_h = height;
866 if (new_h <= 0)
867 new_h = 20;
868 }
869
870 DoMoveWindow( new_x, new_y, new_w, new_h );
871 }
872
873 void wxWindowX11::DoSetClientSize(int width, int height)
874 {
875 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
876
877 Window xwindow = (Window) m_mainWindow;
878
879 wxCHECK_RET( xwindow, wxT("invalid window") );
880
881 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
882
883 if (m_mainWindow != m_clientWindow)
884 {
885 xwindow = (Window) m_clientWindow;
886
887 wxWindow *window = (wxWindow*) this;
888 wxRenderer *renderer = window->GetRenderer();
889 if (renderer)
890 {
891 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
892 width -= border.x + border.width;
893 height -= border.y + border.height;
894 }
895
896 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
897 }
898 }
899
900 void wxWindowX11::DoMoveWindow(int x, int y, int width, int height)
901 {
902 Window xwindow = (Window) m_mainWindow;
903
904 wxCHECK_RET( xwindow, wxT("invalid window") );
905
906 #if !wxUSE_NANOX
907
908 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, width, height );
909 if (m_mainWindow != m_clientWindow)
910 {
911 xwindow = (Window) m_clientWindow;
912
913 wxWindow *window = (wxWindow*) this;
914 wxRenderer *renderer = window->GetRenderer();
915 if (renderer)
916 {
917 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
918 x = border.x;
919 y = border.y;
920 width -= border.x + border.width;
921 height -= border.y + border.height;
922 }
923 else
924 {
925 x = 0;
926 y = 0;
927 }
928
929 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
930 if (sb && sb->IsShown())
931 {
932 wxSize size = sb->GetSize();
933 height -= size.y;
934 }
935 sb = window->GetScrollbar( wxVERTICAL );
936 if (sb && sb->IsShown())
937 {
938 wxSize size = sb->GetSize();
939 width -= size.x;
940 }
941
942 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, wxMax(1, width), wxMax(1, height) );
943 }
944
945 #else
946
947 XWindowChanges windowChanges;
948 windowChanges.x = x;
949 windowChanges.y = y;
950 windowChanges.width = width;
951 windowChanges.height = height;
952 windowChanges.stack_mode = 0;
953 int valueMask = CWX | CWY | CWWidth | CWHeight;
954
955 XConfigureWindow( wxGlobalDisplay(), xwindow, valueMask, &windowChanges );
956
957 #endif
958 }
959
960 void wxWindowX11::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
961 {
962 m_minWidth = minW;
963 m_minHeight = minH;
964 m_maxWidth = maxW;
965 m_maxHeight = maxH;
966
967 #if !wxUSE_NANOX
968 XSizeHints sizeHints;
969 sizeHints.flags = 0;
970
971 if (minW > -1 && minH > -1)
972 {
973 sizeHints.flags |= PMinSize;
974 sizeHints.min_width = minW;
975 sizeHints.min_height = minH;
976 }
977 if (maxW > -1 && maxH > -1)
978 {
979 sizeHints.flags |= PMaxSize;
980 sizeHints.max_width = maxW;
981 sizeHints.max_height = maxH;
982 }
983 if (incW > -1 && incH > -1)
984 {
985 sizeHints.flags |= PResizeInc;
986 sizeHints.width_inc = incW;
987 sizeHints.height_inc = incH;
988 }
989
990 XSetWMNormalHints(wxGlobalDisplay(), (Window) m_mainWindow, &sizeHints );
991 #endif
992 }
993
994 // ---------------------------------------------------------------------------
995 // text metrics
996 // ---------------------------------------------------------------------------
997
998 int wxWindowX11::GetCharHeight() const
999 {
1000 wxCHECK_MSG( m_font.Ok(), 0, wxT("valid window font needed") );
1001
1002 #if wxUSE_UNICODE
1003 // There should be an easier way.
1004 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1005 pango_layout_set_font_description( layout, GetFont().GetNativeFontInfo()->description );
1006 pango_layout_set_text(layout, "H", 1 );
1007 int w,h;
1008 pango_layout_get_pixel_size(layout, &w, &h);
1009 g_object_unref( G_OBJECT( layout ) );
1010
1011 return h;
1012 #else
1013 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, wxGlobalDisplay());
1014
1015 int direction, ascent, descent;
1016 XCharStruct overall;
1017 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1018 &descent, &overall);
1019
1020 // return (overall.ascent + overall.descent);
1021 return (ascent + descent);
1022 #endif
1023 }
1024
1025 int wxWindowX11::GetCharWidth() const
1026 {
1027 wxCHECK_MSG( m_font.Ok(), 0, wxT("valid window font needed") );
1028
1029 #if wxUSE_UNICODE
1030 // There should be an easier way.
1031 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1032 pango_layout_set_font_description( layout, GetFont().GetNativeFontInfo()->description );
1033 pango_layout_set_text(layout, "H", 1 );
1034 int w,h;
1035 pango_layout_get_pixel_size(layout, &w, &h);
1036 g_object_unref( G_OBJECT( layout ) );
1037
1038 return w;
1039 #else
1040 WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, wxGlobalDisplay());
1041
1042 int direction, ascent, descent;
1043 XCharStruct overall;
1044 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1045 &descent, &overall);
1046
1047 return overall.width;
1048 #endif
1049 }
1050
1051 void wxWindowX11::GetTextExtent(const wxString& string,
1052 int *x, int *y,
1053 int *descent, int *externalLeading,
1054 const wxFont *theFont) const
1055 {
1056 wxFont fontToUse = m_font;
1057 if (theFont) fontToUse = *theFont;
1058
1059 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
1060
1061 if (string.IsEmpty())
1062 {
1063 if (x) (*x) = 0;
1064 if (y) (*y) = 0;
1065 return;
1066 }
1067
1068 #if wxUSE_UNICODE
1069 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1070
1071 PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
1072 pango_layout_set_font_description(layout, desc);
1073
1074 const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
1075 pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data ));
1076
1077 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
1078
1079
1080 PangoRectangle rect;
1081 pango_layout_line_get_extents(line, NULL, &rect);
1082
1083 if (x) (*x) = (wxCoord) (rect.width / PANGO_SCALE);
1084 if (y) (*y) = (wxCoord) (rect.height / PANGO_SCALE);
1085 if (descent)
1086 {
1087 // Do something about metrics here
1088 (*descent) = 0;
1089 }
1090 if (externalLeading) (*externalLeading) = 0; // ??
1091
1092 g_object_unref( G_OBJECT( layout ) );
1093 #else
1094 WXFontStructPtr pFontStruct = fontToUse.GetFontStruct(1.0, wxGlobalDisplay());
1095
1096 int direction, ascent, descent2;
1097 XCharStruct overall;
1098 int slen = string.Len();
1099
1100 XTextExtents((XFontStruct*) pFontStruct, (char*) string.c_str(), slen,
1101 &direction, &ascent, &descent2, &overall);
1102
1103 if ( x )
1104 *x = (overall.width);
1105 if ( y )
1106 *y = (ascent + descent2);
1107 if (descent)
1108 *descent = descent2;
1109 if (externalLeading)
1110 *externalLeading = 0;
1111 #endif
1112 }
1113
1114 // ----------------------------------------------------------------------------
1115 // painting
1116 // ----------------------------------------------------------------------------
1117
1118 void wxWindowX11::Refresh(bool eraseBack, const wxRect *rect)
1119 {
1120 if (eraseBack)
1121 {
1122 if (rect)
1123 {
1124 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1125 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
1126 }
1127 else
1128 {
1129 int height,width;
1130 GetSize( &width, &height );
1131
1132 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1133 m_clearRegion.Clear();
1134 m_clearRegion.Union( 0, 0, width, height );
1135 }
1136 }
1137
1138 if (rect)
1139 {
1140 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1141 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
1142 }
1143 else
1144 {
1145 int height,width;
1146 GetSize( &width, &height );
1147
1148 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1149 m_updateRegion.Clear();
1150 m_updateRegion.Union( 0, 0, width, height );
1151 }
1152 }
1153
1154 void wxWindowX11::Update()
1155 {
1156 if (m_updateNcArea)
1157 {
1158 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1159 // Send nc paint events.
1160 SendNcPaintEvents();
1161 }
1162
1163 if (!m_updateRegion.IsEmpty())
1164 {
1165 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1166 // Actually send erase events.
1167 SendEraseEvents();
1168
1169 // Actually send paint events.
1170 SendPaintEvents();
1171 }
1172 }
1173
1174 void wxWindowX11::SendEraseEvents()
1175 {
1176 if (m_clearRegion.IsEmpty()) return;
1177
1178 wxClientDC dc( (wxWindow*)this );
1179 dc.SetClippingRegion( m_clearRegion );
1180
1181 wxEraseEvent erase_event( GetId(), &dc );
1182 erase_event.SetEventObject( this );
1183
1184 if (!GetEventHandler()->ProcessEvent(erase_event) )
1185 {
1186 Display *xdisplay = wxGlobalDisplay();
1187 Window xwindow = (Window) GetClientAreaWindow();
1188 XSetForeground( xdisplay, g_eraseGC, m_backgroundColour.GetPixel() );
1189
1190 wxRegionIterator upd( m_clearRegion );
1191 while (upd)
1192 {
1193 XFillRectangle( xdisplay, xwindow, g_eraseGC,
1194 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
1195 upd ++;
1196 }
1197 }
1198
1199 m_clearRegion.Clear();
1200 }
1201
1202 void wxWindowX11::SendPaintEvents()
1203 {
1204 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1205
1206 m_clipPaintRegion = TRUE;
1207
1208 wxPaintEvent paint_event( GetId() );
1209 paint_event.SetEventObject( this );
1210 GetEventHandler()->ProcessEvent( paint_event );
1211
1212 m_updateRegion.Clear();
1213
1214 m_clipPaintRegion = FALSE;
1215 }
1216
1217 void wxWindowX11::SendNcPaintEvents()
1218 {
1219 wxWindow *window = (wxWindow*) this;
1220
1221 // All this for drawing the small square between the scrollbars.
1222 int width = 0;
1223 int height = 0;
1224 int x = 0;
1225 int y = 0;
1226 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
1227 if (sb && sb->IsShown())
1228 {
1229 height = sb->GetSize().y;
1230 y = sb->GetPosition().y;
1231
1232 sb = window->GetScrollbar( wxVERTICAL );
1233 if (sb && sb->IsShown())
1234 {
1235 width = sb->GetSize().x;
1236 x = sb->GetPosition().x;
1237
1238 Display *xdisplay = wxGlobalDisplay();
1239 Window xwindow = (Window) GetMainWindow();
1240 Colormap cm = (Colormap) wxTheApp->GetMainColormap( wxGetDisplay() );
1241 wxColour colour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1242 colour.CalcPixel( (WXColormap) cm );
1243
1244 XSetForeground( xdisplay, g_eraseGC, colour.GetPixel() );
1245
1246 XFillRectangle( xdisplay, xwindow, g_eraseGC, x, y, width, height );
1247 }
1248 }
1249
1250 wxNcPaintEvent nc_paint_event( GetId() );
1251 nc_paint_event.SetEventObject( this );
1252 GetEventHandler()->ProcessEvent( nc_paint_event );
1253
1254 m_updateNcArea = FALSE;
1255 }
1256
1257 // ----------------------------------------------------------------------------
1258 // event handlers
1259 // ----------------------------------------------------------------------------
1260
1261 // Responds to colour changes: passes event on to children.
1262 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent& event)
1263 {
1264 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1265 while ( node )
1266 {
1267 // Only propagate to non-top-level windows
1268 wxWindow *win = node->GetData();
1269 if ( win->GetParent() )
1270 {
1271 wxSysColourChangedEvent event2;
1272 event.SetEventObject(win);
1273 win->GetEventHandler()->ProcessEvent(event2);
1274 }
1275
1276 node = node->GetNext();
1277 }
1278 }
1279
1280 // See handler for InFocus case in app.cpp for details.
1281 wxWindow* g_GettingFocus = NULL;
1282
1283 void wxWindowX11::OnInternalIdle()
1284 {
1285 // Update invalidated regions.
1286 Update();
1287
1288 // This calls the UI-update mechanism (querying windows for
1289 // menu/toolbar/control state information)
1290 if (wxUpdateUIEvent::CanUpdate((wxWindow*) this))
1291 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1292
1293 // Set the input focus if couldn't do it before
1294 if (m_needsInputFocus)
1295 {
1296 #if 0
1297 wxString msg;
1298 msg.Printf("Setting focus for %s from OnInternalIdle\n", GetClassInfo()->GetClassName());
1299 printf(msg.c_str());
1300 #endif
1301 SetFocus();
1302
1303 // If it couldn't set the focus now, there's
1304 // no point in trying again.
1305 m_needsInputFocus = FALSE;
1306 }
1307 g_GettingFocus = NULL;
1308 }
1309
1310 // ----------------------------------------------------------------------------
1311 // function which maintain the global hash table mapping Widgets to wxWidgets
1312 // ----------------------------------------------------------------------------
1313
1314 static bool DoAddWindowToTable(wxWindowHash *hash, Window w, wxWindow *win)
1315 {
1316 if ( !hash->insert(wxWindowHash::value_type(w, win)).second )
1317 {
1318 wxLogDebug( wxT("Widget table clash: new widget is 0x%08x, %s"),
1319 (unsigned int)w, win->GetClassInfo()->GetClassName());
1320 return FALSE;
1321 }
1322
1323 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x <-> window %p (%s)"),
1324 (unsigned int) w, win, win->GetClassInfo()->GetClassName());
1325
1326 return TRUE;
1327 }
1328
1329 static inline wxWindow *DoGetWindowFromTable(wxWindowHash *hash, Window w)
1330 {
1331 wxWindowHash::iterator i = hash->find(w);
1332 return i == hash->end() ? NULL : i->second;
1333 }
1334
1335 static inline void DoDeleteWindowFromTable(wxWindowHash *hash, Window w)
1336 {
1337 wxLogTrace( wxT("widget"), wxT("XWindow 0x%08x deleted"), (unsigned int) w);
1338
1339 hash->erase(w);
1340 }
1341
1342 // ----------------------------------------------------------------------------
1343 // public wrappers
1344 // ----------------------------------------------------------------------------
1345
1346 bool wxAddWindowToTable(Window w, wxWindow *win)
1347 {
1348 return DoAddWindowToTable(wxWidgetHashTable, w, win);
1349 }
1350
1351 wxWindow *wxGetWindowFromTable(Window w)
1352 {
1353 return DoGetWindowFromTable(wxWidgetHashTable, w);
1354 }
1355
1356 void wxDeleteWindowFromTable(Window w)
1357 {
1358 DoDeleteWindowFromTable(wxWidgetHashTable, w);
1359 }
1360
1361 bool wxAddClientWindowToTable(Window w, wxWindow *win)
1362 {
1363 return DoAddWindowToTable(wxClientWidgetHashTable, w, win);
1364 }
1365
1366 wxWindow *wxGetClientWindowFromTable(Window w)
1367 {
1368 return DoGetWindowFromTable(wxClientWidgetHashTable, w);
1369 }
1370
1371 void wxDeleteClientWindowFromTable(Window w)
1372 {
1373 DoDeleteWindowFromTable(wxClientWidgetHashTable, w);
1374 }
1375
1376 // ----------------------------------------------------------------------------
1377 // X11-specific accessors
1378 // ----------------------------------------------------------------------------
1379
1380 WXWindow wxWindowX11::GetMainWindow() const
1381 {
1382 return m_mainWindow;
1383 }
1384
1385 WXWindow wxWindowX11::GetClientAreaWindow() const
1386 {
1387 return m_clientWindow;
1388 }
1389
1390 // ----------------------------------------------------------------------------
1391 // TranslateXXXEvent() functions
1392 // ----------------------------------------------------------------------------
1393
1394 bool wxTranslateMouseEvent(wxMouseEvent& wxevent, wxWindow *win, Window window, XEvent *xevent)
1395 {
1396 switch (XEventGetType(xevent))
1397 {
1398 case EnterNotify:
1399 case LeaveNotify:
1400 case ButtonPress:
1401 case ButtonRelease:
1402 case MotionNotify:
1403 {
1404 wxEventType eventType = wxEVT_NULL;
1405
1406 if (XEventGetType(xevent) == EnterNotify)
1407 {
1408 //if (local_event.xcrossing.mode!=NotifyNormal)
1409 // return ; // Ignore grab events
1410 eventType = wxEVT_ENTER_WINDOW;
1411 // canvas->GetEventHandler()->OnSetFocus();
1412 }
1413 else if (XEventGetType(xevent) == LeaveNotify)
1414 {
1415 //if (local_event.xcrossingr.mode!=NotifyNormal)
1416 // return ; // Ignore grab events
1417 eventType = wxEVT_LEAVE_WINDOW;
1418 // canvas->GetEventHandler()->OnKillFocus();
1419 }
1420 else if (XEventGetType(xevent) == MotionNotify)
1421 {
1422 eventType = wxEVT_MOTION;
1423 }
1424 else if (XEventGetType(xevent) == ButtonPress)
1425 {
1426 wxevent.SetTimestamp(XButtonEventGetTime(xevent));
1427 int button = 0;
1428 if (XButtonEventLChanged(xevent))
1429 {
1430 eventType = wxEVT_LEFT_DOWN;
1431 button = 1;
1432 }
1433 else if (XButtonEventMChanged(xevent))
1434 {
1435 eventType = wxEVT_MIDDLE_DOWN;
1436 button = 2;
1437 }
1438 else if (XButtonEventRChanged(xevent))
1439 {
1440 eventType = wxEVT_RIGHT_DOWN;
1441 button = 3;
1442 }
1443
1444 // check for a double click
1445 // TODO: where can we get this value from?
1446 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1447 long dclickTime = 200;
1448 long ts = wxevent.GetTimestamp();
1449
1450 int buttonLast = win->GetLastClickedButton();
1451 long lastTS = win->GetLastClickTime();
1452 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
1453 {
1454 // I have a dclick
1455 win->SetLastClick(0, ts);
1456 if ( eventType == wxEVT_LEFT_DOWN )
1457 eventType = wxEVT_LEFT_DCLICK;
1458 else if ( eventType == wxEVT_MIDDLE_DOWN )
1459 eventType = wxEVT_MIDDLE_DCLICK;
1460 else if ( eventType == wxEVT_RIGHT_DOWN )
1461 eventType = wxEVT_RIGHT_DCLICK;
1462 }
1463 else
1464 {
1465 // not fast enough or different button
1466 win->SetLastClick(button, ts);
1467 }
1468 }
1469 else if (XEventGetType(xevent) == ButtonRelease)
1470 {
1471 if (XButtonEventLChanged(xevent))
1472 {
1473 eventType = wxEVT_LEFT_UP;
1474 }
1475 else if (XButtonEventMChanged(xevent))
1476 {
1477 eventType = wxEVT_MIDDLE_UP;
1478 }
1479 else if (XButtonEventRChanged(xevent))
1480 {
1481 eventType = wxEVT_RIGHT_UP;
1482 }
1483 else return FALSE;
1484 }
1485 else
1486 {
1487 return FALSE;
1488 }
1489
1490 wxevent.SetEventType(eventType);
1491
1492 wxevent.m_x = XButtonEventGetX(xevent);
1493 wxevent.m_y = XButtonEventGetY(xevent);
1494
1495 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
1496 || (XButtonEventLIsDown(xevent)
1497 && (eventType != wxEVT_LEFT_UP)));
1498 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
1499 || (XButtonEventMIsDown(xevent)
1500 && (eventType != wxEVT_MIDDLE_UP)));
1501 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
1502 || (XButtonEventRIsDown (xevent)
1503 && (eventType != wxEVT_RIGHT_UP)));
1504
1505 wxevent.m_shiftDown = XButtonEventShiftIsDown(xevent);
1506 wxevent.m_controlDown = XButtonEventCtrlIsDown(xevent);
1507 wxevent.m_altDown = XButtonEventAltIsDown(xevent);
1508 wxevent.m_metaDown = XButtonEventMetaIsDown(xevent);
1509
1510 wxevent.SetId(win->GetId());
1511 wxevent.SetEventObject(win);
1512
1513 return TRUE;
1514 }
1515 }
1516 return FALSE;
1517 }
1518
1519 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Window WXUNUSED(win), XEvent *xevent, bool isAscii)
1520 {
1521 switch (XEventGetType(xevent))
1522 {
1523 case KeyPress:
1524 case KeyRelease:
1525 {
1526 char buf[20];
1527
1528 KeySym keySym;
1529 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
1530 int id = wxCharCodeXToWX (keySym);
1531 // id may be WXK_xxx code - these are outside ASCII range, so we
1532 // can't just use toupper() on id.
1533 // Only change this if we want the raw key that was pressed,
1534 // and don't change it if we want an ASCII value.
1535 if (!isAscii && (id >= 'a' && id <= 'z'))
1536 {
1537 id = id + 'A' - 'a';
1538 }
1539
1540 wxevent.m_shiftDown = XKeyEventShiftIsDown(xevent);
1541 wxevent.m_controlDown = XKeyEventCtrlIsDown(xevent);
1542 wxevent.m_altDown = XKeyEventAltIsDown(xevent);
1543 wxevent.m_metaDown = XKeyEventMetaIsDown(xevent);
1544 wxevent.SetEventObject(win);
1545 wxevent.m_keyCode = id;
1546 wxevent.SetTimestamp(XKeyEventGetTime(xevent));
1547
1548 wxevent.m_x = XKeyEventGetX(xevent);
1549 wxevent.m_y = XKeyEventGetY(xevent);
1550
1551 return id > -1;
1552 }
1553 default:
1554 break;
1555 }
1556 return FALSE;
1557 }
1558
1559 // ----------------------------------------------------------------------------
1560 // Colour stuff
1561 // ----------------------------------------------------------------------------
1562
1563 bool wxWindowX11::SetBackgroundColour(const wxColour& col)
1564 {
1565 wxWindowBase::SetBackgroundColour(col);
1566
1567 Display *xdisplay = (Display*) wxGlobalDisplay();
1568 int xscreen = DefaultScreen( xdisplay );
1569 Colormap cm = DefaultColormap( xdisplay, xscreen );
1570
1571 m_backgroundColour.CalcPixel( (WXColormap) cm );
1572
1573 // We don't set the background colour as we paint
1574 // the background ourselves.
1575 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1576
1577 return TRUE;
1578 }
1579
1580 bool wxWindowX11::SetForegroundColour(const wxColour& col)
1581 {
1582 if ( !wxWindowBase::SetForegroundColour(col) )
1583 return FALSE;
1584
1585 return TRUE;
1586 }
1587
1588 // ----------------------------------------------------------------------------
1589 // global functions
1590 // ----------------------------------------------------------------------------
1591
1592 wxWindow *wxGetActiveWindow()
1593 {
1594 // TODO
1595 wxFAIL_MSG(wxT("Not implemented"));
1596 return NULL;
1597 }
1598
1599 /* static */
1600 wxWindow *wxWindowBase::GetCapture()
1601 {
1602 return (wxWindow *)g_captureWindow;
1603 }
1604
1605
1606 // Find the wxWindow at the current mouse position, returning the mouse
1607 // position.
1608 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1609 {
1610 return wxFindWindowAtPoint(wxGetMousePosition());
1611 }
1612
1613 // Get the current mouse position.
1614 wxPoint wxGetMousePosition()
1615 {
1616 #if wxUSE_NANOX
1617 /* TODO */
1618 return wxPoint(0, 0);
1619 #else
1620 Display *display = wxGlobalDisplay();
1621 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
1622 Window rootReturn, childReturn;
1623 int rootX, rootY, winX, winY;
1624 unsigned int maskReturn;
1625
1626 XQueryPointer (display,
1627 rootWindow,
1628 &rootReturn,
1629 &childReturn,
1630 &rootX, &rootY, &winX, &winY, &maskReturn);
1631 return wxPoint(rootX, rootY);
1632 #endif
1633 }
1634
1635
1636 // ----------------------------------------------------------------------------
1637 // wxNoOptimize: switch off size optimization
1638 // ----------------------------------------------------------------------------
1639
1640 int wxNoOptimize::ms_count = 0;
1641
1642
1643 // ----------------------------------------------------------------------------
1644 // wxDCModule
1645 // ----------------------------------------------------------------------------
1646
1647 class wxWinModule : public wxModule
1648 {
1649 public:
1650 bool OnInit();
1651 void OnExit();
1652
1653 private:
1654 DECLARE_DYNAMIC_CLASS(wxWinModule)
1655 };
1656
1657 IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
1658
1659 bool wxWinModule::OnInit()
1660 {
1661 Display *xdisplay = wxGlobalDisplay();
1662 int xscreen = DefaultScreen( xdisplay );
1663 Window xroot = RootWindow( xdisplay, xscreen );
1664 g_eraseGC = XCreateGC( xdisplay, xroot, 0, NULL );
1665 XSetFillStyle( xdisplay, g_eraseGC, FillSolid );
1666
1667 return TRUE;
1668 }
1669
1670 void wxWinModule::OnExit()
1671 {
1672 Display *xdisplay = wxGlobalDisplay();
1673 XFreeGC( xdisplay, g_eraseGC );
1674 }
1675
1676