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