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