From 11edfd035e19c90544bf10ff3845e8d7f7e80c5b Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 15 Jan 2010 14:42:19 +0000 Subject: [PATCH] dynamically resize text in the big clock, first version git-svn-id: svn://localhost/ardour2/branches/3.0@6494 d708f5d6-7413-0410-9779-e7cbd77b26cf --- gtk2_ardour/ardour_ui.cc | 2 + gtk2_ardour/ardour_ui.h | 5 ++ gtk2_ardour/ardour_ui_ed.cc | 119 +++++++++++++++++++++++++++++++++++- gtk2_ardour/audio_clock.cc | 68 +++++++++++++++++---- gtk2_ardour/audio_clock.h | 1 + libs/gtkmm2ext/utils.cc | 12 +++- 6 files changed, 192 insertions(+), 15 deletions(-) diff --git a/gtk2_ardour/ardour_ui.cc b/gtk2_ardour/ardour_ui.cc index 8ab4cf6577..3bbd748277 100644 --- a/gtk2_ardour/ardour_ui.cc +++ b/gtk2_ardour/ardour_ui.cc @@ -197,6 +197,8 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[]) engine = 0; _session_is_new = false; big_clock_window = 0; + big_clock_height = 0; + big_clock_resize_in_progress = false; session_selector_window = 0; last_key_press_time = 0; _will_create_new_session_automatically = false; diff --git a/gtk2_ardour/ardour_ui.h b/gtk2_ardour/ardour_ui.h index 9cf245fbeb..bb8db0ef5a 100644 --- a/gtk2_ardour/ardour_ui.h +++ b/gtk2_ardour/ardour_ui.h @@ -313,6 +313,11 @@ class ARDOUR_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr AudioClock big_clock; Gtk::Window* big_clock_window; + void big_clock_size_allocate (Gtk::Allocation&); + bool idle_big_clock_text_resizer (int width, int height); + void big_clock_realized (); + bool big_clock_resize_in_progress; + int big_clock_height; void float_big_clock (Gtk::Window* parent); bool main_window_state_event_handler (GdkEventWindowState*, bool window_was_editor); diff --git a/gtk2_ardour/ardour_ui_ed.cc b/gtk2_ardour/ardour_ui_ed.cc index 6a8f68cf6f..d9a6958e70 100644 --- a/gtk2_ardour/ardour_ui_ed.cc +++ b/gtk2_ardour/ardour_ui_ed.cc @@ -536,13 +536,22 @@ ARDOUR_UI::setup_clock () big_clock_window->set_title (_("Big Clock")); big_clock_window->set_type_hint (Gdk::WINDOW_TYPE_HINT_UTILITY); - big_clock_window->signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), big_clock_window, (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH))); + big_clock_window->signal_realize().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_realized)); big_clock_window->signal_unmap().connect (sigc::bind (sigc::ptr_fun(&ActionManager::uncheck_toggleaction), X_("/Common/ToggleBigClock"))); big_clock_window->signal_key_press_event().connect (sigc::bind (sigc::ptr_fun (relay_key_press), big_clock_window), false); + big_clock_window->signal_size_allocate().connect (sigc::mem_fun (*this, &ARDOUR_UI::big_clock_size_allocate)); manage_window (*big_clock_window); } +void +ARDOUR_UI::big_clock_realized () +{ + set_decoration (big_clock_window, (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)); + int x, y, w, d; + big_clock_window->get_window()->get_geometry (x, y, w, big_clock_height, d); +} + void ARDOUR_UI::float_big_clock (Gtk::Window* parent) { @@ -555,3 +564,111 @@ ARDOUR_UI::float_big_clock (Gtk::Window* parent) } } +void +ARDOUR_UI::big_clock_size_allocate (Gtk::Allocation& allocation) +{ + if (!big_clock_resize_in_progress) { + Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::idle_big_clock_text_resizer), 0, 0)); + big_clock_resize_in_progress = true; + } + + big_clock_window->set_size_request (allocation.get_width() - 2, allocation.get_height() - 1); +} + +bool +ARDOUR_UI::idle_big_clock_text_resizer (int win_w, int win_h) +{ + extern void get_pixel_size (Glib::RefPtr layout, int& width, int& height); + Glib::RefPtr win = big_clock_window->get_window(); + assert (win); + + big_clock_resize_in_progress = false; + + Pango::FontDescription fd (big_clock.get_style()->get_font()); + string family = fd.get_family(); + int size = fd.get_size (); + int original_size; + bool absolute = fd.get_size_is_absolute (); + int stepsize; + + if (!absolute) { + size /= PANGO_SCALE; + } + + original_size = size; + + int x, y, winw, winh, d; + int w, h; + int slop; + int limit; + + win->get_geometry (x, y, winw, winh, d); + + Glib::RefPtr layout = big_clock.create_pango_layout ("0"); + get_pixel_size (layout, w, h); + + /* we want about 10% of the font height as padding, and we'll allow 10% of slop + in the accuracy of the fit. + */ + + slop = 10; + limit = winh - (h/4); + + if (h < limit && limit - h < slop) { + /* current font is smaller than the window height but not by too much */ + return false; + } + + stepsize = 16; + if (h > limit) { + /* font is too big, lets get smaller */ + size -= stepsize; + } else { + /* font is too big, lets get bigger */ + size += stepsize; + } + + while (1) { + + char buf[family.length()+16]; + snprintf (buf, family.length()+16, "%s %d", family.c_str(), size); + Pango::FontDescription fd (buf); + layout->set_font_description (fd); + get_pixel_size (layout, w, h); + + if (abs (h - limit) < slop) { + if (size != original_size) { + + /* use the size from the last loop */ + + Glib::RefPtr rcstyle = big_clock.get_modifier_style (); + rcstyle->set_font (fd); + big_clock.modify_style (rcstyle); + } + break; + } + + if (h > limit) { + + /* too big, stepsize should be smaller */ + + if (size < 2) { + break; + } + size -= stepsize; + + } else if (h < limit) { + + /* too small (but not small enough): step size should be bigger */ + + if (size > 720) { + break; + } + size += stepsize; + } + + stepsize /= 2; + } + + return false; +} diff --git a/gtk2_ardour/audio_clock.cc b/gtk2_ardour/audio_clock.cc index 86497709c6..bdf0ceda33 100644 --- a/gtk2_ardour/audio_clock.cc +++ b/gtk2_ardour/audio_clock.cc @@ -2044,30 +2044,30 @@ AudioClock::set_mode (Mode m) void AudioClock::set_size_requests () { - /* note that in some fonts, "88" is narrower than "00", hence the 2 pixel padding */ + /* note that in some fonts, "88" is narrower than "00" */ switch (_mode) { case Timecode: - Gtkmm2ext::set_size_request_to_display_given_text (hours_label, "-00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (minutes_label, "00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (seconds_label, "00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (frames_label, "00", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (hours_label, "-88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (minutes_label, "88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (seconds_label, "88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (frames_label, "88", 5, 5); break; case BBT: - Gtkmm2ext::set_size_request_to_display_given_text (bars_label, "-000", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (beats_label, "00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (ticks_label, "0000", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (bars_label, "-888", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (beats_label, "88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (ticks_label, "8888", 5, 5); break; case MinSec: - Gtkmm2ext::set_size_request_to_display_given_text (ms_hours_label, "00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (ms_minutes_label, "00", 5, 5); - Gtkmm2ext::set_size_request_to_display_given_text (ms_seconds_label, "00.000", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (ms_hours_label, "88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (ms_minutes_label, "88", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (ms_seconds_label, "88.888", 5, 5); break; case Frames: - Gtkmm2ext::set_size_request_to_display_given_text (audio_frames_label, "0000000000", 5, 5); + Gtkmm2ext::set_size_request_to_display_given_text (audio_frames_label, "8888888888", 5, 5); break; case Off: @@ -2082,3 +2082,47 @@ AudioClock::set_bbt_reference (nframes64_t pos) { bbt_reference_time = pos; } + +void +AudioClock::on_style_changed (const Glib::RefPtr