factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / core / GUI / Console.cpp
blobf9e60fa93a2f08bf15caec7d2eb2ef64bdb9eb1d
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "GUI/Console.h"
23 #include "win32def.h"
25 #include "GameData.h"
26 #include "Interface.h"
27 #include "Palette.h"
28 #include "ScriptEngine.h"
29 #include "Video.h"
31 Console::Console(void)
33 Cursor = NULL;
34 Back = NULL;
35 max = 128;
36 Buffer = ( unsigned char * ) malloc( max );
37 Buffer[0] = 0;
38 for(size_t i=0;i<HISTORY_SIZE;i++) {
39 History[i] = ( unsigned char * ) malloc( max );
40 History[i][0] = 0;
42 CurPos = 0;
43 HistPos = 0;
44 HistMax = 0;
45 palette = NULL;
48 Console::~Console(void)
50 free( Buffer );
51 for (size_t i=0;i<HISTORY_SIZE;i++) {
52 free( History[i] );
54 Video *video = core->GetVideoDriver();
56 gamedata->FreePalette( palette );
57 video->FreeSprite( Cursor );
60 /** Draws the Console on the Output Display */
61 void Console::Draw(unsigned short x, unsigned short y)
63 if (Back) {
64 core->GetVideoDriver()->BlitSprite( Back, 0, y, true );
66 Color black = {
67 0x00, 0x00, 0x00, 0xff
69 Region r( x + XPos, y + YPos, Width, Height );
70 core->GetVideoDriver()->DrawRect( r, black );
71 font->Print( r, Buffer, palette,
72 IE_FONT_ALIGN_LEFT | IE_FONT_ALIGN_MIDDLE, true, NULL,
73 Cursor, CurPos, true );
75 /** Set Font */
76 void Console::SetFont(Font* f)
78 if (f != NULL) {
79 font = f;
82 /** Set Cursor */
83 void Console::SetCursor(Sprite2D* cur)
85 if (cur != NULL) {
86 Cursor = cur;
89 /** Set BackGround */
90 void Console::SetBackGround(Sprite2D* back)
92 //if 'back' is NULL then no BackGround will be drawn
93 Back = back;
95 /** Sets the Text of the current control */
96 int Console::SetText(const char* string, int /*pos*/)
98 strncpy( ( char * ) Buffer, string, max );
99 return 0;
101 /** Key Press Event */
102 void Console::OnKeyPress(unsigned char Key, unsigned short /*Mod*/)
104 if (Key >= 0x20) {
105 size_t len = strlen( ( char* ) Buffer );
106 if (len + 1 < max) {
107 for (size_t i = len; i > CurPos; i--) {
108 Buffer[i] = Buffer[i - 1];
110 Buffer[CurPos++] = Key;
111 Buffer[len + 1] = 0;
115 /** Special Key Press */
116 void Console::OnSpecialKeyPress(unsigned char Key)
118 size_t len;
120 switch (Key) {
121 case GEM_BACKSP:
122 if (CurPos != 0) {
123 size_t len = strlen( ( const char * ) Buffer );
124 for (size_t i = CurPos; i < len; i++) {
125 Buffer[i - 1] = Buffer[i];
127 Buffer[len - 1] = 0;
128 CurPos--;
130 break;
131 case GEM_HOME:
132 CurPos = 0;
133 break;
134 case GEM_END:
135 CurPos = (unsigned short) strlen( (const char * ) Buffer);
136 break;
137 case GEM_UP:
138 HistoryBack();
139 break;
140 case GEM_DOWN:
141 HistoryForward();
142 break;
143 case GEM_LEFT:
144 if (CurPos > 0)
145 CurPos--;
146 break;
147 case GEM_RIGHT:
148 len = strlen( ( const char * ) Buffer );
149 if (CurPos < len) {
150 CurPos++;
152 break;
153 case GEM_DELETE:
154 len = strlen( ( const char * ) Buffer );
155 if (CurPos < len) {
156 for (size_t i = CurPos; i < len; i++) {
157 Buffer[i] = Buffer[i + 1];
160 break;
161 case GEM_RETURN:
162 core->GetGUIScriptEngine()->ExecString( ( char* ) Buffer );
163 HistoryAdd(false);
164 Buffer[0] = 0;
165 CurPos = 0;
166 HistPos = 0;
167 Changed = true;
168 break;
172 //ctrl-up
173 void Console::HistoryBack()
175 HistoryAdd(false);
176 if (HistPos < HistMax-1 && Buffer[0]) {
177 HistPos++;
179 memcpy(Buffer, History[HistPos], max);
180 CurPos = (unsigned short) strlen ((const char *) Buffer);
183 //ctrl-down
184 void Console::HistoryForward()
186 HistoryAdd(false);
187 if (HistPos == 0) {
188 Buffer[0]=0;
189 CurPos=0;
190 return;
192 HistPos--;
193 memcpy(Buffer, History[HistPos], max);
194 CurPos = (unsigned short) strlen ((const char *) Buffer);
197 void Console::HistoryAdd(bool force)
199 int i;
201 if (!force && !Buffer[0])
202 return;
203 for (i=0;i<HistMax;i++) {
204 if (!strnicmp((const char *) History[i],(const char *) Buffer,max) )
205 return;
207 if (History[0][0]) {
208 for (i=HISTORY_SIZE-1; i>0; i--) {
209 memcpy(History[i], History[i-1], max);
212 memcpy(History[0], Buffer, max);
213 if (HistMax<HISTORY_SIZE) {
214 HistMax++;
218 bool Console::SetEvent(int /*eventType*/, EventHandler /*handler*/)
220 return false;