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