1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 #pragma implementation "univtoplevel.h"
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
35 #include "wx/toplevel.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/bitmap.h"
39 #include "wx/cshelp.h"
40 #include "wx/evtloop.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 BEGIN_EVENT_TABLE(wxTopLevelWindow
, wxTopLevelWindowNative
)
48 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow
)
49 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint
)
52 WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow
)
54 // ============================================================================
56 // ============================================================================
58 int wxTopLevelWindow::ms_drawDecorations
= -1;
59 int wxTopLevelWindow::ms_canIconize
= -1;
61 void wxTopLevelWindow::Init()
68 bool wxTopLevelWindow::Create(wxWindow
*parent
,
70 const wxString
& title
,
76 // init them to avoid compiler warnings
80 if ( ms_drawDecorations
== -1 )
83 !wxSystemSettings::HasFeature(wxSYS_CAN_DRAW_FRAME_DECORATIONS
)
84 || wxGetEnv(wxT("WXDECOR"), NULL
);
85 // FIXME -- wxUniv should provide a way to force non-native decorations!
86 // $WXDECOR is just a hack in absence of better wxUniv solution
89 if ( ms_canIconize
== -1 )
91 ms_canIconize
= wxSystemSettings::HasFeature(wxSYS_CAN_ICONIZE_FRAME
);
94 if ( ms_drawDecorations
)
96 CreateInputHandler(wxINP_HANDLER_TOPLEVEL
);
99 exstyleOrig
= GetExtraStyle();
100 style
&= ~(wxCAPTION
| wxMINIMIZE_BOX
| wxMAXIMIZE_BOX
|
101 wxSYSTEM_MENU
| wxRESIZE_BORDER
| wxFRAME_TOOL_WINDOW
|
103 style
= wxSIMPLE_BORDER
;
104 SetExtraStyle(exstyleOrig
&
105 ~(wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
));
108 if ( !wxTopLevelWindowNative::Create(parent
, id
, title
, pos
,
112 if ( ms_drawDecorations
)
114 m_windowStyle
= styleOrig
;
115 m_exStyle
= exstyleOrig
;
121 bool wxTopLevelWindow::ShowFullScreen(bool show
, long style
)
123 if ( show
== IsFullScreen() ) return FALSE
;
125 if ( ms_drawDecorations
)
129 m_fsSavedStyle
= m_windowStyle
;
130 if ( style
& wxFULLSCREEN_NOBORDER
)
131 m_windowStyle
|= wxSIMPLE_BORDER
;
132 if ( style
& wxFULLSCREEN_NOCAPTION
)
133 m_windowStyle
&= ~wxCAPTION
;
137 m_windowStyle
= m_fsSavedStyle
;
141 return wxTopLevelWindowNative::ShowFullScreen(show
, style
);
144 long wxTopLevelWindow::GetDecorationsStyle() const
148 if ( m_windowStyle
& wxCAPTION
)
150 style
|= wxTOPLEVEL_TITLEBAR
| wxTOPLEVEL_BUTTON_CLOSE
;
151 if ( (m_windowStyle
& wxMINIMIZE_BOX
) && ms_canIconize
)
152 style
|= wxTOPLEVEL_BUTTON_ICONIZE
;
153 if ( m_windowStyle
& wxMAXIMIZE_BOX
)
156 style
|= wxTOPLEVEL_BUTTON_RESTORE
;
158 style
|= wxTOPLEVEL_BUTTON_MAXIMIZE
;
161 if ( m_exStyle
& (wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
))
162 style
|= wxTOPLEVEL_BUTTON_HELP
;
165 if ( (m_windowStyle
& (wxSIMPLE_BORDER
| wxNO_BORDER
)) == 0 )
166 style
|= wxTOPLEVEL_BORDER
;
167 if ( m_windowStyle
& (wxRESIZE_BORDER
| wxTHICK_FRAME
) )
168 style
|= wxTOPLEVEL_RESIZEABLE
;
171 style
|= wxTOPLEVEL_MAXIMIZED
;
172 if ( GetIcon().Ok() )
173 style
|= wxTOPLEVEL_ICON
;
175 style
|= wxTOPLEVEL_ACTIVE
;
180 void wxTopLevelWindow::RefreshTitleBar()
182 wxNcPaintEvent
event(GetId());
183 event
.SetEventObject(this);
184 GetEventHandler()->ProcessEvent(event
);
187 // ----------------------------------------------------------------------------
188 // client area handling
189 // ----------------------------------------------------------------------------
191 wxPoint
wxTopLevelWindow::GetClientAreaOrigin() const
193 if ( ms_drawDecorations
)
196 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
197 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
199 rect
= m_renderer
->GetFrameClientArea(rect
,
200 GetDecorationsStyle());
201 return rect
.GetPosition();
205 return wxTopLevelWindowNative::GetClientAreaOrigin();
209 void wxTopLevelWindow::DoGetClientSize(int *width
, int *height
) const
211 if ( ms_drawDecorations
)
214 // VS: we can't use real client area size in 'rect', because
215 // wxTLWNative::DoGetClientSize calls GetClientAreaOrigin
216 // under wxMSW which in turn calls DoGetClientSize...
217 // inifinite recursion
219 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
223 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
225 rect
= m_renderer
->GetFrameClientArea(rect
,
226 GetDecorationsStyle());
230 *height
= rect
.height
;
233 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
236 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
238 if ( ms_drawDecorations
)
240 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
241 GetDecorationsStyle());
242 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
245 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
248 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
250 if ( !ms_drawDecorations
|| !m_renderer
)
254 // get the window rect
256 wxSize size
= GetSize();
260 rect
.height
= size
.y
;
263 m_renderer
->DrawFrameTitleBar(dc
, rect
,
264 GetTitle(), m_titlebarIcon
,
265 GetDecorationsStyle(),
271 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
274 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
275 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
277 return m_renderer
->HitTestFrame(rect
, pt
+GetClientAreaOrigin(), GetDecorationsStyle());
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
286 wxTopLevelWindowNative::SetIcon(icon
);
288 if ( ms_drawDecorations
&& m_renderer
)
290 wxSize size
= m_renderer
->GetFrameIconSize();
292 if ( !icon
.Ok() || size
.x
== -1 )
293 m_titlebarIcon
= icon
;
297 bmp1
.CopyFromIcon(icon
);
299 m_titlebarIcon
= wxNullIcon
;
300 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
301 m_titlebarIcon
= icon
;
304 wxImage img
= bmp1
.ConvertToImage();
305 img
.Rescale(size
.x
, size
.y
);
306 m_titlebarIcon
.CopyFromBitmap(wxBitmap(img
));
312 // ----------------------------------------------------------------------------
313 // interactive manipulation
314 // ----------------------------------------------------------------------------
316 #define wxINTERACTIVE_RESIZE_DIR \
317 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E | \
318 wxINTERACTIVE_RESIZE_S | wxINTERACTIVE_RESIZE_N)
320 struct wxInteractiveMoveData
322 wxTopLevelWindowBase
*m_window
;
323 wxEventLoop
*m_evtLoop
;
328 wxSize m_minSize
, m_maxSize
;
331 class wxInteractiveMoveHandler
: public wxEvtHandler
334 wxInteractiveMoveHandler(wxInteractiveMoveData
& data
) : m_data(data
) {}
337 DECLARE_EVENT_TABLE()
338 void OnMouseMove(wxMouseEvent
& event
);
339 void OnMouseDown(wxMouseEvent
& event
);
340 void OnMouseUp(wxMouseEvent
& event
);
341 void OnKeyDown(wxKeyEvent
& event
);
343 wxInteractiveMoveData
& m_data
;
346 BEGIN_EVENT_TABLE(wxInteractiveMoveHandler
, wxEvtHandler
)
347 EVT_MOTION(wxInteractiveMoveHandler::OnMouseMove
)
348 EVT_LEFT_DOWN(wxInteractiveMoveHandler::OnMouseDown
)
349 EVT_LEFT_UP(wxInteractiveMoveHandler::OnMouseUp
)
350 EVT_KEY_DOWN(wxInteractiveMoveHandler::OnKeyDown
)
354 static inline LINKAGEMODE
355 void wxApplyResize(wxInteractiveMoveData
& data
, const wxPoint
& diff
)
357 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
359 data
.m_rect
.x
+= diff
.x
;
360 data
.m_rect
.width
-= diff
.x
;
362 else if ( data
.m_flags
& wxINTERACTIVE_RESIZE_E
)
364 data
.m_rect
.width
+= diff
.x
;
366 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
368 data
.m_rect
.y
+= diff
.y
;
369 data
.m_rect
.height
-= diff
.y
;
371 else if ( data
.m_flags
& wxINTERACTIVE_RESIZE_S
)
373 data
.m_rect
.height
+= diff
.y
;
376 if ( data
.m_minSize
.x
!= -1 && data
.m_rect
.width
< data
.m_minSize
.x
)
378 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
379 data
.m_rect
.x
-= data
.m_minSize
.x
- data
.m_rect
.width
;
380 data
.m_rect
.width
= data
.m_minSize
.x
;
382 if ( data
.m_maxSize
.x
!= -1 && data
.m_rect
.width
> data
.m_maxSize
.x
)
384 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
385 data
.m_rect
.x
-= data
.m_minSize
.x
- data
.m_rect
.width
;
386 data
.m_rect
.width
= data
.m_maxSize
.x
;
388 if ( data
.m_minSize
.y
!= -1 && data
.m_rect
.height
< data
.m_minSize
.y
)
390 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
391 data
.m_rect
.y
-= data
.m_minSize
.y
- data
.m_rect
.height
;
392 data
.m_rect
.height
= data
.m_minSize
.y
;
394 if ( data
.m_maxSize
.y
!= -1 && data
.m_rect
.height
> data
.m_maxSize
.y
)
396 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
397 data
.m_rect
.y
-= data
.m_minSize
.y
- data
.m_rect
.height
;
398 data
.m_rect
.height
= data
.m_maxSize
.y
;
402 void wxInteractiveMoveHandler::OnMouseMove(wxMouseEvent
& event
)
404 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
407 else if ( m_data
.m_flags
& wxINTERACTIVE_MOVE
)
409 wxPoint diff
= wxGetMousePosition() - m_data
.m_pos
;
410 m_data
.m_rect
= m_data
.m_rectOrig
;
411 m_data
.m_rect
.Offset(diff
);
412 m_data
.m_window
->Move(m_data
.m_rect
.GetPosition());
415 else if ( m_data
.m_flags
& wxINTERACTIVE_RESIZE
)
417 wxPoint diff
= wxGetMousePosition() - m_data
.m_pos
;
418 m_data
.m_rect
= m_data
.m_rectOrig
;
419 wxApplyResize(m_data
, diff
);
420 m_data
.m_window
->SetSize(m_data
.m_rect
);
424 void wxInteractiveMoveHandler::OnMouseDown(wxMouseEvent
& event
)
426 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
428 m_data
.m_flags
&= ~wxINTERACTIVE_WAIT_FOR_INPUT
;
429 m_data
.m_pos
= wxGetMousePosition();
433 void wxInteractiveMoveHandler::OnKeyDown(wxKeyEvent
& event
)
435 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
437 m_data
.m_flags
&= ~wxINTERACTIVE_WAIT_FOR_INPUT
;
438 m_data
.m_pos
= wxGetMousePosition();
443 switch ( event
.GetKeyCode() )
445 case WXK_UP
: diff
= wxPoint(0, -16); break;
446 case WXK_DOWN
: diff
= wxPoint(0, 16); break;
447 case WXK_LEFT
: diff
= wxPoint(-16, 0); break;
448 case WXK_RIGHT
: diff
= wxPoint(16, 0); break;
450 m_data
.m_window
->SetSize(m_data
.m_rectOrig
);
451 m_data
.m_evtLoop
->Exit();
454 m_data
.m_evtLoop
->Exit();
460 if ( m_data
.m_flags
& wxINTERACTIVE_MOVE
)
462 m_data
.m_rect
.Offset(diff
);
463 m_data
.m_window
->Move(m_data
.m_rect
.GetPosition());
465 else /* wxINTERACTIVE_RESIZE */
467 if ( !(m_data
.m_flags
& wxINTERACTIVE_RESIZE_DIR
) )
470 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_N
;
471 else if ( diff
.y
> 0 )
472 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_S
;
474 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_W
;
475 else if ( diff
.x
> 0 )
476 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_E
;
479 wxApplyResize(m_data
, diff
);
480 m_data
.m_window
->SetSize(m_data
.m_rect
);
485 void wxInteractiveMoveHandler::OnMouseUp(wxMouseEvent
& event
)
487 m_data
.m_evtLoop
->Exit();
491 void wxTopLevelWindow::InteractiveMove(int flags
)
493 wxASSERT_MSG( !((flags
& wxINTERACTIVE_MOVE
) && (flags
& wxINTERACTIVE_RESIZE
)),
494 wxT("can't move and resize window at the same time") );
496 wxASSERT_MSG( !(flags
& wxINTERACTIVE_RESIZE
) ||
497 (flags
& wxINTERACTIVE_WAIT_FOR_INPUT
) ||
498 (flags
& wxINTERACTIVE_RESIZE_DIR
),
499 wxT("direction of resizing not specified") );
501 wxInteractiveMoveData data
;
503 wxWindow
*focus
= FindFocus();
505 // FIXME - display resize cursor if waiting for initial input
507 data
.m_window
= this;
508 data
.m_evtLoop
= &loop
;
509 data
.m_flags
= flags
;
510 data
.m_rect
= data
.m_rectOrig
= GetRect();
511 data
.m_pos
= wxGetMousePosition();
512 data
.m_minSize
= wxSize(GetMinWidth(), GetMinHeight());
513 data
.m_maxSize
= wxSize(GetMaxWidth(), GetMaxHeight());
515 this->PushEventHandler(new wxInteractiveMoveHandler(data
));
517 focus
->PushEventHandler(new wxInteractiveMoveHandler(data
));
523 this->PopEventHandler(TRUE
/*delete*/);
525 focus
->PopEventHandler(TRUE
/*delete*/);
528 // ----------------------------------------------------------------------------
530 // ----------------------------------------------------------------------------
532 void wxTopLevelWindow::ClickTitleBarButton(long button
)
536 case wxTOPLEVEL_BUTTON_CLOSE
:
540 case wxTOPLEVEL_BUTTON_ICONIZE
:
544 case wxTOPLEVEL_BUTTON_MAXIMIZE
:
548 case wxTOPLEVEL_BUTTON_RESTORE
:
552 case wxTOPLEVEL_BUTTON_HELP
:
555 wxContextHelp
contextHelp(this);
561 wxFAIL_MSG(wxT("incorrect button specification"));
565 bool wxTopLevelWindow::PerformAction(const wxControlAction
& action
,
567 const wxString
& strArg
)
569 bool isActive
= numArg
!= 0;
571 if ( action
== wxACTION_TOPLEVEL_ACTIVATE
)
573 if ( m_isActive
!= isActive
)
575 m_isActive
= isActive
;
581 else if ( action
== wxACTION_TOPLEVEL_BUTTON_PRESS
)
583 m_pressedButton
= numArg
;
588 else if ( action
== wxACTION_TOPLEVEL_BUTTON_RELEASE
)
595 else if ( action
== wxACTION_TOPLEVEL_BUTTON_CLICK
)
599 ClickTitleBarButton(numArg
);
603 else if ( action
== wxACTION_TOPLEVEL_MOVE
)
605 InteractiveMove(wxINTERACTIVE_MOVE
);
609 else if ( action
== wxACTION_TOPLEVEL_RESIZE
)
611 int flags
= wxINTERACTIVE_RESIZE
;
612 if ( numArg
& wxHT_TOPLEVEL_BORDER_N
)
613 flags
|= wxINTERACTIVE_RESIZE_N
;
614 if ( numArg
& wxHT_TOPLEVEL_BORDER_S
)
615 flags
|= wxINTERACTIVE_RESIZE_S
;
616 if ( numArg
& wxHT_TOPLEVEL_BORDER_W
)
617 flags
|= wxINTERACTIVE_RESIZE_W
;
618 if ( numArg
& wxHT_TOPLEVEL_BORDER_E
)
619 flags
|= wxINTERACTIVE_RESIZE_E
;
620 InteractiveMove(flags
);
629 // ============================================================================
630 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
631 // ============================================================================
633 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler
*inphand
)
634 : wxStdInputHandler(inphand
)
639 m_borderCursorOn
= FALSE
;
642 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer
*consumer
,
643 const wxMouseEvent
& event
)
645 // the button has 2 states: pressed and normal with the following
646 // transitions between them:
648 // normal -> left down -> capture mouse and go to pressed state
649 // pressed -> left up inside -> generate click -> go to normal
650 // outside ------------------>
652 // the other mouse buttons are ignored
653 if ( event
.Button(1) )
655 if ( event
.ButtonDown(1) )
657 wxTopLevelWindow
*w
= wxStaticCast(consumer
->GetInputWindow(), wxTopLevelWindow
);
658 long hit
= w
->HitTest(event
.GetPosition());
660 if ( hit
& wxHT_TOPLEVEL_ANY_BUTTON
)
663 m_winCapture
->CaptureMouse();
666 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
669 else if ( hit
& wxHT_TOPLEVEL_TITLEBAR
)
671 consumer
->PerformAction(wxACTION_TOPLEVEL_MOVE
);
674 else if ( (consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
675 && (hit
& wxHT_TOPLEVEL_ANY_BORDER
) )
677 consumer
->PerformAction(wxACTION_TOPLEVEL_RESIZE
, hit
);
686 m_winCapture
->ReleaseMouse();
689 if ( m_winHitTest
== m_winPressed
)
691 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK
, m_winPressed
);
695 //else: the mouse was released outside the window, this doesn't
700 return wxStdInputHandler::HandleMouse(consumer
, event
);
703 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
704 const wxMouseEvent
& event
)
706 if ( event
.GetEventObject() == m_winCapture
)
708 long hit
= m_winCapture
->HitTest(event
.GetPosition());
710 if ( hit
!= m_winHitTest
)
712 if ( hit
!= m_winPressed
)
713 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE
, m_winPressed
);
715 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
721 else if ( consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
723 wxTopLevelWindow
*win
= wxStaticCast(consumer
->GetInputWindow(),
725 long hit
= win
->HitTest(event
.GetPosition());
727 if ( hit
!= m_winHitTest
)
731 if ( m_borderCursorOn
)
733 m_borderCursorOn
= FALSE
;
734 win
->SetCursor(m_origCursor
);
737 if ( hit
& wxHT_TOPLEVEL_ANY_BORDER
)
739 m_borderCursorOn
= TRUE
;
744 case wxHT_TOPLEVEL_BORDER_N
:
745 case wxHT_TOPLEVEL_BORDER_S
:
746 cur
= wxCursor(wxCURSOR_SIZENS
);
748 case wxHT_TOPLEVEL_BORDER_W
:
749 case wxHT_TOPLEVEL_BORDER_E
:
750 cur
= wxCursor(wxCURSOR_SIZEWE
);
752 case wxHT_TOPLEVEL_BORDER_NE
:
753 case wxHT_TOPLEVEL_BORDER_SW
:
754 cur
= wxCursor(wxCURSOR_SIZENESW
);
756 case wxHT_TOPLEVEL_BORDER_NW
:
757 case wxHT_TOPLEVEL_BORDER_SE
:
758 cur
= wxCursor(wxCURSOR_SIZENWSE
);
761 m_borderCursorOn
= FALSE
;
764 if ( m_borderCursorOn
)
766 m_origCursor
= win
->GetCursor();
773 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
776 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer
*consumer
,
779 if ( m_borderCursorOn
)
781 consumer
->GetInputWindow()->SetCursor(m_origCursor
);
782 m_borderCursorOn
= FALSE
;
784 consumer
->PerformAction(wxACTION_TOPLEVEL_ACTIVATE
, activated
);