]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/window.cpp
Temporarily disabled OS/2 specific code.
[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 #ifndef WX_PRECOMP
27 #include "wx/window.h"
28 #include "wx/msgdlg.h"
29 #include "wx/accel.h"
30 #include "wx/setup.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 #endif
37
38 #if wxUSE_DRAG_AND_DROP
39 #include "wx/dnd.h"
40 #endif
41
42 #include "wx/log.h"
43 #include "wx/sysopt.h"
44 #include "wx/mgl/private.h"
45 #include "wx/intl.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_padPageUp, WXK_NUMPAD_PRIOR)
316 KEY (KB_padPageDown, WXK_NUMPAD_PAGEDOWN)
317 //KEY (KB_padPageDown, WXK_NUMPAD_NEXT)
318 KEY (KB_1, '1')
319 KEY (KB_2, '2')
320 KEY (KB_3, '3')
321 KEY (KB_4, '4')
322 KEY (KB_5, '5')
323 KEY (KB_6, '6')
324 KEY (KB_7, '7')
325 KEY (KB_8, '8')
326 KEY (KB_9, '9')
327 KEY (KB_0, '0')
328 KEY (KB_minus, WXK_SUBTRACT)
329 KEY (KB_equals, WXK_ADD)
330 KEY (KB_backSlash, '\\')
331 KEY (KB_Q, 'Q')
332 KEY (KB_W, 'W')
333 KEY (KB_E, 'E')
334 KEY (KB_R, 'R')
335 KEY (KB_T, 'T')
336 KEY (KB_Y, 'Y')
337 KEY (KB_U, 'U')
338 KEY (KB_I, 'I')
339 KEY (KB_O, 'O')
340 KEY (KB_P, 'P')
341 KEY (KB_leftSquareBrace,'[')
342 KEY (KB_rightSquareBrace,']')
343 KEY (KB_A, 'A')
344 KEY (KB_S, 'S')
345 KEY (KB_D, 'D')
346 KEY (KB_F, 'F')
347 KEY (KB_G, 'G')
348 KEY (KB_H, 'H')
349 KEY (KB_J, 'J')
350 KEY (KB_K, 'K')
351 KEY (KB_L, 'L')
352 KEY (KB_semicolon, ';')
353 KEY (KB_apostrophe, '\'')
354 KEY (KB_Z, 'Z')
355 KEY (KB_X, 'X')
356 KEY (KB_C, 'C')
357 KEY (KB_V, 'V')
358 KEY (KB_B, 'B')
359 KEY (KB_N, 'N')
360 KEY (KB_M, 'M')
361 KEY (KB_comma, ',')
362 KEY (KB_period, '.')
363 KEY (KB_divide, WXK_DIVIDE)
364 KEY (KB_space, WXK_SPACE)
365 KEY (KB_tilde, '~')
366
367 default: break;
368 }
369 }
370
371 if ( key == 0 )
372 {
373 switch ( EVT_scanCode(event->message) )
374 {
375 KEY (KB_padEnter, WXK_NUMPAD_ENTER)
376 KEY (KB_F1, WXK_F1)
377 KEY (KB_F2, WXK_F2)
378 KEY (KB_F3, WXK_F3)
379 KEY (KB_F4, WXK_F4)
380 KEY (KB_F5, WXK_F5)
381 KEY (KB_F6, WXK_F6)
382 KEY (KB_F7, WXK_F7)
383 KEY (KB_F8, WXK_F8)
384 KEY (KB_F9, WXK_F9)
385 KEY (KB_F10, WXK_F10)
386 KEY (KB_F11, WXK_F11)
387 KEY (KB_F12, WXK_F12)
388 KEY (KB_left, WXK_LEFT)
389 KEY (KB_right, WXK_RIGHT)
390 KEY (KB_up, WXK_UP)
391 KEY (KB_down, WXK_DOWN)
392 KEY (KB_insert, WXK_INSERT)
393 KEY (KB_delete, WXK_DELETE)
394 KEY (KB_home, WXK_HOME)
395 KEY (KB_end, WXK_END)
396 KEY (KB_pageUp, WXK_PAGEUP)
397 KEY (KB_pageDown, WXK_PAGEDOWN)
398 KEY (KB_capsLock, WXK_CAPITAL)
399 KEY (KB_numLock, WXK_NUMLOCK)
400 KEY (KB_scrollLock, WXK_SCROLL)
401 KEY (KB_leftShift, WXK_SHIFT)
402 KEY (KB_rightShift, WXK_SHIFT)
403 KEY (KB_leftCtrl, WXK_CONTROL)
404 KEY (KB_rightCtrl, WXK_CONTROL)
405 KEY (KB_leftAlt, WXK_ALT)
406 KEY (KB_rightAlt, WXK_ALT)
407 KEY (KB_leftWindows, WXK_START)
408 KEY (KB_rightWindows, WXK_START)
409 KEY (KB_menu, WXK_MENU)
410 KEY (KB_sysReq, WXK_SNAPSHOT)
411 KEY (KB_esc, WXK_ESCAPE)
412 KEY (KB_backspace, WXK_BACK)
413 KEY (KB_tab, WXK_TAB)
414 KEY (KB_enter, WXK_RETURN)
415
416 default:
417 key = EVT_asciiCode(event->message);
418 break;
419 }
420 }
421
422 #undef KEY
423
424 return key;
425 }
426
427 static bool wxHandleSpecialKeys(wxKeyEvent& event)
428 {
429 // Add an easy way to capture screenshots:
430 if ( event.m_keyCode == WXK_SNAPSHOT
431 #ifdef __WXDEBUG__ // FIXME_MGL - remove when KB_sysReq works in MGL!
432 || (event.m_keyCode == WXK_F1 &&
433 event.m_shiftDown && event.m_controlDown)
434 #endif
435 )
436 {
437 wxCaptureScreenshot(event.m_altDown/*only active wnd?*/);
438 return true;
439 }
440
441 return false;
442 }
443
444 static ibool MGLAPI wxWindowKeybHandler(window_t *wnd, event_t *e)
445 {
446 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
447
448 if ( !win->IsEnabled() ) return FALSE;
449
450 wxPoint where;
451 MGL_wmCoordGlobalToLocal(win->GetHandle(),
452 e->where_x, e->where_y, &where.x, &where.y);
453
454 wxKeyEvent event;
455 event.SetEventObject(win);
456 event.SetTimestamp(e->when);
457 event.m_keyCode = wxScanToKeyCode(e, true);
458 event.m_scanCode = 0; // not used by wx at all
459 event.m_x = where.x;
460 event.m_y = where.y;
461 event.m_shiftDown = ( e->modifiers & EVT_SHIFTKEY ) != 0;
462 event.m_controlDown = ( e->modifiers & EVT_CTRLSTATE ) != 0;
463 event.m_altDown = ( e->modifiers & EVT_LEFTALT ) != 0;
464 event.m_metaDown = ( e->modifiers & EVT_RIGHTALT ) != 0;
465
466 if ( e->what == EVT_KEYUP )
467 {
468 event.SetEventType(wxEVT_KEY_UP);
469 return win->GetEventHandler()->ProcessEvent(event);
470 }
471 else
472 {
473 bool ret;
474 wxKeyEvent event2;
475
476 event.SetEventType(wxEVT_KEY_DOWN);
477 event2 = event;
478
479 ret = win->GetEventHandler()->ProcessEvent(event);
480
481 // wxMSW doesn't send char events with Alt pressed
482 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
483 // will only be sent if it is not in an accelerator table:
484 event2.m_keyCode = wxScanToKeyCode(e, false);
485 if ( !ret && event2.m_keyCode != 0 )
486 {
487 event2.SetEventType(wxEVT_CHAR);
488 ret = win->GetEventHandler()->ProcessEvent(event2);
489 }
490
491 // Synthetize navigation key event, but do it only if the TAB key
492 // wasn't handled yet:
493 if ( !ret && event.m_keyCode == WXK_TAB &&
494 win->GetParent() && win->GetParent()->HasFlag(wxTAB_TRAVERSAL) )
495 {
496 wxNavigationKeyEvent navEvent;
497 navEvent.SetEventObject(win->GetParent());
498 // Shift-TAB goes in reverse direction:
499 navEvent.SetDirection(!event.m_shiftDown);
500 // Ctrl-TAB changes the (parent) window, i.e. switch notebook page:
501 navEvent.SetWindowChange(event.m_controlDown);
502 navEvent.SetCurrentFocus(wxStaticCast(win, wxWindow));
503 ret = win->GetParent()->GetEventHandler()->ProcessEvent(navEvent);
504 }
505
506 // Finally, process special meaning keys that are usually
507 // a responsibility of OS or window manager:
508 if ( !ret )
509 ret = wxHandleSpecialKeys(event);
510
511 return ret;
512 }
513 }
514
515 // ---------------------------------------------------------------------------
516 // event tables
517 // ---------------------------------------------------------------------------
518
519 // in wxUniv this class is abstract because it doesn't have DoPopupMenu()
520 IMPLEMENT_ABSTRACT_CLASS(wxWindowMGL, wxWindowBase)
521
522 BEGIN_EVENT_TABLE(wxWindowMGL, wxWindowBase)
523 END_EVENT_TABLE()
524
525 // ===========================================================================
526 // implementation
527 // ===========================================================================
528
529 // ----------------------------------------------------------------------------
530 // constructors and such
531 // ----------------------------------------------------------------------------
532
533 extern wxVideoMode wxGetDefaultDisplayMode();
534
535 void wxWindowMGL::Init()
536 {
537 // First of all, make sure window manager is up and running. If it is
538 // not the case, initialize it in default display mode
539 if ( !g_winMng )
540 {
541 if ( !wxTheApp->SetDisplayMode(wxGetDefaultDisplayMode()) )
542 wxLogFatalError(_("Cannot initialize display."));
543 }
544
545 // mgl specific:
546 m_wnd = NULL;
547 m_isShown = true;
548 m_frozen = false;
549 m_paintMGLDC = NULL;
550 m_eraseBackground = -1;
551 }
552
553 // Destructor
554 wxWindowMGL::~wxWindowMGL()
555 {
556 SendDestroyEvent();
557
558 m_isBeingDeleted = true;
559
560 if ( gs_mouseCapture == this )
561 ReleaseMouse();
562
563 if (gs_activeFrame == this)
564 {
565 gs_activeFrame = NULL;
566 // activate next frame in Z-order:
567 if ( m_wnd->prev )
568 {
569 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
570 win->SetFocus();
571 }
572 else if ( m_wnd->next )
573 {
574 wxWindowMGL *win = (wxWindowMGL*)m_wnd->next->userData;
575 win->SetFocus();
576 }
577 }
578
579 if ( gs_focusedWindow == this )
580 KillFocus();
581
582 if ( gs_windowUnderMouse == this )
583 gs_windowUnderMouse = NULL;
584
585 DestroyChildren();
586
587 if ( m_wnd )
588 MGL_wmDestroyWindow(m_wnd);
589 }
590
591 // real construction (Init() must have been called before!)
592 bool wxWindowMGL::Create(wxWindow *parent,
593 wxWindowID id,
594 const wxPoint& pos,
595 const wxSize& size,
596 long style,
597 const wxString& name)
598 {
599 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
600 return false;
601
602 if ( parent )
603 parent->AddChild(this);
604
605 int x, y, w, h;
606 x = pos.x, y = pos.y;
607 if ( x == -1 )
608 x = 0; // FIXME_MGL, something better, see GTK+
609 if ( y == -1 )
610 y = 0; // FIXME_MGL, something better, see GTK+
611 AdjustForParentClientOrigin(x, y, 0);
612 w = WidthDefault(size.x);
613 h = HeightDefault(size.y);
614
615 long mgl_style = 0;
616 window_t *wnd_parent = parent ? parent->GetHandle() : NULL;
617
618 if ( style & wxFULL_REPAINT_ON_RESIZE )
619 {
620 mgl_style |= MGL_WM_FULL_REPAINT_ON_RESIZE;
621 }
622 if ( style & wxSTAY_ON_TOP )
623 {
624 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
625 }
626 if ( style & wxPOPUP_WINDOW )
627 {
628 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
629 // it is created hidden as other top level windows
630 m_isShown = false;
631 wnd_parent = NULL;
632 }
633
634 window_t *wnd = MGL_wmCreateWindow(g_winMng, wnd_parent, x, y, w, h);
635
636 MGL_wmSetWindowFlags(wnd, mgl_style);
637 MGL_wmShowWindow(wnd, m_isShown);
638
639 SetMGLwindow_t(wnd);
640
641 return true;
642 }
643
644 void wxWindowMGL::SetMGLwindow_t(struct window_t *wnd)
645 {
646 if ( m_wnd )
647 MGL_wmDestroyWindow(m_wnd);
648
649 m_wnd = wnd;
650 if ( !m_wnd ) return;
651
652 m_isShown = (bool)m_wnd->visible;
653
654 MGL_wmSetWindowUserData(m_wnd, (void*) this);
655 MGL_wmSetWindowPainter(m_wnd, wxWindowPainter);
656 MGL_wmPushWindowEventHandler(m_wnd, wxWindowMouseHandler, EVT_MOUSEEVT, 0);
657 MGL_wmPushWindowEventHandler(m_wnd, wxWindowKeybHandler, EVT_KEYEVT, 0);
658
659 if ( m_cursor.Ok() )
660 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
661 else
662 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
663 }
664
665 // ---------------------------------------------------------------------------
666 // basic operations
667 // ---------------------------------------------------------------------------
668
669 void wxWindowMGL::SetFocus()
670 {
671 if ( gs_focusedWindow == this ) return;
672
673 wxWindowMGL *oldFocusedWindow = gs_focusedWindow;
674
675 if ( gs_focusedWindow )
676 {
677 gs_toBeFocusedWindow = (wxWindow*)this;
678 gs_focusedWindow->KillFocus();
679 gs_toBeFocusedWindow = NULL;
680 }
681
682 gs_focusedWindow = this;
683
684 MGL_wmCaptureEvents(GetHandle(), EVT_KEYEVT, wxMGL_CAPTURE_KEYB);
685
686 wxWindowMGL *active = wxGetTopLevelParent(this);
687 if ( !(m_windowStyle & wxPOPUP_WINDOW) && active != gs_activeFrame )
688 {
689 if ( gs_activeFrame )
690 {
691 wxActivateEvent event(wxEVT_ACTIVATE, false, gs_activeFrame->GetId());
692 event.SetEventObject(gs_activeFrame);
693 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
694 }
695
696 gs_activeFrame = active;
697 wxActivateEvent event(wxEVT_ACTIVATE, true, gs_activeFrame->GetId());
698 event.SetEventObject(gs_activeFrame);
699 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
700 }
701
702 wxFocusEvent event(wxEVT_SET_FOCUS, GetId());
703 event.SetEventObject(this);
704 event.SetWindow((wxWindow*)oldFocusedWindow);
705 GetEventHandler()->ProcessEvent(event);
706
707 #if wxUSE_CARET
708 // caret needs to be informed about focus change
709 wxCaret *caret = GetCaret();
710 if ( caret )
711 caret->OnSetFocus();
712 #endif // wxUSE_CARET
713 }
714
715 void wxWindowMGL::KillFocus()
716 {
717 if ( gs_focusedWindow != this ) return;
718 gs_focusedWindow = NULL;
719
720 if ( m_isBeingDeleted ) return;
721
722 MGL_wmUncaptureEvents(GetHandle(), wxMGL_CAPTURE_KEYB);
723
724 #if wxUSE_CARET
725 // caret needs to be informed about focus change
726 wxCaret *caret = GetCaret();
727 if ( caret )
728 caret->OnKillFocus();
729 #endif // wxUSE_CARET
730
731 wxFocusEvent event(wxEVT_KILL_FOCUS, GetId());
732 event.SetEventObject(this);
733 event.SetWindow(gs_toBeFocusedWindow);
734 GetEventHandler()->ProcessEvent(event);
735 }
736
737 // ----------------------------------------------------------------------------
738 // this wxWindowBase function is implemented here (in platform-specific file)
739 // because it is static and so couldn't be made virtual
740 // ----------------------------------------------------------------------------
741 wxWindow *wxWindowBase::DoFindFocus()
742 {
743 return (wxWindow*)gs_focusedWindow;
744 }
745
746 bool wxWindowMGL::Show(bool show)
747 {
748 if ( !wxWindowBase::Show(show) )
749 return false;
750
751 MGL_wmShowWindow(m_wnd, show);
752
753 if (!show && gs_activeFrame == this)
754 {
755 // activate next frame in Z-order:
756 if ( m_wnd->prev )
757 {
758 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
759 win->SetFocus();
760 }
761 else if ( m_wnd->next )
762 {
763 wxWindowMGL *win = (wxWindowMGL*)m_wnd->next->userData;
764 win->SetFocus();
765 }
766 else
767 {
768 gs_activeFrame = NULL;
769 }
770 }
771
772 return true;
773 }
774
775 // Raise the window to the top of the Z order
776 void wxWindowMGL::Raise()
777 {
778 MGL_wmRaiseWindow(m_wnd);
779 }
780
781 // Lower the window to the bottom of the Z order
782 void wxWindowMGL::Lower()
783 {
784 MGL_wmLowerWindow(m_wnd);
785 }
786
787 void wxWindowMGL::DoCaptureMouse()
788 {
789 if ( gs_mouseCapture )
790 MGL_wmUncaptureEvents(gs_mouseCapture->m_wnd, wxMGL_CAPTURE_MOUSE);
791
792 gs_mouseCapture = this;
793 MGL_wmCaptureEvents(m_wnd, EVT_MOUSEEVT, wxMGL_CAPTURE_MOUSE);
794 }
795
796 void wxWindowMGL::DoReleaseMouse()
797 {
798 wxASSERT_MSG( gs_mouseCapture == this, wxT("attempt to release mouse, but this window hasn't captured it") );
799
800 MGL_wmUncaptureEvents(m_wnd, wxMGL_CAPTURE_MOUSE);
801 gs_mouseCapture = NULL;
802 }
803
804 /* static */ wxWindow *wxWindowBase::GetCapture()
805 {
806 return (wxWindow*)gs_mouseCapture;
807 }
808
809 bool wxWindowMGL::SetCursor(const wxCursor& cursor)
810 {
811 if ( !wxWindowBase::SetCursor(cursor) )
812 {
813 // no change
814 return false;
815 }
816
817 if ( m_cursor.Ok() )
818 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
819 else
820 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
821
822 return true;
823 }
824
825 void wxWindowMGL::WarpPointer(int x, int y)
826 {
827 int w, h;
828 wxDisplaySize(&w, &h);
829
830 ClientToScreen(&x, &y);
831 if ( x < 0 )
832 x = 0;
833 if ( y < 0 )
834 y = 0;
835 if ( x >= w )
836 x = w-1;
837 if ( y >= h )
838 y = h-1;
839
840 EVT_setMousePos(x, y);
841 }
842
843 // Set this window to be the child of 'parent'.
844 bool wxWindowMGL::Reparent(wxWindowBase *parent)
845 {
846 if ( !wxWindowBase::Reparent(parent) )
847 return false;
848
849 MGL_wmReparentWindow(m_wnd, parent->GetHandle());
850
851 return true;
852 }
853
854
855 // ---------------------------------------------------------------------------
856 // drag and drop
857 // ---------------------------------------------------------------------------
858
859 #if wxUSE_DRAG_AND_DROP
860
861 void wxWindowMGL::SetDropTarget(wxDropTarget *pDropTarget)
862 {
863 if ( m_dropTarget != 0 ) {
864 m_dropTarget->Revoke(m_hWnd);
865 delete m_dropTarget;
866 }
867
868 m_dropTarget = pDropTarget;
869 if ( m_dropTarget != 0 )
870 m_dropTarget->Register(m_hWnd);
871 }
872 // FIXME_MGL
873 #endif // wxUSE_DRAG_AND_DROP
874
875 // old style file-manager drag&drop support: we retain the old-style
876 // DragAcceptFiles in parallel with SetDropTarget.
877 void wxWindowMGL::DragAcceptFiles(bool WXUNUSED(accept))
878 {
879 #if 0 // FIXME_MGL
880 HWND hWnd = GetHwnd();
881 if ( hWnd )
882 ::DragAcceptFiles(hWnd, (BOOL)accept);
883 #endif
884 }
885
886 // ---------------------------------------------------------------------------
887 // moving and resizing
888 // ---------------------------------------------------------------------------
889
890 // Get total size
891 void wxWindowMGL::DoGetSize(int *x, int *y) const
892 {
893 wxASSERT_MSG( m_wnd, wxT("invalid window") );
894
895 if (x) *x = m_wnd->width;
896 if (y) *y = m_wnd->height;
897 }
898
899 void wxWindowMGL::DoGetPosition(int *x, int *y) const
900 {
901 wxASSERT_MSG( m_wnd, wxT("invalid window") );
902
903 int pX = 0, pY = 0;
904 AdjustForParentClientOrigin(pX, pY, 0);
905
906 if (x) *x = m_wnd->x - pX;
907 if (y) *y = m_wnd->y - pY;
908 }
909
910 void wxWindowMGL::DoScreenToClient(int *x, int *y) const
911 {
912 int ax, ay;
913 MGL_wmCoordGlobalToLocal(m_wnd, 0, 0, &ax, &ay);
914 if (x)
915 (*x) += ax;
916 if (y)
917 (*y) += ay;
918 }
919
920 void wxWindowMGL::DoClientToScreen(int *x, int *y) const
921 {
922 int ax, ay;
923 MGL_wmCoordLocalToGlobal(m_wnd, 0, 0, &ax, &ay);
924 if (x)
925 (*x) += ax;
926 if (y)
927 (*y) += ay;
928 }
929
930 // Get size *available for subwindows* i.e. excluding menu bar etc.
931 void wxWindowMGL::DoGetClientSize(int *x, int *y) const
932 {
933 DoGetSize(x, y);
934 }
935
936 void wxWindowMGL::DoMoveWindow(int x, int y, int width, int height)
937 {
938 wxRect rcClient(GetClientRect());
939
940 MGL_wmSetWindowPosition(m_wnd, x, y, width, height);
941
942 // When the origin or a window stays fixed but the height or width
943 // changes, invalidate the old and new non-client areas
944 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) &&
945 m_wnd->x == x && m_wnd->y == y &&
946 rcClient.Intersect(GetClientRect()) != wxRect(0, 0, width, height) )
947 {
948 wxRegion rgn(0, 0, width, height);
949 rgn.Subtract(rcClient);
950
951 // This should work I think, but doesn't seem to:
952 //MGL_wmInvalidateWindowRegion(m_wnd, rgn.GetMGLRegion().rgnPointer());
953
954 // Use MGL_wmInvalidateWindowRect instead:
955 for (wxRegionIterator it(rgn); it; it++)
956 {
957 rect_t rc;
958 rc.left = it.GetX();
959 rc.top = it.GetY();
960 rc.right = rc.left + it.GetW();
961 rc.bottom = rc.top + it.GetH();
962 MGL_wmInvalidateWindowRect(m_wnd, &rc);
963 }
964 }
965 }
966
967 // set the size of the window: if the dimensions are positive, just use them,
968 // but if any of them is equal to -1, it means that we must find the value for
969 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
970 // which case -1 is a valid value for x and y)
971 //
972 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
973 // the width/height to best suit our contents, otherwise we reuse the current
974 // width/height
975 void wxWindowMGL::DoSetSize(int x, int y, int width, int height, int sizeFlags)
976 {
977 // get the current size and position...
978 int currentX, currentY;
979 GetPosition(&currentX, &currentY);
980 int currentW,currentH;
981 GetSize(&currentW, &currentH);
982
983 // ... and don't do anything (avoiding flicker) if it's already ok
984 if ( x == currentX && y == currentY &&
985 width == currentW && height == currentH )
986 {
987 return;
988 }
989
990 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
991 x = currentX;
992 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
993 y = currentY;
994
995 AdjustForParentClientOrigin(x, y, sizeFlags);
996
997 wxSize size(-1, -1);
998 if ( width == -1 )
999 {
1000 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1001 {
1002 size = DoGetBestSize();
1003 width = size.x;
1004 }
1005 else
1006 {
1007 // just take the current one
1008 width = currentW;
1009 }
1010 }
1011
1012 if ( height == -1 )
1013 {
1014 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1015 {
1016 if ( size.x == -1 )
1017 {
1018 size = DoGetBestSize();
1019 }
1020 //else: already called DoGetBestSize() above
1021
1022 height = size.y;
1023 }
1024 else
1025 {
1026 // just take the current one
1027 height = currentH;
1028 }
1029 }
1030
1031 int maxWidth = GetMaxWidth(),
1032 minWidth = GetMinWidth(),
1033 maxHeight = GetMaxHeight(),
1034 minHeight = GetMinHeight();
1035
1036 if ( minWidth != -1 && width < minWidth ) width = minWidth;
1037 if ( maxWidth != -1 && width > maxWidth ) width = maxWidth;
1038 if ( minHeight != -1 && height < minHeight ) height = minHeight;
1039 if ( maxHeight != -1 && height > maxHeight ) height = maxHeight;
1040
1041 if ( m_wnd->x != x || m_wnd->y != y ||
1042 (int)m_wnd->width != width || (int)m_wnd->height != height )
1043 {
1044 DoMoveWindow(x, y, width, height);
1045
1046 wxSize newSize(width, height);
1047 wxSizeEvent event(newSize, GetId());
1048 event.SetEventObject(this);
1049 GetEventHandler()->ProcessEvent(event);
1050 }
1051 }
1052
1053 void wxWindowMGL::DoSetClientSize(int width, int height)
1054 {
1055 SetSize(width, height);
1056 }
1057
1058 // ---------------------------------------------------------------------------
1059 // text metrics
1060 // ---------------------------------------------------------------------------
1061
1062 int wxWindowMGL::GetCharHeight() const
1063 {
1064 wxScreenDC dc;
1065 dc.SetFont(m_font);
1066 return dc.GetCharHeight();
1067 }
1068
1069 int wxWindowMGL::GetCharWidth() const
1070 {
1071 wxScreenDC dc;
1072 dc.SetFont(m_font);
1073 return dc.GetCharWidth();
1074 }
1075
1076 void wxWindowMGL::GetTextExtent(const wxString& string,
1077 int *x, int *y,
1078 int *descent, int *externalLeading,
1079 const wxFont *theFont) const
1080 {
1081 wxScreenDC dc;
1082 if (!theFont)
1083 theFont = &m_font;
1084 dc.GetTextExtent(string, x, y, descent, externalLeading, (wxFont*)theFont);
1085 }
1086
1087
1088 // ---------------------------------------------------------------------------
1089 // painting
1090 // ---------------------------------------------------------------------------
1091
1092 void wxWindowMGL::Clear()
1093 {
1094 wxClientDC dc((wxWindow *)this);
1095 wxBrush brush(GetBackgroundColour(), wxSOLID);
1096 dc.SetBackground(brush);
1097 dc.Clear();
1098 }
1099
1100 #include "wx/menu.h"
1101 void wxWindowMGL::Refresh(bool eraseBack, const wxRect *rect)
1102 {
1103 if ( m_eraseBackground == -1 )
1104 m_eraseBackground = eraseBack;
1105 else
1106 m_eraseBackground |= eraseBack;
1107
1108 if ( rect )
1109 {
1110 rect_t r;
1111 r.left = rect->GetLeft(), r.right = rect->GetRight();
1112 r.top = rect->GetTop(), r.bottom = rect->GetBottom();
1113 MGL_wmInvalidateWindowRect(GetHandle(), &r);
1114 }
1115 else
1116 MGL_wmInvalidateWindow(GetHandle());
1117 }
1118
1119 void wxWindowMGL::Update()
1120 {
1121 if ( !m_frozen )
1122 MGL_wmUpdateDC(g_winMng);
1123 }
1124
1125 void wxWindowMGL::Freeze()
1126 {
1127 m_frozen = true;
1128 m_refreshAfterThaw = false;
1129 }
1130
1131 void wxWindowMGL::Thaw()
1132 {
1133 m_frozen = false;
1134 if ( m_refreshAfterThaw )
1135 Refresh();
1136 }
1137
1138 void wxWindowMGL::HandlePaint(MGLDevCtx *dc)
1139 {
1140 if ( m_frozen )
1141 {
1142 // Don't paint anything if the window is frozen.
1143 m_refreshAfterThaw = true;
1144 return;
1145 }
1146
1147 #ifdef __WXDEBUG__
1148 // FIXME_MGL -- debugging stuff, to be removed!
1149 static int debugPaintEvents = -1;
1150 if ( debugPaintEvents == -1 )
1151 debugPaintEvents = wxGetEnv(wxT("WXMGL_DEBUG_PAINT_EVENTS"), NULL);
1152 if ( debugPaintEvents )
1153 {
1154 dc->setColorRGB(255,0,255);
1155 dc->fillRect(-1000,-1000,2000,2000);
1156 wxUsleep(50);
1157 }
1158 #endif
1159
1160 MGLRegion clip;
1161 dc->getClipRegion(clip);
1162 m_updateRegion = wxRegion(clip);
1163 m_paintMGLDC = dc;
1164
1165 #if wxUSE_CARET
1166 // must hide caret temporarily, otherwise we'd get rendering artifacts
1167 wxCaret *caret = GetCaret();
1168 if ( caret )
1169 caret->Hide();
1170 #endif // wxUSE_CARET
1171
1172 if ( m_eraseBackground != 0 )
1173 {
1174 wxWindowDC dc((wxWindow*)this);
1175 wxEraseEvent eventEr(m_windowId, &dc);
1176 eventEr.SetEventObject(this);
1177 GetEventHandler()->ProcessEvent(eventEr);
1178 }
1179 m_eraseBackground = -1;
1180
1181 wxNcPaintEvent eventNc(GetId());
1182 eventNc.SetEventObject(this);
1183 GetEventHandler()->ProcessEvent(eventNc);
1184
1185 wxPaintEvent eventPt(GetId());
1186 eventPt.SetEventObject(this);
1187 GetEventHandler()->ProcessEvent(eventPt);
1188
1189 #if wxUSE_CARET
1190 if ( caret )
1191 caret->Show();
1192 #endif // wxUSE_CARET
1193
1194 m_paintMGLDC = NULL;
1195 m_updateRegion.Clear();
1196 }
1197
1198
1199 // Find the wxWindow at the current mouse position, returning the mouse
1200 // position.
1201 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1202 {
1203 return wxFindWindowAtPoint(pt = wxGetMousePosition());
1204 }
1205
1206 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
1207 {
1208 window_t *wnd = MGL_wmGetWindowAtPosition(g_winMng, pt.x, pt.y);
1209 return (wxWindow*)wnd->userData;
1210 }
1211
1212
1213 // ---------------------------------------------------------------------------
1214 // idle events processing
1215 // ---------------------------------------------------------------------------
1216
1217 void wxWindowMGL::OnInternalIdle()
1218 {
1219 if (wxUpdateUIEvent::CanUpdate(this))
1220 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1221 }