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