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