Working on callbacks and basic implementation
This commit is contained in:
parent
da84db257a
commit
107caa8027
73
src/editor.c
73
src/editor.c
@ -10,15 +10,17 @@
|
|||||||
/* Editor */
|
/* Editor */
|
||||||
static GtkWidget * _new_menubar(Editor * editor);
|
static GtkWidget * _new_menubar(Editor * editor);
|
||||||
/* callbacks */
|
/* callbacks */
|
||||||
static gboolean _editor_on_close(GtkWidget * widget, GdkEvent * event,
|
static void _editor_on_close(GtkWidget * widget, gpointer data);
|
||||||
|
static void _editor_on_edit_preferences(GtkWidget * widget, gpointer data);
|
||||||
|
static gboolean _editor_on_exit(GtkWidget * widget, GdkEvent * event,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
static void _editor_on_file_close(GtkWidget * widget, gpointer data);
|
static void _editor_on_file_close(GtkWidget * widget, gpointer data);
|
||||||
static void _editor_on_file_new(GtkWidget * widget, gpointer data);
|
static void _editor_on_file_new(GtkWidget * widget, gpointer data);
|
||||||
static void _editor_on_file_open(GtkWidget * widget, gpointer data);
|
static void _editor_on_file_open(GtkWidget * widget, gpointer data);
|
||||||
static void _editor_on_file_save(GtkWidget * widget, gpointer data);
|
static void _editor_on_file_save(GtkWidget * widget, gpointer data);
|
||||||
static void _editor_on_file_save_as(GtkWidget * widget, gpointer data);
|
static void _editor_on_file_save_as(GtkWidget * widget, gpointer data);
|
||||||
static void _editor_on_edit_preferences(GtkWidget * widget, gpointer data);
|
|
||||||
static void _editor_on_help_about(GtkWidget * widget, gpointer data);
|
static void _editor_on_help_about(GtkWidget * widget, gpointer data);
|
||||||
|
static void _editor_on_open(GtkWidget * widget, gpointer data);
|
||||||
struct _menu
|
struct _menu
|
||||||
{
|
{
|
||||||
char * name;
|
char * name;
|
||||||
@ -80,7 +82,7 @@ Editor * editor_new(void)
|
|||||||
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");
|
gtk_window_set_title(GTK_WINDOW(editor->window), "Text editor");
|
||||||
g_signal_connect(G_OBJECT(editor->window), "delete_event", G_CALLBACK(
|
g_signal_connect(G_OBJECT(editor->window), "delete_event", G_CALLBACK(
|
||||||
_editor_on_close), editor);
|
_editor_on_exit), editor);
|
||||||
vbox = gtk_vbox_new(FALSE, 0);
|
vbox = gtk_vbox_new(FALSE, 0);
|
||||||
/* menubar */
|
/* menubar */
|
||||||
gtk_box_pack_start(GTK_BOX(vbox), _new_menubar(editor), FALSE, FALSE,
|
gtk_box_pack_start(GTK_BOX(vbox), _new_menubar(editor), FALSE, FALSE,
|
||||||
@ -90,8 +92,12 @@ Editor * editor_new(void)
|
|||||||
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
|
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
|
||||||
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
||||||
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
|
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);
|
||||||
|
g_signal_connect(G_OBJECT(tb_button), "clicked", G_CALLBACK(
|
||||||
|
_editor_on_open), editor);
|
||||||
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
||||||
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_CLOSE);
|
tb_button = gtk_tool_button_new_from_stock(GTK_STOCK_CLOSE);
|
||||||
|
g_signal_connect(G_OBJECT(tb_button), "clicked", G_CALLBACK(
|
||||||
|
_editor_on_close), editor);
|
||||||
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), tb_button, -1);
|
||||||
gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
|
gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
|
||||||
/* view */
|
/* view */
|
||||||
@ -163,12 +169,11 @@ static int _editor_error(Editor * editor, char const * message, int ret)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean _editor_on_close(GtkWidget * widget, GdkEvent * event,
|
static void _editor_on_close(GtkWidget * widget, gpointer data)
|
||||||
gpointer data)
|
|
||||||
{
|
{
|
||||||
/* FIXME ask the user whether to save or not */
|
Editor * editor = data;
|
||||||
gtk_main_quit();
|
|
||||||
return FALSE;
|
editor_close(editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _preferences_set(Editor * editor);
|
static void _preferences_set(Editor * editor);
|
||||||
@ -246,6 +251,14 @@ static void _preferences_on_ok(GtkWidget * widget, gpointer data)
|
|||||||
/* FIXME apply settings */
|
/* FIXME apply settings */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean _editor_on_exit(GtkWidget * widget, GdkEvent * event,
|
||||||
|
gpointer data)
|
||||||
|
{
|
||||||
|
Editor * editor = data;
|
||||||
|
|
||||||
|
return editor_close(editor);
|
||||||
|
}
|
||||||
|
|
||||||
static void _editor_on_file_close(GtkWidget * widget, gpointer data)
|
static void _editor_on_file_close(GtkWidget * widget, gpointer data)
|
||||||
{
|
{
|
||||||
Editor * editor = data;
|
Editor * editor = data;
|
||||||
@ -262,6 +275,8 @@ static void _editor_on_file_new(GtkWidget * widget, gpointer data)
|
|||||||
static void _editor_on_file_open(GtkWidget * widget, gpointer data)
|
static void _editor_on_file_open(GtkWidget * widget, gpointer data)
|
||||||
{
|
{
|
||||||
Editor * editor = data;
|
Editor * editor = data;
|
||||||
|
|
||||||
|
editor_open_dialog(editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _editor_on_file_save(GtkWidget * widget, gpointer data)
|
static void _editor_on_file_save(GtkWidget * widget, gpointer data)
|
||||||
@ -318,8 +333,50 @@ static void _editor_on_help_about(GtkWidget * widget, gpointer data)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _editor_on_open(GtkWidget * widget, gpointer data)
|
||||||
|
{
|
||||||
|
Editor * editor = data;
|
||||||
|
|
||||||
|
editor_open_dialog(editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void editor_delete(Editor * editor)
|
void editor_delete(Editor * editor)
|
||||||
{
|
{
|
||||||
free(editor);
|
free(editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* useful */
|
||||||
|
gboolean editor_close(Editor * editor)
|
||||||
|
{
|
||||||
|
if(editor->saved)
|
||||||
|
{
|
||||||
|
gtk_main_quit();
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
/* FIXME dialog for confirmation */
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void editor_open(Editor * editor, char const * filename)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void editor_open_dialog(Editor * editor)
|
||||||
|
{
|
||||||
|
GtkWidget * dialog;
|
||||||
|
int ret;
|
||||||
|
char * filename;
|
||||||
|
|
||||||
|
dialog = gtk_file_chooser_dialog_new("Open file...",
|
||||||
|
GTK_WINDOW(editor->window),
|
||||||
|
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, 1,
|
||||||
|
GTK_STOCK_OPEN, 0, NULL);
|
||||||
|
if((ret = gtk_dialog_run(GTK_DIALOG(dialog))) == 0)
|
||||||
|
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(
|
||||||
|
dialog));
|
||||||
|
gtk_widget_destroy(dialog);
|
||||||
|
editor_open(editor, filename);
|
||||||
|
}
|
||||||
|
@ -25,4 +25,9 @@ typedef struct _Editor
|
|||||||
Editor * editor_new(void);
|
Editor * editor_new(void);
|
||||||
void editor_delete(Editor * editor);
|
void editor_delete(Editor * editor);
|
||||||
|
|
||||||
|
/* useful */
|
||||||
|
gboolean editor_close(Editor * editor);
|
||||||
|
void editor_open(Editor * editor, char const * filename);
|
||||||
|
void editor_open_dialog(Editor * editor);
|
||||||
|
|
||||||
#endif /* !EDITOR_EDITOR_H */
|
#endif /* !EDITOR_EDITOR_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user