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"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(wxTopLevelWindow
, wxTopLevelWindowNative
)
47 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow
)
48 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint
)
51 WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow
)
53 // ============================================================================
55 // ============================================================================
57 int wxTopLevelWindow::ms_drawDecorations
= -1;
59 void wxTopLevelWindow::Init()
66 bool wxTopLevelWindow::Create(wxWindow
*parent
,
68 const wxString
& title
,
74 // init them to avoid compiler warnings
78 // FIXME -- wxUniv should provide a way to force non-native decorations!
79 if ( ms_drawDecorations
== -1 )
80 ms_drawDecorations
= !wxSystemSettings::HasFrameDecorations();
82 if ( ms_drawDecorations
)
84 CreateInputHandler(wxINP_HANDLER_TOPLEVEL
);
87 exstyleOrig
= GetExtraStyle();
88 style
&= ~(wxCAPTION
| wxMINIMIZE_BOX
| wxMAXIMIZE_BOX
|
89 wxSYSTEM_MENU
| wxRESIZE_BORDER
| wxFRAME_TOOL_WINDOW
|
91 style
= wxSIMPLE_BORDER
;
92 SetExtraStyle(exstyleOrig
&
93 ~(wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
));
96 if ( !wxTopLevelWindowNative::Create(parent
, id
, title
, pos
,
100 if ( ms_drawDecorations
)
102 m_windowStyle
= styleOrig
;
103 m_exStyle
= exstyleOrig
;
109 bool wxTopLevelWindow::ShowFullScreen(bool show
, long style
)
111 if ( show
== IsFullScreen() ) return FALSE
;
113 if ( ms_drawDecorations
)
117 m_fsSavedStyle
= m_windowStyle
;
118 if ( style
& wxFULLSCREEN_NOBORDER
)
119 m_windowStyle
|= wxSIMPLE_BORDER
;
120 if ( style
& wxFULLSCREEN_NOCAPTION
)
121 m_windowStyle
&= ~wxCAPTION
;
125 m_windowStyle
= m_fsSavedStyle
;
129 return wxTopLevelWindowNative::ShowFullScreen(show
, style
);
132 long wxTopLevelWindow::GetDecorationsStyle() const
136 if ( m_windowStyle
& wxCAPTION
)
138 style
|= wxTOPLEVEL_TITLEBAR
| wxTOPLEVEL_BUTTON_CLOSE
;
139 if ( m_windowStyle
& wxMINIMIZE_BOX
)
140 style
|= wxTOPLEVEL_BUTTON_ICONIZE
;
141 if ( m_windowStyle
& wxMAXIMIZE_BOX
)
144 style
|= wxTOPLEVEL_BUTTON_RESTORE
;
146 style
|= wxTOPLEVEL_BUTTON_MAXIMIZE
;
149 if ( m_exStyle
& (wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
))
150 style
|= wxTOPLEVEL_BUTTON_HELP
;
153 if ( (m_windowStyle
& (wxSIMPLE_BORDER
| wxNO_BORDER
)) == 0 )
154 style
|= wxTOPLEVEL_BORDER
;
155 if ( m_windowStyle
& (wxRESIZE_BORDER
| wxTHICK_FRAME
) )
156 style
|= wxTOPLEVEL_RESIZEABLE
;
159 style
|= wxTOPLEVEL_MAXIMIZED
;
160 if ( GetIcon().Ok() )
161 style
|= wxTOPLEVEL_ICON
;
163 style
|= wxTOPLEVEL_ACTIVE
;
168 void wxTopLevelWindow::RefreshTitleBar()
170 wxNcPaintEvent
event(GetId());
171 event
.SetEventObject(this);
172 GetEventHandler()->ProcessEvent(event
);
175 // ----------------------------------------------------------------------------
176 // client area handling
177 // ----------------------------------------------------------------------------
179 wxPoint
wxTopLevelWindow::GetClientAreaOrigin() const
181 if ( ms_drawDecorations
)
184 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
185 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
187 rect
= m_renderer
->GetFrameClientArea(rect
,
188 GetDecorationsStyle());
189 return rect
.GetPosition();
193 return wxTopLevelWindowNative::GetClientAreaOrigin();
197 void wxTopLevelWindow::DoGetClientSize(int *width
, int *height
) const
199 if ( ms_drawDecorations
)
202 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
203 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
205 rect
= m_renderer
->GetFrameClientArea(rect
,
206 GetDecorationsStyle());
210 *height
= rect
.height
;
213 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
216 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
218 if ( ms_drawDecorations
)
220 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
221 GetDecorationsStyle());
222 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
225 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
228 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
230 if ( !ms_drawDecorations
|| !m_renderer
)
234 // get the window rect
236 wxSize size
= GetSize();
240 rect
.height
= size
.y
;
243 m_renderer
->DrawFrameTitleBar(dc
, rect
,
244 GetTitle(), m_titlebarIcon
,
245 GetDecorationsStyle(),
251 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
254 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
255 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
257 return m_renderer
->HitTestFrame(rect
, pt
, GetDecorationsStyle());
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
266 wxTopLevelWindowNative::SetIcon(icon
);
268 if ( ms_drawDecorations
&& m_renderer
)
270 wxSize size
= m_renderer
->GetFrameIconSize();
272 if ( !icon
.Ok() || size
.x
== -1 )
273 m_titlebarIcon
= icon
;
277 bmp1
.CopyFromIcon(icon
);
279 m_titlebarIcon
= wxNullIcon
;
280 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
281 m_titlebarIcon
= icon
;
284 wxImage img
= bmp1
.ConvertToImage();
285 img
.Rescale(size
.x
, size
.y
);
286 m_titlebarIcon
.CopyFromBitmap(wxBitmap(img
));
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 void wxTopLevelWindow::ClickTitleBarButton(long button
)
300 case wxTOPLEVEL_BUTTON_CLOSE
:
304 case wxTOPLEVEL_BUTTON_ICONIZE
:
308 case wxTOPLEVEL_BUTTON_MAXIMIZE
:
312 case wxTOPLEVEL_BUTTON_RESTORE
:
316 case wxTOPLEVEL_BUTTON_HELP
:
319 wxContextHelp
contextHelp(this);
325 wxFAIL_MSG(wxT("incorrect button specification"));
329 bool wxTopLevelWindow::PerformAction(const wxControlAction
& action
,
331 const wxString
& strArg
)
333 bool isActive
= numArg
!= 0;
335 if ( action
== wxACTION_TOPLEVEL_ACTIVATE
)
337 if ( m_isActive
!= isActive
)
339 m_isActive
= isActive
;
345 else if ( action
== wxACTION_TOPLEVEL_BUTTON_PRESS
)
347 m_pressedButton
= numArg
;
352 else if ( action
== wxACTION_TOPLEVEL_BUTTON_RELEASE
)
359 else if ( action
== wxACTION_TOPLEVEL_BUTTON_CLICK
)
363 ClickTitleBarButton(numArg
);
367 else if ( action
== wxACTION_TOPLEVEL_MOVE
)
369 InteractiveMove(wxINTERACTIVE_MOVE
);
373 else if ( action
== wxACTION_TOPLEVEL_RESIZE
)
375 int flags
= wxINTERACTIVE_RESIZE
;
376 if ( numArg
& wxHT_TOPLEVEL_BORDER_N
)
377 flags
|= wxINTERACTIVE_RESIZE_N
;
378 if ( numArg
& wxHT_TOPLEVEL_BORDER_S
)
379 flags
|= wxINTERACTIVE_RESIZE_S
;
380 if ( numArg
& wxHT_TOPLEVEL_BORDER_W
)
381 flags
|= wxINTERACTIVE_RESIZE_W
;
382 if ( numArg
& wxHT_TOPLEVEL_BORDER_E
)
383 flags
|= wxINTERACTIVE_RESIZE_E
;
384 InteractiveMove(flags
);
393 // ============================================================================
394 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
395 // ============================================================================
397 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler
*inphand
)
398 : wxStdInputHandler(inphand
)
403 m_borderCursorOn
= FALSE
;
406 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer
*consumer
,
407 const wxMouseEvent
& event
)
409 // the button has 2 states: pressed and normal with the following
410 // transitions between them:
412 // normal -> left down -> capture mouse and go to pressed state
413 // pressed -> left up inside -> generate click -> go to normal
414 // outside ------------------>
416 // the other mouse buttons are ignored
417 if ( event
.Button(1) )
419 if ( event
.ButtonDown(1) )
421 wxTopLevelWindow
*w
= wxStaticCast(consumer
->GetInputWindow(), wxTopLevelWindow
);
422 long hit
= w
->HitTest(event
.GetPosition());
424 if ( hit
& wxHT_TOPLEVEL_ANY_BUTTON
)
427 m_winCapture
->CaptureMouse();
430 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
433 else if ( hit
& wxHT_TOPLEVEL_TITLEBAR
)
435 consumer
->PerformAction(wxACTION_TOPLEVEL_MOVE
);
438 else if ( (consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
439 && (hit
& wxHT_TOPLEVEL_ANY_BORDER
) )
441 consumer
->PerformAction(wxACTION_TOPLEVEL_RESIZE
, hit
);
450 m_winCapture
->ReleaseMouse();
453 if ( m_winHitTest
== m_winPressed
)
455 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK
, m_winPressed
);
459 //else: the mouse was released outside the window, this doesn't
464 return wxStdInputHandler::HandleMouse(consumer
, event
);
467 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
468 const wxMouseEvent
& event
)
470 if ( event
.GetEventObject() == m_winCapture
)
472 long hit
= m_winCapture
->HitTest(event
.GetPosition());
474 if ( hit
!= m_winHitTest
)
476 if ( hit
!= m_winPressed
)
477 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE
, m_winPressed
);
479 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
485 else if ( consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
487 wxTopLevelWindow
*win
= wxStaticCast(consumer
->GetInputWindow(),
489 long hit
= win
->HitTest(event
.GetPosition());
491 if ( hit
!= m_winHitTest
)
495 if ( m_borderCursorOn
)
497 m_borderCursorOn
= FALSE
;
498 win
->SetCursor(m_origCursor
);
501 if ( hit
& wxHT_TOPLEVEL_ANY_BORDER
)
503 m_borderCursorOn
= TRUE
;
508 case wxHT_TOPLEVEL_BORDER_N
:
509 case wxHT_TOPLEVEL_BORDER_S
:
510 cur
= wxCursor(wxCURSOR_SIZENS
);
512 case wxHT_TOPLEVEL_BORDER_W
:
513 case wxHT_TOPLEVEL_BORDER_E
:
514 cur
= wxCursor(wxCURSOR_SIZEWE
);
516 case wxHT_TOPLEVEL_BORDER_NE
:
517 case wxHT_TOPLEVEL_BORDER_SW
:
518 cur
= wxCursor(wxCURSOR_SIZENESW
);
520 case wxHT_TOPLEVEL_BORDER_NW
:
521 case wxHT_TOPLEVEL_BORDER_SE
:
522 cur
= wxCursor(wxCURSOR_SIZENWSE
);
525 m_borderCursorOn
= FALSE
;
528 if ( m_borderCursorOn
)
530 m_origCursor
= win
->GetCursor();
537 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
540 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer
*consumer
,
543 if ( m_borderCursorOn
)
545 consumer
->GetInputWindow()->SetCursor(m_origCursor
);
546 m_borderCursorOn
= FALSE
;
548 consumer
->PerformAction(wxACTION_TOPLEVEL_ACTIVATE
, activated
);