]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Some new datafile keywords
[libs/gltk.git] / source / entry.cpp
index 65bae7214001e842929aab8618982dfbf11cdc84..12eff06ae4ffbc542e3b80726242b60ea03ee4ee 100644 (file)
-#include <SDL/SDL_keysym.h>
 #include <msp/gl/matrix.h>
+#include <msp/gl/meshbuilder.h>
 #include <msp/gl/texture.h>
-#include <msp/gl/transform.h>
+#include <msp/input/keys.h>
 #include "entry.h"
+#include "graphic.h"
 #include "part.h"
 #include "style.h"
+#include "vslider.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
-Entry::Entry(const Resources &r, const string &t):
-       Widget(r),
-       text(t),
-       edit_pos(0)
+Entry::Entry(const string &t):
+       text(),
+       multiline(false),
+       edit_width(10),
+       edit_height(1),
+       edit_pos(0),
+       first_row(0),
+       visible_rows(1),
+       text_part(0),
+       slider(0),
+       got_key_press(false)
 {
-       update_style();
+       set_text(t);
+}
+
+void Entry::autosize_special(const Part &part, Geometry &ageom)
+{
+       if(part.get_name()=="text")
+       {
+               const Sides &margin = part.get_margin();
+               const GL::Font &font = style->get_font();
+               unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
+               ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
+
+               unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
+               if(multiline)
+               {
+                       unsigned line_spacing = style->get_font_size()*6/5;
+                       ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
+               }
+               else
+                       ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
+       }
+       else if(part.get_name()=="slider" && multiline)
+       {
+               Geometry sgeom = part.get_geometry();
+               if(!sgeom.w || !sgeom.h)
+               {
+                       slider->autosize();
+                       if(!sgeom.w)
+                               sgeom.w = slider->get_geometry().w;
+                       if(!sgeom.h)
+                               sgeom.h = slider->get_geometry().h;
+               }
+
+               const Sides &margin = part.get_margin();
+               ageom.w = max(ageom.w, sgeom.w+margin.left+margin.right);
+               ageom.h = max(ageom.h, sgeom.h+margin.top+margin.bottom);
+       }
 }
 
 void Entry::set_text(const string &t)
 {
-       text=t;
-       if(edit_pos>text.size())
-               edit_pos=text.size();
+       text = t;
+       edit_pos = text.size();
+
+       if(multiline)
+               check_view_range();
+
+       rebuild();
 }
 
-void Entry::key_press(unsigned key, unsigned, wchar_t ch)
+void Entry::set_edit_size(unsigned w, unsigned h)
 {
-       if(key==SDLK_LEFT)
+       edit_width = w;
+       edit_height = h;
+       signal_autosize_changed.emit();
+}
+
+void Entry::set_multiline(bool m)
+{
+       multiline = m;
+       if(multiline)
+       {
+               if(!slider)
+               {
+                       slider = new VSlider;
+                       add(*slider);
+                       slider->set_step(1);
+                       slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
+                       reposition_slider();
+               }
+               check_view_range();
+       }
+}
+
+void Entry::rebuild_special(const Part &part)
+{
+       if(part.get_name()=="text")
+               text.build(part, geom, first_row, part_cache);
+       else if(part.get_name()=="cursor")
+       {
+               const Graphic *graphic = part.get_graphic(state);
+               if(!text_part || !graphic || !graphic->get_texture())
+                       return;
+
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+
+               if(row<first_row || row>=first_row+visible_rows)
+                       return;
+
+               Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
+
+               GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
+               bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+               graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
+       }
+       else
+               Widget::rebuild_special(part);
+}
+
+void Entry::render_special(const Part &part, GL::Renderer &renderer) const
+{
+       if(part.get_name()=="slider" && multiline)
+               slider->render(renderer);
+}
+
+void Entry::key_press(unsigned key, unsigned)
+{
+       got_key_press = true;
+       if(key==Input::KEY_LEFT)
        {
                if(edit_pos>0)
-                       --edit_pos;
+                       set_edit_position(edit_pos-1);
        }
-       else if(key==SDLK_RIGHT)
+       else if(key==Input::KEY_RIGHT)
        {
                if(edit_pos<text.size())
-                       ++edit_pos;
+                       set_edit_position(edit_pos+1);
+       }
+       else if(key==Input::KEY_DOWN && multiline)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(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);
+               set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
        }
-       else if(key==SDLK_BACKSPACE)
+       else if(key==Input::KEY_BACKSPACE)
        {
                if(edit_pos>0)
+               {
                        text.erase(--edit_pos, 1);
+                       check_view_range();
+                       rebuild();
+               }
        }
-       else
+       else if(key==Input::KEY_ENTER)
        {
-               text+=ch;
+               if(multiline)
+               {
+                       text.insert(edit_pos++, "\n");
+                       check_view_range();
+                       rebuild();
+               }
+               else
+                       signal_enter.emit();
        }
 }
 
-void Entry::focus_in()
+void Entry::character(wchar_t ch)
 {
-       if(state!=DISABLED)
-               state=ACTIVE;
+       if(got_key_press && ch>=' ')
+       {
+               text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
+               ++edit_pos;
+               rebuild();
+       }
 }
 
 void Entry::focus_out()
 {
-       if(state==ACTIVE)
-               state=NORMAL;
+       Widget::focus_out();
+       got_key_press = false;
 }
 
-void Entry::render_part(const Part &part) const
+void Entry::on_geometry_change()
 {
-       if(part.get_name()=="text")
-               render_text(part, text);
-       /*{
-               const GL::Font *const font=style->get_font();
+       reposition_slider();
 
-               const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+       if(multiline)
+               check_view_range();
+}
 
-               GL::push_matrix();
+void Entry::on_style_change()
+{
+       text.set_style(style);
 
-               part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
-               GL::scale_uniform(font_size);
+       if(!style)
+       {
+               text_part = 0;
+               return;
+       }
+
+       text_part = style->get_part("text");
+
+       reposition_slider();
 
-               const Color &color=style->get_font_color();
-               glColor3f(color.r, color.g, color.b);
-               if(state==ACTIVE)
+       if(multiline)
+               check_view_range();
+}
+
+void Entry::set_edit_position(unsigned ep)
+{
+       edit_pos = ep;
+       check_view_range();
+       rebuild();
+}
+
+void Entry::reposition_slider()
+{
+       if(!style || !slider)
+               return;
+
+       if(const Part *slider_part = style->get_part("slider"))
+       {
+               Geometry sgeom = slider_part->get_geometry();
+               if(!sgeom.w || !sgeom.h)
                {
-                       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));
+                       slider->autosize();
+                       if(!sgeom.w)
+                               sgeom.w = slider->get_geometry().w;
+                       if(!sgeom.h)
+                               sgeom.h = slider->get_geometry().h;
                }
-               else
-                       font->draw_string(text);
-               glColor3f(1, 1, 1);
 
-               GL::pop_matrix();
-               render_text(part, text);
-       }*/
-       /*else if(part.get_name()=="cursor")
-       {
-               unsigned gw=part.get_width();
-               unsigned gh=(part.get_fill_y() ? geom.h : part.get_height());
-
-               const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
-
-               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::pop_matrix();
-       }*/
-       else
-               Widget::render_part(part);
+               slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
+               slider->set_geometry(sgeom);
+       }
+}
+
+void Entry::check_view_range()
+{
+       if(!multiline || !text_part)
+               return;
+
+       float font_size = style->get_font_size();
+       unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
+
+       const Sides &margin = text_part->get_margin();
+       visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
+
+       unsigned row, col;
+       text.offset_to_coords(edit_pos, row, col);
+
+       if(first_row>row)
+               first_row = row;
+       else if(row>=first_row+visible_rows)
+               first_row = row+1-visible_rows;
+
+       unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
+       slider->set_range(0, scroll);
+       slider->set_value(scroll-first_row);
+}
+
+void Entry::slider_value_changed(double value)
+{
+       if(text.get_n_lines()>visible_rows)
+               first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
+}
+
+
+Entry::Loader::Loader(Entry &e):
+       DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
+{
+       add("edit_size", &Entry::edit_width, &Entry::edit_height);
+       add("multiline", &Loader::multiline);
+}
+
+void Entry::Loader::multiline(bool m)
+{
+       obj.set_multiline(m);
 }
 
 } // namespace GLtk