Added gm_editor_saved, added gm_editor_close, added gm_editor_set_lines_from_string, added gm_editor_set_lines_from_file, fixed indentation, added close signal, added saved signal
This commit is contained in:
parent
93820b9776
commit
e1463f369c
|
@ -3,25 +3,28 @@
|
|||
#include "gm-editor.h"
|
||||
#include "gm-world.h"
|
||||
#include "gm-support.h"
|
||||
#include "gm-string.h"
|
||||
|
||||
#define GM_EDITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GM_TYPE_EDITOR, GmEditorPrivate))
|
||||
|
||||
struct _GmEditorPrivate {
|
||||
gchar *name;
|
||||
gchar *upload_cmd;
|
||||
gchar *mcp_type;
|
||||
|
||||
GmEditType type;
|
||||
gboolean is_code;
|
||||
gchar *name;
|
||||
gchar *upload_cmd;
|
||||
gchar *mcp_type;
|
||||
|
||||
GList *lines;
|
||||
GmEditType type;
|
||||
gboolean is_code;
|
||||
|
||||
GList *lines;
|
||||
};
|
||||
|
||||
/* Signals */
|
||||
|
||||
enum {
|
||||
SAVE,
|
||||
NUM_SIGNALS
|
||||
SAVED,
|
||||
CLOSE,
|
||||
NUM_SIGNALS
|
||||
};
|
||||
|
||||
static guint editor_signals[NUM_SIGNALS] = {0};
|
||||
|
@ -35,7 +38,7 @@ gm_editor_finalize(GObject *object) {
|
|||
g_free(editor->priv->name);
|
||||
g_free(editor->priv->upload_cmd);
|
||||
g_free(editor->priv->mcp_type);
|
||||
g_list_free_simple(editor->priv->lines);
|
||||
gm_g_list_free_simple(editor->priv->lines);
|
||||
|
||||
G_OBJECT_CLASS(gm_editor_parent_class)->finalize(object);
|
||||
}
|
||||
|
@ -55,6 +58,24 @@ gm_editor_class_init(GmEditorClass *klass) {
|
|||
g_cclosure_marshal_VOID__VOID,
|
||||
G_TYPE_NONE,
|
||||
0);
|
||||
editor_signals[SAVED] =
|
||||
g_signal_new("saved",
|
||||
G_OBJECT_CLASS_TYPE(object_class),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
G_STRUCT_OFFSET(GmEditorClass, saved),
|
||||
NULL, NULL,
|
||||
g_cclosure_marshal_VOID__VOID,
|
||||
G_TYPE_NONE,
|
||||
0);
|
||||
editor_signals[CLOSE] =
|
||||
g_signal_new("close",
|
||||
G_OBJECT_CLASS_TYPE(object_class),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
G_STRUCT_OFFSET(GmEditorClass, close),
|
||||
NULL, NULL,
|
||||
g_cclosure_marshal_VOID__VOID,
|
||||
G_TYPE_NONE,
|
||||
0);
|
||||
|
||||
g_type_class_add_private(object_class, sizeof(GmEditorPrivate));
|
||||
}
|
||||
|
@ -92,6 +113,59 @@ gm_editor_new_mcp(gchar *name, gchar *reference, gchar *type,
|
|||
return editor;
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_lines_free(GmEditor *editor) {
|
||||
gm_g_list_free_simple(editor->priv->lines);
|
||||
editor->priv->lines = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_add_line(GmEditor *editor, gchar *line, GList **lastptr) {
|
||||
if (!editor->priv->lines) {
|
||||
editor->priv->lines = g_list_append(NULL, line);
|
||||
*lastptr = editor->priv->lines;
|
||||
} else {
|
||||
g_list_append(*lastptr, line);
|
||||
*lastptr = (*lastptr)->next;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_set_lines_from_string(GmEditor *editor, gchar const *text) {
|
||||
gchar *fptr, *line;
|
||||
GList *lastptr;
|
||||
|
||||
gm_editor_lines_free(editor);
|
||||
|
||||
while ((fptr = g_utf8_strchr(text, '\n', -1))) {
|
||||
line = g_strndup(text, fptr - text);
|
||||
gm_string_remove_char(line, '\r');
|
||||
|
||||
gm_editor_add_line(editor, line, &lastptr);
|
||||
text = g_utf8_next_char(fptr);
|
||||
}
|
||||
|
||||
if (*text != '\0') {
|
||||
line = g_strdup(text);
|
||||
gm_string_remove_char(line, '\r');
|
||||
|
||||
gm_editor_add_line(editor, line, &lastptr);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_set_lines_from_file(GmEditor *editor, gchar const *filename) {
|
||||
gchar *text = gm_read_file(filename);
|
||||
|
||||
if (!text) {
|
||||
// TODO: Couldn't read file... what to do
|
||||
return;
|
||||
}
|
||||
|
||||
gm_editor_set_lines_from_string(editor, text);
|
||||
g_free(text);
|
||||
}
|
||||
|
||||
gboolean
|
||||
gm_editor_is_code(GmEditor *editor) {
|
||||
return editor->priv->is_code;
|
||||
|
@ -126,3 +200,14 @@ void
|
|||
gm_editor_save(GmEditor *editor) {
|
||||
g_signal_emit(editor, editor_signals[SAVE], 0);
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_saved(GmEditor *editor) {
|
||||
g_signal_emit(editor, editor_signals[SAVED], 0);
|
||||
}
|
||||
|
||||
void
|
||||
gm_editor_close(GmEditor *editor) {
|
||||
g_signal_emit(editor, editor_signals[CLOSE], 0);
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue