combobox width fixes from mtaht, backported from 2.X
[ardour.git] / libs / gtkmm2ext / utils.cc
1 /*
2     Copyright (C) 1999 Paul Barton-Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <map>
22
23 #include <gtk/gtkpaned.h>
24 #include <gtk/gtk.h>
25
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>
32
33 #include "i18n.h"
34
35 using namespace std;
36
37 void
38 Gtkmm2ext::get_ink_pixel_size (Glib::RefPtr<Pango::Layout> layout,
39                                int& width,
40                                int& height)
41 {
42         Pango::Rectangle ink_rect = layout->get_ink_extents ();
43         
44         width = (ink_rect.get_width() + PANGO_SCALE / 2) / PANGO_SCALE;
45         height = (ink_rect.get_height() + PANGO_SCALE / 2) / PANGO_SCALE;
46 }
47
48 void
49 Gtkmm2ext::set_size_request_to_display_given_text (Gtk::Widget &w, const gchar *text,
50                                                    gint hpadding, gint vpadding)
51         
52 {
53         int width, height;
54         w.ensure_style ();
55         
56         get_ink_pixel_size (w.create_pango_layout (text), width, height);
57         w.set_size_request(width + hpadding, height + vpadding);
58 }
59
60 void
61 Gtkmm2ext::set_size_request_to_display_given_text (Gtk::Widget &w, 
62                                 const std::vector<std::string>& strings,
63                                            gint hpadding, gint vpadding)
64         
65 {
66         int width, height;
67         int width_max = 0;
68         int height_max = 0;
69         w.ensure_style ();
70         
71         for (vector<string>::const_iterator i = strings.begin(); 
72                 i != strings.end(); ++i) {
73         get_ink_pixel_size (w.create_pango_layout (*i), width, height);
74         width_max = max(width_max,width);
75         height_max = max(height_max, height);
76         }
77         w.set_size_request(width_max + hpadding, height_max + vpadding);
78 }
79
80 void
81 Gtkmm2ext::init ()
82 {
83         // Necessary for gettext
84         (void) bindtextdomain(PACKAGE, LOCALEDIR);
85 }
86
87 void
88 Gtkmm2ext::set_popdown_strings (Gtk::ComboBoxText& cr, const vector<string>& strings)
89 {
90         cr.clear ();
91
92         for (vector<string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
93                 cr.append_text (*i);
94         }
95 }
96
97 GdkWindow*
98 Gtkmm2ext::get_paned_handle (Gtk::Paned& paned)
99 {
100         return GTK_PANED(paned.gobj())->handle;
101 }
102
103 void
104 Gtkmm2ext::set_decoration (Gtk::Window* win, Gdk::WMDecoration decor)
105 {
106         win->get_window()->set_decorations (decor);
107 }
108
109 void Gtkmm2ext::set_treeview_header_as_default_label(Gtk::TreeViewColumn* c)
110 {
111         gtk_tree_view_column_set_widget( c->gobj(), GTK_WIDGET(0) );
112 }
113