Make labels in the details pane selectable
[beagle.git] / search / Tiles / WidgetFu.cs
blob4dd9fae1e0b8f9ecef83e951aa4f3e27536d95ca
1 using System;
2 using System.Runtime.InteropServices;
4 namespace Search.Tiles {
6 public static class WidgetFu {
8 public static Gtk.Label NewLabel ()
10 Gtk.Label label = new Gtk.Label ();
11 label.SetAlignment (0.0f, 0.5f);
12 label.Selectable = true;
13 return label;
16 public static Gtk.Label NewLabel (string text)
18 Gtk.Label label = NewLabel ();
19 label.Text = text;
20 return label;
23 public static Gtk.Label NewBoldLabel (string text)
25 Gtk.Label label = NewLabel ();
26 label.UseMarkup = true;
27 label.LabelProp = "<b>" + GLib.Markup.EscapeText (text) + "</b>";
28 return label;
31 public static Gtk.Label NewGrayLabel ()
33 Gtk.Label label = NewLabel ();
34 label.ModifyFg (Gtk.StateType.Normal, label.Style.Foreground (Gtk.StateType.Insensitive));
35 return label;
38 public static Gtk.Label NewGrayLabel (string text)
40 Gtk.Label label = NewGrayLabel ();
41 label.Text = text;
42 return label;
45 [DllImport ("libgtk-x11-2.0.so.0")]
46 private static extern void gtk_label_set_ellipsize (IntPtr label, int mode);
48 [DllImport ("libgtk-x11-2.0.so.0")]
49 private static extern void gtk_label_set_max_width_chars (IntPtr label, int max_width);
51 public static void EllipsizeLabel (Gtk.Label label)
53 gtk_label_set_ellipsize (label.Handle, 3);
56 public static void EllipsizeLabel (Gtk.Label label, int maxWidth)
58 gtk_label_set_ellipsize (label.Handle, 3);
59 gtk_label_set_max_width_chars (label.Handle, maxWidth);
62 [DllImport ("libgtk-x11-2.0.so.0")]
63 private static extern IntPtr gtk_icon_theme_get_default ();
65 [DllImport ("libgtk-x11-2.0.so.0")]
66 private static extern IntPtr gtk_icon_theme_lookup_icon (IntPtr theme, string name, int size, int flags, IntPtr error);
68 [DllImport ("libgtk-x11-2.0.so.0")]
69 private static extern int gtk_icon_info_get_base_size (IntPtr icon_info);
71 [DllImport ("libgtk-x11-2.0.so.0")]
72 private static extern void gtk_icon_info_free (IntPtr icon_info);
74 [DllImport ("libgtk-x11-2.0.so.0")]
75 private static extern IntPtr gtk_icon_theme_load_icon (IntPtr theme, string name, int size, int flags, IntPtr error);
77 public static Gdk.Pixbuf LoadThemeIcon (string name, int size)
79 try {
80 IntPtr info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_default (), name, size, 0, IntPtr.Zero);
81 if (info == IntPtr.Zero)
82 return null;
84 int base_size = gtk_icon_info_get_base_size (info);
86 // If the icon has no base size, or it would be huge
87 // compared to what we asked for, scale it down.
88 // 1.33334 is pretty arbitrary, roughly calculated
89 // as the difference between icon sizes 48 and 64.
90 if (base_size == 0 || base_size > size * 1.33334)
91 base_size = size;
92 gtk_icon_info_free (info);
94 IntPtr native = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), name, base_size, 0, IntPtr.Zero);
95 if (native != IntPtr.Zero) {
96 Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(native, true);
97 return ret;
99 } catch (System.Exception e) {
100 System.Console.Write (e.ToString ());
102 return null;
105 public static Gdk.Pixbuf LoadMimeIcon (string mimetype, int size)
107 Gtk.IconTheme icon_theme = Gtk.IconTheme.Default;
108 Gnome.IconLookupResultFlags result;
110 // FIXME when ximian bug #76540 is fixed
111 // change "new Gnome.Vfs.FileInfo (IntPtr.Zero)" to "null"
112 string icon_name = Gnome.Icon.Lookup (icon_theme, null, null, null, new Gnome.Vfs.FileInfo (IntPtr.Zero), mimetype, (Gnome.IconLookupFlags) 0, out result);
114 if (icon_name == null)
115 return null;
117 Gtk.IconInfo icon_info = icon_theme.LookupIcon (icon_name, size, 0);
119 if (icon_info == null)
120 return null;
121 try {
122 return icon_info.LoadIcon ();
123 } catch (System.Exception e) {
124 System.Console.Write (e.ToString ());
126 return null;
129 [DllImport ("libbeagleuiglue.so")]
130 private static extern void ui_glue_set_drag_pixbuf_from_image (IntPtr drag_context, IntPtr image);
132 public static void SetDragImage (Gdk.DragContext context, Gtk.Image image)
134 ui_glue_set_drag_pixbuf_from_image (context.Handle, image.Handle);