]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/window.cpp
mention wx[Flex]GridSizer too
[wxWidgets.git] / src / mgl / window.cpp
CommitLineData
32b8ec41 1/////////////////////////////////////////////////////////////////////////////
a4bbc9f7
VS
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 SciTech Software, Inc. (www.scitechsoft.com)
8// Licence: wxWindows license
32b8ec41
VZ
9/////////////////////////////////////////////////////////////////////////////
10
a4bbc9f7
VS
11// ===========================================================================
12// declarations
13// ===========================================================================
14
15// ---------------------------------------------------------------------------
16// headers
17// ---------------------------------------------------------------------------
32b8ec41
VZ
18
19#ifdef __GNUG__
20 #pragma implementation "window.h"
21#endif
22
a4bbc9f7
VS
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/accel.h"
33 #include "wx/setup.h"
34 #include "wx/dc.h"
35 #include "wx/dcclient.h"
36 #include "wx/utils.h"
37 #include "wx/app.h"
38 #include "wx/panel.h"
39 #include "wx/caret.h"
40#endif
41
42#if wxUSE_DRAG_AND_DROP
43 #include "wx/dnd.h"
44#endif
45
46#include "wx/log.h"
47#include "wx/sysopt.h"
48#include "wx/mgl/private.h"
49#include "wx/intl.h"
50#include "wx/dcscreen.h"
51
52#include <mgraph.hpp>
53
54#if wxUSE_TOOLTIPS
55 #include "wx/tooltip.h"
56#endif
57
58// ---------------------------------------------------------------------------
59// global variables
60// ---------------------------------------------------------------------------
61
62// MGL window manager and associated DC.
63winmng_t *g_winMng = NULL;
64MGLDevCtx *g_displayDC = NULL;
65
2b260bc3 66// the window that has keyboard focus:
7cdbf02c 67static wxWindowMGL *gs_focusedWindow = NULL;
7bdc1879 68// the window that is currently under mouse cursor:
7cdbf02c
VS
69static wxWindowMGL *gs_windowUnderMouse = NULL;
70// the window that has mouse capture
71static wxWindowMGL *gs_mouseCapture = NULL;
72// the frame that is currently active (i.e. its child has focus). It is
73// used to generate wxActivateEvents
74static wxWindowMGL *gs_activeFrame = NULL;
a4bbc9f7
VS
75
76// ---------------------------------------------------------------------------
77// constants
78// ---------------------------------------------------------------------------
79
80// Custom identifiers used to distinguish between various event handlers
81// and capture handlers passed to MGL_wm
82enum
83{
84 wxMGL_CAPTURE_MOUSE = 1,
85 wxMGL_CAPTURE_KEYB = 2
86};
87
88
89// ---------------------------------------------------------------------------
90// private functions
91// ---------------------------------------------------------------------------
92
a4bbc9f7
VS
93// wxCreateMGL_WM creates MGL display DC and associates it with winmng_t
94// structure. Dimensions and depth of the DC are fetched from wxSystemOptions
95// object.
96// This function is *not* called from wxApp's initialization but rather at
97// the time when WM is needed, i.e. when first wxWindow is created. This
98// has two important effects:
99// a) it is possible to write windowless wxMGL apps
100// b) the app has plenty of time in wxApp::OnInit to feed wxSystemOptions
101// with desired settings
102
fd495ab3 103// FIXME_MGL -- move to app.cpp??
ef344ff8
VS
104static void wxDesktopPainter(window_t *wnd, MGLDC *dc)
105{
106 // FIXME_MGL - for now...
107 MGL_setColorRGB(0x63, 0x63, 0x96);
108 MGL_fillRectCoord(0, 0, wnd->width, wnd->height);
109}
110
111
a4bbc9f7
VS
112bool wxCreateMGL_WM()
113{
114 int mode;
115 int width = 640, height = 480, depth = 16;
116 int refresh = MGL_DEFAULT_REFRESH;
117
118#if wxUSE_SYSTEM_OPTIONS
ef344ff8 119 // FIXME_MGL -- so what is The Proper Way?
a4bbc9f7
VS
120 if ( wxSystemOptions::HasOption(wxT("mgl.screen-width") )
121 width = wxSystemOptions::GetOptionInt(wxT("mgl.screen-width"));
122 if ( wxSystemOptions::HasOption(wxT("mgl.screen-height") )
123 height = wxSystemOptions::GetOptionInt(wxT("mgl.screen-height"));
124 if ( wxSystemOptions::HasOption(wxT("mgl.screen-depth") )
125 depth = wxSystemOptions::GetOptionInt(wxT("mgl.screen-depth"));
126 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh") )
127 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
128#endif
129
130 mode = MGL_findMode(width, height, depth);
131 if ( mode == -1 )
132 {
133 wxLogWarning(_("Mode %ix%i-%i not available, falling back to default mode."), width, height, depth);
134 mode = 0; // always available
135 }
136 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
137 if ( !g_displayDC->isValid() )
138 {
139 delete g_displayDC;
140 g_displayDC = NULL;
141 return FALSE;
142 }
143
144 g_winMng = MGL_wmCreate(g_displayDC->getDC());
145 if (!g_winMng)
146 return FALSE;
ef344ff8
VS
147
148 MGL_wmSetWindowPainter(MGL_wmGetRootWindow(g_winMng), wxDesktopPainter);
a4bbc9f7
VS
149
150 return TRUE;
151}
152
153void wxDestroyMGL_WM()
154{
7bdc1879 155 if ( g_winMng )
a4bbc9f7
VS
156 {
157 MGL_wmDestroy(g_winMng);
158 g_winMng = NULL;
159 }
7bdc1879
VS
160 if ( g_displayDC )
161 {
162 delete g_displayDC;
163 g_displayDC = NULL;
164 }
a4bbc9f7
VS
165}
166
7cdbf02c
VS
167// Returns toplevel grandparent of given window:
168static wxWindowMGL* wxGetTopLevelParent(wxWindowMGL *win)
169{
170 wxWindowMGL *p = win;
171 while (p && !p->IsTopLevel())
172 p = p->GetParent();
173 return p;
174}
175
7bdc1879
VS
176// ---------------------------------------------------------------------------
177// MGL_WM hooks:
178// ---------------------------------------------------------------------------
179
180static void wxWindowPainter(window_t *wnd, MGLDC *dc)
181{
182 wxWindowMGL *w = (wxWindow*) wnd->userData;
544c782d 183 if ( w && !(w->GetWindowStyle() & wxTRANSPARENT_WINDOW) )
7bdc1879
VS
184 {
185 MGLDevCtx ctx(dc);
186 w->HandlePaint(&ctx);
187 }
ef344ff8
VS
188 // FIXME_MGL -- root window should be a regular window so that
189 // enter/leave and activate/deactivate events work correctly
190}
191
192static ibool wxWindowMouseHandler(window_t *wnd, event_t *e)
193{
194 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
985520bc 195 wxPoint where;
ef344ff8 196
985520bc
VS
197 MGL_wmCoordGlobalToLocal(win->GetHandle(),
198 e->where_x, e->where_y, &where.x, &where.y);
199
a200c35e
VS
200 if ( !win->IsEnabled() ) return FALSE;
201
ef344ff8
VS
202 wxEventType type = wxEVT_NULL;
203 wxMouseEvent event;
204 event.SetEventObject(win);
205 event.SetTimestamp(e->when);
206 event.m_x = where.x;
207 event.m_y = where.y;
208 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
209 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
210 event.m_altDown = e->modifiers & EVT_LEFTALT;
211 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
212 event.m_leftDown = e->modifiers & EVT_LEFTBUT;
213 event.m_middleDown = e->modifiers & EVT_MIDDLEBUT;
214 event.m_rightDown = e->modifiers & EVT_RIGHTBUT;
215
216 switch (e->what)
217 {
218 case EVT_MOUSEDOWN:
219 if ( e->message & EVT_LEFTBMASK )
220 type = (e->message & EVT_DBLCLICK) ?
221 wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN;
222 else if ( e->message & EVT_MIDDLEBMASK )
223 type = (e->message & EVT_DBLCLICK) ?
224 wxEVT_MIDDLE_DCLICK : wxEVT_MIDDLE_DOWN;
225 else if ( e->message & EVT_RIGHTBMASK )
226 type = (e->message & EVT_DBLCLICK) ?
227 wxEVT_RIGHT_DCLICK : wxEVT_RIGHT_DOWN;
228 break;
229
230 case EVT_MOUSEUP:
231 if ( e->message & EVT_LEFTBMASK )
232 type = wxEVT_LEFT_UP;
233 else if ( e->message & EVT_MIDDLEBMASK )
234 type = wxEVT_MIDDLE_UP;
235 else if ( e->message & EVT_RIGHTBMASK )
236 type = wxEVT_RIGHT_UP;
237 break;
238
239 case EVT_MOUSEMOVE:
7cdbf02c
VS
240 if ( !gs_mouseCapture )
241 {
242 if ( win != gs_windowUnderMouse )
243 {
244 if ( gs_windowUnderMouse )
245 {
246 wxMouseEvent event2(event);
247 MGL_wmCoordGlobalToLocal(gs_windowUnderMouse->GetHandle(),
248 e->where_x, e->where_y,
249 &event2.m_x, &event2.m_y);
250
251 event2.SetEventObject(gs_windowUnderMouse);
252 event2.SetEventType(wxEVT_LEAVE_WINDOW);
253 gs_windowUnderMouse->GetEventHandler()->ProcessEvent(event2);
254 }
255
256 wxMouseEvent event3(event);
257 event3.SetEventType(wxEVT_ENTER_WINDOW);
258 win->GetEventHandler()->ProcessEvent(event3);
259
260 gs_windowUnderMouse = win;
261 }
262 }
263 else // gs_mouseCapture
ef344ff8 264 {
7cdbf02c
VS
265 bool inside = (where.x >= 0 &&
266 where.y >= 0 &&
267 where.x < win->GetSize().x &&
268 where.y < win->GetSize().y);
269 if ( (inside && gs_windowUnderMouse != win) ||
270 (!inside && gs_windowUnderMouse == win) )
ef344ff8 271 {
7cdbf02c
VS
272 wxMouseEvent evt(inside ?
273 wxEVT_ENTER_WINDOW : wxEVT_LEAVE_WINDOW);
274 evt.SetEventObject(win);
275 win->GetEventHandler()->ProcessEvent(evt);
276 gs_windowUnderMouse = inside ? win : NULL;
ef344ff8 277 }
ef344ff8
VS
278 }
279
280 type = wxEVT_MOTION;
281 break;
282
283 default:
284 break;
285 }
286
287 if ( type == wxEVT_NULL )
288 {
289 return FALSE;
290 }
291 else
292 {
293 event.SetEventType(type);
294 return win->GetEventHandler()->ProcessEvent(event);
295 }
296}
297
2b260bc3 298static long wxScanToKeyCode(event_t *event)
ef344ff8 299{
2b260bc3
VS
300 #define KEY(mgl,wx) case mgl: key = wx; break;
301
302 long key = 0;
303 switch ( EVT_scanCode(event->message) )
304 {
305 KEY (KB_padEnter, WXK_NUMPAD_ENTER)
306 KEY (KB_padMinus, WXK_NUMPAD_SUBTRACT)
307 KEY (KB_padPlus, WXK_NUMPAD_ADD)
308 KEY (KB_padTimes, WXK_NUMPAD_MULTIPLY)
309 KEY (KB_padDivide, WXK_NUMPAD_DIVIDE)
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_padCenter, WXK_NUMPAD_SEPARATOR) // ?
323 KEY (KB_F1, WXK_F1)
324 KEY (KB_F2, WXK_F2)
325 KEY (KB_F3, WXK_F3)
326 KEY (KB_F4, WXK_F4)
327 KEY (KB_F5, WXK_F5)
328 KEY (KB_F6, WXK_F6)
329 KEY (KB_F7, WXK_F7)
330 KEY (KB_F8, WXK_F8)
331 KEY (KB_F9, WXK_F9)
332 KEY (KB_F10, WXK_F10)
333 KEY (KB_F11, WXK_F11)
334 KEY (KB_F12, WXK_F12)
335 KEY (KB_left, WXK_LEFT)
336 KEY (KB_right, WXK_RIGHT)
337 KEY (KB_up, WXK_UP)
338 KEY (KB_down, WXK_DOWN)
339 KEY (KB_insert, WXK_INSERT)
340 KEY (KB_delete, WXK_DELETE)
341 KEY (KB_home, WXK_HOME)
342 KEY (KB_end, WXK_END)
343 KEY (KB_pageUp, WXK_PAGEUP)
344 KEY (KB_pageDown, WXK_PAGEDOWN)
345 KEY (KB_capsLock, WXK_CAPITAL)
346 KEY (KB_numLock, WXK_NUMLOCK)
347 KEY (KB_scrollLock, WXK_SCROLL)
348 KEY (KB_leftShift, WXK_SHIFT)
349 KEY (KB_rightShift, WXK_SHIFT)
350 KEY (KB_leftCtrl, WXK_CONTROL)
351 KEY (KB_rightCtrl, WXK_CONTROL)
352 KEY (KB_leftAlt, WXK_ALT)
353 KEY (KB_rightAlt, WXK_ALT)
354 KEY (KB_leftWindows, WXK_START)
355 KEY (KB_rightWindows, WXK_START)
356 KEY (KB_menu, WXK_MENU)
357 KEY (KB_sysReq, WXK_SNAPSHOT)
358 KEY (KB_esc, WXK_ESCAPE)
359 KEY (KB_1, '1')
360 KEY (KB_2, '2')
361 KEY (KB_3, '3')
362 KEY (KB_4, '4')
363 KEY (KB_5, '5')
364 KEY (KB_6, '6')
365 KEY (KB_7, '7')
366 KEY (KB_8, '8')
367 KEY (KB_9, '9')
368 KEY (KB_0, '0')
369 KEY (KB_minus, WXK_SUBTRACT)
370 KEY (KB_equals, WXK_ADD)
371 KEY (KB_backSlash, '\\')
372 KEY (KB_backspace, WXK_BACK)
373 KEY (KB_tab, WXK_TAB)
374 KEY (KB_Q, 'Q')
375 KEY (KB_W, 'W')
376 KEY (KB_E, 'E')
377 KEY (KB_R, 'R')
378 KEY (KB_T, 'T')
379 KEY (KB_Y, 'Y')
380 KEY (KB_U, 'U')
381 KEY (KB_I, 'I')
382 KEY (KB_O, 'O')
383 KEY (KB_P, 'P')
384 KEY (KB_leftSquareBrace,'[')
385 KEY (KB_rightSquareBrace,']')
386 KEY (KB_enter, WXK_RETURN)
387 KEY (KB_A, 'A')
388 KEY (KB_S, 'S')
389 KEY (KB_D, 'D')
390 KEY (KB_F, 'F')
391 KEY (KB_G, 'G')
392 KEY (KB_H, 'H')
393 KEY (KB_J, 'J')
394 KEY (KB_K, 'K')
395 KEY (KB_L, 'L')
396 KEY (KB_semicolon, ';')
397 KEY (KB_apostrophe, '\'')
398 KEY (KB_Z, 'Z')
399 KEY (KB_X, 'X')
400 KEY (KB_C, 'C')
401 KEY (KB_V, 'V')
402 KEY (KB_B, 'B')
403 KEY (KB_N, 'N')
404 KEY (KB_M, 'M')
405 KEY (KB_comma, ',')
406 KEY (KB_period, '.')
407 KEY (KB_divide, WXK_DIVIDE)
408 KEY (KB_space, WXK_SPACE)
409 KEY (KB_tilde, '~')
410
411 default:
412 key = EVT_asciiCode(event->message);
413 break;
414 }
415
544c782d
VS
416 #undef KEY
417
2b260bc3 418 return key;
ef344ff8
VS
419}
420
2b260bc3 421static long wxAsciiToKeyCode(event_t *event)
ef344ff8 422{
2b260bc3
VS
423 return (long)EVT_asciiCode(event->message);
424}
425
426static ibool wxWindowKeybHandler(window_t *wnd, event_t *e)
427{
2b260bc3 428 wxWindowMGL *win = (wxWindowMGL*)MGL_wmGetWindowUserData(wnd);
a200c35e
VS
429
430 if ( !win->IsEnabled() ) return FALSE;
431
985520bc
VS
432 wxPoint where;
433 MGL_wmCoordGlobalToLocal(win->GetHandle(),
434 e->where_x, e->where_y, &where.x, &where.y);
2b260bc3
VS
435
436 wxKeyEvent event;
437 event.SetEventObject(win);
438 event.SetTimestamp(e->when);
439 event.m_keyCode = wxScanToKeyCode(e);
440 event.m_scanCode = 0; // not used by wx at all
441 event.m_x = where.x;
442 event.m_y = where.y;
2b260bc3
VS
443 event.m_shiftDown = e->modifiers & EVT_SHIFTKEY;
444 event.m_controlDown = e->modifiers & EVT_CTRLSTATE;
445 event.m_altDown = e->modifiers & EVT_LEFTALT;
446 event.m_metaDown = e->modifiers & EVT_RIGHTALT;
447
448 if ( e->what == EVT_KEYUP )
449 {
450 event.SetEventType(wxEVT_KEY_UP);
451 return win->GetEventHandler()->ProcessEvent(event);
452 }
453 else
454 {
455 bool ret;
456 wxKeyEvent event2;
457
458 event.SetEventType(wxEVT_KEY_DOWN);
459 event2 = event;
460
461 ret = win->GetEventHandler()->ProcessEvent(event);
462
463#if wxUSE_ACCEL
464 if ( !ret )
465 {
466 for (wxWindowMGL *w = win; w; w = w->GetParent())
467 {
468 int command = w->GetAcceleratorTable()->GetCommand(event);
469 if ( command != -1 )
470 {
471 wxCommandEvent eventc(wxEVT_COMMAND_MENU_SELECTED, command);
472 ret = w->GetEventHandler()->ProcessEvent(eventc);
473 break;
474 }
475 if ( w->IsTopLevel() )
476 break;
477 }
478 }
479#endif // wxUSE_ACCEL
480
481 /* wxMSW doesn't send char events with Alt pressed */
482 /* Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
483 will only be sent if it is not in an accelerator table. */
484 event2.m_keyCode = wxAsciiToKeyCode(e);
485 if ( !ret && event2.m_keyCode != 0 )
486 {
487 event2.SetEventType(wxEVT_CHAR);
488 ret = win->GetEventHandler()->ProcessEvent(event2);
489 }
490
491 return ret;
492 }
7bdc1879 493}
a4bbc9f7
VS
494
495// ---------------------------------------------------------------------------
496// event tables
497// ---------------------------------------------------------------------------
498
ef344ff8 499// in wxUniv this class is abstract because it doesn't have DoPopupMenu()
a4bbc9f7
VS
500IMPLEMENT_ABSTRACT_CLASS(wxWindowMGL, wxWindowBase)
501
502BEGIN_EVENT_TABLE(wxWindowMGL, wxWindowBase)
a4bbc9f7
VS
503END_EVENT_TABLE()
504
505// ===========================================================================
506// implementation
507// ===========================================================================
508
509// ----------------------------------------------------------------------------
510// constructors and such
511// ----------------------------------------------------------------------------
512
513void wxWindowMGL::Init()
514{
515 // generic:
516 InitBase();
517
518 // mgl specific:
a4bbc9f7
VS
519 m_wnd = NULL;
520 m_isShown = TRUE;
521 m_isBeingDeleted = FALSE;
522 m_isEnabled = TRUE;
523 m_frozen = FALSE;
524 m_paintMGLDC = NULL;
2b260bc3 525 m_eraseBackground = -1;
a4bbc9f7
VS
526}
527
528// Destructor
529wxWindowMGL::~wxWindowMGL()
530{
531 m_isBeingDeleted = TRUE;
532
7cdbf02c
VS
533 if ( gs_mouseCapture == this )
534 ReleaseMouse();
d46330c5 535
7cdbf02c 536 if (gs_activeFrame == this)
d46330c5 537 {
31ae546e 538 gs_activeFrame = NULL;
d46330c5
VS
539 // activate next frame in Z-order:
540 if ( m_wnd->prev )
541 {
542 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
543 win->SetFocus();
544 }
d46330c5
VS
545 }
546
7cdbf02c 547 if ( gs_focusedWindow == this )
7bdc1879 548 KillFocus();
d46330c5 549
7cdbf02c
VS
550 if ( gs_windowUnderMouse == this )
551 gs_windowUnderMouse = NULL;
a4bbc9f7
VS
552
553 // VS: destroy children first and _then_ detach *this from its parent.
554 // If we'd do it the other way around, children wouldn't be able
555 // find their parent frame (see above).
556 DestroyChildren();
557
558 if ( m_parent )
559 m_parent->RemoveChild(this);
560
561 if ( m_wnd )
562 MGL_wmDestroyWindow(m_wnd);
563}
564
565// real construction (Init() must have been called before!)
566bool wxWindowMGL::Create(wxWindow *parent,
567 wxWindowID id,
568 const wxPoint& pos,
569 const wxSize& size,
570 long style,
571 const wxString& name)
572{
a4bbc9f7
VS
573 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
574 return FALSE;
575
544c782d 576 if ( parent )
7bdc1879 577 parent->AddChild(this);
a4bbc9f7 578
ef344ff8
VS
579 int x, y, w, h;
580 x = pos.x, y = pos.y;
581 if ( x == -1 )
582 x = 0; // FIXME_MGL, something better, see GTK+
583 if ( y == -1 )
584 y = 0; // FIXME_MGL, something better, see GTK+
585 w = WidthDefault(size.x);
586 h = HeightDefault(size.y);
72fa862b
VS
587
588 long mgl_style = 0;
7cdbf02c 589 window_t *wnd_parent = parent ? parent->GetHandle() : NULL;
544c782d 590
72fa862b 591 if ( !(style & wxNO_FULL_REPAINT_ON_RESIZE) )
544c782d 592 {
72fa862b 593 mgl_style |= MGL_WM_FULL_REPAINT_ON_RESIZE;
544c782d 594 }
72fa862b 595 if ( style & wxSTAY_ON_TOP )
544c782d 596 {
72fa862b 597 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
598 }
599 if ( style & wxPOPUP_WINDOW )
600 {
7cdbf02c 601 mgl_style |= MGL_WM_ALWAYS_ON_TOP;
544c782d
VS
602 // it is created hidden as other top level windows
603 m_isShown = FALSE;
7cdbf02c 604 wnd_parent = NULL;
544c782d 605 }
ef344ff8 606
7cdbf02c 607 m_wnd = MGL_wmCreateWindow(g_winMng, wnd_parent, x, y, w, h);
ef344ff8 608
72fa862b 609 MGL_wmSetWindowFlags(m_wnd, mgl_style);
a4bbc9f7
VS
610 MGL_wmSetWindowUserData(m_wnd, (void*) this);
611 MGL_wmSetWindowPainter(m_wnd, wxWindowPainter);
ef344ff8
VS
612 MGL_wmShowWindow(m_wnd, m_isShown);
613 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
614
615 MGL_wmPushWindowEventHandler(m_wnd, wxWindowMouseHandler, EVT_MOUSEEVT, 0);
616 MGL_wmPushWindowEventHandler(m_wnd, wxWindowKeybHandler, EVT_KEYEVT, 0);
985520bc 617
a4bbc9f7
VS
618 return TRUE;
619}
620
621// ---------------------------------------------------------------------------
622// basic operations
623// ---------------------------------------------------------------------------
624
625void wxWindowMGL::SetFocus()
626{
7cdbf02c
VS
627 if ( gs_focusedWindow )
628 gs_focusedWindow->KillFocus();
a4bbc9f7 629
7cdbf02c 630 gs_focusedWindow = this;
a4bbc9f7 631
2b260bc3 632 MGL_wmCaptureEvents(GetHandle(), EVT_KEYEVT, wxMGL_CAPTURE_KEYB);
a4bbc9f7 633
a4bbc9f7
VS
634#if wxUSE_CARET
635 // caret needs to be informed about focus change
636 wxCaret *caret = GetCaret();
7cdbf02c 637 if ( caret )
a4bbc9f7
VS
638 caret->OnSetFocus();
639#endif // wxUSE_CARET
32b8ec41 640
7cdbf02c
VS
641 wxWindowMGL *active = wxGetTopLevelParent(this);
642 if ( active != gs_activeFrame )
a4bbc9f7 643 {
7cdbf02c
VS
644 if ( gs_activeFrame )
645 {
646 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, gs_activeFrame->GetId());
647 event.SetEventObject(gs_activeFrame);
648 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
649 }
650
651 gs_activeFrame = active;
652 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, gs_activeFrame->GetId());
653 event.SetEventObject(gs_activeFrame);
654 gs_activeFrame->GetEventHandler()->ProcessEvent(event);
a4bbc9f7
VS
655 }
656
657 wxFocusEvent event(wxEVT_SET_FOCUS, GetId());
658 event.SetEventObject(this);
659 GetEventHandler()->ProcessEvent(event);
660}
32b8ec41 661
a4bbc9f7 662void wxWindowMGL::KillFocus()
32b8ec41 663{
7cdbf02c
VS
664 if ( gs_focusedWindow != this ) return;
665 gs_focusedWindow = NULL;
a4bbc9f7 666
7bdc1879
VS
667 if ( m_isBeingDeleted ) return;
668
a4bbc9f7
VS
669 MGL_wmUncaptureEvents(GetHandle(), wxMGL_CAPTURE_KEYB);
670
671#if wxUSE_CARET
672 // caret needs to be informed about focus change
673 wxCaret *caret = GetCaret();
7bdc1879 674 if ( caret )
a4bbc9f7
VS
675 caret->OnKillFocus();
676#endif // wxUSE_CARET
677
7bdc1879 678 if ( IsTopLevel() )
a4bbc9f7 679 {
544c782d 680 // FIXME_MGL - this is wrong, see wxGTK!
a4bbc9f7
VS
681 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, GetId());
682 event.SetEventObject(this);
683 GetEventHandler()->ProcessEvent(event);
684 }
685
686 wxFocusEvent event(wxEVT_KILL_FOCUS, GetId());
687 event.SetEventObject(this);
688 GetEventHandler()->ProcessEvent(event);
32b8ec41
VZ
689}
690
a4bbc9f7
VS
691// ----------------------------------------------------------------------------
692// this wxWindowBase function is implemented here (in platform-specific file)
693// because it is static and so couldn't be made virtual
694// ----------------------------------------------------------------------------
32b8ec41
VZ
695wxWindow *wxWindowBase::FindFocus()
696{
7cdbf02c 697 return (wxWindow*)gs_focusedWindow;
a4bbc9f7
VS
698}
699
700bool wxWindowMGL::Show(bool show)
701{
702 if ( !wxWindowBase::Show(show) )
703 return FALSE;
704
705 MGL_wmShowWindow(m_wnd, show);
ead60ec5
VS
706
707 if (!show && gs_activeFrame == this)
708 {
709 // activate next frame in Z-order:
710 if ( m_wnd->prev )
711 {
712 wxWindowMGL *win = (wxWindowMGL*)m_wnd->prev->userData;
713 win->SetFocus();
714 }
715 }
716
a4bbc9f7
VS
717 return TRUE;
718}
719
720// Raise the window to the top of the Z order
721void wxWindowMGL::Raise()
722{
723 MGL_wmRaiseWindow(m_wnd);
724}
725
726// Lower the window to the bottom of the Z order
727void wxWindowMGL::Lower()
728{
729 MGL_wmLowerWindow(m_wnd);
730}
731
732void wxWindowMGL::CaptureMouse()
733{
7cdbf02c
VS
734 if ( gs_mouseCapture )
735 MGL_wmUncaptureEvents(gs_mouseCapture->m_wnd, wxMGL_CAPTURE_MOUSE);
736
737 gs_mouseCapture = this;
a4bbc9f7
VS
738 MGL_wmCaptureEvents(m_wnd, EVT_MOUSEEVT, wxMGL_CAPTURE_MOUSE);
739}
740
741void wxWindowMGL::ReleaseMouse()
742{
7cdbf02c
VS
743 wxASSERT_MSG( gs_mouseCapture == this, wxT("attempt to release mouse, but this window hasn't captured it") )
744
a4bbc9f7 745 MGL_wmUncaptureEvents(m_wnd, wxMGL_CAPTURE_MOUSE);
7cdbf02c 746 gs_mouseCapture = NULL;
a4bbc9f7
VS
747}
748
749/* static */ wxWindow *wxWindowBase::GetCapture()
750{
7cdbf02c 751 return (wxWindow*)gs_mouseCapture;
a4bbc9f7
VS
752}
753
754bool wxWindowMGL::SetCursor(const wxCursor& cursor)
755{
756 if ( !wxWindowBase::SetCursor(cursor) )
757 {
758 // no change
759 return FALSE;
760 }
761
762 if ( m_cursor.Ok() )
763 MGL_wmSetWindowCursor(m_wnd, *m_cursor.GetMGLCursor());
ef344ff8
VS
764 else
765 MGL_wmSetWindowCursor(m_wnd, *wxSTANDARD_CURSOR->GetMGLCursor());
a4bbc9f7
VS
766
767 return TRUE;
768}
769
770void wxWindowMGL::WarpPointer(int x, int y)
771{
772 ClientToScreen(&x, &y);
773 EVT_setMousePos(x, y);
774}
775
776#if WXWIN_COMPATIBILITY
777// If nothing defined for this, try the parent.
778// E.g. we may be a button loaded from a resource, with no callback function
779// defined.
780void wxWindowMGL::OnCommand(wxWindow& win, wxCommandEvent& event)
781{
782 if ( GetEventHandler()->ProcessEvent(event) )
783 return;
784 if ( m_parent )
785 m_parent->GetEventHandler()->OnCommand(win, event);
786}
787#endif // WXWIN_COMPATIBILITY_2
788
789#if WXWIN_COMPATIBILITY
790wxObject* wxWindowMGL::GetChild(int number) const
791{
792 // Return a pointer to the Nth object in the Panel
793 wxNode *node = GetChildren().First();
794 int n = number;
795 while (node && n--)
796 node = node->Next();
797 if ( node )
798 {
799 wxObject *obj = (wxObject *)node->Data();
800 return(obj);
801 }
802 else
803 return NULL;
804}
805#endif // WXWIN_COMPATIBILITY
806
807// Set this window to be the child of 'parent'.
808bool wxWindowMGL::Reparent(wxWindowBase *parent)
809{
810 if ( !wxWindowBase::Reparent(parent) )
811 return FALSE;
812
813 MGL_wmReparentWindow(m_wnd, parent->GetHandle());
814
815 return TRUE;
816}
817
818
819// ---------------------------------------------------------------------------
820// drag and drop
821// ---------------------------------------------------------------------------
822
823#if wxUSE_DRAG_AND_DROP
824
825void wxWindowMGL::SetDropTarget(wxDropTarget *pDropTarget)
826{
827 if ( m_dropTarget != 0 ) {
828 m_dropTarget->Revoke(m_hWnd);
829 delete m_dropTarget;
830 }
831
832 m_dropTarget = pDropTarget;
833 if ( m_dropTarget != 0 )
834 m_dropTarget->Register(m_hWnd);
835}
836// FIXME_MGL
837#endif // wxUSE_DRAG_AND_DROP
838
839// old style file-manager drag&drop support: we retain the old-style
840// DragAcceptFiles in parallel with SetDropTarget.
841void wxWindowMGL::DragAcceptFiles(bool accept)
842{
843#if 0 // FIXME_MGL
844 HWND hWnd = GetHwnd();
845 if ( hWnd )
846 ::DragAcceptFiles(hWnd, (BOOL)accept);
847#endif
848}
849
850// ---------------------------------------------------------------------------
851// moving and resizing
852// ---------------------------------------------------------------------------
853
854// Get total size
855void wxWindowMGL::DoGetSize(int *x, int *y) const
856{
857 if (x) *x = m_wnd->width;
858 if (y) *y = m_wnd->height;
859}
860
861void wxWindowMGL::DoGetPosition(int *x, int *y) const
862{
863 if (x) *x = m_wnd->x;
864 if (y) *y = m_wnd->y;
865}
866
867void wxWindowMGL::DoScreenToClient(int *x, int *y) const
868{
869 int ax, ay;
985520bc 870 MGL_wmCoordGlobalToLocal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 871 if (x)
985520bc 872 (*x) += ax;
a4bbc9f7 873 if (y)
985520bc 874 (*y) += ay;
a4bbc9f7
VS
875}
876
877void wxWindowMGL::DoClientToScreen(int *x, int *y) const
878{
879 int ax, ay;
985520bc 880 MGL_wmCoordLocalToGlobal(m_wnd, 0, 0, &ax, &ay);
a4bbc9f7 881 if (x)
985520bc 882 (*x) += ax;
a4bbc9f7 883 if (y)
985520bc 884 (*y) += ay;
a4bbc9f7
VS
885}
886
887// Get size *available for subwindows* i.e. excluding menu bar etc.
888void wxWindowMGL::DoGetClientSize(int *x, int *y) const
889{
890 DoGetSize(x, y);
891}
892
893void wxWindowMGL::DoMoveWindow(int x, int y, int width, int height)
894{
895 MGL_wmSetWindowPosition(GetHandle(), x, y, width, height);
896}
897
898// set the size of the window: if the dimensions are positive, just use them,
899// but if any of them is equal to -1, it means that we must find the value for
900// it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
901// which case -1 is a valid value for x and y)
902//
903// If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
904// the width/height to best suit our contents, otherwise we reuse the current
905// width/height
906void wxWindowMGL::DoSetSize(int x, int y, int width, int height, int sizeFlags)
907{
908 // get the current size and position...
909 int currentX, currentY;
910 GetPosition(&currentX, &currentY);
911 int currentW,currentH;
912 GetSize(&currentW, &currentH);
913
914 // ... and don't do anything (avoiding flicker) if it's already ok
915 if ( x == currentX && y == currentY &&
916 width == currentW && height == currentH )
917 {
918 return;
919 }
920
921 if ( x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
922 x = currentX;
923 if ( y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
924 y = currentY;
925
a4bbc9f7 926 AdjustForParentClientOrigin(x, y, sizeFlags);
a4bbc9f7
VS
927
928 wxSize size(-1, -1);
929 if ( width == -1 )
930 {
931 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
932 {
933 size = DoGetBestSize();
934 width = size.x;
935 }
936 else
937 {
938 // just take the current one
939 width = currentW;
940 }
941 }
942
943 if ( height == -1 )
944 {
945 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
946 {
947 if ( size.x == -1 )
948 {
949 size = DoGetBestSize();
950 }
951 //else: already called DoGetBestSize() above
952
953 height = size.y;
954 }
955 else
956 {
957 // just take the current one
958 height = currentH;
959 }
960 }
961
962 DoMoveWindow(x, y, width, height);
963
964 wxSizeEvent event(wxSize(width, height), GetId());
965 event.SetEventObject(this);
966 GetEventHandler()->ProcessEvent(event);
967}
968
969void wxWindowMGL::DoSetClientSize(int width, int height)
970{
971 SetSize(width, height);
972}
973
974// ---------------------------------------------------------------------------
975// text metrics
976// ---------------------------------------------------------------------------
977
978int wxWindowMGL::GetCharHeight() const
979{
980 wxScreenDC dc;
981 dc.SetFont(m_font);
982 return dc.GetCharHeight();
983}
984
985int wxWindowMGL::GetCharWidth() const
986{
987 wxScreenDC dc;
988 dc.SetFont(m_font);
989 return dc.GetCharWidth();
990}
991
992void wxWindowMGL::GetTextExtent(const wxString& string,
993 int *x, int *y,
994 int *descent, int *externalLeading,
995 const wxFont *theFont) const
996{
997 wxScreenDC dc;
998 if (!theFont)
999 theFont = &m_font;
1000 dc.GetTextExtent(string, x, y, descent, externalLeading, (wxFont*)theFont);
1001}
1002
1003#if wxUSE_CARET && WXWIN_COMPATIBILITY
1004// ---------------------------------------------------------------------------
1005// Caret manipulation
1006// ---------------------------------------------------------------------------
1007
1008void wxWindowMGL::CreateCaret(int w, int h)
1009{
1010 SetCaret(new wxCaret(this, w, h));
1011}
1012
1013void wxWindowMGL::CreateCaret(const wxBitmap *WXUNUSED(bitmap))
1014{
1015 wxFAIL_MSG("not implemented");
1016}
1017
1018void wxWindowMGL::ShowCaret(bool show)
1019{
1020 wxCHECK_RET( m_caret, "no caret to show" );
1021
1022 m_caret->Show(show);
1023}
1024
1025void wxWindowMGL::DestroyCaret()
1026{
1027 SetCaret(NULL);
1028}
1029
1030void wxWindowMGL::SetCaretPos(int x, int y)
1031{
1032 wxCHECK_RET( m_caret, "no caret to move" );
1033
1034 m_caret->Move(x, y);
1035}
1036
1037void wxWindowMGL::GetCaretPos(int *x, int *y) const
1038{
1039 wxCHECK_RET( m_caret, "no caret to get position of" );
1040
1041 m_caret->GetPosition(x, y);
1042}
1043#endif // wxUSE_CARET
1044
1045
a4bbc9f7
VS
1046// ---------------------------------------------------------------------------
1047// painting
1048// ---------------------------------------------------------------------------
1049
1050void wxWindowMGL::Clear()
1051{
1052 wxClientDC dc((wxWindow *)this);
1053 wxBrush brush(GetBackgroundColour(), wxSOLID);
1054 dc.SetBackground(brush);
1055 dc.Clear();
1056}
1057
2b260bc3 1058void wxWindowMGL::Refresh(bool eraseBack, const wxRect *rect)
a4bbc9f7 1059{
2b260bc3
VS
1060 if ( m_eraseBackground == -1 )
1061 m_eraseBackground = eraseBack;
1062 else
1063 m_eraseBackground |= eraseBack;
1064
a4bbc9f7
VS
1065 if ( rect )
1066 {
1067 rect_t r;
1068 r.left = rect->GetLeft(), r.right = rect->GetRight();
1069 r.top = rect->GetTop(), r.bottom = rect->GetBottom();
1070 MGL_wmInvalidateWindowRect(GetHandle(), &r);
1071 }
1072 else
1073 MGL_wmInvalidateWindow(GetHandle());
1074}
1075
1076void wxWindowMGL::Update()
1077{
a4bbc9f7
VS
1078 if ( !m_frozen )
1079 MGL_wmUpdateDC(g_winMng);
1080}
1081
1082void wxWindowMGL::Freeze()
1083{
1084 m_frozen = TRUE;
1085 m_refreshAfterThaw = FALSE;
1086}
1087
1088void wxWindowMGL::Thaw()
1089{
1090 m_frozen = FALSE;
1091 if ( m_refreshAfterThaw )
1092 Refresh();
1093}
1094
a4bbc9f7
VS
1095void wxWindowMGL::HandlePaint(MGLDevCtx *dc)
1096{
1097 if ( m_frozen )
1098 {
1099 // Don't paint anything if the window is frozen.
fd495ab3 1100 m_refreshAfterThaw = TRUE;
a4bbc9f7
VS
1101 return;
1102 }
ef344ff8 1103
fd495ab3
VS
1104 MGLRegion clip;
1105 dc->getClipRegion(clip);
1106 m_updateRegion = wxRegion(clip);
a4bbc9f7
VS
1107 m_paintMGLDC = dc;
1108
2b260bc3 1109 if ( m_eraseBackground != 0 )
7bdc1879 1110 {
2b260bc3
VS
1111 wxWindowDC dc((wxWindow*)this);
1112 wxEraseEvent eventEr(m_windowId, &dc);
1113 eventEr.SetEventObject(this);
1114 GetEventHandler()->ProcessEvent(eventEr);
7bdc1879 1115 }
2b260bc3 1116 m_eraseBackground = -1;
a4bbc9f7
VS
1117
1118 wxNcPaintEvent eventNc(GetId());
1119 eventNc.SetEventObject(this);
1120 GetEventHandler()->ProcessEvent(eventNc);
1121
1122 wxPaintEvent eventPt(GetId());
1123 eventPt.SetEventObject(this);
1124 GetEventHandler()->ProcessEvent(eventPt);
a4bbc9f7 1125
7bdc1879 1126 m_paintMGLDC = NULL;
fd495ab3 1127 m_updateRegion.Clear();
a4bbc9f7
VS
1128}
1129
1130
1131// Find the wxWindow at the current mouse position, returning the mouse
1132// position.
1133wxWindow* wxFindWindowAtPointer(wxPoint& pt)
1134{
1135 return wxFindWindowAtPoint(pt = wxGetMousePosition());
1136}
1137
1138wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
1139{
1140 window_t *wnd = MGL_wmGetWindowAtPosition(g_winMng, pt.x, pt.y);
1141 return (wxWindow*)wnd->userData;
32b8ec41 1142}