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