2008-04-08 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / src / anjuta_cbs.c
blobda425ddffa525c0de7787556a87aa89f0c70abb0
1 /*
2 anjuta_cbs.c
3 Copyright (C) 2000 Naba Kumar <naba@gnome.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <string.h>
28 #include <gnome.h>
30 #include <libanjuta/resources.h>
32 #include "text_editor.h"
33 #include "anjuta.h"
34 #include "controls.h"
35 #include "fileselection.h"
36 #include "utilities.h"
37 #include "launcher.h"
38 #include "debugger.h"
41 /* Blurb ?? */
42 extern gboolean closing_state;
44 gint on_anjuta_delete (GtkWidget *w, GdkEvent *event, gpointer data)
46 AnjutaApp *app;
47 TextEditor *te;
48 GList *list;
49 gboolean file_not_saved;
50 gint max_recent_files, max_recent_prjs;
52 app = ANJUTA_APP (w);
54 file_not_saved = FALSE;
55 max_recent_files =
56 anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
57 MAXIMUM_RECENT_FILES);
58 max_recent_prjs =
59 anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
60 MAXIMUM_RECENT_PROJECTS);
61 list = app->text_editor_list;
62 while (list)
64 te = list->data;
65 if (te->full_filename)
66 app->recent_files =
67 glist_path_dedup(update_string_list (app->recent_files,
68 te->full_filename,
69 max_recent_files));
70 if (!text_editor_is_saved (te))
71 file_not_saved = TRUE;
72 list = g_list_next (list);
74 if (app->project_dbase->project_is_open)
76 app->recent_projects =
77 glist_path_dedup(update_string_list (app->recent_projects,
78 app->project_dbase->proj_filename,
79 max_recent_files));
81 anjuta_app_save_settings (app);
83 /* No need to check for saved project, as it is done in
84 close project call later. */
85 if (file_not_saved)
87 GtkWidget *dialog;
88 gint response;
89 dialog = gtk_message_dialog_new (GTK_WINDOW (app),
90 GTK_DIALOG_DESTROY_WITH_PARENT,
91 GTK_MESSAGE_QUESTION,
92 GTK_BUTTONS_NONE,
93 _("One or more files are not saved.\n"
94 "Do you still want to exit?"));
95 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
96 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
97 GTK_STOCK_QUIT, GTK_RESPONSE_YES,
98 NULL);
99 response = gtk_dialog_run (GTK_DIALOG (dialog));
100 gtk_widget_destroy (dialog);
101 if (response == GTK_RESPONSE_YES)
102 anjuta_clean_exit ();
103 return TRUE;
105 else
106 anjuta_app_clean_exit (app);
107 return TRUE;
110 void
111 on_anjuta_destroy (GtkWidget * w, gpointer data)
113 /* Nothing to be done here */
116 void
117 on_anjuta_dnd_drop (gchar* filename, gpointer data)
119 anjuta_goto_file_line (filename, 0);
122 gboolean
123 on_anjuta_window_focus_in_event (GtkWidget * widget,
124 GdkEventFocus * event, gpointer user_data)
126 gint page_num;
127 TextEditor *te;
129 if (g_list_length (GTK_NOTEBOOK (app->notebook)->children) > 0)
131 page_num =
132 gtk_notebook_get_current_page (GTK_NOTEBOOK
133 (app->notebook));
134 if (page_num >= 0)
135 te = anjuta_get_notebook_text_editor (page_num);
136 else
137 te = NULL;
139 else if (app->text_editor_list == NULL)
141 te = NULL;
143 else
145 /* If there is TextEditor in the list, but no page
146 * * in the notebook, we don't change the current
147 * * TextEditor
149 return FALSE;
151 if (te == anjuta_get_current_text_editor ())
152 return FALSE;
153 anjuta_set_current_text_editor (te);
154 return FALSE;
157 enum {
158 m___ = 0,
159 mS__ = GDK_SHIFT_MASK,
160 m_C_ = GDK_CONTROL_MASK,
161 m__M = GDK_MOD1_MASK,
162 mSC_ = GDK_SHIFT_MASK | GDK_CONTROL_MASK,
165 enum {
166 ID_NEXTBUFFER = 1, /* Note: the value mustn't be 0 ! */
167 ID_PREVBUFFER,
168 ID_FIRSTBUFFER
171 typedef struct {
172 int modifiers;
173 unsigned int gdk_key;
174 int id;
175 } ShortcutMapping;
177 static ShortcutMapping global_keymap[] = {
178 { m_C_, GDK_Tab, ID_NEXTBUFFER },
179 { mSC_, GDK_ISO_Left_Tab, ID_PREVBUFFER },
180 { m__M, GDK_1, ID_FIRSTBUFFER },
181 { m__M, GDK_2, ID_FIRSTBUFFER + 1},
182 { m__M, GDK_3, ID_FIRSTBUFFER + 2},
183 { m__M, GDK_4, ID_FIRSTBUFFER + 3},
184 { m__M, GDK_5, ID_FIRSTBUFFER + 4},
185 { m__M, GDK_6, ID_FIRSTBUFFER + 5},
186 { m__M, GDK_7, ID_FIRSTBUFFER + 6},
187 { m__M, GDK_8, ID_FIRSTBUFFER + 7},
188 { m__M, GDK_9, ID_FIRSTBUFFER + 8},
189 { m__M, GDK_0, ID_FIRSTBUFFER + 9},
190 { 0, 0, 0 }
193 gint
194 on_anjuta_window_key_press_event (GtkWidget *widget,
195 GdkEventKey *event,
196 gpointer user_data)
198 int modifiers;
199 int i;
201 g_return_val_if_fail (widget != NULL, FALSE);
202 g_return_val_if_fail (GNOME_IS_APP (widget), FALSE);
203 g_return_val_if_fail (event != NULL, FALSE);
205 modifiers = event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK);
207 for (i = 0; global_keymap[i].id; i++)
208 if (event->keyval == global_keymap[i].gdk_key &&
209 (event->state & global_keymap[i].modifiers) == global_keymap[i].modifiers)
210 break;
212 if (!global_keymap[i].id)
213 return FALSE;
215 switch (global_keymap[i].id) {
216 case ID_NEXTBUFFER:
217 case ID_PREVBUFFER: {
218 GtkNotebook *notebook = GTK_NOTEBOOK (app->notebook);
219 int pages_nb;
220 int cur_page;
222 if (!notebook->children)
223 return FALSE;
225 if (!g_tabbing)
227 g_tabbing = TRUE;
230 pages_nb = g_list_length (notebook->children);
231 cur_page = gtk_notebook_get_current_page (notebook);
233 if (global_keymap[i].id == ID_NEXTBUFFER)
234 cur_page = (cur_page < pages_nb - 1) ? cur_page + 1 : 0;
235 else
236 cur_page = cur_page ? cur_page - 1 : pages_nb -1;
238 gtk_notebook_set_page (notebook, cur_page);
240 break;
242 default:
243 if (global_keymap[i].id >= ID_FIRSTBUFFER &&
244 global_keymap[i].id <= (ID_FIRSTBUFFER + 9))
246 GtkNotebook *notebook = GTK_NOTEBOOK (app->notebook);
247 int page_req = global_keymap[i].id - ID_FIRSTBUFFER;
249 if (!notebook->children)
250 return FALSE;
251 gtk_notebook_set_page(notebook, page_req);
253 else
254 return FALSE;
257 /* Note: No reason for a shortcut to do more than one thing a time */
258 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
260 return TRUE;
263 gint
264 on_anjuta_window_key_release_event (GtkWidget *widget,
265 GdkEventKey *event,
266 gpointer user_data)
268 g_return_val_if_fail (widget != NULL, FALSE);
269 g_return_val_if_fail (GNOME_IS_APP (widget), FALSE);
270 g_return_val_if_fail (event != NULL, FALSE);
272 if (g_tabbing && ((event->keyval == GDK_Control_L) ||
273 (event->keyval == GDK_Control_R)))
275 GtkNotebook *notebook = GTK_NOTEBOOK (app->notebook);
276 GtkWidget *widget;
277 int cur_page;
278 g_tabbing = FALSE;
280 if (anjuta_preferences_get_int (app->preferences,
281 EDITOR_TABS_RECENT_FIRST))
284 TTimo: move the current notebook page to first position
285 that maintains Ctrl-TABing on a list of most recently edited files
287 cur_page = gtk_notebook_get_current_page (notebook);
288 widget = gtk_notebook_get_nth_page (notebook, cur_page);
289 gtk_notebook_reorder_child (notebook, widget, 0);
292 return FALSE;
295 void
296 on_open_filesel_ok_clicked (GtkButton * button, gpointer user_data)
298 gchar *full_filename;
299 gchar *entry_filename = NULL;
300 int i;
301 GList * list;
302 int elements;
304 list = fileselection_get_filelist(app->fileselection);
305 elements = g_list_length(list);
306 /* If filename is only written in entry but not selected (Bug #506441) */
307 if (elements == 0)
309 entry_filename = fileselection_get_filename(app->fileselection);
310 if (entry_filename)
312 list = g_list_append(list, entry_filename);
313 elements++;
316 for(i=0;i<elements;i++)
318 full_filename = g_strdup(g_list_nth_data(list,i));
319 if (!full_filename)
320 return;
321 anjuta_goto_file_line (full_filename, -1);
322 g_free (full_filename);
325 if (entry_filename)
327 g_list_remove(list, entry_filename);
328 g_free(entry_filename);
330 /* g_free(list); */
333 static void
334 save_as_real (void)
336 TextEditor *te;
337 gchar *full_filename, *saved_filename, *saved_full_filename;
338 gint page_num;
339 GtkWidget *child;
340 gint status;
342 full_filename = fileselection_get_filename (app->save_as_fileselection);
343 if (!full_filename)
344 return;
346 te = anjuta_get_current_text_editor ();
347 g_return_if_fail (te != NULL);
348 g_return_if_fail (strlen (extract_filename (full_filename)) != 0);
350 saved_filename = te->filename;
351 saved_full_filename = te->full_filename;
353 te->full_filename = g_strdup (full_filename);
354 te->filename = g_strdup (extract_filename (full_filename));
355 status = text_editor_save_file (te, TRUE);
356 gtk_widget_hide (app->save_as_fileselection);
357 if (status == FALSE)
359 g_free (te->filename);
360 te->filename = saved_filename;
361 g_free (te->full_filename);
362 te->full_filename = saved_full_filename;
363 if (closing_state) closing_state = FALSE;
364 g_free (full_filename);
365 return;
366 } else {
367 if (saved_filename)
368 g_free (saved_filename);
369 if (saved_full_filename)
371 g_free (saved_full_filename);
373 if (closing_state)
375 anjuta_remove_current_text_editor ();
376 closing_state = FALSE;
378 else
380 text_editor_set_hilite_type(te);
381 if (te->mode == TEXT_EDITOR_PAGED)
383 GtkLabel* label;
385 page_num =
386 gtk_notebook_get_current_page (GTK_NOTEBOOK
387 (app->notebook));
388 g_return_if_fail (page_num >= 0);
389 child =
390 gtk_notebook_get_nth_page (GTK_NOTEBOOK
391 (app->notebook), page_num);
392 /* This crashes */
393 /* gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (app->notebook),
394 child,
395 anjuta_get_notebook_text_editor
396 (page_num)->filename);
398 label = GTK_LABEL(te->widgets.tab_label);
399 gtk_label_set_text(GTK_LABEL(label), anjuta_get_notebook_text_editor
400 (page_num)->filename);
402 gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(app->notebook),
403 child,
404 anjuta_get_notebook_text_editor
405 (page_num)->filename);
408 anjuta_update_title ();
409 g_free (full_filename);
413 void
414 on_save_as_filesel_ok_clicked (GtkButton * button, gpointer user_data)
416 gchar *filename;
418 filename = fileselection_get_filename (app->save_as_fileselection);
419 if (file_is_regular (filename))
421 GtkWidget *dialog;
422 dialog = gtk_message_dialog_new (GTK_WINDOW (app),
423 GTK_DIALOG_DESTROY_WITH_PARENT,
424 GTK_MESSAGE_QUESTION,
425 GTK_BUTTONS_NONE,
426 _("The file '%s' already exists.\n"
427 "Do you want to replace it with the one you are saving?"),
428 filename);
429 gtk_dialog_add_button (GTK_DIALOG(dialog),
430 GTK_STOCK_CANCEL,
431 GTK_RESPONSE_CANCEL);
432 anjuta_dialog_add_button (GTK_DIALOG (dialog),
433 _("_Replace"),
434 GTK_STOCK_REFRESH,
435 GTK_RESPONSE_YES);
436 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
437 save_as_real ();
438 gtk_widget_destroy (dialog);
440 else
441 save_as_real ();
442 g_free (filename);
444 if (anjuta_preferences_get_int (ANJUTA_PREFERENCES (app->preferences),
445 EDITOR_TABS_ORDERING))
446 anjuta_order_tabs ();
449 void
450 on_save_as_filesel_cancel_clicked (GtkButton * button, gpointer user_data)
452 gtk_widget_hide (app->save_as_fileselection);
453 closing_state = FALSE;
456 static void
457 build_msg_save_real (void)
459 gchar *filename;
460 FILE *msgfile;
462 filename = fileselection_get_filename (app->save_as_build_msg_sel);
463 msgfile = fopen(filename, "w");
464 if (! msgfile)
466 anjuta_error (_("Could not open file for writing"));
467 return;
470 an_message_manager_save_build(app->messages, msgfile);
472 fclose(msgfile);
474 return;
477 void
478 on_build_msg_save_ok_clicked(GtkButton * button, gpointer user_data)
480 gchar *filename;
482 filename = fileselection_get_filename (app->save_as_build_msg_sel);
483 if (file_is_regular (filename))
485 GtkWidget *dialog;
486 dialog = gtk_message_dialog_new (GTK_WINDOW (app),
487 GTK_DIALOG_DESTROY_WITH_PARENT,
488 GTK_MESSAGE_QUESTION,
489 GTK_BUTTONS_NONE,
490 _("The file '%s' already exists.\n"
491 "Do you want to replace it with the one you are saving?."),
492 filename);
493 gtk_dialog_add_button (GTK_DIALOG (dialog),
494 GTK_STOCK_CANCEL,
495 GTK_RESPONSE_CANCEL);
496 anjuta_dialog_add_button (GTK_DIALOG (dialog),
497 _("_Replace"),
498 GTK_STOCK_REFRESH,
499 GTK_RESPONSE_YES);
500 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
501 build_msg_save_real ();
502 gtk_widget_destroy (dialog);
504 else
505 build_msg_save_real ();
506 g_free (filename);
509 void
510 on_build_msg_save_cancel_clicked(GtkButton * button, gpointer user_data)
512 gtk_widget_hide (app->save_as_build_msg_sel);
513 closing_state = FALSE;
516 #if 0
517 void
518 on_prj_list_undock_button_clicked (GtkButton * button, gpointer user_data)
520 project_dbase_undock (app->project_dbase);
524 void
525 on_prj_list_hide_button_clicked (GtkButton * button, gpointer user_data)
527 project_dbase_hide (app->project_dbase);
531 void
532 on_mesg_win_hide_button_clicked (GtkButton * button, gpointer user_data)
534 gtk_widget_hide(GTK_WIDGET(app->messages));
538 void
539 on_mesg_win_undock_button_clicked (GtkButton * button, gpointer user_data)
541 an_message_manager_undock (app->messages);
544 #endif
546 void
547 on_anjuta_progress_cancel (gpointer data)
549 app->in_progress = FALSE;
550 app->progress_cancel_cb (data);
551 return;
554 gdouble
555 on_anjuta_progress_cb (gpointer data)
557 return (app->progress_value);