X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fentry.cpp;h=91bd9c0ce82072219bdd46efa48e2d40e39392dc;hb=0af3c2393bd00f39db3bfaf5b78a7a44f0fd5ff1;hp=65bae7214001e842929aab8618982dfbf11cdc84;hpb=131ac8ff2c06f94d40f4bf98d4a6ec0d113cdffc;p=libs%2Fgltk.git diff --git a/source/entry.cpp b/source/entry.cpp index 65bae72..91bd9c0 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -1,8 +1,16 @@ -#include +/* $Id$ + +This file is part of libmspgltk +Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + #include #include #include +#include #include "entry.h" +#include "graphic.h" #include "part.h" #include "style.h" @@ -13,106 +21,102 @@ namespace GLtk { Entry::Entry(const Resources &r, const string &t): Widget(r), - text(t), + text(), + multiline(false), edit_pos(0) { update_style(); + set_text(t); } void Entry::set_text(const string &t) { - text=t; - if(edit_pos>text.size()) - edit_pos=text.size(); + text = 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==SDLK_LEFT) + if(key==Input::KEY_LEFT) { if(edit_pos>0) --edit_pos; } - else if(key==SDLK_RIGHT) + else if(key==Input::KEY_RIGHT) { if(edit_pos0) + 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 + else if(key==Input::KEY_ENTER) { - text+=ch; + if(multiline) + text.insert(edit_pos++, "\n"); + else + signal_enter.emit(); + } + else if(ch>=' ') + { + text.insert(edit_pos, Codecs::encode(Codecs::ustring(1, ch))); + ++edit_pos; } } -void Entry::focus_in() -{ - if(state!=DISABLED) - state=ACTIVE; -} - -void Entry::focus_out() -{ - if(state==ACTIVE) - state=NORMAL; -} - -void Entry::render_part(const Part &part) const +void Entry::render_special(const Part &part) const { if(part.get_name()=="text") - render_text(part, text); - /*{ - const GL::Font *const font=style->get_font(); - - const float font_size=font->get_default_size(); - unsigned text_w=static_cast(font->get_string_width(text)*font_size); - - GL::push_matrix(); - - part.get_alignment().apply(geom, text_w, static_cast(font->get_ascent()*font_size)); - GL::scale_uniform(font_size); - - const Color &color=style->get_font_color(); - glColor3f(color.r, color.g, color.b); - if(state==ACTIVE) - { - cout<<"foo\n"; - font->draw_string(text.substr(0, edit_pos)); - GL::Texture::unbind(); - glBegin(GL_LINES); - glVertex2f(0, 0); - glVertex2f(0, 1); - glEnd(); - font->draw_string(text.substr(edit_pos)); - } - else - font->draw_string(text); - glColor3f(1, 1, 1); - - GL::pop_matrix(); - render_text(part, text); - }*/ - /*else if(part.get_name()=="cursor") + text.render(part, geom); + else if(part.get_name()=="cursor") { - unsigned gw=part.get_width(); - unsigned gh=(part.get_fill_y() ? geom.h : part.get_height()); + if(!part.get_graphic(state)) + return; + + unsigned row, col; + text.offset_to_coords(edit_pos, row, col); - const float font_size=font->get_default_size(); - unsigned text_w=static_cast(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((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0); - const Graphic *graphic=part.get_graphic(state); - graphic->render(gw, gh); + GL::translate(rgeom.x, rgeom.y, 0); + part.get_graphic(state)->render(part.get_geometry().w, part.get_geometry().h); GL::pop_matrix(); - }*/ - else - Widget::render_part(part); + } +} + +void Entry::on_style_change() +{ + text.set_style(style); } + +Entry::Loader::Loader(Entry &ent): + Widget::Loader(ent) +{ } + } // namespace GLtk } // namespace Msp