More error checking, Updates window title
This commit is contained in:
parent
ec5977f747
commit
6ac74326a5
89
src/editor.c
89
src/editor.c
@ -9,17 +9,31 @@
|
|||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* types */
|
||||||
|
struct _menu
|
||||||
|
{
|
||||||
|
char * name;
|
||||||
|
GtkSignalFunc callback;
|
||||||
|
char * stock;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _menubar
|
||||||
|
{
|
||||||
|
char * name;
|
||||||
|
struct _menu * menu;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* constants */
|
/* constants */
|
||||||
static char const * _authors[] =
|
static char const * _authors[] =
|
||||||
{
|
{
|
||||||
"Pierre 'khorben' Pronchery",
|
"Pierre 'khorben' Pronchery",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
static char const _license[] = "GPLv2";
|
static char const _license[] = "GPLv2";
|
||||||
|
|
||||||
/* Editor */
|
/* FIXME callbacks should be in a separate file */
|
||||||
static GtkWidget * _new_menubar(Editor * editor);
|
|
||||||
/* callbacks */
|
|
||||||
static void _on_close(GtkWidget * widget, gpointer data);
|
static void _on_close(GtkWidget * widget, gpointer data);
|
||||||
static gboolean _on_closex(GtkWidget * widget, GdkEvent * event, gpointer data);
|
static gboolean _on_closex(GtkWidget * widget, GdkEvent * event, gpointer data);
|
||||||
static void _on_edit_preferences(GtkWidget * widget, gpointer data);
|
static void _on_edit_preferences(GtkWidget * widget, gpointer data);
|
||||||
@ -31,17 +45,6 @@ static void _on_file_save_as(GtkWidget * widget, gpointer data);
|
|||||||
static void _on_help_about(GtkWidget * widget, gpointer data);
|
static void _on_help_about(GtkWidget * widget, gpointer data);
|
||||||
static void _on_new(GtkWidget * widget, gpointer data);
|
static void _on_new(GtkWidget * widget, gpointer data);
|
||||||
static void _on_open(GtkWidget * widget, gpointer data);
|
static void _on_open(GtkWidget * widget, gpointer data);
|
||||||
struct _menu
|
|
||||||
{
|
|
||||||
char * name;
|
|
||||||
GtkSignalFunc callback;
|
|
||||||
char * stock;
|
|
||||||
};
|
|
||||||
struct _menubar
|
|
||||||
{
|
|
||||||
char * name;
|
|
||||||
struct _menu * menu;
|
|
||||||
};
|
|
||||||
struct _menu _menu_file[] =
|
struct _menu _menu_file[] =
|
||||||
{
|
{
|
||||||
{ "_New", G_CALLBACK(_on_file_new), GTK_STOCK_NEW },
|
{ "_New", G_CALLBACK(_on_file_new), GTK_STOCK_NEW },
|
||||||
@ -53,6 +56,7 @@ struct _menu _menu_file[] =
|
|||||||
{ "_Close", G_CALLBACK(_on_file_close), GTK_STOCK_CLOSE },
|
{ "_Close", G_CALLBACK(_on_file_close), GTK_STOCK_CLOSE },
|
||||||
{ NULL, NULL, NULL }
|
{ NULL, NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _menu _menu_edit[] =
|
struct _menu _menu_edit[] =
|
||||||
{
|
{
|
||||||
{ "_Cut", NULL, GTK_STOCK_CUT },
|
{ "_Cut", NULL, GTK_STOCK_CUT },
|
||||||
@ -63,6 +67,7 @@ struct _menu _menu_edit[] =
|
|||||||
GTK_STOCK_PREFERENCES },
|
GTK_STOCK_PREFERENCES },
|
||||||
{ NULL, NULL, NULL }
|
{ NULL, NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _menu _menu_help[] =
|
struct _menu _menu_help[] =
|
||||||
{
|
{
|
||||||
#if GTK_CHECK_VERSION(2, 6, 0)
|
#if GTK_CHECK_VERSION(2, 6, 0)
|
||||||
@ -72,6 +77,7 @@ struct _menu _menu_help[] =
|
|||||||
#endif
|
#endif
|
||||||
{ NULL, NULL, NULL }
|
{ NULL, NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct _menubar _menubar[] =
|
static struct _menubar _menubar[] =
|
||||||
{
|
{
|
||||||
{ "_File", _menu_file },
|
{ "_File", _menu_file },
|
||||||
@ -79,6 +85,11 @@ static struct _menubar _menubar[] =
|
|||||||
{ "_Help", _menu_help },
|
{ "_Help", _menu_help },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* Editor */
|
||||||
|
static void _new_set_title(Editor * editor);
|
||||||
|
static GtkWidget * _new_menubar(Editor * editor);
|
||||||
Editor * editor_new(void)
|
Editor * editor_new(void)
|
||||||
{
|
{
|
||||||
Editor * editor;
|
Editor * editor;
|
||||||
@ -93,7 +104,7 @@ Editor * editor_new(void)
|
|||||||
/* widgets */
|
/* widgets */
|
||||||
editor->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
editor->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||||
gtk_window_set_default_size(GTK_WINDOW(editor->window), 512, 384);
|
gtk_window_set_default_size(GTK_WINDOW(editor->window), 512, 384);
|
||||||
gtk_window_set_title(GTK_WINDOW(editor->window), "Text editor");
|
_new_set_title(editor);
|
||||||
g_signal_connect(G_OBJECT(editor->window), "delete_event", G_CALLBACK(
|
g_signal_connect(G_OBJECT(editor->window), "delete_event", G_CALLBACK(
|
||||||
_on_closex), editor);
|
_on_closex), editor);
|
||||||
vbox = gtk_vbox_new(FALSE, 0);
|
vbox = gtk_vbox_new(FALSE, 0);
|
||||||
@ -133,6 +144,15 @@ Editor * editor_new(void)
|
|||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _new_set_title(Editor * editor)
|
||||||
|
{
|
||||||
|
char buf[256];
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "%s%s", "Text editor - ", editor->filename
|
||||||
|
== NULL ? "(Untitled)" : editor->filename);
|
||||||
|
gtk_window_set_title(GTK_WINDOW(editor->window), buf);
|
||||||
|
}
|
||||||
|
|
||||||
static GtkWidget * _new_menubar(Editor * editor)
|
static GtkWidget * _new_menubar(Editor * editor)
|
||||||
{
|
{
|
||||||
GtkWidget * tb_menubar;
|
GtkWidget * tb_menubar;
|
||||||
@ -585,6 +605,7 @@ void editor_open(Editor * editor, char const * filename)
|
|||||||
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(
|
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(gtk_text_view_get_buffer(
|
||||||
GTK_TEXT_VIEW(editor->view))), FALSE);
|
GTK_TEXT_VIEW(editor->view))), FALSE);
|
||||||
editor->filename = g_strdup(filename);
|
editor->filename = g_strdup(filename);
|
||||||
|
_new_set_title(editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -609,7 +630,7 @@ void editor_open_dialog(Editor * editor)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void editor_save(Editor * editor)
|
gboolean editor_save(Editor * editor)
|
||||||
{
|
{
|
||||||
FILE * fp;
|
FILE * fp;
|
||||||
GtkTextBuffer * tbuf;
|
GtkTextBuffer * tbuf;
|
||||||
@ -621,29 +642,35 @@ void editor_save(Editor * editor)
|
|||||||
if(editor->filename == NULL)
|
if(editor->filename == NULL)
|
||||||
{
|
{
|
||||||
editor_save_as_dialog(editor);
|
editor_save_as_dialog(editor);
|
||||||
return;
|
return FALSE;
|
||||||
}
|
}
|
||||||
if((fp = fopen(editor->filename, "w")) == NULL)
|
if((fp = fopen(editor->filename, "w")) == NULL)
|
||||||
{
|
{
|
||||||
_editor_error(editor, "Could not save file", 0);
|
_editor_error(editor, "Could not save file", 0);
|
||||||
return;
|
return FALSE;
|
||||||
}
|
}
|
||||||
tbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(editor->view));
|
tbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(editor->view));
|
||||||
|
/* FIXME allocating the complete file is not optimal */
|
||||||
gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tbuf), &start);
|
gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tbuf), &start);
|
||||||
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tbuf), &end);
|
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tbuf), &end);
|
||||||
buf = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(tbuf), &start, &end,
|
buf = gtk_text_buffer_get_text(GTK_TEXT_BUFFER(tbuf), &start, &end,
|
||||||
FALSE);
|
FALSE);
|
||||||
len = strlen(buf);
|
len = strlen(buf);
|
||||||
if(fwrite(buf, sizeof(char), len, fp) != len)
|
if(fwrite(buf, sizeof(char), len, fp) != len)
|
||||||
|
{
|
||||||
|
g_free(buf);
|
||||||
|
fclose(fp);
|
||||||
_editor_error(editor, "Partial write", 0);
|
_editor_error(editor, "Partial write", 0);
|
||||||
else
|
return FALSE;
|
||||||
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tbuf), FALSE);
|
}
|
||||||
fclose(fp);
|
|
||||||
g_free(buf);
|
g_free(buf);
|
||||||
|
fclose(fp);
|
||||||
|
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tbuf), FALSE);
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void editor_save_as(Editor * editor, char const * filename)
|
gboolean editor_save_as(Editor * editor, char const * filename)
|
||||||
{
|
{
|
||||||
GtkWidget * dialog;
|
GtkWidget * dialog;
|
||||||
int ret;
|
int ret;
|
||||||
@ -658,18 +685,23 @@ void editor_save_as(Editor * editor, char const * filename)
|
|||||||
ret = gtk_dialog_run(GTK_DIALOG(dialog));
|
ret = gtk_dialog_run(GTK_DIALOG(dialog));
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
if(ret == GTK_RESPONSE_NO)
|
if(ret == GTK_RESPONSE_NO)
|
||||||
return;
|
return FALSE;
|
||||||
}
|
}
|
||||||
g_free(editor->filename);
|
g_free(editor->filename);
|
||||||
editor->filename = g_strdup(filename);
|
if((editor->filename = g_strdup(filename)) == NULL)
|
||||||
editor_save(editor);
|
return _editor_error(editor, "Allocation error", FALSE);
|
||||||
|
if(editor_save(editor) != TRUE)
|
||||||
|
return FALSE;
|
||||||
|
_new_set_title(editor);
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void editor_save_as_dialog(Editor * editor)
|
gboolean editor_save_as_dialog(Editor * editor)
|
||||||
{
|
{
|
||||||
GtkWidget * dialog;
|
GtkWidget * dialog;
|
||||||
char * filename = NULL;
|
char * filename = NULL;
|
||||||
|
gboolean ret;
|
||||||
|
|
||||||
dialog = gtk_file_chooser_dialog_new("Save as...",
|
dialog = gtk_file_chooser_dialog_new("Save as...",
|
||||||
GTK_WINDOW(editor->window),
|
GTK_WINDOW(editor->window),
|
||||||
@ -681,7 +713,8 @@ void editor_save_as_dialog(Editor * editor)
|
|||||||
dialog));
|
dialog));
|
||||||
gtk_widget_destroy(dialog);
|
gtk_widget_destroy(dialog);
|
||||||
if(filename == NULL)
|
if(filename == NULL)
|
||||||
return;
|
return FALSE;
|
||||||
editor_save_as(editor, filename);
|
ret = editor_save_as(editor, filename);
|
||||||
g_free(filename);
|
g_free(filename);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,8 @@ void editor_delete(Editor * editor);
|
|||||||
gboolean editor_close(Editor * editor);
|
gboolean editor_close(Editor * editor);
|
||||||
void editor_open(Editor * editor, char const * filename);
|
void editor_open(Editor * editor, char const * filename);
|
||||||
void editor_open_dialog(Editor * editor);
|
void editor_open_dialog(Editor * editor);
|
||||||
void editor_save(Editor * editor);
|
gboolean editor_save(Editor * editor);
|
||||||
void editor_save_as(Editor * editor, char const * filename);
|
gboolean editor_save_as(Editor * editor, char const * filename);
|
||||||
void editor_save_as_dialog(Editor * editor);
|
gboolean editor_save_as_dialog(Editor * editor);
|
||||||
|
|
||||||
#endif /* !EDITOR_EDITOR_H */
|
#endif /* !EDITOR_EDITOR_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user