Load icons manually on Windows
authorSam Thursfield <samthursfield@codethink.co.uk>
Fri, 20 May 2011 13:52:54 +0000 (14:52 +0100)
committerSam Thursfield <samthursfield@codethink.co.uk>
Fri, 20 May 2011 14:02:06 +0000 (15:02 +0100)
Icon themes don't really make sense (or work) when the app is running
in an independent sandbox prefix. Instead, the icons we need will be
put in share/icons and loaded directly using GdkPixbuf.

Makefile.am
src/moonshot-identities-manager.vala
src/moonshot-utils.vala [new file with mode: 0644]
src/moonshot-window.vala
windows/app.wxs

index 4df40bb..d877b2a 100644 (file)
@@ -21,7 +21,8 @@ src_moonshot_SOURCES = \
         src/moonshot-idcard-widget.vala \
         src/moonshot-custom-vbox.vala \
         src/moonshot-identities-manager.vala \
-        src/moonshot-window.vala
+        src/moonshot-window.vala \
+        src/moonshot-utils.vala
 
 src_moonshot_LDADD = \
         $(moonshot_LIBS)
index 7095197..8d5d0c5 100644 (file)
@@ -108,19 +108,7 @@ class IdentitiesManager : Object {
         id_card.issuer = id_card_data[1];
         id_card.services = {"email","jabber","irc"};
         id_card.nai = id_card.username + "@" + id_card.issuer;
-
-        var icon_theme = Gtk.IconTheme.get_default ();
-        try
-        {
-            id_card.pixbuf = icon_theme.load_icon ("avatar-default",
-                                                   48,
-                                                   Gtk.IconLookupFlags.FORCE_SIZE);
-        }
-        catch (Error e)
-        {
-            id_card.pixbuf = null;
-            stdout.printf("Error: %s\n", e.message);
-        }
+        id_card.pixbuf = find_icon ("avatar-default", 48);
 
         return id_card;
     }
diff --git a/src/moonshot-utils.vala b/src/moonshot-utils.vala
new file mode 100644 (file)
index 0000000..87c5ff8
--- /dev/null
@@ -0,0 +1,41 @@
+#if OS_WIN32
+extern string? g_win32_get_package_installation_directory_of_module (void *module);
+#endif
+
+public Gdk.Pixbuf? find_icon_sized (string name, Gtk.IconSize icon_size)
+{
+    int width, height;
+    Gtk.icon_size_lookup (icon_size, out width, out height);
+    return find_icon (name, width);
+}
+
+/* Portability hack: making Gtk icon themes work on Windows is
+ * difficult; let's just bundle the icons that are necessary and
+ * load them manually.
+ */
+
+public Gdk.Pixbuf? find_icon (string name, int size)
+{
+    try
+    {
+#if OS_WIN32
+        string? base_path = g_win32_get_package_installation_directory_of_module (null);
+
+        // Hack to allow running within the source tree
+        int last_dir_index = base_path.last_index_of_char ('\\');
+        if (base_path.substring (last_dir_index) == "\\src")
+            base_path = base_path.slice(0, last_dir_index);
+
+        string? filename = Path.build_filename (base_path, "share", "icons", "%s.png".printf (name));
+        return new Gdk.Pixbuf.from_file_at_size (filename, size, size);
+#else
+        var icon_theme = Gtk.IconTheme.get_default ();
+        return icon_theme.load_icon (name, icon_size, Gtk.IconLookupFlags.FORCE_SIZE);
+#endif
+    }
+    catch (Error e)
+    {
+        stdout.printf("Error loading icon '%s': %s\n", name, e.message);
+        return null;
+    }
+}
index 7ade76a..5fb5d83 100644 (file)
@@ -197,20 +197,7 @@ class MainWindow : Window
         id_card.username = dialog.username;
         id_card.password = dialog.password;
         id_card.nai = id_card.username + "@" + id_card.issuer;
-
-        var icon_theme = IconTheme.get_default ();
-        try
-        {
-            id_card.pixbuf = icon_theme.load_icon ("avatar-default",
-                                                   48,
-                                                   IconLookupFlags.FORCE_SIZE);
-        }
-        catch (Error e)
-        {
-            id_card.pixbuf = null;
-            stdout.printf("Error: %s\n", e.message);
-        }
-
+        id_card.pixbuf = find_icon ("avatar-default", 48);
         id_card.services = {"email","jabber","irc"};
 
         return id_card;
@@ -515,17 +502,18 @@ class MainWindow : Window
         this.search_entry = new Entry();
 
         set_atk_name_description (search_entry, _("Search entry"), _("Search for a specific ID Card"));
-        this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
-                                                   "edit-find-symbolic");
-        this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
+        this.search_entry.set_icon_from_pixbuf (EntryIconPosition.PRIMARY,
+                                                find_icon_sized ("edit-find-symbolic", Gtk.IconSize.MENU));
         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
                                                  _("Search identity or service"));
+        this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
 
-        this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
-                                                   "edit-clear-symbolic");
-        this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
+        this.search_entry.set_icon_from_pixbuf (EntryIconPosition.SECONDARY,
+                                                find_icon_sized ("edit-clear-symbolic", Gtk.IconSize.MENU));
         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
                                                  _("Clear the current search"));
+        this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
+
 
         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
@@ -655,7 +643,6 @@ class MainWindow : Window
         // Force specific theme settings on Windows without requiring a gtkrc file
         Gtk.Settings settings = Gtk.Settings.get_default ();
         settings.set_string_property ("gtk-theme-name", "ms-windows", "moonshot");
-        settings.set_string_property ("gtk-icon-theme-name", "Gnome", "moonshot");
         settings.set_long_property ("gtk-menu-images", 0, "moonshot");
 #endif
 
index 6daf1d1..c1f098e 100644 (file)
               </Directory>
             </Directory>
 
-            <!-- FIXME: there *must* be a better way to do this ... surely?? -->
             <Directory Id="ICONS" Name="icons">
-              <Directory Id="GNOME" Name="gnome">
-                <Component Id="IconsGnomeIndex" Guid="A54365DE-A0A8-4568-B977-A5A266A1D16E">
-                  <File Id="INDEX.THEME" Name="index.theme" Source="share\icons\gnome\index.theme" />
-                </Component>
-
-                <Directory Id="_48x48" Name="48x48">
-                  <Directory Id="status" Name="status">
-                    <Component Id="IconsGnome48Status" Guid="7E14FF90-DF0B-4AD1-B983-0F1DB8892EE3">
-                      <File Id="AVATAR_DEFAULT" Name="avatar.default.png" Source="share\icons\gnome\48x48\status\avatar-default.png" />
-                    </Component>
-                  </Directory>
-                </Directory>
-              </Directory>
+              <Component Id="Icons" Guid="413C032F-002A-4E81-B320-9EF64248E22D">
+                <File Id="AVATAR_DEFAULT" Name="avatar-default.png" Source="share\icons\avatar-default.png" />
+                <File Id="EDIT_CLEAR_SYMBOLIC" Name="edit-clear-symbolic.png" Source="share\icons\edit-clear-symbolic.png" />
+                <File Id="EDIT_FIND_SYMBOLIC" Name="edit-find-symbolic.png" Source="share\icons\edit-find-symbolic.png" />
+              </Component>
             </Directory>
           </Directory>
         </Directory>
       <ComponentRef Id='Gtk2BinFiles' />
       <ComponentRef Id='Gtk2ThemeEngineFiles' />
       <ComponentRef Id='Gtk2ThemeRcFiles' />
-      <ComponentRef Id='IconsGnomeIndex' />
-      <ComponentRef Id='IconsGnome48Status' />
+      <ComponentRef Id='Icons' />
     </Feature>
   </Product>
 </Wix>