2 Copyright (C) 2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <libart_lgpl/art_misc.h>
24 #include <gtkmm/window.h>
25 #include <gtkmm/combo.h>
26 #include <gtkmm/label.h>
27 #include <gtkmm/paned.h>
28 #include <gtk/gtkpaned.h>
30 #include <gtkmm2ext/utils.h>
32 #include "ardour_ui.h"
35 #include "rgb_macros.h"
42 short_version (string orig, string::size_type target_length)
44 /* this tries to create a recognizable abbreviation
45 of "orig" by removing characters until we meet
46 a certain target length.
48 note that we deliberately leave digits in the result
53 string::size_type pos;
55 /* remove white-space and punctuation, starting at end */
57 while (orig.length() > target_length) {
58 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
61 orig.replace (pos, 1, "");
64 /* remove lower-case vowels, starting at end */
66 while (orig.length() > target_length) {
67 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
70 orig.replace (pos, 1, "");
73 /* remove upper-case vowels, starting at end */
75 while (orig.length() > target_length) {
76 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
79 orig.replace (pos, 1, "");
82 /* remove lower-case consonants, starting at end */
84 while (orig.length() > target_length) {
85 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
88 orig.replace (pos, 1, "");
91 /* remove upper-case consonants, starting at end */
93 while (orig.length() > target_length) {
94 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
97 orig.replace (pos, 1, "");
100 /* whatever the length is now, use it */
106 fit_to_pixels (string str, int pixel_width, string font)
111 Pango::FontDescription fontdesc (font);
113 int namelen = str.length();
114 char cstr[namelen+1];
115 strcpy (cstr, str.c_str());
118 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (cstr);
120 layout->set_font_description (fontdesc);
121 layout->get_pixel_size (width, height);
123 if (width < (pixel_width)) {
128 cstr[namelen] = '\0';
136 atoi (const string& s)
138 return atoi (s.c_str());
142 atof (const string& s)
144 return atof (s.c_str());
148 strip_whitespace_edges (string& str)
151 string::size_type len;
156 for (i = 0; i < len; ++i) {
157 if (isgraph (str[i])) {
164 for (i = len - 1; i >= 0; --i) {
165 if (isgraph (str[i])) {
170 str = str.substr (s, (i - s) + 1);
174 internationalize (const char **array)
178 for (uint32_t i = 0; array[i]; ++i) {
179 v.push_back (_(array[i]));
186 just_hide_it (GdkEventAny *ev, Gtk::Window *win)
188 ARDOUR_UI::instance()->allow_focus (false);
194 allow_keyboard_focus (bool yn)
196 ARDOUR_UI::instance()->allow_focus (yn);
199 /* xpm2rgb copied from nixieclock, which bore the legend:
201 nixieclock - a nixie desktop timepiece
202 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
204 and was released under the GPL.
208 xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
210 static long vals[256], val;
211 uint32_t t, x, y, colors, cpp;
213 unsigned char *savergb, *rgb;
217 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
218 error << string_compose (_("bad XPM header %1"), xpm[0])
223 savergb = rgb = (unsigned char*)art_alloc (h * w * 3);
225 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
226 for (t = 0; t < colors; ++t) {
227 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
231 // COLORMAP -> RGB CONVERSION
232 // Get low 3 bytes from vals[]
236 for (y = h-1; y > 0; --y) {
238 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
239 val = vals[(int)*p++];
240 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
241 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
242 *(rgb+0) = val & 0xff; // 0:R
250 xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
252 static long vals[256], val;
253 uint32_t t, x, y, colors, cpp;
255 unsigned char *savergb, *rgb;
260 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
261 error << string_compose (_("bad XPM header %1"), xpm[0])
266 savergb = rgb = (unsigned char*)art_alloc (h * w * 4);
268 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
270 if (strstr (xpm[1], "None")) {
271 sscanf (xpm[1], "%c", &transparent);
278 for (; t < colors; ++t) {
279 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
283 // COLORMAP -> RGB CONVERSION
284 // Get low 3 bytes from vals[]
288 for (y = h-1; y > 0; --y) {
292 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
294 if (transparent && (*p++ == transparent)) {
302 *(rgb+3) = alpha; // 3: alpha
303 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
304 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
305 *(rgb+0) = val & 0xff; // 0:R
312 Gnome::Canvas::Points*
313 get_canvas_points (string who, uint32_t npoints)
315 // cerr << who << ": wants " << npoints << " canvas points" << endl;
316 #ifdef TRAP_EXCESSIVE_POINT_REQUESTS
317 if (npoints > (uint32_t) gdk_screen_width() + 4) {
321 return new Gnome::Canvas::Points (npoints);
325 channel_combo_get_channel_count (Gtk::Combo& combo)
327 string str = combo.get_entry()->get_text();
330 if (str == _("mono")) {
332 } else if (str == _("stereo")) {
334 } else if ((chns = atoi (str)) != 0) {
342 int_from_hex (char hic, char loc)
344 int hi; /* hi byte */
345 int lo; /* low byte */
349 if( ('0'<=hi) && (hi<='9') ) {
351 } else if( ('a'<= hi) && (hi<= 'f') ) {
353 } else if( ('A'<=hi) && (hi<='F') ) {
359 if( ('0'<=lo) && (lo<='9') ) {
361 } else if( ('a'<=lo) && (lo<='f') ) {
363 } else if( ('A'<=lo) && (lo<='F') ) {
367 return lo + (16 * hi);
371 url_decode (string& url)
373 string::iterator last;
374 string::iterator next;
376 for (string::iterator i = url.begin(); i != url.end(); ++i) {
382 if (url.length() <= 3) {
388 --last; /* points at last char */
389 --last; /* points at last char - 1 */
391 for (string::iterator i = url.begin(); i != last; ) {
402 if (isxdigit (*i) && isxdigit (*next)) {
403 /* replace first digit with char */
404 *i = int_from_hex (*i,*next);
405 ++i; /* points at 2nd of 2 digits */
414 Pango::FontDescription
415 get_font_for_style (string widgetname)
418 Glib::RefPtr<Style> style;
420 foobar.set_name (widgetname);
421 foobar.ensure_style();
423 style = foobar.get_style ();
424 return style->get_font();
428 pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
430 if (ev->window != Gtkmm2ext::get_paned_handle (*pane)) {
434 if (Keyboard::is_delete_event (ev)) {
439 pos = pane->get_position ();
441 if (dynamic_cast<VPaned*>(pane)) {
442 cmp = pane->get_height();
444 cmp = pane->get_width();
447 /* we have to use approximations here because we can't predict the
448 exact position or sizes of the pane (themes, etc)
451 if (pos < 10 || abs (pos - cmp) < 10) {
453 /* already collapsed: restore it (note that this is cast from a pointer value to int, which is tricky on 64bit */
455 pane->set_position ((gint64) pane->get_data ("rpos"));
459 int collapse_direction;
461 /* store the current position */
463 pane->set_data ("rpos", (gpointer) pos);
465 /* collapse to show the relevant child in full */
467 collapse_direction = (gint64) pane->get_data ("collapse-direction");
469 if (collapse_direction) {
470 pane->set_position (1);
472 if (dynamic_cast<VPaned*>(pane)) {
473 pane->set_position (pane->get_height());
475 pane->set_position (pane->get_width());
486 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
490 foo.set_name (style);
493 GtkRcStyle* waverc = foo.get_style()->gobj()->rc_style;
496 r = waverc->fg[Gtk::STATE_NORMAL].red / 257;
497 g = waverc->fg[Gtk::STATE_NORMAL].green / 257;
498 b = waverc->fg[Gtk::STATE_NORMAL].blue / 257;
500 /* what a hack ... "a" is for "active" */
502 a = waverc->fg[GTK_STATE_ACTIVE].red / 257;
505 warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
508 return (uint32_t) RGBA_TO_UINT(r,g,b,a);
512 decorate (Gtk::Window& w, Gdk::WMDecoration d)
514 w.get_window()->set_decorations (d);