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