1/*
2 * Copyright (C) 2009 Kevin Ollivier  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include "config.h"
28
29#include "scrollbar_render.h"
30
31#include <wx/defs.h>
32#include <wx/dc.h>
33#include <wx/dcgraph.h>
34#include <wx/renderer.h>
35
36#include <Carbon/Carbon.h>
37
38static int wxScrollbarPartToHIPressedState(wxScrollbarPart part)
39{
40    switch (part) {
41        case wxSCROLLPART_BACKBTNSTART:
42            return kThemeTopOutsideArrowPressed;
43        case wxSCROLLPART_BACKBTNEND:
44            return kThemeTopOutsideArrowPressed; // This does not make much sense.  For some reason the outside constant is required.
45        case wxSCROLLPART_FWDBTNSTART:
46            return kThemeTopInsideArrowPressed;
47        case wxSCROLLPART_FWDBTNEND:
48            return kThemeBottomOutsideArrowPressed;
49        case wxSCROLLPART_THUMB:
50            return kThemeThumbPressed;
51        default:
52            return 0;
53    }
54}
55
56void wxRenderer_DrawScrollbar(wxWindow* WXUNUSED(window), wxDC& dc,
57                             const wxRect& rect, wxOrientation orient, int current,
58                             wxScrollbarPart focusPart, wxScrollbarPart hoverPart, int max, int step, int flags)
59{
60    const wxCoord x = rect.x;
61    const wxCoord y = rect.y;
62    const wxCoord w = rect.width;
63    const wxCoord h = rect.height;
64
65    dc.SetBrush( *wxWHITE_BRUSH );
66    dc.SetPen( *wxTRANSPARENT_PEN );
67    dc.DrawRectangle(rect);
68
69    dc.SetBrush( *wxTRANSPARENT_BRUSH );
70
71    HIRect hiRect = CGRectMake( x, y, w, h );
72
73    CGContextRef cgContext = NULL;
74    wxGraphicsContext* gc = NULL;
75#if wxCHECK_VERSION(2,9,0)
76    wxGCDCImpl *impl = dynamic_cast<wxGCDCImpl*> (dc.GetImpl());
77    if (impl)
78        gc = impl->GetGraphicsContext();
79#else
80    gc = dc.GetGraphicsContext();
81#endif
82    if (gc)
83        cgContext = (CGContextRef) gc->GetNativeContext();
84
85    if (cgContext)
86    {
87        HIThemeTrackDrawInfo trackInfo;
88        trackInfo.version = 0;
89        trackInfo.kind = kThemeMediumScrollBar;
90        trackInfo.bounds = hiRect;
91        trackInfo.min = 0;
92        trackInfo.max = max;
93        trackInfo.value = current;
94        trackInfo.trackInfo.scrollbar.viewsize = step;
95        trackInfo.attributes = 0;
96        if (orient == wxHORIZONTAL)
97            trackInfo.attributes |= kThemeTrackHorizontal;
98        trackInfo.enableState = (flags & wxCONTROL_FOCUSED) ? kThemeTrackActive : kThemeTrackInactive;
99        trackInfo.trackInfo.scrollbar.pressState = wxScrollbarPartToHIPressedState(focusPart);
100        trackInfo.attributes |= kThemeTrackShowThumb;
101
102        if (flags & wxCONTROL_DISABLED)
103            trackInfo.enableState = kThemeTrackDisabled;
104
105        HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
106    }
107}
108