anjuta-docman.c 26.7 KB
Newer Older
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
    anjuta-docman.c
    Copyright (C) 2003  Naba Kumar <naba@gnome.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <libanjuta/pixmaps.h>
#include <libanjuta/resources.h>
#include <libanjuta/anjuta-utils.h>
#include <libanjuta/anjuta-preferences.h>
Johannes Schmid's avatar
Johannes Schmid committed
25 26 27

#include <gtk/gtkfilechooserdialog.h>
#include <libgnomevfs/gnome-vfs.h>
28 29 30 31

#include "text_editor.h"
#include "anjuta-docman.h"
#include "file_history.h"
Johannes Schmid's avatar
Johannes Schmid committed
32 33
#include "plugin.h"
#include "action-callbacks.h"
34

35 36
gboolean closing_state;

37 38 39
struct _AnjutaDocmanPriv {
	AnjutaPreferences *preferences;
	TextEditor *current_editor;
40 41
	GtkWidget *fileselection;
	GtkWidget *save_as_fileselection;
42
	GList *editors;
Naba Kumar's avatar
Naba Kumar committed
43
	GtkWidget *popup_menu;
44 45 46 47 48
};

static void anjuta_docman_order_tabs(AnjutaDocman *docman);
static void anjuta_docman_update_page_label (AnjutaDocman *docman, GtkWidget *te);
static void anjuta_docman_grab_text_focus (AnjutaDocman *docman);
Naba Kumar's avatar
Naba Kumar committed
49 50 51
static void on_notebook_switch_page (GtkNotebook * notebook,
									 GtkNotebookPage * page,
									 gint page_num, AnjutaDocman *docman);
52 53 54 55 56 57 58 59 60 61 62 63

struct _AnjutaDocmanPage {
	GtkWidget *widget;
	GtkWidget *close_image;
	GtkWidget *close_button;
	GtkWidget *label;
	GtkWidget *box;
};

typedef struct _AnjutaDocmanPage AnjutaDocmanPage;

static void
Johannes Schmid's avatar
Johannes Schmid committed
64 65
on_text_editor_notebook_close_page (GtkButton* button, 
									AnjutaDocman* docman)
66
{
Johannes Schmid's avatar
Johannes Schmid committed
67 68 69 70 71 72 73 74
	/* This is a hack because we cannot get the real
	Editor plugin */
	EditorPlugin* dummy_plugin = g_new0(EditorPlugin, 1);
	dummy_plugin->docman = GTK_WIDGET(docman);

	on_close_file1_activate (NULL, dummy_plugin);
	
	g_free(dummy_plugin);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

static void
editor_tab_widget_destroy (AnjutaDocmanPage* page)
{
	g_return_if_fail(page != NULL);
	g_return_if_fail(page->close_button != NULL);

	g_object_unref (G_OBJECT (page->close_button));
	g_object_unref (G_OBJECT (page->close_image));
	g_object_unref (G_OBJECT (page->label));
	
	page->close_image = NULL;
	page->close_button = NULL;
	page->label = NULL;
}

static GtkWidget*
Johannes Schmid's avatar
Johannes Schmid committed
93
editor_tab_widget_new(AnjutaDocmanPage* page, AnjutaDocman* docman)
94 95 96 97 98 99
{
	GtkWidget *button15;
	GtkWidget *close_pixmap;
	GtkWidget *tmp_toolbar_icon;
	GtkWidget *label;
	GtkWidget *box;
Johannes Schmid's avatar
Johannes Schmid committed
100
	int h, w;
101 102 103 104 105 106 107
	GdkColor color;
	
	g_return_val_if_fail(GTK_IS_WIDGET (page->widget), NULL);
	
	if (page->close_image)
		editor_tab_widget_destroy (page);
	
Johannes Schmid's avatar
Johannes Schmid committed
108 109 110
	gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &w, &h);

	tmp_toolbar_icon = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
111 112 113 114 115
	gtk_widget_show(tmp_toolbar_icon);
	
	button15 = gtk_button_new();
	gtk_container_add(GTK_CONTAINER(button15), tmp_toolbar_icon);
	gtk_button_set_relief(GTK_BUTTON(button15), GTK_RELIEF_NONE);
Johannes Schmid's avatar
Johannes Schmid committed
116
	gtk_widget_set_size_request (button15, w, h);
117
	
Johannes Schmid's avatar
Johannes Schmid committed
118 119
	close_pixmap = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
	gtk_widget_set_size_request(close_pixmap, w,h);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	gtk_widget_set_sensitive(close_pixmap, FALSE);
	
	label = gtk_label_new (TEXT_EDITOR (page->widget)->filename);
	gtk_widget_show (label);
	
	color.red = 0;
	color.green = 0;
	color.blue = 0;
	
	gtk_widget_modify_fg (button15, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_fg (button15, GTK_STATE_INSENSITIVE, &color);
	gtk_widget_modify_fg (button15, GTK_STATE_ACTIVE, &color);
	gtk_widget_modify_fg (button15, GTK_STATE_PRELIGHT, &color);
	gtk_widget_modify_fg (button15, GTK_STATE_SELECTED, &color);
	gtk_widget_show(button15);
	
Johannes Schmid's avatar
Johannes Schmid committed
136
	box = gtk_hbox_new(FALSE, 2);
137
	gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0);
138 139
	gtk_box_pack_start(GTK_BOX(box), button15, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(box), close_pixmap, FALSE, FALSE, 0);
140 141 142 143 144
	
	gtk_widget_show(box);

	gtk_signal_connect (GTK_OBJECT (button15), "clicked",
				GTK_SIGNAL_FUNC(on_text_editor_notebook_close_page),
Johannes Schmid's avatar
Johannes Schmid committed
145
				docman);
146 147 148 149 150 151 152 153 154 155 156 157 158

	page->close_image = close_pixmap;
	page->close_button = button15;
	page->label = label;
	
	g_object_ref (page->close_button);
	g_object_ref (page->close_image);
	g_object_ref (page->label);

	return box;
}

static AnjutaDocmanPage *
Johannes Schmid's avatar
Johannes Schmid committed
159
anjuta_docman_page_new (GtkWidget *editor, AnjutaDocman* docman)
160 161 162 163 164 165
{
	AnjutaDocmanPage *page;
	
	page = g_new0 (AnjutaDocmanPage, 1);
	page->widget = GTK_WIDGET (editor);
	g_object_ref (G_OBJECT (page->widget));
Johannes Schmid's avatar
Johannes Schmid committed
166
	page->box = editor_tab_widget_new (page, docman);
167 168 169 170 171 172 173 174 175 176 177
	return page;
}

static void
anjuta_docman_page_destroy (AnjutaDocmanPage *page)
{
	editor_tab_widget_destroy (page);
	g_object_unref (G_OBJECT (page->widget));
	g_free (page);
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191
void
anjuta_docman_open_file (AnjutaDocman *docman)
{
	gtk_widget_show (docman->priv->fileselection);
}

void
anjuta_docman_save_as_file (AnjutaDocman *docman)
{
	TextEditor *te;

	te = anjuta_docman_get_current_editor (docman);
	if (te == NULL)
		return;
Johannes Schmid's avatar
Johannes Schmid committed
192 193
	gtk_file_chooser_set_uri (GTK_FILE_CHOOSER(docman->priv->save_as_fileselection),
								te->uri);
194 195 196 197
	gtk_widget_show (docman->priv->save_as_fileselection);
}

static void
Johannes Schmid's avatar
Johannes Schmid committed
198
on_open_filesel_ok_clicked (GtkDialog* dialog, gint id, AnjutaDocman *docman)
199
{
Johannes Schmid's avatar
Johannes Schmid committed
200
	gchar *uri;
201 202
	gchar *entry_filename = NULL;
	int i;
Johannes Schmid's avatar
Johannes Schmid committed
203
	GSList * list;
204
	int elements;
Johannes Schmid's avatar
Johannes Schmid committed
205 206 207 208 209 210

	if (id != GTK_RESPONSE_ACCEPT)
	{
		gtk_widget_hide (docman->priv->save_as_fileselection);
		return;
	}
211
	
Johannes Schmid's avatar
Johannes Schmid committed
212 213
	list = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(dialog));
	elements = g_slist_length(list);
214 215
	for(i=0;i<elements;i++)
	{
Johannes Schmid's avatar
Johannes Schmid committed
216 217
		uri = g_strdup(g_slist_nth_data(list,i));
		if (!uri)
218
			return;
Johannes Schmid's avatar
Johannes Schmid committed
219 220
		anjuta_docman_goto_file_line (docman, uri, -1);
		g_free (uri);
221 222 223 224
	}

	if (entry_filename)
	{
Johannes Schmid's avatar
Johannes Schmid committed
225
		g_slist_remove(list, entry_filename);
226 227 228 229 230 231 232 233
		g_free(entry_filename);
	}
}

static void
save_as_real (AnjutaDocman *docman)
{
	TextEditor *te;
Johannes Schmid's avatar
Johannes Schmid committed
234
	gchar *uri, *saved_filename, *saved_uri, *filename;
235
	gint status;
Johannes Schmid's avatar
Johannes Schmid committed
236 237
	uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER(docman->priv->save_as_fileselection));
	if (!uri)
238 239 240 241 242 243
		return;

	te = anjuta_docman_get_current_editor (docman);
	g_return_if_fail (te != NULL);

	saved_filename = te->filename;
Johannes Schmid's avatar
Johannes Schmid committed
244
	saved_uri = te->uri;
245
	
Johannes Schmid's avatar
Johannes Schmid committed
246 247 248
	filename = g_filename_from_uri(uri, NULL, NULL);
	te->uri = g_strdup (uri);
	te->filename = g_path_get_basename(filename); 
249 250 251 252 253 254
	status = text_editor_save_file (te, TRUE);
	gtk_widget_hide (docman->priv->save_as_fileselection);
	if (status == FALSE)
	{
		g_free (te->filename);
		te->filename = saved_filename;
Johannes Schmid's avatar
Johannes Schmid committed
255 256
		g_free (te->uri);
		te->uri = saved_uri;
257 258 259 260 261
		if (closing_state) closing_state = FALSE;
		return;
	} else {
		if (saved_filename)
			g_free (saved_filename);
Johannes Schmid's avatar
Johannes Schmid committed
262
		if (saved_uri)
263
		{
Johannes Schmid's avatar
Johannes Schmid committed
264
			g_free (saved_uri);
265 266 267 268 269 270 271 272 273 274 275 276 277 278
		}
		if (closing_state)
		{
			anjuta_docman_remove_editor (docman, NULL);
			closing_state = FALSE;
		}
		else
		{
			text_editor_set_hilite_type(te);
		}
	}
}

static void
Johannes Schmid's avatar
Johannes Schmid committed
279
on_save_as_filesel_ok_clicked (GtkDialog* dialog, gint id, AnjutaDocman *docman)
280
{
Johannes Schmid's avatar
Johannes Schmid committed
281 282 283 284 285 286 287 288
	gchar* uri;
	GnomeVFSURI* vfs_uri;
	
	if (id != GTK_RESPONSE_ACCEPT)
	{
		gtk_widget_hide (docman->priv->save_as_fileselection);
		return;
	} 	
289

Johannes Schmid's avatar
Johannes Schmid committed
290 291 292
	uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
	vfs_uri = gnome_vfs_uri_new(uri);
	if (gnome_vfs_uri_exists(vfs_uri))
293 294 295 296 297 298 299 300 301 302
	{
		GtkWidget *dialog;
		GtkWidget *parent;
		parent = gtk_widget_get_toplevel (GTK_WIDGET (docman));
		dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
										 GTK_DIALOG_DESTROY_WITH_PARENT,
										 GTK_MESSAGE_QUESTION,
										 GTK_BUTTONS_NONE,
										 _("The file '%s' already exists.\n"
										 "Do you want to replace it with the one you are saving?"),
Johannes Schmid's avatar
Johannes Schmid committed
303
										 uri);
304 305 306 307 308 309 310 311 312 313 314 315 316
		gtk_dialog_add_button (GTK_DIALOG(dialog),
							   GTK_STOCK_CANCEL,
							   GTK_RESPONSE_CANCEL);
		anjuta_util_dialog_add_button (GTK_DIALOG (dialog),
								  _("_Replace"),
								  GTK_STOCK_REFRESH,
								  GTK_RESPONSE_YES);
		if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES)
			save_as_real (docman);
		gtk_widget_destroy (dialog);
	}
	else
		save_as_real (docman);
Johannes Schmid's avatar
Johannes Schmid committed
317
	g_free (uri);
318 319 320 321 322 323

	if (anjuta_preferences_get_int (ANJUTA_PREFERENCES (docman->priv->preferences),
									EDITOR_TABS_ORDERING))
		anjuta_docman_order_tabs (docman);
}

Johannes Schmid's avatar
Johannes Schmid committed
324 325
static GtkWidget*
create_file_open_dialog_gui(GtkWindow* parent, AnjutaDocman* docman)
326
{
Johannes Schmid's avatar
Johannes Schmid committed
327 328 329 330 331 332 333
	GtkWidget* dialog = 
		gtk_file_chooser_dialog_new (_("Open file"), 
									parent,
									GTK_FILE_CHOOSER_ACTION_OPEN,
									GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
									GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
									NULL);
Johannes Schmid's avatar
Johannes Schmid committed
334
	gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(dialog), TRUE);
Johannes Schmid's avatar
Johannes Schmid committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	g_signal_connect(G_OBJECT(dialog), "response", 
					G_CALLBACK(on_open_filesel_ok_clicked), docman);
	g_signal_connect_swapped (GTK_OBJECT (dialog), 
                             "response", 
                             G_CALLBACK (gtk_widget_hide),
                             GTK_OBJECT (dialog));

	return dialog;
}

static GtkWidget*
create_file_save_dialog_gui(GtkWindow* parent, AnjutaDocman* docman)
{
	GtkWidget* dialog = 
		gtk_file_chooser_dialog_new (_("Save file as"), 
									parent,
									GTK_FILE_CHOOSER_ACTION_SAVE,
									GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
									GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
									NULL); 

	g_signal_connect(G_OBJECT(dialog), "response", 
					G_CALLBACK(on_save_as_filesel_ok_clicked), docman);	
	g_signal_connect_swapped (GTK_OBJECT (dialog), 
                             "response", 
                             G_CALLBACK (gtk_widget_hide),
                             GTK_OBJECT (dialog));
	return dialog;
363 364
}

365 366 367 368 369
gpointer parent_class;

static void
anjuta_docman_instance_init (AnjutaDocman *docman)
{
370 371
	GtkWidget *parent;

372 373 374
	/* FIXME: */
	// parent = gtk_widget_get_toplevel (GTK_WIDGET (docman));
	parent = NULL;
375
	docman->priv = g_new0 (AnjutaDocmanPriv, 1);
Naba Kumar's avatar
Naba Kumar committed
376
	docman->priv->popup_menu = NULL;
377
	docman->priv->fileselection =
Johannes Schmid's avatar
Johannes Schmid committed
378
		create_file_open_dialog_gui(GTK_WINDOW (parent), docman);
379
	docman->priv->save_as_fileselection =
Johannes Schmid's avatar
Johannes Schmid committed
380
		create_file_save_dialog_gui (GTK_WINDOW (parent), docman);
381
	gtk_window_set_modal ((GtkWindow *) docman->priv->save_as_fileselection, TRUE);
382 383
	
	gtk_notebook_set_scrollable (GTK_NOTEBOOK (docman), TRUE);
Naba Kumar's avatar
Naba Kumar committed
384 385
	g_signal_connect (G_OBJECT(docman), "switch_page",
					  G_CALLBACK (on_notebook_switch_page), docman);
386 387 388 389 390
}

static void
anjuta_docman_class_init (AnjutaDocmanClass *klass)
{
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	static gboolean initialized;
	
	if (!initialized)
	{
		initialized = TRUE;
		
		/* Signal */
		g_signal_new ("editor_changed",
			ANJUTA_TYPE_DOCMAN,
			G_SIGNAL_RUN_LAST,
			G_STRUCT_OFFSET (AnjutaDocmanClass, editor_changed),
			NULL, NULL,
			g_cclosure_marshal_VOID__OBJECT,
			G_TYPE_NONE,
			1,
			G_TYPE_OBJECT);
	}
408 409
}

410 411 412 413 414 415 416 417 418 419
GtkWidget*
anjuta_docman_new (AnjutaPreferences *pref)
{
	GtkWidget *docman = NULL;
	docman = gtk_widget_new (ANJUTA_TYPE_DOCMAN, NULL);
	if (docman)
	    ANJUTA_DOCMAN (docman)->priv->preferences = pref;
	return docman;
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/*! state flag for Ctrl-TAB */
static gboolean g_tabbing = FALSE;

static void
on_notebook_switch_page (GtkNotebook * notebook,
						 GtkNotebookPage * page,
						 gint page_num, AnjutaDocman *docman)
{
	GtkWidget *widget;
	/* TTimo: reorder so that the most recently used files are always
	 * at the beginning of the tab list
	 */
	widget = gtk_notebook_get_nth_page (notebook, page_num);
	anjuta_docman_set_current_editor (docman, TEXT_EDITOR (widget));
	if (!g_tabbing &&
		!anjuta_preferences_get_int (docman->priv->preferences, EDITOR_TABS_ORDERING) && 
		anjuta_preferences_get_int (docman->priv->preferences, EDITOR_TABS_RECENT_FIRST))
	{
		gtk_notebook_reorder_child (notebook, widget, 0);
	}
}

442 443 444 445 446 447 448
static void
on_editor_save_point (TextEditor *editor, gboolean entering,
					  AnjutaDocman *docman)
{
	anjuta_docman_update_page_label (docman, GTK_WIDGET (editor));
}

449
TextEditor *
450
anjuta_docman_add_editor (AnjutaDocman *docman, const gchar *uri, const gchar *name)
451 452 453
{
	GtkWidget *te;
	AnjutaDocmanPage *page;
Naba Kumar's avatar
Naba Kumar committed
454
	
455
	te = text_editor_new (ANJUTA_PREFERENCES (docman->priv->preferences),
Johannes Schmid's avatar
Johannes Schmid committed
456
						  uri, name);
Johannes Schmid's avatar
Johannes Schmid committed
457 458 459 460 461
	/* File cannot be loaded, texteditor brings up an error dialog */
	if (te == NULL)
	{
		return NULL;
	}
462
	text_editor_set_popup_menu (TEXT_EDITOR (te), docman->priv->popup_menu);
Johannes Schmid's avatar
Johannes Schmid committed
463
	
464
	gtk_widget_show (te);
Johannes Schmid's avatar
Johannes Schmid committed
465
	page = anjuta_docman_page_new (te, docman);
466 467 468 469 470 471 472 473
	
	anjuta_docman_set_current_editor (docman, TEXT_EDITOR (te));
	docman->priv->editors = g_list_append (docman->priv->editors, (gpointer)page);
	
	gtk_notebook_prepend_page (GTK_NOTEBOOK (docman), te, page->box);
	gtk_notebook_set_menu_label_text(GTK_NOTEBOOK (docman), te,
									 TEXT_EDITOR (te)->filename);
	
Naba Kumar's avatar
Naba Kumar committed
474 475 476
	g_signal_handlers_block_by_func (GTK_OBJECT (docman),
				       GTK_SIGNAL_FUNC (on_notebook_switch_page),
				       docman);
477 478 479 480 481 482
	if (anjuta_preferences_get_int (ANJUTA_PREFERENCES (docman->priv->preferences),
									EDITOR_TABS_ORDERING))
			anjuta_docman_order_tabs (docman);
	
	gtk_signal_handler_unblock_by_func (GTK_OBJECT (docman),
			    GTK_SIGNAL_FUNC (on_notebook_switch_page),
Naba Kumar's avatar
Naba Kumar committed
483
			    docman);
484 485 486
	g_signal_connect (TEXT_EDITOR (te), "save_point",
					  G_CALLBACK (on_editor_save_point), docman);
	
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	anjuta_docman_set_current_editor(docman, TEXT_EDITOR (te));
	return TEXT_EDITOR (te);
}

static AnjutaDocmanPage *
anjuta_docman_page_from_widget (AnjutaDocman *docman, TextEditor *te)
{
	GList *node;
	node = docman->priv->editors;
	while (node)
	{
		AnjutaDocmanPage *page;
		page = node->data;
		g_assert (page);
		if (page->widget == GTK_WIDGET (te))
			return page;
		node = g_list_next (node);
	}
	return NULL;
}

void
anjuta_docman_remove_editor (AnjutaDocman *docman, TextEditor* te)
{
	// GtkWidget *submenu;
	// GtkWidget *wid;
	AnjutaDocmanPage *page;
Naba Kumar's avatar
Naba Kumar committed
514
	gint page_num;
515 516 517 518 519 520 521 522 523
	
	if (!te)
		te = anjuta_docman_get_current_editor (docman);

	if (te == NULL)
		return;

	page = anjuta_docman_page_from_widget (docman, te);

Johannes Schmid's avatar
Johannes Schmid committed
524
	gtk_signal_handler_block_by_func (GTK_OBJECT (docman),
Naba Kumar's avatar
Naba Kumar committed
525
				       GTK_SIGNAL_FUNC (on_notebook_switch_page),
Johannes Schmid's avatar
Johannes Schmid committed
526
				       docman);
527 528 529
	g_signal_handlers_disconnect_by_func (G_OBJECT (te),
										  G_CALLBACK (on_editor_save_point),
										  docman);
530
#if 0 // FIXME
Johannes Schmid's avatar
Johannes Schmid committed
531
	if (te->uri != NULL)
532 533 534 535 536 537 538 539 540
	{
		gint max_recent_files;
		GtkWidget *recent_submenu;

		max_recent_files =
			anjuta_preferences_get_int (app->preferences,
					     MAXIMUM_RECENT_FILES);
		app->recent_files =
			glist_path_dedup(update_string_list (dockman->priv->recent_files,
Johannes Schmid's avatar
Johannes Schmid committed
541
					    te->uri, max_recent_files));
542 543 544 545 546 547 548 549 550 551 552
		submenu =
			create_submenu (_("Recent Files "), app->recent_files,
					GTK_SIGNAL_FUNC (on_recent_files_menu_item_activate));
		
		recent_submenu = egg_menu_merge_get_widget (anjuta_ui_get_menu_merge (docman->ui),
													"/MenuMain/MenuFile/RecentFiles");
		gtk_menu_item_set_submenu (GTK_MENU_ITEM(recent_submenu), submenu);
	}
	breakpoints_dbase_clear_all_in_editor (debugger.breakpoints_dbase, te);
#endif
	
Naba Kumar's avatar
Naba Kumar committed
553
	page_num = 
554
		gtk_notebook_page_num (GTK_NOTEBOOK (docman), GTK_WIDGET (te));
Naba Kumar's avatar
Naba Kumar committed
555
	gtk_notebook_remove_page (GTK_NOTEBOOK (docman), page_num);
556 557 558
	docman->priv->editors = g_list_remove (docman->priv->editors, page);
	anjuta_docman_page_destroy (page);
	
Naba Kumar's avatar
Naba Kumar committed
559
	if (docman->priv->current_editor == te)
560
		anjuta_docman_set_current_editor (docman, NULL);
Naba Kumar's avatar
Naba Kumar committed
561
	
562 563 564 565
	/* This is called to set the next active document */
	if (GTK_NOTEBOOK (docman)->children == NULL)
		anjuta_docman_set_current_editor (docman, NULL);
	else
Naba Kumar's avatar
Naba Kumar committed
566 567 568 569 570 571 572 573
	{
		GtkWidget *current_editor;
		page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (docman));
		current_editor = gtk_notebook_get_nth_page (GTK_NOTEBOOK (docman),
													page_num);
		anjuta_docman_set_current_editor (docman, TEXT_EDITOR (current_editor));
		gtk_widget_grab_focus (GTK_WIDGET (current_editor));
	}
Johannes Schmid's avatar
Johannes Schmid committed
574
	gtk_signal_handler_unblock_by_func (GTK_OBJECT (docman),
575
			    GTK_SIGNAL_FUNC (on_notebook_switch_page),
Johannes Schmid's avatar
Johannes Schmid committed
576
			    docman);
577 578
}

Naba Kumar's avatar
Naba Kumar committed
579 580 581 582 583 584
void
anjuta_docman_set_popup_menu (AnjutaDocman *docman, GtkWidget *menu)
{
	docman->priv->popup_menu = menu;
}

585 586 587 588 589 590 591 592 593 594 595
TextEditor *
anjuta_docman_get_current_editor (AnjutaDocman *docman)
{
	return docman->priv->current_editor;
}

void
anjuta_docman_set_current_editor (AnjutaDocman *docman, TextEditor * te)
{
	TextEditor *ote = docman->priv->current_editor;
	
596 597 598
	if (ote == te)
		return;
	
599 600 601 602 603 604 605 606 607 608 609 610
	if (ote != NULL)
	{
		AnjutaDocmanPage *page = anjuta_docman_page_from_widget (docman, ote);
		if (page && page->close_button != NULL)
		{
			gtk_widget_hide (page->close_button);
			gtk_widget_show (page->close_image);
		}
	}
	docman->priv->current_editor = te;
	if (te != NULL)
	{
Naba Kumar's avatar
Naba Kumar committed
611 612
		gint page_num;
		AnjutaDocmanPage *page = anjuta_docman_page_from_widget (docman, te);
613 614 615 616 617
		if (page && page->close_button != NULL)
		{
			gtk_widget_show (page->close_button);
			gtk_widget_hide (page->close_image);
		}
Naba Kumar's avatar
Naba Kumar committed
618 619 620 621 622 623 624 625 626 627 628 629 630
		page_num = gtk_notebook_page_num (GTK_NOTEBOOK (docman),
										  GTK_WIDGET (te));
		g_signal_handlers_block_by_func (GTK_OBJECT (docman),
						   GTK_SIGNAL_FUNC (on_notebook_switch_page),
						   docman);
		gtk_notebook_set_current_page (GTK_NOTEBOOK (docman), page_num);
		if (anjuta_preferences_get_int (ANJUTA_PREFERENCES (docman->priv->preferences),
										EDITOR_TABS_ORDERING))
				anjuta_docman_order_tabs (docman);
		
		gtk_signal_handler_unblock_by_func (GTK_OBJECT (docman),
					GTK_SIGNAL_FUNC (on_notebook_switch_page),
					docman);
631 632 633
	}
	if (te == NULL)
	{
Naba Kumar's avatar
Naba Kumar committed
634 635
		const gchar *dir = g_get_home_dir();
		chdir (dir);
636
	}
637
	else if (te->uri)
638 639
	{
		gchar* dir;
Johannes Schmid's avatar
Johannes Schmid committed
640
		dir = g_dirname (te->uri);
641 642 643
		chdir (dir);
		g_free (dir);
	}
644
	g_signal_emit_by_name (G_OBJECT (docman), "editor_changed", te);
645 646 647 648
	return;
}

TextEditor *
649
anjuta_docman_goto_file_line (AnjutaDocman *docman, const gchar *fname, glong lineno)
650 651 652 653 654
{
	return anjuta_docman_goto_file_line_mark (docman, fname, lineno, TRUE);
}

TextEditor *
655
anjuta_docman_goto_file_line_mark (AnjutaDocman *docman, const gchar *fname,
656 657
								   glong lineno, gboolean mark)
{
Johannes Schmid's avatar
Johannes Schmid committed
658 659
	gchar *uri;
	GnomeVFSURI* vfs_uri;
660 661 662 663 664 665
	GList *node;

	TextEditor *te;

	g_return_val_if_fail (fname, NULL);
	
Johannes Schmid's avatar
Johannes Schmid committed
666 667 668 669 670 671 672
	/*filename = anjuta_docman_get_full_filename (docman, fname);*/
	vfs_uri = gnome_vfs_uri_new(fname);
	uri = gnome_vfs_uri_to_string(vfs_uri, GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref(vfs_uri);
	/* g_free(filename); */

	g_return_val_if_fail (uri != NULL, NULL);
673 674 675 676 677 678 679 680
	
	node = docman->priv->editors;
	
	while (node)
	{
		AnjutaDocmanPage *page;
		page = (AnjutaDocmanPage *) node->data;
		te = TEXT_EDITOR (page->widget);
Johannes Schmid's avatar
Johannes Schmid committed
681
		if (te->uri == NULL)
682 683 684 685
		{
			node = g_list_next (node);
			continue;
		}
Johannes Schmid's avatar
Johannes Schmid committed
686
		if (strcmp (uri, te->uri) == 0)
687 688 689 690 691 692 693
		{
			text_editor_check_disk_status (te, TRUE);
			if (lineno >= 0)
				text_editor_goto_line (te, lineno, mark, TRUE);
			anjuta_docman_show_editor (docman, GTK_WIDGET (te));
			anjuta_docman_grab_text_focus (docman);
			gtk_widget_grab_focus (GTK_WIDGET (te));
Johannes Schmid's avatar
Johannes Schmid committed
694 695
			g_free (uri);
			// an_file_history_push(te->uri, lineno);
696 697 698 699
			return te;
		}
		node = g_list_next (node);
	}
Johannes Schmid's avatar
Johannes Schmid committed
700
	te = anjuta_docman_add_editor (docman, uri, NULL);
701 702
	if (te)
	{
Johannes Schmid's avatar
Johannes Schmid committed
703
		an_file_history_push(te->uri, lineno);
704 705 706
		if (lineno >= 0)
			text_editor_goto_line (te, lineno, mark, FALSE);
	}
Johannes Schmid's avatar
Johannes Schmid committed
707
	g_free (uri);
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	return te ;
}

static gchar*
get_real_path(const gchar *file_name)
{
	if (file_name)
	{
		gchar path[PATH_MAX+1];
		memset(path, '\0', PATH_MAX+1);
		realpath(file_name, path);
		return g_strdup(path);
	}
	else
		return NULL;
}

gchar *
anjuta_docman_get_full_filename (AnjutaDocman *docman, const gchar *fn)
{
	TextEditor *te;
	GList *te_list;
	gchar *real_path;
	gchar *fname;
	
	g_return_val_if_fail(fn, NULL);
	real_path = get_real_path (fn);
	
	/* If it is full and absolute path, there is no need to 
	go further, even if the file is not found*/
	if (fn[0] == '/')
Johannes Schmid's avatar
Johannes Schmid committed
739
	{
740
		return real_path;
Johannes Schmid's avatar
Johannes Schmid committed
741 742
	}
	
743 744 745 746 747 748 749 750 751 752 753 754 755 756
	/* First, check if we can get the file straightaway */
	if (g_file_test (real_path, G_FILE_TEST_IS_REGULAR))
		return real_path;
	g_free(real_path);

	/* Get the name part of the file */
	fname = g_path_get_basename (fn);
	
	/* Next, check if the current text editor buffer matches this name */
	if (NULL != (te = anjuta_docman_get_current_editor(docman)))
	{
		if (strcmp(te->filename, fname) == 0)
		{
			g_free (fname);
Johannes Schmid's avatar
Johannes Schmid committed
757
			return g_strdup (te->uri);
758 759 760 761 762 763 764 765 766 767 768
		}
	}
	/* Next, see if the name matches any of the opened files */
	for (te_list = docman->priv->editors; te_list; te_list = g_list_next (te_list))
	{
		AnjutaDocmanPage *page;
		page = (AnjutaDocmanPage *) te_list->data;
		te = TEXT_EDITOR (page->widget);
		if (strcmp(fname, te->filename) == 0)
		{
			g_free (fname);
Johannes Schmid's avatar
Johannes Schmid committed
769
			return g_strdup(te->uri);
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		}
	}
	g_free (fname);
	return NULL;
}

void
anjuta_docman_show_editor (AnjutaDocman *docman, GtkWidget* te)
{
	GList *node;
	gint i;

	if (te == NULL)
		return;
	node = GTK_NOTEBOOK (docman)->children;
	i = 0;
	while (node)
	{
		GtkWidget *t;

		t = gtk_notebook_get_nth_page (GTK_NOTEBOOK (docman), i);
		if (t && t == te)
		{
Naba Kumar's avatar
Naba Kumar committed
793
			gtk_notebook_set_current_page (GTK_NOTEBOOK (docman), i);
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
			anjuta_docman_set_current_editor (docman, TEXT_EDITOR (te));
			return;
		}
		i++;
		node = g_list_next (node);
	}
}

// FIXME: Should be called from a te callback
static void
anjuta_docman_update_page_label (AnjutaDocman *docman, GtkWidget *te)
{
	GdkColor tmpcolor;
	AnjutaDocmanPage *page;
	
	if (te == NULL)
		return;

	page = anjuta_docman_page_from_widget (docman, TEXT_EDITOR (te));
	if (!page || page->label == NULL)
		return;
	
	if (text_editor_is_saved(TEXT_EDITOR (te)))
	{
		gdk_color_parse("black",&tmpcolor);
	}
	else
	{
		gdk_color_parse("red",&tmpcolor);
	}
	gtk_widget_modify_fg (page->label, GTK_STATE_NORMAL, &tmpcolor);
	gtk_widget_modify_fg (page->label, GTK_STATE_INSENSITIVE, &tmpcolor);
	gtk_widget_modify_fg (page->label, GTK_STATE_ACTIVE, &tmpcolor);
	gtk_widget_modify_fg (page->label, GTK_STATE_PRELIGHT, &tmpcolor);
	gtk_widget_modify_fg (page->label, GTK_STATE_SELECTED, &tmpcolor);
}

#if 0
void anjuta_set_zoom_factor(gint zoom)
{
	TextEditor *te = anjuta_get_current_text_editor();
	if (te)
		text_editor_set_zoom_factor(te, zoom);
}
#endif

static void
anjuta_docman_grab_text_focus (AnjutaDocman *docman)
{
	GtkWidget *text;
	text = GTK_WIDGET (anjuta_docman_get_current_editor (docman));
	if (!text)
		return; // This is not an error condition.
	text_editor_grab_focus (TEXT_EDITOR (text));
}

void
anjuta_docman_delete_all_markers (AnjutaDocman *docman, gint marker)
{
	GList *node;
	AnjutaDocmanPage *page;
	node = docman->priv->editors;
	
	while (node)
	{
		TextEditor* te;
		page = node->data;
		te = TEXT_EDITOR (page->widget);
		text_editor_delete_marker_all (te, marker);
		node = g_list_next (node);
	}
}

void
anjuta_docman_delete_all_indicators (AnjutaDocman *docman)
{
	GList *node;
	TextEditor *te;

	node = docman->priv->editors;
	while (node)
	{
		AnjutaDocmanPage *page;
		page = node->data;
		te = TEXT_EDITOR (page->widget);
Johannes Schmid's avatar
Johannes Schmid committed
879
		if (te->uri == NULL)
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
		{
			node = g_list_next (node);
			continue;
		}
		text_editor_set_indicator (te, -1, -1);
		node = g_list_next (node);
	}
}

TextEditor *
anjuta_docman_get_editor_from_path (AnjutaDocman *docman, const gchar *szFullPath )
{
	GList *node;
	AnjutaDocmanPage *page;
	TextEditor *te;

	g_return_val_if_fail (szFullPath != NULL, NULL);
	node = docman->priv->editors;
	while (node)
	{
		page = node->data;
		te = (TextEditor *) page->widget;
Johannes Schmid's avatar
Johannes Schmid committed
902
		if (te->uri != NULL)
903
		{
Johannes Schmid's avatar
Johannes Schmid committed
904
			if ( !strcmp ( szFullPath, te->uri) )
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
			{
				return te ;
			}
		}
		node = g_list_next (node);
	}
	return NULL;
}

/* Saves a file to keep it synchronized with external programs */
void 
anjuta_docman_save_file_if_modified (AnjutaDocman *docman, const gchar *szFullPath)
{
	TextEditor *te;

	g_return_if_fail ( szFullPath != NULL );

	te = anjuta_docman_get_editor_from_path (docman, szFullPath);
	if( NULL != te )
	{
		if( !text_editor_is_saved (te) )
		{
			text_editor_save_file (te, TRUE);
		}
	}
	return;
}

/* If an external program changed the file, we must reload it */
void 
anjuta_docman_reload_file (AnjutaDocman *docman, const gchar *szFullPath)
{
	TextEditor *te;

	g_return_if_fail (szFullPath != NULL);

	te = anjuta_docman_get_editor_from_path (docman, szFullPath);
	if( NULL != te )
	{
		glong	nNowPos = te->current_line ;
		/*text_editor_check_disk_status ( te, TRUE );asd sdf*/
		text_editor_load_file (te);
		text_editor_goto_line (te,  nNowPos, FALSE, FALSE);
	}
	return;
}

typedef struct _order_struct order_struct;
struct _order_struct
{
	gchar *m_label;
	GtkWidget *m_widget;
};

static int
do_ordertab1 (const void *a, const void *b)
{
	order_struct aos,bos;
	aos = *(order_struct*)a;
	bos = *(order_struct*)b;
	return (g_strcasecmp (aos.m_label, bos.m_label));
}

static void
anjuta_docman_order_tabs(AnjutaDocman *docman)
{
	gint i,j;
	GList *children;
	GtkWidget *widget,*label;
	order_struct *tab_labels;
	children = gtk_container_children (GTK_CONTAINER(docman));
	
	j = g_list_length(children);
	if (j<2)
		return;
	tab_labels = g_new0(order_struct,j);
	for (i=0;i<j;i++)
	{
		widget = gtk_notebook_get_nth_page (GTK_NOTEBOOK(docman), i);
		if (widget)
		{
			label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (docman), widget);
			children = gtk_container_children (GTK_CONTAINER (label));
			for (; children; children = g_list_next(children))
			{
				if (GTK_IS_LABEL (children->data))
				{
					tab_labels[i].m_label = GTK_LABEL (children->data)->label;
					tab_labels[i].m_widget = widget;
				}
			}
		}
	}
	qsort (tab_labels, j , sizeof(order_struct), do_ordertab1);
	for (i=0;i<j;i++)
		gtk_notebook_reorder_child (GTK_NOTEBOOK (docman),
									tab_labels[i].m_widget, i);
	g_free (tab_labels);
}

gboolean
anjuta_docman_set_editor_properties (AnjutaDocman *docman)
{
	TextEditor *te = docman->priv->current_editor;
	if (te)
	{
		gchar *word;
Johannes Schmid's avatar
Johannes Schmid committed
1012
		// FIXME: anjuta_set_file_properties (app->current_text_editor->uri);
1013
		word = text_editor_get_current_word (docman->priv->current_editor);
1014
		prop_set_with_key(te->props_base, "current.file.selection"
1015 1016 1017
		  , word?word:"");
		if (word)
			g_free(word);
1018
		prop_set_int_with_key(te->props_base, "current.file.lineno"
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
		  , text_editor_get_current_lineno(docman->priv->current_editor));
		return TRUE;
	}
	else
		return FALSE;
}

TextEditor*
anjuta_docman_find_editor_with_path (AnjutaDocman *docman,
									 const gchar *file_path)
{
	TextEditor *te;
	GList *tmp;
	
	te = NULL;
	for (tmp = docman->priv->editors; tmp; tmp = g_list_next(tmp))
	{
		AnjutaDocmanPage *page;
		page = (AnjutaDocmanPage *) tmp->data;
		if (!page)
			continue;
		te = (TextEditor *) page->widget;
		if (!te)
			continue;
Johannes Schmid's avatar
Johannes Schmid committed
1043
		if (te->uri && 0 == strcmp(file_path, te->uri))
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
			break;
	}
	return te;
}

GList*
anjuta_docman_get_all_editors (AnjutaDocman *docman)
{
	GList *editors;
	GList *node;
	
	editors = NULL;
	node = docman->priv->editors;
	while (node)
	{
		AnjutaDocmanPage *page;
		TextEditor *te;
		page = (AnjutaDocmanPage *) node->data;
		te = (TextEditor *) page->widget;
		editors = g_list_prepend (editors, te);
		node = g_list_next (node);
	}
	editors = g_list_reverse (editors);
	return editors;
}
1069 1070 1071

ANJUTA_TYPE_BEGIN(AnjutaDocman, anjuta_docman, GTK_TYPE_NOTEBOOK);
ANJUTA_TYPE_END;