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