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