| | 1 | /* The FrobTadsWindow class provides an easy to use interface to the |
| | 2 | * curses WINDOW* structure. It's basically a wrapper around the curses |
| | 3 | * routines, tailored somewhat to our own needs. The methods are short |
| | 4 | * one-liners and therefore good candidates for inlining, so using this |
| | 5 | * class implies no overhead. |
| | 6 | */ |
| | 7 | #ifndef TADSWINDOW_H |
| | 8 | #define TADSWINDOW_H |
| | 9 | |
| | 10 | #include "common.h" |
| | 11 | |
| | 12 | #include "frobcurses.h" |
| | 13 | |
| | 14 | class FrobTadsWindow { |
| | 15 | private: |
| | 16 | // The curses window we maintain. |
| | 17 | WINDOW* fWin; |
| | 18 | |
| | 19 | public: |
| | 20 | /* Creates a new top-level window with 'lines' height, 'cols' |
| | 21 | * width, and coordinates 'yPos' and 'xPos'. |
| | 22 | */ |
| | 23 | FrobTadsWindow( int lines, int cols, int yPos, int xPos ) |
| | 24 | : fWin(newwin(lines, cols, yPos, xPos)) |
| | 25 | { } |
| | 26 | |
| | 27 | ~FrobTadsWindow() { delwin(this->fWin); } |
| | 28 | |
| | 29 | /* Returns the height (lines) of the window. |
| | 30 | */ |
| | 31 | int |
| | 32 | height() const |
| | 33 | { int y, x; getmaxyx(this->fWin, y, x); return y; } |
| | 34 | |
| | 35 | /* Returns the width (columns) of the window. |
| | 36 | */ |
| | 37 | int |
| | 38 | width() const |
| | 39 | { int y, x; getmaxyx(this->fWin, y, x); return x; } |
| | 40 | |
| | 41 | /* Moves the cursor to line 'y', column 'x'. |
| | 42 | */ |
| | 43 | int |
| | 44 | moveCursor( int y, int x ) { return wmove(this->fWin, y, x); } |
| | 45 | |
| | 46 | /* Gets a keystroke from the window. A timeout can be set with |
| | 47 | * setTimeout() before calling this method. |
| | 48 | * |
| | 49 | * The returned value is the same as the curses getch() routine. |
| | 50 | */ |
| | 51 | int |
| | 52 | getChar() { return wgetch(this->fWin); } |
| | 53 | |
| | 54 | /* Sets a timeout for subsequent input operations. If 'timeout' |
| | 55 | * milliseconds pass and there's no input, ERR is returned. If |
| | 56 | * 'timeout' is < 1 (zero or negative), the input methods will |
| | 57 | * not use a timeout but wait indefinitely for input. |
| | 58 | */ |
| | 59 | void |
| | 60 | setTimeout( int timeout ) { if (timeout == 0) timeout = -1; wtimeout(this->fWin, timeout); } |
| | 61 | |
| | 62 | /* Writes the string 'str' to the window at the specified |
| | 63 | * position. Note the type of 'str'; it's not a 'char*'. The |
| | 64 | * 'chtype' datatype is used by curses to store a string |
| | 65 | * together with the attributes of its characters. This results |
| | 66 | * in faster output operations. |
| | 67 | */ |
| | 68 | int |
| | 69 | printStr( int y, int x, chtype* str ) { return mvwaddchstr(this->fWin, y, x, str); } |
| | 70 | |
| | 71 | /* Writes the character 'ch' to the window at the specified |
| | 72 | * coordinates. |
| | 73 | */ |
| | 74 | int |
| | 75 | printChar( int y, int x, const chtype ch ) { return mvwaddch(this->fWin, y, x, ch); } |
| | 76 | |
| | 77 | /* Returns the character at position (x,y). |
| | 78 | */ |
| | 79 | chtype |
| | 80 | charAt( int y, int x ) { return mvwinch(this->fWin, y, x); } |
| | 81 | |
| | 82 | /* Blanks the window (erases its contents). |
| | 83 | */ |
| | 84 | int |
| | 85 | blank() { return werase(this->fWin); } |
| | 86 | |
| | 87 | /* Enables/disables scrolling. |
| | 88 | */ |
| | 89 | int |
| | 90 | enableScrolling( bool bf ) { return scrollok(this->fWin, bf); } |
| | 91 | |
| | 92 | /* Flushes the internal buffers so any pending output-operations |
| | 93 | * will be processed. This is just a curses wrefresh(). |
| | 94 | */ |
| | 95 | int |
| | 96 | flush() { return wrefresh(this->fWin); } |
| | 97 | |
| | 98 | /* Mark the entire window as "touched"; throw away all |
| | 99 | * optimization information about which parts of the window have |
| | 100 | * changed, forcing curses to redraw all characters. |
| | 101 | */ |
| | 102 | int |
| | 103 | touch() { return touchwin(this->fWin); } |
| | 104 | |
| | 105 | /* Enables/disables "keypad mode". In this mode, input methods |
| | 106 | * (getChar(), etc.) will recognize special keys like function |
| | 107 | * keys, arrow keys, insert, delete, etc. These are returned as |
| | 108 | * KEY_* values (as defined in <curses.h>). |
| | 109 | */ |
| | 110 | int |
| | 111 | keypadMode( bool bf ) { return keypad(this->fWin, bf); } |
| | 112 | |
| | 113 | /* Enables/disables 8-bit input. Normally, input is 7-bit, |
| | 114 | * which means that things like German unlauts und everything |
| | 115 | * else above the 7-bit ASCII range won't work. |
| | 116 | */ |
| | 117 | int |
| | 118 | input8bit( bool bf ) { return meta(this->fWin, bf); } |
| | 119 | }; |
| | 120 | |
| | 121 | #endif // TADSWINDOW_H |