Don't use deprecated GtkToolbar API in GimpTextEditor:

2004-11-04  Michael Natterer  <mitch@gimp.org>

	Don't use deprecated GtkToolbar API in GimpTextEditor:

	* app/actions/Makefile.am
	* app/actions/actions.c
	* app/actions/text-editor-actions.[ch]
	* app/actions/text-editor-commands.[ch]: added acions and
	callbacks for the new "text-editor" action group.

	* app/menus/menus.c: register a "<TextEditor>" UI manager.

	* menus/Makefile.am
	* menus/text-editor-toolbar.xml: new file for the toolbar.

	* app/widgets/gimptexteditor.[ch]: use the toolbar created by the
	UI manager instead of constructing it using deprecated API.

	* app/tools/gimptextoptions.c: changed accordingly.

	* app/widgets/gimpwidgets-utils.[ch]: added gimp_text_buffer_load()
	(used by text-editor-commands.c).
This commit is contained in:
Michael Natterer 2004-11-04 14:24:32 +00:00 committed by Michael Natterer
parent 04f914f1a6
commit 5d7b121fd7
15 changed files with 467 additions and 224 deletions

View file

@ -1,3 +1,26 @@
2004-11-04 Michael Natterer <mitch@gimp.org>
Don't use deprecated GtkToolbar API in GimpTextEditor:
* app/actions/Makefile.am
* app/actions/actions.c
* app/actions/text-editor-actions.[ch]
* app/actions/text-editor-commands.[ch]: added acions and
callbacks for the new "text-editor" action group.
* app/menus/menus.c: register a "<TextEditor>" UI manager.
* menus/Makefile.am
* menus/text-editor-toolbar.xml: new file for the toolbar.
* app/widgets/gimptexteditor.[ch]: use the toolbar created by the
UI manager instead of constructing it using deprecated API.
* app/tools/gimptextoptions.c: changed accordingly.
* app/widgets/gimpwidgets-utils.[ch]: added gimp_text_buffer_load()
(used by text-editor-commands.c).
2004-11-04 Michael Natterer <mitch@gimp.org>
* plug-ins/ifscompose/ifscompose.c: #undef GTK_DISABLE_DEPRECATED.

View file

@ -113,6 +113,10 @@ libappactions_a_SOURCES = \
templates-actions.h \
templates-commands.c \
templates-commands.h \
text-editor-actions.c \
text-editor-actions.h \
text-editor-commands.c \
text-editor-commands.h \
tool-options-actions.c \
tool-options-actions.h \
tool-options-commands.c \

View file

@ -72,6 +72,7 @@
#include "qmask-actions.h"
#include "select-actions.h"
#include "templates-actions.h"
#include "text-editor-actions.h"
#include "tool-options-actions.h"
#include "tools-actions.h"
#include "vectors-actions.h"
@ -170,6 +171,9 @@ static GimpActionFactoryEntry action_groups[] =
{ "templates", N_("Templates"), GIMP_STOCK_TEMPLATE,
templates_actions_setup,
templates_actions_update },
{ "text-editor", N_("Text Editor"), GIMP_STOCK_EDIT,
text_editor_actions_setup,
text_editor_actions_update },
{ "tool-options", N_("Tool Options"), GIMP_STOCK_TOOL_OPTIONS,
tool_options_actions_setup,
tool_options_actions_update },

View file

@ -0,0 +1,107 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <gtk/gtk.h>
#include "libgimpwidgets/gimpwidgets.h"
#include "actions-types.h"
#include "widgets/gimpactiongroup.h"
#include "widgets/gimptexteditor.h"
#include "widgets/gimphelp-ids.h"
#include "text-editor-actions.h"
#include "text-editor-commands.h"
#include "gimp-intl.h"
static GimpActionEntry text_editor_actions[] =
{
{ "text-editor-toolbar", GIMP_STOCK_EDIT,
"Text Editor Toolbar", NULL, NULL, NULL,
GIMP_HELP_TEXT_EDITOR_DIALOG },
{ "text-editor-load", GTK_STOCK_OPEN,
N_("Open"), "",
N_("Load text from file"),
G_CALLBACK (text_editor_load_cmd_callback),
NULL },
{ "text-editor-clear", GTK_STOCK_CLEAR,
N_("Clear"), "",
N_("Clear all text"),
G_CALLBACK (text_editor_clear_cmd_callback),
NULL }
};
static GimpRadioActionEntry text_editor_direction_actions[] =
{
{ "text-editor-direction-ltr", GIMP_STOCK_TEXT_DIR_LTR,
N_("LTR"), "",
N_("From left to right"),
GIMP_TEXT_DIRECTION_LTR,
NULL },
{ "text-editor-direction-rtl", GIMP_STOCK_TEXT_DIR_RTL,
N_("RTL"), "",
N_("From right to left"),
GIMP_TEXT_DIRECTION_RTL,
NULL }
};
void
text_editor_actions_setup (GimpActionGroup *group)
{
gimp_action_group_add_actions (group,
text_editor_actions,
G_N_ELEMENTS (text_editor_actions));
gimp_action_group_add_radio_actions (group,
text_editor_direction_actions,
G_N_ELEMENTS (text_editor_direction_actions),
GIMP_TEXT_DIRECTION_LTR,
G_CALLBACK (text_editor_direction_cmd_callback));
}
void
text_editor_actions_update (GimpActionGroup *group,
gpointer data)
{
GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
#define SET_ACTIVE(action,condition) \
gimp_action_group_set_action_active (group, action, (condition) != 0)
switch (editor->base_dir)
{
case GIMP_TEXT_DIRECTION_LTR:
SET_ACTIVE ("text-editor-direction-ltr", TRUE);
break;
case GIMP_TEXT_DIRECTION_RTL:
SET_ACTIVE ("text-editor-direction-rtl", TRUE);
break;
}
#undef SET_ACTIVE
}

View file

@ -0,0 +1,28 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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.
*/
#ifndef __TEXT_EDITOR_ACIONS_H__
#define __TEXT_EDITOR_ACIONS_H__
void text_editor_actions_setup (GimpActionGroup *group);
void text_editor_actions_update (GimpActionGroup *group,
gpointer data);
#endif /* __TEXT_EDITOR_ACTIONS_H__ */

View file

@ -0,0 +1,143 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "actions-types.h"
#include "widgets/gimptexteditor.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpwidgets-utils.h"
#include "text-editor-commands.h"
#include "gimp-intl.h"
/* local function prototypes */
static void text_editor_load_response (GtkWidget *dialog,
gint response_id,
GimpTextEditor *editor);
/* public functions */
void
text_editor_load_cmd_callback (GtkAction *action,
gpointer data)
{
GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
GtkFileChooser *chooser;
if (editor->file_dialog)
{
gtk_window_present (GTK_WINDOW (editor->file_dialog));
return;
}
editor->file_dialog =
gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
GTK_WINDOW (editor),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
chooser = GTK_FILE_CHOOSER (editor->file_dialog);
g_object_add_weak_pointer (G_OBJECT (chooser),
(gpointer) &editor->file_dialog);
gtk_window_set_role (GTK_WINDOW (chooser), "gimp-text-load-file");
gtk_window_set_position (GTK_WINDOW (chooser), GTK_WIN_POS_MOUSE);
gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser), TRUE);
g_signal_connect (chooser, "response",
G_CALLBACK (text_editor_load_response),
editor);
g_signal_connect (chooser, "delete_event",
G_CALLBACK (gtk_true),
NULL);
gtk_widget_show (GTK_WIDGET (chooser));
}
void
text_editor_clear_cmd_callback (GtkAction *action,
gpointer data)
{
GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
gtk_text_buffer_set_text (buffer, "", 0);
}
void
text_editor_direction_cmd_callback (GtkAction *action,
GtkAction *current,
gpointer data)
{
GimpTextEditor *editor = GIMP_TEXT_EDITOR (data);
gint value;
value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action));
gimp_text_editor_set_direction (editor, (GimpTextDirection) value);
}
/* private functions */
static void
text_editor_load_response (GtkWidget *dialog,
gint response_id,
GimpTextEditor *editor)
{
if (response_id == GTK_RESPONSE_OK)
{
GtkTextBuffer *buffer;
gchar *filename;
GError *error = NULL;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (! gimp_text_buffer_load (buffer, filename, &error))
{
g_message (_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), error->message);
g_clear_error (&error);
g_free (filename);
return;
}
g_free (filename);
}
gtk_widget_destroy (dialog);
}

View file

@ -0,0 +1,32 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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.
*/
#ifndef __TEXT_EDITOR_COMMANDS_H__
#define __TEXT_EDITOR_COMMANDS_H__
void text_editor_load_cmd_callback (GtkAction *action,
gpointer data);
void text_editor_clear_cmd_callback (GtkAction *action,
gpointer data);
void text_editor_direction_cmd_callback (GtkAction *action,
GtkAction *current,
gpointer data);
#endif /* __TEXT_EDITOR_COMMANDS_H__ */

View file

@ -279,6 +279,14 @@ menus_init (Gimp *gimp,
"tool-options-menu.xml",
tool_options_menu_setup,
NULL);
gimp_menu_factory_manager_register (global_menu_factory, "<TextEditor>",
"text-editor",
NULL,
"/text-editor-toolbar",
"text-editor-toolbar.xml",
NULL,
NULL);
}
void

View file

@ -36,6 +36,8 @@
#include "text/gimptext.h"
#include "widgets/gimpcolorpanel.h"
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpdock.h"
#include "widgets/gimppropwidgets.h"
#include "widgets/gimptexteditor.h"
#include "widgets/gimpviewablebox.h"
@ -403,6 +405,8 @@ gimp_text_options_gui (GimpToolOptions *tool_options)
vbox = gimp_tool_options_gui (tool_options);
g_object_set_data (G_OBJECT (tool_options), "tool-options-vbox", vbox);
table = gtk_table_new (9, 3, FALSE);
gtk_table_set_col_spacings (GTK_TABLE (table), 2);
gtk_table_set_row_spacings (GTK_TABLE (table), 2);
@ -508,12 +512,21 @@ GtkWidget *
gimp_text_options_editor_new (GimpTextOptions *options,
const gchar *title)
{
GtkWidget *editor;
GimpMenuFactory *menu_factory;
GtkWidget *vbox;
GtkWidget *toplevel;
GtkWidget *editor;
g_return_val_if_fail (GIMP_IS_TEXT_OPTIONS (options), NULL);
g_return_val_if_fail (title != NULL, NULL);
editor = gimp_text_editor_new (title);
vbox = g_object_get_data (G_OBJECT (options), "tool-options-vbox");
toplevel = gtk_widget_get_toplevel (vbox);
menu_factory = GIMP_DOCK (toplevel)->dialog_factory->menu_factory;
editor = gimp_text_editor_new (title, menu_factory);
gimp_text_editor_set_direction (GIMP_TEXT_EDITOR (editor),
options->base_dir);

View file

@ -21,23 +21,18 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "widgets-types.h"
#include "core/gimpmarshal.h"
#include "gimpenumwidgets.h"
#include "gimphelp-ids.h"
#include "gimpmenufactory.h"
#include "gimptexteditor.h"
#include "gimpuimanager.h"
#include "gimp-intl.h"
@ -53,20 +48,8 @@ enum
static void gimp_text_editor_class_init (GimpTextEditorClass *klass);
static void gimp_text_editor_init (GimpTextEditor *editor);
static void gimp_text_editor_text_changed (GtkTextBuffer *buffer,
GimpTextEditor *editor);
static void gimp_text_editor_dir_changed (GtkWidget *widget,
GimpTextEditor *editor);
static void gimp_text_editor_load (GtkWidget *widget,
GimpTextEditor *editor);
static void gimp_text_editor_load_response (GtkWidget *dialog,
gint response_id,
GimpTextEditor *editor);
static gboolean gimp_text_editor_load_file (GimpTextEditor *editor,
const gchar *filename);
static void gimp_text_editor_clear (GtkWidget *widget,
GimpTextEditor *editor);
static void gimp_text_editor_text_changed (GtkTextBuffer *buffer,
GimpTextEditor *editor);
static GimpDialogClass *parent_class = NULL;
@ -114,6 +97,7 @@ gimp_text_editor_class_init (GimpTextEditorClass *klass)
NULL, NULL,
gimp_marshal_VOID__VOID,
G_TYPE_NONE, 0);
text_editor_signals[DIR_CHANGED] =
g_signal_new ("dir_changed",
G_TYPE_FROM_CLASS (klass),
@ -131,8 +115,8 @@ static void
gimp_text_editor_init (GimpTextEditor *editor)
{
editor->view = NULL;
editor->group = NULL;
editor->file_dialog = NULL;
editor->ui_manager = NULL;
switch (gtk_widget_get_default_direction ())
{
@ -146,19 +130,20 @@ gimp_text_editor_init (GimpTextEditor *editor)
}
}
/* public functions */
GtkWidget *
gimp_text_editor_new (const gchar *title)
gimp_text_editor_new (const gchar *title,
GimpMenuFactory *menu_factory)
{
GimpTextEditor *editor;
GtkTextBuffer *buffer;
GtkToolbar *toolbar;
GtkWidget *toolbar;
GtkWidget *scrolled_window;
GtkWidget *box;
GtkWidget *button;
GList *children;
GList *list;
g_return_val_if_fail (title != NULL, NULL);
g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
editor = g_object_new (GIMP_TYPE_TEXT_EDITOR,
"title", title,
@ -174,49 +159,15 @@ gimp_text_editor_new (const gchar *title)
G_CALLBACK (gtk_widget_destroy),
NULL);
toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (editor)->vbox),
GTK_WIDGET (toolbar), FALSE, FALSE, 0);
gtk_widget_show (GTK_WIDGET (toolbar));
editor->ui_manager = gimp_menu_factory_manager_new (menu_factory,
"<TextEditor>",
editor, FALSE);
gtk_toolbar_insert_stock (toolbar, GTK_STOCK_OPEN,
_("Load text from file"), NULL,
G_CALLBACK (gimp_text_editor_load), editor,
0);
gtk_toolbar_insert_stock (toolbar, GTK_STOCK_CLEAR,
_("Clear all text"), NULL,
G_CALLBACK (gimp_text_editor_clear), editor,
1);
gtk_toolbar_append_space (toolbar);
box = gimp_enum_stock_box_new (GIMP_TYPE_TEXT_DIRECTION,
"gimp-text-dir",
gtk_toolbar_get_icon_size (toolbar),
G_CALLBACK (gimp_text_editor_dir_changed),
editor,
&editor->group);
gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (editor->group),
editor->base_dir);
children = gtk_container_get_children (GTK_CONTAINER (box));
for (list = children; list; list = g_list_next (list))
{
button = GTK_WIDGET (list->data);
g_object_ref (button);
gtk_container_remove (GTK_CONTAINER (box), button);
gtk_toolbar_append_widget (toolbar, button, NULL, NULL);
g_object_unref (button);
}
g_list_free (children);
gtk_object_sink (GTK_OBJECT (box));
toolbar = gimp_ui_manager_ui_get (editor->ui_manager,
"/text-editor-toolbar");
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (editor)->vbox), toolbar,
FALSE, FALSE, 0);
gtk_widget_show (toolbar);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
@ -251,6 +202,8 @@ gimp_text_editor_new (const gchar *title)
gtk_widget_grab_focus (editor->view);
gimp_ui_manager_update (editor->ui_manager, editor);
return GTK_WIDGET (editor);
}
@ -288,13 +241,6 @@ gimp_text_editor_get_text (GimpTextEditor *editor)
return gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
}
static void
gimp_text_editor_text_changed (GtkTextBuffer *buffer,
GimpTextEditor *editor)
{
g_signal_emit (editor, text_editor_signals[TEXT_CHANGED], 0);
}
void
gimp_text_editor_set_direction (GimpTextEditor *editor,
GimpTextDirection base_dir)
@ -306,16 +252,6 @@ gimp_text_editor_set_direction (GimpTextEditor *editor,
editor->base_dir = base_dir;
g_signal_handlers_block_by_func (editor->group,
G_CALLBACK (gimp_text_editor_dir_changed),
editor);
gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (editor->group), base_dir);
g_signal_handlers_unblock_by_func (editor->group,
G_CALLBACK (gimp_text_editor_dir_changed),
editor);
if (editor->view)
{
switch (editor->base_dir)
@ -329,6 +265,8 @@ gimp_text_editor_set_direction (GimpTextEditor *editor,
}
}
gimp_ui_manager_update (editor->ui_manager, editor);
g_signal_emit (editor, text_editor_signals[DIR_CHANGED], 0);
}
@ -340,140 +278,12 @@ gimp_text_editor_get_direction (GimpTextEditor *editor)
return editor->base_dir;
}
static void
gimp_text_editor_dir_changed (GtkWidget *widget,
GimpTextEditor *editor)
{
GimpTextDirection dir;
dir = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
"gimp-item-data"));
gimp_text_editor_set_direction (editor, dir);
}
/* private functions */
static void
gimp_text_editor_load (GtkWidget *widget,
GimpTextEditor *editor)
gimp_text_editor_text_changed (GtkTextBuffer *buffer,
GimpTextEditor *editor)
{
GtkFileChooser *chooser;
if (editor->file_dialog)
{
gtk_window_present (GTK_WINDOW (editor->file_dialog));
return;
}
editor->file_dialog =
gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
GTK_WINDOW (editor),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
NULL);
chooser = GTK_FILE_CHOOSER (editor->file_dialog);
g_object_add_weak_pointer (G_OBJECT (chooser),
(gpointer) &editor->file_dialog);
gtk_window_set_role (GTK_WINDOW (chooser), "gimp-text-load-file");
gtk_window_set_position (GTK_WINDOW (chooser), GTK_WIN_POS_MOUSE);
gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser), TRUE);
g_signal_connect (chooser, "response",
G_CALLBACK (gimp_text_editor_load_response),
editor);
g_signal_connect (chooser, "delete_event",
G_CALLBACK (gtk_true),
NULL);
gtk_widget_show (GTK_WIDGET (chooser));
}
static void
gimp_text_editor_load_response (GtkWidget *dialog,
gint response_id,
GimpTextEditor *editor)
{
if (response_id == GTK_RESPONSE_OK)
{
gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
if (! gimp_text_editor_load_file (editor, filename))
{
g_free (filename);
return;
}
g_free (filename);
}
gtk_widget_destroy (dialog);
}
static gboolean
gimp_text_editor_load_file (GimpTextEditor *editor,
const gchar *filename)
{
GtkTextBuffer *buffer;
FILE *file;
gchar buf[2048];
gint remaining = 0;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
file = fopen (filename, "r");
if (!file)
{
g_message (_("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno));
return FALSE;
}
gtk_text_buffer_set_text (buffer, "", 0);
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
while (!feof (file))
{
const char *leftover;
gint count;
gint to_read = sizeof (buf) - remaining - 1;
count = fread (buf + remaining, 1, to_read, file);
buf[count + remaining] = '\0';
g_utf8_validate (buf, count + remaining, &leftover);
gtk_text_buffer_insert (buffer, &iter, buf, leftover - buf);
remaining = (buf + remaining + count) - leftover;
g_memmove (buf, leftover, remaining);
if (remaining > 6 || count < to_read)
break;
}
if (remaining)
g_message (_("Invalid UTF-8 data in file '%s'."),
gimp_filename_to_utf8 (filename));
return TRUE;
}
static void
gimp_text_editor_clear (GtkWidget *widget,
GimpTextEditor *editor)
{
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
gtk_text_buffer_set_text (buffer, "", 0);
g_signal_emit (editor, text_editor_signals[TEXT_CHANGED], 0);
}

View file

@ -36,9 +36,9 @@ struct _GimpTextEditor
/*< private >*/
GimpTextDirection base_dir;
GtkWidget *group;
GtkWidget *view;
GtkWidget *file_dialog;
GimpUIManager *ui_manager;
};
struct _GimpTextEditorClass
@ -51,7 +51,8 @@ struct _GimpTextEditorClass
GType gimp_text_editor_get_type (void) G_GNUC_CONST;
GtkWidget * gimp_text_editor_new (const gchar *title);
GtkWidget * gimp_text_editor_new (const gchar *title,
GimpMenuFactory *menu_factory);
void gimp_text_editor_set_text (GimpTextEditor *editor,
const gchar *text,

View file

@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@ -744,13 +745,66 @@ gimp_dialog_set_sensitive (GtkDialog *dialog,
gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, sensitive);
}
gboolean
gimp_text_buffer_load (GtkTextBuffer *buffer,
const gchar *filename,
GError **error)
{
FILE *file;
gchar buf[2048];
gint remaining = 0;
GtkTextIter iter;
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
file = fopen (filename, "r");
if (! file)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
g_strerror (errno));
return FALSE;
}
gtk_text_buffer_set_text (buffer, "", 0);
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
while (! feof (file))
{
const char *leftover;
gint count;
gint to_read = sizeof (buf) - remaining - 1;
count = fread (buf + remaining, 1, to_read, file);
buf[count + remaining] = '\0';
g_utf8_validate (buf, count + remaining, &leftover);
gtk_text_buffer_insert (buffer, &iter, buf, leftover - buf);
remaining = (buf + remaining + count) - leftover;
g_memmove (buf, leftover, remaining);
if (remaining > 6 || count < to_read)
break;
}
if (remaining)
g_message (_("Invalid UTF-8 data in file '%s'."),
gimp_filename_to_utf8 (filename));
return TRUE;
}
gboolean
gimp_text_buffer_save (GtkTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,
GError **error)
{
GtkTextIter start_iter;
GtkTextIter start_iter;
GtkTextIter end_iter;
gint fd;
gchar *text_contents;

View file

@ -72,6 +72,9 @@ void gimp_window_set_hint (GtkWindow *window,
void gimp_dialog_set_sensitive (GtkDialog *dialog,
gboolean sensitive);
gboolean gimp_text_buffer_load (GtkTextBuffer *buffer,
const gchar *filename,
GError **error);
gboolean gimp_text_buffer_save (GtkTextBuffer *buffer,
const gchar *filename,
gboolean selection_only,

View file

@ -28,6 +28,7 @@ menudata_DATA = \
qmask-menu.xml \
selection-editor-menu.xml \
templates-menu.xml \
text-editor-toolbar.xml \
tool-options-menu.xml \
tools-menu.xml \
vectors-menu.xml

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ui SYSTEM "gtkuimanager.dtd">
<ui>
<toolbar action="text-editor-toolbar">
<toolitem action="text-editor-load" />
<toolitem action="text-editor-clear" />
<separator />
<toolitem action="text-editor-direction-ltr" />
<toolitem action="text-editor-direction-rtl" />
</toolbar>
</ui>