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