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"
36 #include "rgb_macros.h"
43 short_version (string orig, string::size_type target_length)
45 /* this tries to create a recognizable abbreviation
46 of "orig" by removing characters until we meet
47 a certain target length.
49 note that we deliberately leave digits in the result
54 string::size_type pos;
56 /* remove white-space and punctuation, starting at end */
58 while (orig.length() > target_length) {
59 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
62 orig.replace (pos, 1, "");
65 /* remove lower-case vowels, starting at end */
67 while (orig.length() > target_length) {
68 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
71 orig.replace (pos, 1, "");
74 /* remove upper-case vowels, starting at end */
76 while (orig.length() > target_length) {
77 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
80 orig.replace (pos, 1, "");
83 /* remove lower-case consonants, starting at end */
85 while (orig.length() > target_length) {
86 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
89 orig.replace (pos, 1, "");
92 /* remove upper-case consonants, starting at end */
94 while (orig.length() > target_length) {
95 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
98 orig.replace (pos, 1, "");
101 /* whatever the length is now, use it */
107 fit_to_pixels (string str, int pixel_width, string font)
112 Pango::FontDescription fontdesc (font);
114 int namelen = str.length();
115 char cstr[namelen+1];
116 strcpy (cstr, str.c_str());
119 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (cstr);
121 layout->set_font_description (fontdesc);
122 layout->get_pixel_size (width, height);
124 if (width < (pixel_width)) {
129 cstr[namelen] = '\0';
137 atoi (const string& s)
139 return atoi (s.c_str());
143 atof (const string& s)
145 return atof (s.c_str());
149 strip_whitespace_edges (string& str)
152 string::size_type len;
157 for (i = 0; i < len; ++i) {
158 if (isgraph (str[i])) {
165 for (i = len - 1; i >= 0; --i) {
166 if (isgraph (str[i])) {
171 str = str.substr (s, (i - s) + 1);
175 internationalize (const char **array)
179 for (uint32_t i = 0; array[i]; ++i) {
180 v.push_back (_(array[i]));
187 just_hide_it (GdkEventAny *ev, Gtk::Window *win)
193 /* xpm2rgb copied from nixieclock, which bore the legend:
195 nixieclock - a nixie desktop timepiece
196 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
198 and was released under the GPL.
202 xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
204 static long vals[256], val;
205 uint32_t t, x, y, colors, cpp;
207 unsigned char *savergb, *rgb;
211 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
212 error << string_compose (_("bad XPM header %1"), xpm[0])
217 savergb = rgb = (unsigned char*)art_alloc (h * w * 3);
219 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
220 for (t = 0; t < colors; ++t) {
221 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
225 // COLORMAP -> RGB CONVERSION
226 // Get low 3 bytes from vals[]
230 for (y = h-1; y > 0; --y) {
232 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
233 val = vals[(int)*p++];
234 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
235 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
236 *(rgb+0) = val & 0xff; // 0:R
244 xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
246 static long vals[256], val;
247 uint32_t t, x, y, colors, cpp;
249 unsigned char *savergb, *rgb;
254 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
255 error << string_compose (_("bad XPM header %1"), xpm[0])
260 savergb = rgb = (unsigned char*)art_alloc (h * w * 4);
262 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
264 if (strstr (xpm[1], "None")) {
265 sscanf (xpm[1], "%c", &transparent);
272 for (; t < colors; ++t) {
273 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
277 // COLORMAP -> RGB CONVERSION
278 // Get low 3 bytes from vals[]
282 for (y = h-1; y > 0; --y) {
286 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
288 if (transparent && (*p++ == transparent)) {
296 *(rgb+3) = alpha; // 3: alpha
297 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
298 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
299 *(rgb+0) = val & 0xff; // 0:R
306 ArdourCanvas::Points*
307 get_canvas_points (string who, uint32_t npoints)
309 // cerr << who << ": wants " << npoints << " canvas points" << endl;
310 #ifdef TRAP_EXCESSIVE_POINT_REQUESTS
311 if (npoints > (uint32_t) gdk_screen_width() + 4) {
315 return new ArdourCanvas::Points (npoints);
319 channel_combo_get_channel_count (Gtk::ComboBoxText& combo)
321 string str = combo.get_active_text();
324 if (str == _("mono")) {
326 } else if (str == _("stereo")) {
328 } else if ((chns = atoi (str)) != 0) {
336 int_from_hex (char hic, char loc)
338 int hi; /* hi byte */
339 int lo; /* low byte */
343 if( ('0'<=hi) && (hi<='9') ) {
345 } else if( ('a'<= hi) && (hi<= 'f') ) {
347 } else if( ('A'<=hi) && (hi<='F') ) {
353 if( ('0'<=lo) && (lo<='9') ) {
355 } else if( ('a'<=lo) && (lo<='f') ) {
357 } else if( ('A'<=lo) && (lo<='F') ) {
361 return lo + (16 * hi);
365 url_decode (string& url)
367 string::iterator last;
368 string::iterator next;
370 for (string::iterator i = url.begin(); i != url.end(); ++i) {
376 if (url.length() <= 3) {
382 --last; /* points at last char */
383 --last; /* points at last char - 1 */
385 for (string::iterator i = url.begin(); i != last; ) {
396 if (isxdigit (*i) && isxdigit (*next)) {
397 /* replace first digit with char */
398 *i = int_from_hex (*i,*next);
399 ++i; /* points at 2nd of 2 digits */
408 Pango::FontDescription
409 get_font_for_style (string widgetname)
412 Glib::RefPtr<Style> style;
414 foobar.set_name (widgetname);
415 foobar.ensure_style();
417 style = foobar.get_style ();
418 return style->get_font();
422 pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
424 if (ev->window != Gtkmm2ext::get_paned_handle (*pane)) {
428 if (Keyboard::is_delete_event (ev)) {
433 pos = pane->get_position ();
435 if (dynamic_cast<VPaned*>(pane)) {
436 cmp = pane->get_height();
438 cmp = pane->get_width();
441 /* we have to use approximations here because we can't predict the
442 exact position or sizes of the pane (themes, etc)
445 if (pos < 10 || abs (pos - cmp) < 10) {
447 /* already collapsed: restore it (note that this is cast from a pointer value to int, which is tricky on 64bit */
449 pane->set_position ((gint64) pane->get_data ("rpos"));
453 int collapse_direction;
455 /* store the current position */
457 pane->set_data ("rpos", (gpointer) pos);
459 /* collapse to show the relevant child in full */
461 collapse_direction = (gint64) pane->get_data ("collapse-direction");
463 if (collapse_direction) {
464 pane->set_position (1);
466 if (dynamic_cast<VPaned*>(pane)) {
467 pane->set_position (pane->get_height());
469 pane->set_position (pane->get_width());
480 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
484 foo.set_name (style);
487 GtkRcStyle* waverc = foo.get_style()->gobj()->rc_style;
490 r = waverc->fg[Gtk::STATE_NORMAL].red / 257;
491 g = waverc->fg[Gtk::STATE_NORMAL].green / 257;
492 b = waverc->fg[Gtk::STATE_NORMAL].blue / 257;
494 /* what a hack ... "a" is for "active" */
496 a = waverc->fg[GTK_STATE_ACTIVE].red / 257;
499 warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
502 return (uint32_t) RGBA_TO_UINT(r,g,b,a);