2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / search / Tiles / WidgetFu.cs
blob90ecc19235ce081f6da5ba8866ba5a34e2703d0d
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 return label;
15 public static Gtk.Label NewLabel (string text)
17 Gtk.Label label = NewLabel ();
18 label.Text = text;
19 return label;
22 public static Gtk.Label NewBoldLabel (string text)
24 Gtk.Label label = NewLabel ();
25 label.UseMarkup = true;
26 label.LabelProp = "<b>" + GLib.Markup.EscapeText (text) + "</b>";
27 return label;
30 public static Gtk.Label NewGrayLabel ()
32 Gtk.Label label = NewLabel ();
33 label.ModifyFg (Gtk.StateType.Normal, label.Style.Foreground (Gtk.StateType.Insensitive));
34 return label;
37 public static Gtk.Label NewGrayLabel (string text)
39 Gtk.Label label = NewGrayLabel ();
40 label.Text = text;
41 return label;
44 [DllImport ("libgtk-x11-2.0.so.0")]
45 private static extern void gtk_label_set_ellipsize (IntPtr label, int mode);
47 [DllImport ("libgtk-x11-2.0.so.0")]
48 private static extern void gtk_label_set_max_width_chars (IntPtr label, int max_width);
50 public static void EllipsizeLabel (Gtk.Label label)
52 gtk_label_set_ellipsize (label.Handle, 3);
55 public static void EllipsizeLabel (Gtk.Label label, int maxWidth)
57 gtk_label_set_ellipsize (label.Handle, 3);
58 gtk_label_set_max_width_chars (label.Handle, maxWidth);
61 [DllImport ("libgtk-x11-2.0.so.0")]
62 private static extern IntPtr gtk_icon_theme_get_default ();
64 [DllImport ("libgtk-x11-2.0.so.0")]
65 private static extern IntPtr gtk_icon_theme_lookup_icon (IntPtr theme, string name, int size, int flags, IntPtr error);
67 [DllImport ("libgtk-x11-2.0.so.0")]
68 private static extern int gtk_icon_info_get_base_size (IntPtr icon_info);
70 [DllImport ("libgtk-x11-2.0.so.0")]
71 private static extern void gtk_icon_info_free (IntPtr icon_info);
73 [DllImport ("libgtk-x11-2.0.so.0")]
74 private static extern IntPtr gtk_icon_theme_load_icon (IntPtr theme, string name, int size, int flags, IntPtr error);
76 public static Gdk.Pixbuf LoadThemeIcon (string name, int size)
78 try {
79 IntPtr info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_default (), name, size, 0, IntPtr.Zero);
80 if (info == IntPtr.Zero)
81 return null;
83 int base_size = gtk_icon_info_get_base_size (info);
85 // If the icon has no base size, or it would be huge
86 // compared to what we asked for, scale it down.
87 // 1.33334 is pretty arbitrary, roughly calculated
88 // as the difference between icon sizes 48 and 64.
89 if (base_size == 0 || base_size > size * 1.33334)
90 base_size = size;
91 gtk_icon_info_free (info);
93 IntPtr native = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), name, base_size, 0, IntPtr.Zero);
94 if (native != IntPtr.Zero) {
95 Gdk.Pixbuf ret = (Gdk.Pixbuf) GLib.Object.GetObject(native, true);
96 return ret;
98 } catch (System.Exception e) {
99 System.Console.Write (e.ToString ());
101 return null;
104 public static Gdk.Pixbuf LoadMimeIcon (string mimetype, int size)
106 Gtk.IconTheme icon_theme = Gtk.IconTheme.Default;
107 Gnome.IconLookupResultFlags result;
109 // FIXME when ximian bug #76540 is fixed
110 // change "new Gnome.Vfs.FileInfo (IntPtr.Zero)" to "null"
111 string icon_name = Gnome.Icon.Lookup (icon_theme, null, null, null, new Gnome.Vfs.FileInfo (IntPtr.Zero), mimetype, (Gnome.IconLookupFlags) 0, out result);
113 if (icon_name == null)
114 return null;
116 Gtk.IconInfo icon_info = icon_theme.LookupIcon (icon_name, size, 0);
118 if (icon_info == null)
119 return null;
120 try {
121 return icon_info.LoadIcon ();
122 } catch (System.Exception e) {
123 System.Console.Write (e.ToString ());
125 return null;
128 [DllImport ("libbeagleuiglue.so")]
129 private static extern void ui_glue_set_drag_pixbuf_from_image (IntPtr drag_context, IntPtr image);
131 public static void SetDragImage (Gdk.DragContext context, Gtk.Image image)
133 ui_glue_set_drag_pixbuf_from_image (context.Handle, image.Handle);