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