1/*
2 * Copyright (C) 2011 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.browser.view;
18
19import com.android.browser.R;
20
21import android.content.Context;
22import android.graphics.Canvas;
23import android.view.View;
24
25/**
26 * shows views in a stack
27 */
28public class PieStackView extends BasePieView {
29
30    private static final int SLOP = 5;
31
32    private OnCurrentListener mCurrentListener;
33    private int mMinHeight;
34
35    public interface OnCurrentListener {
36        public void onSetCurrent(int index);
37    }
38
39    public PieStackView(Context ctx) {
40        mMinHeight = (int) ctx.getResources()
41                .getDimension(R.dimen.qc_tab_title_height);
42    }
43
44    public void setOnCurrentListener(OnCurrentListener l) {
45        mCurrentListener = l;
46    }
47
48    @Override
49    public void setCurrent(int ix) {
50        super.setCurrent(ix);
51        if (mCurrentListener != null) {
52            mCurrentListener.onSetCurrent(ix);
53        }
54    }
55
56    /**
57     * this will be called before the first draw call
58     */
59    @Override
60    public void layout(int anchorX, int anchorY, boolean left, float angle,
61            int pHeight) {
62        super.layout(anchorX, anchorY, left, angle, pHeight);
63        buildViews();
64        mWidth = mChildWidth;
65        mHeight = mChildHeight + (mViews.size() - 1) * mMinHeight;
66        mLeft = anchorX + (left ? SLOP : -(SLOP + mChildWidth));
67        mTop = anchorY - mHeight / 2;
68        if (mViews != null) {
69            layoutChildrenLinear();
70        }
71    }
72
73    private void layoutChildrenLinear() {
74        final int n = mViews.size();
75        int top = mTop;
76        int dy = (n == 1) ? 0 : (mHeight - mChildHeight) / (n - 1);
77        for (View view : mViews) {
78            int x = mLeft;
79            view.layout(x, top, x + mChildWidth, top + mChildHeight);
80            top += dy;
81        }
82    }
83
84    @Override
85    public void draw(Canvas canvas) {
86        if ((mViews != null) && (mCurrent > -1)) {
87            final int n = mViews.size();
88            for (int i = 0; i < mCurrent; i++) {
89                drawView(mViews.get(i), canvas);
90            }
91            for (int i = n - 1; i > mCurrent; i--) {
92                drawView(mViews.get(i), canvas);
93            }
94            drawView(mViews.get(mCurrent), canvas);
95        }
96    }
97
98    @Override
99    protected int findChildAt(int y) {
100        final int ix = (y - mTop) * mViews.size() / mHeight;
101        return ix;
102    }
103
104}
105