Annotation of nono/lib/textscreen.cpp, revision 1.1.1.2

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2018 [email protected]
                      4: //
                      5: 
                      6: #include "header.h"
                      7: #include "textscreen.h"
                      8: 
                      9: //
                     10: // テキストスクリーン
                     11: //
                     12: 
                     13: // コンストラクタ
                     14: TextScreen::TextScreen()
                     15: {
                     16: }
                     17: 
                     18: // コンストラクタ
                     19: TextScreen::TextScreen(int arg_col, int arg_row)
                     20: {
                     21:        Init(arg_col, arg_row);
                     22: }
                     23: 
                     24: // デストラクタ
                     25: TextScreen::~TextScreen()
                     26: {
                     27: }
                     28: 
                     29: // 初期化
                     30: void
                     31: TextScreen::Init(int arg_col, int arg_row)
                     32: {
                     33:        col = arg_col;
                     34:        row = arg_row;
                     35: 
1.1.1.2 ! root       36:        textbuf.reset(new uint16 [col * row]);
1.1       root       37:        X = 0;
                     38:        Y = 0;
                     39: 
                     40:        Clear();
                     41: }
                     42: 
                     43: // クリア
                     44: void
                     45: TextScreen::Clear()
                     46: {
                     47:        for (int i = 0; i < col * row; i++) {
                     48:                textbuf[i] = 0x0020;
                     49:        }
1.1.1.2 ! root       50: 
        !            51:        // 折り返しモードならクリアでホームポジションへ
        !            52:        if (foldmode) {
        !            53:                Locate(0, 0);
        !            54:        }
        !            55: }
        !            56: 
        !            57: // 現在位置を移動
        !            58: void
        !            59: TextScreen::Locate(int x, int y)
        !            60: {
        !            61:        X = x;
        !            62:        Y = y;
1.1       root       63: }
                     64: 
                     65: // 現在のカーソル位置に1文字出力する。
                     66: // カーソルは一つ移動する。
1.1.1.2 ! root       67: // foldmode = false なら、移動後右もしくは下にはみ出しても何もしない。
        !            68: // foldmode = true なら、右にはみ出した場合は次行左端に移動、
        !            69: // 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。
1.1       root       70: void
                     71: TextScreen::Putc(uint16 ch)
                     72: {
                     73:        if (X >= col || Y >= row) {
                     74:                return;
                     75:        }
                     76: 
1.1.1.2 ! root       77:        if (ch == '\n') {
        !            78:                CRLF();
        !            79:        } else {
        !            80:                textbuf[(Y * col) + X] = ch;
        !            81:                X++;
        !            82: 
        !            83:                // 折り返しモード
        !            84:                if (foldmode && X >= col) {
        !            85:                        CRLF();
        !            86:                }
        !            87:        }
1.1       root       88: }
                     89: 
                     90: // テキストスクリーンに文字列を出力する。
                     91: // 右もしくは下にはみ出した場合は無視する。
                     92: // XXX どっかで拾えてもいいような
                     93: void
                     94: TextScreen::Puts(const char *str)
                     95: {
                     96:        while (*str != '\0') {
                     97:                Putc(*str++);
                     98:        }
                     99: }
                    100: 
                    101: // テキストスクリーンに文字列を出力する。
                    102: // 右もしくは下にはみ出した場合は無視する。
                    103: void
                    104: TextScreen::Puts(uint attr, const char *str)
                    105: {
                    106:        while (*str != '\0') {
                    107:                Putc(attr | *str++);
                    108:        }
                    109: }
                    110: 
1.1.1.2 ! root      111: // テキストスクリーンの現在位置から書式付き文字列を出力する。
        !           112: void
        !           113: TextScreen::Print(const char *fmt, ...)
        !           114: {
        !           115:        char buf[1024];
        !           116:        va_list ap;
        !           117: 
        !           118:        va_start(ap, fmt);
        !           119:        vsnprintf(buf, sizeof(buf), fmt, ap);
        !           120:        va_end(ap);
        !           121: 
        !           122:        Puts(buf);
        !           123: }
        !           124: 
1.1       root      125: // テキストスクリーンに座標を指定して書式付き文字列を出力する。
                    126: void
                    127: TextScreen::Print(int x, int y, const char *fmt, ...)
                    128: {
                    129:        char buf[1024];
                    130:        va_list ap;
                    131: 
                    132:        va_start(ap, fmt);
                    133:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    134:        va_end(ap);
                    135: 
1.1.1.2 ! root      136:        Locate(x, y);
1.1       root      137:        Puts(buf);
                    138: }
                    139: 
                    140: // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。
                    141: void
                    142: TextScreen::Print(int x, int y, uint attr, const char *fmt, ...)
                    143: {
                    144:        char buf[1024];
                    145:        va_list ap;
                    146: 
                    147:        va_start(ap, fmt);
                    148:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    149:        va_end(ap);
                    150: 
1.1.1.2 ! root      151:        Locate(x, y);
1.1       root      152:        Puts(attr, buf);
                    153: }
1.1.1.2 ! root      154: 
        !           155: // 改行
        !           156: void
        !           157: TextScreen::CRLF()
        !           158: {
        !           159:        X = 0;
        !           160:        Y++;
        !           161: 
        !           162:        if (Y >= row) {
        !           163:                // スクロール
        !           164:                memmove(&textbuf[0], &textbuf[col],
        !           165:                        (col - 1) * row * sizeof(textbuf[0]));
        !           166:                // カーソル位置は最下行
        !           167:                Y = row - 1;
        !           168:        }
        !           169: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.