00001 /***************************************************************************** 00002 * Copyright (C) 2005 Eliot 00003 * Authors: Olivier Teuliere <ipkiss@via.ecp.fr> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 *****************************************************************************/ 00019 00020 #ifndef _NCURSES_H_ 00021 #define _NCURSES_H_ 00022 00023 #include <curses.h> 00024 #include <string> 00025 00026 class Game; 00027 class Training; 00028 class Duplicate; 00029 class FreeGame; 00030 00031 using std::string; 00032 00033 00034 /** 00035 * This class implements the ncurses interface. 00036 */ 00037 class CursesIntf 00038 { 00039 public: 00040 // Pre-requisite: the given Game object MUST have been allocated with new 00041 // (in particular: not on the stack) 00042 // This class also takes the responsability of destroying the Game object. 00043 CursesIntf(WINDOW *win, Game& iGame); 00044 ~CursesIntf(); 00045 bool isDying() const { return m_dying; } 00046 int handleKey(int iKey); 00047 void redraw(WINDOW *win); 00048 00049 private: 00050 enum State 00051 { 00052 DEFAULT, // Default state 00053 HELP, // Help panel is shown 00054 HISTORY, // Game history panel is shown 00055 RESULTS // Search results panel is shown 00056 }; 00057 // Draw a titled box with the specified position and size 00058 static void drawBox(WINDOW *win, int y, int x, int h, int w, 00059 const string& iTitle); 00060 // Clear a rectangular zone 00061 static void clearRect(WINDOW *win, int y, int x, int h, int w); 00062 // Print a line in a box, taking care of the current offset 00063 void boxPrint(WINDOW *win, int y, int x, const char *fmt, ...); 00064 // Write a message in the "status line" 00065 void drawStatus(WINDOW *win, int y, int x, 00066 const string& iMessage, bool error = true); 00067 // Draw the board, with the coordinates 00068 void drawBoard(WINDOW *win, int y, int x) const; 00069 // Draw the boxes for scores and racks 00070 void drawScoresRacks(WINDOW *win, int y, int x) const; 00071 // Draw the results panel 00072 void drawResults(WINDOW *win, int y, int x); 00073 // Draw the history panel 00074 void drawHistory(WINDOW *win, int y, int x); 00075 // Draw the help panel 00076 void drawHelp(WINDOW *win, int y, int x); 00077 // Draw the "Play word" box, and handle the played word 00078 void playWord(WINDOW *win, int y, int x); 00079 void checkWord(WINDOW *win, int y, int x); 00080 void saveGame(WINDOW *win, int y, int x); 00081 void loadGame(WINDOW *win, int y, int x); 00082 void passTurn(WINDOW *win, int y, int x, FreeGame &iGame); 00083 void setRack(WINDOW *win, int y, int x, Training &iGame); 00084 // Get a string from the user, with a maximum length 00085 // The string is validated if the user presses Enter (return value: true) 00086 // and it is cancelled if the user presses Esc (return value: false) 00087 bool readString(WINDOW *win, int y, int x, int n, string &oString, 00088 unsigned int flag = 0); 00089 // Any combination of the following constants can be used as the "flag" 00090 // parameter of the readString() method. 00091 // Indicate that the '?' character is accepted 00092 static const unsigned int kJOKER = 1 << 0; 00093 // Accept characters for a file name 00094 static const unsigned int kFILENAME = 1 << 0; 00095 00096 // Handle the key in Training mode 00097 int handleKeyForGame(int iKey, Training &iGame); 00098 // Handle the key in Duplicate mode 00099 int handleKeyForGame(int iKey, Duplicate &iGame); 00100 // Handle the key in FreeGame mode 00101 int handleKeyForGame(int iKey, FreeGame &iGame); 00102 00103 // Main window for drawing 00104 WINDOW *m_win; 00105 // Current game 00106 // Invariant: the pointer will always point to a valid Game object 00107 Game *m_game; 00108 // Interface state 00109 State m_state; 00110 // True when the user requested to quit 00111 bool m_dying; 00112 // Index of the first line of data to be displayed in the current box 00113 int m_boxStart; 00114 // Number of lines of the current box (border excluded) 00115 int m_boxLines; 00116 // Number of lines of the data to be displayed in the current box 00117 int m_boxLinesData; 00118 // Index of the first line of the box where to write 00119 int m_boxY; 00120 // True if dots must be shown on empty squares 00121 bool m_showDots; 00122 }; 00123 00124 #endif 00125