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