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