void spreadsheet_cell_text::handle_event(event::ptr e) { switch (e->type()) { case event::key_up: { key_event::ptr ke = boost::dynamic_pointer_cast<key_event>(e); switch ( ke->fn_key() ) { case key_backspace: INFO("Backspace hit in ssheet_cell_text"); if (text_.size() > 0) text_.resize(text_.size()-1); if (parent_.lock()) parent_.lock()->redraw(parent_.lock()->rect()); return; } if ((ke->fn_key() != key_backspace) && (ke->character() != 0)) { text_+=ke->character(); if (parent_.lock()) parent_.lock()->redraw(parent_.lock()->rect()); return; } } } spreadsheet_cell::handle_event(e); }
/// Handle common widget events /// Try to handle the passed event, if there is nothing to do with it pass it up to the parent. /// Every non container should call widget::handle_event(e) from within its handle_event /// routine if it hasn't handled the event on its own. /// (or container::handle_event(e) which does just that) void widget::handle_event(event::ptr e) { // default handlers for focus/un_focus mouse_enter/leave events // a derived widget should call widget::handle_event() for these basic events switch (e->type()) { case event::focus: if (focusable_) { focused_ = true; redraw(rect()); return; // only return if we are focusable, otherwise try our parent } break; case event::un_focus: if (focusable_) { focused_ = false; pressed_ = false; redraw(rect()); return; // only return if we are focusable, otherwise try our parent } break; case event::mouse_enter: hover_ = true; redraw(rect()); return; break; case event::mouse_leave: hover_ = false; pressed_ = false; redraw(rect()); return; break; case event::key_up: { key_event::ptr ke = boost::shared_static_cast<key_event>(e); if (on_keyup(ke->fn_key())) return; break; } case event::mouse_motion: case event::mouse_up: case event::mouse_down: { // if this is a mouse event, translate the coordinates back relative to the parent mouse_event::ptr me = boost::shared_static_cast<mouse_event>(e); widget::ptr widget_parent = boost::shared_dynamic_cast<widget>(parent_.lock()); if (widget_parent) { me->x(me->x() + widget_parent->position().x()); me->y(me->y() + widget_parent->position().y()); } // this is handed up to the parent below } } parent_.lock()->handle_event(e); }
void numeric_spinner::handle_event(event::ptr e) { switch (e->type()) { case event::key_down: { key_event::ptr ke = boost::shared_static_cast<key_event>(e); switch ( ke->fn_key() ) { case key_enter: case key_uparrow: value_ = value_+increment_; if (value_ > max_) { if (wrap_) value_ = min_; else value_ = max_; } build_label(); on_change(); redraw(rect()); return; break; case key_downarrow: value_ = value_-increment_; if (value_ < min_) { if (wrap_) value_ = max_; else value_ = min_; } build_label(); on_change(); redraw(rect()); return; break; } break; } case event::mouse_up: { mouse_event::ptr me = boost::shared_static_cast<mouse_event>(e); if (region(me->x(), me->y()) == DOWN_ARROW) { value_ = value_-increment_; if (value_ < min_) { if (wrap_) value_ = max_; else value_ = min_; } } else { value_ = value_+increment_; if (value_ > max_) { if (wrap_) value_ = min_; else value_ = max_; } } build_label(); on_change(); redraw(rect()); return; } } widget::handle_event(e); }
// event_handler interface void application::handle_event(event::ptr e) { //INFO("application::handle_event()"); switch(e->type()) { case event::key_down: { // FIXME :Carter: shouldnt this be a polymorphic cast? key_event::ptr ke = boost::shared_static_cast<key_event>(e); switch (ke->fn_key()) { case key_escape: quit(); break; case key_tab: case key_rightarrow: case key_downarrow: { INFO("next pressed"); component::weak_ptr prev_focused_widget = focused_widget_; focused_widget_ = focused_widget_.lock()->focus_next(); component::weak_ptr temp_widget = prev_focused_widget; while (!focused_widget_.lock()) { INFO("no more focusable widgets, asking parent"); temp_widget = temp_widget.lock()->parent(); if (!temp_widget.lock()) throw error_message_exception("application::handle_event() - " "unable to find next focusable widget"); focused_widget_ = temp_widget.lock()->focus_next(); } prev_focused_widget.lock()->handle_event(event::create(event::un_focus)); focused_widget_.lock()->handle_event(event::create(event::focus)); break; } case key_enter: INFO("enter pressed"); break; case key_leftarrow: case key_uparrow: { INFO("prev pressed"); component::weak_ptr prev_focused_widget = focused_widget_; focused_widget_ = focused_widget_.lock()->focus_prev(); component::weak_ptr temp_widget = prev_focused_widget; while (!focused_widget_.lock()) { temp_widget = temp_widget.lock()->parent(); if (!temp_widget.lock()) throw error_message_exception("application::handle_event() - " "unable to find previous focusable widget"); focused_widget_ = temp_widget.lock()->focus_prev(); } prev_focused_widget.lock()->handle_event(event::create(event::un_focus)); focused_widget_.lock()->handle_event(event::create(event::focus)); break; } default: // unhandled key break; } break; } case event::key_up: break; case event::mouse_down: break; case event::mouse_up: break; case event::mouse_motion: break; case event::quit: quit(); break; default: break; //INFO("application::handle_event - unknown event"); } }