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)
192 /* xpm2rgb copied from nixieclock, which bore the legend:
194 nixieclock - a nixie desktop timepiece
195 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
197 and was released under the GPL.
201 xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
203 static long vals[256], val;
204 uint32_t t, x, y, colors, cpp;
206 unsigned char *savergb, *rgb;
210 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
211 error << string_compose (_("bad XPM header %1"), xpm[0])
216 savergb = rgb = (unsigned char*)art_alloc (h * w * 3);
218 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
219 for (t = 0; t < colors; ++t) {
220 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
224 // COLORMAP -> RGB CONVERSION
225 // Get low 3 bytes from vals[]
229 for (y = h-1; y > 0; --y) {
231 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
232 val = vals[(int)*p++];
233 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
234 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
235 *(rgb+0) = val & 0xff; // 0:R
243 xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
245 static long vals[256], val;
246 uint32_t t, x, y, colors, cpp;
248 unsigned char *savergb, *rgb;
253 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
254 error << string_compose (_("bad XPM header %1"), xpm[0])
259 savergb = rgb = (unsigned char*)art_alloc (h * w * 4);
261 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
263 if (strstr (xpm[1], "None")) {
264 sscanf (xpm[1], "%c", &transparent);
271 for (; t < colors; ++t) {
272 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
276 // COLORMAP -> RGB CONVERSION
277 // Get low 3 bytes from vals[]
281 for (y = h-1; y > 0; --y) {
285 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
287 if (transparent && (*p++ == transparent)) {
295 *(rgb+3) = alpha; // 3: alpha
296 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
297 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
298 *(rgb+0) = val & 0xff; // 0:R
305 Gnome::Canvas::Points*
306 get_canvas_points (string who, uint32_t npoints)
308 // cerr << who << ": wants " << npoints << " canvas points" << endl;
309 #ifdef TRAP_EXCESSIVE_POINT_REQUESTS
310 if (npoints > (uint32_t) gdk_screen_width() + 4) {
314 return new Gnome::Canvas::Points (npoints);
318 channel_combo_get_channel_count (Gtk::Combo& combo)
320 string str = combo.get_entry()->get_text();
323 if (str == _("mono")) {
325 } else if (str == _("stereo")) {
327 } else if ((chns = atoi (str)) != 0) {
335 int_from_hex (char hic, char loc)
337 int hi; /* hi byte */
338 int lo; /* low byte */
342 if( ('0'<=hi) && (hi<='9') ) {
344 } else if( ('a'<= hi) && (hi<= 'f') ) {
346 } else if( ('A'<=hi) && (hi<='F') ) {
352 if( ('0'<=lo) && (lo<='9') ) {
354 } else if( ('a'<=lo) && (lo<='f') ) {
356 } else if( ('A'<=lo) && (lo<='F') ) {
360 return lo + (16 * hi);
364 url_decode (string& url)
366 string::iterator last;
367 string::iterator next;
369 for (string::iterator i = url.begin(); i != url.end(); ++i) {
375 if (url.length() <= 3) {
381 --last; /* points at last char */
382 --last; /* points at last char - 1 */
384 for (string::iterator i = url.begin(); i != last; ) {
395 if (isxdigit (*i) && isxdigit (*next)) {
396 /* replace first digit with char */
397 *i = int_from_hex (*i,*next);
398 ++i; /* points at 2nd of 2 digits */
407 Pango::FontDescription
408 get_font_for_style (string widgetname)
411 Glib::RefPtr<Style> style;
413 foobar.set_name (widgetname);
414 foobar.ensure_style();
416 style = foobar.get_style ();
417 return style->get_font();
421 pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
423 if (ev->window != Gtkmm2ext::get_paned_handle (*pane)) {
427 if (Keyboard::is_delete_event (ev)) {
432 pos = pane->get_position ();
434 if (dynamic_cast<VPaned*>(pane)) {
435 cmp = pane->get_height();
437 cmp = pane->get_width();
440 /* we have to use approximations here because we can't predict the
441 exact position or sizes of the pane (themes, etc)
444 if (pos < 10 || abs (pos - cmp) < 10) {
446 /* already collapsed: restore it (note that this is cast from a pointer value to int, which is tricky on 64bit */
448 pane->set_position ((gint64) pane->get_data ("rpos"));
452 int collapse_direction;
454 /* store the current position */
456 pane->set_data ("rpos", (gpointer) pos);
458 /* collapse to show the relevant child in full */
460 collapse_direction = (gint64) pane->get_data ("collapse-direction");
462 if (collapse_direction) {
463 pane->set_position (1);
465 if (dynamic_cast<VPaned*>(pane)) {
466 pane->set_position (pane->get_height());
468 pane->set_position (pane->get_width());
479 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
483 foo.set_name (style);
486 GtkRcStyle* waverc = foo.get_style()->gobj()->rc_style;
489 r = waverc->fg[Gtk::STATE_NORMAL].red / 257;
490 g = waverc->fg[Gtk::STATE_NORMAL].green / 257;
491 b = waverc->fg[Gtk::STATE_NORMAL].blue / 257;
493 /* what a hack ... "a" is for "active" */
495 a = waverc->fg[GTK_STATE_ACTIVE].red / 257;
498 warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
501 return (uint32_t) RGBA_TO_UINT(r,g,b,a);
505 decorate (Gtk::Window& w, Gdk::WMDecoration d)
507 w.get_window()->set_decorations (d);