]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/plugintoolbar.cpp
iqmmodel: better format description
[xonotic/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 #include <gtk/gtk.h>
25
26 #include "itoolbar.h"
27 #include "modulesystem.h"
28
29 #include "stream/stringstream.h"
30 #include "gtkutil/image.h"
31 #include "gtkutil/container.h"
32
33 #include "mainframe.h"
34 #include "plugin.h"
35
36 ui::Image new_plugin_image(const char *filename)
37 {
38     {
39         StringOutputStream fullpath(256);
40         fullpath << GameToolsPath_get() << g_pluginsDir << "bitmaps/" << filename;
41         if (auto image = image_new_from_file_with_mask(fullpath.c_str())) { return image; }
42     }
43
44     {
45         StringOutputStream fullpath(256);
46         fullpath << AppPath_get() << g_pluginsDir << "bitmaps/" << filename;
47         if (auto image = image_new_from_file_with_mask(fullpath.c_str())) { return image; }
48     }
49
50     {
51         StringOutputStream fullpath(256);
52         fullpath << AppPath_get() << g_modulesDir << "bitmaps/" << filename;
53         if (auto image = image_new_from_file_with_mask(fullpath.c_str())) { return image; }
54     }
55
56     return image_new_missing();
57 }
58
59 void
60 toolbar_insert(ui::Toolbar toolbar, const char *icon, const char *text, const char *tooltip, IToolbarButton::EType type,
61                GCallback handler, gpointer data)
62 {
63     if (type == IToolbarButton::eSpace) {
64         auto it = ui::ToolItem::from(gtk_separator_tool_item_new());
65         it.show();
66         toolbar.add(it);
67         return;
68     }
69     if (type == IToolbarButton::eButton) {
70         auto button = ui::ToolButton::from(gtk_tool_button_new(new_plugin_image(icon), text));
71         gtk_widget_set_tooltip_text(button, tooltip);
72         gtk_widget_show_all(button);
73         button.connect("clicked", G_CALLBACK(handler), data);
74         toolbar.add(button);
75         return;
76     }
77     if (type == IToolbarButton::eToggleButton) {
78         auto button = ui::ToolButton::from(gtk_toggle_tool_button_new());
79         gtk_tool_button_set_icon_widget(button, new_plugin_image(icon));
80         gtk_tool_button_set_label(button, text);
81         gtk_widget_set_tooltip_text(button, tooltip);
82         gtk_widget_show_all(button);
83         button.connect("toggled", G_CALLBACK(handler), data);
84         toolbar.add(button);
85         return;
86     }
87     ERROR_MESSAGE("invalid toolbar button type");
88 }
89
90 void ActivateToolbarButton(ui::ToolButton widget, gpointer data)
91 {
92     (const_cast<const IToolbarButton *>( reinterpret_cast<IToolbarButton *>( data )))->activate();
93 }
94
95 void PlugInToolbar_AddButton(ui::Toolbar toolbar, const IToolbarButton *button)
96 {
97     toolbar_insert(toolbar, button->getImage(), button->getText(), button->getTooltip(), button->getType(),
98                    G_CALLBACK(ActivateToolbarButton),
99                    reinterpret_cast<gpointer>( const_cast<IToolbarButton *>( button )));
100 }
101
102 ui::Toolbar g_plugin_toolbar{ui::null};
103
104 void PluginToolbar_populate()
105 {
106     class AddToolbarItemVisitor : public ToolbarModules::Visitor {
107         ui::Toolbar m_toolbar;
108     public:
109         AddToolbarItemVisitor(ui::Toolbar toolbar)
110                 : m_toolbar(toolbar)
111         {
112         }
113
114         void visit(const char *name, const _QERPlugToolbarTable &table) const
115         {
116             const std::size_t count = table.m_pfnToolbarButtonCount();
117             for (std::size_t i = 0; i < count; ++i) {
118                 PlugInToolbar_AddButton(m_toolbar, table.m_pfnGetToolbarButton(i));
119             }
120         }
121
122     } visitor(g_plugin_toolbar);
123
124     Radiant_getToolbarModules().foreachModule(visitor);
125 }
126
127 void PluginToolbar_clear()
128 {
129     container_remove_all(g_plugin_toolbar);
130 }
131
132 ui::Toolbar create_plugin_toolbar()
133 {
134
135     auto toolbar = ui::Toolbar::from(gtk_toolbar_new());
136     gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
137     gtk_toolbar_set_style(toolbar, GTK_TOOLBAR_ICONS);
138     toolbar.show();
139
140     g_plugin_toolbar = toolbar;
141
142     PluginToolbar_populate();
143
144     return toolbar;
145 }