~ubuntu-branches/ubuntu/quantal/zaz/quantal

« back to all changes in this revision

Viewing changes to src/lineeditor.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-11-16 02:28:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111116022810-d3tehh974q280vit
Tags: 1.0.0~dfsg1-1
* New Upstream Release
* Upgraded Standards-Version from 3.8.3 to 3.9.2
* New homepage: http://phuzzboxmedia.com/index.php/games/open-sourced-zaz
* Refreshed patches
* Moved package to DebSrc 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
{
28
28
    if (txt.size() > maxLength)
29
29
        txt.erase(maxLength);
 
30
 
 
31
    SDL_EnableUNICODE(SDL_ENABLE);
 
32
}
 
33
 
 
34
LineEditor::~LineEditor()
 
35
{
 
36
    SDL_EnableUNICODE(SDL_DISABLE);
30
37
}
31
38
 
32
39
void LineEditor::Render()
69
76
 
70
77
}
71
78
 
 
79
uint LineEditor::utf8_strlen(const char *s)
 
80
{
 
81
    int i = 0;
 
82
    int j = 0;
 
83
 
 
84
    while (s[i])
 
85
    {
 
86
        if ((s[i] & 0xC0) != 0x80)
 
87
            j++;
 
88
        i++;
 
89
    }
 
90
    return (j);
 
91
}
 
92
 
72
93
void LineEditor::Logic(FrameEvents events)
73
94
{
74
95
    blinkCountdown--;
81
102
 
82
103
    if (!events.empty)
83
104
    {
 
105
        // suppress enters
 
106
        for (vector<SDLKey>::iterator i = events.keyDown.begin(); i != events.keyDown.end(); ++i)
 
107
        {
 
108
            if (*i == SDLK_RETURN || *i == SDLK_KP_ENTER)
 
109
                return;
 
110
        }
 
111
 
 
112
 
84
113
        // read shift state
85
114
        for (vector<SDLKey>::iterator i = events.keyDown.begin(); i != events.keyDown.end(); ++i)
86
115
        {
95
124
        }
96
125
 
97
126
        char c = 0;
 
127
        bool addchar = true;
98
128
        for (vector<SDLKey>::iterator i = events.keyDown.begin(); i != events.keyDown.end(); ++i)
99
129
        {
100
130
            if (*i == SDLK_BACKSPACE)
101
 
                txt = txt.substr(0, txt.size() - 1);
102
 
 
103
 
                        if (*i >= SDLK_a && *i <= SDLK_z) // add a char
 
131
            {
 
132
                if (txt.size())
 
133
                {
 
134
                    if (txt[txt.size() -1] < 0)
 
135
                        txt = txt.substr(0, txt.size() - 1);
 
136
 
 
137
 
 
138
                    txt = txt.substr(0, txt.size() - 1);
 
139
                }
 
140
                addchar = false;
 
141
            }
 
142
 
 
143
            if (*i >= SDLK_a && *i <= SDLK_z) // add a char
104
144
            {
105
145
                c = (*i - SDLK_a) + (shift?'A':'a');
106
146
            }
114
154
                c = ' ';
115
155
        }
116
156
 
117
 
        if (c != 0)
118
 
        {
119
 
            if (txt.size() < maxLength)
120
 
                txt.append(&c, 1);
 
157
        bool unicode_added = false;
 
158
 
 
159
        if (addchar)
 
160
            for (vector<Uint16>::iterator i = events.unicode.begin(); i != events.unicode.end(); ++i)
 
161
            {
 
162
                char uc[8];
 
163
                wchar_t wc[2];
 
164
 
 
165
                char illegal[] = "/\\:*?\"<>|"; // illegal windows characters for file names
 
166
#ifdef WIN32
 
167
                wc[0] = *i;
 
168
                wc[1] = 0;
 
169
 
 
170
                int rc = WideCharToMultiByte(CP_UTF8, 0, wc, -1, uc, 8, NULL, NULL);
 
171
#else
 
172
                int rc = wctomb(uc, wchar_t(*i));
 
173
 
 
174
                if (rc > 0)
 
175
                    uc[rc] = 0;
 
176
 
 
177
#endif
 
178
 
 
179
                if (rc > 0)
 
180
                {
 
181
                    bool legal = true;
 
182
                    int l = strlen(illegal);
 
183
                    for (int lc = 0; lc < l; lc++)
 
184
                        if (uc[0] == illegal[lc])
 
185
                            legal = false;
 
186
 
 
187
                    if (legal)
 
188
                    {
 
189
                        txt.append(uc);
 
190
                        unicode_added = true;
 
191
                    }
 
192
                }
 
193
            }
 
194
 
 
195
        if (!unicode_added && c != 0)
 
196
        {
 
197
            txt.append(&c, 1);
 
198
        }
 
199
 
 
200
        while (utf8_strlen(txt.c_str()) > maxLength)
 
201
        {
 
202
            if (txt[txt.size() -1] < 0)
 
203
                txt = txt.substr(0, txt.size() - 1);
 
204
 
 
205
            txt = txt.substr(0, txt.size() - 1);
121
206
        }
122
207
 
123
208
        pos = txt.size();