]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Support multiline text editing
[libs/gltk.git] / source / entry.cpp
index 7f50ed038fbdd6cc4a758adcc0973d84f550b726..7d5ecb66986d4ef9580745ca3c5594c942cd0182 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -16,17 +16,17 @@ Distributed under the LGPL
 
 using namespace std;
 
-#include <iostream>
-
 namespace Msp {
 namespace GLtk {
 
 Entry::Entry(const Resources &r, const string &t):
        Widget(r),
-       text(t),
-       edit_pos(text.size())
+       text(),
+       multiline(false),
+       edit_pos(0)
 {
        update_style();
+       set_text(t);
 }
 
 void Entry::set_text(const string &t)
@@ -35,6 +35,11 @@ void Entry::set_text(const string &t)
        edit_pos=text.size();
 }
 
+void Entry::set_multiline(bool m)
+{
+       multiline = m;
+}
+
 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 {
        if(key==Input::KEY_LEFT)
@@ -47,11 +52,33 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
                if(edit_pos<text.size())
                        ++edit_pos;
        }
+       else if(key==Input::KEY_DOWN && multiline)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               edit_pos=text.coords_to_offset(row+1, col);
+       }
+       else if(key==Input::KEY_UP && multiline)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               if(row>0)
+                       edit_pos=text.coords_to_offset(row-1, col);
+               else
+                       edit_pos=0;
+       }
        else if(key==Input::KEY_BACKSPACE)
        {
                if(edit_pos>0)
                        text.erase(--edit_pos, 1);
        }
+       else if(key==Input::KEY_ENTER)
+       {
+               if(multiline)
+                       text.insert(edit_pos++, "\n");
+               else
+                       signal_enter.emit();
+       }
        else if(ch>=' ')
        {
                text.insert(edit_pos, Codecs::encode<Codecs::Utf8>(Codecs::ustring(1, ch)));
@@ -62,27 +89,30 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 void Entry::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
-               render_text(part, text);
+               text.render(part, geom);
        else if(part.get_name()=="cursor")
        {
                if(!part.get_graphic(state))
                        return;
 
-               const GL::Font *const font=style->get_font();
-               const float font_size=font->get_default_size();
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
 
-               Geometry rgeom=part.get_geometry();
-               rgeom.x=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
-               rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+               Geometry rgeom=text.coords_to_geometry(row, col);
                part.get_alignment().apply(rgeom, geom, part.get_margin());
 
                GL::push_matrix();
                GL::translate(rgeom.x, rgeom.y, 0);
-               part.get_graphic(state)->render(part.get_geometry().w, rgeom.h);
+               part.get_graphic(state)->render(part.get_geometry().w, part.get_geometry().h);
                GL::pop_matrix();
        }
 }
 
+void Entry::on_style_change()
+{
+       text.set_style(style);
+}
+
 
 Entry::Loader::Loader(Entry &ent):
        Widget::Loader(ent)