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 if ( ms_drawDecorations
== -1 )
79 ms_drawDecorations
= !wxSystemSettings::HasFrameDecorations() ||
80 wxGetEnv(wxT("WXDECOR"), NULL
);
81 // FIXME -- wxUniv should provide a way to force non-native decorations!
82 // $WXDECOR is just a hack in absence of better wxUniv solution
84 if ( ms_drawDecorations
)
86 CreateInputHandler(wxINP_HANDLER_TOPLEVEL
);
89 exstyleOrig
= GetExtraStyle();
90 style
&= ~(wxCAPTION
| wxMINIMIZE_BOX
| wxMAXIMIZE_BOX
|
91 wxSYSTEM_MENU
| wxRESIZE_BORDER
| wxFRAME_TOOL_WINDOW
|
93 style
= wxSIMPLE_BORDER
;
94 SetExtraStyle(exstyleOrig
&
95 ~(wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
));
98 if ( !wxTopLevelWindowNative::Create(parent
, id
, title
, pos
,
102 if ( ms_drawDecorations
)
104 m_windowStyle
= styleOrig
;
105 m_exStyle
= exstyleOrig
;
111 bool wxTopLevelWindow::ShowFullScreen(bool show
, long style
)
113 if ( show
== IsFullScreen() ) return FALSE
;
115 if ( ms_drawDecorations
)
119 m_fsSavedStyle
= m_windowStyle
;
120 if ( style
& wxFULLSCREEN_NOBORDER
)
121 m_windowStyle
|= wxSIMPLE_BORDER
;
122 if ( style
& wxFULLSCREEN_NOCAPTION
)
123 m_windowStyle
&= ~wxCAPTION
;
127 m_windowStyle
= m_fsSavedStyle
;
131 return wxTopLevelWindowNative::ShowFullScreen(show
, style
);
134 long wxTopLevelWindow::GetDecorationsStyle() const
138 if ( m_windowStyle
& wxCAPTION
)
140 style
|= wxTOPLEVEL_TITLEBAR
| wxTOPLEVEL_BUTTON_CLOSE
;
141 if ( m_windowStyle
& wxMINIMIZE_BOX
)
142 style
|= wxTOPLEVEL_BUTTON_ICONIZE
;
143 if ( m_windowStyle
& wxMAXIMIZE_BOX
)
146 style
|= wxTOPLEVEL_BUTTON_RESTORE
;
148 style
|= wxTOPLEVEL_BUTTON_MAXIMIZE
;
151 if ( m_exStyle
& (wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
))
152 style
|= wxTOPLEVEL_BUTTON_HELP
;
155 if ( (m_windowStyle
& (wxSIMPLE_BORDER
| wxNO_BORDER
)) == 0 )
156 style
|= wxTOPLEVEL_BORDER
;
157 if ( m_windowStyle
& (wxRESIZE_BORDER
| wxTHICK_FRAME
) )
158 style
|= wxTOPLEVEL_RESIZEABLE
;
161 style
|= wxTOPLEVEL_MAXIMIZED
;
162 if ( GetIcon().Ok() )
163 style
|= wxTOPLEVEL_ICON
;
165 style
|= wxTOPLEVEL_ACTIVE
;
170 void wxTopLevelWindow::RefreshTitleBar()
172 wxNcPaintEvent
event(GetId());
173 event
.SetEventObject(this);
174 GetEventHandler()->ProcessEvent(event
);
177 // ----------------------------------------------------------------------------
178 // client area handling
179 // ----------------------------------------------------------------------------
181 wxPoint
wxTopLevelWindow::GetClientAreaOrigin() const
183 if ( ms_drawDecorations
)
186 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
187 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
189 rect
= m_renderer
->GetFrameClientArea(rect
,
190 GetDecorationsStyle());
191 return rect
.GetPosition();
195 return wxTopLevelWindowNative::GetClientAreaOrigin();
199 void wxTopLevelWindow::DoGetClientSize(int *width
, int *height
) const
201 if ( ms_drawDecorations
)
204 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
205 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
207 rect
= m_renderer
->GetFrameClientArea(rect
,
208 GetDecorationsStyle());
212 *height
= rect
.height
;
215 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
218 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
220 if ( ms_drawDecorations
)
222 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
223 GetDecorationsStyle());
224 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
227 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
230 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
232 if ( !ms_drawDecorations
|| !m_renderer
)
236 // get the window rect
238 wxSize size
= GetSize();
242 rect
.height
= size
.y
;
245 m_renderer
->DrawFrameTitleBar(dc
, rect
,
246 GetTitle(), m_titlebarIcon
,
247 GetDecorationsStyle(),
253 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
256 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
257 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
259 return m_renderer
->HitTestFrame(rect
, pt
, GetDecorationsStyle());
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
268 wxTopLevelWindowNative::SetIcon(icon
);
270 if ( ms_drawDecorations
&& m_renderer
)
272 wxSize size
= m_renderer
->GetFrameIconSize();
274 if ( !icon
.Ok() || size
.x
== -1 )
275 m_titlebarIcon
= icon
;
279 bmp1
.CopyFromIcon(icon
);
281 m_titlebarIcon
= wxNullIcon
;
282 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
283 m_titlebarIcon
= icon
;
286 wxImage img
= bmp1
.ConvertToImage();
287 img
.Rescale(size
.x
, size
.y
);
288 m_titlebarIcon
.CopyFromBitmap(wxBitmap(img
));
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void wxTopLevelWindow::ClickTitleBarButton(long button
)
302 case wxTOPLEVEL_BUTTON_CLOSE
:
306 case wxTOPLEVEL_BUTTON_ICONIZE
:
310 case wxTOPLEVEL_BUTTON_MAXIMIZE
:
314 case wxTOPLEVEL_BUTTON_RESTORE
:
318 case wxTOPLEVEL_BUTTON_HELP
:
321 wxContextHelp
contextHelp(this);
327 wxFAIL_MSG(wxT("incorrect button specification"));
331 bool wxTopLevelWindow::PerformAction(const wxControlAction
& action
,
333 const wxString
& strArg
)
335 bool isActive
= numArg
!= 0;
337 if ( action
== wxACTION_TOPLEVEL_ACTIVATE
)
339 if ( m_isActive
!= isActive
)
341 m_isActive
= isActive
;
347 else if ( action
== wxACTION_TOPLEVEL_BUTTON_PRESS
)
349 m_pressedButton
= numArg
;
354 else if ( action
== wxACTION_TOPLEVEL_BUTTON_RELEASE
)
361 else if ( action
== wxACTION_TOPLEVEL_BUTTON_CLICK
)
365 ClickTitleBarButton(numArg
);
369 else if ( action
== wxACTION_TOPLEVEL_MOVE
)
371 InteractiveMove(wxINTERACTIVE_MOVE
);
375 else if ( action
== wxACTION_TOPLEVEL_RESIZE
)
377 int flags
= wxINTERACTIVE_RESIZE
;
378 if ( numArg
& wxHT_TOPLEVEL_BORDER_N
)
379 flags
|= wxINTERACTIVE_RESIZE_N
;
380 if ( numArg
& wxHT_TOPLEVEL_BORDER_S
)
381 flags
|= wxINTERACTIVE_RESIZE_S
;
382 if ( numArg
& wxHT_TOPLEVEL_BORDER_W
)
383 flags
|= wxINTERACTIVE_RESIZE_W
;
384 if ( numArg
& wxHT_TOPLEVEL_BORDER_E
)
385 flags
|= wxINTERACTIVE_RESIZE_E
;
386 InteractiveMove(flags
);
395 // ============================================================================
396 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
397 // ============================================================================
399 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler
*inphand
)
400 : wxStdInputHandler(inphand
)
405 m_borderCursorOn
= FALSE
;
408 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer
*consumer
,
409 const wxMouseEvent
& event
)
411 // the button has 2 states: pressed and normal with the following
412 // transitions between them:
414 // normal -> left down -> capture mouse and go to pressed state
415 // pressed -> left up inside -> generate click -> go to normal
416 // outside ------------------>
418 // the other mouse buttons are ignored
419 if ( event
.Button(1) )
421 if ( event
.ButtonDown(1) )
423 wxTopLevelWindow
*w
= wxStaticCast(consumer
->GetInputWindow(), wxTopLevelWindow
);
424 long hit
= w
->HitTest(event
.GetPosition());
426 if ( hit
& wxHT_TOPLEVEL_ANY_BUTTON
)
429 m_winCapture
->CaptureMouse();
432 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
435 else if ( hit
& wxHT_TOPLEVEL_TITLEBAR
)
437 consumer
->PerformAction(wxACTION_TOPLEVEL_MOVE
);
440 else if ( (consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
441 && (hit
& wxHT_TOPLEVEL_ANY_BORDER
) )
443 consumer
->PerformAction(wxACTION_TOPLEVEL_RESIZE
, hit
);
452 m_winCapture
->ReleaseMouse();
455 if ( m_winHitTest
== m_winPressed
)
457 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK
, m_winPressed
);
461 //else: the mouse was released outside the window, this doesn't
466 return wxStdInputHandler::HandleMouse(consumer
, event
);
469 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
470 const wxMouseEvent
& event
)
472 if ( event
.GetEventObject() == m_winCapture
)
474 long hit
= m_winCapture
->HitTest(event
.GetPosition());
476 if ( hit
!= m_winHitTest
)
478 if ( hit
!= m_winPressed
)
479 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE
, m_winPressed
);
481 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
487 else if ( consumer
->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER
)
489 wxTopLevelWindow
*win
= wxStaticCast(consumer
->GetInputWindow(),
491 long hit
= win
->HitTest(event
.GetPosition());
493 if ( hit
!= m_winHitTest
)
497 if ( m_borderCursorOn
)
499 m_borderCursorOn
= FALSE
;
500 win
->SetCursor(m_origCursor
);
503 if ( hit
& wxHT_TOPLEVEL_ANY_BORDER
)
505 m_borderCursorOn
= TRUE
;
510 case wxHT_TOPLEVEL_BORDER_N
:
511 case wxHT_TOPLEVEL_BORDER_S
:
512 cur
= wxCursor(wxCURSOR_SIZENS
);
514 case wxHT_TOPLEVEL_BORDER_W
:
515 case wxHT_TOPLEVEL_BORDER_E
:
516 cur
= wxCursor(wxCURSOR_SIZEWE
);
518 case wxHT_TOPLEVEL_BORDER_NE
:
519 case wxHT_TOPLEVEL_BORDER_SW
:
520 cur
= wxCursor(wxCURSOR_SIZENESW
);
522 case wxHT_TOPLEVEL_BORDER_NW
:
523 case wxHT_TOPLEVEL_BORDER_SE
:
524 cur
= wxCursor(wxCURSOR_SIZENWSE
);
527 m_borderCursorOn
= FALSE
;
530 if ( m_borderCursorOn
)
532 m_origCursor
= win
->GetCursor();
539 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
542 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer
*consumer
,
545 if ( m_borderCursorOn
)
547 consumer
->GetInputWindow()->SetCursor(m_origCursor
);
548 m_borderCursorOn
= FALSE
;
550 consumer
->PerformAction(wxACTION_TOPLEVEL_ACTIVATE
, activated
);