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"
29 #include "wx/toplevel.h"
30 #include "wx/univ/renderer.h"
31 #include "wx/dcclient.h"
32 #include "wx/bitmap.h"
34 #include "wx/cshelp.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 BEGIN_EVENT_TABLE(wxTopLevelWindow
, wxTopLevelWindowNative
)
42 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow
)
43 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint
)
46 WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow
)
49 // ============================================================================
51 // ============================================================================
53 int wxTopLevelWindow::ms_drawDecorations
= -1;
55 void wxTopLevelWindow::Init()
62 bool wxTopLevelWindow::Create(wxWindow
*parent
,
64 const wxString
& title
,
66 const wxSize
& sizeOrig
,
70 // init them to avoid compiler warnings
74 if ( ms_drawDecorations
== -1 )
75 ms_drawDecorations
= TRUE
;
76 // FIXME_MGL -- this is temporary; we assume for now that native TLW
77 // can't do decorations, which is not true
79 if ( ms_drawDecorations
)
81 CreateInputHandler(wxINP_HANDLER_TOPLEVEL
);
84 exstyleOrig
= GetExtraStyle();
85 style
&= ~(wxCAPTION
| wxMINIMIZE_BOX
| wxMAXIMIZE_BOX
|
86 wxSYSTEM_MENU
| wxRESIZE_BORDER
| wxFRAME_TOOL_WINDOW
|
88 style
= wxSIMPLE_BORDER
;
89 SetExtraStyle(exstyleOrig
&
90 ~(wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
));
93 if ( !wxTopLevelWindowNative::Create(parent
, id
, title
, pos
,
94 sizeOrig
, style
, name
) )
97 if ( ms_drawDecorations
)
99 m_windowStyle
= styleOrig
;
100 m_exStyle
= exstyleOrig
;
106 bool wxTopLevelWindow::ShowFullScreen(bool show
, long style
)
108 if ( show
== IsFullScreen() ) return FALSE
;
110 if ( ms_drawDecorations
)
114 m_fsSavedStyle
= m_windowStyle
;
115 if ( style
& wxFULLSCREEN_NOBORDER
)
116 m_windowStyle
|= wxSIMPLE_BORDER
;
117 if ( style
& wxFULLSCREEN_NOCAPTION
)
118 m_windowStyle
&= ~wxCAPTION
;
122 m_windowStyle
= m_fsSavedStyle
;
126 return wxTopLevelWindowNative::ShowFullScreen(show
, style
);
129 long wxTopLevelWindow::GetDecorationsStyle() const
133 if ( m_windowStyle
& wxCAPTION
)
135 style
|= wxTOPLEVEL_TITLEBAR
| wxTOPLEVEL_BUTTON_CLOSE
;
136 if ( m_windowStyle
& wxMINIMIZE_BOX
)
137 style
|= wxTOPLEVEL_BUTTON_ICONIZE
;
138 if ( m_windowStyle
& wxMAXIMIZE_BOX
)
139 style
|= wxTOPLEVEL_BUTTON_MAXIMIZE
;
141 if ( m_exStyle
& (wxFRAME_EX_CONTEXTHELP
| wxDIALOG_EX_CONTEXTHELP
))
142 style
|= wxTOPLEVEL_BUTTON_HELP
;
145 if ( (m_windowStyle
& (wxSIMPLE_BORDER
| wxNO_BORDER
)) == 0 )
146 style
|= wxTOPLEVEL_BORDER
;
147 if ( m_windowStyle
& (wxRESIZE_BORDER
| wxTHICK_FRAME
) )
148 style
|= wxTOPLEVEL_RESIZEABLE
;
151 style
|= wxTOPLEVEL_MAXIMIZED
;
152 if ( GetIcon().Ok() )
153 style
|= wxTOPLEVEL_ICON
;
155 style
|= wxTOPLEVEL_ACTIVE
;
160 void wxTopLevelWindow::RefreshTitleBar()
162 wxNcPaintEvent
event(GetId());
163 event
.SetEventObject(this);
164 GetEventHandler()->ProcessEvent(event
);
167 // ----------------------------------------------------------------------------
168 // client area handling
169 // ----------------------------------------------------------------------------
171 wxPoint
wxTopLevelWindow::GetClientAreaOrigin() const
173 if ( ms_drawDecorations
)
176 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
177 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
179 rect
= m_renderer
->GetFrameClientArea(rect
,
180 GetDecorationsStyle());
181 return rect
.GetPosition();
185 return wxTopLevelWindowNative::GetClientAreaOrigin();
189 void wxTopLevelWindow::DoGetClientSize(int *width
, int *height
) const
191 if ( ms_drawDecorations
)
194 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
195 wxRect rect
= wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
197 rect
= m_renderer
->GetFrameClientArea(rect
,
198 GetDecorationsStyle());
202 *height
= rect
.height
;
205 wxTopLevelWindowNative::DoGetClientSize(width
, height
);
208 void wxTopLevelWindow::DoSetClientSize(int width
, int height
)
210 if ( ms_drawDecorations
)
212 wxSize size
= m_renderer
->GetFrameTotalSize(wxSize(width
, height
),
213 GetDecorationsStyle());
214 wxTopLevelWindowNative::DoSetClientSize(size
.x
, size
.y
);
217 wxTopLevelWindowNative::DoSetClientSize(width
, height
);
220 void wxTopLevelWindow::OnNcPaint(wxPaintEvent
& event
)
222 if ( !ms_drawDecorations
|| !m_renderer
)
226 // get the window rect
228 wxSize size
= GetSize();
232 rect
.height
= size
.y
;
235 m_renderer
->DrawFrameTitleBar(dc
, rect
,
236 GetTitle(), m_titlebarIcon
,
237 GetDecorationsStyle(),
243 long wxTopLevelWindow::HitTest(const wxPoint
& pt
) const
246 wxTopLevelWindowNative::DoGetClientSize(&w
, &h
);
247 wxRect
rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w
, h
));
249 return m_renderer
->HitTestFrame(rect
, pt
, GetDecorationsStyle());
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 void wxTopLevelWindow::SetIcon(const wxIcon
& icon
)
258 wxTopLevelWindowNative::SetIcon(icon
);
259 if ( !m_renderer
) return;
261 wxSize size
= m_renderer
->GetFrameIconSize();
263 if ( !icon
.Ok() || size
.x
== -1 )
264 m_titlebarIcon
= icon
;
268 bmp1
.CopyFromIcon(icon
);
270 m_titlebarIcon
= wxNullIcon
;
271 else if ( bmp1
.GetWidth() == size
.x
&& bmp1
.GetHeight() == size
.y
)
272 m_titlebarIcon
= icon
;
275 wxImage img
= bmp1
.ConvertToImage();
276 img
.Rescale(size
.x
, size
.y
);
277 m_titlebarIcon
.CopyFromBitmap(wxBitmap(img
));
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void wxTopLevelWindow::ClickTitleBarButton(long button
)
290 case wxTOPLEVEL_BUTTON_CLOSE
:
294 case wxTOPLEVEL_BUTTON_ICONIZE
:
298 case wxTOPLEVEL_BUTTON_MAXIMIZE
:
302 case wxTOPLEVEL_BUTTON_RESTORE
:
306 case wxTOPLEVEL_BUTTON_HELP
:
309 wxContextHelp
contextHelp(this);
315 wxFAIL_MSG(wxT("incorrect button specification"));
319 bool wxTopLevelWindow::PerformAction(const wxControlAction
& action
,
321 const wxString
& strArg
)
323 if ( action
== wxACTION_TOPLEVEL_ACTIVATE
)
325 if ( m_isActive
!= (bool)numArg
)
328 m_isActive
= (bool)numArg
;
329 wxNcPaintEvent
event(GetId());
330 event
.SetEventObject(this);
331 GetEventHandler()->ProcessEvent(event
);
336 else if ( action
== wxACTION_TOPLEVEL_BUTTON_PRESS
)
338 m_pressedButton
= numArg
;
343 else if ( action
== wxACTION_TOPLEVEL_BUTTON_RELEASE
)
350 else if ( action
== wxACTION_TOPLEVEL_BUTTON_CLICK
)
354 ClickTitleBarButton(numArg
);
363 // ============================================================================
364 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
365 // ============================================================================
367 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler
*inphand
)
368 : wxStdInputHandler(inphand
)
375 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer
*consumer
,
376 const wxMouseEvent
& event
)
378 // the button has 2 states: pressed and normal with the following
379 // transitions between them:
381 // normal -> left down -> capture mouse and go to pressed state
382 // pressed -> left up inside -> generate click -> go to normal
383 // outside ------------------>
385 // the other mouse buttons are ignored
386 if ( event
.Button(1) )
388 if ( event
.ButtonDown(1) )
390 wxTopLevelWindow
*w
= wxStaticCast(consumer
->GetInputWindow(), wxTopLevelWindow
);
391 long hit
= w
->HitTest(event
.GetPosition());
393 if ( hit
& wxHT_TOPLEVEL_ANY_BUTTON
)
396 m_winCapture
->CaptureMouse();
399 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
408 m_winCapture
->ReleaseMouse();
411 if ( m_winHitTest
== m_winPressed
)
413 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK
, m_winPressed
);
417 //else: the mouse was released outside the window, this doesn't
422 return wxStdInputHandler::HandleMouse(consumer
, event
);
425 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
426 const wxMouseEvent
& event
)
428 // we only have to do something when the mouse leaves/enters the pressed
429 // button and don't care about the other ones
430 if ( event
.GetEventObject() == m_winCapture
)
432 long hit
= m_winCapture
->HitTest(event
.GetPosition());
434 if ( hit
!= m_winHitTest
)
436 if ( hit
!= m_winPressed
)
437 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE
, m_winPressed
);
439 consumer
->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS
, m_winPressed
);
446 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
449 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer
*consumer
,
452 consumer
->PerformAction(wxACTION_TOPLEVEL_ACTIVATE
, activated
);