TODO update
[geanyprj.git] / menu.c
blob4f4cfca7c60d76057ff847eb17b1158610b108a3
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>
23 #include "geany.h"
24 #include "support.h"
25 #include "prefs.h"
26 #include "plugindata.h"
27 #include "document.h"
28 #include "filetypes.h"
29 #include "keybindings.h"
30 #include "utils.h"
31 #include "pluginmacros.h"
33 #include "project.h"
35 #include "geanyprj.h"
37 PluginFields *plugin_fields;
38 extern GeanyData *geany_data;
41 static struct
43 GtkWidget *new_project;
44 GtkWidget *delete_project;
46 GtkWidget *add_file;
48 GtkWidget *preferences;
50 GtkWidget *find_in_files;
51 } menu_items;
53 // simple struct to keep references to the elements of the properties dialog
54 typedef struct _PropertyDialogElements
56 GtkWidget *dialog;
57 GtkWidget *name;
58 GtkWidget *description;
59 GtkWidget *file_name;
60 GtkWidget *base_path;
61 GtkWidget *make_in_base_path;
62 GtkWidget *run_cmd;
63 GtkWidget *regenerate;
64 GtkWidget *type;
65 GtkWidget *patterns;
66 } PropertyDialogElements;
68 static PropertyDialogElements *
69 build_properties_dialog(gboolean properties)
71 GtkWidget *vbox;
72 GtkWidget *table;
73 GtkWidget *image;
74 GtkWidget *button;
75 GtkWidget *bbox;
76 GtkWidget *label;
77 GtkTooltips *tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
78 PropertyDialogElements *e;
79 gint response;
80 gchar *dir = NULL;
81 gchar *basename = NULL;
82 gint idx;
83 gint i;
85 idx = p_document->get_cur_idx();
86 if (DOC_IDX_VALID(idx) &&
87 doc_list[idx].file_name != NULL && g_path_is_absolute(doc_list[idx].file_name))
89 dir = g_path_get_dirname(doc_list[idx].file_name);
91 else
93 dir = g_strdup("");
95 basename = g_path_get_basename(dir);
97 e = g_new0(PropertyDialogElements, 1);
99 if (properties)
101 e->dialog =
102 gtk_dialog_new_with_buttons(_("Project Preferences"),
103 GTK_WINDOW(app->window),
104 GTK_DIALOG_DESTROY_WITH_PARENT,
105 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
106 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
108 else
110 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(app->window),
111 GTK_DIALOG_DESTROY_WITH_PARENT,
112 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
113 NULL);
115 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
116 bbox = gtk_hbox_new(FALSE, 0);
117 button = gtk_button_new();
118 image = gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON);
119 label = gtk_label_new_with_mnemonic(_("C_reate"));
120 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 3);
121 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 3);
122 gtk_container_add(GTK_CONTAINER(button), bbox);
123 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
126 vbox = p_ui->dialog_vbox_new(GTK_DIALOG(e->dialog));
128 table = gtk_table_new(5, 2, FALSE);
129 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
130 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
132 label = gtk_label_new(_("Name:"));
133 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
135 e->name = gtk_entry_new();
136 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
137 gtk_entry_set_text(GTK_ENTRY(e->name), basename);
139 p_ui->table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
141 label = gtk_label_new(_("Location:"));
142 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
143 e->file_name = gtk_entry_new();
144 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
146 if (properties)
148 gtk_widget_set_sensitive(e->file_name, FALSE);
149 p_ui->table_add_row(GTK_TABLE(table), 1, label, e->file_name, NULL);
151 else
153 button = gtk_button_new();
154 image = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
155 gtk_container_add(GTK_CONTAINER(button), image);
156 bbox = p_ui->path_box_new(_("Choose Project Location"),
157 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
158 GTK_ENTRY(e->file_name));
159 gtk_entry_set_text(GTK_ENTRY(e->file_name), dir);
160 p_ui->table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
164 label = gtk_label_new(_("Base path:"));
165 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
167 e->base_path = gtk_entry_new();
168 gtk_tooltips_set_tip(tooltips, e->base_path,
169 _("Base directory of all files that make up the project. "
170 "This can be a new path, or an existing directory tree. "
171 "You can use paths relative to the project filename."), NULL);
172 bbox = p_ui->path_box_new(_("Choose Project Base Path"),
173 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
174 gtk_entry_set_text(GTK_ENTRY(e->base_path), dir);
176 p_ui->table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
178 label = gtk_label_new(_(""));
179 e->regenerate = gtk_check_button_new_with_label(_("Generate file list on load"));
180 gtk_tooltips_set_tip(tooltips, e->regenerate,
181 _("Automatically add files that match project type on project load "
182 "automaticly. You can't manually add/remove files if "
183 "you checked this option, since your modification will be lost on "
184 "on next project load"), NULL);
185 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(e->regenerate), TRUE);
186 p_ui->table_add_row(GTK_TABLE(table), 3, label, e->regenerate, NULL);
189 label = gtk_label_new(_("Type:"));
190 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
192 e->type = gtk_combo_box_new_text();
193 for (i = 0; i < NEW_PROJECT_TYPE_SIZE; i++)
194 gtk_combo_box_append_text(GTK_COMBO_BOX(e->type), project_type_string[i]);
195 gtk_combo_box_set_active(GTK_COMBO_BOX(e->type), 0);
197 p_ui->table_add_row(GTK_TABLE(table), 4, label, e->type, NULL);
199 gtk_container_add(GTK_CONTAINER(vbox), table);
200 g_free(dir);
201 g_free(basename);
203 return e;
206 void
207 on_new_project(GtkMenuItem * menuitem, gpointer user_data)
209 GtkWidget *vbox;
210 GtkWidget *table;
211 GtkWidget *image;
212 GtkWidget *button;
213 GtkWidget *bbox;
214 GtkWidget *label;
215 GtkTooltips *tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
216 PropertyDialogElements *e;
217 gint response;
218 gint idx;
219 gint i;
221 e = build_properties_dialog(FALSE);
222 gtk_widget_show_all(e->dialog);
224 retry:
225 response = gtk_dialog_run(GTK_DIALOG(e->dialog));
226 if (response == GTK_RESPONSE_OK)
228 gchar *path, *name, *base_path;
229 GSList *lst;
230 struct GeanyProject *prj;
232 path = g_build_filename(gtk_entry_get_text(GTK_ENTRY(e->file_name)), ".geanyprj",
233 NULL);
235 if (g_file_test(path, G_FILE_TEST_EXISTS))
237 p_ui->set_statusbar(TRUE, _("Project file \"%s\" already exists"), path);
238 g_free(path);
239 goto retry;
241 prj = (struct GeanyProject *) g_new0(struct GeanyProject, 1);
243 geany_project_set_path(prj, path);
244 geany_project_set_base_path(prj, gtk_entry_get_text(GTK_ENTRY(e->base_path)));
245 geany_project_set_name(prj, gtk_entry_get_text(GTK_ENTRY(e->name)));
246 geany_project_set_description(prj, "");
247 geany_project_set_run_cmd(prj, "");
248 geany_project_set_type_int(prj, gtk_combo_box_get_active(GTK_COMBO_BOX(e->type)));
249 geany_project_set_regenerate(prj,
250 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
251 (e->regenerate)));
253 geany_project_regenerate_file_list(prj);
255 geany_project_save(prj);
256 geany_project_free(prj);
257 p_document->open_file(path, FALSE, NULL, NULL);
260 gtk_widget_destroy(e->dialog);
261 g_free(e);
264 void
265 on_preferences(GtkMenuItem * menuitem, gpointer user_data)
267 GtkWidget *vbox;
268 GtkWidget *table;
269 GtkWidget *image;
270 GtkWidget *button;
271 GtkWidget *bbox;
272 GtkWidget *label;
273 GtkTooltips *tooltips = GTK_TOOLTIPS(p_support->lookup_widget(app->window, "tooltips"));
274 PropertyDialogElements *e;
275 gint response;
276 gint i;
277 gchar *project_dir;
279 e = build_properties_dialog(TRUE);
281 project_dir = g_path_get_dirname(g_current_project->path);
282 gtk_entry_set_text(GTK_ENTRY(e->file_name), project_dir);
283 gtk_entry_set_text(GTK_ENTRY(e->name), g_current_project->name);
284 gtk_entry_set_text(GTK_ENTRY(e->base_path), g_current_project->base_path);
285 gtk_combo_box_set_active(GTK_COMBO_BOX(e->type), g_current_project->type);
286 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(e->regenerate),
287 g_current_project->regenerate);
289 gtk_widget_show_all(e->dialog);
291 retry:
292 response = gtk_dialog_run(GTK_DIALOG(e->dialog));
293 if (response == GTK_RESPONSE_OK)
295 geany_project_set_base_path(g_current_project,
296 gtk_entry_get_text(GTK_ENTRY(e->base_path)));
297 geany_project_set_name(g_current_project, gtk_entry_get_text(GTK_ENTRY(e->name)));
298 geany_project_set_description(g_current_project, "");
299 geany_project_set_run_cmd(g_current_project, "");
300 geany_project_set_type_int(g_current_project,
301 gtk_combo_box_get_active(GTK_COMBO_BOX(e->type)));
302 geany_project_set_regenerate(g_current_project,
303 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON
304 (e->regenerate)));
305 geany_project_save(g_current_project);
307 if (g_current_project->regenerate)
309 geany_project_regenerate_file_list(g_current_project);
311 sidebar_refresh();
314 gtk_widget_destroy(e->dialog);
315 g_free(e);
316 g_free(project_dir);
319 void
320 on_delete_project(GtkMenuItem * menuitem, gpointer user_data)
322 gchar *path;
323 if (!g_current_project)
324 return;
326 if (p_dialogs->show_question("Do you really wish to delete current project:\n%s?",
327 g_current_project->name))
329 path = p_utils->get_locale_from_utf8(g_current_project->path);
330 xproject_close(FALSE);
331 unlink(path);
332 g_free(path);
336 void
337 on_add_file(GtkMenuItem * menuitem, gpointer user_data)
339 gint idx;
341 idx = p_document->get_cur_idx();
342 g_return_if_fail(DOC_IDX_VALID(idx) &&
343 doc_list[idx].file_name != NULL &&
344 g_path_is_absolute(doc_list[idx].file_name));
346 if (!g_current_project)
347 return;
349 xproject_add_file(doc_list[idx].file_name);
352 void
353 on_find_in_project(GtkMenuItem * menuitem, gpointer user_data)
355 gchar *dir;
356 if (!g_current_project)
357 return;
359 dir = g_path_get_dirname(g_current_project->path);
360 setptr(dir, g_build_filename(dir, g_current_project->base_path, NULL));
361 p_search->show_find_in_files_dialog(dir);
362 g_free(dir);
365 static void
366 update_menu_items()
368 gint idx;
369 gboolean cur_file_exists;
370 gboolean badd_file;
372 idx = p_document->get_cur_idx();
374 cur_file_exists = DOC_IDX_VALID(idx) &&
375 doc_list[idx].file_name != NULL && g_path_is_absolute(doc_list[idx].file_name);
377 badd_file = (g_current_project ? TRUE : FALSE) &&
378 cur_file_exists &&
379 !g_hash_table_lookup(g_current_project->tags, doc_list[idx].file_name);
381 gtk_widget_set_sensitive(menu_items.new_project, TRUE);
382 gtk_widget_set_sensitive(menu_items.delete_project, g_current_project ? TRUE : FALSE);
384 gtk_widget_set_sensitive(menu_items.add_file, badd_file);
386 gtk_widget_set_sensitive(menu_items.preferences, g_current_project ? TRUE : FALSE);
388 gtk_widget_set_sensitive(menu_items.find_in_files, g_current_project ? TRUE : FALSE);
391 void
392 tools_menu_init()
394 GtkWidget *item, *image;
396 GtkWidget *menu_prj = NULL;
397 GtkWidget *menu_prj_menu = NULL;
398 GtkTooltips *tooltips = NULL;
400 tooltips = gtk_tooltips_new();
402 menu_prj = gtk_image_menu_item_new_with_mnemonic(_("_Project"));
403 gtk_container_add(GTK_CONTAINER(geany_data->tools_menu), menu_prj);
405 g_signal_connect((gpointer) menu_prj, "activate", G_CALLBACK(update_menu_items), NULL);
407 menu_prj_menu = gtk_menu_new();
408 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_prj), menu_prj_menu);
413 image = gtk_image_new_from_stock(GTK_STOCK_NEW, GTK_ICON_SIZE_MENU);
414 item = gtk_image_menu_item_new_with_mnemonic(_("New Project"));
415 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
416 gtk_widget_show(item);
417 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
418 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_new_project), NULL);
419 menu_items.new_project = item;
421 image = gtk_image_new_from_stock(GTK_STOCK_DELETE, GTK_ICON_SIZE_MENU);
422 gtk_widget_show(image);
423 item = gtk_image_menu_item_new_with_mnemonic(_("Delete Project"));
424 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
425 gtk_widget_show(item);
426 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
427 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_delete_project), NULL);
428 menu_items.delete_project = item;
430 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
432 image = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
433 gtk_widget_show(image);
434 item = gtk_image_menu_item_new_with_mnemonic(_("Add File"));
435 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
436 gtk_widget_show(item);
437 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
438 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_add_file), NULL);
439 menu_items.add_file = item;
441 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
443 image = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_MENU);
444 gtk_widget_show(image);
445 item = gtk_image_menu_item_new_with_mnemonic(_("Preferences"));
446 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
447 gtk_widget_show(item);
448 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
449 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_preferences), NULL);
450 menu_items.preferences = item;
452 gtk_container_add(GTK_CONTAINER(menu_prj_menu), gtk_separator_menu_item_new());
454 image = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
455 gtk_widget_show(image);
456 item = gtk_image_menu_item_new_with_mnemonic(_("Find in Project"));
457 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
458 gtk_widget_show(item);
459 gtk_container_add(GTK_CONTAINER(menu_prj_menu), item);
460 g_signal_connect((gpointer) item, "activate", G_CALLBACK(on_find_in_project), NULL);
461 menu_items.find_in_files = item;
463 gtk_widget_show_all(menu_prj);
465 plugin_fields->menu_item = menu_prj;
466 plugin_fields->flags = PLUGIN_IS_DOCUMENT_SENSITIVE;
469 void
470 tools_menu_uninit()
472 gtk_widget_destroy(plugin_fields->menu_item);