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