summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/FbTk/TextBox.cc35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/FbTk/TextBox.cc b/src/FbTk/TextBox.cc
index 34c0689..fb1d484 100644
--- a/src/FbTk/TextBox.cc
+++ b/src/FbTk/TextBox.cc
@@ -19,7 +19,7 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: TextBox.cc,v 1.13 2004/09/11 22:58:20 fluxgen Exp $ 22// $Id: TextBox.cc,v 1.14 2004/10/06 19:05:12 akir Exp $
23 23
24#include "TextBox.hh" 24#include "TextBox.hh"
25#include "Font.hh" 25#include "Font.hh"
@@ -90,10 +90,13 @@ void TextBox::setInputFocus() {
90 90
91void TextBox::cursorHome() { 91void TextBox::cursorHome() {
92 m_start_pos = m_cursor_pos = 0; 92 m_start_pos = m_cursor_pos = 0;
93 adjustEndPos();
93} 94}
94 95
95void TextBox::cursorEnd() { 96void TextBox::cursorEnd() {
96 m_cursor_pos = m_end_pos = text().size(); 97 m_end_pos = text().size();
98 adjustStartPos();
99 m_cursor_pos = m_end_pos - m_start_pos;
97} 100}
98 101
99void TextBox::cursorForward() { 102void TextBox::cursorForward() {
@@ -137,6 +140,7 @@ void TextBox::insertText(const std::string &val) {
137 m_text.insert(m_start_pos + cursorPosition(), val); 140 m_text.insert(m_start_pos + cursorPosition(), val);
138 m_cursor_pos += val.size(); 141 m_cursor_pos += val.size();
139 m_end_pos += val.size(); 142 m_end_pos += val.size();
143
140 if (m_start_pos + cursorPosition() < m_end_pos) 144 if (m_start_pos + cursorPosition() < m_end_pos)
141 adjustEndPos(); 145 adjustEndPos();
142 else 146 else
@@ -185,6 +189,22 @@ void TextBox::exposeEvent(XExposeEvent &event) {
185 189
186void TextBox::buttonPressEvent(XButtonEvent &event) { 190void TextBox::buttonPressEvent(XButtonEvent &event) {
187 setInputFocus(); 191 setInputFocus();
192 if (event.window == window()) {
193 std::string::size_type i;
194 std::string::size_type click_pos = m_end_pos;
195 int delta = width();
196 int tmp = 0;
197 for(i = m_start_pos; i <= m_end_pos; i++) {
198 tmp = abs(event.x - font().textWidth(m_text.c_str() + m_start_pos, i - m_start_pos));
199
200 if (tmp < delta) {
201 delta = tmp;
202 click_pos = i;
203 }
204 }
205 m_cursor_pos = click_pos - m_start_pos;
206 clear();
207 }
188} 208}
189 209
190void TextBox::keyPressEvent(XKeyEvent &event) { 210void TextBox::keyPressEvent(XKeyEvent &event) {
@@ -222,8 +242,9 @@ void TextBox::keyPressEvent(XKeyEvent &event) {
222 case XK_c: 242 case XK_c:
223 cursorHome(); 243 cursorHome();
224 m_text = ""; 244 m_text = "";
225 adjustStartPos(); 245 m_start_pos = 0;
226 adjustEndPos(); 246 m_cursor_pos = 0;
247 m_end_pos = 0;
227 break; 248 break;
228 } 249 }
229 } else if (event.state == ShiftMask || event.state == 0x2000) { 250 } else if (event.state == ShiftMask || event.state == 0x2000) {
@@ -282,19 +303,17 @@ void TextBox::adjustEndPos() {
282} 303}
283 304
284void TextBox::adjustStartPos() { 305void TextBox::adjustStartPos() {
285 // reset global start po 306
286 m_start_pos = 0;
287
288 int text_width = font().textWidth(text().c_str(), m_end_pos); 307 int text_width = font().textWidth(text().c_str(), m_end_pos);
289 if (text_width < static_cast<signed>(width())) 308 if (text_width < static_cast<signed>(width()))
290 return; 309 return;
291 310
292 int start_pos = 0; 311 int start_pos = 0;
293 text_width = font().textWidth(text().c_str() + start_pos, m_end_pos - start_pos);
294 while (text_width > static_cast<signed>(width())) { 312 while (text_width > static_cast<signed>(width())) {
295 start_pos++; 313 start_pos++;
296 text_width = font().textWidth(text().c_str() + start_pos, m_end_pos - start_pos); 314 text_width = font().textWidth(text().c_str() + start_pos, m_end_pos - start_pos);
297 } 315 }
316
298 // adjust cursorPosition() according relative to change to m_start_pos 317 // adjust cursorPosition() according relative to change to m_start_pos
299 m_cursor_pos -= start_pos - m_start_pos; 318 m_cursor_pos -= start_pos - m_start_pos;
300 m_start_pos = start_pos; 319 m_start_pos = start_pos;