2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program 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.
12 * This program 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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 #include "projectprivate.h"
41 #include "msgwindow.h"
46 #include "interface.h"
50 #include "filetypes.h"
53 ProjectPrefs project_prefs
= { NULL
, FALSE
, FALSE
};
56 static GeanyProjectPrivate priv
;
57 static GeanyIndentPrefs indentation
;
59 static StashGroup
*indent_group
= NULL
;
63 gchar
*project_file_path
; /* in UTF-8 */
64 } local_prefs
= { NULL
};
67 static gboolean entries_modified
;
69 /* simple struct to keep references to the elements of the properties dialog */
70 typedef struct _PropertyDialogElements
75 GtkWidget
*description
;
79 BuildTableData build_properties
;
80 } PropertyDialogElements
;
83 static gboolean
update_config(const PropertyDialogElements
*e
);
84 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
);
85 static gboolean
load_config(const gchar
*filename
);
86 static gboolean
write_config(gboolean emit_signal
);
87 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
88 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
89 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
);
90 static void apply_editor_prefs(void);
93 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
94 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
95 #define MAX_NAME_LEN 50
96 /* "projects" is part of the default project base path so be careful when translating
97 * please avoid special characters and spaces, look at the source for details or ask Frank */
98 #define PROJECT_DIR _("projects")
101 void project_new(void)
109 PropertyDialogElements
*e
;
111 if (! project_ask_close())
114 g_return_if_fail(app
->project
== NULL
);
116 e
= g_new0(PropertyDialogElements
, 1);
117 e
->dialog
= gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets
.window
),
118 GTK_DIALOG_DESTROY_WITH_PARENT
,
119 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
121 gtk_widget_set_name(e
->dialog
, "GeanyDialogProject");
122 bbox
= gtk_hbox_new(FALSE
, 0);
123 button
= gtk_button_new();
124 image
= gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON
);
125 label
= gtk_label_new_with_mnemonic(_("C_reate"));
126 gtk_box_pack_start(GTK_BOX(bbox
), image
, FALSE
, FALSE
, 3);
127 gtk_box_pack_start(GTK_BOX(bbox
), label
, FALSE
, FALSE
, 3);
128 gtk_container_add(GTK_CONTAINER(button
), bbox
);
129 gtk_dialog_add_action_widget(GTK_DIALOG(e
->dialog
), button
, GTK_RESPONSE_OK
);
131 vbox
= ui_dialog_vbox_new(GTK_DIALOG(e
->dialog
));
133 entries_modified
= FALSE
;
135 table
= gtk_table_new(3, 2, FALSE
);
136 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
137 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
139 label
= gtk_label_new(_("Name:"));
140 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
142 e
->name
= gtk_entry_new();
143 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
144 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
146 ui_table_add_row(GTK_TABLE(table
), 0, label
, e
->name
, NULL
);
148 label
= gtk_label_new(_("Filename:"));
149 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
151 e
->file_name
= gtk_entry_new();
152 ui_entry_add_clear_icon(GTK_ENTRY(e
->file_name
));
153 gtk_entry_set_width_chars(GTK_ENTRY(e
->file_name
), 30);
154 button
= gtk_button_new();
155 g_signal_connect(button
, "clicked", G_CALLBACK(on_file_save_button_clicked
), e
);
156 image
= gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON
);
157 gtk_container_add(GTK_CONTAINER(button
), image
);
158 bbox
= gtk_hbox_new(FALSE
, 6);
159 gtk_box_pack_start_defaults(GTK_BOX(bbox
), e
->file_name
);
160 gtk_box_pack_start(GTK_BOX(bbox
), button
, FALSE
, FALSE
, 0);
162 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 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
169 ui_widget_set_tooltip_text(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."));
173 bbox
= ui_path_box_new(_("Choose Project Base Path"),
174 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
->base_path
));
176 ui_table_add_row(GTK_TABLE(table
), 2, label
, bbox
, NULL
);
178 gtk_container_add(GTK_CONTAINER(vbox
), table
);
181 g_signal_connect(e
->name
, "changed", G_CALLBACK(on_name_entry_changed
), e
);
182 /* run the callback manually to initialise the base_path and file_name fields */
183 on_name_entry_changed(GTK_EDITABLE(e
->name
), e
);
185 g_signal_connect(e
->file_name
, "changed", G_CALLBACK(on_entries_changed
), e
);
186 g_signal_connect(e
->base_path
, "changed", G_CALLBACK(on_entries_changed
), e
);
188 gtk_widget_show_all(e
->dialog
);
190 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
)
192 if (update_config(e
))
195 ui_set_statusbar(TRUE
, _("Project \"%s\" created."), app
->project
->name
);
197 ui_add_recent_project_file(app
->project
->file_name
);
201 gtk_widget_destroy(e
->dialog
);
206 gboolean
project_load_file_with_session(const gchar
*locale_file_name
)
208 if (project_load_file(locale_file_name
))
210 if (project_prefs
.project_session
)
212 configuration_open_files();
213 /* open a new file if no other file was opened */
214 document_new_file_if_non_open();
223 static void run_open_dialog(GtkDialog
*dialog
)
225 while (gtk_dialog_run(dialog
) == GTK_RESPONSE_ACCEPT
)
227 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
229 /* try to load the config */
230 if (! project_load_file_with_session(filename
))
232 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
234 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename
);
235 gtk_widget_grab_focus(GTK_WIDGET(dialog
));
236 g_free(utf8_filename
);
247 void project_open(void)
249 const gchar
*dir
= local_prefs
.project_file_path
;
254 GtkFileFilter
*filter
;
257 if (! project_ask_close()) return;
260 file
= win32_show_project_open_dialog(main_widgets
.window
, _("Open Project"), dir
, FALSE
, TRUE
);
263 /* try to load the config */
264 if (! project_load_file_with_session(file
))
266 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file
);
272 dialog
= gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets
.window
),
273 GTK_FILE_CHOOSER_ACTION_OPEN
,
274 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
275 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
276 gtk_widget_set_name(dialog
, "GeanyDialogProject");
278 /* set default Open, so pressing enter can open multiple files */
279 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
280 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
281 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
282 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
283 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(main_widgets
.window
));
284 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog
), TRUE
);
286 /* add FileFilters */
287 filter
= gtk_file_filter_new();
288 gtk_file_filter_set_name(filter
, _("All files"));
289 gtk_file_filter_add_pattern(filter
, "*");
290 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
291 filter
= gtk_file_filter_new();
292 gtk_file_filter_set_name(filter
, _("Project files"));
293 gtk_file_filter_add_pattern(filter
, "*." GEANY_PROJECT_EXT
);
294 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
295 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog
), filter
);
297 locale_path
= utils_get_locale_from_utf8(dir
);
298 if (g_file_test(locale_path
, G_FILE_TEST_EXISTS
) &&
299 g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
301 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
305 gtk_widget_show_all(dialog
);
306 run_open_dialog(GTK_DIALOG(dialog
));
307 gtk_widget_destroy(GTK_WIDGET(dialog
));
312 /* Called when creating, opening, closing and updating projects. */
313 static void update_ui(void)
315 if (main_status
.quitting
)
318 ui_set_window_title(NULL
);
319 build_menu_update(NULL
);
320 sidebar_openfiles_update_all();
324 static void remove_foreach_project_filetype(gpointer data
, gpointer user_data
)
326 GeanyFiletype
*ft
= data
;
329 setptr(ft
->projfilecmds
, NULL
);
330 setptr(ft
->projexeccmds
, NULL
);
331 setptr(ft
->projerror_regex_string
, NULL
);
332 ft
->project_list_entry
= -1;
337 /* open_default will make function reload default session files on close */
338 void project_close(gboolean open_default
)
340 g_return_if_fail(app
->project
!= NULL
);
342 ui_set_statusbar(TRUE
, _("Project \"%s\" closed."), app
->project
->name
);
344 /* use write_config() to save project session files */
347 /* remove project filetypes build entries */
348 if (app
->project
->build_filetypes_list
!= NULL
)
350 g_ptr_array_foreach(app
->project
->build_filetypes_list
, remove_foreach_project_filetype
, NULL
);
351 g_ptr_array_free(app
->project
->build_filetypes_list
, FALSE
);
354 /* remove project non filetype build menu items */
355 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_NON_FT
, -1);
356 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_EXEC
, -1);
358 g_free(app
->project
->name
);
359 g_free(app
->project
->description
);
360 g_free(app
->project
->file_name
);
361 g_free(app
->project
->base_path
);
363 g_free(app
->project
);
366 apply_editor_prefs(); /* ensure that global settings are restored */
368 if (project_prefs
.project_session
)
370 /* close all existing tabs first */
371 document_close_all();
373 /* after closing all tabs let's open the tabs found in the default config */
374 if (open_default
&& cl_options
.load_session
)
376 configuration_reload_default_session();
377 configuration_open_files();
378 /* open a new file if no other file was opened */
379 document_new_file_if_non_open();
382 g_signal_emit_by_name(geany_object
, "project-close");
388 static gint build_page_num
= 0;
391 static void create_properties_dialog(PropertyDialogElements
*e
)
393 GtkWidget
*table
, *notebook
, *build_table
;
397 GeanyDocument
*doc
= document_get_current();
398 GeanyFiletype
*ft
= NULL
;
400 e
->dialog
= create_project_dialog();
401 gtk_window_set_transient_for(GTK_WINDOW(e
->dialog
), GTK_WINDOW(main_widgets
.window
));
402 gtk_window_set_destroy_with_parent(GTK_WINDOW(e
->dialog
), TRUE
);
403 gtk_widget_set_name(e
->dialog
, "GeanyDialogProject");
405 ui_entry_add_clear_icon(GTK_ENTRY(ui_lookup_widget(e
->dialog
, "spin_indent_width")));
407 table
= gtk_table_new(5, 2, FALSE
);
408 gtk_container_set_border_width(GTK_CONTAINER(table
), 6);
409 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
410 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
412 label
= gtk_label_new(_("Filename:"));
413 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 0, 1,
414 (GtkAttachOptions
) (GTK_FILL
),
415 (GtkAttachOptions
) (0), 0, 0);
416 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
418 e
->file_name
= gtk_label_new("");
419 gtk_label_set_selectable(GTK_LABEL(e
->file_name
), TRUE
);
420 gtk_table_attach(GTK_TABLE(table
), e
->file_name
, 1, 2, 0, 1,
421 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
422 (GtkAttachOptions
) (0), 0, 0);
423 gtk_misc_set_alignment(GTK_MISC(e
->file_name
), 0, 0);
425 label
= gtk_label_new(_("Name:"));
426 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 1, 2,
427 (GtkAttachOptions
) (GTK_FILL
),
428 (GtkAttachOptions
) (0), 0, 0);
429 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
431 e
->name
= gtk_entry_new();
432 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
433 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
434 gtk_table_attach(GTK_TABLE(table
), e
->name
, 1, 2, 1, 2,
435 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
436 (GtkAttachOptions
) (0), 0, 0);
438 label
= gtk_label_new(_("Description:"));
439 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 2, 3,
440 (GtkAttachOptions
) (GTK_FILL
),
441 (GtkAttachOptions
) (GTK_FILL
), 0, 0);
442 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
444 e
->description
= gtk_text_view_new();
445 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(e
->description
), GTK_WRAP_WORD
);
446 swin
= gtk_scrolled_window_new(NULL
, NULL
);
447 gtk_widget_set_size_request(swin
, 250, 80);
448 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
449 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
450 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin
), GTK_WIDGET(e
->description
));
451 gtk_table_attach(GTK_TABLE(table
), swin
, 1, 2, 2, 3,
452 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
453 (GtkAttachOptions
) (0), 0, 0);
455 label
= gtk_label_new(_("Base path:"));
456 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 3, 4,
457 (GtkAttachOptions
) (GTK_FILL
),
458 (GtkAttachOptions
) (0), 0, 0);
459 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
461 e
->base_path
= gtk_entry_new();
462 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
463 ui_widget_set_tooltip_text(e
->base_path
,
464 _("Base directory of all files that make up the project. "
465 "This can be a new path, or an existing directory tree. "
466 "You can use paths relative to the project filename."));
467 bbox
= ui_path_box_new(_("Choose Project Base Path"),
468 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
->base_path
));
469 gtk_table_attach(GTK_TABLE(table
), bbox
, 1, 2, 3, 4,
470 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
471 (GtkAttachOptions
) (0), 0, 0);
473 if (doc
!= NULL
) ft
= doc
->file_type
;
474 build_table
= build_commands_table(doc
, GEANY_BCS_PROJ
, &(e
->build_properties
), ft
);
475 gtk_container_set_border_width(GTK_CONTAINER(build_table
), 6);
476 label
= gtk_label_new(_("Build"));
477 notebook
= ui_lookup_widget(e
->dialog
, "project_notebook");
478 build_page_num
= gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
), build_table
, label
, 2);
479 e
->notebook
= notebook
;
481 g_signal_connect(ui_lookup_widget(e
->dialog
, "radio_long_line_custom"), "toggled",
482 G_CALLBACK(on_radio_long_line_custom_toggled
), ui_lookup_widget(e
->dialog
, "spin_long_line"));
485 label
= gtk_label_new(_("File patterns:"));
486 /* <small>Separate multiple patterns by a new line</small> */
487 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, 6, 7,
488 (GtkAttachOptions
) (GTK_FILL
),
489 (GtkAttachOptions
) (GTK_FILL
), 0, 0);
490 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
492 e
->patterns
= gtk_text_view_new();
493 swin
= gtk_scrolled_window_new(NULL
, NULL
);
494 gtk_widget_set_size_request(swin
, -1, 80);
495 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
),
496 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
497 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin
), GTK_WIDGET(e
->patterns
));
498 gtk_table_attach(GTK_TABLE(table
), swin
, 1, 2, 4, 5,
499 (GtkAttachOptions
) (GTK_EXPAND
| GTK_FILL
),
500 (GtkAttachOptions
) (0), 0, 0);
503 label
= gtk_label_new(_("Project"));
504 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook
), table
, label
, 0);
509 static void show_project_properties(gboolean show_build
)
511 PropertyDialogElements
*e
= g_new(PropertyDialogElements
, 1);
512 GeanyProject
*p
= app
->project
;
513 GtkWidget
*widget
= NULL
;
514 GtkWidget
*radio_long_line_custom
;
516 g_return_if_fail(app
->project
!= NULL
);
518 entries_modified
= FALSE
;
520 create_properties_dialog(e
);
522 stash_group_display(indent_group
, e
->dialog
);
524 /* fill the elements with the appropriate data */
525 gtk_entry_set_text(GTK_ENTRY(e
->name
), p
->name
);
526 gtk_label_set_text(GTK_LABEL(e
->file_name
), p
->file_name
);
527 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), p
->base_path
);
529 radio_long_line_custom
= ui_lookup_widget(e
->dialog
, "radio_long_line_custom");
530 switch (p
->long_line_behaviour
)
532 case 0: widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled"); break;
533 case 1: widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default"); break;
534 case 2: widget
= radio_long_line_custom
; break;
536 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
538 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line");
539 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), (gdouble
)p
->long_line_column
);
540 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom
), widget
);
542 if (p
->description
!= NULL
)
544 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
545 gtk_text_buffer_set_text(buffer
, p
->description
, -1);
549 if (p
->file_patterns
!= NULL
)
550 { /* set the file patterns */
552 gint len
= g_strv_length(p
->file_patterns
);
553 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->patterns
));
554 GString
*str
= g_string_sized_new(len
* 4);
556 for (i
= 0; i
< len
; i
++)
558 if (p
->file_patterns
[i
] != NULL
)
560 g_string_append(str
, p
->file_patterns
[i
]);
561 g_string_append_c(str
, '\n');
564 gtk_text_buffer_set_text(buffer
, str
->str
, -1);
565 g_string_free(str
, TRUE
);
569 g_signal_emit_by_name(geany_object
, "project-dialog-create", e
->notebook
);
570 gtk_widget_show_all(e
->dialog
);
572 /* note: notebook page must be shown before setting current page */
574 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
->notebook
), build_page_num
);
576 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
->notebook
), 0);
578 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
)
580 if (update_config(e
))
582 g_signal_emit_by_name(geany_object
, "project-dialog-confirmed", e
->notebook
);
584 ui_set_statusbar(TRUE
, _("Project \"%s\" saved."), app
->project
->name
);
588 build_free_fields(e
->build_properties
);
589 gtk_widget_destroy(e
->dialog
);
594 void project_properties(void)
596 show_project_properties(FALSE
);
600 void project_build_properties(void)
602 show_project_properties(TRUE
);
606 /* checks whether there is an already open project and asks the user if he wants to close it or
607 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
608 * and TRUE if we can go ahead */
609 gboolean
project_ask_close(void)
611 if (app
->project
!= NULL
)
613 if (dialogs_show_question_full(NULL
, GTK_STOCK_CLOSE
, GTK_STOCK_CANCEL
,
614 _("Do you want to close it before proceeding?"),
615 _("The '%s' project is already open."), app
->project
->name
))
617 project_close(FALSE
);
628 static GeanyProject
*create_project(void)
630 GeanyProject
*project
= g_new0(GeanyProject
, 1);
632 memset(&priv
, 0, sizeof priv
);
633 indentation
= *editor_get_indent_prefs(NULL
);
634 priv
.indentation
= &indentation
;
635 project
->priv
= &priv
;
637 project
->long_line_behaviour
= 1 /* use global settings */;
638 project
->long_line_column
= editor_prefs
.long_line_global_column
;
640 app
->project
= project
;
645 /* Verifies data for New & Properties dialogs.
646 * Returns: FALSE if the user needs to change any data. */
647 static gboolean
update_config(const PropertyDialogElements
*e
)
649 const gchar
*name
, *file_name
, *base_path
;
650 gchar
*locale_filename
;
653 gboolean new_project
= FALSE
;
656 g_return_val_if_fail(e
!= NULL
, TRUE
);
658 name
= gtk_entry_get_text(GTK_ENTRY(e
->name
));
659 name_len
= strlen(name
);
662 SHOW_ERR(_("The specified project name is too short."));
663 gtk_widget_grab_focus(e
->name
);
666 else if (name_len
> MAX_NAME_LEN
)
668 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN
);
669 gtk_widget_grab_focus(e
->name
);
673 if (app
->project
== NULL
)
674 file_name
= gtk_entry_get_text(GTK_ENTRY(e
->file_name
));
676 file_name
= gtk_label_get_text(GTK_LABEL(e
->file_name
));
678 if (! NZV(file_name
))
680 SHOW_ERR(_("You have specified an invalid project filename."));
681 gtk_widget_grab_focus(e
->file_name
);
685 locale_filename
= utils_get_locale_from_utf8(file_name
);
686 base_path
= gtk_entry_get_text(GTK_ENTRY(e
->base_path
));
688 { /* check whether the given directory actually exists */
689 gchar
*locale_path
= utils_get_locale_from_utf8(base_path
);
691 if (! g_path_is_absolute(locale_path
))
692 { /* relative base path, so add base dir of project file name */
693 gchar
*dir
= g_path_get_dirname(locale_filename
);
694 setptr(locale_path
, g_strconcat(dir
, G_DIR_SEPARATOR_S
, locale_path
, NULL
));
698 if (! g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
702 create_dir
= dialogs_show_question_full(NULL
, GTK_STOCK_OK
, GTK_STOCK_CANCEL
,
703 _("Create the project's base path directory?"),
704 _("The path \"%s\" does not exist."),
708 err_code
= utils_mkdir(locale_path
, TRUE
);
710 if (! create_dir
|| err_code
!= 0)
713 SHOW_ERR1(_("Project base directory could not be created (%s)."),
714 g_strerror(err_code
));
715 gtk_widget_grab_focus(e
->base_path
);
716 utils_free_pointers(2, locale_path
, locale_filename
, NULL
);
722 /* finally test whether the given project file can be written */
723 if ((err_code
= utils_is_file_writeable(locale_filename
)) != 0)
725 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code
));
726 gtk_widget_grab_focus(e
->file_name
);
727 g_free(locale_filename
);
730 g_free(locale_filename
);
732 if (app
->project
== NULL
)
739 setptr(p
->name
, g_strdup(name
));
740 setptr(p
->file_name
, g_strdup(file_name
));
741 /* use "." if base_path is empty */
742 setptr(p
->base_path
, g_strdup(NZV(base_path
) ? base_path
: "./"));
744 if (! new_project
) /* save properties specific fields */
746 GtkTextIter start
, end
;
747 GtkTextBuffer
*buffer
;
748 GeanyDocument
*doc
= document_get_current();
749 GeanyBuildCommand
*oldvalue
;
750 GeanyFiletype
*ft
= doc
? doc
->file_type
: NULL
;
753 /* get and set the project description */
754 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
755 gtk_text_buffer_get_start_iter(buffer
, &start
);
756 gtk_text_buffer_get_end_iter(buffer
, &end
);
757 setptr(p
->description
, g_strdup(gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
)));
759 stash_group_update(indent_group
, e
->dialog
);
761 /* read the project build menu */
762 oldvalue
= ft
? ft
->projfilecmds
: NULL
;
763 build_read_project(ft
, e
->build_properties
);
765 if (ft
!= NULL
&& ft
->projfilecmds
!= oldvalue
&& ft
->project_list_entry
< 0)
767 if (p
->build_filetypes_list
== NULL
)
768 p
->build_filetypes_list
= g_ptr_array_new();
769 ft
->project_list_entry
= p
->build_filetypes_list
->len
;
770 g_ptr_array_add(p
->build_filetypes_list
, ft
);
772 build_menu_update(doc
);
774 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled");
775 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
776 p
->long_line_behaviour
= 0;
779 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default");
780 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
781 p
->long_line_behaviour
= 1;
783 /* "Custom" radio button must be checked */
784 p
->long_line_behaviour
= 2;
787 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line");
788 p
->long_line_column
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
789 apply_editor_prefs();
792 /* get and set the project file patterns */
793 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->patterns
));
794 gtk_text_buffer_get_start_iter(buffer
, &start
);
795 gtk_text_buffer_get_end_iter(buffer
, &end
);
796 tmp
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
797 g_strfreev(p
->file_patterns
);
798 p
->file_patterns
= g_strsplit(tmp
, "\n", -1);
810 static void run_dialog(GtkWidget
*dialog
, GtkWidget
*entry
)
812 /* set filename in the file chooser dialog */
813 const gchar
*utf8_filename
= gtk_entry_get_text(GTK_ENTRY(entry
));
814 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
816 if (g_path_is_absolute(locale_filename
))
818 if (g_file_test(locale_filename
, G_FILE_TEST_EXISTS
))
820 /* if the current filename is a directory, we must use
821 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
822 * we end up in the parent directory */
823 if (g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
824 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_filename
);
826 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
828 else /* if the file doesn't yet exist, use at least the current directory */
830 gchar
*locale_dir
= g_path_get_dirname(locale_filename
);
831 gchar
*name
= g_path_get_basename(utf8_filename
);
833 if (g_file_test(locale_dir
, G_FILE_TEST_EXISTS
))
834 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dir
);
835 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), name
);
841 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog
)) != GTK_FILE_CHOOSER_ACTION_OPEN
)
843 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
845 g_free(locale_filename
);
848 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
850 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
851 gchar
*tmp_utf8_filename
= utils_get_utf8_from_locale(filename
);
853 gtk_entry_set_text(GTK_ENTRY(entry
), tmp_utf8_filename
);
855 g_free(tmp_utf8_filename
);
858 gtk_widget_destroy(dialog
);
863 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
)
866 gchar
*path
= win32_show_project_open_dialog(e
->dialog
, _("Choose Project Filename"),
867 gtk_entry_get_text(GTK_ENTRY(e
->file_name
)), TRUE
, TRUE
);
870 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), path
);
876 /* initialise the dialog */
877 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL
,
878 GTK_FILE_CHOOSER_ACTION_SAVE
,
879 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
880 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
881 gtk_widget_set_name(dialog
, "GeanyDialogProject");
882 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
883 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
884 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
885 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
887 run_dialog(dialog
, e
->file_name
);
892 /* sets the project base path and the project file name according to the project name */
893 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
898 const gchar
*project_dir
= local_prefs
.project_file_path
;
900 if (entries_modified
)
903 name
= gtk_editable_get_chars(editable
, 0, -1);
906 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
907 name
, G_DIR_SEPARATOR_S
, NULL
);
908 if (project_prefs
.project_file_in_basedir
)
909 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, name
, G_DIR_SEPARATOR_S
,
910 name
, "." GEANY_PROJECT_EXT
, NULL
);
912 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
913 name
, "." GEANY_PROJECT_EXT
, NULL
);
917 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
918 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
922 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), base_path
);
923 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), file_name
);
925 entries_modified
= FALSE
;
932 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
934 entries_modified
= TRUE
;
938 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
)
940 gtk_widget_set_sensitive(spin_long_line
, gtk_toggle_button_get_active(radio
));
944 gboolean
project_load_file(const gchar
*locale_file_name
)
946 g_return_val_if_fail(locale_file_name
!= NULL
, FALSE
);
948 if (load_config(locale_file_name
))
950 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
952 ui_set_statusbar(TRUE
, _("Project \"%s\" opened."), app
->project
->name
);
954 ui_add_recent_project_file(utf8_filename
);
955 g_free(utf8_filename
);
960 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
962 ui_set_statusbar(TRUE
, _("Project file \"%s\" could not be loaded."), utf8_filename
);
963 g_free(utf8_filename
);
969 /* Reads the given filename and creates a new project with the data found in the file.
970 * At this point there should not be an already opened project in Geany otherwise it will just
972 * The filename is expected in the locale encoding. */
973 static gboolean
load_config(const gchar
*filename
)
978 /* there should not be an open project */
979 g_return_val_if_fail(app
->project
== NULL
&& filename
!= NULL
, FALSE
);
981 config
= g_key_file_new();
982 if (! g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
))
984 g_key_file_free(config
);
988 p
= create_project();
990 stash_group_load_from_key_file(indent_group
, config
);
992 p
->name
= utils_get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
993 p
->description
= utils_get_setting_string(config
, "project", "description", "");
994 p
->file_name
= utils_get_utf8_from_locale(filename
);
995 p
->base_path
= utils_get_setting_string(config
, "project", "base_path", "");
996 p
->file_patterns
= g_key_file_get_string_list(config
, "project", "file_patterns", NULL
, NULL
);
998 p
->long_line_behaviour
= utils_get_setting_integer(config
, "long line marker",
999 "long_line_behaviour", 1 /* follow global */);
1000 p
->long_line_column
= utils_get_setting_integer(config
, "long line marker",
1001 "long_line_column", editor_prefs
.long_line_global_column
);
1002 apply_editor_prefs();
1004 build_load_menu(config
, GEANY_BCS_PROJ
, (gpointer
)p
);
1005 if (project_prefs
.project_session
)
1007 /* save current (non-project) session (it could has been changed since program startup) */
1008 configuration_save_default_session();
1009 /* now close all open files */
1010 document_close_all();
1011 /* read session files so they can be opened with configuration_open_files() */
1012 configuration_load_session_files(config
, FALSE
);
1014 g_signal_emit_by_name(geany_object
, "project-open", config
);
1015 g_key_file_free(config
);
1022 static void apply_editor_prefs(void)
1027 editor_apply_update_prefs(documents
[i
]->editor
);
1031 /* Write the project settings as well as the project session files into its configuration files.
1032 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1033 * is called while closing a project, this is used to skip emitting the signal because
1034 * project-close will be emitted afterwards.
1035 * Returns: TRUE if project file was written successfully. */
1036 static gboolean
write_config(gboolean emit_signal
)
1042 gboolean ret
= FALSE
;
1044 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
1048 config
= g_key_file_new();
1049 /* try to load an existing config to keep manually added comments */
1050 filename
= utils_get_locale_from_utf8(p
->file_name
);
1051 g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
);
1053 stash_group_save_to_key_file(indent_group
, config
);
1055 g_key_file_set_string(config
, "project", "name", p
->name
);
1056 g_key_file_set_string(config
, "project", "base_path", p
->base_path
);
1059 g_key_file_set_string(config
, "project", "description", p
->description
);
1060 if (p
->file_patterns
)
1061 g_key_file_set_string_list(config
, "project", "file_patterns",
1062 (const gchar
**) p
->file_patterns
, g_strv_length(p
->file_patterns
));
1064 g_key_file_set_integer(config
, "long line marker", "long_line_behaviour", p
->long_line_behaviour
);
1065 g_key_file_set_integer(config
, "long line marker", "long_line_column", p
->long_line_column
);
1067 /* store the session files into the project too */
1068 if (project_prefs
.project_session
)
1069 configuration_save_session_files(config
);
1070 build_save_menu(config
, (gpointer
)p
, GEANY_BCS_PROJ
);
1073 g_signal_emit_by_name(geany_object
, "project-save", config
);
1075 /* write the file */
1076 data
= g_key_file_to_data(config
, NULL
, NULL
);
1077 ret
= (utils_write_file(filename
, data
) == 0);
1081 g_key_file_free(config
);
1087 /* Constructs the project's base path which is used for "Make all" and "Execute".
1088 * The result is an absolute string in UTF-8 encoding which is either the same as
1089 * base path if it is absolute or it is built out of project file name's dir and base_path.
1090 * If there is no project or project's base_path is invalid, NULL will be returned.
1091 * The returned string should be freed when no longer needed. */
1092 gchar
*project_get_base_path(void)
1094 GeanyProject
*project
= app
->project
;
1096 if (project
&& NZV(project
->base_path
))
1098 if (g_path_is_absolute(project
->base_path
))
1099 return g_strdup(project
->base_path
);
1101 { /* build base_path out of project file name's dir and base_path */
1103 gchar
*dir
= g_path_get_dirname(project
->file_name
);
1105 if (utils_str_equal(project
->base_path
, "./"))
1108 path
= g_strconcat(dir
, G_DIR_SEPARATOR_S
, project
->base_path
, NULL
);
1117 /* This is to save project-related global settings, NOT project file settings. */
1118 void project_save_prefs(GKeyFile
*config
)
1120 GeanyProject
*project
= app
->project
;
1122 if (cl_options
.load_session
)
1124 const gchar
*utf8_filename
= (project
== NULL
) ? "" : project
->file_name
;
1126 g_key_file_set_string(config
, "project", "session_file", utf8_filename
);
1128 g_key_file_set_string(config
, "project", "project_file_path",
1129 NVL(local_prefs
.project_file_path
, ""));
1133 void project_load_prefs(GKeyFile
*config
)
1135 if (cl_options
.load_session
)
1137 g_return_if_fail(project_prefs
.session_file
== NULL
);
1138 project_prefs
.session_file
= utils_get_setting_string(config
, "project",
1139 "session_file", "");
1141 local_prefs
.project_file_path
= utils_get_setting_string(config
, "project",
1142 "project_file_path", NULL
);
1143 if (local_prefs
.project_file_path
== NULL
)
1145 local_prefs
.project_file_path
= g_strconcat(g_get_home_dir(),
1146 G_DIR_SEPARATOR_S
, PROJECT_DIR
, NULL
);
1151 /* Initialize project-related preferences in the Preferences dialog. */
1152 void project_setup_prefs(void)
1154 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1155 GtkWidget
*path_btn
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_button");
1156 static gboolean callback_setup
= FALSE
;
1158 g_return_if_fail(local_prefs
.project_file_path
!= NULL
);
1160 gtk_entry_set_text(GTK_ENTRY(path_entry
), local_prefs
.project_file_path
);
1161 if (! callback_setup
)
1162 { /* connect the callback only once */
1163 callback_setup
= TRUE
;
1164 ui_setup_open_button_callback(path_btn
, NULL
,
1165 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(path_entry
));
1170 /* Update project-related preferences after using the Preferences dialog. */
1171 void project_apply_prefs(void)
1173 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1176 str
= gtk_entry_get_text(GTK_ENTRY(path_entry
));
1177 setptr(local_prefs
.project_file_path
, g_strdup(str
));
1181 void project_init(void)
1185 group
= stash_group_new("indentation");
1186 /* defaults are copied from editor indent prefs */
1187 stash_group_set_use_defaults(group
, FALSE
);
1188 indent_group
= group
;
1190 stash_group_add_spin_button_integer(group
, &indentation
.width
,
1191 "indent_width", 4, "spin_indent_width");
1192 stash_group_add_radio_buttons(group
, (gint
*)(gpointer
)&indentation
.type
,
1193 "indent_type", GEANY_INDENT_TYPE_TABS
,
1194 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES
,
1195 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS
,
1196 "radio_indent_both", GEANY_INDENT_TYPE_BOTH
,
1198 /* This is a 'hidden' pref for backwards-compatibility */
1199 stash_group_add_integer(group
, &indentation
.hard_tab_width
,
1200 "indent_hard_tab_width", 8);
1201 stash_group_add_toggle_button(group
, &indentation
.detect_type
,
1202 "detect_indent", FALSE
, "check_detect_indent");
1203 stash_group_add_combo_box(group
, (gint
*)(gpointer
)&indentation
.auto_indent_mode
,
1204 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS
, "combo_auto_indent_mode");
1208 void project_finalize(void)
1210 stash_group_free(indent_group
);