more corrections to interactive resizing
[wxWidgets.git] / src / univ / topluniv.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: topluniv.cpp
3 // Author: Vaclav Slavik
4 // Id: $Id$
5 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "univtoplevel.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #include "wx/defs.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #endif
34
35 #include "wx/toplevel.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/bitmap.h"
38 #include "wx/image.h"
39 #include "wx/cshelp.h"
40 #include "wx/evtloop.h"
41
42
43 // ----------------------------------------------------------------------------
44 // event tables
45 // ----------------------------------------------------------------------------
46
47 BEGIN_EVENT_TABLE(wxTopLevelWindow, wxTopLevelWindowNative)
48 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow)
49 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint)
50 EVT_MENU_RANGE(wxID_CLOSE_FRAME, wxID_RESTORE_FRAME, wxTopLevelWindow::OnSystemMenu)
51 END_EVENT_TABLE()
52
53 WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow)
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
59 int wxTopLevelWindow::ms_drawDecorations = -1;
60 int wxTopLevelWindow::ms_canIconize = -1;
61
62 void wxTopLevelWindow::Init()
63 {
64 m_isActive = FALSE;
65 m_windowStyle = 0;
66 m_pressedButton = 0;
67 }
68
69 bool wxTopLevelWindow::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxString& title,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString &name)
76 {
77 // init them to avoid compiler warnings
78 long styleOrig = 0,
79 exstyleOrig = 0;
80
81 if ( ms_drawDecorations == -1 )
82 {
83 ms_drawDecorations =
84 !wxSystemSettings::HasFeature(wxSYS_CAN_DRAW_FRAME_DECORATIONS)
85 || wxGetEnv(wxT("WXDECOR"), NULL);
86 // FIXME -- wxUniv should provide a way to force non-native decorations!
87 // $WXDECOR is just a hack in absence of better wxUniv solution
88 }
89
90 if ( ms_canIconize == -1 )
91 {
92 ms_canIconize = wxSystemSettings::HasFeature(wxSYS_CAN_ICONIZE_FRAME);
93 }
94
95 if ( ms_drawDecorations )
96 {
97 CreateInputHandler(wxINP_HANDLER_TOPLEVEL);
98
99 styleOrig = style;
100 exstyleOrig = GetExtraStyle();
101 style &= ~(wxCAPTION | wxMINIMIZE_BOX | wxMAXIMIZE_BOX |
102 wxSYSTEM_MENU | wxRESIZE_BORDER | wxFRAME_TOOL_WINDOW |
103 wxTHICK_FRAME);
104 style = wxSIMPLE_BORDER;
105 SetExtraStyle(exstyleOrig &
106 ~(wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP));
107 }
108
109 if ( !wxTopLevelWindowNative::Create(parent, id, title, pos,
110 size, style, name) )
111 return FALSE;
112
113 if ( ms_drawDecorations )
114 {
115 m_windowStyle = styleOrig;
116 m_exStyle = exstyleOrig;
117 }
118
119 return TRUE;
120 }
121
122 bool wxTopLevelWindow::ShowFullScreen(bool show, long style)
123 {
124 if ( show == IsFullScreen() ) return FALSE;
125
126 if ( ms_drawDecorations )
127 {
128 if ( show )
129 {
130 m_fsSavedStyle = m_windowStyle;
131 if ( style & wxFULLSCREEN_NOBORDER )
132 m_windowStyle |= wxSIMPLE_BORDER;
133 if ( style & wxFULLSCREEN_NOCAPTION )
134 m_windowStyle &= ~wxCAPTION;
135 }
136 else
137 {
138 m_windowStyle = m_fsSavedStyle;
139 }
140 }
141
142 return wxTopLevelWindowNative::ShowFullScreen(show, style);
143 }
144
145 long wxTopLevelWindow::GetDecorationsStyle() const
146 {
147 long style = 0;
148
149 if ( m_windowStyle & wxCAPTION )
150 {
151 style |= wxTOPLEVEL_TITLEBAR | wxTOPLEVEL_BUTTON_CLOSE;
152 if ( (m_windowStyle & wxMINIMIZE_BOX) && ms_canIconize )
153 style |= wxTOPLEVEL_BUTTON_ICONIZE;
154 if ( m_windowStyle & wxMAXIMIZE_BOX )
155 {
156 if ( IsMaximized() )
157 style |= wxTOPLEVEL_BUTTON_RESTORE;
158 else
159 style |= wxTOPLEVEL_BUTTON_MAXIMIZE;
160 }
161 #if wxUSE_HELP
162 if ( m_exStyle & (wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP))
163 style |= wxTOPLEVEL_BUTTON_HELP;
164 #endif
165 }
166 if ( (m_windowStyle & (wxSIMPLE_BORDER | wxNO_BORDER)) == 0 )
167 style |= wxTOPLEVEL_BORDER;
168 if ( m_windowStyle & (wxRESIZE_BORDER | wxTHICK_FRAME) )
169 style |= wxTOPLEVEL_RESIZEABLE;
170
171 if ( IsMaximized() )
172 style |= wxTOPLEVEL_MAXIMIZED;
173 if ( GetIcon().Ok() )
174 style |= wxTOPLEVEL_ICON;
175 if ( m_isActive )
176 style |= wxTOPLEVEL_ACTIVE;
177
178 return style;
179 }
180
181 void wxTopLevelWindow::RefreshTitleBar()
182 {
183 wxNcPaintEvent event(GetId());
184 event.SetEventObject(this);
185 GetEventHandler()->ProcessEvent(event);
186 }
187
188 // ----------------------------------------------------------------------------
189 // client area handling
190 // ----------------------------------------------------------------------------
191
192 wxPoint wxTopLevelWindow::GetClientAreaOrigin() const
193 {
194 if ( ms_drawDecorations )
195 {
196 int w, h;
197 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
198 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
199 wxSize(w, h));
200 rect = m_renderer->GetFrameClientArea(rect,
201 GetDecorationsStyle());
202 return rect.GetPosition();
203 }
204 else
205 {
206 return wxTopLevelWindowNative::GetClientAreaOrigin();
207 }
208 }
209
210 void wxTopLevelWindow::DoGetClientSize(int *width, int *height) const
211 {
212 if ( ms_drawDecorations )
213 {
214 int w, h;
215 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
216 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
217 wxSize(w, h));
218 rect = m_renderer->GetFrameClientArea(rect,
219 GetDecorationsStyle());
220 if ( width )
221 *width = rect.width;
222 if ( height )
223 *height = rect.height;
224 }
225 else
226 wxTopLevelWindowNative::DoGetClientSize(width, height);
227 }
228
229 void wxTopLevelWindow::DoSetClientSize(int width, int height)
230 {
231 if ( ms_drawDecorations )
232 {
233 wxSize size = m_renderer->GetFrameTotalSize(wxSize(width, height),
234 GetDecorationsStyle());
235 wxTopLevelWindowNative::DoSetClientSize(size.x, size.y);
236 }
237 else
238 wxTopLevelWindowNative::DoSetClientSize(width, height);
239 }
240
241 void wxTopLevelWindow::OnNcPaint(wxPaintEvent& event)
242 {
243 if ( !ms_drawDecorations || !m_renderer )
244 event.Skip();
245 else
246 {
247 // get the window rect
248 wxRect rect;
249 wxSize size = GetSize();
250 rect.x =
251 rect.y = 0;
252 rect.width = size.x;
253 rect.height = size.y;
254
255 wxWindowDC dc(this);
256 m_renderer->DrawFrameTitleBar(dc, rect,
257 GetTitle(), m_titlebarIcon,
258 GetDecorationsStyle(),
259 m_pressedButton,
260 wxCONTROL_PRESSED);
261 }
262 }
263
264 long wxTopLevelWindow::HitTest(const wxPoint& pt) const
265 {
266 int w, h;
267 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
268 wxRect rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w, h));
269
270 return m_renderer->HitTestFrame(rect, pt+GetClientAreaOrigin(), GetDecorationsStyle());
271 }
272
273 int wxTopLevelWindow::GetMinWidth() const
274 {
275 if ( ms_drawDecorations )
276 {
277 return wxMax(wxTopLevelWindowNative::GetMinWidth(),
278 m_renderer->GetFrameMinSize(GetDecorationsStyle()).x);
279 }
280 else
281 return wxTopLevelWindowNative::GetMinWidth();
282 }
283
284 int wxTopLevelWindow::GetMinHeight() const
285 {
286 if ( ms_drawDecorations )
287 {
288 return wxMax(wxTopLevelWindowNative::GetMinHeight(),
289 m_renderer->GetFrameMinSize(GetDecorationsStyle()).y);
290 }
291 else
292 return wxTopLevelWindowNative::GetMinHeight();
293 }
294
295 // ----------------------------------------------------------------------------
296 // icons
297 // ----------------------------------------------------------------------------
298
299 void wxTopLevelWindow::SetIcon(const wxIcon& icon)
300 {
301 wxTopLevelWindowNative::SetIcon(icon);
302
303 if ( ms_drawDecorations && m_renderer )
304 {
305 wxSize size = m_renderer->GetFrameIconSize();
306
307 if ( !icon.Ok() || size.x == -1 )
308 m_titlebarIcon = icon;
309 else
310 {
311 wxBitmap bmp1;
312 bmp1.CopyFromIcon(icon);
313 if ( !bmp1.Ok() )
314 m_titlebarIcon = wxNullIcon;
315 else if ( bmp1.GetWidth() == size.x && bmp1.GetHeight() == size.y )
316 m_titlebarIcon = icon;
317 else
318 {
319 wxImage img = bmp1.ConvertToImage();
320 img.Rescale(size.x, size.y);
321 m_titlebarIcon.CopyFromBitmap(wxBitmap(img));
322 }
323 }
324 }
325 }
326
327 // ----------------------------------------------------------------------------
328 // interactive manipulation
329 // ----------------------------------------------------------------------------
330
331
332 static bool wxGetResizingCursor(long hitTestResult, wxCursor& cursor)
333 {
334 if ( hitTestResult & wxHT_TOPLEVEL_ANY_BORDER )
335 {
336 switch (hitTestResult)
337 {
338 case wxHT_TOPLEVEL_BORDER_N:
339 case wxHT_TOPLEVEL_BORDER_S:
340 cursor = wxCursor(wxCURSOR_SIZENS);
341 break;
342 case wxHT_TOPLEVEL_BORDER_W:
343 case wxHT_TOPLEVEL_BORDER_E:
344 cursor = wxCursor(wxCURSOR_SIZEWE);
345 break;
346 case wxHT_TOPLEVEL_BORDER_NE:
347 case wxHT_TOPLEVEL_BORDER_SW:
348 cursor = wxCursor(wxCURSOR_SIZENESW);
349 break;
350 case wxHT_TOPLEVEL_BORDER_NW:
351 case wxHT_TOPLEVEL_BORDER_SE:
352 cursor = wxCursor(wxCURSOR_SIZENWSE);
353 break;
354 default:
355 return FALSE;
356 break;
357 }
358 return TRUE;
359 }
360
361 return FALSE;
362 }
363
364 #define wxINTERACTIVE_RESIZE_DIR \
365 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E | \
366 wxINTERACTIVE_RESIZE_S | wxINTERACTIVE_RESIZE_N)
367
368 struct wxInteractiveMoveData
369 {
370 wxTopLevelWindow *m_window;
371 wxEventLoop *m_evtLoop;
372 int m_flags;
373 wxRect m_rect;
374 wxRect m_rectOrig;
375 wxPoint m_pos;
376 wxSize m_minSize, m_maxSize;
377 bool m_sizingCursor;
378 };
379
380 class wxInteractiveMoveHandler : public wxEvtHandler
381 {
382 public:
383 wxInteractiveMoveHandler(wxInteractiveMoveData& data) : m_data(data) {}
384
385 private:
386 DECLARE_EVENT_TABLE()
387 void OnMouseMove(wxMouseEvent& event);
388 void OnMouseDown(wxMouseEvent& event);
389 void OnMouseUp(wxMouseEvent& event);
390 void OnKeyDown(wxKeyEvent& event);
391
392 wxInteractiveMoveData& m_data;
393 };
394
395 BEGIN_EVENT_TABLE(wxInteractiveMoveHandler, wxEvtHandler)
396 EVT_MOTION(wxInteractiveMoveHandler::OnMouseMove)
397 EVT_LEFT_DOWN(wxInteractiveMoveHandler::OnMouseDown)
398 EVT_LEFT_UP(wxInteractiveMoveHandler::OnMouseUp)
399 EVT_KEY_DOWN(wxInteractiveMoveHandler::OnKeyDown)
400 END_EVENT_TABLE()
401
402
403 static inline LINKAGEMODE
404 void wxApplyResize(wxInteractiveMoveData& data, const wxPoint& diff)
405 {
406 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
407 {
408 data.m_rect.x += diff.x;
409 data.m_rect.width -= diff.x;
410 }
411 else if ( data.m_flags & wxINTERACTIVE_RESIZE_E )
412 {
413 data.m_rect.width += diff.x;
414 }
415 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
416 {
417 data.m_rect.y += diff.y;
418 data.m_rect.height -= diff.y;
419 }
420 else if ( data.m_flags & wxINTERACTIVE_RESIZE_S )
421 {
422 data.m_rect.height += diff.y;
423 }
424
425 if ( data.m_minSize.x != -1 && data.m_rect.width < data.m_minSize.x )
426 {
427 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
428 data.m_rect.x -= data.m_minSize.x - data.m_rect.width;
429 data.m_rect.width = data.m_minSize.x;
430 }
431 if ( data.m_maxSize.x != -1 && data.m_rect.width > data.m_maxSize.x )
432 {
433 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
434 data.m_rect.x -= data.m_minSize.x - data.m_rect.width;
435 data.m_rect.width = data.m_maxSize.x;
436 }
437 if ( data.m_minSize.y != -1 && data.m_rect.height < data.m_minSize.y )
438 {
439 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
440 data.m_rect.y -= data.m_minSize.y - data.m_rect.height;
441 data.m_rect.height = data.m_minSize.y;
442 }
443 if ( data.m_maxSize.y != -1 && data.m_rect.height > data.m_maxSize.y )
444 {
445 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
446 data.m_rect.y -= data.m_minSize.y - data.m_rect.height;
447 data.m_rect.height = data.m_maxSize.y;
448 }
449 }
450
451 void wxInteractiveMoveHandler::OnMouseMove(wxMouseEvent& event)
452 {
453 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
454 event.Skip();
455
456 else if ( m_data.m_flags & wxINTERACTIVE_MOVE )
457 {
458 wxPoint diff = wxGetMousePosition() - m_data.m_pos;
459 m_data.m_rect = m_data.m_rectOrig;
460 m_data.m_rect.Offset(diff);
461 m_data.m_window->Move(m_data.m_rect.GetPosition());
462 }
463
464 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE )
465 {
466 wxPoint diff = wxGetMousePosition() - m_data.m_pos;
467 m_data.m_rect = m_data.m_rectOrig;
468 wxApplyResize(m_data, diff);
469 m_data.m_window->SetSize(m_data.m_rect);
470 }
471 }
472
473 void wxInteractiveMoveHandler::OnMouseDown(wxMouseEvent& event)
474 {
475 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
476 {
477 m_data.m_evtLoop->Exit();
478 }
479 }
480
481 void wxInteractiveMoveHandler::OnKeyDown(wxKeyEvent& event)
482 {
483 wxPoint diff(-1,-1);
484
485 switch ( event.GetKeyCode() )
486 {
487 case WXK_UP: diff = wxPoint(0, -16); break;
488 case WXK_DOWN: diff = wxPoint(0, 16); break;
489 case WXK_LEFT: diff = wxPoint(-16, 0); break;
490 case WXK_RIGHT: diff = wxPoint(16, 0); break;
491 case WXK_ESCAPE:
492 m_data.m_window->SetSize(m_data.m_rectOrig);
493 m_data.m_evtLoop->Exit();
494 return;
495 case WXK_RETURN:
496 m_data.m_evtLoop->Exit();
497 return;
498 }
499
500 if ( diff.x != -1 )
501 {
502 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
503 {
504 m_data.m_flags &= ~wxINTERACTIVE_WAIT_FOR_INPUT;
505 if ( m_data.m_sizingCursor )
506 {
507 wxEndBusyCursor();
508 m_data.m_sizingCursor = FALSE;
509 }
510
511 if ( m_data.m_flags & wxINTERACTIVE_MOVE )
512 {
513 m_data.m_pos = m_data.m_window->GetPosition() +
514 wxPoint(m_data.m_window->GetSize().x/2, 8);
515 }
516 }
517
518 wxPoint warp;
519 bool changeCur = FALSE;
520
521 if ( m_data.m_flags & wxINTERACTIVE_MOVE )
522 {
523 m_data.m_rect.Offset(diff);
524 m_data.m_window->Move(m_data.m_rect.GetPosition());
525 warp = wxPoint(m_data.m_window->GetSize().x/2, 8);
526 }
527 else /* wxINTERACTIVE_RESIZE */
528 {
529 if ( !(m_data.m_flags &
530 (wxINTERACTIVE_RESIZE_N | wxINTERACTIVE_RESIZE_S)) )
531 {
532 if ( diff.y < 0 )
533 {
534 m_data.m_flags |= wxINTERACTIVE_RESIZE_N;
535 m_data.m_pos.y = m_data.m_window->GetPosition().y;
536 changeCur = TRUE;
537 }
538 else if ( diff.y > 0 )
539 {
540 m_data.m_flags |= wxINTERACTIVE_RESIZE_S;
541 m_data.m_pos.y = m_data.m_window->GetPosition().y +
542 m_data.m_window->GetSize().y;
543 changeCur = TRUE;
544 }
545 }
546 if ( !(m_data.m_flags &
547 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E)) )
548 {
549 if ( diff.x < 0 )
550 {
551 m_data.m_flags |= wxINTERACTIVE_RESIZE_W;
552 m_data.m_pos.x = m_data.m_window->GetPosition().x;
553 changeCur = TRUE;
554 }
555 else if ( diff.x > 0 )
556 {
557 m_data.m_flags |= wxINTERACTIVE_RESIZE_E;
558 m_data.m_pos.x = m_data.m_window->GetPosition().x +
559 m_data.m_window->GetSize().x;
560 changeCur = TRUE;
561 }
562 }
563
564 wxApplyResize(m_data, diff);
565 m_data.m_window->SetSize(m_data.m_rect);
566
567 if ( m_data.m_flags & wxINTERACTIVE_RESIZE_W )
568 warp.x = 0;
569 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE_E )
570 warp.x = m_data.m_window->GetSize().x-1;
571 else
572 warp.x = wxGetMousePosition().x - m_data.m_window->GetPosition().x;
573
574 if ( m_data.m_flags & wxINTERACTIVE_RESIZE_N )
575 warp.y = 0;
576 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE_S )
577 warp.y = m_data.m_window->GetSize().y-1;
578 else
579 warp.y = wxGetMousePosition().y - m_data.m_window->GetPosition().y;
580 }
581
582 warp -= m_data.m_window->GetClientAreaOrigin();
583 m_data.m_window->WarpPointer(warp.x, warp.y);
584
585 if ( changeCur )
586 {
587 long hit = m_data.m_window->HitTest(warp);
588 wxCursor cur;
589 if ( wxGetResizingCursor(hit, cur) )
590 {
591 if ( m_data.m_sizingCursor )
592 wxEndBusyCursor();
593 wxBeginBusyCursor(&cur);
594 m_data.m_sizingCursor = TRUE;
595 }
596 }
597 }
598 }
599
600 void wxInteractiveMoveHandler::OnMouseUp(wxMouseEvent& event)
601 {
602 m_data.m_evtLoop->Exit();
603 }
604
605
606 void wxTopLevelWindow::InteractiveMove(int flags)
607 {
608 wxASSERT_MSG( !((flags & wxINTERACTIVE_MOVE) && (flags & wxINTERACTIVE_RESIZE)),
609 wxT("can't move and resize window at the same time") );
610
611 wxASSERT_MSG( !(flags & wxINTERACTIVE_RESIZE) ||
612 (flags & wxINTERACTIVE_WAIT_FOR_INPUT) ||
613 (flags & wxINTERACTIVE_RESIZE_DIR),
614 wxT("direction of resizing not specified") );
615
616 wxInteractiveMoveData data;
617 wxEventLoop loop;
618
619 SetFocus();
620
621 #ifndef __WXGTK__
622 if ( flags & wxINTERACTIVE_WAIT_FOR_INPUT )
623 {
624 wxCursor sizingCursor(wxCURSOR_SIZING);
625 wxBeginBusyCursor(&sizingCursor);
626 data.m_sizingCursor = TRUE;
627 }
628 else
629 #endif
630 data.m_sizingCursor = FALSE;
631
632 data.m_window = this;
633 data.m_evtLoop = &loop;
634 data.m_flags = flags;
635 data.m_rect = data.m_rectOrig = GetRect();
636 data.m_pos = wxGetMousePosition();
637 data.m_minSize = wxSize(GetMinWidth(), GetMinHeight());
638 data.m_maxSize = wxSize(GetMaxWidth(), GetMaxHeight());
639
640 wxEvtHandler *handler = new wxInteractiveMoveHandler(data);
641 this->PushEventHandler(handler);
642
643 CaptureMouse();
644 loop.Run();
645 ReleaseMouse();
646
647 this->RemoveEventHandler(handler);
648 delete handler;
649
650 if ( data.m_sizingCursor )
651 wxEndBusyCursor();
652 }
653
654 // ----------------------------------------------------------------------------
655 // actions
656 // ----------------------------------------------------------------------------
657
658 void wxTopLevelWindow::ClickTitleBarButton(long button)
659 {
660 switch ( button )
661 {
662 case wxTOPLEVEL_BUTTON_CLOSE:
663 Close();
664 break;
665
666 case wxTOPLEVEL_BUTTON_ICONIZE:
667 Iconize();
668 break;
669
670 case wxTOPLEVEL_BUTTON_MAXIMIZE:
671 Maximize();
672 break;
673
674 case wxTOPLEVEL_BUTTON_RESTORE:
675 Restore();
676 break;
677
678 case wxTOPLEVEL_BUTTON_HELP:
679 #if wxUSE_HELP
680 {
681 wxContextHelp contextHelp(this);
682 }
683 #endif
684 break;
685
686 default:
687 wxFAIL_MSG(wxT("incorrect button specification"));
688 }
689 }
690
691 bool wxTopLevelWindow::PerformAction(const wxControlAction& action,
692 long numArg,
693 const wxString& strArg)
694 {
695 bool isActive = numArg != 0;
696
697 if ( action == wxACTION_TOPLEVEL_ACTIVATE )
698 {
699 if ( m_isActive != isActive )
700 {
701 m_isActive = isActive;
702 RefreshTitleBar();
703 }
704 return TRUE;
705 }
706
707 else if ( action == wxACTION_TOPLEVEL_BUTTON_PRESS )
708 {
709 m_pressedButton = numArg;
710 RefreshTitleBar();
711 return TRUE;
712 }
713
714 else if ( action == wxACTION_TOPLEVEL_BUTTON_RELEASE )
715 {
716 m_pressedButton = 0;
717 RefreshTitleBar();
718 return TRUE;
719 }
720
721 else if ( action == wxACTION_TOPLEVEL_BUTTON_CLICK )
722 {
723 m_pressedButton = 0;
724 RefreshTitleBar();
725 ClickTitleBarButton(numArg);
726 return TRUE;
727 }
728
729 else if ( action == wxACTION_TOPLEVEL_MOVE )
730 {
731 InteractiveMove(wxINTERACTIVE_MOVE);
732 return TRUE;
733 }
734
735 else if ( action == wxACTION_TOPLEVEL_RESIZE )
736 {
737 int flags = wxINTERACTIVE_RESIZE;
738 if ( numArg & wxHT_TOPLEVEL_BORDER_N )
739 flags |= wxINTERACTIVE_RESIZE_N;
740 if ( numArg & wxHT_TOPLEVEL_BORDER_S )
741 flags |= wxINTERACTIVE_RESIZE_S;
742 if ( numArg & wxHT_TOPLEVEL_BORDER_W )
743 flags |= wxINTERACTIVE_RESIZE_W;
744 if ( numArg & wxHT_TOPLEVEL_BORDER_E )
745 flags |= wxINTERACTIVE_RESIZE_E;
746 InteractiveMove(flags);
747 return TRUE;
748 }
749
750 else
751 return FALSE;
752 }
753
754 void wxTopLevelWindow::OnSystemMenu(wxCommandEvent& event)
755 {
756 bool ret = TRUE;
757
758 switch (event.GetId())
759 {
760 case wxID_CLOSE_FRAME:
761 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
762 wxTOPLEVEL_BUTTON_CLOSE);
763 break;
764 case wxID_MOVE_FRAME:
765 InteractiveMove(wxINTERACTIVE_MOVE | wxINTERACTIVE_WAIT_FOR_INPUT);
766 break;
767 case wxID_RESIZE_FRAME:
768 InteractiveMove(wxINTERACTIVE_RESIZE | wxINTERACTIVE_WAIT_FOR_INPUT);
769 break;
770 case wxID_MAXIMIZE_FRAME:
771 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
772 wxTOPLEVEL_BUTTON_MAXIMIZE);
773 break;
774 case wxID_ICONIZE_FRAME:
775 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
776 wxTOPLEVEL_BUTTON_ICONIZE);
777 break;
778 case wxID_RESTORE_FRAME:
779 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
780 wxTOPLEVEL_BUTTON_RESTORE);
781 break;
782
783 default:
784 ret = FALSE;
785 }
786
787 if ( !ret )
788 event.Skip();
789 }
790
791
792 // ============================================================================
793 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
794 // ============================================================================
795
796 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler *inphand)
797 : wxStdInputHandler(inphand)
798 {
799 m_winCapture = NULL;
800 m_winHitTest = 0;
801 m_winPressed = 0;
802 m_borderCursorOn = FALSE;
803 }
804
805 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer *consumer,
806 const wxMouseEvent& event)
807 {
808 // the button has 2 states: pressed and normal with the following
809 // transitions between them:
810 //
811 // normal -> left down -> capture mouse and go to pressed state
812 // pressed -> left up inside -> generate click -> go to normal
813 // outside ------------------>
814 //
815 // the other mouse buttons are ignored
816 if ( event.Button(1) )
817 {
818 if ( event.ButtonDown(1) )
819 {
820 wxTopLevelWindow *w = wxStaticCast(consumer->GetInputWindow(), wxTopLevelWindow);
821 long hit = w->HitTest(event.GetPosition());
822
823 if ( hit & wxHT_TOPLEVEL_ANY_BUTTON )
824 {
825 m_winCapture = w;
826 m_winCapture->CaptureMouse();
827 m_winHitTest = hit;
828 m_winPressed = hit;
829 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
830 return TRUE;
831 }
832 else if ( (hit & wxHT_TOPLEVEL_TITLEBAR) && !w->IsMaximized() )
833 {
834 consumer->PerformAction(wxACTION_TOPLEVEL_MOVE);
835 return TRUE;
836 }
837 else if ( (consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER)
838 && (hit & wxHT_TOPLEVEL_ANY_BORDER) )
839 {
840 consumer->PerformAction(wxACTION_TOPLEVEL_RESIZE, hit);
841 return TRUE;
842 }
843 }
844
845 else // up
846 {
847 if ( m_winCapture )
848 {
849 m_winCapture->ReleaseMouse();
850 m_winCapture = NULL;
851
852 if ( m_winHitTest == m_winPressed )
853 {
854 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK, m_winPressed);
855 return TRUE;
856 }
857 }
858 //else: the mouse was released outside the window, this doesn't
859 // count as a click
860 }
861 }
862
863 return wxStdInputHandler::HandleMouse(consumer, event);
864 }
865
866 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer *consumer,
867 const wxMouseEvent& event)
868 {
869 if ( event.GetEventObject() == m_winCapture )
870 {
871 long hit = m_winCapture->HitTest(event.GetPosition());
872
873 if ( hit != m_winHitTest )
874 {
875 if ( hit != m_winPressed )
876 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE, m_winPressed);
877 else
878 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
879
880 m_winHitTest = hit;
881 return TRUE;
882 }
883 }
884 else if ( consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER )
885 {
886 wxTopLevelWindow *win = wxStaticCast(consumer->GetInputWindow(),
887 wxTopLevelWindow);
888 long hit = win->HitTest(event.GetPosition());
889
890 if ( hit != m_winHitTest )
891 {
892 m_winHitTest = hit;
893
894 if ( m_borderCursorOn )
895 {
896 m_borderCursorOn = FALSE;
897 win->SetCursor(m_origCursor);
898 }
899
900 if ( hit & wxHT_TOPLEVEL_ANY_BORDER )
901 {
902 wxCursor cur;
903
904 m_borderCursorOn = wxGetResizingCursor(hit, cur);
905 if ( m_borderCursorOn )
906 {
907 m_origCursor = win->GetCursor();
908 win->SetCursor(cur);
909 }
910 }
911 }
912 }
913
914 return wxStdInputHandler::HandleMouseMove(consumer, event);
915 }
916
917 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer *consumer,
918 bool activated)
919 {
920 if ( m_borderCursorOn )
921 {
922 consumer->GetInputWindow()->SetCursor(m_origCursor);
923 m_borderCursorOn = FALSE;
924 }
925 consumer->PerformAction(wxACTION_TOPLEVEL_ACTIVATE, activated);
926 return FALSE;
927 }