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