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