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