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