#include /* Define TfeTextView Widget which is the child object of GtkTextView */ #define TFE_TYPE_TEXT_VIEW tfe_text_view_get_type () G_DECLARE_FINAL_TYPE (TfeTextView, tfe_text_view, TFE, TEXT_VIEW, GtkTextView) struct _TfeTextView { GtkTextView parent; GFile *file; }; G_DEFINE_TYPE (TfeTextView, tfe_text_view, GTK_TYPE_TEXT_VIEW); static void tfe_text_view_init (TfeTextView *tv) { } static void tfe_text_view_class_init (TfeTextViewClass *class) { } void tfe_text_view_set_file (TfeTextView *tv, GFile *f) { tv -> file = f; } GFile * tfe_text_view_get_file (TfeTextView *tv) { return tv -> file; } GtkWidget * tfe_text_view_new (void) { return GTK_WIDGET (g_object_new (TFE_TYPE_TEXT_VIEW, NULL)); } /* ---------- end of the definition of TfeTextView ---------- */ static gboolean before_close (GtkWindow *win, GtkWidget *nb) { GtkWidget *scr; GtkWidget *tv; GFile *file; GtkTextBuffer *tb; GtkTextIter start_iter; GtkTextIter end_iter; char *contents; unsigned int n; unsigned int i; n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)); for (i = 0; i < n; ++i) { scr = gtk_notebook_get_nth_page (GTK_NOTEBOOK (nb), i); tv = gtk_scrolled_window_get_child (GTK_SCROLLED_WINDOW (scr)); file = tfe_text_view_get_file (TFE_TEXT_VIEW (tv)); tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); gtk_text_buffer_get_bounds (tb, &start_iter, &end_iter); contents = gtk_text_buffer_get_text (tb, &start_iter, &end_iter, FALSE); if (! g_file_replace_contents (file, contents, strlen (contents), NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, NULL)) g_print ("ERROR : Can't save %s.", g_file_get_path (file)); } return FALSE; } static void on_activate (GApplication *app, gpointer user_data) { g_print ("You need a filename argument.\n"); } static void on_open (GApplication *app, GFile ** files, gint n_files, gchar *hint, gpointer user_data) { GtkWidget *win; GtkWidget *nb; GtkWidget *lab; GtkNotebookPage *nbp; GtkWidget *scr; GtkWidget *tv; GtkTextBuffer *tb; char *contents; gsize length; char *filename; int i; win = gtk_application_window_new (GTK_APPLICATION (app)); gtk_window_set_title (GTK_WINDOW (win), "file editor"); gtk_window_set_default_size (GTK_WINDOW (win), 400, 300); gtk_window_maximize (GTK_WINDOW (win)); nb = gtk_notebook_new (); gtk_window_set_child (GTK_WINDOW (win), nb); for (i = 0; i < n_files; i++) { if (g_file_load_contents (files[i], NULL, &contents, &length, NULL, NULL)) { scr = gtk_scrolled_window_new (); tv = tfe_text_view_new (); tb = gtk_text_view_get_buffer (GTK_TEXT_VIEW (tv)); gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (tv), GTK_WRAP_WORD_CHAR); gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), tv); tfe_text_view_set_file (TFE_TEXT_VIEW (tv), g_file_dup (files[i])); gtk_text_buffer_set_text (tb, contents, length); g_free (contents); filename = g_file_get_basename (files[i]); lab = gtk_label_new (filename); gtk_notebook_append_page (GTK_NOTEBOOK (nb), scr, lab); nbp = gtk_notebook_get_page (GTK_NOTEBOOK (nb), scr); g_object_set (nbp, "tab-expand", TRUE, NULL); g_free (filename); } else { filename = g_file_get_path (files[i]); g_print ("No such file: %s.\n", filename); g_free (filename); } } if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (nb)) > 0) { g_signal_connect (win, "close-request", G_CALLBACK (before_close), nb); gtk_widget_show (win); } else gtk_window_destroy (GTK_WINDOW (win)); } int main (int argc, char **argv) { GtkApplication *app; int stat; app = gtk_application_new ("com.github.ToshioCP.tfe1", G_APPLICATION_HANDLES_OPEN); g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL); g_signal_connect (app, "open", G_CALLBACK (on_open), NULL); stat =g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return stat; }