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