⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 document.c

📁 一个简单的文本编辑器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- *//* * gedit * * Copyright (C) 1998, 1999, 2000  Alex Roberts, Evan Lawrence, Jason Leach, Jose M Celorio * * 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 <config.h>#include <gnome.h>#include <libgnomevfs/gnome-vfs.h>#include "window.h"#include "undo.h"#include "file.h"#include "utils.h"#include "document.h"#include "prefs.h"#include "menus.h"#include "commands.h"GnomeMDI *mdi;typedef void (*gedit_document_signal) (GtkObject *, gpointer, gpointer);static GnomeMDIChildClass *parent_class = NULL;static gchar*		gedit_document_get_config_string (GnomeMDIChild *child);static gint		remove_child_cb (GnomeMDI *mdi, GeditDocument *doc);static	GtkWidget*	gedit_document_create_view (GnomeMDIChild *child);static	void		gedit_document_destroy (GtkObject *obj);static	void		gedit_document_class_init (GeditDocumentClass *class);static	void		gedit_document_init (GeditDocument *doc);	GtkType		gedit_document_get_type (void);/* ----- Local structs ------------ */struct _GeditDocumentClass{	GnomeMDIChildClass parent_class;	void (*document_changed)(GeditDocument *, gpointer);	};/** * gedit_document_insert_text: Inserts text to a document and hadles all the details like *                             adding the data to the undo stack and adding it to multiple views *                             Provides an abstraction layer between gedit and the text widget. * @doc: document to insert text * @text: null terminated text to insert * @position: position to insert the text at * @undoable: if this insertion gets added to the undo stack or not. *  * **/voidgedit_document_insert_text (GeditDocument *doc, const guchar *text, guint position, gint undoable){	GeditView *view;	GtkText *text_widget;	GtkWidget *editable;	gint length;	gint exclude_this_view;	if (doc->readonly)	     return;	g_return_if_fail (text!=NULL);	view = g_list_nth_data (doc->views, 0);	g_return_if_fail (view!=NULL);	text_widget = GTK_TEXT (view->text);	g_return_if_fail (text_widget!=NULL);	editable = GTK_WIDGET (text_widget);	g_return_if_fail (editable!=NULL);	length = strlen (text);	exclude_this_view = FALSE;	doc_insert_text_real_cb (editable, text, length, &position, view, exclude_this_view, undoable);}/** * gedit_document_delete_text: Deletes text from a document and hadles all the details like *                             adding the data to the undo stack and deleting it from multiple views *                             Provides an abstraction layer between gedit and the text widget. * @doc: document to delete text from * @position: position to delete the text from * @length: length of the text to delete * @undoable: if this deletion gets added to the undo stack or not. *  * **/voidgedit_document_delete_text (GeditDocument *doc, guint position, gint length, gint undoable){	GeditView *view;	GtkText *text_widget;	GtkWidget *editable;	gint exclude_this_view;	if (doc->readonly)	     return;	view = g_list_nth_data (doc->views, 0);	g_return_if_fail (view!=NULL);	text_widget = GTK_TEXT (view->text);	g_return_if_fail (text_widget!=NULL);	editable = GTK_WIDGET (text_widget);	g_return_if_fail (editable!=NULL);	exclude_this_view = FALSE;	doc_delete_text_real_cb (editable, position, position+length, view, exclude_this_view, undoable);}/** * gedit_document_replace_text: * @doc:  * @text:  * @position:  * @length:  * @undoable:  *  * is equivalent to delete_text + insert_text **/voidgedit_document_replace_text (GeditDocument *doc, const guchar * text_to_insert,			     gint length, guint position, gint undoable){	gchar *text_to_delete;	gedit_debug (DEBUG_DOCUMENT, "");	g_return_if_fail (doc != NULL);	g_return_if_fail (text_to_insert != NULL);	text_to_delete = gedit_document_get_chars (doc, position, position + length);	g_return_if_fail (text_to_delete != NULL);	if (undoable)	{		gint   text_to_delete_length;		gint   text_to_insert_length;		text_to_delete_length = strlen (text_to_delete);		text_to_insert_length = strlen (text_to_insert);		gedit_undo_add (text_to_delete,				position,				position + length,				GEDIT_UNDO_ACTION_REPLACE_DELETE,				doc,				NULL);		gedit_undo_add (text_to_insert,				position,				position + text_to_insert_length,				GEDIT_UNDO_ACTION_REPLACE_INSERT,				doc,				NULL);	}	gedit_document_delete_text (doc, position, length, FALSE);	gedit_document_insert_text (doc, text_to_insert, position, FALSE);		g_free (text_to_delete);}voidgedit_document_set_readonly (GeditDocument *doc, gint readonly){	GeditView * nth_view;	gint n;		gedit_debug (DEBUG_DOCUMENT, "");	g_return_if_fail(doc != NULL);		doc->readonly = readonly;	doc->changed = FALSE;			for (n = 0; n < g_list_length (doc->views); n++)	{		nth_view = g_list_nth_data (doc->views, n);		gedit_view_set_readonly (nth_view, doc->readonly);	}}voidgedit_document_text_changed_signal_connect (GeditDocument *doc){	GeditView *nth_view;	gint n;		for (n = 0; n < g_list_length (doc->views); n++)	{		nth_view = g_list_nth_data (doc->views, n);		g_return_if_fail (nth_view != NULL);		nth_view->view_text_changed_signal =			gtk_signal_connect (GTK_OBJECT(nth_view->text), "changed",					    GTK_SIGNAL_FUNC (gedit_view_text_changed_cb), nth_view);	}  }/** * gedit_document_get_tab_name: * @doc:  *  * determines the correct tab name for doc and assings a untitled * number if necessary * * Return Value: a potiner to a newly allocated string  **/gchar*gedit_document_get_tab_name (GeditDocument *doc, gboolean star){	GeditDocument *nth_doc;	int max_number = 0;	int i;	gedit_debug (DEBUG_DOCUMENT, "");		g_return_val_if_fail (doc != NULL, g_strdup ("?"));		if (doc->filename != NULL)	{		gchar * tab_name = NULL;		gchar * unescaped_str = gnome_vfs_unescape_string_for_display (doc->filename);		if(unescaped_str == NULL) 		{			g_warning ("unescaped_str == NULL in gedit_document_get_tab_name ()");			unescaped_str = g_strdup (doc->filename);		}				if (!doc->changed || !star)		{			tab_name = g_strdup_printf ("%s%s", doc->readonly?_("RO - "):"", g_basename(unescaped_str));		}		else		{						tab_name = g_strdup_printf ("%s%s*", doc->readonly?_("RO - "):"", g_basename(unescaped_str));		}		g_free (unescaped_str);				return tab_name;	}	else	{	        if (doc->untitled_number == 0)		{			for (i = 0; i < g_list_length (mdi->children); i++)			{				nth_doc = (GeditDocument *)g_list_nth_data (mdi->children, i);								if (nth_doc->untitled_number > max_number)				{					max_number = nth_doc->untitled_number;				}			}			doc->untitled_number = max_number + 1;		}		if (!doc->changed || !star)		{			return _(g_strdup_printf ("%s %d", _("Untitled"), doc->untitled_number));		}		else		{			return _(g_strdup_printf ("%s %d*", _("Untitled"), doc->untitled_number));		}	}}guchar *gedit_document_get_chars (GeditDocument *doc, guint start_pos, guint end_pos){	guchar * buffer;	GtkText * text;	GeditView * view;	gedit_debug (DEBUG_DOCUMENT, "");	g_return_val_if_fail (doc!=NULL, NULL);	g_return_val_if_fail (end_pos > start_pos, NULL);	view = g_list_nth_data (doc->views, 0);	g_return_val_if_fail (view!=NULL, NULL);	text = GTK_TEXT (view->text);	buffer = gtk_editable_get_chars ( GTK_EDITABLE ( text ),					  start_pos,					  end_pos);	return buffer;}/** * gedit_document_get_buffer: * @doc:  *  * returns a newly allocated buffer containg the text inside doc *  * Return Value:  **/guchar *gedit_document_get_buffer (GeditDocument *doc){	guchar * buffer;	guint length;	GtkText * text;	GeditView * view;	gedit_debug (DEBUG_DOCUMENT, "");	g_return_val_if_fail (doc!=NULL, NULL);	view = g_list_nth_data (doc->views, 0);	g_return_val_if_fail (view!=NULL, NULL);	text = GTK_TEXT (view->text);	length = gtk_text_get_length (text);	buffer = gtk_editable_get_chars ( GTK_EDITABLE ( text ),					  0,					  length);	return buffer;}guintgedit_document_get_buffer_length (GeditDocument *doc){	guint length;	GeditView * view;	gedit_debug (DEBUG_DOCUMENT, "");	g_return_val_if_fail (doc!=NULL, 0);	view = g_list_nth_data (doc->views, 0);	g_return_val_if_fail (view!=NULL, 0);	length = gtk_text_get_length (GTK_TEXT (view->text));	return length;}GeditDocument *gedit_document_new (void){	GeditDocument *doc;	gedit_debug (DEBUG_DOCUMENT, "");	doc = gtk_type_new (gedit_document_get_type ());	g_return_val_if_fail (doc != NULL, NULL);	gnome_mdi_add_child (mdi, GNOME_MDI_CHILD (doc));	gnome_mdi_add_view (mdi, GNOME_MDI_CHILD (doc));	gedit_window_set_widgets_sensitivity (TRUE);	gedit_window_set_toolbar_labels (GEDIT_VIEW(doc->views->data)->app);	return doc;}GeditDocument *gedit_document_new_with_title (const gchar *title){	GeditDocument *doc;		gedit_debug (DEBUG_DOCUMENT, "");	g_return_val_if_fail (title != NULL, NULL);	doc = gtk_type_new (gedit_document_get_type ());	g_return_val_if_fail (doc != NULL, NULL);	doc->filename = g_strdup (title);		gnome_mdi_add_child (mdi, GNOME_MDI_CHILD (doc));	gnome_mdi_add_view (mdi, GNOME_MDI_CHILD (doc));	gedit_window_set_widgets_sensitivity (TRUE);	return doc;}gintgedit_document_new_with_file (const gchar *file_name){	GeditDocument *doc;	gedit_debug (DEBUG_DOCUMENT, "");	doc = gedit_document_current();	if (doc!=NULL) 		if (doc->changed || doc->filename)			doc = NULL;	if (gedit_file_open (doc, file_name) == 1)		return FALSE;		gedit_window_set_widgets_sensitivity (TRUE);	return TRUE;}GeditDocument *gedit_document_current (void){	GeditDocument *current_document = NULL;	if (mdi->active_child)		current_document = GEDIT_DOCUMENT (mdi->active_child); 	return current_document;}static gchar *gedit_document_get_config_string (GnomeMDIChild *child){	gedit_debug (DEBUG_DOCUMENT, "");	return g_strdup_printf ("%d", GPOINTER_TO_INT (gtk_object_get_user_data (GTK_OBJECT (child))));}static gintremove_child_cb (GnomeMDI *mdi, GeditDocument *doc){	gedit_debug (DEBUG_DOCUMENT, "start");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -