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 // FIXME -- wxUniv should provide a way to force non-native decorations!
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 // FIXME: to be removed as soon as wxTLW/wxFrame/wxDialog creation code in
101 // wxMSW is rationalized
103 extern const wxChar
*wxFrameClassName
;
104 if ( !MSWCreate(id
, NULL
, wxFrameClassName
, this, title
,
105 pos
.x
, pos
.y
, size
.x
, size
.y
, style
) )
109 if ( ms_drawDecorations
)
111 m_windowStyle
= styleOrig
;
112 m_exStyle
= exstyleOrig
;
118 bool wxTopLevelWindow::ShowFullScreen(bool show
, long style
)
120 if ( show
== IsFullScreen() ) return FALSE
;
122 if ( ms_drawDecorations
)
126 m_fsSavedStyle
= m_windowStyle
;
127 if ( style
& wxFULLSCREEN_NOBORDER
)
128 m_windowStyle
|= wxSIMPLE_BORDER
;
129 if ( style
& wxFULLSCREEN_NOCAPTION
)
130 m_windowStyle
&= ~wxCAPTION
;
134 m_windowStyle
= m_fsSavedStyle
;
138 return wxTopLevelWindowNative::ShowFullScreen(show
, style
);
141 long wxTopLevelWindow::GetDecorationsStyle() const
145 if ( m_windowStyle
& wxCAPTION
)
147 style
|= wxTOPLEVEL_TITLEBAR
| wxTOPLEVEL_BUTTON_CLOSE
;
148 if ( m_windowStyle
& wxMINIMIZE_BOX
)
149 style
|= wxTOPLEVEL_BUTTON_ICONIZE
;
150 if ( m_windowStyle
& wxMAXIMIZE_BOX
)
151 style
|= wxTOPLEVEL_BUTTON_MAXIMIZE
;
153 if ( m_exStyle
& (wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
))
154 style
|= wxTOPLEVEL_BUTTON_HELP
;
157 if ( (m_windowStyle
& (wxSIMPLE_BORDER
| wxNO_BORDER
)) == 0 )
158 style
|= wxTOPLEVEL_BORDER
;
159 if ( m_windowStyle
& (wxRESIZE_BORDER
| wxTHICK_FRAME
) )
160 style
|= wxTOPLEVEL_RESIZEABLE
;
163 style
|= wxTOPLEVEL_MAXIMIZED
;
164 if ( GetIcon().Ok() )
165 style
|= wxTOPLEVEL_ICON
;
167 style
|= wxTOPLEVEL_ACTIVE
;
172 void wxTopLevelWindow::RefreshTitleBar()
174 wxNcPaintEvent
event(GetId());
175 event
.SetEventObject(this);
176 GetEventHandler()->ProcessEvent(event
);
179 // ----------------------------------------------------------------------------
180 // client area handling
181 // ----------------------------------------------------------------------------
183 wxPoint
wxTopLevelWindow::GetClientAreaOrigin() const
185 if ( ms_drawDecorations
)
188 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
189 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
191 rect
= m_renderer
->GetFrameClientArea(rect
,
192 GetDecorationsStyle());
193 return rect
.GetPosition();
197 return wxTopLevelWindowNative::GetClientAreaOrigin();
201 void wxTopLevelWindow::DoGetClientSize(int *width
, int *height
) const
203 if ( ms_drawDecorations
)
206 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
207 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
209 rect
= m_renderer
->GetFrameClientArea(rect
,
210 GetDecorationsStyle());
214 *height
= rect
.height
;
217 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
220 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
222 if ( ms_drawDecorations
)
224 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
225 GetDecorationsStyle());
226 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
229 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
232 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
234 if ( !ms_drawDecorations
|| !m_renderer
)
238 // get the window rect
240 wxSize size
= GetSize();
244 rect
.height
= size
.y
;
247 m_renderer
->DrawFrameTitleBar(dc
, rect
,
248 GetTitle(), m_titlebarIcon
,
249 GetDecorationsStyle(),
255 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
258 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
259 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
261 return m_renderer
->HitTestFrame(rect
, pt
, GetDecorationsStyle());
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
270 wxTopLevelWindowNative::SetIcon(icon
);
271 if ( !m_renderer
) return;
273 wxSize size
= m_renderer
->GetFrameIconSize();
275 if ( !icon
.Ok() || size
.x
== -1 )
276 m_titlebarIcon
= icon
;
280 bmp1
.CopyFromIcon(icon
);
282 m_titlebarIcon
= wxNullIcon
;
283 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
284 m_titlebarIcon
= icon
;
287 wxImage img
= bmp1
.ConvertToImage();
288 img
.Rescale(size
.x
, size
.y
);
289 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
);