]> git.saurik.com Git - wxWidgets.git/blame - src/univ/winuniv.cpp
applied patch 1216005 : message and new NavDirDialog
[wxWidgets.git] / src / univ / winuniv.cpp
CommitLineData
1e6feb95
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: univ/window.cpp
3// Purpose: implementation of extra wxWindow methods for wxUniv port
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.08.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
196cc38b 21 #pragma implementation "univwindow.h"
1e6feb95
VZ
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/window.h"
34 #include "wx/dcclient.h"
35 #include "wx/dcmemory.h"
36 #include "wx/event.h"
37 #include "wx/scrolbar.h"
38 #include "wx/menu.h"
8cb172b4 39 #include "wx/frame.h"
1e6feb95
VZ
40#endif // WX_PRECOMP
41
ee7908db 42#include "wx/log.h"
1e6feb95
VZ
43#include "wx/univ/colschem.h"
44#include "wx/univ/renderer.h"
45#include "wx/univ/theme.h"
46
47#if wxUSE_CARET
48 #include "wx/caret.h"
49#endif // wxUSE_CARET
50
51// turn Refresh() debugging on/off
52#define WXDEBUG_REFRESH
53
54#ifndef __WXDEBUG__
55 #undef WXDEBUG_REFRESH
56#endif
57
cff7ef89
VS
58#if defined(WXDEBUG_REFRESH) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
59#include "wx/msw/private.h"
60#endif
61
1e6feb95
VZ
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// event tables
68// ----------------------------------------------------------------------------
69
70// we can't use wxWindowNative here as it won't be expanded inside the macro
71#if defined(__WXMSW__)
72 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMSW)
73#elif defined(__WXGTK__)
74 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowGTK)
75#elif defined(__WXMGL__)
76 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowMGL)
8354aa92
RR
77#elif defined(__WXX11__)
78 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowX11)
19193a2c
KB
79#elif defined(__WXPM__)
80 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowOS2)
1e6feb95
VZ
81#endif
82
83BEGIN_EVENT_TABLE(wxWindow, wxWindowNative)
84 EVT_SIZE(wxWindow::OnSize)
85
86#if wxUSE_ACCEL || wxUSE_MENUS
87 EVT_KEY_DOWN(wxWindow::OnKeyDown)
88#endif // wxUSE_ACCEL
89
90#if wxUSE_MENUS
91 EVT_CHAR(wxWindow::OnChar)
92 EVT_KEY_UP(wxWindow::OnKeyUp)
93#endif // wxUSE_MENUS
94
95 EVT_PAINT(wxWindow::OnPaint)
96 EVT_NC_PAINT(wxWindow::OnNcPaint)
97 EVT_ERASE_BACKGROUND(wxWindow::OnErase)
98END_EVENT_TABLE()
99
100// ----------------------------------------------------------------------------
101// creation
102// ----------------------------------------------------------------------------
103
104void wxWindow::Init()
105{
106 m_scrollbarVert =
107 m_scrollbarHorz = (wxScrollBar *)NULL;
108
a290fa5a 109 m_isCurrent = false;
1e6feb95
VZ
110
111 m_renderer = wxTheme::Get()->GetRenderer();
2b5f62a0 112
a290fa5a
WS
113 m_oldSize.x = wxDefaultCoord;
114 m_oldSize.y = wxDefaultCoord;
1e6feb95
VZ
115}
116
117bool wxWindow::Create(wxWindow *parent,
118 wxWindowID id,
119 const wxPoint& pos,
120 const wxSize& size,
121 long style,
122 const wxString& name)
123{
588cf92f 124 long actualStyle = style;
8da1f9a9 125
588cf92f
JS
126 // FIXME: may need this on other platforms
127#ifdef __WXMSW__
128 actualStyle &= ~wxVSCROLL;
129 actualStyle &= ~wxHSCROLL;
8da1f9a9
VZ
130#endif
131
2387541f
VZ
132 // we add wxCLIP_CHILDREN to get the same ("natural") behaviour under MSW
133 // as under the other platforms
1e6feb95 134 if ( !wxWindowNative::Create(parent, id, pos, size,
588cf92f 135 actualStyle | wxCLIP_CHILDREN,
2387541f 136 name) )
1e6feb95 137 {
a290fa5a 138 return false;
1e6feb95
VZ
139 }
140
588cf92f
JS
141 // Set full style again, including those we didn't want present
142 // when calling the base window Create().
143 wxWindowBase::SetWindowStyleFlag(style);
144
1cb853a8
JS
145 // if we allow or should always have a vertical scrollbar, make it
146 if ( style & wxVSCROLL || style & wxALWAYS_SHOW_SB )
1e6feb95 147 {
ab6b6b15 148#if wxUSE_TWO_WINDOWS
a290fa5a 149 SetInsertIntoMain( true );
ab6b6b15 150#endif
a290fa5a 151 m_scrollbarVert = new wxScrollBar(this, wxID_ANY,
1e6feb95
VZ
152 wxDefaultPosition, wxDefaultSize,
153 wxSB_VERTICAL);
ab6b6b15 154#if wxUSE_TWO_WINDOWS
a290fa5a 155 SetInsertIntoMain( false );
ab6b6b15 156#endif
588cf92f 157 }
1e6feb95 158
1cb853a8 159 // if we should allow a horizontal scrollbar, make it
588cf92f
JS
160 if ( style & wxHSCROLL )
161 {
162#if wxUSE_TWO_WINDOWS
a290fa5a 163 SetInsertIntoMain( true );
588cf92f 164#endif
a290fa5a 165 m_scrollbarHorz = new wxScrollBar(this, wxID_ANY,
588cf92f
JS
166 wxDefaultPosition, wxDefaultSize,
167 wxSB_HORIZONTAL);
168#if wxUSE_TWO_WINDOWS
a290fa5a 169 SetInsertIntoMain( false );
588cf92f
JS
170#endif
171 }
8da1f9a9 172
588cf92f
JS
173 if (m_scrollbarHorz || m_scrollbarVert)
174 {
175 // position it/them
1e6feb95
VZ
176 PositionScrollbars();
177 }
178
a290fa5a 179 return true;
1e6feb95
VZ
180}
181
8da1f9a9
VZ
182wxWindow::~wxWindow()
183{
184 m_isBeingDeleted = true;
185
186 // we have to destroy our children before we're destroyed because our
187 // children suppose that we're of type wxWindow, not just wxWindowNative,
188 // and so bad things may happen if they're deleted from the base class dtor
189 // as by then we're not a wxWindow any longer and wxUniv-specific virtual
190 // functions can't be called
191 DestroyChildren();
192}
193
1e6feb95
VZ
194// ----------------------------------------------------------------------------
195// background pixmap
196// ----------------------------------------------------------------------------
197
198void wxWindow::SetBackground(const wxBitmap& bitmap,
199 int alignment,
200 wxStretch stretch)
201{
202 m_bitmapBg = bitmap;
203 m_alignBgBitmap = alignment;
204 m_stretchBgBitmap = stretch;
205}
206
207const wxBitmap& wxWindow::GetBackgroundBitmap(int *alignment,
208 wxStretch *stretch) const
209{
210 if ( m_bitmapBg.Ok() )
211 {
212 if ( alignment )
213 *alignment = m_alignBgBitmap;
214 if ( stretch )
215 *stretch = m_stretchBgBitmap;
216 }
217
218 return m_bitmapBg;
219}
220
221// ----------------------------------------------------------------------------
222// painting
223// ----------------------------------------------------------------------------
224
1e6feb95 225// the event handlers executed when the window must be repainted
3795cdba 226void wxWindow::OnNcPaint(wxNcPaintEvent& WXUNUSED(event))
1e6feb95
VZ
227{
228 if ( m_renderer )
229 {
230 // get the window rect
231 wxRect rect;
232 wxSize size = GetSize();
233 rect.x =
234 rect.y = 0;
235 rect.width = size.x;
236 rect.height = size.y;
237
238 // if the scrollbars are outside the border, we must adjust the rect to
239 // exclude them
240 if ( !m_renderer->AreScrollbarsInsideBorder() )
241 {
242 wxScrollBar *scrollbar = GetScrollbar(wxVERTICAL);
243 if ( scrollbar )
244 rect.width -= scrollbar->GetSize().x;
245
246 scrollbar = GetScrollbar(wxHORIZONTAL);
247 if ( scrollbar )
248 rect.height -= scrollbar->GetSize().y;
249 }
250
251 // get the DC and draw the border on it
252 wxWindowDC dc(this);
253 DoDrawBorder(dc, rect);
254 }
255}
256
257void wxWindow::OnPaint(wxPaintEvent& event)
258{
259 if ( !m_renderer )
260 {
261 // it is a native control which paints itself
262 event.Skip();
263 }
264 else
265 {
266 // get the DC to use and create renderer on it
267 wxPaintDC dc(this);
268 wxControlRenderer renderer(this, dc, m_renderer);
269
270 // draw the control
271 DoDraw(&renderer);
272 }
273}
274
5ebcf581
RR
275// the event handler executed when the window background must be painted
276void wxWindow::OnErase(wxEraseEvent& event)
277{
278 if ( !m_renderer )
279 {
280 event.Skip();
281
282 return;
283 }
2b5f62a0 284
5ebcf581
RR
285 DoDrawBackground(*event.GetDC());
286
287 // if we have both scrollbars, we also have a square in the corner between
288 // them which we must paint
289 if ( m_scrollbarVert && m_scrollbarHorz )
290 {
291 wxSize size = GetSize();
292 wxRect rectClient = GetClientRect(),
293 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
294
295 wxRect rectCorner;
296 rectCorner.x = rectClient.GetRight() + 1;
297 rectCorner.y = rectClient.GetBottom() + 1;
298 rectCorner.SetRight(size.x - rectBorder.width);
299 rectCorner.SetBottom(size.y - rectBorder.height);
300
301 if ( GetUpdateRegion().Contains(rectCorner) )
302 {
303 m_renderer->DrawScrollCorner(*event.GetDC(), rectCorner);
304 }
305 }
306}
307
1e6feb95
VZ
308bool wxWindow::DoDrawBackground(wxDC& dc)
309{
1e6feb95 310 wxRect rect;
2b5f62a0 311
5ebcf581
RR
312 wxSize size = GetSize(); // Why not GetClientSize() ?
313 rect.x = 0;
314 rect.y = 0;
315 rect.width = size.x;
316 rect.height = size.y;
2b5f62a0
VZ
317
318 wxWindow * const parent = GetParent();
3d74a760 319 if ( HasTransparentBackground() && parent )
1e6feb95 320 {
5ebcf581 321 wxASSERT( !IsTopLevel() );
2b5f62a0 322
5ebcf581 323 wxPoint pos = GetPosition();
2b5f62a0 324
5ebcf581 325 AdjustForParentClientOrigin( pos.x, pos.y, 0 );
2b5f62a0 326
5ebcf581 327 // Adjust DC logical origin
635eb8bc
JS
328 wxCoord org_x, org_y, x, y;
329 dc.GetLogicalOrigin( &org_x, &org_y );
330 x = org_x + pos.x;
331 y = org_y + pos.y;
5ebcf581 332 dc.SetLogicalOrigin( x, y );
2b5f62a0 333
5ebcf581
RR
334 // Adjust draw rect
335 rect.x = pos.x;
336 rect.y = pos.y;
2b5f62a0 337
5ebcf581 338 // Let parent draw the background
2b5f62a0 339 parent->EraseBackground( dc, rect );
635eb8bc
JS
340
341 // Restore DC logical origin
342 dc.SetLogicalOrigin( org_x, org_y );
1e6feb95 343 }
5ebcf581
RR
344 else
345 {
346 // Draw background ouselves
2b5f62a0 347 EraseBackground( dc, rect );
5ebcf581 348 }
2b5f62a0 349
a290fa5a 350 return true;
5ebcf581 351}
1e6feb95 352
5ebcf581
RR
353void wxWindow::EraseBackground(wxDC& dc, const wxRect& rect)
354{
1e6feb95
VZ
355 if ( GetBackgroundBitmap().Ok() )
356 {
5ebcf581 357 // Get the bitmap and the flags
1e6feb95
VZ
358 int alignment;
359 wxStretch stretch;
360 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
361 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
362 }
2b5f62a0 363 else
1e6feb95 364 {
5ebcf581 365 // Just fill it with bg colour if no bitmap
2b5f62a0 366
1e6feb95
VZ
367 m_renderer->DrawBackground(dc, wxTHEME_BG_COLOUR(this),
368 rect, GetStateFlags());
369 }
1e6feb95
VZ
370}
371
372void wxWindow::DoDrawBorder(wxDC& dc, const wxRect& rect)
373{
374 // draw outline unless the update region is enitrely inside it in which
375 // case we don't need to do it
376#if 0 // doesn't seem to work, why?
377 if ( wxRegion(rect).Contains(GetUpdateRegion().GetBox()) != wxInRegion )
378#endif
379 {
380 m_renderer->DrawBorder(dc, GetBorder(), rect, GetStateFlags());
381 }
382}
383
61fef19b 384void wxWindow::DoDraw(wxControlRenderer * WXUNUSED(renderer))
1e6feb95
VZ
385{
386}
387
388void wxWindow::Refresh(bool eraseBackground, const wxRect *rectClient)
389{
390 wxRect rectWin;
391 wxPoint pt = GetClientAreaOrigin();
392
393 wxSize size = GetClientSize();
394
395 if ( rectClient )
396 {
397 rectWin = *rectClient;
398
399 // don't refresh anything beyond the client area (scrollbars for
400 // example)
401 if ( rectWin.GetRight() > size.x )
402 rectWin.SetRight(size.x);
403 if ( rectWin.GetBottom() > size.y )
404 rectWin.SetBottom(size.y);
405
406 rectWin.Offset(pt);
407 }
408 else // refresh the entire client area
409 {
410 rectWin.x = pt.x;
411 rectWin.y = pt.y;
412 rectWin.width = size.x;
413 rectWin.height = size.y;
414 }
415
416 // debugging helper
417#ifdef WXDEBUG_REFRESH
a290fa5a 418 static bool s_refreshDebug = false;
1e6feb95
VZ
419 if ( s_refreshDebug )
420 {
421 wxWindowDC dc(this);
422 dc.SetBrush(*wxCYAN_BRUSH);
423 dc.SetPen(*wxTRANSPARENT_PEN);
424 dc.DrawRectangle(rectWin);
425
426 // under Unix we use "--sync" X option for this
8cb172b4 427 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1e6feb95
VZ
428 ::GdiFlush();
429 ::Sleep(200);
430 #endif // __WXMSW__
431 }
432#endif // WXDEBUG_REFRESH
433
434 wxWindowNative::Refresh(eraseBackground, &rectWin);
82e5f91b
JS
435
436 // Refresh all sub controls if any.
ac32ba44 437 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
82e5f91b
JS
438 while ( node )
439 {
440 wxWindow *win = node->GetData();
8da1f9a9 441 // Only refresh sub controls when it is visible
82e5f91b 442 // and when it is in the update region.
333a6456 443 if(!win->IsKindOf(CLASSINFO(wxTopLevelWindow)) && win->IsShown() && wxRegion(rectWin).Contains(win->GetRect()) != wxOutRegion)
82e5f91b 444 win->Refresh(eraseBackground, &rectWin);
8da1f9a9 445
82e5f91b
JS
446 node = node->GetNext();
447 }
1e6feb95
VZ
448}
449
450// ----------------------------------------------------------------------------
451// state flags
452// ----------------------------------------------------------------------------
453
454bool wxWindow::Enable(bool enable)
455{
456 if ( !wxWindowNative::Enable(enable) )
a290fa5a 457 return false;
1e6feb95
VZ
458
459 // disabled window can't keep focus
c4bcd8fc 460 if ( FindFocus() == this && GetParent() != NULL )
1e6feb95
VZ
461 {
462 GetParent()->SetFocus();
463 }
464
465 if ( m_renderer )
466 {
467 // a window with renderer is drawn by ourselves and it has to be
468 // refreshed to reflect its new status
469 Refresh();
470 }
471
a290fa5a 472 return true;
1e6feb95
VZ
473}
474
475bool wxWindow::IsFocused() const
476{
477 wxWindow *self = wxConstCast(this, wxWindow);
478 return self->FindFocus() == self;
479}
480
481bool wxWindow::IsPressed() const
482{
a290fa5a 483 return false;
1e6feb95
VZ
484}
485
486bool wxWindow::IsDefault() const
487{
a290fa5a 488 return false;
1e6feb95
VZ
489}
490
491bool wxWindow::IsCurrent() const
492{
493 return m_isCurrent;
494}
495
496bool wxWindow::SetCurrent(bool doit)
497{
498 if ( doit == m_isCurrent )
a290fa5a 499 return false;
1e6feb95
VZ
500
501 m_isCurrent = doit;
502
503 if ( CanBeHighlighted() )
504 Refresh();
505
a290fa5a 506 return true;
1e6feb95
VZ
507}
508
509int wxWindow::GetStateFlags() const
510{
511 int flags = 0;
512 if ( !IsEnabled() )
513 flags |= wxCONTROL_DISABLED;
514
515 // the following states are only possible if our application is active - if
516 // it is not, even our default/focused controls shouldn't appear as such
517 if ( wxTheApp->IsActive() )
518 {
519 if ( IsCurrent() )
520 flags |= wxCONTROL_CURRENT;
521 if ( IsFocused() )
522 flags |= wxCONTROL_FOCUSED;
523 if ( IsPressed() )
524 flags |= wxCONTROL_PRESSED;
525 if ( IsDefault() )
526 flags |= wxCONTROL_ISDEFAULT;
527 }
528
529 return flags;
530}
531
532// ----------------------------------------------------------------------------
533// size
534// ----------------------------------------------------------------------------
535
536void wxWindow::OnSize(wxSizeEvent& event)
537{
aae73669 538 event.Skip();
2b5f62a0 539
1e6feb95
VZ
540 if ( m_scrollbarVert || m_scrollbarHorz )
541 {
542 PositionScrollbars();
543 }
2b5f62a0 544
ab6b6b15 545#if 0 // ndef __WXMSW__
ec47a147 546 // Refresh the area (strip) previously occupied by the border
2b5f62a0 547
e441e1f4 548 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) && IsShown() )
ec47a147 549 {
aae73669
RR
550 // This code assumes that wxSizeEvent.GetSize() returns
551 // the area of the entire window, not just the client
552 // area.
ec47a147 553 wxSize newSize = event.GetSize();
2b5f62a0 554
a290fa5a 555 if (m_oldSize.x == wxDefaultCoord && m_oldSize.y == wxDefaultCoord)
aae73669
RR
556 {
557 m_oldSize = newSize;
558 return;
559 }
2b5f62a0 560
ec47a147
RR
561 if (HasFlag( wxSIMPLE_BORDER ))
562 {
563 if (newSize.y > m_oldSize.y)
564 {
565 wxRect rect;
566 rect.x = 0;
567 rect.width = m_oldSize.x;
aae73669 568 rect.y = m_oldSize.y-2;
ec47a147 569 rect.height = 1;
a290fa5a 570 Refresh( true, &rect );
ec47a147 571 }
aae73669
RR
572 else if (newSize.y < m_oldSize.y)
573 {
574 wxRect rect;
575 rect.y = newSize.y;
576 rect.x = 0;
577 rect.height = 1;
578 rect.width = newSize.x;
a290fa5a 579 wxWindowNative::Refresh( true, &rect );
aae73669 580 }
2b5f62a0 581
ec47a147
RR
582 if (newSize.x > m_oldSize.x)
583 {
584 wxRect rect;
585 rect.y = 0;
586 rect.height = m_oldSize.y;
aae73669 587 rect.x = m_oldSize.x-2;
ec47a147 588 rect.width = 1;
a290fa5a 589 Refresh( true, &rect );
ec47a147 590 }
aae73669
RR
591 else if (newSize.x < m_oldSize.x)
592 {
593 wxRect rect;
594 rect.x = newSize.x;
595 rect.y = 0;
596 rect.width = 1;
597 rect.height = newSize.y;
a290fa5a 598 wxWindowNative::Refresh( true, &rect );
aae73669 599 }
ec47a147
RR
600 }
601 else
602 if (HasFlag( wxSUNKEN_BORDER ) || HasFlag( wxRAISED_BORDER ))
603 {
604 if (newSize.y > m_oldSize.y)
605 {
606 wxRect rect;
607 rect.x = 0;
608 rect.width = m_oldSize.x;
aae73669 609 rect.y = m_oldSize.y-4;
ec47a147 610 rect.height = 2;
a290fa5a 611 Refresh( true, &rect );
ec47a147 612 }
aae73669
RR
613 else if (newSize.y < m_oldSize.y)
614 {
615 wxRect rect;
616 rect.y = newSize.y;
617 rect.x = 0;
618 rect.height = 2;
619 rect.width = newSize.x;
a290fa5a 620 wxWindowNative::Refresh( true, &rect );
aae73669 621 }
2b5f62a0 622
ec47a147
RR
623 if (newSize.x > m_oldSize.x)
624 {
625 wxRect rect;
626 rect.y = 0;
627 rect.height = m_oldSize.y;
aae73669 628 rect.x = m_oldSize.x-4;
ec47a147 629 rect.width = 2;
a290fa5a 630 Refresh( true, &rect );
ec47a147 631 }
aae73669
RR
632 else if (newSize.x < m_oldSize.x)
633 {
634 wxRect rect;
635 rect.x = newSize.x;
636 rect.y = 0;
637 rect.width = 2;
638 rect.height = newSize.y;
a290fa5a 639 wxWindowNative::Refresh( true, &rect );
aae73669 640 }
ec47a147 641 }
2b5f62a0 642
ec47a147
RR
643 m_oldSize = newSize;
644 }
645#endif
1e6feb95
VZ
646}
647
648wxSize wxWindow::DoGetBestSize() const
649{
650 return AdjustSize(DoGetBestClientSize());
651}
652
653wxSize wxWindow::DoGetBestClientSize() const
654{
655 return wxWindowNative::DoGetBestSize();
656}
657
658wxSize wxWindow::AdjustSize(const wxSize& size) const
659{
660 wxSize sz = size;
661 if ( m_renderer )
662 m_renderer->AdjustSize(&sz, this);
663 return sz;
664}
665
666wxPoint wxWindow::GetClientAreaOrigin() const
667{
668 wxPoint pt = wxWindowBase::GetClientAreaOrigin();
669
ab6b6b15
RR
670#if wxUSE_TWO_WINDOWS
671#else
1e6feb95
VZ
672 if ( m_renderer )
673 pt += m_renderer->GetBorderDimensions(GetBorder()).GetPosition();
ab6b6b15 674#endif
1e6feb95
VZ
675
676 return pt;
677}
678
679void wxWindow::DoGetClientSize(int *width, int *height) const
680{
3379ed37
VZ
681 // if it is a native window, we assume it handles the scrollbars itself
682 // too - and if it doesn't, there is not much we can do
683 if ( !m_renderer )
684 {
685 wxWindowNative::DoGetClientSize(width, height);
686
687 return;
688 }
689
1e6feb95
VZ
690 int w, h;
691 wxWindowNative::DoGetClientSize(&w, &h);
692
693 // we assume that the scrollbars are positioned correctly (by a previous
694 // call to PositionScrollbars()) here
695
696 wxRect rectBorder;
697 if ( m_renderer )
698 rectBorder = m_renderer->GetBorderDimensions(GetBorder());
699
700 bool inside = m_renderer->AreScrollbarsInsideBorder();
701
702 if ( width )
703 {
704 // in any case, take account of the scrollbar
705 if ( m_scrollbarVert )
706 w -= m_scrollbarVert->GetSize().x;
707
708 // if we don't have scrollbar or if it is outside the border (and not
709 // blended into it), take account of the right border as well
710 if ( !m_scrollbarVert || inside )
711 w -= rectBorder.width;
712
713 // and always account for the left border
714 *width = w - rectBorder.x;
715
716 // we shouldn't return invalid width
717 if ( *width < 0 )
718 *width = 0;
719 }
720
721 if ( height )
722 {
723 if ( m_scrollbarHorz )
724 h -= m_scrollbarHorz->GetSize().y;
725
726 if ( !m_scrollbarHorz || inside )
727 h -= rectBorder.height;
728
729 *height = h - rectBorder.y;
730
731 // we shouldn't return invalid height
732 if ( *height < 0 )
733 *height = 0;
734 }
735}
736
737void wxWindow::DoSetClientSize(int width, int height)
738{
739 // take into account the borders
740 wxRect rectBorder = m_renderer->GetBorderDimensions(GetBorder());
741 width += rectBorder.x;
742 height += rectBorder.y;
743
744 // and the scrollbars (as they may be offset into the border, use the
745 // scrollbar position, not size - this supposes that PositionScrollbars()
746 // had been called before)
747 bool inside = m_renderer->AreScrollbarsInsideBorder();
748 wxSize size = GetSize();
749 if ( m_scrollbarVert )
750 width += size.x - m_scrollbarVert->GetPosition().x;
751 if ( !m_scrollbarVert || inside )
752 width += rectBorder.width;
753
754 if ( m_scrollbarHorz )
755 height += size.y - m_scrollbarHorz->GetPosition().y;
756 if ( !m_scrollbarHorz || inside )
757 height += rectBorder.height;
758
759 wxWindowNative::DoSetClientSize(width, height);
760}
761
762wxHitTest wxWindow::DoHitTest(wxCoord x, wxCoord y) const
763{
764 wxHitTest ht = wxWindowNative::DoHitTest(x, y);
765 if ( ht == wxHT_WINDOW_INSIDE )
766 {
767 if ( m_scrollbarVert && x >= m_scrollbarVert->GetPosition().x )
768 {
769 // it can still be changed below because it may also be the corner
770 ht = wxHT_WINDOW_VERT_SCROLLBAR;
771 }
772
773 if ( m_scrollbarHorz && y >= m_scrollbarHorz->GetPosition().y )
774 {
775 ht = ht == wxHT_WINDOW_VERT_SCROLLBAR ? wxHT_WINDOW_CORNER
776 : wxHT_WINDOW_HORZ_SCROLLBAR;
777 }
778 }
779
780 return ht;
781}
782
783// ----------------------------------------------------------------------------
784// scrolling: we implement it entirely ourselves except for ScrollWindow()
785// function which is supposed to be (efficiently) implemented by the native
786// window class
787// ----------------------------------------------------------------------------
788
789void wxWindow::RefreshScrollbars()
790{
791 if ( m_scrollbarHorz )
792 m_scrollbarHorz->Refresh();
793
794 if ( m_scrollbarVert )
795 m_scrollbarVert->Refresh();
796}
797
798void wxWindow::PositionScrollbars()
799{
800 // do not use GetClientSize/Rect as it relies on the scrollbars being
801 // correctly positioned
802
803 wxSize size = GetSize();
804 wxBorder border = GetBorder();
805 wxRect rectBorder = m_renderer->GetBorderDimensions(border);
806 bool inside = m_renderer->AreScrollbarsInsideBorder();
807
808 int height = m_scrollbarHorz ? m_scrollbarHorz->GetSize().y : 0;
809 int width = m_scrollbarVert ? m_scrollbarVert->GetSize().x : 0;
810
811 wxRect rectBar;
812 if ( m_scrollbarVert )
813 {
814 rectBar.x = size.x - width;
815 if ( inside )
816 rectBar.x -= rectBorder.width;
817 rectBar.width = width;
818 rectBar.y = 0;
819 if ( inside )
820 rectBar.y += rectBorder.y;
821 rectBar.height = size.y - height;
822 if ( inside )
823 rectBar.height -= rectBorder.y + rectBorder.height;
824
825 m_scrollbarVert->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
826 }
827
828 if ( m_scrollbarHorz )
829 {
830 rectBar.y = size.y - height;
831 if ( inside )
832 rectBar.y -= rectBorder.height;
833 rectBar.height = height;
834 rectBar.x = 0;
835 if ( inside )
836 rectBar.x += rectBorder.x;
837 rectBar.width = size.x - width;
838 if ( inside )
839 rectBar.width -= rectBorder.x + rectBorder.width;
840
841 m_scrollbarHorz->SetSize(rectBar, wxSIZE_NO_ADJUSTMENTS);
842 }
843
844 RefreshScrollbars();
845}
846
847void wxWindow::SetScrollbar(int orient,
848 int pos,
849 int pageSize,
850 int range,
851 bool refresh)
852{
3f393e3d
VZ
853 wxASSERT_MSG( pageSize <= range,
854 _T("page size can't be greater than range") );
855
a290fa5a 856 bool hasClientSizeChanged = false;
1e6feb95 857 wxScrollBar *scrollbar = GetScrollbar(orient);
3f393e3d 858 if ( range && (pageSize < range) )
1e6feb95
VZ
859 {
860 if ( !scrollbar )
861 {
862 // create it
ab6b6b15 863#if wxUSE_TWO_WINDOWS
a290fa5a 864 SetInsertIntoMain( true );
ab6b6b15 865#endif
a290fa5a 866 scrollbar = new wxScrollBar(this, wxID_ANY,
1e6feb95
VZ
867 wxDefaultPosition, wxDefaultSize,
868 orient & wxVERTICAL ? wxSB_VERTICAL
869 : wxSB_HORIZONTAL);
ab6b6b15 870#if wxUSE_TWO_WINDOWS
a290fa5a 871 SetInsertIntoMain( false );
ab6b6b15 872#endif
1e6feb95
VZ
873 if ( orient & wxVERTICAL )
874 m_scrollbarVert = scrollbar;
875 else
876 m_scrollbarHorz = scrollbar;
877
878 // the client area diminished as we created a scrollbar
a290fa5a 879 hasClientSizeChanged = true;
1e6feb95
VZ
880
881 PositionScrollbars();
882 }
883 else if ( GetWindowStyle() & wxALWAYS_SHOW_SB )
884 {
885 // we might have disabled it before
886 scrollbar->Enable();
887 }
888
889 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
890 }
891 else // no range means no scrollbar
892 {
893 if ( scrollbar )
894 {
895 // wxALWAYS_SHOW_SB only applies to the vertical scrollbar
896 if ( (orient & wxVERTICAL) && (GetWindowStyle() & wxALWAYS_SHOW_SB) )
897 {
898 // just disable the scrollbar
899 scrollbar->SetScrollbar(pos, pageSize, range, pageSize, refresh);
900 scrollbar->Disable();
901 }
902 else // really remove the scrollbar
903 {
904 delete scrollbar;
905
906 if ( orient & wxVERTICAL )
907 m_scrollbarVert = NULL;
908 else
909 m_scrollbarHorz = NULL;
910
911 // the client area increased as we removed a scrollbar
a290fa5a 912 hasClientSizeChanged = true;
1e6feb95
VZ
913
914 // the size of the remaining scrollbar must be adjusted
915 if ( m_scrollbarHorz || m_scrollbarVert )
916 {
917 PositionScrollbars();
918 }
919 }
920 }
921 }
922
923 // give the window a chance to relayout
924 if ( hasClientSizeChanged )
925 {
a17a79ba
RR
926#if wxUSE_TWO_WINDOWS
927 wxWindowNative::SetSize( GetSize() );
928#else
1e6feb95
VZ
929 wxSizeEvent event(GetSize());
930 (void)GetEventHandler()->ProcessEvent(event);
a17a79ba 931#endif
1e6feb95
VZ
932 }
933}
934
61fef19b 935void wxWindow::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
1e6feb95
VZ
936{
937 wxScrollBar *scrollbar = GetScrollbar(orient);
938 wxCHECK_RET( scrollbar, _T("no scrollbar to set position for") );
939
940 scrollbar->SetThumbPosition(pos);
7d297414
VZ
941
942 // VZ: I think we can safely ignore this as we always refresh it
943 // automatically whenever the value chanegs
944#if 0
1e6feb95
VZ
945 if ( refresh )
946 Refresh();
7d297414 947#endif
1e6feb95
VZ
948}
949
950int wxWindow::GetScrollPos(int orient) const
951{
952 wxScrollBar *scrollbar = GetScrollbar(orient);
953 return scrollbar ? scrollbar->GetThumbPosition() : 0;
954}
955
956int wxWindow::GetScrollThumb(int orient) const
957{
958 wxScrollBar *scrollbar = GetScrollbar(orient);
959 return scrollbar ? scrollbar->GetThumbSize() : 0;
960}
961
962int wxWindow::GetScrollRange(int orient) const
963{
964 wxScrollBar *scrollbar = GetScrollbar(orient);
965 return scrollbar ? scrollbar->GetRange() : 0;
966}
967
968void wxWindow::ScrollWindow(int dx, int dy, const wxRect *rect)
969{
66a58655
VS
970 // use native scrolling when available and do it in generic way
971 // otherwise:
4125131b
RR
972#ifdef __WXX11__
973
66a58655 974 wxWindowNative::ScrollWindow(dx, dy, rect);
4125131b 975
2b5f62a0 976#else // !wxX11
4125131b 977
1e6feb95
VZ
978 // before scrolling it, ensure that we don't have any unpainted areas
979 Update();
980
981 wxRect r;
982
983 if ( dx )
984 {
985 r = ScrollNoRefresh(dx, 0, rect);
a290fa5a 986 Refresh(true /* erase bkgnd */, &r);
1e6feb95
VZ
987 }
988
989 if ( dy )
990 {
991 r = ScrollNoRefresh(0, dy, rect);
a290fa5a 992 Refresh(true /* erase bkgnd */, &r);
1e6feb95 993 }
2b5f62a0
VZ
994
995 // scroll children accordingly:
66a58655 996 wxPoint offset(dx, dy);
2b5f62a0 997
ac32ba44 998 for (wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
66a58655
VS
999 node; node = node->GetNext())
1000 {
1001 wxWindow *child = node->GetData();
1c9f1f4d
VS
1002 if ( child == m_scrollbarVert || child == m_scrollbarHorz )
1003 continue;
1004
1005 // VS: Scrolling children has non-trivial semantics. If rect=NULL then
2b5f62a0 1006 // it is easy: we scroll all children. Otherwise it gets
1c9f1f4d
VS
1007 // complicated:
1008 // 1. if scrolling in one direction only, scroll only
1009 // those children that intersect shaft defined by the rectangle
1010 // and scrolling direction
1011 // 2. if scrolling in both axes, scroll all children
2b5f62a0 1012
1c9f1f4d
VS
1013 if ( rect && (dx * dy == 0 /* moving in only one of x, y axis */) )
1014 {
1015 wxRect childRect = child->GetRect();
1016 if ( dx == 0 && (childRect.GetLeft() <= rect->GetRight() ||
1017 childRect.GetRight() >= rect->GetLeft()) )
1018 {
1019 child->Move(child->GetPosition() + offset);
1020 }
1021 else if ( dy == 0 && (childRect.GetTop() <= rect->GetBottom() ||
1022 childRect.GetBottom() >= rect->GetTop()) )
1023 {
1024 child->Move(child->GetPosition() + offset);
1025 }
1026 }
1027 else
66a58655
VS
1028 {
1029 child->Move(child->GetPosition() + offset);
1030 }
2b5f62a0
VZ
1031 }
1032#endif // wxX11/!wxX11
1e6feb95
VZ
1033}
1034
1035wxRect wxWindow::ScrollNoRefresh(int dx, int dy, const wxRect *rectTotal)
1036{
1037 wxASSERT_MSG( !dx || !dy, _T("can't be used for diag scrolling") );
1038
1039 // the rect to refresh (which we will calculate)
1040 wxRect rect;
1041
1042 if ( !dx && !dy )
1043 {
1044 // nothing to do
1045 return rect;
1046 }
1047
1048 // calculate the part of the window which we can just redraw in the new
1049 // location
1050 wxSize sizeTotal = rectTotal ? rectTotal->GetSize() : GetClientSize();
1051
1052 wxLogTrace(_T("scroll"), _T("rect is %dx%d, scroll by %d, %d"),
1053 sizeTotal.x, sizeTotal.y, dx, dy);
1054
1055 // the initial and end point of the region we move in client coords
1056 wxPoint ptSource, ptDest;
1057 if ( rectTotal )
1058 {
1059 ptSource = rectTotal->GetPosition();
1060 ptDest = rectTotal->GetPosition();
1061 }
1062
1063 // the size of this region
1064 wxSize size;
1065 size.x = sizeTotal.x - abs(dx);
1066 size.y = sizeTotal.y - abs(dy);
1067 if ( size.x <= 0 || size.y <= 0 )
1068 {
1069 // just redraw everything as nothing of the displayed image will stay
1070 wxLogTrace(_T("scroll"), _T("refreshing everything"));
1071
1072 rect = rectTotal ? *rectTotal : wxRect(0, 0, sizeTotal.x, sizeTotal.y);
1073 }
1074 else // move the part which doesn't change to the new location
1075 {
1076 // note that when we scroll the canvas in some direction we move the
1077 // block which doesn't need to be refreshed in the opposite direction
1078
1079 if ( dx < 0 )
1080 {
1081 // scroll to the right, move to the left
1082 ptSource.x -= dx;
1083 }
1084 else
1085 {
1086 // scroll to the left, move to the right
1087 ptDest.x += dx;
1088 }
1089
1090 if ( dy < 0 )
1091 {
1092 // scroll down, move up
1093 ptSource.y -= dy;
1094 }
1095 else
1096 {
1097 // scroll up, move down
1098 ptDest.y += dy;
1099 }
1100
1101#if wxUSE_CARET
1102 // we need to hide the caret before moving or it will erase itself at
1103 // the wrong (old) location
1104 wxCaret *caret = GetCaret();
1105 if ( caret )
1106 caret->Hide();
1107#endif // wxUSE_CARET
1108
1109 // do move
1110 wxClientDC dc(this);
1111 wxBitmap bmp(size.x, size.y);
1112 wxMemoryDC dcMem;
1113 dcMem.SelectObject(bmp);
1114
c47addef 1115 dcMem.Blit(wxPoint(0,0), size, &dc, ptSource
03147cd0 1116#if defined(__WXGTK__) && !defined(wxHAS_WORKING_GTK_DC_BLIT)
1e6feb95
VZ
1117 + GetClientAreaOrigin()
1118#endif // broken wxGTK wxDC::Blit
1119 );
c47addef 1120 dc.Blit(ptDest, size, &dcMem, wxPoint(0,0));
1e6feb95
VZ
1121
1122 wxLogTrace(_T("scroll"),
1123 _T("Blit: (%d, %d) of size %dx%d -> (%d, %d)"),
1124 ptSource.x, ptSource.y,
1125 size.x, size.y,
1126 ptDest.x, ptDest.y);
1127
1128 // and now repaint the uncovered area
1129
1130 // FIXME: We repaint the intersection of these rectangles twice - is
1131 // it bad? I don't think so as it is rare to scroll the window
1132 // diagonally anyhow and so adding extra logic to compute
1133 // rectangle intersection is probably not worth the effort
1134
1135 rect.x = ptSource.x;
1136 rect.y = ptSource.y;
1137
1138 if ( dx )
1139 {
1140 if ( dx < 0 )
1141 {
1142 // refresh the area along the right border
1143 rect.x += size.x + dx;
1144 rect.width = -dx;
1145 }
1146 else
1147 {
1148 // refresh the area along the left border
1149 rect.width = dx;
1150 }
1151
1152 rect.height = sizeTotal.y;
1153
1154 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1155 rect.x, rect.y,
1156 rect.GetRight() + 1, rect.GetBottom() + 1);
1157 }
1158
1159 if ( dy )
1160 {
1161 if ( dy < 0 )
1162 {
1163 // refresh the area along the bottom border
1164 rect.y += size.y + dy;
1165 rect.height = -dy;
1166 }
1167 else
1168 {
1169 // refresh the area along the top border
1170 rect.height = dy;
1171 }
1172
1173 rect.width = sizeTotal.x;
1174
1175 wxLogTrace(_T("scroll"), _T("refreshing (%d, %d)-(%d, %d)"),
1176 rect.x, rect.y,
1177 rect.GetRight() + 1, rect.GetBottom() + 1);
1178 }
1179
1180#if wxUSE_CARET
1181 if ( caret )
1182 caret->Show();
1183#endif // wxUSE_CARET
1184 }
1185
1186 return rect;
1187}
1188
1e6feb95
VZ
1189// ----------------------------------------------------------------------------
1190// accelerators and menu hot keys
1191// ----------------------------------------------------------------------------
1192
1193#if wxUSE_MENUS
1194 // the last window over which Alt was pressed (used by OnKeyUp)
1195 wxWindow *wxWindow::ms_winLastAltPress = NULL;
1196#endif // wxUSE_MENUS
1197
1198#if wxUSE_ACCEL || wxUSE_MENUS
1199
1200void wxWindow::OnKeyDown(wxKeyEvent& event)
1201{
1202#if wxUSE_MENUS
1203 int key = event.GetKeyCode();
f52c3131 1204 if ( !event.ControlDown() && (key == WXK_ALT || key == WXK_F10) )
1e6feb95
VZ
1205 {
1206 ms_winLastAltPress = this;
1207
1208 // it can't be an accel anyhow
1209 return;
1210 }
1211
1212 ms_winLastAltPress = NULL;
1213#endif // wxUSE_MENUS
1214
1215#if wxUSE_ACCEL
1216 for ( wxWindow *win = this; win; win = win->GetParent() )
1217 {
1218 int command = win->GetAcceleratorTable()->GetCommand(event);
1219 if ( command != -1 )
1220 {
1221 wxCommandEvent eventCmd(wxEVT_COMMAND_MENU_SELECTED, command);
1222 if ( win->GetEventHandler()->ProcessEvent(eventCmd) )
1223 {
1224 // skip "event.Skip()" below
1225 return;
1226 }
1227 }
1228
1229 if ( win->IsTopLevel() )
1230 {
1231 // try the frame menu bar
1232#if wxUSE_MENUS
1233 wxFrame *frame = wxDynamicCast(win, wxFrame);
1234 if ( frame )
1235 {
1236 wxMenuBar *menubar = frame->GetMenuBar();
1237 if ( menubar && menubar->ProcessAccelEvent(event) )
1238 {
1239 // skip "event.Skip()" below
1240 return;
1241 }
1242 }
1243#endif // wxUSE_MENUS
1244
81762e79
VS
1245 // if it wasn't in a menu, try to find a button
1246 if ( command != -1 )
1247 {
1248 wxWindow* child = win->FindWindow(command);
1249 if ( child && wxDynamicCast(child, wxButton) )
1250 {
1251 wxCommandEvent eventCmd(wxEVT_COMMAND_BUTTON_CLICKED, command);
1252 eventCmd.SetEventObject(child);
1253 if ( child->GetEventHandler()->ProcessEvent(eventCmd) )
1254 {
1255 // skip "event.Skip()" below
1256 return;
1257 }
1258 }
1259 }
1260
1e6feb95
VZ
1261 // don't propagate accels from the child frame to the parent one
1262 break;
1263 }
1264 }
1265#endif // wxUSE_ACCEL
1266
1267 event.Skip();
1268}
1269
1270#endif // wxUSE_ACCEL
1271
1272#if wxUSE_MENUS
1273
1274wxMenuBar *wxWindow::GetParentFrameMenuBar() const
1275{
1276 for ( const wxWindow *win = this; win; win = win->GetParent() )
1277 {
1278 if ( win->IsTopLevel() )
1279 {
1280 wxFrame *frame = wxDynamicCast(win, wxFrame);
1281 if ( frame )
1282 {
1283 return frame->GetMenuBar();
1284 }
1285
1286 // don't look further - we don't want to return the menubar of the
1287 // parent frame
1288 break;
1289 }
1290 }
1291
1292 return NULL;
1293}
1294
1295void wxWindow::OnChar(wxKeyEvent& event)
1296{
1297 if ( event.AltDown() && !event.ControlDown() )
1298 {
1299 int key = event.GetKeyCode();
1300
1301 wxMenuBar *menubar = GetParentFrameMenuBar();
1302 if ( menubar )
1303 {
1304 int item = menubar->FindNextItemForAccel(-1, key);
1305 if ( item != -1 )
1306 {
1307 menubar->PopupMenu((size_t)item);
1308
1309 // skip "event.Skip()" below
1310 return;
1311 }
1312 }
1313 }
1314
1315 event.Skip();
1316}
1317
1318void wxWindow::OnKeyUp(wxKeyEvent& event)
1319{
1320 int key = event.GetKeyCode();
f52c3131 1321 if ( !event.HasModifiers() && (key == WXK_ALT || key == WXK_F10) )
1e6feb95
VZ
1322 {
1323 // only process Alt release specially if there were no other key
1324 // presses since Alt had been pressed and if both events happened in
1325 // the same window
1326 if ( ms_winLastAltPress == this )
1327 {
1328 wxMenuBar *menubar = GetParentFrameMenuBar();
1329 if ( menubar && this != menubar )
1330 {
1331 menubar->SelectMenu(0);
1332 }
1333 }
1334 }
1335 else
1336 {
1337 event.Skip();
1338 }
1339
1340 // in any case reset it
1341 ms_winLastAltPress = NULL;
1342}
1343
1344#endif // wxUSE_MENUS
1345
30827629
VZ
1346// ----------------------------------------------------------------------------
1347// MSW-specific section
1348// ----------------------------------------------------------------------------
1349
1350#ifdef __WXMSW__
1351
1352#include "wx/msw/private.h"
1353
c140b7e7 1354WXLRESULT wxWindow::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
30827629
VZ
1355{
1356 if ( message == WM_NCHITTEST )
1357 {
1358 // the windows which contain the other windows should let the mouse
1359 // events through, otherwise a window inside a static box would
1360 // never get any events at all
1361 if ( IsStaticBox() )
1362 {
1363 return HTTRANSPARENT;
1364 }
1365 }
1366
1367 return wxWindowNative::MSWWindowProc(message, wParam, lParam);
1368}
1369
1370#endif // __WXMSW__
1371