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