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