X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fentry.cpp;h=b2c78004b233b6ad9243e781639a6756a417387e;hb=HEAD;hp=efb4b45c57d354ca4f11b453ff567fe58d108421;hpb=9b29612d1cde85fee9b3f011e86a5cabe5dbcce3;p=libs%2Fgltk.git diff --git a/source/entry.cpp b/source/entry.cpp index efb4b45..b2c7800 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -1,92 +1,521 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include +#include #include -#include #include #include "entry.h" #include "graphic.h" #include "part.h" +#include "slider.h" #include "style.h" using namespace std; -#include - namespace Msp { namespace GLtk { -Entry::Entry(const Resources &r, const string &t): - Widget(r), - text(t), - edit_pos(text.size()) +Entry::Entry(const string &t) { - update_style(); + input_type = INPUT_TEXT; + set_text(t); +} + +void Entry::autosize_special(const Part &part, Geometry &ageom) const +{ + if(part.get_name()=="text") + { + const Sides &margin = part.get_margin(); + const GL::Font &font = style->get_font(); + unsigned en_width = static_cast(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((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) + autosize_child(*slider, part, ageom); } void Entry::set_text(const string &t) { - text=t; - edit_pos=text.size(); + if(t!=text.get()) + { + text = t; + signal_text_changed.emit(text.get()); + } + set_edit_position(text.size()); +} + +void Entry::insert(size_t pos, const string &t) +{ + if(t.empty()) + return; + + text.insert(pos, t); + signal_text_changed.emit(text.get()); + + adjust_edit_position_for_change(pos, t.size()); + if(multiline) + check_view_range(); + + mark_rebuild(); } -void Entry::key_press(unsigned key, unsigned, wchar_t ch) +void Entry::erase(size_t pos, size_t len) { - if(key==Input::KEY_LEFT) + if(!len) + return; + + text.erase(pos, len); + signal_text_changed.emit(text.get()); + + adjust_edit_position_for_change(pos, -len); + if(multiline) + check_view_range(); + + mark_rebuild(); +} + +bool Entry::get_selection(size_t &start, size_t &end) const +{ + if(!selection_active) + return false; + + start = selection_pos; + end = edit_pos; + if(start>end) + swap(start, end); + + return true; +} + +void Entry::translate_position(size_t pos, size_t &row, size_t &col) const +{ + text.offset_to_coords(pos, row, col); +} + +size_t Entry::translate_position(size_t row, size_t col) const +{ + return text.coords_to_offset(row, col); +} + +void Entry::set_edit_size(unsigned w, unsigned h) +{ + edit_width = w; + edit_height = h; + signal_autosize_changed.emit(); +} + +void Entry::set_multiline(bool m) +{ + multiline = m; + if(multiline) { - if(edit_pos>0) - --edit_pos; + if(!slider) + { + unique_ptr s = make_unique(); + slider = s.get(); + add(move(s)); + slider->set_step(1); + slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed)); + mark_rebuild(); + } + check_view_range(); } - else if(key==Input::KEY_RIGHT) +} + +void Entry::rebuild_special(const Part &part) +{ + if(part.get_name()=="text") + text.build(part, state, geom, first_row, part_cache); + else if(part.get_name()=="cursor") { - if(edit_posget_texture()) + return; + + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + + if(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.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0)); + graphic->build(part.get_geometry().w, part.get_geometry().h, bld); } - else if(key==Input::KEY_BACKSPACE) + else if(part.get_name()=="selection") { - if(edit_pos>0) - text.erase(--edit_pos, 1); + if(!selection_active) + return; + + const Graphic *graphic = part.get_graphic(state); + if(!text_part || !graphic || !graphic->get_texture()) + return; + + size_t start, end; + get_selection(start, end); + + size_t row, col; + text.offset_to_coords(start, row, col); + size_t end_row, end_col; + text.offset_to_coords(end, end_row, end_col); + + if(end_row=first_row+visible_rows) + return; + + if(row=first_row+visible_rows) + { + end_row = first_row+visible_rows-1; + end_col = text.get_line_length(end_row); + } + + while(row<=end_row) + { + size_t ec = (row==end_row ? end_col : text.get_line_length(row)); + if(ec>col) + { + Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col); + Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec); + + GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture())); + bld.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0)); + graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld); + } + + ++row; + col = 0; + } + } + else if(part.get_name()=="slider") + { + if(multiline) + { + reposition_child(*slider, part); + Widget::rebuild_special(part); + } } 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::touch_press(int x, int y, unsigned finger) +{ + if(finger==0) + set_focus(); + Widget::touch_press(x, y, finger); +} + +bool Entry::key_press(unsigned key, unsigned mod) +{ + got_key_press = true; + if(key==Input::KEY_BACKSPACE) { - text.insert(edit_pos, Codecs::encode(Codecs::ustring(1, ch))); - ++edit_pos; + if(selection_active) + erase_selection(true); + else if(edit_pos>0) + { + size_t start_pos = text.move_offset(edit_pos, -1); + erase(start_pos, edit_pos-start_pos); + } } + else if(key==Input::KEY_DELETE) + { + if(selection_active) + erase_selection(true); + else + { + size_t end_pos = text.move_offset(edit_pos, 1); + erase(edit_pos, end_pos-edit_pos); + } + } + else if(key==Input::KEY_ENTER && multiline) + insert(edit_pos, "\n"); + else if(key==Input::KEY_END) + { + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT); + } + else if(key==Input::KEY_HOME) + { + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT); + } + else if(key==Input::KEY_PGUP) + { + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + set_edit_position(text.coords_to_offset((row=' ' && ch!=0x7F) { - if(!part.get_graphic(state)) - return; + if(selection_active) + erase_selection(false); + insert(edit_pos, StringCodec::encode(StringCodec::ustring(1, ch))); + return true; + } + + return false; +} + +void Entry::focus_in() +{ + cursor_blink = true; + Widget::focus_in(); + check_cursor_blink(); +} + +void Entry::focus_out() +{ + Widget::focus_out(); + got_key_press = false; + check_cursor_blink(); +} - const GL::Font *const font=style->get_font(); - const float font_size=font->get_default_size(); +bool Entry::navigate(Navigation nav) +{ + if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline)) + move_edit_position(nav, false); + else + return false; + + return true; +} - Geometry rgeom=part.get_geometry(); - rgeom.x=static_cast(font->get_string_width(text.substr(0, edit_pos))*font_size); - rgeom.w=static_cast(font->get_string_width(text)*font_size); - part.get_alignment().apply(rgeom, geom, part.get_margin()); +void Entry::animate(const Time::TimeDelta &) +{ + cursor_blink = !cursor_blink; + mark_rebuild(); +} + +void Entry::on_size_change() +{ + if(multiline) + check_view_range(); +} + +void Entry::on_style_change() +{ + text.set_style(style); - GL::push_matrix(); - GL::translate(rgeom.x, rgeom.y, 0); - part.get_graphic(state)->render(part.get_geometry().w, rgeom.h); - GL::pop_matrix(); + if(!style) + { + text_part = nullptr; + return; } + + text_part = style->find_part("text"); + + if(multiline) + check_view_range(); + + check_cursor_blink(); } +void Entry::move_edit_position(Navigation nav, bool select) +{ + if(nav==NAV_LEFT) + set_edit_position(text.move_offset(edit_pos, -1), select); + else if(nav==NAV_RIGHT) + set_edit_position(text.move_offset(edit_pos, 1), select); + else if(nav==NAV_DOWN) + { + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + set_edit_position(text.coords_to_offset(row+1, col), select); + } + else if(nav==NAV_UP) + { + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select); + } + else + throw invalid_argument("Entry::move_edit_position"); +} -Entry::Loader::Loader(Entry &ent): - Widget::Loader(ent) -{ } +void Entry::adjust_edit_position_for_change(size_t pos, ptrdiff_t change) +{ + size_t old_edit_pos = edit_pos; + size_t old_select_pos = selection_pos; + + if(change>0) + { + if(edit_pos>=pos) + edit_pos += change; + if(selection_active && selection_pos>=pos) + selection_pos += change; + } + else if(change<0) + { + if(edit_pos>=pos) + edit_pos -= min(edit_pos-pos, -change); + if(selection_active && selection_pos>=pos) + selection_pos -= min(selection_pos-pos, -change); + } + + if(edit_pos!=old_edit_pos) + signal_edit_position_changed.emit(edit_pos); + if(selection_active && (edit_pos!=old_edit_pos || selection_pos!=old_select_pos)) + { + size_t start, end; + if(get_selection(start, end)) + signal_selection_changed.emit(start, end); + } +} + +void Entry::set_edit_position(size_t ep, bool select) +{ + bool selection_was_active = selection_active; + if(select && !selection_active) + selection_pos = edit_pos; + selection_active = select; + + size_t old_edit_pos = edit_pos; + edit_pos = min(ep, text.size()); + + if(edit_pos!=old_edit_pos) + { + signal_edit_position_changed.emit(edit_pos); + size_t start, end; + if(get_selection(start, end)) + signal_selection_changed.emit(start, end); + else if(selection_was_active) + signal_selection_changed.emit(edit_pos, edit_pos); + } + + if(multiline) + check_view_range(); + mark_rebuild(); +} + +void Entry::erase_selection(bool emit_change) +{ + size_t start, end; + if(!get_selection(start, end)) + return; + + text.erase(start, end-start); + if(emit_change) + signal_text_changed.emit(text.get()); + set_edit_position(start, false); +} + +void Entry::check_cursor_blink() +{ + const Part *cursor_part = style->find_part("cursor"); + bool has_blink = (cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS)); + + cursor_blink = (state&FOCUS); + if((state&FOCUS) && style && has_blink) + { + set_animation_interval(Time::sec/2); + mark_rebuild(); + } + else + { + if(has_blink) + mark_rebuild(); + stop_animation(); + } +} + +void Entry::check_view_range() +{ + if(!multiline || !text_part) + return; + + visible_rows = text.get_visible_lines(*text_part, geom, nullptr); + + size_t row, col; + text.offset_to_coords(edit_pos, row, col); + + size_t old_first_row = first_row; + if(first_row>row) + first_row = row; + else if(row>=first_row+visible_rows) + first_row = row+1-visible_rows; + + size_t scroll = max(text.get_n_lines(), visible_rows)-visible_rows; + if(first_row>scroll) + first_row = scroll; + + if(first_row!=old_first_row) + signal_scroll_position_changed.emit(first_row); + + slider->set_range(0, scroll); + slider->set_page_size(visible_rows); + slider->set_value(scroll-first_row); +} + +void Entry::slider_value_changed(double value) +{ + if(text.get_n_lines()>visible_rows) + { + size_t old_first_row = first_row; + first_row = text.get_n_lines()-visible_rows-static_cast(value); + if(first_row!=old_first_row) + signal_scroll_position_changed.emit(first_row); + mark_rebuild(); + } +} + + +Entry::Loader::Loader(Entry &e): + DataFile::DerivedObjectLoader(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 } // namespace Msp