X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fbarcontroller.cc;h=3e998bbcdbe6ec69df121db08cf52d1921f3e423;hb=51ad790745400eeb38547fa4de842202fec934b8;hp=a3b2a13bf5aaf37ed08a4e5bab6910c7cd95a880;hpb=da45f489dd3d923bbba8f1cee39ab445c4bf1b64;p=ardour.git diff --git a/libs/gtkmm2ext/barcontroller.cc b/libs/gtkmm2ext/barcontroller.cc index a3b2a13bf5..3e998bbcdb 100644 --- a/libs/gtkmm2ext/barcontroller.cc +++ b/libs/gtkmm2ext/barcontroller.cc @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -25,9 +26,10 @@ #include -#include -#include -#include +#include "gtkmm2ext/gtk_ui.h" +#include "gtkmm2ext/utils.h" +#include "gtkmm2ext/keyboard.h" +#include "gtkmm2ext/barcontroller.h" #include "i18n.h" @@ -36,12 +38,10 @@ using namespace Gtk; using namespace Gtkmm2ext; BarController::BarController (Gtk::Adjustment& adj, - PBD::Controllable& mc, - sigc::slot lc) + boost::shared_ptr mc) : adjustment (adj), binding_proxy (mc), - label_callback (lc), spinner (adjustment) { @@ -49,8 +49,8 @@ BarController::BarController (Gtk::Adjustment& adj, grabbed = false; switching = false; switch_on_release = false; - with_text = true; use_parent = false; + logarithmic = false; layout = darea.create_pango_layout(""); @@ -76,7 +76,10 @@ BarController::BarController (Gtk::Adjustment& adj, spinner.signal_activate().connect (mem_fun (*this, &BarController::entry_activated)); spinner.signal_focus_out_event().connect (mem_fun (*this, &BarController::entry_focus_out)); + spinner.signal_input().connect (mem_fun (*this, &BarController::entry_input)); + spinner.signal_output().connect (mem_fun (*this, &BarController::entry_output)); spinner.set_digits (3); + spinner.set_numeric (true); add (darea); show_all (); @@ -144,14 +147,14 @@ BarController::button_release (GdkEventButton* ev) return true; } - if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) { + if ((ev->state & (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier)) == Keyboard::TertiaryModifier) { adjustment.set_value (initial_value); } else { double scale; - if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) { + if ((ev->state & (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) == (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) { scale = 0.01; - } else if (ev->state & GDK_CONTROL_MASK) { + } else if (ev->state & Keyboard::PrimaryModifier) { scale = 0.1; } else { scale = 1.0; @@ -179,9 +182,9 @@ BarController::scroll (GdkEventScroll* ev) { double scale; - if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) { + if ((ev->state & (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) == (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) { scale = 0.01; - } else if (ev->state & GDK_CONTROL_MASK) { + } else if (ev->state & Keyboard::PrimaryModifier) { scale = 0.1; } else { scale = 1.0; @@ -211,13 +214,13 @@ BarController::motion (GdkEventMotion* ev) return true; } - if ((ev->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)) == GDK_SHIFT_MASK) { + if ((ev->state & (Keyboard::TertiaryModifier|Keyboard::PrimaryModifier)) == Keyboard::TertiaryModifier) { return TRUE; } - if ((ev->state & (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) == (GDK_CONTROL_MASK|GDK_SHIFT_MASK)) { + if ((ev->state & (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) == (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) { scale = 0.01; - } else if (ev->state & GDK_CONTROL_MASK) { + } else if (ev->state & Keyboard::PrimaryModifier) { scale = 0.1; } else { scale = 1.0; @@ -259,7 +262,7 @@ BarController::mouse_control (double x, GdkWindow* window, double scaling) } bool -BarController::expose (GdkEventExpose* event) +BarController::expose (GdkEventExpose* /*event*/) { Glib::RefPtr win (darea.get_window()); Widget* parent; @@ -343,48 +346,34 @@ BarController::expose (GdkEventExpose* event) break; } - if (with_text) { - /* draw label */ - - char buf[64]; - buf[0] = '\0'; - - if (label_callback) - label_callback (buf, 64); - - if (buf[0] != '\0') { - - layout->set_text (buf); + /* draw label */ - int width, height; - layout->get_pixel_size (width, height); + int xpos = -1; + std::string const label = get_label (xpos); - int xpos; + if (!label.empty()) { + + layout->set_text (label); + + int width, height; + layout->get_pixel_size (width, height); + if (xpos == -1) { xpos = max (3, 1 + (x2 - (width/2))); xpos = min (darea.get_width() - width - 3, xpos); - - win->draw_layout (get_style()->get_text_gc (get_state()), - xpos, - (darea.get_height()/2) - (height/2), - layout); } + + win->draw_layout (get_style()->get_text_gc (get_state()), + xpos, + (darea.get_height()/2) - (height/2), + layout); } - + return true; } void -BarController::set_with_text (bool yn) -{ - if (with_text != yn) { - with_text = yn; - queue_draw (); - } -} - -void -BarController::set_style (Style s) +BarController::set_style (barStyle s) { _style = s; darea.queue_draw (); @@ -437,18 +426,11 @@ BarController::switch_to_spinner () void BarController::entry_activated () { - string text = spinner.get_text (); - float val; - - if (sscanf (text.c_str(), "%f", &val) == 1) { - adjustment.set_value (val); - } - switch_to_bar (); } bool -BarController::entry_focus_out (GdkEventFocus* ev) +BarController::entry_focus_out (GdkEventFocus* /*ev*/) { entry_activated (); return true; @@ -467,3 +449,85 @@ BarController::set_sensitive (bool yn) Frame::set_sensitive (yn); darea.set_sensitive (yn); } + +/* + This is called when we need to update the adjustment with the value + from the spinner's text entry. + + We need to use Gtk::Entry::get_text to avoid recursive nastiness :) + + If we're not in logarithmic mode we can return false to use the + default conversion. + + In theory we should check for conversion errors but set numeric + mode to true on the spinner prevents invalid input. +*/ +int +BarController::entry_input (double* new_value) +{ + if (!logarithmic) { + return false; + } + + // extract a double from the string and take its log + Entry *entry = dynamic_cast(&spinner); + stringstream stream(entry->get_text()); + stream.imbue(std::locale("")); + + double value; + stream >> value; + + *new_value = log(value); + return true; +} + +/* + This is called when we need to update the spinner's text entry + with the value of the adjustment. + + We need to use Gtk::Entry::set_text to avoid recursive nastiness :) + + If we're not in logarithmic mode we can return false to use the + default conversion. +*/ +bool +BarController::entry_output () +{ + if (!logarithmic) { + return false; + } + + // generate the exponential and turn it into a string + // convert to correct locale. + + stringstream stream; + string str; + size_t found; + + // Gtk.Entry does not like the thousands separator, so we have to + // remove it after conversion from float to string. + + stream.imbue(std::locale("")); + stream.precision(spinner.get_digits()); + + stream << fixed << exp(spinner.get_adjustment()->get_value()); + + str=stream.str(); + + // find thousands separators, remove them + found = str.find(use_facet >(std::locale("")).thousands_sep()); + while(found != str.npos) { + str.erase(found,1); + + //find next + found = str.find(use_facet >(std::locale("")).thousands_sep()); + } + + Entry *entry = dynamic_cast(&spinner); + entry->set_text(str); + + return true; +} + + +