]> git.saurik.com Git - wxWidgets.git/blame - src/univ/topluniv.cpp
make it possible to have empty lines in wxListCtrl
[wxWidgets.git] / src / univ / topluniv.cpp
CommitLineData
0f1bf77d
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: topluniv.cpp
3// Author: Vaclav Slavik
4// Id: $Id$
52750c2e 5// Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
0f1bf77d
VS
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"
f8e3449c
VS
29
30#ifndef WX_PRECOMP
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33#endif
34
0f1bf77d 35#include "wx/toplevel.h"
24a23c35 36#include "wx/univ/renderer.h"
24a23c35
VS
37#include "wx/bitmap.h"
38#include "wx/image.h"
813edf09 39#include "wx/cshelp.h"
621b405f 40#include "wx/evtloop.h"
0f1bf77d
VS
41
42
43// ----------------------------------------------------------------------------
44// event tables
45// ----------------------------------------------------------------------------
46
24a23c35 47BEGIN_EVENT_TABLE(wxTopLevelWindow, wxTopLevelWindowNative)
ea1b0d6c 48 WX_EVENT_TABLE_INPUT_CONSUMER(wxTopLevelWindow)
24a23c35 49 EVT_NC_PAINT(wxTopLevelWindow::OnNcPaint)
71cb2ca1 50 EVT_MENU_RANGE(wxID_CLOSE_FRAME, wxID_RESTORE_FRAME, wxTopLevelWindow::OnSystemMenu)
24a23c35
VS
51END_EVENT_TABLE()
52
ea1b0d6c
VS
53WX_FORWARD_TO_INPUT_CONSUMER(wxTopLevelWindow)
54
0f1bf77d
VS
55// ============================================================================
56// implementation
57// ============================================================================
58
24a23c35 59int wxTopLevelWindow::ms_drawDecorations = -1;
253293c1 60int wxTopLevelWindow::ms_canIconize = -1;
24a23c35 61
0f1bf77d
VS
62void wxTopLevelWindow::Init()
63{
24a23c35 64 m_isActive = FALSE;
ea1b0d6c 65 m_windowStyle = 0;
813edf09 66 m_pressedButton = 0;
0f1bf77d
VS
67}
68
69bool wxTopLevelWindow::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxString& title,
72 const wxPoint& pos,
7ee7c43f 73 const wxSize& size,
0f1bf77d
VS
74 long style,
75 const wxString &name)
76{
2e9f62da
VZ
77 // init them to avoid compiler warnings
78 long styleOrig = 0,
79 exstyleOrig = 0;
24a23c35
VS
80
81 if ( ms_drawDecorations == -1 )
253293c1
VS
82 {
83 ms_drawDecorations =
7516ed26 84 !wxSystemSettings::HasFeature(wxSYS_CAN_DRAW_FRAME_DECORATIONS)
253293c1 85 || wxGetEnv(wxT("WXDECOR"), NULL);
4c785218
VS
86 // FIXME -- wxUniv should provide a way to force non-native decorations!
87 // $WXDECOR is just a hack in absence of better wxUniv solution
253293c1
VS
88 }
89
90 if ( ms_canIconize == -1 )
91 {
7516ed26 92 ms_canIconize = wxSystemSettings::HasFeature(wxSYS_CAN_ICONIZE_FRAME);
253293c1 93 }
24a23c35
VS
94
95 if ( ms_drawDecorations )
96 {
ea1b0d6c
VS
97 CreateInputHandler(wxINP_HANDLER_TOPLEVEL);
98
24a23c35
VS
99 styleOrig = style;
100 exstyleOrig = GetExtraStyle();
7ee7c43f
VZ
101 style &= ~(wxCAPTION | wxMINIMIZE_BOX | wxMAXIMIZE_BOX |
102 wxSYSTEM_MENU | wxRESIZE_BORDER | wxFRAME_TOOL_WINDOW |
813edf09
VS
103 wxTHICK_FRAME);
104 style = wxSIMPLE_BORDER;
7ee7c43f 105 SetExtraStyle(exstyleOrig &
813edf09 106 ~(wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP));
24a23c35
VS
107 }
108
7ee7c43f
VZ
109 if ( !wxTopLevelWindowNative::Create(parent, id, title, pos,
110 size, style, name) )
111 return FALSE;
112
24a23c35
VS
113 if ( ms_drawDecorations )
114 {
115 m_windowStyle = styleOrig;
116 m_exStyle = exstyleOrig;
117 }
7ee7c43f 118
0f1bf77d
VS
119 return TRUE;
120}
121
122bool wxTopLevelWindow::ShowFullScreen(bool show, long style)
123{
124 if ( show == IsFullScreen() ) return FALSE;
7ee7c43f 125
ea1b0d6c
VS
126 if ( ms_drawDecorations )
127 {
128 if ( show )
129 {
130 m_fsSavedStyle = m_windowStyle;
131 if ( style & wxFULLSCREEN_NOBORDER )
132 m_windowStyle |= wxSIMPLE_BORDER;
133 if ( style & wxFULLSCREEN_NOCAPTION )
134 m_windowStyle &= ~wxCAPTION;
135 }
136 else
137 {
138 m_windowStyle = m_fsSavedStyle;
139 }
140 }
2e9f62da 141
ea1b0d6c 142 return wxTopLevelWindowNative::ShowFullScreen(show, style);
0f1bf77d
VS
143}
144
24a23c35
VS
145long wxTopLevelWindow::GetDecorationsStyle() const
146{
147 long style = 0;
148
149 if ( m_windowStyle & wxCAPTION )
150 {
ea1b0d6c 151 style |= wxTOPLEVEL_TITLEBAR | wxTOPLEVEL_BUTTON_CLOSE;
253293c1 152 if ( (m_windowStyle & wxMINIMIZE_BOX) && ms_canIconize )
813edf09 153 style |= wxTOPLEVEL_BUTTON_ICONIZE;
24a23c35 154 if ( m_windowStyle & wxMAXIMIZE_BOX )
15678bec
VS
155 {
156 if ( IsMaximized() )
157 style |= wxTOPLEVEL_BUTTON_RESTORE;
158 else
159 style |= wxTOPLEVEL_BUTTON_MAXIMIZE;
160 }
813edf09 161#if wxUSE_HELP
24a23c35 162 if ( m_exStyle & (wxFRAME_EX_CONTEXTHELP | wxDIALOG_EX_CONTEXTHELP))
ea1b0d6c 163 style |= wxTOPLEVEL_BUTTON_HELP;
813edf09 164#endif
24a23c35
VS
165 }
166 if ( (m_windowStyle & (wxSIMPLE_BORDER | wxNO_BORDER)) == 0 )
167 style |= wxTOPLEVEL_BORDER;
168 if ( m_windowStyle & (wxRESIZE_BORDER | wxTHICK_FRAME) )
169 style |= wxTOPLEVEL_RESIZEABLE;
7ee7c43f 170
24a23c35
VS
171 if ( IsMaximized() )
172 style |= wxTOPLEVEL_MAXIMIZED;
173 if ( GetIcon().Ok() )
174 style |= wxTOPLEVEL_ICON;
ea1b0d6c 175 if ( m_isActive )
24a23c35
VS
176 style |= wxTOPLEVEL_ACTIVE;
177
178 return style;
179}
180
813edf09
VS
181void wxTopLevelWindow::RefreshTitleBar()
182{
183 wxNcPaintEvent event(GetId());
184 event.SetEventObject(this);
185 GetEventHandler()->ProcessEvent(event);
186}
187
24a23c35
VS
188// ----------------------------------------------------------------------------
189// client area handling
190// ----------------------------------------------------------------------------
191
192wxPoint wxTopLevelWindow::GetClientAreaOrigin() const
193{
194 if ( ms_drawDecorations )
195 {
196 int w, h;
197 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
198 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
199 wxSize(w, h));
7ee7c43f 200 rect = m_renderer->GetFrameClientArea(rect,
24a23c35
VS
201 GetDecorationsStyle());
202 return rect.GetPosition();
203 }
204 else
205 {
206 return wxTopLevelWindowNative::GetClientAreaOrigin();
207 }
208}
209
210void wxTopLevelWindow::DoGetClientSize(int *width, int *height) const
211{
212 if ( ms_drawDecorations )
213 {
214 int w, h;
215 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
216 wxRect rect = wxRect(wxTopLevelWindowNative::GetClientAreaOrigin(),
217 wxSize(w, h));
7ee7c43f 218 rect = m_renderer->GetFrameClientArea(rect,
24a23c35
VS
219 GetDecorationsStyle());
220 if ( width )
221 *width = rect.width;
222 if ( height )
223 *height = rect.height;
224 }
225 else
226 wxTopLevelWindowNative::DoGetClientSize(width, height);
227}
228
229void wxTopLevelWindow::DoSetClientSize(int width, int height)
230{
231 if ( ms_drawDecorations )
232 {
7ee7c43f 233 wxSize size = m_renderer->GetFrameTotalSize(wxSize(width, height),
24a23c35
VS
234 GetDecorationsStyle());
235 wxTopLevelWindowNative::DoSetClientSize(size.x, size.y);
236 }
237 else
238 wxTopLevelWindowNative::DoSetClientSize(width, height);
239}
240
241void wxTopLevelWindow::OnNcPaint(wxPaintEvent& event)
242{
243 if ( !ms_drawDecorations || !m_renderer )
244 event.Skip();
245 else
246 {
247 // get the window rect
248 wxRect rect;
249 wxSize size = GetSize();
250 rect.x =
251 rect.y = 0;
252 rect.width = size.x;
253 rect.height = size.y;
254
255 wxWindowDC dc(this);
7ee7c43f 256 m_renderer->DrawFrameTitleBar(dc, rect,
24a23c35 257 GetTitle(), m_titlebarIcon,
813edf09
VS
258 GetDecorationsStyle(),
259 m_pressedButton,
260 wxCONTROL_PRESSED);
24a23c35
VS
261 }
262}
263
813edf09
VS
264long wxTopLevelWindow::HitTest(const wxPoint& pt) const
265{
266 int w, h;
267 wxTopLevelWindowNative::DoGetClientSize(&w, &h);
268 wxRect rect(wxTopLevelWindowNative::GetClientAreaOrigin(), wxSize(w, h));
7ee7c43f 269
badc44fe 270 return m_renderer->HitTestFrame(rect, pt+GetClientAreaOrigin(), GetDecorationsStyle());
813edf09
VS
271}
272
e7dda1ff
VS
273int wxTopLevelWindow::GetMinWidth() const
274{
275 if ( ms_drawDecorations )
276 {
277 return wxMax(wxTopLevelWindowNative::GetMinWidth(),
278 m_renderer->GetFrameMinSize(GetDecorationsStyle()).x);
279 }
280 else
281 return wxTopLevelWindowNative::GetMinWidth();
282}
283
284int wxTopLevelWindow::GetMinHeight() const
285{
286 if ( ms_drawDecorations )
287 {
288 return wxMax(wxTopLevelWindowNative::GetMinHeight(),
289 m_renderer->GetFrameMinSize(GetDecorationsStyle()).y);
290 }
291 else
292 return wxTopLevelWindowNative::GetMinHeight();
293}
294
24a23c35
VS
295// ----------------------------------------------------------------------------
296// icons
297// ----------------------------------------------------------------------------
298
299void wxTopLevelWindow::SetIcon(const wxIcon& icon)
300{
301 wxTopLevelWindowNative::SetIcon(icon);
24a23c35 302
15678bec 303 if ( ms_drawDecorations && m_renderer )
24a23c35 304 {
15678bec
VS
305 wxSize size = m_renderer->GetFrameIconSize();
306
307 if ( !icon.Ok() || size.x == -1 )
24a23c35
VS
308 m_titlebarIcon = icon;
309 else
310 {
15678bec
VS
311 wxBitmap bmp1;
312 bmp1.CopyFromIcon(icon);
313 if ( !bmp1.Ok() )
314 m_titlebarIcon = wxNullIcon;
315 else if ( bmp1.GetWidth() == size.x && bmp1.GetHeight() == size.y )
316 m_titlebarIcon = icon;
317 else
318 {
319 wxImage img = bmp1.ConvertToImage();
320 img.Rescale(size.x, size.y);
321 m_titlebarIcon.CopyFromBitmap(wxBitmap(img));
322 }
24a23c35
VS
323 }
324 }
325}
ea1b0d6c 326
621b405f
VS
327// ----------------------------------------------------------------------------
328// interactive manipulation
329// ----------------------------------------------------------------------------
330
331#define wxINTERACTIVE_RESIZE_DIR \
332 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E | \
333 wxINTERACTIVE_RESIZE_S | wxINTERACTIVE_RESIZE_N)
334
335struct wxInteractiveMoveData
336{
337 wxTopLevelWindowBase *m_window;
338 wxEventLoop *m_evtLoop;
339 int m_flags;
340 wxRect m_rect;
341 wxRect m_rectOrig;
342 wxPoint m_pos;
343 wxSize m_minSize, m_maxSize;
71cb2ca1 344 bool m_sizingCursor;
621b405f
VS
345};
346
347class wxInteractiveMoveHandler : public wxEvtHandler
348{
349public:
350 wxInteractiveMoveHandler(wxInteractiveMoveData& data) : m_data(data) {}
351
352private:
353 DECLARE_EVENT_TABLE()
354 void OnMouseMove(wxMouseEvent& event);
355 void OnMouseDown(wxMouseEvent& event);
356 void OnMouseUp(wxMouseEvent& event);
357 void OnKeyDown(wxKeyEvent& event);
358
359 wxInteractiveMoveData& m_data;
360};
361
362BEGIN_EVENT_TABLE(wxInteractiveMoveHandler, wxEvtHandler)
363 EVT_MOTION(wxInteractiveMoveHandler::OnMouseMove)
364 EVT_LEFT_DOWN(wxInteractiveMoveHandler::OnMouseDown)
365 EVT_LEFT_UP(wxInteractiveMoveHandler::OnMouseUp)
366 EVT_KEY_DOWN(wxInteractiveMoveHandler::OnKeyDown)
367END_EVENT_TABLE()
368
369
370static inline LINKAGEMODE
371void wxApplyResize(wxInteractiveMoveData& data, const wxPoint& diff)
372{
373 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
374 {
375 data.m_rect.x += diff.x;
376 data.m_rect.width -= diff.x;
377 }
378 else if ( data.m_flags & wxINTERACTIVE_RESIZE_E )
379 {
380 data.m_rect.width += diff.x;
381 }
382 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
383 {
384 data.m_rect.y += diff.y;
385 data.m_rect.height -= diff.y;
386 }
387 else if ( data.m_flags & wxINTERACTIVE_RESIZE_S )
388 {
389 data.m_rect.height += diff.y;
390 }
391
392 if ( data.m_minSize.x != -1 && data.m_rect.width < data.m_minSize.x )
393 {
394 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
395 data.m_rect.x -= data.m_minSize.x - data.m_rect.width;
396 data.m_rect.width = data.m_minSize.x;
397 }
398 if ( data.m_maxSize.x != -1 && data.m_rect.width > data.m_maxSize.x )
399 {
400 if ( data.m_flags & wxINTERACTIVE_RESIZE_W )
401 data.m_rect.x -= data.m_minSize.x - data.m_rect.width;
402 data.m_rect.width = data.m_maxSize.x;
403 }
404 if ( data.m_minSize.y != -1 && data.m_rect.height < data.m_minSize.y )
405 {
406 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
407 data.m_rect.y -= data.m_minSize.y - data.m_rect.height;
408 data.m_rect.height = data.m_minSize.y;
409 }
410 if ( data.m_maxSize.y != -1 && data.m_rect.height > data.m_maxSize.y )
411 {
412 if ( data.m_flags & wxINTERACTIVE_RESIZE_N )
413 data.m_rect.y -= data.m_minSize.y - data.m_rect.height;
414 data.m_rect.height = data.m_maxSize.y;
415 }
416}
417
418void wxInteractiveMoveHandler::OnMouseMove(wxMouseEvent& event)
419{
420 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
421 event.Skip();
422
423 else if ( m_data.m_flags & wxINTERACTIVE_MOVE )
424 {
425 wxPoint diff = wxGetMousePosition() - m_data.m_pos;
426 m_data.m_rect = m_data.m_rectOrig;
427 m_data.m_rect.Offset(diff);
428 m_data.m_window->Move(m_data.m_rect.GetPosition());
429 }
430
431 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE )
432 {
433 wxPoint diff = wxGetMousePosition() - m_data.m_pos;
434 m_data.m_rect = m_data.m_rectOrig;
435 wxApplyResize(m_data, diff);
436 m_data.m_window->SetSize(m_data.m_rect);
437 }
438}
439
440void wxInteractiveMoveHandler::OnMouseDown(wxMouseEvent& event)
441{
442 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
443 {
71cb2ca1 444 m_data.m_evtLoop->Exit();
621b405f
VS
445 }
446}
447
448void wxInteractiveMoveHandler::OnKeyDown(wxKeyEvent& event)
449{
621b405f
VS
450 wxPoint diff(-1,-1);
451
452 switch ( event.GetKeyCode() )
453 {
454 case WXK_UP: diff = wxPoint(0, -16); break;
455 case WXK_DOWN: diff = wxPoint(0, 16); break;
456 case WXK_LEFT: diff = wxPoint(-16, 0); break;
457 case WXK_RIGHT: diff = wxPoint(16, 0); break;
458 case WXK_ESCAPE:
459 m_data.m_window->SetSize(m_data.m_rectOrig);
460 m_data.m_evtLoop->Exit();
461 return;
462 case WXK_RETURN:
463 m_data.m_evtLoop->Exit();
464 return;
465 }
466
467 if ( diff.x != -1 )
468 {
71cb2ca1
VS
469 if ( m_data.m_flags & wxINTERACTIVE_WAIT_FOR_INPUT )
470 {
471 m_data.m_flags &= ~wxINTERACTIVE_WAIT_FOR_INPUT;
472 if ( m_data.m_sizingCursor )
473 {
474 wxEndBusyCursor();
475 m_data.m_sizingCursor = FALSE;
476 }
477
478 if ( m_data.m_flags & wxINTERACTIVE_MOVE )
479 {
480 m_data.m_pos = m_data.m_window->GetPosition() +
481 wxPoint(m_data.m_window->GetSize().x/2, 8);
482 }
483 }
484
485 wxPoint warp;
486
621b405f
VS
487 if ( m_data.m_flags & wxINTERACTIVE_MOVE )
488 {
489 m_data.m_rect.Offset(diff);
490 m_data.m_window->Move(m_data.m_rect.GetPosition());
71cb2ca1 491 warp = wxPoint(m_data.m_window->GetSize().x/2, 8);
621b405f
VS
492 }
493 else /* wxINTERACTIVE_RESIZE */
494 {
71cb2ca1
VS
495 if ( !(m_data.m_flags &
496 (wxINTERACTIVE_RESIZE_N | wxINTERACTIVE_RESIZE_S)) )
621b405f
VS
497 {
498 if ( diff.y < 0 )
71cb2ca1 499 {
621b405f 500 m_data.m_flags |= wxINTERACTIVE_RESIZE_N;
71cb2ca1
VS
501 m_data.m_pos.y = m_data.m_window->GetPosition().y;
502 }
621b405f 503 else if ( diff.y > 0 )
71cb2ca1 504 {
621b405f 505 m_data.m_flags |= wxINTERACTIVE_RESIZE_S;
71cb2ca1
VS
506 m_data.m_pos.y = m_data.m_window->GetPosition().y +
507 m_data.m_window->GetSize().y;
508 }
509 }
510 if ( !(m_data.m_flags &
511 (wxINTERACTIVE_RESIZE_W | wxINTERACTIVE_RESIZE_E)) )
512 {
621b405f 513 if ( diff.x < 0 )
71cb2ca1 514 {
621b405f 515 m_data.m_flags |= wxINTERACTIVE_RESIZE_W;
71cb2ca1
VS
516 m_data.m_pos.x = m_data.m_window->GetPosition().x;
517 }
621b405f 518 else if ( diff.x > 0 )
71cb2ca1 519 {
621b405f 520 m_data.m_flags |= wxINTERACTIVE_RESIZE_E;
71cb2ca1
VS
521 m_data.m_pos.x = m_data.m_window->GetPosition().x +
522 m_data.m_window->GetSize().x;
523 }
621b405f 524 }
71cb2ca1 525
621b405f
VS
526 wxApplyResize(m_data, diff);
527 m_data.m_window->SetSize(m_data.m_rect);
71cb2ca1
VS
528
529 if ( m_data.m_flags & wxINTERACTIVE_RESIZE_W )
530 warp.x = 0;
531 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE_E )
532 warp.x = m_data.m_window->GetSize().x-1;
533 else
534 warp.x = wxGetMousePosition().x - m_data.m_window->GetPosition().x;
535
536 if ( m_data.m_flags & wxINTERACTIVE_RESIZE_N )
537 warp.y = 0;
538 else if ( m_data.m_flags & wxINTERACTIVE_RESIZE_S )
539 warp.y = m_data.m_window->GetSize().y-1;
540 else
541 warp.y = wxGetMousePosition().y - m_data.m_window->GetPosition().y;
621b405f 542 }
71cb2ca1
VS
543
544 warp -= m_data.m_window->GetClientAreaOrigin();
545 m_data.m_window->WarpPointer(warp.x, warp.y);
621b405f
VS
546 }
547}
548
549void wxInteractiveMoveHandler::OnMouseUp(wxMouseEvent& event)
550{
551 m_data.m_evtLoop->Exit();
552}
553
554
555void wxTopLevelWindow::InteractiveMove(int flags)
556{
557 wxASSERT_MSG( !((flags & wxINTERACTIVE_MOVE) && (flags & wxINTERACTIVE_RESIZE)),
558 wxT("can't move and resize window at the same time") );
559
560 wxASSERT_MSG( !(flags & wxINTERACTIVE_RESIZE) ||
561 (flags & wxINTERACTIVE_WAIT_FOR_INPUT) ||
562 (flags & wxINTERACTIVE_RESIZE_DIR),
563 wxT("direction of resizing not specified") );
564
565 wxInteractiveMoveData data;
566 wxEventLoop loop;
621b405f 567
71cb2ca1
VS
568 SetFocus();
569
570#ifndef __WXGTK__
571 if ( flags & wxINTERACTIVE_WAIT_FOR_INPUT )
572 {
573 wxCursor sizingCursor(wxCURSOR_SIZING);
574 wxBeginBusyCursor(&sizingCursor);
575 data.m_sizingCursor = TRUE;
576 }
577 else
578#endif
579 data.m_sizingCursor = FALSE;
621b405f
VS
580
581 data.m_window = this;
582 data.m_evtLoop = &loop;
583 data.m_flags = flags;
584 data.m_rect = data.m_rectOrig = GetRect();
585 data.m_pos = wxGetMousePosition();
586 data.m_minSize = wxSize(GetMinWidth(), GetMinHeight());
587 data.m_maxSize = wxSize(GetMaxWidth(), GetMaxHeight());
588
71cb2ca1
VS
589 wxEvtHandler *handler = new wxInteractiveMoveHandler(data);
590 this->PushEventHandler(handler);
621b405f
VS
591
592 CaptureMouse();
593 loop.Run();
594 ReleaseMouse();
595
71cb2ca1
VS
596 this->RemoveEventHandler(handler);
597 delete handler;
598
599 if ( data.m_sizingCursor )
600 wxEndBusyCursor();
621b405f
VS
601}
602
ea1b0d6c
VS
603// ----------------------------------------------------------------------------
604// actions
605// ----------------------------------------------------------------------------
606
813edf09
VS
607void wxTopLevelWindow::ClickTitleBarButton(long button)
608{
609 switch ( button )
610 {
611 case wxTOPLEVEL_BUTTON_CLOSE:
612 Close();
613 break;
614
615 case wxTOPLEVEL_BUTTON_ICONIZE:
616 Iconize();
617 break;
618
619 case wxTOPLEVEL_BUTTON_MAXIMIZE:
620 Maximize();
621 break;
622
623 case wxTOPLEVEL_BUTTON_RESTORE:
624 Restore();
625 break;
626
627 case wxTOPLEVEL_BUTTON_HELP:
628#if wxUSE_HELP
629 {
630 wxContextHelp contextHelp(this);
631 }
632#endif
633 break;
7ee7c43f 634
813edf09
VS
635 default:
636 wxFAIL_MSG(wxT("incorrect button specification"));
637 }
638}
639
ea1b0d6c
VS
640bool wxTopLevelWindow::PerformAction(const wxControlAction& action,
641 long numArg,
642 const wxString& strArg)
643{
7ee7c43f
VZ
644 bool isActive = numArg != 0;
645
ea1b0d6c
VS
646 if ( action == wxACTION_TOPLEVEL_ACTIVATE )
647 {
7ee7c43f 648 if ( m_isActive != isActive )
ea1b0d6c 649 {
7ee7c43f 650 m_isActive = isActive;
b22d16ad 651 RefreshTitleBar();
ea1b0d6c
VS
652 }
653 return TRUE;
654 }
7ee7c43f 655
813edf09
VS
656 else if ( action == wxACTION_TOPLEVEL_BUTTON_PRESS )
657 {
658 m_pressedButton = numArg;
659 RefreshTitleBar();
660 return TRUE;
661 }
7ee7c43f 662
813edf09
VS
663 else if ( action == wxACTION_TOPLEVEL_BUTTON_RELEASE )
664 {
665 m_pressedButton = 0;
666 RefreshTitleBar();
667 return TRUE;
668 }
7ee7c43f 669
813edf09
VS
670 else if ( action == wxACTION_TOPLEVEL_BUTTON_CLICK )
671 {
672 m_pressedButton = 0;
673 RefreshTitleBar();
674 ClickTitleBarButton(numArg);
675 return TRUE;
676 }
71e03035 677
b22d16ad
VS
678 else if ( action == wxACTION_TOPLEVEL_MOVE )
679 {
680 InteractiveMove(wxINTERACTIVE_MOVE);
681 return TRUE;
682 }
71e03035 683
b22d16ad
VS
684 else if ( action == wxACTION_TOPLEVEL_RESIZE )
685 {
686 int flags = wxINTERACTIVE_RESIZE;
687 if ( numArg & wxHT_TOPLEVEL_BORDER_N )
688 flags |= wxINTERACTIVE_RESIZE_N;
689 if ( numArg & wxHT_TOPLEVEL_BORDER_S )
690 flags |= wxINTERACTIVE_RESIZE_S;
691 if ( numArg & wxHT_TOPLEVEL_BORDER_W )
692 flags |= wxINTERACTIVE_RESIZE_W;
693 if ( numArg & wxHT_TOPLEVEL_BORDER_E )
694 flags |= wxINTERACTIVE_RESIZE_E;
695 InteractiveMove(flags);
696 return TRUE;
697 }
71e03035 698
ea1b0d6c
VS
699 else
700 return FALSE;
701}
702
71cb2ca1
VS
703void wxTopLevelWindow::OnSystemMenu(wxCommandEvent& event)
704{
705 bool ret = TRUE;
706
707 switch (event.GetId())
708 {
709 case wxID_CLOSE_FRAME:
710 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
711 wxTOPLEVEL_BUTTON_CLOSE);
712 break;
713 case wxID_MOVE_FRAME:
714 InteractiveMove(wxINTERACTIVE_MOVE | wxINTERACTIVE_WAIT_FOR_INPUT);
715 break;
716 case wxID_RESIZE_FRAME:
717 InteractiveMove(wxINTERACTIVE_RESIZE | wxINTERACTIVE_WAIT_FOR_INPUT);
718 break;
719 case wxID_MAXIMIZE_FRAME:
720 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
721 wxTOPLEVEL_BUTTON_MAXIMIZE);
722 break;
723 case wxID_ICONIZE_FRAME:
724 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
725 wxTOPLEVEL_BUTTON_ICONIZE);
726 break;
727 case wxID_RESTORE_FRAME:
728 ret = PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK,
729 wxTOPLEVEL_BUTTON_RESTORE);
730 break;
731
732 default:
733 ret = FALSE;
734 }
735
736 if ( !ret )
737 event.Skip();
738}
739
ea1b0d6c
VS
740
741// ============================================================================
742// wxStdFrameInputHandler: handles focus, resizing and titlebar buttons clicks
743// ============================================================================
744
745wxStdFrameInputHandler::wxStdFrameInputHandler(wxInputHandler *inphand)
746 : wxStdInputHandler(inphand)
747{
813edf09
VS
748 m_winCapture = NULL;
749 m_winHitTest = 0;
750 m_winPressed = 0;
b22d16ad 751 m_borderCursorOn = FALSE;
ea1b0d6c
VS
752}
753
754bool wxStdFrameInputHandler::HandleMouse(wxInputConsumer *consumer,
755 const wxMouseEvent& event)
756{
813edf09
VS
757 // the button has 2 states: pressed and normal with the following
758 // transitions between them:
759 //
760 // normal -> left down -> capture mouse and go to pressed state
761 // pressed -> left up inside -> generate click -> go to normal
762 // outside ------------------>
763 //
764 // the other mouse buttons are ignored
765 if ( event.Button(1) )
766 {
767 if ( event.ButtonDown(1) )
768 {
769 wxTopLevelWindow *w = wxStaticCast(consumer->GetInputWindow(), wxTopLevelWindow);
770 long hit = w->HitTest(event.GetPosition());
7ee7c43f 771
813edf09
VS
772 if ( hit & wxHT_TOPLEVEL_ANY_BUTTON )
773 {
774 m_winCapture = w;
775 m_winCapture->CaptureMouse();
776 m_winHitTest = hit;
777 m_winPressed = hit;
778 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
779 return TRUE;
780 }
aa74396d 781 else if ( (hit & wxHT_TOPLEVEL_TITLEBAR) && !w->IsMaximized() )
b22d16ad
VS
782 {
783 consumer->PerformAction(wxACTION_TOPLEVEL_MOVE);
784 return TRUE;
785 }
786 else if ( (consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER)
787 && (hit & wxHT_TOPLEVEL_ANY_BORDER) )
788 {
789 consumer->PerformAction(wxACTION_TOPLEVEL_RESIZE, hit);
790 return TRUE;
791 }
813edf09
VS
792 }
793
794 else // up
795 {
796 if ( m_winCapture )
797 {
798 m_winCapture->ReleaseMouse();
799 m_winCapture = NULL;
800
801 if ( m_winHitTest == m_winPressed )
802 {
803 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_CLICK, m_winPressed);
804 return TRUE;
805 }
806 }
807 //else: the mouse was released outside the window, this doesn't
808 // count as a click
809 }
810 }
811
ea1b0d6c
VS
812 return wxStdInputHandler::HandleMouse(consumer, event);
813}
814
7ee7c43f 815bool wxStdFrameInputHandler::HandleMouseMove(wxInputConsumer *consumer,
ea1b0d6c
VS
816 const wxMouseEvent& event)
817{
813edf09
VS
818 if ( event.GetEventObject() == m_winCapture )
819 {
820 long hit = m_winCapture->HitTest(event.GetPosition());
821
822 if ( hit != m_winHitTest )
823 {
824 if ( hit != m_winPressed )
825 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_RELEASE, m_winPressed);
826 else
827 consumer->PerformAction(wxACTION_TOPLEVEL_BUTTON_PRESS, m_winPressed);
7ee7c43f 828
813edf09
VS
829 m_winHitTest = hit;
830 return TRUE;
831 }
832 }
b22d16ad
VS
833 else if ( consumer->GetInputWindow()->GetWindowStyle() & wxRESIZE_BORDER )
834 {
71e03035 835 wxTopLevelWindow *win = wxStaticCast(consumer->GetInputWindow(),
b22d16ad
VS
836 wxTopLevelWindow);
837 long hit = win->HitTest(event.GetPosition());
71e03035 838
b22d16ad
VS
839 if ( hit != m_winHitTest )
840 {
841 m_winHitTest = hit;
71e03035 842
b22d16ad
VS
843 if ( m_borderCursorOn )
844 {
845 m_borderCursorOn = FALSE;
846 win->SetCursor(m_origCursor);
847 }
71e03035 848
b22d16ad
VS
849 if ( hit & wxHT_TOPLEVEL_ANY_BORDER )
850 {
851 m_borderCursorOn = TRUE;
852 wxCursor cur;
71e03035 853
b22d16ad
VS
854 switch (hit)
855 {
856 case wxHT_TOPLEVEL_BORDER_N:
857 case wxHT_TOPLEVEL_BORDER_S:
858 cur = wxCursor(wxCURSOR_SIZENS);
859 break;
860 case wxHT_TOPLEVEL_BORDER_W:
861 case wxHT_TOPLEVEL_BORDER_E:
862 cur = wxCursor(wxCURSOR_SIZEWE);
863 break;
864 case wxHT_TOPLEVEL_BORDER_NE:
865 case wxHT_TOPLEVEL_BORDER_SW:
866 cur = wxCursor(wxCURSOR_SIZENESW);
867 break;
868 case wxHT_TOPLEVEL_BORDER_NW:
869 case wxHT_TOPLEVEL_BORDER_SE:
870 cur = wxCursor(wxCURSOR_SIZENWSE);
871 break;
872 default:
873 m_borderCursorOn = FALSE;
874 break;
875 }
876 if ( m_borderCursorOn )
877 {
878 m_origCursor = win->GetCursor();
879 win->SetCursor(cur);
880 }
881 }
882 }
883 }
813edf09 884
ea1b0d6c
VS
885 return wxStdInputHandler::HandleMouseMove(consumer, event);
886}
887
7ee7c43f 888bool wxStdFrameInputHandler::HandleActivation(wxInputConsumer *consumer,
ea1b0d6c
VS
889 bool activated)
890{
b22d16ad
VS
891 if ( m_borderCursorOn )
892 {
893 consumer->GetInputWindow()->SetCursor(m_origCursor);
894 m_borderCursorOn = FALSE;
895 }
ea1b0d6c
VS
896 consumer->PerformAction(wxACTION_TOPLEVEL_ACTIVATE, activated);
897 return FALSE;
898}