]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/window.cpp
Fixed a '(' that should have been a '{'
[wxWidgets.git] / src / mgl / window.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
a4bbc9f7
VS
2// Name: src/mgl/window.cpp
3// Purpose: wxWindow
4// Author: Vaclav Slavik
5// (based on GTK & MSW implementations)
6// RCS-ID: $Id$
eea93267 7// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
e3a1e3e0 8// Licence: wxWindows licence
32b8ec41
VZ
9/////////////////////////////////////////////////////////////////////////////
10
a4bbc9f7
VS
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
32b8ec41 18
14f355c2 19#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
32b8ec41
VZ
20 #pragma implementation "window.h"
21#endif
22
a4bbc9f7
VS
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#ifndef WX_PRECOMP
31 #include "wx/window.h"
d53fc7c9 32 #include "wx/msgdlg.h"
a4bbc9f7
VS
33 #include "wx/accel.h"
34 #include "wx/setup.h"
35 #include "wx/dc.h"
36 #include "wx/dcclient.h"
37 #include "wx/utils.h"
38 #include "wx/app.h"
39 #include "wx/panel.h"
40 #include "wx/caret.h"
41#endif
42
43#if wxUSE_DRAG_AND_DROP
44 #include "wx/dnd.h"
45#endif
46
47#include "wx/log.h"
48#include "wx/sysopt.h"
49#include "wx/mgl/private.h"
50#include "wx/intl.h"
51#include "wx/dcscreen.h"
52
53#include <mgraph.hpp>
54
55#if wxUSE_TOOLTIPS
56 #include "wx/tooltip.h"
57#endif
58
59// ---------------------------------------------------------------------------
60// global variables
61// ---------------------------------------------------------------------------
62
63// MGL window manager and associated DC.
64winmng_t *g_winMng = NULL;
65MGLDevCtx *g_displayDC = NULL;
66
7de59551 67// the window that has keyboard focus:
7cdbf02c 68static wxWindowMGL *gs_focusedWindow = NULL;
7de59551 69// the window that is about to be focused after currently focused
eea93267
VS
70// one looses focus:
71static wxWindow *gs_toBeFocusedWindow = NULL;
7bdc1879 72// the window that is currently under mouse cursor:
7cdbf02c
VS
73static wxWindowMGL *gs_windowUnderMouse = NULL;
74// the window that has mouse capture
75static wxWindowMGL *gs_mouseCapture = NULL;
76// the frame that is currently active (i.e. its child has focus). It is
77// used to generate wxActivateEvents
78static wxWindowMGL *gs_activeFrame = NULL;
a4bbc9f7
VS
79
80// ---------------------------------------------------------------------------
81// constants
82// ---------------------------------------------------------------------------
83
7de59551 84// Custom identifiers used to distinguish between various event handlers
a4bbc9f7
VS
85// and capture handlers passed to MGL_wm
86enum
87{
88 wxMGL_CAPTURE_MOUSE = 1,
89 wxMGL_CAPTURE_KEYB = 2
90};
91
92
93// ---------------------------------------------------------------------------
94// private functions
95// ---------------------------------------------------------------------------
96
7cdbf02c
VS
97// Returns toplevel grandparent of given window:
98static wxWindowMGL* wxGetTopLevelParent(wxWindowMGL *win)
99{
100 wxWindowMGL *p = win;
101 while (p && !p->IsTopLevel())
102 p = p->GetParent();
103 return p;
104}
105
d53fc7c9 106// An easy way to capture screenshots:
2d1efe3f 107static void wxCaptureScreenshot(bool activeWindowOnly)
e6daf008 108{
d53fc7c9
VS
109#ifdef __DOS__
110 #define SCREENSHOT_FILENAME _T("sshot%03i.png")
111#else
112 #define SCREENSHOT_FILENAME _T("screenshot-%03i.png")
113#endif
e6daf008 114 static int screenshot_num = 0;
d53fc7c9 115 wxString screenshot;
7de59551
RD
116
117 do
d53fc7c9
VS
118 {
119 screenshot.Printf(SCREENSHOT_FILENAME, screenshot_num++);
120 } while ( wxFileExists(screenshot) && screenshot_num < 1000 );
7de59551 121
2d1efe3f
VS
122 wxRect r(0, 0, g_displayDC->sizex(), g_displayDC->sizey());
123
124 if ( activeWindowOnly && gs_activeFrame )
125 {
126 r.Intersect(gs_activeFrame->GetRect());
127 }
7de59551
RD
128
129 g_displayDC->savePNGFromDC(screenshot.mb_str(),
06534aa1 130 r.x, r. y, r.x+r.width, r.y+r.height);
7de59551 131
2b5f62a0
VZ
132 wxMessageBox(wxString::Format(_T("Screenshot captured: %s"),
133 screenshot.c_str()));
e6daf008 134}
e6daf008 135
7bdc1879
VS
136// ---------------------------------------------------------------------------
137// MGL_WM hooks:
138// ---------------------------------------------------------------------------
139
b43a9886 140static void MGLAPI wxWindowPainter(window_t *wnd, MGLDC *dc)
7bdc1879
VS
141{
142 wxWindowMGL *w = (wxWindow*) wnd->userData;
7de59551 143
544c782d 144 if ( w && !(w->GetWindowStyle() & wxTRANSPARENT_WINDOW) )
7bdc1879
VS
145 {
146 MGLDevCtx ctx(dc);
147 w->HandlePaint(&ctx);
148 }
ef344ff8
VS
149}
150
b43a9886 151static ibool MGLAPI wxWindowMouseHandler(window_t *wnd, event_t *e)
ef344ff8
VS
152{
153 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
d76048f5 154 wxPoint orig(win->GetClientAreaOrigin());
985520bc 155 wxPoint where;
7de59551
RD
156
157 MGL_wmCoordGlobalToLocal(win->GetHandle(),
985520bc
VS
158 e->where_x, e->where_y, &where.x, &where.y);
159
0a89ac9c
VS
160 for (wxWindowMGL *w = win; w; w = w->GetParent())
161 {
7de59551 162 if ( !w->IsEnabled() )
0a89ac9c
VS
163 return FALSE;
164 if ( w->IsTopLevel() )
165 break;
166 }
7de59551 167
ef344ff8
VS
168 wxEventType type = wxEVT_NULL;
169 wxMouseEvent event;
170 event.SetEventObject(win);
171 event.SetTimestamp(e->when);
d76048f5
VS
172 event.m_x = where.x - orig.x;
173 event.m_y = where.y - orig.y;
ef344ff8
VS
174 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
175 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
176 event.m_altDown = e->modifiers & EVT_LEFTALT;
177 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
178 event.m_leftDown = e->modifiers & EVT_LEFTBUT;
179 event.m_middleDown = e->modifiers & EVT_MIDDLEBUT;
180 event.m_rightDown = e->modifiers & EVT_RIGHTBUT;
7de59551 181
ef344ff8
VS
182 switch (e->what)
183 {
184 case EVT_MOUSEDOWN:
8f5e92db
VS
185 // Change focus if the user clicks outside focused window:
186 if ( win->AcceptsFocus() && wxWindow::FindFocus() != win )
187 win->SetFocus();
188
8f5e92db
VS
189 if ( e->message & EVT_DBLCLICK )
190 {
8f5e92db
VS
191 if ( e->message & EVT_LEFTBMASK )
192 type = wxEVT_LEFT_DCLICK;
193 else if ( e->message & EVT_MIDDLEBMASK )
194 type = wxEVT_MIDDLE_DCLICK;
195 else if ( e->message & EVT_RIGHTBMASK )
196 type = wxEVT_RIGHT_DCLICK;
197 }
ea98f686
VS
198 else
199 {
200 if ( e->message & EVT_LEFTBMASK )
201 type = wxEVT_LEFT_DOWN;
202 else if ( e->message & EVT_MIDDLEBMASK )
203 type = wxEVT_MIDDLE_DOWN;
204 else if ( e->message & EVT_RIGHTBMASK )
205 type = wxEVT_RIGHT_DOWN;
206 }
a679e129 207
ef344ff8
VS
208 break;
209
210 case EVT_MOUSEUP:
211 if ( e->message & EVT_LEFTBMASK )
212 type = wxEVT_LEFT_UP;
213 else if ( e->message & EVT_MIDDLEBMASK )
214 type = wxEVT_MIDDLE_UP;
215 else if ( e->message & EVT_RIGHTBMASK )
216 type = wxEVT_RIGHT_UP;
217 break;
218
219 case EVT_MOUSEMOVE:
7cdbf02c
VS
220 if ( !gs_mouseCapture )
221 {
222 if ( win != gs_windowUnderMouse )
223 {
224 if ( gs_windowUnderMouse )
225 {
226 wxMouseEvent event2(event);
7de59551
RD
227 MGL_wmCoordGlobalToLocal(gs_windowUnderMouse->GetHandle(),
228 e->where_x, e->where_y,
7cdbf02c
VS
229 &event2.m_x, &event2.m_y);
230
d76048f5
VS
231 wxPoint orig(gs_windowUnderMouse->GetClientAreaOrigin());
232 event2.m_x -= orig.x;
233 event2.m_y -= orig.y;
234
7cdbf02c
VS
235 event2.SetEventObject(gs_windowUnderMouse);
236 event2.SetEventType(wxEVT_LEAVE_WINDOW);
237 gs_windowUnderMouse->GetEventHandler()->ProcessEvent(event2);
238 }
239
240 wxMouseEvent event3(event);
241 event3.SetEventType(wxEVT_ENTER_WINDOW);
242 win->GetEventHandler()->ProcessEvent(event3);
243
244 gs_windowUnderMouse = win;
245 }
246 }
247 else // gs_mouseCapture
ef344ff8 248 {
7de59551 249 bool inside = (where.x >= 0 &&
7cdbf02c
VS
250 where.y >= 0 &&
251 where.x < win->GetSize().x &&
252 where.y < win->GetSize().y);
253 if ( (inside && gs_windowUnderMouse != win) ||
254 (!inside && gs_windowUnderMouse == win) )
ef344ff8 255 {
7de59551 256 wxMouseEvent evt(inside ?
7cdbf02c
VS
257 wxEVT_ENTER_WINDOW : wxEVT_LEAVE_WINDOW);
258 evt.SetEventObject(win);
259 win->GetEventHandler()->ProcessEvent(evt);
260 gs_windowUnderMouse = inside ? win : NULL;
ef344ff8 261 }
ef344ff8 262 }
7de59551 263
ef344ff8
VS
264 type = wxEVT_MOTION;
265 break;
266
267 default:
268 break;
269 }
7de59551 270
ef344ff8
VS
271 if ( type == wxEVT_NULL )
272 {
273 return FALSE;
274 }
275 else
276 {
277 event.SetEventType(type);
278 return win->GetEventHandler()->ProcessEvent(event);
279 }
280}
281
a679e129 282static long wxScanToKeyCode(event_t *event, bool translate)
ef344ff8 283{
84c88a17
VS
284 // VS: make it __WXDEBUG__-only, since we have lots of wxLogTrace calls
285 // here and the arguments would be stored in non-debug executable even
286 // though wxLogTrace would be no-op...
287 #ifdef __WXDEBUG__
288 #define KEY(mgl_key,wx_key) \
289 case mgl_key: \
290 wxLogTrace(_T("keyevents"), \
291 _T("key " #mgl_key ", mapped to " #wx_key)); \
292 key = wx_key; \
293 break;
294 #else
295 #define KEY(mgl_key,wx_key) \
7de59551 296 case mgl_key: key = wx_key; break;
84c88a17 297 #endif
2b260bc3
VS
298
299 long key = 0;
7de59551 300
a679e129 301 if ( translate )
2b260bc3 302 {
a679e129
VS
303 switch ( EVT_scanCode(event->message) )
304 {
305 KEY (KB_padMinus, WXK_NUMPAD_SUBTRACT)
306 KEY (KB_padPlus, WXK_NUMPAD_ADD)
307 KEY (KB_padTimes, WXK_NUMPAD_MULTIPLY)
308 KEY (KB_padDivide, WXK_NUMPAD_DIVIDE)
309 KEY (KB_padCenter, WXK_NUMPAD_SEPARATOR) // ?
310 KEY (KB_padLeft, WXK_NUMPAD_LEFT)
311 KEY (KB_padRight, WXK_NUMPAD_RIGHT)
312 KEY (KB_padUp, WXK_NUMPAD_UP)
313 KEY (KB_padDown, WXK_NUMPAD_DOWN)
314 KEY (KB_padInsert, WXK_NUMPAD_INSERT)
315 KEY (KB_padDelete, WXK_NUMPAD_DELETE)
316 KEY (KB_padHome, WXK_NUMPAD_HOME)
317 KEY (KB_padEnd, WXK_NUMPAD_END)
318 KEY (KB_padPageUp, WXK_NUMPAD_PAGEUP)
319 //KEY (KB_padPageUp, WXK_NUMPAD_PRIOR)
320 KEY (KB_padPageDown, WXK_NUMPAD_PAGEDOWN)
321 //KEY (KB_padPageDown, WXK_NUMPAD_NEXT)
322 KEY (KB_1, '1')
323 KEY (KB_2, '2')
324 KEY (KB_3, '3')
325 KEY (KB_4, '4')
326 KEY (KB_5, '5')
327 KEY (KB_6, '6')
328 KEY (KB_7, '7')
329 KEY (KB_8, '8')
330 KEY (KB_9, '9')
331 KEY (KB_0, '0')
332 KEY (KB_minus, WXK_SUBTRACT)
333 KEY (KB_equals, WXK_ADD)
334 KEY (KB_backSlash, '\\')
335 KEY (KB_Q, 'Q')
336 KEY (KB_W, 'W')
337 KEY (KB_E, 'E')
338 KEY (KB_R, 'R')
339 KEY (KB_T, 'T')
340 KEY (KB_Y, 'Y')
341 KEY (KB_U, 'U')
342 KEY (KB_I, 'I')
343 KEY (KB_O, 'O')
344 KEY (KB_P, 'P')
345 KEY (KB_leftSquareBrace,'[')
346 KEY (KB_rightSquareBrace,']')
347 KEY (KB_A, 'A')
348 KEY (KB_S, 'S')
349 KEY (KB_D, 'D')
350 KEY (KB_F, 'F')
351 KEY (KB_G, 'G')
352 KEY (KB_H, 'H')
353 KEY (KB_J, 'J')
354 KEY (KB_K, 'K')
355 KEY (KB_L, 'L')
356 KEY (KB_semicolon, ';')
357 KEY (KB_apostrophe, '\'')
358 KEY (KB_Z, 'Z')
359 KEY (KB_X, 'X')
360 KEY (KB_C, 'C')
361 KEY (KB_V, 'V')
362 KEY (KB_B, 'B')
363 KEY (KB_N, 'N')
364 KEY (KB_M, 'M')
365 KEY (KB_comma, ',')
366 KEY (KB_period, '.')
367 KEY (KB_divide, WXK_DIVIDE)
368 KEY (KB_space, WXK_SPACE)
369 KEY (KB_tilde, '~')
370
371 default: break;
372 }
373 }
7de59551 374
a679e129
VS
375 if ( key == 0 )
376 {
377 switch ( EVT_scanCode(event->message) )
378 {
379 KEY (KB_padEnter, WXK_NUMPAD_ENTER)
380 KEY (KB_F1, WXK_F1)
381 KEY (KB_F2, WXK_F2)
382 KEY (KB_F3, WXK_F3)
383 KEY (KB_F4, WXK_F4)
384 KEY (KB_F5, WXK_F5)
385 KEY (KB_F6, WXK_F6)
386 KEY (KB_F7, WXK_F7)
387 KEY (KB_F8, WXK_F8)
388 KEY (KB_F9, WXK_F9)
389 KEY (KB_F10, WXK_F10)
390 KEY (KB_F11, WXK_F11)
391 KEY (KB_F12, WXK_F12)
392 KEY (KB_left, WXK_LEFT)
393 KEY (KB_right, WXK_RIGHT)
394 KEY (KB_up, WXK_UP)
395 KEY (KB_down, WXK_DOWN)
396 KEY (KB_insert, WXK_INSERT)
397 KEY (KB_delete, WXK_DELETE)
398 KEY (KB_home, WXK_HOME)
399 KEY (KB_end, WXK_END)
400 KEY (KB_pageUp, WXK_PAGEUP)
401 KEY (KB_pageDown, WXK_PAGEDOWN)
402 KEY (KB_capsLock, WXK_CAPITAL)
403 KEY (KB_numLock, WXK_NUMLOCK)
404 KEY (KB_scrollLock, WXK_SCROLL)
405 KEY (KB_leftShift, WXK_SHIFT)
406 KEY (KB_rightShift, WXK_SHIFT)
407 KEY (KB_leftCtrl, WXK_CONTROL)
408 KEY (KB_rightCtrl, WXK_CONTROL)
409 KEY (KB_leftAlt, WXK_ALT)
410 KEY (KB_rightAlt, WXK_ALT)
411 KEY (KB_leftWindows, WXK_START)
412 KEY (KB_rightWindows, WXK_START)
413 KEY (KB_menu, WXK_MENU)
414 KEY (KB_sysReq, WXK_SNAPSHOT)
415 KEY (KB_esc, WXK_ESCAPE)
416 KEY (KB_backspace, WXK_BACK)
417 KEY (KB_tab, WXK_TAB)
7de59551 418 KEY (KB_enter, WXK_RETURN)
a679e129 419
7de59551
RD
420 default:
421 key = EVT_asciiCode(event->message);
a679e129
VS
422 break;
423 }
2b260bc3
VS
424 }
425
544c782d
VS
426 #undef KEY
427
2b260bc3 428 return key;
ef344ff8
VS
429}
430
d53fc7c9
VS
431static bool wxHandleSpecialKeys(wxKeyEvent& event)
432{
433 // Add an easy way to capture screenshots:
434 if ( event.m_keyCode == WXK_SNAPSHOT
435 #ifdef __WXDEBUG__ // FIXME_MGL - remove when KB_sysReq works in MGL!
7de59551
RD
436 || (event.m_keyCode == WXK_F1 &&
437 event.m_shiftDown && event.m_controlDown)
d53fc7c9 438 #endif
197b1626 439 )
d53fc7c9 440 {
2d1efe3f 441 wxCaptureScreenshot(event.m_altDown/*only active wnd?*/);
d53fc7c9
VS
442 return TRUE;
443 }
7de59551 444
d53fc7c9
VS
445 return FALSE;
446}
447
b43a9886 448static ibool MGLAPI wxWindowKeybHandler(window_t *wnd, event_t *e)
2b260bc3 449{
2b260bc3 450 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
a200c35e
VS
451
452 if ( !win->IsEnabled() ) return FALSE;
453
985520bc 454 wxPoint where;
7de59551 455 MGL_wmCoordGlobalToLocal(win->GetHandle(),
985520bc 456 e->where_x, e->where_y, &where.x, &where.y);
7de59551 457
2b260bc3
VS
458 wxKeyEvent event;
459 event.SetEventObject(win);
460 event.SetTimestamp(e->when);
a679e129 461 event.m_keyCode = wxScanToKeyCode(e, TRUE);
2b260bc3
VS
462 event.m_scanCode = 0; // not used by wx at all
463 event.m_x = where.x;
464 event.m_y = where.y;
2b260bc3
VS
465 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
466 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
467 event.m_altDown = e->modifiers & EVT_LEFTALT;
468 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
469
470 if ( e->what == EVT_KEYUP )
471 {
472 event.SetEventType(wxEVT_KEY_UP);
473 return win->GetEventHandler()->ProcessEvent(event);
474 }
475 else
476 {
477 bool ret;
478 wxKeyEvent event2;
479
480 event.SetEventType(wxEVT_KEY_DOWN);
481 event2 = event;
7de59551 482
2b260bc3
VS
483 ret = win->GetEventHandler()->ProcessEvent(event);
484
84c88a17
VS
485 // wxMSW doesn't send char events with Alt pressed
486 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
d53fc7c9 487 // will only be sent if it is not in an accelerator table:
a679e129 488 event2.m_keyCode = wxScanToKeyCode(e, FALSE);
2b260bc3
VS
489 if ( !ret && event2.m_keyCode != 0 )
490 {
491 event2.SetEventType(wxEVT_CHAR);
492 ret = win->GetEventHandler()->ProcessEvent(event2);
493 }
7de59551 494
84c88a17 495 // Synthetize navigation key event, but do it only if the TAB key
d53fc7c9 496 // wasn't handled yet:
84c88a17
VS
497 if ( !ret && event.m_keyCode == WXK_TAB &&
498 win->GetParent() && win->GetParent()->HasFlag(wxTAB_TRAVERSAL) )
499 {
500 wxNavigationKeyEvent navEvent;
501 navEvent.SetEventObject(win->GetParent());
502 // Shift-TAB goes in reverse direction:
503 navEvent.SetDirection(!event.m_shiftDown);
504 // Ctrl-TAB changes the (parent) window, i.e. switch notebook page:
505 navEvent.SetWindowChange(event.m_controlDown);
506 navEvent.SetCurrentFocus(wxStaticCast(win, wxWindow));
507 ret = win->GetParent()->GetEventHandler()->ProcessEvent(navEvent);
508 }
7de59551
RD
509
510 // Finally, process special meaning keys that are usually
d53fc7c9
VS
511 // a responsibility of OS or window manager:
512 if ( !ret )
513 ret = wxHandleSpecialKeys(event);
7de59551 514
2b260bc3
VS
515 return ret;
516 }
7bdc1879 517}
a4bbc9f7
VS
518
519// ---------------------------------------------------------------------------
520// event tables
521// ---------------------------------------------------------------------------
522
ef344ff8 523// in wxUniv this class is abstract because it doesn't have DoPopupMenu()
a4bbc9f7
VS
524IMPLEMENT_ABSTRACT_CLASS(wxWindowMGL, wxWindowBase)
525
526BEGIN_EVENT_TABLE(wxWindowMGL, wxWindowBase)
a4bbc9f7
VS
527END_EVENT_TABLE()
528
529// ===========================================================================
530// implementation
531// ===========================================================================
532
533// ----------------------------------------------------------------------------
534// constructors and such
535// ----------------------------------------------------------------------------
536
1c53456f 537extern wxVideoMode wxGetDefaultDisplayMode();
a3e76614 538
a4bbc9f7
VS
539void wxWindowMGL::Init()
540{
1f43b5c9
VS
541 // First of all, make sure window manager is up and running. If it is
542 // not the case, initialize it in default display mode
543 if ( !g_winMng )
7b9f73d2
VS
544 {
545 if ( !wxTheApp->SetDisplayMode(wxGetDefaultDisplayMode()) )
a1f7eb20 546 wxLogFatalError(_("Cannot initialize display."));
7b9f73d2 547 }
1f43b5c9 548
a4bbc9f7 549 // mgl specific:
a4bbc9f7
VS
550 m_wnd = NULL;
551 m_isShown = TRUE;
a4bbc9f7
VS
552 m_frozen = FALSE;
553 m_paintMGLDC = NULL;
2b260bc3 554 m_eraseBackground = -1;
a4bbc9f7
VS
555}
556
557// Destructor
558wxWindowMGL::~wxWindowMGL()
559{
7de59551
RD
560 SendDestroyEvent();
561
a4bbc9f7
VS
562 m_isBeingDeleted = TRUE;
563
7cdbf02c
VS
564 if ( gs_mouseCapture == this )
565 ReleaseMouse();
d46330c5 566
7cdbf02c 567 if (gs_activeFrame == this)
d46330c5 568 {
66858adf
VS
569 gs_activeFrame = NULL;
570 // activate next frame in Z-order:
571 if ( m_wnd->prev )
572 {
573 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
574 win->SetFocus();
575 }
576 else if ( m_wnd->next )
577 {
578 wxWindowMGL *win = (wxWindowMGL*)m_wnd->next->userData;
579 win->SetFocus();
d46330c5 580 }
d46330c5 581 }
7de59551 582
7cdbf02c 583 if ( gs_focusedWindow == this )
7bdc1879 584 KillFocus();
d46330c5 585
7cdbf02c
VS
586 if ( gs_windowUnderMouse == this )
587 gs_windowUnderMouse = NULL;
a4bbc9f7 588
a4bbc9f7
VS
589 DestroyChildren();
590
a4bbc9f7
VS
591 if ( m_wnd )
592 MGL_wmDestroyWindow(m_wnd);
593}
594
595// real construction (Init() must have been called before!)
596bool wxWindowMGL::Create(wxWindow *parent,
597 wxWindowID id,
598 const wxPoint& pos,
599 const wxSize& size,
600 long style,
601 const wxString& name)
602{
a4bbc9f7
VS
603 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
604 return FALSE;
605
544c782d 606 if ( parent )
7bdc1879 607 parent->AddChild(this);
a4bbc9f7 608
ef344ff8
VS
609 int x, y, w, h;
610 x = pos.x, y = pos.y;
611 if ( x == -1 )
612 x = 0; // FIXME_MGL, something better, see GTK+
613 if ( y == -1 )
614 y = 0; // FIXME_MGL, something better, see GTK+
82186005 615 AdjustForParentClientOrigin(x, y, 0);
ef344ff8
VS
616 w = WidthDefault(size.x);
617 h = HeightDefault(size.y);
7de59551 618
72fa862b 619 long mgl_style = 0;
7cdbf02c 620 window_t *wnd_parent = parent ? parent->GetHandle() : NULL;
544c782d 621
e441e1f4 622 if ( style & wxFULL_REPAINT_ON_RESIZE )
544c782d 623 {
72fa862b 624 mgl_style |= MGL_WM_FULL_REPAINT_ON_RESIZE;
544c782d 625 }
72fa862b 626 if ( style & wxSTAY_ON_TOP )
544c782d 627 {
72fa862b 628 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
629 }
630 if ( style & wxPOPUP_WINDOW )
631 {
7cdbf02c 632 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
633 // it is created hidden as other top level windows
634 m_isShown = FALSE;
7cdbf02c 635 wnd_parent = NULL;
544c782d 636 }
ef344ff8 637
58061670
VS
638 window_t *wnd = MGL_wmCreateWindow(g_winMng, wnd_parent, x, y, w, h);
639
640 MGL_wmSetWindowFlags(wnd, mgl_style);
641 MGL_wmShowWindow(wnd, m_isShown);
7de59551 642
58061670
VS
643 SetMGLwindow_t(wnd);
644
645 return TRUE;
646}
647
648void wxWindowMGL::SetMGLwindow_t(struct window_t *wnd)
649{
650 if ( m_wnd )
651 MGL_wmDestroyWindow(m_wnd);
652
653 m_wnd = wnd;
654 if ( !m_wnd ) return;
7de59551 655
58061670 656 m_isShown = m_wnd->visible;
ef344ff8 657
a4bbc9f7
VS
658 MGL_wmSetWindowUserData(m_wnd, (void*) this);
659 MGL_wmSetWindowPainter(m_wnd, wxWindowPainter);
ef344ff8
VS
660 MGL_wmPushWindowEventHandler(m_wnd, wxWindowMouseHandler, EVT_MOUSEEVT, 0);
661 MGL_wmPushWindowEventHandler(m_wnd, wxWindowKeybHandler, EVT_KEYEVT, 0);
58061670
VS
662
663 if ( m_cursor.Ok() )
664 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
665 else
666 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
a4bbc9f7
VS
667}
668
669// ---------------------------------------------------------------------------
670// basic operations
671// ---------------------------------------------------------------------------
672
673void wxWindowMGL::SetFocus()
674{
a46935cb
VS
675 if ( gs_focusedWindow == this ) return;
676
eea93267
VS
677 wxWindowMGL *oldFocusedWindow = gs_focusedWindow;
678
7cdbf02c 679 if ( gs_focusedWindow )
eea93267
VS
680 {
681 gs_toBeFocusedWindow = (wxWindow*)this;
7cdbf02c 682 gs_focusedWindow->KillFocus();
eea93267
VS
683 gs_toBeFocusedWindow = NULL;
684 }
7de59551 685
7cdbf02c 686 gs_focusedWindow = this;
7de59551 687
2b260bc3 688 MGL_wmCaptureEvents(GetHandle(), EVT_KEYEVT, wxMGL_CAPTURE_KEYB);
a4bbc9f7 689
7cdbf02c 690 wxWindowMGL *active = wxGetTopLevelParent(this);
61bd618f 691 if ( !(m_windowStyle & wxPOPUP_WINDOW) && active != gs_activeFrame )
a4bbc9f7 692 {
7cdbf02c
VS
693 if ( gs_activeFrame )
694 {
695 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, gs_activeFrame->GetId());
696 event.SetEventObject(gs_activeFrame);
697 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
698 }
699
700 gs_activeFrame = active;
701 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, gs_activeFrame->GetId());
702 event.SetEventObject(gs_activeFrame);
703 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
a4bbc9f7 704 }
7de59551 705
a4bbc9f7
VS
706 wxFocusEvent event(wxEVT_SET_FOCUS, GetId());
707 event.SetEventObject(this);
eea93267 708 event.SetWindow((wxWindow*)oldFocusedWindow);
a46935cb 709 GetEventHandler()->ProcessEvent(event);
869dabf8
VS
710
711#if wxUSE_CARET
712 // caret needs to be informed about focus change
713 wxCaret *caret = GetCaret();
714 if ( caret )
715 caret->OnSetFocus();
716#endif // wxUSE_CARET
a4bbc9f7 717}
32b8ec41 718
a4bbc9f7 719void wxWindowMGL::KillFocus()
32b8ec41 720{
7cdbf02c
VS
721 if ( gs_focusedWindow != this ) return;
722 gs_focusedWindow = NULL;
a4bbc9f7 723
7bdc1879 724 if ( m_isBeingDeleted ) return;
7de59551 725
a4bbc9f7
VS
726 MGL_wmUncaptureEvents(GetHandle(), wxMGL_CAPTURE_KEYB);
727
728#if wxUSE_CARET
729 // caret needs to be informed about focus change
730 wxCaret *caret = GetCaret();
7bdc1879 731 if ( caret )
a4bbc9f7
VS
732 caret->OnKillFocus();
733#endif // wxUSE_CARET
734
a4bbc9f7
VS
735 wxFocusEvent event(wxEVT_KILL_FOCUS, GetId());
736 event.SetEventObject(this);
eea93267 737 event.SetWindow(gs_toBeFocusedWindow);
a46935cb 738 GetEventHandler()->ProcessEvent(event);
32b8ec41
VZ
739}
740
a4bbc9f7
VS
741// ----------------------------------------------------------------------------
742// this wxWindowBase function is implemented here (in platform-specific file)
743// because it is static and so couldn't be made virtual
744// ----------------------------------------------------------------------------
32b8ec41
VZ
745wxWindow *wxWindowBase::FindFocus()
746{
7cdbf02c 747 return (wxWindow*)gs_focusedWindow;
a4bbc9f7
VS
748}
749
750bool wxWindowMGL::Show(bool show)
751{
752 if ( !wxWindowBase::Show(show) )
753 return FALSE;
754
755 MGL_wmShowWindow(m_wnd, show);
7de59551 756
ead60ec5
VS
757 if (!show && gs_activeFrame == this)
758 {
66858adf
VS
759 // activate next frame in Z-order:
760 if ( m_wnd->prev )
761 {
762 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
763 win->SetFocus();
764 }
765 else if ( m_wnd->next )
766 {
767 wxWindowMGL *win = (wxWindowMGL*)m_wnd->next->userData;
768 win->SetFocus();
769 }
770 else
771 {
772 gs_activeFrame = NULL;
ead60ec5
VS
773 }
774 }
775
a4bbc9f7
VS
776 return TRUE;
777}
778
779// Raise the window to the top of the Z order
780void wxWindowMGL::Raise()
781{
782 MGL_wmRaiseWindow(m_wnd);
783}
784
785// Lower the window to the bottom of the Z order
786void wxWindowMGL::Lower()
787{
788 MGL_wmLowerWindow(m_wnd);
789}
790
4116c221 791void wxWindowMGL::DoCaptureMouse()
a4bbc9f7 792{
7cdbf02c
VS
793 if ( gs_mouseCapture )
794 MGL_wmUncaptureEvents(gs_mouseCapture->m_wnd, wxMGL_CAPTURE_MOUSE);
795
796 gs_mouseCapture = this;
a4bbc9f7
VS
797 MGL_wmCaptureEvents(m_wnd, EVT_MOUSEEVT, wxMGL_CAPTURE_MOUSE);
798}
799
4116c221 800void wxWindowMGL::DoReleaseMouse()
a4bbc9f7 801{
63fd5f0b 802 wxASSERT_MSG( gs_mouseCapture == this, wxT("attempt to release mouse, but this window hasn't captured it") );
7de59551 803
a4bbc9f7 804 MGL_wmUncaptureEvents(m_wnd, wxMGL_CAPTURE_MOUSE);
7cdbf02c 805 gs_mouseCapture = NULL;
a4bbc9f7
VS
806}
807
808/* static */ wxWindow *wxWindowBase::GetCapture()
809{
7cdbf02c 810 return (wxWindow*)gs_mouseCapture;
a4bbc9f7
VS
811}
812
813bool wxWindowMGL::SetCursor(const wxCursor& cursor)
814{
815 if ( !wxWindowBase::SetCursor(cursor) )
816 {
817 // no change
818 return FALSE;
819 }
820
821 if ( m_cursor.Ok() )
822 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
ef344ff8
VS
823 else
824 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
a4bbc9f7
VS
825
826 return TRUE;
827}
828
829void wxWindowMGL::WarpPointer(int x, int y)
830{
8014db41
VS
831 int w, h;
832 wxDisplaySize(&w, &h);
833
a4bbc9f7 834 ClientToScreen(&x, &y);
8014db41
VS
835 if ( x < 0 )
836 x = 0;
837 if ( y < 0 )
838 y = 0;
839 if ( x >= w )
840 x = w-1;
841 if ( y >= h )
842 y = h-1;
7de59551 843
a4bbc9f7
VS
844 EVT_setMousePos(x, y);
845}
846
a4bbc9f7
VS
847// Set this window to be the child of 'parent'.
848bool wxWindowMGL::Reparent(wxWindowBase *parent)
849{
850 if ( !wxWindowBase::Reparent(parent) )
851 return FALSE;
852
853 MGL_wmReparentWindow(m_wnd, parent->GetHandle());
854
855 return TRUE;
856}
857
858
859// ---------------------------------------------------------------------------
860// drag and drop
861// ---------------------------------------------------------------------------
862
863#if wxUSE_DRAG_AND_DROP
864
865void wxWindowMGL::SetDropTarget(wxDropTarget *pDropTarget)
866{
867 if ( m_dropTarget != 0 ) {
868 m_dropTarget->Revoke(m_hWnd);
869 delete m_dropTarget;
870 }
871
872 m_dropTarget = pDropTarget;
873 if ( m_dropTarget != 0 )
874 m_dropTarget->Register(m_hWnd);
875}
876// FIXME_MGL
877#endif // wxUSE_DRAG_AND_DROP
878
879// old style file-manager drag&drop support: we retain the old-style
880// DragAcceptFiles in parallel with SetDropTarget.
881void wxWindowMGL::DragAcceptFiles(bool accept)
882{
883#if 0 // FIXME_MGL
884 HWND hWnd = GetHwnd();
885 if ( hWnd )
886 ::DragAcceptFiles(hWnd, (BOOL)accept);
887#endif
888}
889
890// ---------------------------------------------------------------------------
891// moving and resizing
892// ---------------------------------------------------------------------------
893
894// Get total size
895void wxWindowMGL::DoGetSize(int *x, int *y) const
896{
ea98f686 897 wxASSERT_MSG( m_wnd, wxT("invalid window") );
7de59551 898
a4bbc9f7
VS
899 if (x) *x = m_wnd->width;
900 if (y) *y = m_wnd->height;
901}
902
903void wxWindowMGL::DoGetPosition(int *x, int *y) const
904{
ea98f686
VS
905 wxASSERT_MSG( m_wnd, wxT("invalid window") );
906
907 int pX = 0, pY = 0;
7de59551 908 AdjustForParentClientOrigin(pX, pY, 0);
e41dcea5 909
ea98f686
VS
910 if (x) *x = m_wnd->x - pX;
911 if (y) *y = m_wnd->y - pY;
a4bbc9f7
VS
912}
913
914void wxWindowMGL::DoScreenToClient(int *x, int *y) const
915{
916 int ax, ay;
985520bc 917 MGL_wmCoordGlobalToLocal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 918 if (x)
985520bc 919 (*x) += ax;
a4bbc9f7 920 if (y)
985520bc 921 (*y) += ay;
a4bbc9f7
VS
922}
923
924void wxWindowMGL::DoClientToScreen(int *x, int *y) const
925{
926 int ax, ay;
985520bc 927 MGL_wmCoordLocalToGlobal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 928 if (x)
985520bc 929 (*x) += ax;
a4bbc9f7 930 if (y)
985520bc 931 (*y) += ay;
a4bbc9f7
VS
932}
933
934// Get size *available for subwindows* i.e. excluding menu bar etc.
935void wxWindowMGL::DoGetClientSize(int *x, int *y) const
936{
937 DoGetSize(x, y);
938}
939
940void wxWindowMGL::DoMoveWindow(int x, int y, int width, int height)
941{
942 MGL_wmSetWindowPosition(GetHandle(), x, y, width, height);
943}
944
945// set the size of the window: if the dimensions are positive, just use them,
946// but if any of them is equal to -1, it means that we must find the value for
947// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
948// which case -1 is a valid value for x and y)
949//
950// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
951// the width/height to best suit our contents, otherwise we reuse the current
952// width/height
953void wxWindowMGL::DoSetSize(int x, int y, int width, int height, int sizeFlags)
954{
955 // get the current size and position...
956 int currentX, currentY;
957 GetPosition(&currentX, &currentY);
958 int currentW,currentH;
959 GetSize(&currentW, &currentH);
960
961 // ... and don't do anything (avoiding flicker) if it's already ok
962 if ( x == currentX && y == currentY &&
963 width == currentW && height == currentH )
964 {
965 return;
966 }
967
968 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
969 x = currentX;
970 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
971 y = currentY;
972
a4bbc9f7 973 AdjustForParentClientOrigin(x, y, sizeFlags);
a4bbc9f7
VS
974
975 wxSize size(-1, -1);
976 if ( width == -1 )
977 {
978 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
979 {
980 size = DoGetBestSize();
981 width = size.x;
982 }
983 else
984 {
985 // just take the current one
986 width = currentW;
987 }
988 }
7de59551 989
a4bbc9f7
VS
990 if ( height == -1 )
991 {
992 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
993 {
994 if ( size.x == -1 )
995 {
996 size = DoGetBestSize();
997 }
998 //else: already called DoGetBestSize() above
999
1000 height = size.y;
1001 }
1002 else
1003 {
1004 // just take the current one
1005 height = currentH;
1006 }
1007 }
7de59551 1008
6feddea3 1009 int maxWidth = GetMaxWidth(),
7de59551 1010 minWidth = GetMinWidth(),
6feddea3
VS
1011 maxHeight = GetMaxHeight(),
1012 minHeight = GetMinHeight();
7de59551 1013
6feddea3
VS
1014 if ( minWidth != -1 && width < minWidth ) width = minWidth;
1015 if ( maxWidth != -1 && width > maxWidth ) width = maxWidth;
1016 if ( minHeight != -1 && height < minHeight ) height = minHeight;
1017 if ( maxHeight != -1 && height > maxHeight ) height = maxHeight;
a4bbc9f7 1018
7de59551 1019 if ( m_wnd->x != x || m_wnd->y != y ||
88f2a771
VS
1020 (int)m_wnd->width != width || (int)m_wnd->height != height )
1021 {
1022 DoMoveWindow(x, y, width, height);
a4bbc9f7 1023
88f2a771
VS
1024 wxSizeEvent event(wxSize(width, height), GetId());
1025 event.SetEventObject(this);
1026 GetEventHandler()->ProcessEvent(event);
1027 }
a4bbc9f7
VS
1028}
1029
1030void wxWindowMGL::DoSetClientSize(int width, int height)
1031{
1032 SetSize(width, height);
1033}
1034
1035// ---------------------------------------------------------------------------
1036// text metrics
1037// ---------------------------------------------------------------------------
1038
1039int wxWindowMGL::GetCharHeight() const
1040{
1041 wxScreenDC dc;
1042 dc.SetFont(m_font);
1043 return dc.GetCharHeight();
1044}
1045
1046int wxWindowMGL::GetCharWidth() const
1047{
1048 wxScreenDC dc;
1049 dc.SetFont(m_font);
1050 return dc.GetCharWidth();
1051}
1052
1053void wxWindowMGL::GetTextExtent(const wxString& string,
1054 int *x, int *y,
1055 int *descent, int *externalLeading,
1056 const wxFont *theFont) const
1057{
1058 wxScreenDC dc;
1059 if (!theFont)
1060 theFont = &m_font;
1061 dc.GetTextExtent(string, x, y, descent, externalLeading, (wxFont*)theFont);
1062}
1063
a4bbc9f7 1064
a4bbc9f7
VS
1065// ---------------------------------------------------------------------------
1066// painting
1067// ---------------------------------------------------------------------------
1068
1069void wxWindowMGL::Clear()
1070{
1071 wxClientDC dc((wxWindow *)this);
1072 wxBrush brush(GetBackgroundColour(), wxSOLID);
1073 dc.SetBackground(brush);
1074 dc.Clear();
1075}
1076
61bd618f 1077#include "wx/menu.h"
2b260bc3 1078void wxWindowMGL::Refresh(bool eraseBack, const wxRect *rect)
a4bbc9f7 1079{
2b260bc3
VS
1080 if ( m_eraseBackground == -1 )
1081 m_eraseBackground = eraseBack;
1082 else
1083 m_eraseBackground |= eraseBack;
7de59551 1084
a4bbc9f7
VS
1085 if ( rect )
1086 {
1087 rect_t r;
1088 r.left = rect->GetLeft(), r.right = rect->GetRight();
1089 r.top = rect->GetTop(), r.bottom = rect->GetBottom();
1090 MGL_wmInvalidateWindowRect(GetHandle(), &r);
1091 }
1092 else
1093 MGL_wmInvalidateWindow(GetHandle());
1094}
1095
1096void wxWindowMGL::Update()
1097{
a4bbc9f7
VS
1098 if ( !m_frozen )
1099 MGL_wmUpdateDC(g_winMng);
1100}
1101
1102void wxWindowMGL::Freeze()
1103{
1104 m_frozen = TRUE;
1105 m_refreshAfterThaw = FALSE;
1106}
1107
1108void wxWindowMGL::Thaw()
1109{
1110 m_frozen = FALSE;
1111 if ( m_refreshAfterThaw )
1112 Refresh();
1113}
1114
a4bbc9f7
VS
1115void wxWindowMGL::HandlePaint(MGLDevCtx *dc)
1116{
1117 if ( m_frozen )
1118 {
7de59551 1119 // Don't paint anything if the window is frozen.
fd495ab3 1120 m_refreshAfterThaw = TRUE;
a4bbc9f7
VS
1121 return;
1122 }
7de59551
RD
1123
1124#ifdef __WXDEBUG__
82186005
VS
1125 // FIXME_MGL -- debugging stuff, to be removed!
1126 static int debugPaintEvents = -1;
1127 if ( debugPaintEvents == -1 )
1128 debugPaintEvents = wxGetEnv(wxT("WXMGL_DEBUG_PAINT_EVENTS"), NULL);
1129 if ( debugPaintEvents )
1130 {
1131 dc->setColorRGB(255,0,255);
1132 dc->fillRect(-1000,-1000,2000,2000);
1133 wxUsleep(50);
1134 }
b8c0528d
VS
1135#endif
1136
fd495ab3
VS
1137 MGLRegion clip;
1138 dc->getClipRegion(clip);
1139 m_updateRegion = wxRegion(clip);
a4bbc9f7
VS
1140 m_paintMGLDC = dc;
1141
a679e129
VS
1142#if wxUSE_CARET
1143 // must hide caret temporarily, otherwise we'd get rendering artifacts
1144 wxCaret *caret = GetCaret();
1145 if ( caret )
1146 caret->Hide();
1147#endif // wxUSE_CARET
1148
2b260bc3 1149 if ( m_eraseBackground != 0 )
7bdc1879 1150 {
2b260bc3
VS
1151 wxWindowDC dc((wxWindow*)this);
1152 wxEraseEvent eventEr(m_windowId, &dc);
1153 eventEr.SetEventObject(this);
1154 GetEventHandler()->ProcessEvent(eventEr);
7bdc1879 1155 }
2b260bc3 1156 m_eraseBackground = -1;
a4bbc9f7
VS
1157
1158 wxNcPaintEvent eventNc(GetId());
1159 eventNc.SetEventObject(this);
1160 GetEventHandler()->ProcessEvent(eventNc);
7de59551 1161
a4bbc9f7
VS
1162 wxPaintEvent eventPt(GetId());
1163 eventPt.SetEventObject(this);
1164 GetEventHandler()->ProcessEvent(eventPt);
a4bbc9f7 1165
a679e129
VS
1166#if wxUSE_CARET
1167 if ( caret )
1168 caret->Show();
1169#endif // wxUSE_CARET
1170
7bdc1879 1171 m_paintMGLDC = NULL;
fd495ab3 1172 m_updateRegion.Clear();
a4bbc9f7
VS
1173}
1174
1175
1176// Find the wxWindow at the current mouse position, returning the mouse
1177// position.
1178wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1179{
1180 return wxFindWindowAtPoint(pt = wxGetMousePosition());
1181}
1182
1183wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
1184{
1185 window_t *wnd = MGL_wmGetWindowAtPosition(g_winMng, pt.x, pt.y);
1186 return (wxWindow*)wnd->userData;
32b8ec41 1187}
d53fc7c9
VS
1188
1189
1190// ---------------------------------------------------------------------------
1191// idle events processing
1192// ---------------------------------------------------------------------------
1193
e39af974 1194void wxWindowMGL::OnInternalIdle()
d53fc7c9 1195{
e39af974
JS
1196 if (wxUpdateUIEvent::CanUpdate(this))
1197 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
d53fc7c9 1198}