1/*
2 * Copyright (C) 2013 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.terminal;
18
19import static com.android.terminal.Terminal.TAG;
20
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Color;
24import android.util.Log;
25import android.view.View;
26
27import com.android.terminal.TerminalView.TerminalMetrics;
28
29/**
30 * Rendered contents of a single line of a {@link Terminal} session.
31 */
32public class TerminalLineView extends View {
33    public int pos;
34    public int row;
35    public int cols;
36
37    private final Terminal mTerm;
38    private final TerminalMetrics mMetrics;
39
40    public TerminalLineView(Context context, Terminal term, TerminalMetrics metrics) {
41        super(context);
42        mTerm = term;
43        mMetrics = metrics;
44    }
45
46    @Override
47    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
48        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
49                getDefaultSize(mMetrics.charHeight, heightMeasureSpec));
50    }
51
52    @Override
53    protected void onDraw(Canvas canvas) {
54        super.onDraw(canvas);
55
56        if (mTerm == null) {
57            Log.w(TAG, "onDraw() without a terminal");
58            canvas.drawColor(Color.MAGENTA);
59            return;
60        }
61
62        final TerminalMetrics m = mMetrics;
63
64        for (int col = 0; col < cols;) {
65            mTerm.getCellRun(row, col, m.run);
66
67            m.bgPaint.setColor(m.run.bg);
68            m.textPaint.setColor(m.run.fg);
69
70            final int x = col * m.charWidth;
71            final int xEnd = x + (m.run.colSize * m.charWidth);
72
73            canvas.save();
74            canvas.translate(x, 0);
75            canvas.clipRect(0, 0, m.run.colSize * m.charWidth, m.charHeight);
76
77            canvas.drawPaint(m.bgPaint);
78            canvas.drawPosText(m.run.data, 0, m.run.dataSize, m.pos, m.textPaint);
79
80            canvas.restore();
81
82            col += m.run.colSize;
83        }
84
85        if (mTerm.getCursorVisible() && mTerm.getCursorRow() == row) {
86            canvas.save();
87            canvas.translate(mTerm.getCursorCol() * m.charWidth, 0);
88            canvas.drawRect(0, 0, m.charWidth, m.charHeight, m.cursorPaint);
89            canvas.restore();
90        }
91
92    }
93}
94