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"
37 #include "canvas_impl.h"
44 short_version (string orig, string::size_type target_length)
46 /* this tries to create a recognizable abbreviation
47 of "orig" by removing characters until we meet
48 a certain target length.
50 note that we deliberately leave digits in the result
55 string::size_type pos;
57 /* remove white-space and punctuation, starting at end */
59 while (orig.length() > target_length) {
60 if ((pos = orig.find_last_of (_("\"\n\t ,<.>/?:;'[{}]~`!@#$%^&*()_-+="))) == string::npos) {
63 orig.replace (pos, 1, "");
66 /* remove lower-case vowels, starting at end */
68 while (orig.length() > target_length) {
69 if ((pos = orig.find_last_of (_("aeiou"))) == string::npos) {
72 orig.replace (pos, 1, "");
75 /* remove upper-case vowels, starting at end */
77 while (orig.length() > target_length) {
78 if ((pos = orig.find_last_of (_("AEIOU"))) == string::npos) {
81 orig.replace (pos, 1, "");
84 /* remove lower-case consonants, starting at end */
86 while (orig.length() > target_length) {
87 if ((pos = orig.find_last_of (_("bcdfghjklmnpqrtvwxyz"))) == string::npos) {
90 orig.replace (pos, 1, "");
93 /* remove upper-case consonants, starting at end */
95 while (orig.length() > target_length) {
96 if ((pos = orig.find_last_of (_("BCDFGHJKLMNPQRTVWXYZ"))) == string::npos) {
99 orig.replace (pos, 1, "");
102 /* whatever the length is now, use it */
108 fit_to_pixels (string str, int pixel_width, string font)
113 Pango::FontDescription fontdesc (font);
115 int namelen = str.length();
116 char cstr[namelen+1];
117 strcpy (cstr, str.c_str());
120 Glib::RefPtr<Pango::Layout> layout = foo.create_pango_layout (cstr);
122 layout->set_font_description (fontdesc);
123 layout->get_pixel_size (width, height);
125 if (width < (pixel_width)) {
130 cstr[namelen] = '\0';
138 atoi (const string& s)
140 return atoi (s.c_str());
144 atof (const string& s)
146 return atof (s.c_str());
150 strip_whitespace_edges (string& str)
153 string::size_type len;
158 for (i = 0; i < len; ++i) {
159 if (isgraph (str[i])) {
166 for (i = len - 1; i >= 0; --i) {
167 if (isgraph (str[i])) {
172 str = str.substr (s, (i - s) + 1);
176 internationalize (const char **array)
180 for (uint32_t i = 0; array[i]; ++i) {
181 v.push_back (_(array[i]));
188 just_hide_it (GdkEventAny *ev, Gtk::Window *win)
194 /* xpm2rgb copied from nixieclock, which bore the legend:
196 nixieclock - a nixie desktop timepiece
197 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
199 and was released under the GPL.
203 xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
205 static long vals[256], val;
206 uint32_t t, x, y, colors, cpp;
208 unsigned char *savergb, *rgb;
212 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
213 error << string_compose (_("bad XPM header %1"), xpm[0])
218 savergb = rgb = (unsigned char*)art_alloc (h * w * 3);
220 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
221 for (t = 0; t < colors; ++t) {
222 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
226 // COLORMAP -> RGB CONVERSION
227 // Get low 3 bytes from vals[]
231 for (y = h-1; y > 0; --y) {
233 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
234 val = vals[(int)*p++];
235 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
236 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
237 *(rgb+0) = val & 0xff; // 0:R
245 xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
247 static long vals[256], val;
248 uint32_t t, x, y, colors, cpp;
250 unsigned char *savergb, *rgb;
255 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
256 error << string_compose (_("bad XPM header %1"), xpm[0])
261 savergb = rgb = (unsigned char*)art_alloc (h * w * 4);
263 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
265 if (strstr (xpm[1], "None")) {
266 sscanf (xpm[1], "%c", &transparent);
273 for (; t < colors; ++t) {
274 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
278 // COLORMAP -> RGB CONVERSION
279 // Get low 3 bytes from vals[]
283 for (y = h-1; y > 0; --y) {
287 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
289 if (transparent && (*p++ == transparent)) {
297 *(rgb+3) = alpha; // 3: alpha
298 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
299 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
300 *(rgb+0) = val & 0xff; // 0:R
307 ArdourCanvas::Points*
308 get_canvas_points (string who, uint32_t npoints)
310 // cerr << who << ": wants " << npoints << " canvas points" << endl;
311 #ifdef TRAP_EXCESSIVE_POINT_REQUESTS
312 if (npoints > (uint32_t) gdk_screen_width() + 4) {
316 return new ArdourCanvas::Points (npoints);
320 channel_combo_get_channel_count (Gtk::ComboBoxText& combo)
322 string str = combo.get_active_text();
325 if (str == _("mono")) {
327 } else if (str == _("stereo")) {
329 } else if ((chns = atoi (str)) != 0) {
337 int_from_hex (char hic, char loc)
339 int hi; /* hi byte */
340 int lo; /* low byte */
344 if( ('0'<=hi) && (hi<='9') ) {
346 } else if( ('a'<= hi) && (hi<= 'f') ) {
348 } else if( ('A'<=hi) && (hi<='F') ) {
354 if( ('0'<=lo) && (lo<='9') ) {
356 } else if( ('a'<=lo) && (lo<='f') ) {
358 } else if( ('A'<=lo) && (lo<='F') ) {
362 return lo + (16 * hi);
366 url_decode (string& url)
368 string::iterator last;
369 string::iterator next;
371 for (string::iterator i = url.begin(); i != url.end(); ++i) {
377 if (url.length() <= 3) {
383 --last; /* points at last char */
384 --last; /* points at last char - 1 */
386 for (string::iterator i = url.begin(); i != last; ) {
397 if (isxdigit (*i) && isxdigit (*next)) {
398 /* replace first digit with char */
399 *i = int_from_hex (*i,*next);
400 ++i; /* points at 2nd of 2 digits */
409 Pango::FontDescription
410 get_font_for_style (string widgetname)
413 Glib::RefPtr<Style> style;
415 foobar.set_name (widgetname);
416 foobar.ensure_style();
418 style = foobar.get_style ();
419 return style->get_font();
423 pane_handler (GdkEventButton* ev, Gtk::Paned* pane)
425 if (ev->window != Gtkmm2ext::get_paned_handle (*pane)) {
429 if (Keyboard::is_delete_event (ev)) {
434 pos = pane->get_position ();
436 if (dynamic_cast<VPaned*>(pane)) {
437 cmp = pane->get_height();
439 cmp = pane->get_width();
442 /* we have to use approximations here because we can't predict the
443 exact position or sizes of the pane (themes, etc)
446 if (pos < 10 || abs (pos - cmp) < 10) {
448 /* already collapsed: restore it (note that this is cast from a pointer value to int, which is tricky on 64bit */
450 pane->set_position ((gint64) pane->get_data ("rpos"));
454 int collapse_direction;
456 /* store the current position */
458 pane->set_data ("rpos", (gpointer) pos);
460 /* collapse to show the relevant child in full */
462 collapse_direction = (gint64) pane->get_data ("collapse-direction");
464 if (collapse_direction) {
465 pane->set_position (1);
467 if (dynamic_cast<VPaned*>(pane)) {
468 pane->set_position (pane->get_height());
470 pane->set_position (pane->get_width());
481 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a)
485 foo.set_name (style);
488 GtkRcStyle* waverc = foo.get_style()->gobj()->rc_style;
491 r = waverc->fg[Gtk::STATE_NORMAL].red / 257;
492 g = waverc->fg[Gtk::STATE_NORMAL].green / 257;
493 b = waverc->fg[Gtk::STATE_NORMAL].blue / 257;
495 /* what a hack ... "a" is for "active" */
497 a = waverc->fg[GTK_STATE_ACTIVE].red / 257;
500 warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
503 return (uint32_t) RGBA_TO_UINT(r,g,b,a);
507 canvas_item_visible (ArdourCanvas::Item* item)
509 return (item->gobj()->object.flags & GNOME_CANVAS_ITEM_VISIBLE) ? true : false;