~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/ui/ui_basic/ui_multilineeditbox.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by the Widelands Development Team
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "font_handler.h"
 
21
#include "types.h"
 
22
#include "ui_multilineeditbox.h"
 
23
#include "ui_multilinetextarea.h"
 
24
#include "ui_scrollbar.h"
 
25
#include "constants.h"
 
26
#include "system.h"
 
27
#include "font_handler.h"
 
28
#include "keycodes.h"
 
29
#include "rendertarget.h"
 
30
#include "error.h"
 
31
 
 
32
/**
 
33
Initialize a edibox that supports multiline strings.
 
34
*/
 
35
UIMultiline_Editbox::UIMultiline_Editbox(UIPanel *parent, int x, int y, uint w, uint h,
 
36
                                       const char *text)
 
37
   : UIMultiline_Textarea(parent, x, y, w, h, text, Align_Left, true) {
 
38
   m_maxchars=0xffff;
 
39
 
 
40
   set_scrollmode(ScrollLog);
 
41
 
 
42
   m_needs_update=false;
 
43
   m_cur_pos=get_text().size();
 
44
 
 
45
   set_handle_mouse(true);
 
46
   set_can_focus(true);
 
47
        set_think(false);
 
48
}
 
49
 
 
50
 
 
51
/**
 
52
Free allocated resources
 
53
*/
 
54
UIMultiline_Editbox::~UIMultiline_Editbox() {
 
55
   changed.call();
 
56
}
 
57
 
 
58
/**
 
59
a key event must be handled
 
60
*/
 
61
bool UIMultiline_Editbox::handle_key(bool down, int code, char c) {
 
62
 
 
63
   m_needs_update=true;
 
64
 
 
65
   if(down) {
 
66
      std::string m_text=get_text();
 
67
      switch(code) {
 
68
         case KEY_BACKSPACE:
 
69
            if(m_text.size() && m_cur_pos) {
 
70
               m_cur_pos--;
 
71
            } else {
 
72
               break;
 
73
            }
 
74
            // Fallthrough
 
75
 
 
76
         case KEY_DELETE:
 
77
            if(m_text.size() && m_cur_pos<m_text.size()) {
 
78
               m_text.erase(m_text.begin() + m_cur_pos);
 
79
               UIMultiline_Textarea::set_text(m_text.c_str());
 
80
            }
 
81
            break;
 
82
 
 
83
         case KEY_LEFT:
 
84
            m_cur_pos-=1;
 
85
            if(static_cast<int>(m_cur_pos)<0) m_cur_pos=0;
 
86
            break;
 
87
 
 
88
         case KEY_RIGHT:
 
89
            m_cur_pos+=1;
 
90
            if(m_cur_pos>=m_text.size()) m_cur_pos=m_text.size();
 
91
            break;
 
92
 
 
93
         case KEY_DOWN:
 
94
            if(m_cur_pos<m_text.size()-1) {
 
95
               uint begin_of_line=m_cur_pos;
 
96
               if(m_text[begin_of_line]=='\n') --begin_of_line;
 
97
               while(begin_of_line>0 && m_text[begin_of_line]!='\n') --begin_of_line;
 
98
               if(begin_of_line!=0) ++begin_of_line;
 
99
               uint begin_of_next_line=m_cur_pos;
 
100
               while(m_text[begin_of_next_line]!='\n' && begin_of_next_line<m_text.size())
 
101
                  ++begin_of_next_line;
 
102
               if(begin_of_next_line==m_text.size())
 
103
                  --begin_of_next_line;
 
104
                else
 
105
                  ++begin_of_next_line;
 
106
               uint end_of_next_line=begin_of_next_line;
 
107
               while(m_text[end_of_next_line]!='\n' && end_of_next_line<m_text.size())
 
108
                  ++end_of_next_line;
 
109
               if(begin_of_next_line+m_cur_pos-begin_of_line > end_of_next_line)
 
110
                  m_cur_pos=end_of_next_line;
 
111
               else
 
112
                  m_cur_pos=begin_of_next_line+m_cur_pos-begin_of_line;
 
113
            }
 
114
            break;
 
115
 
 
116
         case KEY_UP:
 
117
            if(m_cur_pos>0) {
 
118
               uint begin_of_line=m_cur_pos;
 
119
               if(m_text[begin_of_line]=='\n') --begin_of_line;
 
120
               while(begin_of_line>0 && m_text[begin_of_line]!='\n') --begin_of_line;
 
121
               if(begin_of_line!=0) ++begin_of_line;
 
122
               uint end_of_last_line=begin_of_line;
 
123
               if(begin_of_line!=0) --end_of_last_line;
 
124
               uint begin_of_lastline=end_of_last_line;
 
125
               if(m_text[begin_of_lastline]=='\n') --begin_of_lastline;
 
126
               while(begin_of_lastline>0 && m_text[begin_of_lastline]!='\n') --begin_of_lastline;
 
127
               if(begin_of_lastline!=0) ++begin_of_lastline;
 
128
               if(begin_of_lastline+(m_cur_pos-begin_of_line) > end_of_last_line)
 
129
                  m_cur_pos=end_of_last_line;
 
130
               else
 
131
                  m_cur_pos=begin_of_lastline+(m_cur_pos-begin_of_line);
 
132
            }
 
133
            break;
 
134
 
 
135
         case KEY_RETURN:
 
136
            c='\n';
 
137
            // fallthrough
 
138
         default:
 
139
            if(c && m_text.size()<m_maxchars) {
 
140
               m_text.insert(m_cur_pos,1,c);
 
141
               m_cur_pos++;
 
142
            }
 
143
            UIMultiline_Textarea::set_text(m_text.c_str());
 
144
            break;
 
145
      }
 
146
 
 
147
      UIMultiline_Textarea::set_text(m_text.c_str());
 
148
      changed.call();
 
149
      return true;
 
150
   }
 
151
 
 
152
   return false;
 
153
}
 
154
 
 
155
/*
 
156
 * handle mousclicks
 
157
 */
 
158
bool UIMultiline_Editbox::handle_mouseclick(uint btn, bool down, int x, int y) {
 
159
   if(!down) return false;
 
160
   if(btn==MOUSE_LEFT && !has_focus()) {
 
161
      focus();
 
162
      return true;
 
163
   }
 
164
   return UIMultiline_Textarea::handle_mouseclick(btn,down,x,y);
 
165
}
 
166
 
 
167
/**
 
168
Redraw the Editbox
 
169
*/
 
170
void UIMultiline_Editbox::draw(RenderTarget* dst)
 
171
{
 
172
   // make the whole area a bit darker
 
173
   dst->brighten_rect(0,0,get_w(),get_h(),ms_darken_value);
 
174
 
 
175
   std::string m_text=get_text();
 
176
   m_text.append(1,' ');
 
177
   if (m_text.size() )
 
178
   {
 
179
      // Let the font handler worry about all the complicated stuff..
 
180
      if(has_focus())
 
181
         g_fh->draw_string(dst, get_font_name(), get_font_size(), get_font_clr(), RGBColor(0,0,0), 0, 0 - get_m_textpos(), m_text.c_str(), Align_Left, get_eff_w(),m_cur_pos,-ms_darken_value*4);
 
182
      else
 
183
         g_fh->draw_string(dst, get_font_name(), get_font_size(), get_font_clr(), RGBColor(0,0,0), 0, 0 - get_m_textpos(), m_text.c_str(), Align_Left, get_eff_w());
 
184
   }
 
185
 
 
186
   // now draw the textarea
 
187
   UIMultiline_Textarea::draw(dst);
 
188
}
 
189
 
 
190
/*
 
191
 * Set text function needs to take care of the current
 
192
 * position
 
193
 */
 
194
void UIMultiline_Editbox::set_text(const char* str) {
 
195
   if(strlen(str))
 
196
      m_cur_pos=strlen(str);
 
197
   else
 
198
      m_cur_pos=0;
 
199
 
 
200
   UIMultiline_Textarea::set_text(str);
 
201
 
 
202
}
 
203