fix up file renaming code a little bit
[ArdourMidi.git] / libs / gtkmm2ext / utils.cc
blob4e45cdb8b7975646c4626d5d606279daa8181ad4
1 /*
2 Copyright (C) 1999 Paul Barton-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.
18 $Id$
21 #include <map>
23 #include <gtk/gtkpaned.h>
24 #include <gtk/gtk.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm/widget.h>
28 #include <gtkmm/button.h>
29 #include <gtkmm/window.h>
30 #include <gtkmm/paned.h>
31 #include <gtkmm/comboboxtext.h>
33 #include "i18n.h"
35 using namespace std;
37 void
38 Gtkmm2ext::get_ink_pixel_size (Glib::RefPtr<Pango::Layout> layout,
39 int& width,
40 int& height)
42 Pango::Rectangle ink_rect = layout->get_ink_extents ();
44 width = (ink_rect.get_width() + PANGO_SCALE / 2) / PANGO_SCALE;
45 height = (ink_rect.get_height() + PANGO_SCALE / 2) / PANGO_SCALE;
48 void
49 get_pixel_size (Glib::RefPtr<Pango::Layout> layout,
50 int& width,
51 int& height)
53 layout->get_pixel_size (width, height);
56 void
57 Gtkmm2ext::set_size_request_to_display_given_text (Gtk::Widget &w, const gchar *text,
58 gint hpadding, gint vpadding)
61 int width, height;
62 w.ensure_style ();
64 get_pixel_size (w.create_pango_layout (text), width, height);
65 w.set_size_request(width + hpadding, height + vpadding);
68 void
69 Gtkmm2ext::set_size_request_to_display_given_text (Gtk::Widget &w,
70 const std::vector<std::string>& strings,
71 gint hpadding, gint vpadding)
74 int width, height;
75 int width_max = 0;
76 int height_max = 0;
77 w.ensure_style ();
79 for (vector<string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
80 get_pixel_size (w.create_pango_layout (*i), width, height);
81 width_max = max(width_max,width);
82 height_max = max(height_max, height);
84 w.set_size_request(width_max + hpadding, height_max + vpadding);
87 void
88 Gtkmm2ext::init ()
90 // Necessary for gettext
91 (void) bindtextdomain(PACKAGE, LOCALEDIR);
94 void
95 Gtkmm2ext::set_popdown_strings (Gtk::ComboBoxText& cr, const vector<string>& strings, bool set_size, gint hpadding, gint vpadding)
97 vector<string>::const_iterator i;
99 cr.clear ();
101 if (set_size) {
102 vector<string> copy;
104 for (i = strings.begin(); i != strings.end(); ++i) {
105 if ((*i).find_first_of ("gy") != string::npos) {
106 /* contains a descender */
107 break;
111 if (i == strings.end()) {
113 /* make a copy of the strings then add one that has a descener */
115 copy = strings;
116 copy.push_back ("g");
117 set_size_request_to_display_given_text (cr, copy, COMBO_FUDGE+10+hpadding, 15+vpadding);
119 } else {
120 set_size_request_to_display_given_text (cr, strings, COMBO_FUDGE+10+hpadding, 15+vpadding);
124 for (i = strings.begin(); i != strings.end(); ++i) {
125 cr.append_text (*i);
129 GdkWindow*
130 Gtkmm2ext::get_paned_handle (Gtk::Paned& paned)
132 return GTK_PANED(paned.gobj())->handle;
135 void
136 Gtkmm2ext::set_decoration (Gtk::Window* win, Gdk::WMDecoration decor)
138 win->get_window()->set_decorations (decor);
141 void Gtkmm2ext::set_treeview_header_as_default_label(Gtk::TreeViewColumn* c)
143 gtk_tree_view_column_set_widget( c->gobj(), GTK_WIDGET(0) );
146 void
147 Gtkmm2ext::detach_menu (Gtk::Menu& menu)
149 /* its possible for a Gtk::Menu to have no gobj() because it has
150 not yet been instantiated. Catch this and provide a safe
151 detach method.
153 if (menu.gobj()) {
154 if (menu.get_attach_widget()) {
155 menu.detach ();
160 bool
161 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (uint32_t& keyval)
163 int fakekey = GDK_VoidSymbol;
165 switch (keyval) {
166 case GDK_Tab:
167 case GDK_ISO_Left_Tab:
168 fakekey = GDK_nabla;
169 break;
171 case GDK_Up:
172 fakekey = GDK_uparrow;
173 break;
175 case GDK_Down:
176 fakekey = GDK_downarrow;
177 break;
179 case GDK_Right:
180 fakekey = GDK_rightarrow;
181 break;
183 case GDK_Left:
184 fakekey = GDK_leftarrow;
185 break;
187 case GDK_Return:
188 fakekey = GDK_3270_Enter;
189 break;
191 case GDK_KP_Enter:
192 fakekey = GDK_F35;
193 break;
195 default:
196 break;
199 if (fakekey != GDK_VoidSymbol) {
200 keyval = fakekey;
201 return true;
204 return false;
207 uint32_t
208 Gtkmm2ext::possibly_translate_legal_accelerator_to_real_key (uint32_t keyval)
210 switch (keyval) {
211 case GDK_nabla:
212 return GDK_Tab;
213 break;
215 case GDK_uparrow:
216 return GDK_Up;
217 break;
219 case GDK_downarrow:
220 return GDK_Down;
221 break;
223 case GDK_rightarrow:
224 return GDK_Right;
225 break;
227 case GDK_leftarrow:
228 return GDK_Left;
229 break;
231 case GDK_3270_Enter:
232 return GDK_Return;
234 case GDK_F35:
235 return GDK_KP_Enter;
236 break;
239 return keyval;