X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fentry.cpp;h=9f99e794c5e3c7cb98fc0564242af21aac79f172;hb=5152e147e9a053482f6a4895f3d2028864f6cf2f;hp=f21aacbd07beff04c64aae4be124bdfab4e138fd;hpb=b61361ee9f1f049fb3c22a38f68c757c7ca54cd0;p=libs%2Fgltk.git diff --git a/source/entry.cpp b/source/entry.cpp index f21aacb..9f99e79 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -23,12 +23,14 @@ Entry::Entry(const string &t): visible_rows(1), text_part(0), slider(0), - got_key_press(false) + got_key_press(false), + cursor_blink(true) { + input_type = INPUT_TEXT; set_text(t); } -void Entry::autosize_special(const Part &part, Geometry &ageom) +void Entry::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="text") { @@ -47,21 +49,7 @@ void Entry::autosize_special(const Part &part, Geometry &ageom) 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); - } + autosize_child(*slider, part, ageom); } void Entry::set_text(const string &t) @@ -93,7 +81,7 @@ void Entry::set_multiline(bool m) add(*slider); slider->set_step(1); slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed)); - reposition_slider(); + rebuild(); } check_view_range(); } @@ -105,7 +93,8 @@ void Entry::rebuild_special(const Part &part) text.build(part, state, geom, first_row, part_cache); else if(part.get_name()=="cursor") { - const Graphic *graphic = part.get_graphic(state); + State cursor_state = (cursor_blink ? ACTIVE : NORMAL); + const Graphic *graphic = part.get_graphic(state|cursor_state); if(!text_part || !graphic || !graphic->get_texture()) return; @@ -121,6 +110,14 @@ void Entry::rebuild_special(const Part &part) bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0); graphic->build(part.get_geometry().w, part.get_geometry().h, bld); } + else if(part.get_name()=="slider") + { + if(multiline) + { + reposition_child(*slider, part); + Widget::rebuild_special(part); + } + } else Widget::rebuild_special(part); } @@ -131,73 +128,116 @@ void Entry::render_special(const Part &part, GL::Renderer &renderer) const slider->render(renderer); } -void Entry::key_press(unsigned key, unsigned) +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) { got_key_press = true; - if(key==Input::KEY_LEFT) + if(key==Input::KEY_BACKSPACE) { if(edit_pos>0) - set_edit_position(edit_pos-1); + { + text.erase(--edit_pos, 1); + check_view_range(); + rebuild(); + } } - else if(key==Input::KEY_RIGHT) + else if(key==Input::KEY_ENTER && multiline) { - if(edit_pos0 ? text.coords_to_offset(row-1, col) : 0); - } - else if(key==Input::KEY_BACKSPACE) - { - if(edit_pos>0) - { - text.erase(--edit_pos, 1); - check_view_range(); - rebuild(); - } - } - else if(key==Input::KEY_ENTER) - { - if(multiline) - { - text.insert(edit_pos++, "\n"); - check_view_range(); - rebuild(); - } - else - signal_enter.emit(); + set_edit_position(text.coords_to_offset(row, 0)); } + else + return false; + + return true; } -void Entry::character(wchar_t ch) +bool Entry::character(wchar_t ch) { if(got_key_press && ch>=' ') { text.insert(edit_pos, StringCodec::encode(StringCodec::ustring(1, ch))); ++edit_pos; rebuild(); + 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(); } -void Entry::on_geometry_change() +bool Entry::navigate(Navigation nav) +{ + if(nav==NAV_LEFT) + { + if(edit_pos>0) + set_edit_position(edit_pos-1); + } + else if(nav==NAV_RIGHT) + { + if(edit_pos0 ? text.coords_to_offset(row-1, col) : 0); + } + else if(nav==NAV_ACCEPT && !signal_enter.empty()) + signal_enter.emit(); + else + return false; + + return true; +} + +void Entry::animate(const Time::TimeDelta &) { - reposition_slider(); + cursor_blink = !cursor_blink; + rebuild(); +} +void Entry::on_geometry_change() +{ if(multiline) check_view_range(); } @@ -214,10 +254,10 @@ void Entry::on_style_change() text_part = style->get_part("text"); - reposition_slider(); - if(multiline) check_view_range(); + + check_cursor_blink(); } void Entry::set_edit_position(unsigned ep) @@ -227,26 +267,20 @@ void Entry::set_edit_position(unsigned ep) rebuild(); } -void Entry::reposition_slider() +void Entry::check_cursor_blink() { - if(!style || !slider) - return; - - if(const Part *slider_part = style->get_part("slider")) + cursor_blink = (state&FOCUS); + if((state&FOCUS) && style) { - Geometry sgeom = slider_part->get_geometry(); - if(!sgeom.w || !sgeom.h) + const Part *cursor_part = style->get_part("cursor"); + if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS)) { - slider->autosize(); - if(!sgeom.w) - sgeom.w = slider->get_geometry().w; - if(!sgeom.h) - sgeom.h = slider->get_geometry().h; + set_animation_interval(Time::sec/2); + return; } - - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider->set_geometry(sgeom); } + + stop_animation(); } void Entry::check_view_range()