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