]> git.saurik.com Git - wxWidgets.git/blob - src/univ/topluniv.cpp
fixed maximization handling
[wxWidgets.git] / src / univ / topluniv.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: topluniv.cpp
3 // Author: Vaclav Slavik
4 // Id: $Id$
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "univtoplevel.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #include "wx/defs.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #endif
34
35 #include "wx/toplevel.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/bitmap.h"
38 #include "wx/image.h"
39 #include "wx/cshelp.h"
40
41
42 // ----------------------------------------------------------------------------
43 // event tables
44 // ----------------------------------------------------------------------------
45
46 BEGIN_EVENT_TABLE(wxTopLevelWindow, wxTopLevelWindowNative)
47 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow)
48 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint)
49 END_EVENT_TABLE()
50
51 WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow)
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 int wxTopLevelWindow::ms_drawDecorations = -1;
58
59 void wxTopLevelWindow::Init()
60 {
61 m_isActive = FALSE;
62 m_windowStyle = 0;
63 m_pressedButton = 0;
64 }
65
66 bool wxTopLevelWindow::Create(wxWindow *parent,
67 wxWindowID id,
68 const wxString& title,
69 const wxPoint& pos,
70 const wxSize& size,
71 long style,
72 const wxString &name)
73 {
74 // init them to avoid compiler warnings
75 long styleOrig = 0,
76 exstyleOrig = 0;
77
78 if ( ms_drawDecorations == -1 )
79 ms_drawDecorations = !wxSystemSettings::HasFrameDecorations();
80 // FIXME -- wxUniv should provide a way to force non-native decorations!
81
82 if ( ms_drawDecorations )
83 {
84 CreateInputHandler(wxINP_HANDLER_TOPLEVEL);
85
86 styleOrig = style;
87 exstyleOrig = GetExtraStyle();
88 style &= ~(wxCAPTION | wxMINIMIZE_BOX | wxMAXIMIZE_BOX |
89 wxSYSTEM_MENU | wxRESIZE_BORDER | wxFRAME_TOOL_WINDOW |
90 wxTHICK_FRAME);
91 style = wxSIMPLE_BORDER;
92 SetExtraStyle(exstyleOrig &
93 ~(wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP));
94 }
95
96 if ( !wxTopLevelWindowNative::Create(parent, id, title, pos,
97 size, style, name) )
98 return FALSE;
99
100 // FIXME: to be removed as soon as wxTLW/wxFrame/wxDialog creation code in
101 // wxMSW is rationalized
102 #ifdef __WXMSW__
103 extern const wxChar *wxFrameClassName;
104 if ( !MSWCreate(id, NULL, wxFrameClassName, this, title,
105 pos.x, pos.y, size.x, size.y, style) )
106 return FALSE;
107 #endif // __WXMSW__
108
109 if ( ms_drawDecorations )
110 {
111 m_windowStyle = styleOrig;
112 m_exStyle = exstyleOrig;
113 }
114
115 return TRUE;
116 }
117
118 bool wxTopLevelWindow::ShowFullScreen(bool show, long style)
119 {
120 if ( show == IsFullScreen() ) return FALSE;
121
122 if ( ms_drawDecorations )
123 {
124 if ( show )
125 {
126 m_fsSavedStyle = m_windowStyle;
127 if ( style & wxFULLSCREEN_NOBORDER )
128 m_windowStyle |= wxSIMPLE_BORDER;
129 if ( style & wxFULLSCREEN_NOCAPTION )
130 m_windowStyle &= ~wxCAPTION;
131 }
132 else
133 {
134 m_windowStyle = m_fsSavedStyle;
135 }
136 }
137
138 return wxTopLevelWindowNative::ShowFullScreen(show, style);
139 }
140
141 long wxTopLevelWindow::GetDecorationsStyle() const
142 {
143 long style = 0;
144
145 if ( m_windowStyle & wxCAPTION )
146 {
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 {
152 if ( IsMaximized() )
153 style |= wxTOPLEVEL_BUTTON_RESTORE;
154 else
155 style |= wxTOPLEVEL_BUTTON_MAXIMIZE;
156 }
157 #if wxUSE_HELP
158 if ( m_exStyle & (wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP))
159 style |= wxTOPLEVEL_BUTTON_HELP;
160 #endif
161 }
162 if ( (m_windowStyle & (wxSIMPLE_BORDER | wxNO_BORDER)) == 0 )
163 style |= wxTOPLEVEL_BORDER;
164 if ( m_windowStyle & (wxRESIZE_BORDER | wxTHICK_FRAME) )
165 style |= wxTOPLEVEL_RESIZEABLE;
166
167 if ( IsMaximized() )
168 style |= wxTOPLEVEL_MAXIMIZED;
169 if ( GetIcon().Ok() )
170 style |= wxTOPLEVEL_ICON;
171 if ( m_isActive )
172 style |= wxTOPLEVEL_ACTIVE;
173
174 return style;
175 }
176
177 void wxTopLevelWindow::RefreshTitleBar()
178 {
179 wxNcPaintEvent event(GetId());
180 event.SetEventObject(this);
181 GetEventHandler()->ProcessEvent(event);
182 }
183
184 // ----------------------------------------------------------------------------
185 // client area handling
186 // ----------------------------------------------------------------------------
187
188 wxPoint wxTopLevelWindow::GetClientAreaOrigin() const
189 {
190 if ( ms_drawDecorations )
191 {
192 int w, h;
193 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
194 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
195 wxSize(w, h));
196 rect = m_renderer->GetFrameClientArea(rect,
197 GetDecorationsStyle());
198 return rect.GetPosition();
199 }
200 else
201 {
202 return wxTopLevelWindowNative::GetClientAreaOrigin();
203 }
204 }
205
206 void wxTopLevelWindow::DoGetClientSize(int *width, int *height) const
207 {
208 if ( ms_drawDecorations )
209 {
210 int w, h;
211 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
212 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
213 wxSize(w, h));
214 rect = m_renderer->GetFrameClientArea(rect,
215 GetDecorationsStyle());
216 if ( width )
217 *width = rect.width;
218 if ( height )
219 *height = rect.height;
220 }
221 else
222 wxTopLevelWindowNative::DoGetClientSize(width, height);
223 }
224
225 void wxTopLevelWindow::DoSetClientSize(int width, int height)
226 {
227 if ( ms_drawDecorations )
228 {
229 wxSize size = m_renderer->GetFrameTotalSize(wxSize(width, height),
230 GetDecorationsStyle());
231 wxTopLevelWindowNative::DoSetClientSize(size.x, size.y);
232 }
233 else
234 wxTopLevelWindowNative::DoSetClientSize(width, height);
235 }
236
237 void wxTopLevelWindow::OnNcPaint(wxPaintEvent& event)
238 {
239 if ( !ms_drawDecorations || !m_renderer )
240 event.Skip();
241 else
242 {
243 // get the window rect
244 wxRect rect;
245 wxSize size = GetSize();
246 rect.x =
247 rect.y = 0;
248 rect.width = size.x;
249 rect.height = size.y;
250
251 wxWindowDC dc(this);
252 m_renderer->DrawFrameTitleBar(dc, rect,
253 GetTitle(), m_titlebarIcon,
254 GetDecorationsStyle(),
255 m_pressedButton,
256 wxCONTROL_PRESSED);
257 }
258 }
259
260 long wxTopLevelWindow::HitTest(const wxPoint& pt) const
261 {
262 int w, h;
263 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
264 wxRect rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w, h));
265
266 return m_renderer->HitTestFrame(rect, pt, GetDecorationsStyle());
267 }
268
269 // ----------------------------------------------------------------------------
270 // icons
271 // ----------------------------------------------------------------------------
272
273 void wxTopLevelWindow::SetIcon(const wxIcon& icon)
274 {
275 wxTopLevelWindowNative::SetIcon(icon);
276
277 if ( ms_drawDecorations && m_renderer )
278 {
279 wxSize size = m_renderer->GetFrameIconSize();
280
281 if ( !icon.Ok() || size.x == -1 )
282 m_titlebarIcon = icon;
283 else
284 {
285 wxBitmap bmp1;
286 bmp1.CopyFromIcon(icon);
287 if ( !bmp1.Ok() )
288 m_titlebarIcon = wxNullIcon;
289 else if ( bmp1.GetWidth() == size.x && bmp1.GetHeight() == size.y )
290 m_titlebarIcon = icon;
291 else
292 {
293 wxImage img = bmp1.ConvertToImage();
294 img.Rescale(size.x, size.y);
295 m_titlebarIcon.CopyFromBitmap(wxBitmap(img));
296 }
297 }
298 }
299 }
300
301 // ----------------------------------------------------------------------------
302 // actions
303 // ----------------------------------------------------------------------------
304
305 void wxTopLevelWindow::ClickTitleBarButton(long button)
306 {
307 switch ( button )
308 {
309 case wxTOPLEVEL_BUTTON_CLOSE:
310 Close();
311 break;
312
313 case wxTOPLEVEL_BUTTON_ICONIZE:
314 Iconize();
315 break;
316
317 case wxTOPLEVEL_BUTTON_MAXIMIZE:
318 Maximize();
319 break;
320
321 case wxTOPLEVEL_BUTTON_RESTORE:
322 Restore();
323 break;
324
325 case wxTOPLEVEL_BUTTON_HELP:
326 #if wxUSE_HELP
327 {
328 wxContextHelp contextHelp(this);
329 }
330 #endif
331 break;
332
333 default:
334 wxFAIL_MSG(wxT("incorrect button specification"));
335 }
336 }
337
338 bool wxTopLevelWindow::PerformAction(const wxControlAction& action,
339 long numArg,
340 const wxString& strArg)
341 {
342 bool isActive = numArg != 0;
343
344 if ( action == wxACTION_TOPLEVEL_ACTIVATE )
345 {
346 if ( m_isActive != isActive )
347 {
348 m_isActive = isActive;
349 RefreshTitleBar();
350 }
351 return TRUE;
352 }
353
354 else if ( action == wxACTION_TOPLEVEL_BUTTON_PRESS )
355 {
356 m_pressedButton = numArg;
357 RefreshTitleBar();
358 return TRUE;
359 }
360
361 else if ( action == wxACTION_TOPLEVEL_BUTTON_RELEASE )
362 {
363 m_pressedButton = 0;
364 RefreshTitleBar();
365 return TRUE;
366 }
367
368 else if ( action == wxACTION_TOPLEVEL_BUTTON_CLICK )
369 {
370 m_pressedButton = 0;
371 RefreshTitleBar();
372 ClickTitleBarButton(numArg);
373 return TRUE;
374 }
375
376 else if ( action == wxACTION_TOPLEVEL_MOVE )
377 {
378 InteractiveMove(wxINTERACTIVE_MOVE);
379 return TRUE;
380 }
381
382 else if ( action == wxACTION_TOPLEVEL_RESIZE )
383 {
384 int flags = wxINTERACTIVE_RESIZE;
385 if ( numArg & wxHT_TOPLEVEL_BORDER_N )
386 flags |= wxINTERACTIVE_RESIZE_N;
387 if ( numArg & wxHT_TOPLEVEL_BORDER_S )
388 flags |= wxINTERACTIVE_RESIZE_S;
389 if ( numArg & wxHT_TOPLEVEL_BORDER_W )
390 flags |= wxINTERACTIVE_RESIZE_W;
391 if ( numArg & wxHT_TOPLEVEL_BORDER_E )
392 flags |= wxINTERACTIVE_RESIZE_E;
393 InteractiveMove(flags);
394 return TRUE;
395 }
396
397 else
398 return FALSE;
399 }
400
401
402 // ============================================================================
403 // wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
404 // ============================================================================
405
406 wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler *inphand)
407 : wxStdInputHandler(inphand)
408 {
409 m_winCapture = NULL;
410 m_winHitTest = 0;
411 m_winPressed = 0;
412 m_borderCursorOn = FALSE;
413 }
414
415 bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer *consumer,
416 const wxMouseEvent& event)
417 {
418 // the button has 2 states: pressed and normal with the following
419 // transitions between them:
420 //
421 // normal -> left down -> capture mouse and go to pressed state
422 // pressed -> left up inside -> generate click -> go to normal
423 // outside ------------------>
424 //
425 // the other mouse buttons are ignored
426 if ( event.Button(1) )
427 {
428 if ( event.ButtonDown(1) )
429 {
430 wxTopLevelWindow *w = wxStaticCast(consumer->GetInputWindow(), wxTopLevelWindow);
431 long hit = w->HitTest(event.GetPosition());
432
433 if ( hit & wxHT_TOPLEVEL_ANY_BUTTON )
434 {
435 m_winCapture = w;
436 m_winCapture->CaptureMouse();
437 m_winHitTest = hit;
438 m_winPressed = hit;
439 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
440 return TRUE;
441 }
442 else if ( hit & wxHT_TOPLEVEL_TITLEBAR )
443 {
444 consumer->PerformAction(wxACTION_TOPLEVEL_MOVE);
445 return TRUE;
446 }
447 else if ( (consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER)
448 && (hit & wxHT_TOPLEVEL_ANY_BORDER) )
449 {
450 consumer->PerformAction(wxACTION_TOPLEVEL_RESIZE, hit);
451 return TRUE;
452 }
453 }
454
455 else // up
456 {
457 if ( m_winCapture )
458 {
459 m_winCapture->ReleaseMouse();
460 m_winCapture = NULL;
461
462 if ( m_winHitTest == m_winPressed )
463 {
464 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK, m_winPressed);
465 return TRUE;
466 }
467 }
468 //else: the mouse was released outside the window, this doesn't
469 // count as a click
470 }
471 }
472
473 return wxStdInputHandler::HandleMouse(consumer, event);
474 }
475
476 bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer *consumer,
477 const wxMouseEvent& event)
478 {
479 if ( event.GetEventObject() == m_winCapture )
480 {
481 long hit = m_winCapture->HitTest(event.GetPosition());
482
483 if ( hit != m_winHitTest )
484 {
485 if ( hit != m_winPressed )
486 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE, m_winPressed);
487 else
488 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
489
490 m_winHitTest = hit;
491 return TRUE;
492 }
493 }
494 else if ( consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER )
495 {
496 wxTopLevelWindow *win = wxStaticCast(consumer->GetInputWindow(),
497 wxTopLevelWindow);
498 long hit = win->HitTest(event.GetPosition());
499
500 if ( hit != m_winHitTest )
501 {
502 m_winHitTest = hit;
503
504 if ( m_borderCursorOn )
505 {
506 m_borderCursorOn = FALSE;
507 win->SetCursor(m_origCursor);
508 }
509
510 if ( hit & wxHT_TOPLEVEL_ANY_BORDER )
511 {
512 m_borderCursorOn = TRUE;
513 wxCursor cur;
514
515 switch (hit)
516 {
517 case wxHT_TOPLEVEL_BORDER_N:
518 case wxHT_TOPLEVEL_BORDER_S:
519 cur = wxCursor(wxCURSOR_SIZENS);
520 break;
521 case wxHT_TOPLEVEL_BORDER_W:
522 case wxHT_TOPLEVEL_BORDER_E:
523 cur = wxCursor(wxCURSOR_SIZEWE);
524 break;
525 case wxHT_TOPLEVEL_BORDER_NE:
526 case wxHT_TOPLEVEL_BORDER_SW:
527 cur = wxCursor(wxCURSOR_SIZENESW);
528 break;
529 case wxHT_TOPLEVEL_BORDER_NW:
530 case wxHT_TOPLEVEL_BORDER_SE:
531 cur = wxCursor(wxCURSOR_SIZENWSE);
532 break;
533 default:
534 m_borderCursorOn = FALSE;
535 break;
536 }
537 if ( m_borderCursorOn )
538 {
539 m_origCursor = win->GetCursor();
540 win->SetCursor(cur);
541 }
542 }
543 }
544 }
545
546 return wxStdInputHandler::HandleMouseMove(consumer, event);
547 }
548
549 bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer *consumer,
550 bool activated)
551 {
552 if ( m_borderCursorOn )
553 {
554 consumer->GetInputWindow()->SetCursor(m_origCursor);
555 m_borderCursorOn = FALSE;
556 }
557 consumer->PerformAction(wxACTION_TOPLEVEL_ACTIVATE, activated);
558 return FALSE;
559 }