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