]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/plugintoolbar.cpp
test commit
[divverent/netradiant.git] / radiant / plugintoolbar.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "plugintoolbar.h"
23
24
25 #include "itoolbar.h"
26 #include "modulesystem.h"
27
28 #include <gtk/gtktoolbar.h>
29
30 #include "stream/stringstream.h"
31 #include "gtkutil/image.h"
32 #include "gtkutil/container.h"
33
34 #include "mainframe.h"
35 #include "plugin.h"
36
37 GtkImage* new_plugin_image(const char* filename)
38 {
39   {
40     StringOutputStream fullpath(256);
41     fullpath << GameToolsPath_get() << g_pluginsDir << "bitmaps/" << filename;
42     GtkImage* image = image_new_from_file_with_mask(fullpath.c_str());
43     if(image != 0)
44     {
45       return image;
46     }
47   }
48
49   {
50     StringOutputStream fullpath(256);
51     fullpath << AppPath_get() << g_pluginsDir << "bitmaps/" << filename;
52     GtkImage* image = image_new_from_file_with_mask(fullpath.c_str());
53     if(image != 0)
54     {
55       return image;
56     }
57   }
58
59   {
60     StringOutputStream fullpath(256);
61     fullpath << AppPath_get() << g_modulesDir << "bitmaps/" << filename;
62     GtkImage* image = image_new_from_file_with_mask(fullpath.c_str());
63     if(image != 0)
64     {
65       return image;
66     }
67   }
68
69   return image_new_missing();
70
71
72 inline GtkToolbarChildType gtktoolbarchildtype_for_toolbarbuttontype(IToolbarButton::EType type)
73 {
74   switch(type)
75   {
76   case IToolbarButton::eSpace:
77     return GTK_TOOLBAR_CHILD_SPACE;
78   case IToolbarButton::eButton:
79     return GTK_TOOLBAR_CHILD_BUTTON;
80   case IToolbarButton::eToggleButton:
81     return GTK_TOOLBAR_CHILD_TOGGLEBUTTON;
82   case IToolbarButton::eRadioButton:
83     return GTK_TOOLBAR_CHILD_RADIOBUTTON;
84   }
85   ERROR_MESSAGE("invalid toolbar button type");
86   return (GtkToolbarChildType)0;
87 }
88
89 void toolbar_insert(GtkToolbar *toolbar, const char* icon, const char* text, const char* tooltip, IToolbarButton::EType type, GtkSignalFunc handler, gpointer data)
90 {
91   gtk_toolbar_append_element(toolbar, gtktoolbarchildtype_for_toolbarbuttontype(type), 0, text, tooltip, "", GTK_WIDGET(new_plugin_image(icon)), handler, data);
92 }
93
94 void ActivateToolbarButton(GtkWidget *widget, gpointer data)
95 {
96   const_cast<const IToolbarButton*>(reinterpret_cast<IToolbarButton*>(data))->activate();
97 }
98
99 void PlugInToolbar_AddButton(GtkToolbar* toolbar, const IToolbarButton* button)
100 {
101   toolbar_insert(toolbar, button->getImage(), button->getText(), button->getTooltip(), button->getType(), GTK_SIGNAL_FUNC(ActivateToolbarButton), reinterpret_cast<gpointer>(const_cast<IToolbarButton*>(button)));
102 }
103
104 GtkToolbar* g_plugin_toolbar = 0;
105
106 void PluginToolbar_populate()
107 {
108   class AddToolbarItemVisitor : public ToolbarModules::Visitor
109   {
110     GtkToolbar* m_toolbar;
111   public:
112     AddToolbarItemVisitor(GtkToolbar* toolbar)
113       : m_toolbar(toolbar)
114     {
115     }
116     void visit(const char* name, const _QERPlugToolbarTable& table) const
117     {
118       const std::size_t count = table.m_pfnToolbarButtonCount();
119       for(std::size_t i=0;i<count;++i)
120       {
121         PlugInToolbar_AddButton(m_toolbar, table.m_pfnGetToolbarButton(i));
122       }
123     }
124
125   } visitor(g_plugin_toolbar);
126
127   Radiant_getToolbarModules().foreachModule(visitor);
128 }
129
130 void PluginToolbar_clear()
131 {
132   container_remove_all(GTK_CONTAINER(g_plugin_toolbar));
133 }
134
135 GtkToolbar* create_plugin_toolbar()
136 {
137   GtkToolbar *toolbar;
138
139   toolbar = GTK_TOOLBAR(gtk_toolbar_new());
140   gtk_toolbar_set_orientation(toolbar, GTK_ORIENTATION_HORIZONTAL);
141   gtk_toolbar_set_style(toolbar, GTK_TOOLBAR_ICONS);
142   gtk_widget_show(GTK_WIDGET(toolbar));
143
144   g_plugin_toolbar = toolbar;
145
146   PluginToolbar_populate();
147
148   return toolbar;
149 }
150
151