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