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 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
215 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
217 rect
= m_renderer
->GetFrameClientArea(rect
,
218 GetDecorationsStyle());
222 *height
= rect
.height
;
225 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
228 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
230 if ( ms_drawDecorations
)
232 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
233 GetDecorationsStyle());
234 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
237 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
240 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
242 if ( !ms_drawDecorations
|| !m_renderer
)
246 // get the window rect
248 wxSize size
= GetSize();
252 rect
.height
= size
.y
;
255 m_renderer
->DrawFrameTitleBar(dc
, rect
,
256 GetTitle(), m_titlebarIcon
,
257 GetDecorationsStyle(),
263 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
266 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
267 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
269 return m_renderer
->HitTestFrame(rect
, pt
+GetClientAreaOrigin(), GetDecorationsStyle());
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
278 wxTopLevelWindowNative::SetIcon(icon
);
280 if ( ms_drawDecorations
&& m_renderer
)
282 wxSize size
= m_renderer
->GetFrameIconSize();
284 if ( !icon
.Ok() || size
.x
== -1 )
285 m_titlebarIcon
= icon
;
289 bmp1
.CopyFromIcon(icon
);
291 m_titlebarIcon
= wxNullIcon
;
292 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
293 m_titlebarIcon
= icon
;
296 wxImage img
= bmp1
.ConvertToImage();
297 img
.Rescale(size
.x
, size
.y
);
298 m_titlebarIcon
.CopyFromBitmap(wxBitmap(img
));
304 // ----------------------------------------------------------------------------
305 // interactive manipulation
306 // ----------------------------------------------------------------------------
308 #define wxINTERACTIVE_RESIZE_DIR \
309 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E | \
310 wxINTERACTIVE_RESIZE_S | wxINTERACTIVE_RESIZE_N)
312 struct wxInteractiveMoveData
314 wxTopLevelWindowBase
*m_window
;
315 wxEventLoop
*m_evtLoop
;
320 wxSize m_minSize
, m_maxSize
;
323 class wxInteractiveMoveHandler
: public wxEvtHandler
326 wxInteractiveMoveHandler(wxInteractiveMoveData
& data
) : m_data(data
) {}
329 DECLARE_EVENT_TABLE()
330 void OnMouseMove(wxMouseEvent
& event
);
331 void OnMouseDown(wxMouseEvent
& event
);
332 void OnMouseUp(wxMouseEvent
& event
);
333 void OnKeyDown(wxKeyEvent
& event
);
335 wxInteractiveMoveData
& m_data
;
338 BEGIN_EVENT_TABLE(wxInteractiveMoveHandler
, wxEvtHandler
)
339 EVT_MOTION(wxInteractiveMoveHandler::OnMouseMove
)
340 EVT_LEFT_DOWN(wxInteractiveMoveHandler::OnMouseDown
)
341 EVT_LEFT_UP(wxInteractiveMoveHandler::OnMouseUp
)
342 EVT_KEY_DOWN(wxInteractiveMoveHandler::OnKeyDown
)
346 static inline LINKAGEMODE
347 void wxApplyResize(wxInteractiveMoveData
& data
, const wxPoint
& diff
)
349 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
351 data
.m_rect
.x
+= diff
.x
;
352 data
.m_rect
.width
-= diff
.x
;
354 else if ( data
.m_flags
& wxINTERACTIVE_RESIZE_E
)
356 data
.m_rect
.width
+= diff
.x
;
358 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
360 data
.m_rect
.y
+= diff
.y
;
361 data
.m_rect
.height
-= diff
.y
;
363 else if ( data
.m_flags
& wxINTERACTIVE_RESIZE_S
)
365 data
.m_rect
.height
+= diff
.y
;
368 if ( data
.m_minSize
.x
!= -1 && data
.m_rect
.width
< data
.m_minSize
.x
)
370 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
371 data
.m_rect
.x
-= data
.m_minSize
.x
- data
.m_rect
.width
;
372 data
.m_rect
.width
= data
.m_minSize
.x
;
374 if ( data
.m_maxSize
.x
!= -1 && data
.m_rect
.width
> data
.m_maxSize
.x
)
376 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_W
)
377 data
.m_rect
.x
-= data
.m_minSize
.x
- data
.m_rect
.width
;
378 data
.m_rect
.width
= data
.m_maxSize
.x
;
380 if ( data
.m_minSize
.y
!= -1 && data
.m_rect
.height
< data
.m_minSize
.y
)
382 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
383 data
.m_rect
.y
-= data
.m_minSize
.y
- data
.m_rect
.height
;
384 data
.m_rect
.height
= data
.m_minSize
.y
;
386 if ( data
.m_maxSize
.y
!= -1 && data
.m_rect
.height
> data
.m_maxSize
.y
)
388 if ( data
.m_flags
& wxINTERACTIVE_RESIZE_N
)
389 data
.m_rect
.y
-= data
.m_minSize
.y
- data
.m_rect
.height
;
390 data
.m_rect
.height
= data
.m_maxSize
.y
;
394 void wxInteractiveMoveHandler::OnMouseMove(wxMouseEvent
& event
)
396 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
399 else if ( m_data
.m_flags
& wxINTERACTIVE_MOVE
)
401 wxPoint diff
= wxGetMousePosition() - m_data
.m_pos
;
402 m_data
.m_rect
= m_data
.m_rectOrig
;
403 m_data
.m_rect
.Offset(diff
);
404 m_data
.m_window
->Move(m_data
.m_rect
.GetPosition());
407 else if ( m_data
.m_flags
& wxINTERACTIVE_RESIZE
)
409 wxPoint diff
= wxGetMousePosition() - m_data
.m_pos
;
410 m_data
.m_rect
= m_data
.m_rectOrig
;
411 wxApplyResize(m_data
, diff
);
412 m_data
.m_window
->SetSize(m_data
.m_rect
);
416 void wxInteractiveMoveHandler::OnMouseDown(wxMouseEvent
& event
)
418 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
420 m_data
.m_flags
&= ~wxINTERACTIVE_WAIT_FOR_INPUT
;
421 m_data
.m_pos
= wxGetMousePosition();
425 void wxInteractiveMoveHandler::OnKeyDown(wxKeyEvent
& event
)
427 if ( m_data
.m_flags
& wxINTERACTIVE_WAIT_FOR_INPUT
)
429 m_data
.m_flags
&= ~wxINTERACTIVE_WAIT_FOR_INPUT
;
430 m_data
.m_pos
= wxGetMousePosition();
435 switch ( event
.GetKeyCode() )
437 case WXK_UP
: diff
= wxPoint(0, -16); break;
438 case WXK_DOWN
: diff
= wxPoint(0, 16); break;
439 case WXK_LEFT
: diff
= wxPoint(-16, 0); break;
440 case WXK_RIGHT
: diff
= wxPoint(16, 0); break;
442 m_data
.m_window
->SetSize(m_data
.m_rectOrig
);
443 m_data
.m_evtLoop
->Exit();
446 m_data
.m_evtLoop
->Exit();
452 if ( m_data
.m_flags
& wxINTERACTIVE_MOVE
)
454 m_data
.m_rect
.Offset(diff
);
455 m_data
.m_window
->Move(m_data
.m_rect
.GetPosition());
457 else /* wxINTERACTIVE_RESIZE */
459 if ( !(m_data
.m_flags
& wxINTERACTIVE_RESIZE_DIR
) )
462 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_N
;
463 else if ( diff
.y
> 0 )
464 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_S
;
466 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_W
;
467 else if ( diff
.x
> 0 )
468 m_data
.m_flags
|= wxINTERACTIVE_RESIZE_E
;
471 wxApplyResize(m_data
, diff
);
472 m_data
.m_window
->SetSize(m_data
.m_rect
);
477 void wxInteractiveMoveHandler::OnMouseUp(wxMouseEvent
& event
)
479 m_data
.m_evtLoop
->Exit();
483 void wxTopLevelWindow::InteractiveMove(int flags
)
485 wxASSERT_MSG( !((flags
& wxINTERACTIVE_MOVE
) && (flags
& wxINTERACTIVE_RESIZE
)),
486 wxT("can't move and resize window at the same time") );
488 wxASSERT_MSG( !(flags
& wxINTERACTIVE_RESIZE
) ||
489 (flags
& wxINTERACTIVE_WAIT_FOR_INPUT
) ||
490 (flags
& wxINTERACTIVE_RESIZE_DIR
),
491 wxT("direction of resizing not specified") );
493 wxInteractiveMoveData data
;
495 wxWindow
*focus
= FindFocus();
497 // FIXME - display resize cursor if waiting for initial input
499 data
.m_window
= this;
500 data
.m_evtLoop
= &loop
;
501 data
.m_flags
= flags
;
502 data
.m_rect
= data
.m_rectOrig
= GetRect();
503 data
.m_pos
= wxGetMousePosition();
504 data
.m_minSize
= wxSize(GetMinWidth(), GetMinHeight());
505 data
.m_maxSize
= wxSize(GetMaxWidth(), GetMaxHeight());
507 this->PushEventHandler(new wxInteractiveMoveHandler(data
));
509 focus
->PushEventHandler(new wxInteractiveMoveHandler(data
));
515 this->PopEventHandler(TRUE
/*delete*/);
517 focus
->PopEventHandler(TRUE
/*delete*/);
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 void wxTopLevelWindow::ClickTitleBarButton(long button
)
528 case wxTOPLEVEL_BUTTON_CLOSE
:
532 case wxTOPLEVEL_BUTTON_ICONIZE
:
536 case wxTOPLEVEL_BUTTON_MAXIMIZE
:
540 case wxTOPLEVEL_BUTTON_RESTORE
:
544 case wxTOPLEVEL_BUTTON_HELP
:
547 wxContextHelp
contextHelp(this);
553 wxFAIL_MSG(wxT("incorrect button specification"));
557 bool wxTopLevelWindow::PerformAction(const wxControlAction
& action
,
559 const wxString
& strArg
)
561 bool isActive
= numArg
!= 0;
563 if ( action
== wxACTION_TOPLEVEL_ACTIVATE
)
565 if ( m_isActive
!= isActive
)
567 m_isActive
= isActive
;
573 else if ( action
== wxACTION_TOPLEVEL_BUTTON_PRESS
)
575 m_pressedButton
= numArg
;
580 else if ( action
== wxACTION_TOPLEVEL_BUTTON_RELEASE
)
587 else if ( action
== wxACTION_TOPLEVEL_BUTTON_CLICK
)
591 ClickTitleBarButton(numArg
);
595 else if ( action
== wxACTION_TOPLEVEL_MOVE
)
597 InteractiveMove(wxINTERACTIVE_MOVE
);
601 else if ( action
== wxACTION_TOPLEVEL_RESIZE
)
603 int flags
= wxINTERACTIVE_RESIZE
;
604 if ( numArg
& wxHT_TOPLEVEL_BORDER_N
)
605 flags
|= wxINTERACTIVE_RESIZE_N
;
606 if ( numArg
& wxHT_TOPLEVEL_BORDER_S
)
607 flags
|= wxINTERACTIVE_RESIZE_S
;
608 if ( numArg
& wxHT_TOPLEVEL_BORDER_W
)
609 flags
|= wxINTERACTIVE_RESIZE_W
;
610 if ( numArg
& wxHT_TOPLEVEL_BORDER_E
)
611 flags
|= wxINTERACTIVE_RESIZE_E
;
612 InteractiveMove(flags
);
621 // ============================================================================
622 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
623 // ============================================================================
625 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler
*inphand
)
626 : wxStdInputHandler(inphand
)
631 m_borderCursorOn
= FALSE
;
634 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer
*consumer
,
635 const wxMouseEvent
& event
)
637 // the button has 2 states: pressed and normal with the following
638 // transitions between them:
640 // normal -> left down -> capture mouse and go to pressed state
641 // pressed -> left up inside -> generate click -> go to normal
642 // outside ------------------>
644 // the other mouse buttons are ignored
645 if ( event
.Button(1) )
647 if ( event
.ButtonDown(1) )
649 wxTopLevelWindow
*w
= wxStaticCast(consumer
->GetInputWindow(), wxTopLevelWindow
);
650 long hit
= w
->HitTest(event
.GetPosition());
652 if ( hit
& wxHT_TOPLEVEL_ANY_BUTTON
)
655 m_winCapture
->CaptureMouse();
658 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
661 else if ( hit
& wxHT_TOPLEVEL_TITLEBAR
)
663 consumer
->PerformAction(wxACTION_TOPLEVEL_MOVE
);
666 else if ( (consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
667 && (hit
& wxHT_TOPLEVEL_ANY_BORDER
) )
669 consumer
->PerformAction(wxACTION_TOPLEVEL_RESIZE
, hit
);
678 m_winCapture
->ReleaseMouse();
681 if ( m_winHitTest
== m_winPressed
)
683 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK
, m_winPressed
);
687 //else: the mouse was released outside the window, this doesn't
692 return wxStdInputHandler::HandleMouse(consumer
, event
);
695 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
696 const wxMouseEvent
& event
)
698 if ( event
.GetEventObject() == m_winCapture
)
700 long hit
= m_winCapture
->HitTest(event
.GetPosition());
702 if ( hit
!= m_winHitTest
)
704 if ( hit
!= m_winPressed
)
705 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE
, m_winPressed
);
707 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
713 else if ( consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
715 wxTopLevelWindow
*win
= wxStaticCast(consumer
->GetInputWindow(),
717 long hit
= win
->HitTest(event
.GetPosition());
719 if ( hit
!= m_winHitTest
)
723 if ( m_borderCursorOn
)
725 m_borderCursorOn
= FALSE
;
726 win
->SetCursor(m_origCursor
);
729 if ( hit
& wxHT_TOPLEVEL_ANY_BORDER
)
731 m_borderCursorOn
= TRUE
;
736 case wxHT_TOPLEVEL_BORDER_N
:
737 case wxHT_TOPLEVEL_BORDER_S
:
738 cur
= wxCursor(wxCURSOR_SIZENS
);
740 case wxHT_TOPLEVEL_BORDER_W
:
741 case wxHT_TOPLEVEL_BORDER_E
:
742 cur
= wxCursor(wxCURSOR_SIZEWE
);
744 case wxHT_TOPLEVEL_BORDER_NE
:
745 case wxHT_TOPLEVEL_BORDER_SW
:
746 cur
= wxCursor(wxCURSOR_SIZENESW
);
748 case wxHT_TOPLEVEL_BORDER_NW
:
749 case wxHT_TOPLEVEL_BORDER_SE
:
750 cur
= wxCursor(wxCURSOR_SIZENWSE
);
753 m_borderCursorOn
= FALSE
;
756 if ( m_borderCursorOn
)
758 m_origCursor
= win
->GetCursor();
765 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
768 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer
*consumer
,
771 if ( m_borderCursorOn
)
773 consumer
->GetInputWindow()->SetCursor(m_origCursor
);
774 m_borderCursorOn
= FALSE
;
776 consumer
->PerformAction(wxACTION_TOPLEVEL_ACTIVATE
, activated
);