correcting wxX11 for wxkeysym as was done for wxMOTIF
[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 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 // ----------------------------------------------------------------------------
24 // headers
25 // ----------------------------------------------------------------------------
26
27 #include "wx/window.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/hash.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/utils.h"
34 #include "wx/panel.h"
35 #include "wx/frame.h"
36 #include "wx/dc.h"
37 #include "wx/dcclient.h"
38 #include "wx/button.h"
39 #include "wx/menu.h"
40 #include "wx/dialog.h"
41 #include "wx/timer.h"
42 #include "wx/settings.h"
43 #include "wx/msgdlg.h"
44 #include "wx/scrolbar.h"
45 #include "wx/listbox.h"
46 #include "wx/scrolwin.h"
47 #include "wx/layout.h"
48 #include "wx/menuitem.h"
49 #include "wx/module.h"
50 #include "wx/unix/utilsx11.h"
51 #endif
52
53 #include "wx/fontutil.h"
54 #include "wx/univ/renderer.h"
55
56 #if wxUSE_DRAG_AND_DROP
57 #include "wx/dnd.h"
58 #endif
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 if (wxWindowIsVisible(xwindow))
400 {
401 wxLogTrace( wxT("focus"), wxT("wxWindowX11::SetFocus: %s"), GetClassInfo()->GetClassName());
402 // XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToParent, CurrentTime );
403 XSetInputFocus( wxGlobalDisplay(), xwindow, RevertToNone, CurrentTime );
404 m_needsInputFocus = false;
405 }
406 else
407 {
408 m_needsInputFocus = true;
409 }
410 }
411
412 // Get the window with the focus
413 wxWindow *wxWindowBase::DoFindFocus()
414 {
415 Window xfocus = (Window) 0;
416 int revert = 0;
417
418 XGetInputFocus( wxGlobalDisplay(), &xfocus, &revert);
419 if (xfocus)
420 {
421 wxWindow *win = wxGetWindowFromTable( xfocus );
422 if (!win)
423 {
424 win = wxGetClientWindowFromTable( xfocus );
425 }
426
427 return win;
428 }
429
430 return NULL;
431 }
432
433 // Enabling/disabling handled by event loop, and not sending events
434 // if disabled.
435 bool wxWindowX11::Enable(bool enable)
436 {
437 if ( !wxWindowBase::Enable(enable) )
438 return false;
439
440 return true;
441 }
442
443 bool wxWindowX11::Show(bool show)
444 {
445 wxWindowBase::Show(show);
446
447 Window xwindow = (Window) m_mainWindow;
448 Display *xdisp = wxGlobalDisplay();
449 if (show)
450 {
451 // wxLogDebug( "Mapping window of type %s", GetName().c_str() );
452 XMapWindow(xdisp, xwindow);
453 }
454 else
455 {
456 // wxLogDebug( "Unmapping window of type %s", GetName().c_str() );
457 XUnmapWindow(xdisp, xwindow);
458 }
459
460 return true;
461 }
462
463 // Raise the window to the top of the Z order
464 void wxWindowX11::Raise()
465 {
466 if (m_mainWindow)
467 XRaiseWindow( wxGlobalDisplay(), (Window) m_mainWindow );
468 }
469
470 // Lower the window to the bottom of the Z order
471 void wxWindowX11::Lower()
472 {
473 if (m_mainWindow)
474 XLowerWindow( wxGlobalDisplay(), (Window) m_mainWindow );
475 }
476
477 void wxWindowX11::SetLabel(const wxString& WXUNUSED(label))
478 {
479 // TODO
480 }
481
482 wxString wxWindowX11::GetLabel() const
483 {
484 // TODO
485 return wxEmptyString;
486 }
487
488 void wxWindowX11::DoCaptureMouse()
489 {
490 if ((g_captureWindow != NULL) && (g_captureWindow != this))
491 {
492 wxFAIL_MSG(wxT("Trying to capture before mouse released."));
493
494 // Core dump now
495 int *tmp = NULL;
496 (*tmp) = 1;
497 return;
498 }
499
500 if (m_winCaptured)
501 return;
502
503 Window xwindow = (Window) m_clientWindow;
504
505 wxCHECK_RET( xwindow, wxT("invalid window") );
506
507 g_captureWindow = (wxWindow*) this;
508
509 if (xwindow)
510 {
511 int res = XGrabPointer(wxGlobalDisplay(), xwindow,
512 FALSE,
513 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
514 GrabModeAsync,
515 GrabModeAsync,
516 None,
517 None, /* cursor */ // TODO: This may need to be set to the cursor of this window
518 CurrentTime );
519
520 if (res != GrabSuccess)
521 {
522 wxString msg;
523 msg.Printf(wxT("Failed to grab pointer for window %s"), this->GetClassInfo()->GetClassName());
524 wxLogDebug(msg);
525 if (res == GrabNotViewable)
526 {
527 wxLogDebug( wxT("This is not a viewable window - perhaps not shown yet?") );
528 }
529
530 g_captureWindow = NULL;
531 return;
532 }
533
534 m_winCaptured = true;
535 }
536 }
537
538 void wxWindowX11::DoReleaseMouse()
539 {
540 g_captureWindow = NULL;
541
542 if ( !m_winCaptured )
543 return;
544
545 Window xwindow = (Window) m_clientWindow;
546
547 if (xwindow)
548 {
549 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
550 }
551
552 // wxLogDebug( "Ungrabbed pointer in %s", GetName().c_str() );
553
554 m_winCaptured = false;
555 }
556
557 bool wxWindowX11::SetFont(const wxFont& font)
558 {
559 if ( !wxWindowBase::SetFont(font) )
560 {
561 // nothing to do
562 return false;
563 }
564
565 return true;
566 }
567
568 bool wxWindowX11::SetCursor(const wxCursor& cursor)
569 {
570 if ( !wxWindowBase::SetCursor(cursor) )
571 {
572 // no change
573 return false;
574 }
575
576 Window xwindow = (Window) m_clientWindow;
577
578 wxCHECK_MSG( xwindow, false, wxT("invalid window") );
579
580 wxCursor cursorToUse;
581 if (m_cursor.Ok())
582 cursorToUse = m_cursor;
583 else
584 cursorToUse = *wxSTANDARD_CURSOR;
585
586 Cursor xcursor = (Cursor) cursorToUse.GetCursor();
587
588 XDefineCursor( wxGlobalDisplay(), xwindow, xcursor );
589
590 return true;
591 }
592
593 // Coordinates relative to the window
594 void wxWindowX11::WarpPointer (int x, int y)
595 {
596 Window xwindow = (Window) m_clientWindow;
597
598 wxCHECK_RET( xwindow, wxT("invalid window") );
599
600 XWarpPointer( wxGlobalDisplay(), None, xwindow, 0, 0, 0, 0, x, y);
601 }
602
603 // Does a physical scroll
604 void wxWindowX11::ScrollWindow(int dx, int dy, const wxRect *rect)
605 {
606 // No scrolling requested.
607 if ((dx == 0) && (dy == 0)) return;
608
609 if (!m_updateRegion.IsEmpty())
610 {
611 m_updateRegion.Offset( dx, dy );
612
613 int cw = 0;
614 int ch = 0;
615 GetSize( &cw, &ch ); // GetClientSize() ??
616 m_updateRegion.Intersect( 0, 0, cw, ch );
617 }
618
619 if (!m_clearRegion.IsEmpty())
620 {
621 m_clearRegion.Offset( dx, dy );
622
623 int cw = 0;
624 int ch = 0;
625 GetSize( &cw, &ch ); // GetClientSize() ??
626 m_clearRegion.Intersect( 0, 0, cw, ch );
627 }
628
629 Window xwindow = (Window) GetClientAreaWindow();
630
631 wxCHECK_RET( xwindow, wxT("invalid window") );
632
633 Display *xdisplay = wxGlobalDisplay();
634
635 GC xgc = XCreateGC( xdisplay, xwindow, 0, NULL );
636 XSetGraphicsExposures( xdisplay, xgc, True );
637
638 int s_x = 0;
639 int s_y = 0;
640 int cw;
641 int ch;
642 if (rect)
643 {
644 s_x = rect->x;
645 s_y = rect->y;
646
647 cw = rect->width;
648 ch = rect->height;
649 }
650 else
651 {
652 s_x = 0;
653 s_y = 0;
654 GetClientSize( &cw, &ch );
655 }
656
657 #if wxUSE_TWO_WINDOWS
658 wxPoint offset( 0,0 );
659 #else
660 wxPoint offset = GetClientAreaOrigin();
661 s_x += offset.x;
662 s_y += offset.y;
663 #endif
664
665 int w = cw - abs(dx);
666 int h = ch - abs(dy);
667
668 if ((h < 0) || (w < 0))
669 {
670 Refresh();
671 }
672 else
673 {
674 wxRect rect;
675 if (dx < 0) rect.x = cw+dx + offset.x; else rect.x = s_x;
676 if (dy < 0) rect.y = ch+dy + offset.y; else rect.y = s_y;
677 if (dy != 0) rect.width = cw; else rect.width = abs(dx);
678 if (dx != 0) rect.height = ch; else rect.height = abs(dy);
679
680 int d_x = s_x;
681 int d_y = s_y;
682
683 if (dx < 0) s_x += -dx;
684 if (dy < 0) s_y += -dy;
685 if (dx > 0) d_x += dx + offset.x;
686 if (dy > 0) d_y += dy + offset.y;
687
688 XCopyArea( xdisplay, xwindow, xwindow, xgc, s_x, s_y, w, h, d_x, d_y );
689
690 // 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 );
691
692 // wxLogDebug( "Update: %d %d %d %d", rect.x, rect.y, rect.width, rect.height );
693
694 m_updateRegion.Union( rect );
695 m_clearRegion.Union( rect );
696 }
697
698 XFreeGC( xdisplay, xgc );
699
700 // Move Clients, but not the scrollbars
701 // FIXME: There may be a better method to move a lot of Windows within X11
702 wxScrollBar *sbH = ((wxWindow *) this)->GetScrollbar( wxHORIZONTAL );
703 wxScrollBar *sbV = ((wxWindow *) this)->GetScrollbar( wxVERTICAL );
704 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
705 while ( node )
706 {
707 // Only propagate to non-top-level windows
708 wxWindow *win = node->GetData();
709 if ( win->GetParent() && win != sbH && win != sbV )
710 {
711 wxPoint pos = win->GetPosition();
712 // Add the delta to the old Position
713 pos.x += dx;
714 pos.y += dy;
715 win->SetPosition(pos);
716 }
717 node = node->GetNext();
718 }
719 }
720
721 // ---------------------------------------------------------------------------
722 // drag and drop
723 // ---------------------------------------------------------------------------
724
725 #if wxUSE_DRAG_AND_DROP
726
727 void wxWindowX11::SetDropTarget(wxDropTarget * WXUNUSED(pDropTarget))
728 {
729 // TODO
730 }
731
732 #endif
733
734 // Old style file-manager drag&drop
735 void wxWindowX11::DragAcceptFiles(bool WXUNUSED(accept))
736 {
737 // TODO
738 }
739
740 // ----------------------------------------------------------------------------
741 // tooltips
742 // ----------------------------------------------------------------------------
743
744 #if wxUSE_TOOLTIPS
745
746 void wxWindowX11::DoSetToolTip(wxToolTip * WXUNUSED(tooltip))
747 {
748 // TODO
749 }
750
751 #endif // wxUSE_TOOLTIPS
752
753 // ---------------------------------------------------------------------------
754 // moving and resizing
755 // ---------------------------------------------------------------------------
756
757 bool wxWindowX11::PreResize()
758 {
759 return true;
760 }
761
762 // Get total size
763 void wxWindowX11::DoGetSize(int *x, int *y) const
764 {
765 Window xwindow = (Window) m_mainWindow;
766
767 wxCHECK_RET( xwindow, wxT("invalid window") );
768
769 //XSync(wxGlobalDisplay(), False);
770
771 XWindowAttributes attr;
772 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
773 wxASSERT(status);
774
775 if (status)
776 {
777 *x = attr.width /* + 2*m_borderSize */ ;
778 *y = attr.height /* + 2*m_borderSize */ ;
779 }
780 }
781
782 void wxWindowX11::DoGetPosition(int *x, int *y) const
783 {
784 Window window = (Window) m_mainWindow;
785 if (window)
786 {
787 //XSync(wxGlobalDisplay(), False);
788 XWindowAttributes attr;
789 Status status = XGetWindowAttributes(wxGlobalDisplay(), window, & attr);
790 wxASSERT(status);
791
792 if (status)
793 {
794 *x = attr.x;
795 *y = attr.y;
796
797 // We may be faking the client origin. So a window that's really at (0, 30)
798 // may appear (to wxWin apps) to be at (0, 0).
799 if (GetParent())
800 {
801 wxPoint pt(GetParent()->GetClientAreaOrigin());
802 *x -= pt.x;
803 *y -= pt.y;
804 }
805 }
806 }
807 }
808
809 void wxWindowX11::DoScreenToClient(int *x, int *y) const
810 {
811 Display *display = wxGlobalDisplay();
812 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
813 Window thisWindow = (Window) m_clientWindow;
814
815 Window childWindow;
816 int xx = x ? *x : 0;
817 int yy = y ? *y : 0;
818 XTranslateCoordinates(display, rootWindow, thisWindow,
819 xx, yy, x ? x : &xx, y ? y : &yy,
820 &childWindow);
821 }
822
823 void wxWindowX11::DoClientToScreen(int *x, int *y) const
824 {
825 Display *display = wxGlobalDisplay();
826 Window rootWindow = RootWindowOfScreen(DefaultScreenOfDisplay(display));
827 Window thisWindow = (Window) m_clientWindow;
828
829 Window childWindow;
830 int xx = x ? *x : 0;
831 int yy = y ? *y : 0;
832 XTranslateCoordinates(display, thisWindow, rootWindow,
833 xx, yy, x ? x : &xx, y ? y : &yy,
834 &childWindow);
835 }
836
837
838 // Get size *available for subwindows* i.e. excluding menu bar etc.
839 void wxWindowX11::DoGetClientSize(int *x, int *y) const
840 {
841 Window window = (Window) m_mainWindow;
842
843 if (window)
844 {
845 XWindowAttributes attr;
846 Status status = XGetWindowAttributes( wxGlobalDisplay(), window, &attr );
847 wxASSERT(status);
848
849 if (status)
850 {
851 *x = attr.width ;
852 *y = attr.height ;
853 }
854 }
855 }
856
857 void wxWindowX11::DoSetSize(int x, int y, int width, int height, int sizeFlags)
858 {
859 // wxLogDebug("DoSetSize: %s (%ld) %d, %d %dx%d", GetClassInfo()->GetClassName(), GetId(), x, y, width, height);
860
861 Window xwindow = (Window) m_mainWindow;
862
863 wxCHECK_RET( xwindow, wxT("invalid window") );
864
865 XWindowAttributes attr;
866 Status status = XGetWindowAttributes( wxGlobalDisplay(), xwindow, &attr );
867 wxCHECK_RET( status, wxT("invalid window attributes") );
868
869 int new_x = attr.x;
870 int new_y = attr.y;
871 int new_w = attr.width;
872 int new_h = attr.height;
873
874 if (x != wxDefaultCoord || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
875 {
876 int yy = 0;
877 AdjustForParentClientOrigin( x, yy, sizeFlags);
878 new_x = x;
879 }
880 if (y != wxDefaultCoord || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
881 {
882 int xx = 0;
883 AdjustForParentClientOrigin( xx, y, sizeFlags);
884 new_y = y;
885 }
886 if (width != wxDefaultCoord)
887 {
888 new_w = width;
889 if (new_w <= 0)
890 new_w = 20;
891 }
892 if (height != wxDefaultCoord)
893 {
894 new_h = height;
895 if (new_h <= 0)
896 new_h = 20;
897 }
898
899 DoMoveWindow( new_x, new_y, new_w, new_h );
900 }
901
902 void wxWindowX11::DoSetClientSize(int width, int height)
903 {
904 // wxLogDebug("DoSetClientSize: %s (%ld) %dx%d", GetClassInfo()->GetClassName(), GetId(), width, height);
905
906 Window xwindow = (Window) m_mainWindow;
907
908 wxCHECK_RET( xwindow, wxT("invalid window") );
909
910 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
911
912 if (m_mainWindow != m_clientWindow)
913 {
914 xwindow = (Window) m_clientWindow;
915
916 wxWindow *window = (wxWindow*) this;
917 wxRenderer *renderer = window->GetRenderer();
918 if (renderer)
919 {
920 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
921 width -= border.x + border.width;
922 height -= border.y + border.height;
923 }
924
925 XResizeWindow( wxGlobalDisplay(), xwindow, width, height );
926 }
927 }
928
929 void wxWindowX11::DoMoveWindow(int x, int y, int width, int height)
930 {
931 Window xwindow = (Window) m_mainWindow;
932
933 wxCHECK_RET( xwindow, wxT("invalid window") );
934
935 #if !wxUSE_NANOX
936
937 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, width, height );
938 if (m_mainWindow != m_clientWindow)
939 {
940 xwindow = (Window) m_clientWindow;
941
942 wxWindow *window = (wxWindow*) this;
943 wxRenderer *renderer = window->GetRenderer();
944 if (renderer)
945 {
946 wxRect border = renderer->GetBorderDimensions( (wxBorder)(m_windowStyle & wxBORDER_MASK) );
947 x = border.x;
948 y = border.y;
949 width -= border.x + border.width;
950 height -= border.y + border.height;
951 }
952 else
953 {
954 x = 0;
955 y = 0;
956 }
957
958 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
959 if (sb && sb->IsShown())
960 {
961 wxSize size = sb->GetSize();
962 height -= size.y;
963 }
964 sb = window->GetScrollbar( wxVERTICAL );
965 if (sb && sb->IsShown())
966 {
967 wxSize size = sb->GetSize();
968 width -= size.x;
969 }
970
971 XMoveResizeWindow( wxGlobalDisplay(), xwindow, x, y, wxMax(1, width), wxMax(1, height) );
972 }
973
974 #else
975
976 XWindowChanges windowChanges;
977 windowChanges.x = x;
978 windowChanges.y = y;
979 windowChanges.width = width;
980 windowChanges.height = height;
981 windowChanges.stack_mode = 0;
982 int valueMask = CWX | CWY | CWWidth | CWHeight;
983
984 XConfigureWindow( wxGlobalDisplay(), xwindow, valueMask, &windowChanges );
985
986 #endif
987 }
988
989 void wxWindowX11::DoSetSizeHints(int minW, int minH, int maxW, int maxH, int incW, int incH)
990 {
991 m_minWidth = minW;
992 m_minHeight = minH;
993 m_maxWidth = maxW;
994 m_maxHeight = maxH;
995
996 #if !wxUSE_NANOX
997 XSizeHints sizeHints;
998 sizeHints.flags = 0;
999
1000 if (minW > -1 && minH > -1)
1001 {
1002 sizeHints.flags |= PMinSize;
1003 sizeHints.min_width = minW;
1004 sizeHints.min_height = minH;
1005 }
1006 if (maxW > -1 && maxH > -1)
1007 {
1008 sizeHints.flags |= PMaxSize;
1009 sizeHints.max_width = maxW;
1010 sizeHints.max_height = maxH;
1011 }
1012 if (incW > -1 && incH > -1)
1013 {
1014 sizeHints.flags |= PResizeInc;
1015 sizeHints.width_inc = incW;
1016 sizeHints.height_inc = incH;
1017 }
1018
1019 XSetWMNormalHints(wxGlobalDisplay(), (Window) m_mainWindow, &sizeHints );
1020 #endif
1021 }
1022
1023 // ---------------------------------------------------------------------------
1024 // text metrics
1025 // ---------------------------------------------------------------------------
1026
1027 int wxWindowX11::GetCharHeight() const
1028 {
1029 wxFont font(GetFont());
1030 wxCHECK_MSG( font.Ok(), 0, wxT("valid window font needed") );
1031
1032 #if wxUSE_UNICODE
1033 // There should be an easier way.
1034 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1035 pango_layout_set_font_description( layout, font.GetNativeFontInfo()->description );
1036 pango_layout_set_text(layout, "H", 1 );
1037 int w,h;
1038 pango_layout_get_pixel_size(layout, &w, &h);
1039 g_object_unref( G_OBJECT( layout ) );
1040
1041 return h;
1042 #else
1043 WXFontStructPtr pFontStruct = font.GetFontStruct(1.0, wxGlobalDisplay());
1044
1045 int direction, ascent, descent;
1046 XCharStruct overall;
1047 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1048 &descent, &overall);
1049
1050 // return (overall.ascent + overall.descent);
1051 return (ascent + descent);
1052 #endif
1053 }
1054
1055 int wxWindowX11::GetCharWidth() const
1056 {
1057 wxFont font(GetFont());
1058 wxCHECK_MSG( font.Ok(), 0, wxT("valid window font needed") );
1059
1060 #if wxUSE_UNICODE
1061 // There should be an easier way.
1062 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1063 pango_layout_set_font_description( layout, font.GetNativeFontInfo()->description );
1064 pango_layout_set_text(layout, "H", 1 );
1065 int w,h;
1066 pango_layout_get_pixel_size(layout, &w, &h);
1067 g_object_unref( G_OBJECT( layout ) );
1068
1069 return w;
1070 #else
1071 WXFontStructPtr pFontStruct = font.GetFontStruct(1.0, wxGlobalDisplay());
1072
1073 int direction, ascent, descent;
1074 XCharStruct overall;
1075 XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
1076 &descent, &overall);
1077
1078 return overall.width;
1079 #endif
1080 }
1081
1082 void wxWindowX11::DoGetTextExtent(const wxString& string,
1083 int *x, int *y,
1084 int *descent,
1085 int *externalLeading,
1086 const wxFont *theFont) const
1087 {
1088 wxFont fontToUse = GetFont();
1089 if (theFont) fontToUse = *theFont;
1090
1091 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
1092
1093 if (string.empty())
1094 {
1095 if (x) (*x) = 0;
1096 if (y) (*y) = 0;
1097 return;
1098 }
1099
1100 #if wxUSE_UNICODE
1101 PangoLayout *layout = pango_layout_new( wxTheApp->GetPangoContext() );
1102
1103 PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
1104 pango_layout_set_font_description(layout, desc);
1105
1106 const wxCharBuffer data = wxConvUTF8.cWC2MB( string );
1107 pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data ));
1108
1109 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
1110
1111
1112 PangoRectangle rect;
1113 pango_layout_line_get_extents(line, NULL, &rect);
1114
1115 if (x) (*x) = (wxCoord) (rect.width / PANGO_SCALE);
1116 if (y) (*y) = (wxCoord) (rect.height / PANGO_SCALE);
1117 if (descent)
1118 {
1119 // Do something about metrics here
1120 (*descent) = 0;
1121 }
1122 if (externalLeading) (*externalLeading) = 0; // ??
1123
1124 g_object_unref( G_OBJECT( layout ) );
1125 #else
1126 WXFontStructPtr pFontStruct = fontToUse.GetFontStruct(1.0, wxGlobalDisplay());
1127
1128 int direction, ascent, descent2;
1129 XCharStruct overall;
1130 int slen = string.length();
1131
1132 XTextExtents((XFontStruct*) pFontStruct, (const char*) string.c_str(), slen,
1133 &direction, &ascent, &descent2, &overall);
1134
1135 if ( x )
1136 *x = (overall.width);
1137 if ( y )
1138 *y = (ascent + descent2);
1139 if (descent)
1140 *descent = descent2;
1141 if (externalLeading)
1142 *externalLeading = 0;
1143 #endif
1144 }
1145
1146 // ----------------------------------------------------------------------------
1147 // painting
1148 // ----------------------------------------------------------------------------
1149
1150 void wxWindowX11::Refresh(bool eraseBack, const wxRect *rect)
1151 {
1152 if (eraseBack)
1153 {
1154 if (rect)
1155 {
1156 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1157 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
1158 }
1159 else
1160 {
1161 int height,width;
1162 GetSize( &width, &height );
1163
1164 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1165 m_clearRegion.Clear();
1166 m_clearRegion.Union( 0, 0, width, height );
1167 }
1168 }
1169
1170 if (rect)
1171 {
1172 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1173 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
1174 }
1175 else
1176 {
1177 int height,width;
1178 GetSize( &width, &height );
1179
1180 // Schedule for later Updating in ::Update() or ::OnInternalIdle().
1181 m_updateRegion.Clear();
1182 m_updateRegion.Union( 0, 0, width, height );
1183 }
1184 }
1185
1186 void wxWindowX11::Update()
1187 {
1188 if (m_updateNcArea)
1189 {
1190 // wxLogDebug("wxWindowX11::UpdateNC: %s", GetClassInfo()->GetClassName());
1191 // Send nc paint events.
1192 SendNcPaintEvents();
1193 }
1194
1195 if (!m_updateRegion.IsEmpty())
1196 {
1197 // wxLogDebug("wxWindowX11::Update: %s", GetClassInfo()->GetClassName());
1198 // Actually send erase events.
1199 SendEraseEvents();
1200
1201 // Actually send paint events.
1202 SendPaintEvents();
1203 }
1204 }
1205
1206 void wxWindowX11::SendEraseEvents()
1207 {
1208 if (m_clearRegion.IsEmpty()) return;
1209
1210 wxClientDC dc( (wxWindow*)this );
1211 dc.SetDeviceClippingRegion( m_clearRegion );
1212
1213 wxEraseEvent erase_event( GetId(), &dc );
1214 erase_event.SetEventObject( this );
1215
1216 if (!HandleWindowEvent(erase_event) )
1217 {
1218 Display *xdisplay = wxGlobalDisplay();
1219 Window xwindow = (Window) GetClientAreaWindow();
1220 XSetForeground( xdisplay, g_eraseGC, m_backgroundColour.GetPixel() );
1221
1222 wxRegionIterator upd( m_clearRegion );
1223 while (upd)
1224 {
1225 XFillRectangle( xdisplay, xwindow, g_eraseGC,
1226 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
1227 upd ++;
1228 }
1229 }
1230
1231 m_clearRegion.Clear();
1232 }
1233
1234 void wxWindowX11::SendPaintEvents()
1235 {
1236 // wxLogDebug("SendPaintEvents: %s (%ld)", GetClassInfo()->GetClassName(), GetId());
1237
1238 m_clipPaintRegion = true;
1239
1240 wxPaintEvent paint_event( GetId() );
1241 paint_event.SetEventObject( this );
1242 HandleWindowEvent( paint_event );
1243
1244 m_updateRegion.Clear();
1245
1246 m_clipPaintRegion = false;
1247 }
1248
1249 void wxWindowX11::SendNcPaintEvents()
1250 {
1251 wxWindow *window = (wxWindow*) this;
1252
1253 // All this for drawing the small square between the scrollbars.
1254 int width = 0;
1255 int height = 0;
1256 int x = 0;
1257 int y = 0;
1258 wxScrollBar *sb = window->GetScrollbar( wxHORIZONTAL );
1259 if (sb && sb->IsShown())
1260 {
1261 height = sb->GetSize().y;
1262 y = sb->GetPosition().y;
1263
1264 sb = window->GetScrollbar( wxVERTICAL );
1265 if (sb && sb->IsShown())
1266 {
1267 width = sb->GetSize().x;
1268 x = sb->GetPosition().x;
1269
1270 Display *xdisplay = wxGlobalDisplay();
1271 Window xwindow = (Window) X11GetMainWindow();
1272 Colormap cm = (Colormap) wxTheApp->GetMainColormap( wxGetDisplay() );
1273 wxColour colour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1274 colour.CalcPixel( (WXColormap) cm );
1275
1276 XSetForeground( xdisplay, g_eraseGC, colour.GetPixel() );
1277
1278 XFillRectangle( xdisplay, xwindow, g_eraseGC, x, y, width, height );
1279 }
1280 }
1281
1282 wxNcPaintEvent nc_paint_event( GetId() );
1283 nc_paint_event.SetEventObject( this );
1284 HandleWindowEvent( nc_paint_event );
1285
1286 m_updateNcArea = false;
1287 }
1288
1289 // ----------------------------------------------------------------------------
1290 // event handlers
1291 // ----------------------------------------------------------------------------
1292
1293 // Responds to colour changes: passes event on to children.
1294 void wxWindowX11::OnSysColourChanged(wxSysColourChangedEvent& event)
1295 {
1296 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1297 while ( node )
1298 {
1299 // Only propagate to non-top-level windows
1300 wxWindow *win = node->GetData();
1301 if ( win->GetParent() )
1302 {
1303 wxSysColourChangedEvent event2;
1304 event.SetEventObject(win);
1305 win->HandleWindowEvent(event2);
1306 }
1307
1308 node = node->GetNext();
1309 }
1310 }
1311
1312 // See handler for InFocus case in app.cpp for details.
1313 wxWindow* g_GettingFocus = NULL;
1314
1315 void wxWindowX11::OnInternalIdle()
1316 {
1317 // Update invalidated regions.
1318 Update();
1319
1320 // This calls the UI-update mechanism (querying windows for
1321 // menu/toolbar/control state information)
1322 if (wxUpdateUIEvent::CanUpdate((wxWindow*) this) && IsShownOnScreen())
1323 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
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_wheelDelta = WHEEL_DELTA;
1489
1490 // Button 4 means mousewheel up, 5 means down
1491 wxevent.m_wheelRotation = button == Button4 ? WHEEL_DELTA
1492 : -WHEEL_DELTA;
1493 }
1494
1495 // check for a double click
1496 // TODO: where can we get this value from?
1497 //long dclickTime = XtGetMultiClickTime(wxGlobalDisplay());
1498 long dclickTime = 200;
1499 long ts = wxevent.GetTimestamp();
1500
1501 int buttonLast = win->GetLastClickedButton();
1502 long lastTS = win->GetLastClickTime();
1503 if ( buttonLast && buttonLast == button && (ts - lastTS) < dclickTime )
1504 {
1505 // I have a dclick
1506 win->SetLastClick(0, ts);
1507 if ( eventType == wxEVT_LEFT_DOWN )
1508 eventType = wxEVT_LEFT_DCLICK;
1509 else if ( eventType == wxEVT_MIDDLE_DOWN )
1510 eventType = wxEVT_MIDDLE_DCLICK;
1511 else if ( eventType == wxEVT_RIGHT_DOWN )
1512 eventType = wxEVT_RIGHT_DCLICK;
1513 }
1514 else
1515 {
1516 // not fast enough or different button
1517 win->SetLastClick(button, ts);
1518 }
1519 }
1520 else if (XEventGetType(xevent) == ButtonRelease)
1521 {
1522 if (XButtonEventLChanged(xevent))
1523 {
1524 eventType = wxEVT_LEFT_UP;
1525 }
1526 else if (XButtonEventMChanged(xevent))
1527 {
1528 eventType = wxEVT_MIDDLE_UP;
1529 }
1530 else if (XButtonEventRChanged(xevent))
1531 {
1532 eventType = wxEVT_RIGHT_UP;
1533 }
1534 else return false;
1535 }
1536 else
1537 {
1538 return false;
1539 }
1540
1541 wxevent.SetEventType(eventType);
1542
1543 wxevent.m_x = XButtonEventGetX(xevent);
1544 wxevent.m_y = XButtonEventGetY(xevent);
1545
1546 wxevent.m_leftDown = ((eventType == wxEVT_LEFT_DOWN)
1547 || (XButtonEventLIsDown(xevent)
1548 && (eventType != wxEVT_LEFT_UP)));
1549 wxevent.m_middleDown = ((eventType == wxEVT_MIDDLE_DOWN)
1550 || (XButtonEventMIsDown(xevent)
1551 && (eventType != wxEVT_MIDDLE_UP)));
1552 wxevent.m_rightDown = ((eventType == wxEVT_RIGHT_DOWN)
1553 || (XButtonEventRIsDown (xevent)
1554 && (eventType != wxEVT_RIGHT_UP)));
1555
1556 wxevent.m_shiftDown = XButtonEventShiftIsDown(xevent);
1557 wxevent.m_controlDown = XButtonEventCtrlIsDown(xevent);
1558 wxevent.m_altDown = XButtonEventAltIsDown(xevent);
1559 wxevent.m_metaDown = XButtonEventMetaIsDown(xevent);
1560
1561 wxevent.SetId(win->GetId());
1562 wxevent.SetEventObject(win);
1563
1564 return true;
1565 }
1566 }
1567 return false;
1568 }
1569
1570 bool wxTranslateKeyEvent(wxKeyEvent& wxevent, wxWindow *win, Window WXUNUSED(win), XEvent *xevent, bool isAscii)
1571 {
1572 switch (XEventGetType(xevent))
1573 {
1574 case KeyPress:
1575 case KeyRelease:
1576 {
1577 char buf[20];
1578
1579 KeySym keySym;
1580 (void) XLookupString ((XKeyEvent *) xevent, buf, 20, &keySym, NULL);
1581 int id = wxCharCodeXToWX (keySym);
1582 // id may be WXK_xxx code - these are outside ASCII range, so we
1583 // can't just use toupper() on id.
1584 // Only change this if we want the raw key that was pressed,
1585 // and don't change it if we want an ASCII value.
1586 if (!isAscii && (id >= 'a' && id <= 'z'))
1587 {
1588 id = id + 'A' - 'a';
1589 }
1590
1591 wxevent.m_shiftDown = XKeyEventShiftIsDown(xevent);
1592 wxevent.m_controlDown = XKeyEventCtrlIsDown(xevent);
1593 wxevent.m_altDown = XKeyEventAltIsDown(xevent);
1594 wxevent.m_metaDown = XKeyEventMetaIsDown(xevent);
1595 wxevent.SetEventObject(win);
1596 wxevent.m_keyCode = id;
1597 wxevent.SetTimestamp(XKeyEventGetTime(xevent));
1598
1599 wxevent.m_x = XKeyEventGetX(xevent);
1600 wxevent.m_y = XKeyEventGetY(xevent);
1601
1602 return id > -1;
1603 }
1604 default:
1605 break;
1606 }
1607 return false;
1608 }
1609
1610 // ----------------------------------------------------------------------------
1611 // Colour stuff
1612 // ----------------------------------------------------------------------------
1613
1614 bool wxWindowX11::SetBackgroundColour(const wxColour& col)
1615 {
1616 wxWindowBase::SetBackgroundColour(col);
1617
1618 Display *xdisplay = (Display*) wxGlobalDisplay();
1619 int xscreen = DefaultScreen( xdisplay );
1620 Colormap cm = DefaultColormap( xdisplay, xscreen );
1621
1622 m_backgroundColour.CalcPixel( (WXColormap) cm );
1623
1624 // We don't set the background colour as we paint
1625 // the background ourselves.
1626 // XSetWindowBackground( xdisplay, (Window) m_clientWindow, m_backgroundColour.GetPixel() );
1627
1628 return true;
1629 }
1630
1631 bool wxWindowX11::SetForegroundColour(const wxColour& col)
1632 {
1633 if ( !wxWindowBase::SetForegroundColour(col) )
1634 return false;
1635
1636 return true;
1637 }
1638
1639 // ----------------------------------------------------------------------------
1640 // global functions
1641 // ----------------------------------------------------------------------------
1642
1643 wxWindow *wxGetActiveWindow()
1644 {
1645 return wxGetTopLevelParent(wxWindow::FindFocus());
1646 }
1647
1648 /* static */
1649 wxWindow *wxWindowBase::GetCapture()
1650 {
1651 return (wxWindow *)g_captureWindow;
1652 }
1653
1654
1655 // Find the wxWindow at the current mouse position, returning the mouse
1656 // position.
1657 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1658 {
1659 pt = wxGetMousePosition();
1660 return wxFindWindowAtPoint(pt);
1661 }
1662
1663 void wxGetMouseState(int& rootX, int& rootY, unsigned& maskReturn)
1664 {
1665 #if wxUSE_NANOX
1666 /* TODO */
1667 rootX = rootY = 0;
1668 maskReturn = 0;
1669 #else
1670 Display *display = wxGlobalDisplay();
1671 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
1672 Window rootReturn, childReturn;
1673 int winX, winY;
1674
1675 XQueryPointer (display,
1676 rootWindow,
1677 &rootReturn,
1678 &childReturn,
1679 &rootX, &rootY, &winX, &winY, &maskReturn);
1680 #endif
1681 }
1682
1683 // Get the current mouse position.
1684 wxPoint wxGetMousePosition()
1685 {
1686 int x, y;
1687 unsigned mask;
1688
1689 wxGetMouseState(x, y, mask);
1690 return wxPoint(x, y);
1691 }
1692
1693 wxMouseState wxGetMouseState()
1694 {
1695 wxMouseState ms;
1696 int x, y;
1697 unsigned mask;
1698
1699 wxGetMouseState(x, y, mask);
1700
1701 ms.SetX(x);
1702 ms.SetY(y);
1703
1704 ms.SetLeftDown(mask & Button1Mask);
1705 ms.SetMiddleDown(mask & Button2Mask);
1706 ms.SetRightDown(mask & Button3Mask);
1707
1708 ms.SetControlDown(mask & ControlMask);
1709 ms.SetShiftDown(mask & ShiftMask);
1710 ms.SetAltDown(mask & Mod3Mask);
1711 ms.SetMetaDown(mask & Mod1Mask);
1712
1713 return ms;
1714 }
1715
1716
1717 // ----------------------------------------------------------------------------
1718 // wxNoOptimize: switch off size optimization
1719 // ----------------------------------------------------------------------------
1720
1721 int wxNoOptimize::ms_count = 0;
1722
1723
1724 // ----------------------------------------------------------------------------
1725 // wxDCModule
1726 // ----------------------------------------------------------------------------
1727
1728 class wxWinModule : public wxModule
1729 {
1730 public:
1731 wxWinModule()
1732 {
1733 // we must be cleaned up before the display is closed
1734 AddDependency(wxClassInfo::FindClass(wxT("wxX11DisplayModule")));
1735 }
1736
1737 virtual bool OnInit();
1738 virtual void OnExit();
1739
1740 private:
1741 DECLARE_DYNAMIC_CLASS(wxWinModule)
1742 };
1743
1744 IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
1745
1746 bool wxWinModule::OnInit()
1747 {
1748 Display *xdisplay = wxGlobalDisplay();
1749 int xscreen = DefaultScreen( xdisplay );
1750 Window xroot = RootWindow( xdisplay, xscreen );
1751 g_eraseGC = XCreateGC( xdisplay, xroot, 0, NULL );
1752 XSetFillStyle( xdisplay, g_eraseGC, FillSolid );
1753
1754 return true;
1755 }
1756
1757 void wxWinModule::OnExit()
1758 {
1759 Display *xdisplay = wxGlobalDisplay();
1760 XFreeGC( xdisplay, g_eraseGC );
1761 }