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