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