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