Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

inputbox_generic.cpp

Go to the documentation of this file.
00001 /*
00002         ClanGUI, copyrights by various people. Have a look in the CREDITS file.
00003         
00004         This sourcecode is distributed using the Library GNU Public Licence,
00005         version 2 or (at your option) any later version. Please read LICENSE
00006         for details.
00007 */
00008 
00009 #include "precomp.h"
00010 #include "inputbox_generic.h"
00011 #include "API/GUI/component_options.h"
00012 #include "API/Display/Input/input.h"
00013 
00014 CL_ComponentOptions CL_InputBox_Generic::create_options(
00015         const CL_Rect &pos,
00016         const std::string &text,
00017         bool password_mode,
00018         bool readonly,
00019         int maxlength)
00020 {
00021         CL_ComponentOptions options;
00022 
00023         options.add_option_as_int("x", pos.x1);
00024         options.add_option_as_int("y", pos.y1);
00025         options.add_option_as_int("width", pos.get_width());
00026         options.add_option_as_int("height", pos.get_height());
00027         options.add_option_as_bool("passwordmode", password_mode);
00028         options.add_option_as_bool("readonly", readonly);
00029         options.add_option_as_int("maxlength", maxlength);
00030         options.add_option("text", text);
00031 
00032         return options;
00033 }
00034 
00035 CL_InputBox_Generic::CL_InputBox_Generic(CL_InputBox *self, const CL_ComponentOptions &options, CL_StyleManager *style)
00036 :
00037         inputbox(self)
00038 {
00039         ctrl_down = false;
00040         shift_down = false;
00041 
00042         mouse_selecting = false;
00043         edited = false;
00044 
00045         if(options.exists("text"))
00046                 set_text(options.get_value("text"));
00047         else
00048                 set_text("");
00049 
00050         if(options.exists("passwordmode"))
00051                 password_mode = options.get_value_as_bool("passwordmode");
00052         else
00053                 password_mode = false;
00054 
00055         if(options.exists("readonly"))
00056                 read_only = options.get_value_as_bool("readonly");
00057         else
00058                 read_only = false;
00059 
00060         if(options.exists("maxlength"))
00061                 set_max_length(options.get_value_as_int("maxlength"));
00062         else
00063                 set_max_length(0);
00064 
00065         slot_key_down = inputbox->sig_key_down().connect(CL_CreateSlot(this, &CL_InputBox_Generic::on_key_down));
00066         slot_key_up = inputbox->sig_key_up().connect(CL_CreateSlot(this, &CL_InputBox_Generic::on_key_up));
00067         slot_mouse_move = inputbox->sig_mouse_moved().connect(CL_CreateSlot(this, &CL_InputBox_Generic::on_mouse_move));
00068 }
00069 
00070 void CL_InputBox_Generic::on_key_down(CL_Component *comp, CL_InputDevice *device, CL_Key key)
00071 {
00072         // Start selecting with mouse?
00073         if (device == CL_Input::pointers[0])
00074         {
00075                 cursor_position = text.size();
00076                 selecting = false;
00077                 selection_start = -1;
00078                 selection_end = -1;
00079 //              cursor_position = get_mouse_position(key.x, key.y);
00080 /*              mouse_selecting = true;
00081                 selecting = true;
00082                 selection_start = cursor_position;
00083                 inputbox->capture_mouse();
00084 */              return;
00085         }
00086 
00087         // Keyboard?
00088         // TODO: Isn't there a better way to check this ?
00089         if ((CL_Keyboard *)device != CL_Input::keyboards[0]) return;
00090 
00091         switch (key.id)
00092         {
00093         case CL_KEY_DELETE:
00094                 del();
00095                 break;
00096         case CL_KEY_BACKSPACE:
00097                 backspace();
00098                 break;
00099         case CL_KEY_END:
00100                 check_selection();      
00101                 end(selecting);
00102                 break;
00103         case CL_KEY_HOME:
00104                 check_selection();      
00105                 home(selecting);
00106                 break;
00107         case CL_KEY_LEFT:
00108                 check_selection();      
00109 //              if (ctrl_down)
00110 //                      word_left();
00111 //              else
00112                         move_cursor(-1, selecting);
00113                 break;
00114         case CL_KEY_RIGHT:
00115                 check_selection();      
00116 //              if (ctrl_down)
00117 //                      word_right();
00118 //              else
00119                         move_cursor(1, selecting);
00120                 break;
00121         case CL_KEY_ENTER:
00122                 sig_return_pressed(text);
00123 //              inputbox->clear_focus();
00124                 break;
00125         case CL_KEY_LCTRL:
00126         case CL_KEY_RCTRL:
00127                 ctrl_down = true;
00128                 break;
00129         case CL_KEY_LSHIFT:
00130         case CL_KEY_RSHIFT:
00131                 shift_down = true;
00132                 break;
00133         default:
00134                 update_text(key);
00135                 break;
00136         }
00137 }
00138 
00139 void CL_InputBox_Generic::on_key_up(CL_Component *comp, CL_InputDevice *device, CL_Key key)
00140 {
00141         // End mouse-selecting
00142         if (device == CL_Input::pointers[0])
00143         {
00144 /*              cursor_position = get_mouse_position(key.x, key.y);
00145                 if (mouse_selecting && cursor_position == selection_start)
00146                         selecting = false;
00147                 mouse_selecting = false;
00148                 inputbox->release_mouse();
00149 */              return;
00150         }
00151 
00152         switch (key.id)
00153         {
00154         case CL_KEY_LCTRL:
00155         case CL_KEY_RCTRL:
00156                 ctrl_down = false;
00157                 break;
00158         case CL_KEY_LSHIFT:
00159         case CL_KEY_RSHIFT:
00160                 shift_down = false;
00161                 break;
00162         }
00163 }
00164 
00165 void CL_InputBox_Generic::on_mouse_move(CL_Component *comp, CL_InputDevice *device, int x, int y)
00166 {
00167         if (mouse_selecting)
00168                 cursor_position = get_mouse_position(x, y);
00169 }
00170 
00171 void CL_InputBox_Generic::check_selection()
00172 {
00173         if(shift_down)
00174         {
00175                 if(selecting == false)
00176                 {
00177                         selecting = true;
00178                         selection_start = cursor_position;
00179                 }
00180         }
00181         else
00182         {
00183                 selecting = false;
00184                 selection_start = -1;
00185                 selection_end = -1;
00186         }
00187 }
00188 
00189 void CL_InputBox_Generic::set_text(const std::string &new_text)
00190 {
00191         // TODO: Check against max_length
00192 
00193         text = new_text;
00194         selecting = false;
00195         selection_start = -1;
00196         selection_end = -1;
00197         cursor_position = text.size();
00198 }
00199 
00200 const std::string &CL_InputBox_Generic::get_marked_text() const
00201 {
00202         throw CL_Error("CL_InputBox_Generic::get_marked_text() is not implemented");
00203 }
00204 
00205 void CL_InputBox_Generic::set_max_length(int length)
00206 {
00207         max_length = length;
00208 
00209         if(text.size() > max_length && max_length != 0)
00210                 text.erase(max_length, text.size());
00211 }
00212 
00213 void CL_InputBox_Generic::select_all()
00214 {
00215         throw CL_Error("CL_InputBox_Generic::select_all() is not implemented");
00216 }
00217 
00218 void CL_InputBox_Generic::deselect()
00219 {
00220         throw CL_Error("CL_InputBox_Generic::deselect() is not implemented");
00221 }
00222 
00223 void CL_InputBox_Generic::set_selection(int start, int length)
00224 {
00225         throw CL_Error("CL_InputBox_Generic::set_selection() is not implemented");
00226 }
00227 
00228 void CL_InputBox_Generic::set_cursor_position(int pos)
00229 {
00230         cursor_position = pos;
00231         if(cursor_position < 0)
00232                 cursor_position = 0;
00233         if(cursor_position > max_length - 1 && max_length != 0)
00234                 cursor_position = max_length - 1;
00235 }
00236 
00237 void CL_InputBox_Generic::del()
00238 {
00239         if (selecting)
00240                 cut();
00241         else if (cursor_position < (int)text.size())
00242                 text.erase(cursor_position, 1);
00243 }
00244 
00245 void CL_InputBox_Generic::backspace()
00246 {
00247         if (selecting)
00248                 cut();
00249         else if (cursor_position > 0)
00250         {
00251                 text.erase(cursor_position - 1, 1);
00252                 cursor_position--;
00253         }
00254 }
00255 
00256 void CL_InputBox_Generic::home(bool mark)
00257 {
00258         if(mark)
00259         {
00260                 selecting = true;
00261                 selection_start = cursor_position;
00262                 selection_end = 0;
00263         }
00264 
00265         cursor_position = 0;
00266 }
00267 
00268 void CL_InputBox_Generic::end(bool mark)
00269 {
00270         if(mark)
00271         {
00272                 selecting = true;
00273                 selection_start = cursor_position;
00274                 selection_end = text.size();
00275         }
00276 
00277         cursor_position = text.size();
00278 }
00279 
00280 void CL_InputBox_Generic::move_cursor(int delta, bool mark)
00281 {
00282         cursor_position += delta;
00283 
00284         // Check bounds
00285         if (cursor_position < 0) cursor_position = 0;
00286         if (cursor_position > (int)text.size()) cursor_position = (int)text.size();
00287 
00288         if(mark)
00289                 selection_end = cursor_position;
00290 }
00291 
00292 void CL_InputBox_Generic::move_cursor_word(int delta, bool mark)
00293 {
00294         throw CL_Error("CL_InputBox_Generic::move_cursor_word() is not implemented");
00295 /*
00296                 if (ctrl_down)
00297                 {
00298                         while (caret_position > 0 && text[caret_position] == ' ') caret_position--;
00299                         while (caret_position > 0 && text[caret_position] != ' ') caret_position--;
00300                 }
00301                 if (ctrl_down)
00302                 {
00303                         while (caret_position < (int)text.size() && text[caret_position] == ' ') caret_position++;
00304                         while (caret_position < (int)text.size() && text[caret_position] != ' ') caret_position++;
00305                 }
00306 */
00307 }
00308 
00309 void CL_InputBox_Generic::cut()
00310 {
00311         if(selecting) 
00312         {
00313                 int start = get_selection_start();
00314                 int end = get_selection_length();
00315                 text.erase(start, end);
00316                 cursor_position = start;
00317 
00318                 selecting = false;
00319                 selection_start = selection_end = -1;
00320 
00321                 if (cursor_position > (int)text.size())
00322                         cursor_position = (int)text.size();
00323         }
00324 }
00325 
00326 int CL_InputBox_Generic::get_selection_start()
00327 {
00328         if(selection_start < selection_end)
00329                 return selection_start;
00330         else
00331                 return selection_end;
00332 }
00333 
00334 int CL_InputBox_Generic::get_selection_length()
00335 {
00336         return abs(selection_start - selection_end);
00337 }
00338 
00339 void CL_InputBox_Generic::update_text(CL_Key key)
00340 {
00341         if (key.ascii != -1)
00342         {
00343                 cut();
00344                 text.insert(text.begin() + cursor_position, key.ascii);
00345                 cursor_position++;
00346         }
00347 }
00348 
00349 int CL_InputBox_Generic::get_mouse_position(int x, int y)
00350 {
00351 /*      int delta_x = x - X_TEXTOFFSET;
00352         unsigned int i;
00353         for (i = 0; i < text.size(); i++)
00354         {
00355                 char buf[2];
00356                 buf[0] = text[i];
00357                 buf[1] = 0;
00358                 int w = fnt_inputbox->get_text_width(buf);
00359                 delta_x -= w;
00360                 if (delta_x <= -w/2)
00361                 {
00362                                 return i + last_offset;
00363                 }
00364         }
00365         return i;
00366 */
00367         return text.size() - 1;
00368 }

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001