1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.ui;
18
19import android.content.Context;
20import android.graphics.Rect;
21import android.util.TypedValue;
22
23import com.android.gallery3d.glrenderer.GLCanvas;
24import com.android.gallery3d.glrenderer.NinePatchTexture;
25
26public class ScrollBarView extends GLView {
27    @SuppressWarnings("unused")
28    private static final String TAG = "ScrollBarView";
29
30    private int mBarHeight;
31
32    private int mGripHeight;
33    private int mGripPosition;  // left side of the grip
34    private int mGripWidth;     // zero if the grip is disabled
35    private int mGivenGripWidth;
36
37    private int mContentPosition;
38    private int mContentTotal;
39
40    private NinePatchTexture mScrollBarTexture;
41
42    public ScrollBarView(Context context, int gripHeight, int gripWidth) {
43        TypedValue outValue = new TypedValue();
44        context.getTheme().resolveAttribute(
45                android.R.attr.scrollbarThumbHorizontal, outValue, true);
46        mScrollBarTexture = new NinePatchTexture(
47                context, outValue.resourceId);
48        mGripPosition = 0;
49        mGripWidth = 0;
50        mGivenGripWidth = gripWidth;
51        mGripHeight = gripHeight;
52    }
53
54    @Override
55    protected void onLayout(
56            boolean changed, int left, int top, int right, int bottom) {
57        if (!changed) return;
58        mBarHeight = bottom - top;
59    }
60
61    // The content position is between 0 to "total". The current position is
62    // in "position".
63    public void setContentPosition(int position, int total) {
64        if (position == mContentPosition && total == mContentTotal) {
65            return;
66        }
67
68        invalidate();
69
70        mContentPosition = position;
71        mContentTotal = total;
72
73        // If the grip cannot move, don't draw it.
74        if (mContentTotal <= 0) {
75            mGripPosition = 0;
76            mGripWidth = 0;
77            return;
78        }
79
80        // Map from the content range to scroll bar range.
81        //
82        // mContentTotal --> getWidth() - mGripWidth
83        // mContentPosition --> mGripPosition
84        mGripWidth = mGivenGripWidth;
85        float r = (getWidth() - mGripWidth) / (float) mContentTotal;
86        mGripPosition = Math.round(r * mContentPosition);
87    }
88
89    @Override
90    protected void render(GLCanvas canvas) {
91        super.render(canvas);
92        if (mGripWidth == 0) return;
93        Rect b = bounds();
94        int y = (mBarHeight - mGripHeight) / 2;
95        mScrollBarTexture.draw(canvas, mGripPosition, y, mGripWidth, mGripHeight);
96    }
97}
98