2
* Copyright (C) 2002-2004 by the Widelands Development Team
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include "font_handler.h"
22
#include "ui_multilineeditbox.h"
23
#include "ui_multilinetextarea.h"
24
#include "ui_scrollbar.h"
25
#include "constants.h"
27
#include "font_handler.h"
29
#include "rendertarget.h"
33
Initialize a edibox that supports multiline strings.
35
UIMultiline_Editbox::UIMultiline_Editbox(UIPanel *parent, int x, int y, uint w, uint h,
37
: UIMultiline_Textarea(parent, x, y, w, h, text, Align_Left, true) {
40
set_scrollmode(ScrollLog);
43
m_cur_pos=get_text().size();
45
set_handle_mouse(true);
52
Free allocated resources
54
UIMultiline_Editbox::~UIMultiline_Editbox() {
59
a key event must be handled
61
bool UIMultiline_Editbox::handle_key(bool down, int code, char c) {
66
std::string m_text=get_text();
69
if(m_text.size() && m_cur_pos) {
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());
85
if(static_cast<int>(m_cur_pos)<0) m_cur_pos=0;
90
if(m_cur_pos>=m_text.size()) m_cur_pos=m_text.size();
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;
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())
109
if(begin_of_next_line+m_cur_pos-begin_of_line > end_of_next_line)
110
m_cur_pos=end_of_next_line;
112
m_cur_pos=begin_of_next_line+m_cur_pos-begin_of_line;
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;
131
m_cur_pos=begin_of_lastline+(m_cur_pos-begin_of_line);
139
if(c && m_text.size()<m_maxchars) {
140
m_text.insert(m_cur_pos,1,c);
143
UIMultiline_Textarea::set_text(m_text.c_str());
147
UIMultiline_Textarea::set_text(m_text.c_str());
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()) {
164
return UIMultiline_Textarea::handle_mouseclick(btn,down,x,y);
170
void UIMultiline_Editbox::draw(RenderTarget* dst)
172
// make the whole area a bit darker
173
dst->brighten_rect(0,0,get_w(),get_h(),ms_darken_value);
175
std::string m_text=get_text();
176
m_text.append(1,' ');
179
// Let the font handler worry about all the complicated stuff..
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);
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());
186
// now draw the textarea
187
UIMultiline_Textarea::draw(dst);
191
* Set text function needs to take care of the current
194
void UIMultiline_Editbox::set_text(const char* str) {
196
m_cur_pos=strlen(str);
200
UIMultiline_Textarea::set_text(str);