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