1 /*
2  * Copyright 2017 LarsGit223
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /*
20  * Code for the "Workbench" menu.
21  */
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 
26 #include "wb_globals.h"
27 #include "dialogs.h"
28 #include "menu.h"
29 #include "sidebar.h"
30 #include "search_projects.h"
31 
32 extern GeanyPlugin *geany_plugin;
33 
34 typedef struct
35 {
36 	GtkWidget *menu;
37 	GtkWidget *root_item;
38 	GtkWidget *item_new;
39 	GtkWidget *item_open;
40 	GtkWidget *item_settings;
41 	GtkWidget *item_search_projects;
42 	GtkWidget *item_close;
43 }WB_MENU_DATA;
44 static WB_MENU_DATA menu_data;
45 
46 /** Set the context of the workbench menu.
47  *
48  * The context set controls which items in the menu will be active and
49  * which will be deactive.
50  *
51  * @param context The context/situation in which the menu is/shall be
52  *
53  **/
menu_set_context(MENU_CONTEXT context)54 void menu_set_context(MENU_CONTEXT context)
55 {
56 	switch (context)
57 	{
58 		case MENU_CONTEXT_WB_CREATED:
59 		case MENU_CONTEXT_WB_OPENED:
60 			gtk_widget_set_sensitive(menu_data.item_new, FALSE);
61 			gtk_widget_set_sensitive(menu_data.item_open, FALSE);
62 			gtk_widget_set_sensitive(menu_data.item_settings, TRUE);
63 			gtk_widget_set_sensitive(menu_data.item_search_projects, TRUE);
64 			gtk_widget_set_sensitive(menu_data.item_close, TRUE);
65 		break;
66 		case MENU_CONTEXT_SEARCH_PROJECTS_SCANING:
67 			gtk_widget_set_sensitive(menu_data.item_new, FALSE);
68 			gtk_widget_set_sensitive(menu_data.item_open, FALSE);
69 			gtk_widget_set_sensitive(menu_data.item_settings, TRUE);
70 			gtk_widget_set_sensitive(menu_data.item_search_projects, FALSE);
71 			gtk_widget_set_sensitive(menu_data.item_close, FALSE);
72 		break;
73 		case MENU_CONTEXT_WB_CLOSED:
74 			gtk_widget_set_sensitive(menu_data.item_new, TRUE);
75 			gtk_widget_set_sensitive(menu_data.item_open, TRUE);
76 			gtk_widget_set_sensitive(menu_data.item_settings, FALSE);
77 			gtk_widget_set_sensitive(menu_data.item_search_projects, FALSE);
78 			gtk_widget_set_sensitive(menu_data.item_close, FALSE);
79 		break;
80 	}
81 }
82 
83 /* The function handles the menu item "New workbench" */
item_new_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)84 static void item_new_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
85 {
86 	gchar *filename;
87 	GError *error = NULL;
88 
89 	filename = dialogs_create_new_workbench();
90 	if (filename == NULL)
91 	{
92 		return;
93 	}
94 	wb_globals.opened_wb = workbench_new();
95 	workbench_set_filename(wb_globals.opened_wb, filename);
96 	if (workbench_save(wb_globals.opened_wb, &error))
97 	{
98 		menu_set_context(MENU_CONTEXT_WB_CREATED);
99 		sidebar_update(SIDEBAR_CONTEXT_WB_CREATED, NULL);
100 	}
101 	else
102 	{
103 		dialogs_show_msgbox(GTK_MESSAGE_INFO, _("Could not create new workbench file: %s"), error->message);
104 		workbench_free(wb_globals.opened_wb);
105 		wb_globals.opened_wb = NULL;
106 	}
107 	g_free(filename);
108 }
109 
110 
111 /* The function handles the menu item "Open workbench" */
item_open_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)112 static void item_open_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
113 {
114 	gchar *filename;
115 	GError *error = NULL;
116 
117 	filename = dialogs_open_workbench();
118 	if (filename == NULL)
119 	{
120 		return;
121 	}
122 	wb_globals.opened_wb = workbench_new();
123 	if (workbench_load(wb_globals.opened_wb, filename, &error))
124 	{
125 		menu_set_context(MENU_CONTEXT_WB_OPENED);
126 		sidebar_update(SIDEBAR_CONTEXT_WB_OPENED, NULL);
127 	}
128 	else
129 	{
130 		dialogs_show_msgbox(GTK_MESSAGE_INFO, _("Could not open workbench file: %s"), error->message);
131 		workbench_free(wb_globals.opened_wb);
132 		wb_globals.opened_wb = NULL;
133 	}
134 	g_free(filename);
135 }
136 
137 
138 /* The function handles the menu item "Settings" */
item_workbench_settings_activate_cb(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)139 static void item_workbench_settings_activate_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
140 {
141 	if (wb_globals.opened_wb != NULL)
142 	{
143 		gboolean enable_live_update_old, enable_live_update;
144 
145 		enable_live_update_old = workbench_get_enable_live_update(wb_globals.opened_wb);
146 		if (dialogs_workbench_settings(wb_globals.opened_wb))
147 		{
148 			GError *error = NULL;
149 
150 			sidebar_update(SIDEBAR_CONTEXT_WB_SETTINGS_CHANGED, NULL);
151 
152 			/* Save the new settings to the workbench file (.geanywb). */
153 			if (!workbench_save(wb_globals.opened_wb, &error))
154 			{
155 				dialogs_show_msgbox(GTK_MESSAGE_INFO, _("Could not save workbench file: %s"), error->message);
156 			}
157 			sidebar_update(SIDEBAR_CONTEXT_WB_SAVED, NULL);
158 
159 			enable_live_update = workbench_get_enable_live_update(wb_globals.opened_wb);
160 			if (enable_live_update != enable_live_update_old)
161 			{
162 				if (enable_live_update == TRUE)
163 				{
164 					/* Start/create all file monitors. */
165 					workbench_enable_live_update(wb_globals.opened_wb);
166 				}
167 				else
168 				{
169 					/* Stop/free all file monitors. */
170 					workbench_disable_live_update(wb_globals.opened_wb);
171 				}
172 			}
173 		}
174 	}
175 }
176 
177 
178 /* The function handles the menu item "Search projects" */
item_workbench_search_projects_activate_cb(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)179 static void item_workbench_search_projects_activate_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
180 {
181 	if (wb_globals.opened_wb != NULL)
182 	{
183 		search_projects(wb_globals.opened_wb);
184 	}
185 	sidebar_update(SIDEBAR_CONTEXT_PROJECT_ADDED, NULL);
186 }
187 
188 
189 /* The function handles the menu item "Close workbench" */
item_close_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer user_data)190 static void item_close_workbench_activate_cb(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
191 {
192 	workbench_free(wb_globals.opened_wb);
193 	wb_globals.opened_wb = NULL;
194 
195 	menu_set_context(MENU_CONTEXT_WB_CLOSED);
196 	sidebar_update(SIDEBAR_CONTEXT_WB_CLOSED, NULL);
197 }
198 
199 
200 /** Setup the workbench menu.
201  *
202  **/
menu_init(void)203 gboolean menu_init(void)
204 {
205 	/* Create menu and root item/label */
206 	menu_data.menu = gtk_menu_new();
207 	menu_data.root_item = gtk_menu_item_new_with_label(_("Workbench"));
208 	gtk_widget_show(menu_data.root_item);
209 
210 	/* Create new menu item "New Workbench" */
211 	menu_data.item_new = gtk_menu_item_new_with_mnemonic(_("_New..."));
212 	gtk_widget_show(menu_data.item_new);
213 	gtk_menu_shell_append(GTK_MENU_SHELL (menu_data.menu), menu_data.item_new);
214 	g_signal_connect(menu_data.item_new, "activate",
215 					 G_CALLBACK(item_new_workbench_activate_cb), NULL);
216 
217 	/* Create new menu item "Open Workbench" */
218 	menu_data.item_open = gtk_menu_item_new_with_mnemonic(_("_Open..."));
219 	gtk_widget_show(menu_data.item_open);
220 	gtk_menu_shell_append(GTK_MENU_SHELL (menu_data.menu), menu_data.item_open);
221 	g_signal_connect(menu_data.item_open, "activate",
222 					 G_CALLBACK(item_open_workbench_activate_cb), NULL);
223 
224 	/* Create new menu item "Workbench Settings" */
225 	menu_data.item_settings = gtk_menu_item_new_with_mnemonic(_("S_ettings"));
226 	gtk_widget_show(menu_data.item_settings);
227 	gtk_menu_shell_append(GTK_MENU_SHELL (menu_data.menu), menu_data.item_settings);
228 	g_signal_connect(menu_data.item_settings, "activate",
229 					 G_CALLBACK(item_workbench_settings_activate_cb), NULL);
230 
231 	/* Create new menu item "Search Projects" */
232 	menu_data.item_search_projects = gtk_menu_item_new_with_mnemonic(_("Search _projects"));
233 	gtk_widget_show(menu_data.item_search_projects);
234 	gtk_menu_shell_append(GTK_MENU_SHELL (menu_data.menu), menu_data.item_search_projects);
235 	g_signal_connect(menu_data.item_search_projects, "activate",
236 					 G_CALLBACK(item_workbench_search_projects_activate_cb), NULL);
237 
238 	/* Create new menu item "Close Workbench" */
239 	menu_data.item_close = gtk_menu_item_new_with_mnemonic(_("_Close"));
240 	gtk_widget_show(menu_data.item_close);
241 	gtk_menu_shell_append(GTK_MENU_SHELL (menu_data.menu), menu_data.item_close);
242 	g_signal_connect(menu_data.item_close, "activate",
243 					 G_CALLBACK(item_close_workbench_activate_cb), NULL);
244 
245 	/* Add our menu to the main window (left of the help menu) */
246 	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_data.root_item), menu_data.menu);
247 	gtk_container_add(GTK_CONTAINER(wb_globals.geany_plugin->geany_data->main_widgets->tools_menu), menu_data.root_item);
248 
249 	return TRUE;
250 }
251 
252 
253 /** Cleanup menu data/mem.
254  *
255  **/
menu_cleanup(void)256 void menu_cleanup (void)
257 {
258 	gtk_widget_destroy(menu_data.root_item);
259 }
260