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