00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include <SDL.h>
00024
#include "guitypedefs.h"
00025
#include "guicolors.h"
00026
#ifdef _WIN32
00027
#pragma warning(push)
00028
00029
#include <yvals.h>
00030
00031
#pragma warning(disable: 4251)
00032
#pragma warning(disable: 4786) // identifier was truncated to 'number' characters in the debug information
00033
00034
00035
00036
00037
00038
#include <sge.h>
00039
00040
#pragma warning(pop)
00041
#else
00042
#include <sge.h>
00043
#endif
00044
00045
00046
#include "TTFont.h"
00047
#include "SDLControl.h"
00048
#include "Surface.h"
00049
#include "Static.h"
00050
#include "Edit.h"
00051
#include "Xml.h"
00052
00053
using util::Xml;
00054
00055
00056
namespace gui
00057 {
00058
00059
00060 Edit::Edit(
Surface *s,
coord_t x,coord_t y,coord_t w,coord_t h,
SurfaceHelper *pclHelper)
00061 :
Surface(s,x,y,w,h,pclHelper)
00062 ,m_line("")
00063 ,m_echo(0)
00064 ,m_bFocus(false)
00065 {
00066 SetBgColor(
EDIT_DEFAULT_BG_R,
EDIT_DEFAULT_BG_G,
EDIT_DEFAULT_BG_B);
00067 SetFgColor(
EDIT_DEFAULT_FG_R,
EDIT_DEFAULT_FG_G,
EDIT_DEFAULT_FG_B);
00068 SetFrameWidth(
EDIT_DEFAULT_FW);
00069 SetFrameColor(
EDIT_DEFAULT_FC_R,
EDIT_DEFAULT_FC_G,
EDIT_DEFAULT_FC_B);
00070 }
00071
00072
00073 Edit::~Edit()
00074 {
00075 }
00076
00077
00078 void Edit::SetEcho(
char c)
00079 {
00080
m_echo = c;
00081 }
00082
00083
00084 void Edit::Draw()
00085 {
00086 SDL_Rect *area =
GetClientRectPtr();
00087 Uint32 color;
00088
TTFont *font =
GetTTFont();
00089 SDL_Color fg;
00090 SDL_Color bg;
00091
00092 color =
GetFgColor();
00093 memmove(&fg,&color,4);
00094 color =
GetBgColor();
00095 memmove(&bg,&color,4);
00096
00097 SDL_FillRect(m_screen, area, color );
00098
if (font && font -> GetFont())
00099 {
00100
coord_t h = font ->
GetH();
00101
coord_t y =
GetH() / 2 + h / 2;
00102
00103
char *s =
new char[
m_line.size() + 2];
00104
if (
m_echo)
00105 {
00106
int i;
00107
for (i = 0; i < (
int)
m_line.size(); i++)
00108 s[i] =
m_echo;
00109 s[i] = 0;
00110 }
00111
else
00112 {
00113 strcpy(s,
m_line.c_str());
00114 }
00115
if (
m_bFocus)
00116 {
00117 strcat(s,
"_");
00118 }
00119 sge_tt_textoutf(m_screen, font -> GetFont(),
00120 10,y,
00121 fg.r,fg.g,fg.b,
00122 bg.r,bg.g,bg.b,
00123 SDL_ALPHA_OPAQUE,
00124
"%s",s);
00125
delete s;
00126 }
00127 }
00128
00129
00130 void Edit::OnKeyDown(SDL_keysym keysym)
00131 {
00132
char c = (
char)keysym.sym;
00133
00134
if (isprint(c))
00135 {
00136
m_line += c;
00137 SetDirty(
true);
00138 }
00139
else
00140
if (keysym.sym == SDLK_BACKSPACE || keysym.sym == SDLK_DELETE)
00141 {
00142
if (
m_line.size())
00143 {
00144
m_line.erase(
m_line.end() - 1);
00145 SetDirty(
true);
00146 }
00147 }
00148
else
00149
if (keysym.sym == SDLK_RETURN)
00150 {
00151
Surface *pclParent =
GetParent();
00152
if (pclParent)
00153 {
00154
surface_event_t e;
00155 e.
type =
GUI_EVENT_EDIT;
00156 e.
id =
GetID();
00157 e.
local_id =
GetLocalID();
00158 e.
ptr =
this;
00159 pclParent -> OnEvent(&e);
00160 }
00161 }
00162
else
00163 {
00164
return;
00165 }
00166
00167
00168 }
00169
00170
00171 void Edit::SetLine(
const string &str)
00172 {
00173
m_line = str;
00174 SetDirty(
true);
00175
00176 }
00177
00178
00179 const char *Edit::GetLine()
00180 {
00181
return m_line.c_str();
00182 }
00183
00184
00185 void Edit::OnFocus()
00186 {
00187
m_bFocus =
true;
00188 SetDirty(
true);
00189 }
00190
00191
00192 void Edit::OnLostFocus()
00193 {
00194
m_bFocus =
false;
00195 SetDirty(
true);
00196 }
00197
00198
00199 void Edit::OnHide()
00200 {
00201
if (
m_bFocus)
00202 {
00203
00204
00205 SDL_Event e;
00206
00207 e.type = SDL_USEREVENT;
00208 e.user.code =
SDL_USEREVENT_LOSTFOCUS;
00209 e.user.data1 =
this;
00210 e.user.data2 = NULL;
00211 SDL_PushEvent(&e);
00212 }
00213 }
00214
00215
00216 void Edit::DumpMembers(FILE *fil)
00217 {
00218
Xml xml(fil);
00219
00220 Surface::DumpMembers(fil);
00221
00222 xml.
Write(
"LINE",
m_line);
00223 xml.
Write(
"ECHO",
m_echo);
00224 }
00225
00226
00227 }
00228
00229
00230