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