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