compilation fixes for geany svn
[geanyprj.git] / src / menu.c
blob1b9285173c693b651a1c45bcd14e6f8882f56038
1 /*
2 * geanyprj - Alternative project support for geany light IDE.
4 * Copyright 2008 Yura Siamashka <yurand2@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/time.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <glib/gstdio.h>
24 #include "geany.h"
25 #include "support.h"
26 #include "prefs.h"
27 #include "plugindata.h"
28 #include "document.h"
29 #include "filetypes.h"
30 #include "keybindings.h"
31 #include "utils.h"
32 #include "pluginmacros.h"
34 #include "project.h"
36 #include "geanyprj.h"
38 PluginFields *plugin_fields;
39 extern GeanyData *geany_data;
40 extern GeanyFunctions *geany_functions;
43 static struct
45 GtkWidget *new_project;
46 GtkWidget *delete_project;
48 GtkWidget *add_file;
50 GtkWidget *preferences;
52 GtkWidget *find_in_files;
53 } menu_items;
55 // simple struct to keep references to the elements of the properties dialog
56 typedef struct _PropertyDialogElements
58 GtkWidget *dialog;
59 GtkWidget *name;
60 GtkWidget *description;
61 GtkWidget *file_name;
62 GtkWidget *base_path;
63 GtkWidget *make_in_base_path;
64 GtkWidget *run_cmd;
65 GtkWidget *regenerate;
66 GtkWidget *type;
67 GtkWidget *patterns;
68 } PropertyDialogElements;
70 static PropertyDialogElements *
71 build_properties_dialog(gboolean properties)
73 GtkWidget *vbox;
74 GtkWidget *table;
75 GtkWidget *image;
76 GtkWidget *button;
77 GtkWidget *bbox;
78 GtkWidget *label;
79 GtkTooltips *tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
80 PropertyDialogElements *e;
81 gchar *dir = NULL;
82 gchar *basename = NULL;
83 gint idx;
84 gint i;
86 idx = p_document->get_cur_idx();
87 if (DOC_IDX_VALID(idx) &&
88 doc_list[idx].file_name != NULL && g_path_is_absolute(doc_list[idx].file_name))
90 dir = g_path_get_dirname(doc_list[idx].file_name);
92 else
94 dir = g_strdup("");
96 basename = g_path_get_basename(dir);
98 e = g_new0(PropertyDialogElements, 1);
100 if (properties)
102 e->dialog =
103 gtk_dialog_new_with_buttons(_("Project Preferences"),
104 GTK_WINDOW(app->window),
105 GTK_DIALOG_DESTROY_WITH_PARENT,
106 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
107 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
109 else
111 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(app->window),
112 GTK_DIALOG_DESTROY_WITH_PARENT,
113 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
114 NULL);
116 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
117 bbox = gtk_hbox_new(FALSE, 0);
118 button = gtk_button_new();
119 image = gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON);
120 label = gtk_label_new_with_mnemonic(_("C_reate"));
121 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 3);
122 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 3);
123 gtk_container_add(GTK_CONTAINER(button), bbox);
124 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
127 vbox = p_ui->dialog_vbox_new(GTK_DIALOG(e->dialog));
129 table = gtk_table_new(5, 2, FALSE);
130 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
131 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
133 label = gtk_label_new(_("Name:"));
134 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
136 e->name = gtk_entry_new();
137 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
138 gtk_entry_set_text(GTK_ENTRY(e->name), basename);
140 p_ui->table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
142 label = gtk_label_new(_("Location:"));
143 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
144 e->file_name = gtk_entry_new();
145 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
147 if (properties)
149 gtk_widget_set_sensitive(e->file_name, FALSE);
150 p_ui->table_add_row(GTK_TABLE(table), 1, label, e->file_name, NULL);
152 else
154 button = gtk_button_new();
155 image = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
156 gtk_container_add(GTK_CONTAINER(button), image);
157 bbox = p_ui->path_box_new(_("Choose Project Location"),
158 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
159 GTK_ENTRY(e->file_name));
160 gtk_entry_set_text(GTK_ENTRY(e->file_name), dir);
161 p_ui->table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
165 label = gtk_label_new(_("Base path:"));
166 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
168 e->base_path = gtk_entry_new();
169 gtk_tooltips_set_tip(tooltips, e->base_path,
170 _("Base directory of all files that make up the project. "
171 "This can be a new path, or an existing directory tree. "
172 "You can use paths relative to the project filename."), NULL);
173 bbox = p_ui->path_box_new(_("Choose Project Base Path"),
174 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
175 gtk_entry_set_text(GTK_ENTRY(e->base_path), dir);
177 p_ui->table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
179 label = gtk_label_new(_(""));
180 e->regenerate = gtk_check_button_new_with_label(_("Generate file list on load"));
181 gtk_tooltips_set_tip(tooltips, e->regenerate,
182 _("Automatically add files that match project type on project load "
183 "automaticly. You can't manually add/remove files if "
184 "you checked this option, since your modification will be lost on "
185 "on next project load"), NULL);
186 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(e->regenerate), TRUE);
187 p_ui->table_add_row(GTK_TABLE(table), 3, label, e->regenerate, NULL);
190 label = gtk_label_new(_("Type:"));
191 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
193 e->type = gtk_combo_box_new_text();
194 for (i = 0; i < NEW_PROJECT_TYPE_SIZE; i++)
195 gtk_combo_box_append_text(GTK_COMBO_BOX(e->type), project_type_string[i]);
196 gtk_combo_box_set_active(GTK_COMBO_BOX(e->type), 0);
198 p_ui->table_add_row(GTK_TABLE(table), 4, label, e->type, NULL);
200 gtk_container_add(GTK_CONTAINER(vbox), table);
201 g_free(dir);
202 g_free(basename);
204 return e;
207 void
208 on_new_project(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data)
210 PropertyDialogElements *e;
211 gint response;
213 e = build_properties_dialog(FALSE);
214 gtk_widget_show_all(e->dialog);
216 retry:
217 response = gtk_dialog_run(GTK_DIALOG(e->dialog));
218 if (response == GTK_RESPONSE_OK)
220 gchar *path;
221 struct GeanyProject *prj;
223 path = g_build_filename(gtk_entry_get_text(GTK_ENTRY(e->file_name)), ".geanyprj",
224 NULL);
226 if (g_file_test(path, G_FILE_TEST_EXISTS))
228 p_ui->set_statusbar(TRUE, _("Project file \"%s\" already exists"), path);
229 g_free(path);
230 goto retry;
232 prj = geany_project_new();
234 geany_project_set_path(prj, path);
235 geany_project_set_base_path(prj, gtk_entry_get_text(GTK_ENTRY(e->base_path)));
236 geany_project_set_name(prj, gtk_entry_get_text(GTK_ENTRY(e->name)));
237 geany_project_set_description(prj, "");
238 geany_project_set_run_cmd(prj, "");
239 geany_project_set_type_int(prj, gtk_combo_box_get_active(GTK_COMBO_BOX(e->type)));
240 geany_project_set_regenerate(prj,
241 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
242 (e->regenerate)));
244 geany_project_regenerate_file_list(prj);
246 geany_project_save(prj);
247 geany_project_free(prj);
248 p_document->open_file(path, FALSE, NULL, NULL);
251 gtk_widget_destroy(e->dialog);
252 g_free(e);
255 void
256 on_preferences(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data)
258 PropertyDialogElements *e;
259 gint response;
260 gchar *project_dir;
262 e = build_properties_dialog(TRUE);
264 project_dir = g_path_get_dirname(g_current_project->path);
265 gtk_entry_set_text(GTK_ENTRY(e->file_name), project_dir);
266 gtk_entry_set_text(GTK_ENTRY(e->name), g_current_project->name);
267 gtk_entry_set_text(GTK_ENTRY(e->base_path), g_current_project->base_path);
268 gtk_combo_box_set_active(GTK_COMBO_BOX(e->type), g_current_project->type);
269 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(e->regenerate),
270 g_current_project->regenerate);
272 gtk_widget_show_all(e->dialog);
274 response = gtk_dialog_run(GTK_DIALOG(e->dialog));
275 if (response == GTK_RESPONSE_OK)
277 geany_project_set_base_path(g_current_project,
278 gtk_entry_get_text(GTK_ENTRY(e->base_path)));
279 geany_project_set_name(g_current_project, gtk_entry_get_text(GTK_ENTRY(e->name)));
280 geany_project_set_description(g_current_project, "");
281 geany_project_set_run_cmd(g_current_project, "");
282 geany_project_set_type_int(g_current_project,
283 gtk_combo_box_get_active(GTK_COMBO_BOX(e->type)));
284 geany_project_set_regenerate(g_current_project,
285 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
286 (e->regenerate)));
287 geany_project_save(g_current_project);
289 if (g_current_project->regenerate)
291 geany_project_regenerate_file_list(g_current_project);
293 sidebar_refresh();
296 gtk_widget_destroy(e->dialog);
297 g_free(e);
298 g_free(project_dir);
301 void
302 on_delete_project(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data)
304 gchar *path;
305 if (!g_current_project)
306 return;
308 if (p_dialogs->show_question("Do you really wish to delete current project:\n%s?",
309 g_current_project->name))
311 path = p_utils->get_locale_from_utf8(g_current_project->path);
312 xproject_close(FALSE);
313 g_unlink(path);
314 g_free(path);
318 void
319 on_add_file(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data)
321 gint idx;
323 idx = p_document->get_cur_idx();
324 g_return_if_fail(DOC_IDX_VALID(idx) &&
325 doc_list[idx].file_name != NULL &&
326 g_path_is_absolute(doc_list[idx].file_name));
328 if (!g_current_project)
329 return;
331 xproject_add_file(doc_list[idx].file_name);
334 void
335 on_find_in_project(G_GNUC_UNUSED GtkMenuItem * menuitem, G_GNUC_UNUSED gpointer user_data)
337 gchar *dir;
338 if (!g_current_project)
339 return;
341 dir = g_path_get_dirname(g_current_project->path);
342 setptr(dir, g_build_filename(dir, g_current_project->base_path, NULL));
343 p_search->show_find_in_files_dialog(dir);
344 g_free(dir);
347 static void
348 update_menu_items()
350 gint idx;
351 gboolean cur_file_exists;
352 gboolean badd_file;
354 idx = p_document->get_cur_idx();
356 cur_file_exists = DOC_IDX_VALID(idx) &&
357 doc_list[idx].file_name != NULL && g_path_is_absolute(doc_list[idx].file_name);
359 badd_file = (g_current_project ? TRUE : FALSE) &&
360 !g_current_project->regenerate &&
361 cur_file_exists &&
362 !g_hash_table_lookup(g_current_project->tags, doc_list[idx].file_name);
364 gtk_widget_set_sensitive(menu_items.new_project, TRUE);
365 gtk_widget_set_sensitive(menu_items.delete_project, g_current_project ? TRUE : FALSE);
367 gtk_widget_set_sensitive(menu_items.add_file, badd_file);
369 gtk_widget_set_sensitive(menu_items.preferences, g_current_project ? TRUE : FALSE);
371 gtk_widget_set_sensitive(menu_items.find_in_files, g_current_project ? TRUE : FALSE);
374 void
375 tools_menu_init()
377 GtkWidget *item, *image;
379 GtkWidget *menu_prj = NULL;
380 GtkWidget *menu_prj_menu = NULL;
381 GtkTooltips *tooltips = NULL;
383 tooltips = gtk_tooltips_new();
385 menu_prj = gtk_image_menu_item_new_with_mnemonic(_("_Project"));
386 gtk_container_add(GTK_CONTAINER(geany_data->tools_menu), menu_prj);
388 g_signal_connect((gpointer) menu_prj, "activate", G_CALLBACK(update_menu_items), NULL);
390 menu_prj_menu = gtk_menu_new();
391 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_prj), menu_prj_menu);
396 image = gtk_image_new_from_stock(GTK_STOCK_NEW, GTK_ICON_SIZE_MENU);
397 item = gtk_image_menu_item_new_with_mnemonic(_("New Project"));
398 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
399 gtk_widget_show(item);
400 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
401 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_new_project), NULL);
402 menu_items.new_project = item;
404 image = gtk_image_new_from_stock(GTK_STOCK_DELETE, GTK_ICON_SIZE_MENU);
405 gtk_widget_show(image);
406 item = gtk_image_menu_item_new_with_mnemonic(_("Delete Project"));
407 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
408 gtk_widget_show(item);
409 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
410 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_delete_project), NULL);
411 menu_items.delete_project = item;
413 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
415 image = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
416 gtk_widget_show(image);
417 item = gtk_image_menu_item_new_with_mnemonic(_("Add File"));
418 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
419 gtk_widget_show(item);
420 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
421 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_add_file), NULL);
422 menu_items.add_file = item;
424 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
426 image = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU);
427 gtk_widget_show(image);
428 item = gtk_image_menu_item_new_with_mnemonic(_("Preferences"));
429 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
430 gtk_widget_show(item);
431 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
432 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_preferences), NULL);
433 menu_items.preferences = item;
435 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
437 image = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
438 gtk_widget_show(image);
439 item = gtk_image_menu_item_new_with_mnemonic(_("Find in Project"));
440 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
441 gtk_widget_show(item);
442 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
443 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_find_in_project), NULL);
444 menu_items.find_in_files = item;
446 gtk_widget_show_all(menu_prj);
448 plugin_fields->menu_item = menu_prj;
449 plugin_fields->flags = PLUGIN_IS_DOCUMENT_SENSITIVE;
452 void
453 tools_menu_uninit()
455 gtk_widget_destroy(plugin_fields->menu_item);