I have a small application, where I want to create some buttons, toolbars, etc. but I do not want to use a pre-existing GTK icon theme, I want to create my own. So I looked for tutorials and whatnot, but as it turned out, it was not very well discussed online. So I tried to do something on my own:
Header File:
#include <gtkmm.h>
#include <string>
#include <iostream>
#include <errno.h>
class IconFactoryBuilder
{
public:
IconFactoryBuilder();
~IconFactoryBuilder();
void RegisterNewIcons(std::string pPath);
private:
Glib::RefPtr<Gtk::IconFactory> mCustomFactory;
};
Cpp File:
#include <IconFactoryBuilder.h>
IconFactoryBuilder::IconFactoryBuilder() {
mCustomFactory = Gtk::IconFactory::create();
}
IconFactoryBuilder::~IconFactoryBuilder() {
}
void IconFactoryBuilder::RegisterNewIcons(std::string pPath) {
Glib::RefPtr<Gtk::IconSet> iconSet = Gtk::IconSet::create();
Gtk::IconSource someSource;
try{
Gtk::Image *someImage=Gtk::manage(new Gtk::Image(pPath+"appbar.at.png"));
someImage->set_pixel_size(Gtk::IconSize(48));
someSource.set_pixbuf(someImage->get_pixbuf());
someSource.set_size(Gtk::ICON_SIZE_DIALOG);
someSource.set_size_wildcarded();
}
catch(const Glib::Exception &ex) {
std::cerr << "An error occurred while opening the icon file!" << strerror(errno) << std::endl;
}
catch(...) {
std::cerr << "Unknown Error!" << std::endl;
}
iconSet->add_source(someSource);
const Gtk::StockID somestock("MyNewIcon");
Gtk::Stock::add(Gtk::StockItem(somestock, "somelabel"));
mCustomFactory->add(somestock, iconSet);
mCustomFactory->add_default();
}
But now I am quite stuck, because I do not really know how to call this new icon I have created. I also do not know if the above written code is enough to actually find the icon or not.
You created an icon factory... factory?
;-)
The serious answer to your question is that you don't need Gtk::IconFactory
. Unfortunately the GTK 2 documentation doesn't tell you that it's unnecessary. What you do need is the freedesktop.org Standard Icon Naming Specification. Create your icons, give them simple names, organize according to the directory structure described there, install it to the appropriate place, and your icons will "just work" when you create a Pixbuf
or Image
using the ...from_icon_name()
functions. (example: Gtk::Image::set_from_icon_name()
)
Here is a page from the Gnome developer wiki on how to provide your own icons: http://developer.gnome.org/integration-guide/stable/icons.html.en
And here is a page from a tutorial I wrote about installing custom icons: http://ptomato.name/advanced-gtk-techniques/html/desktop-file.html