From 88da55cd1e141eaf684e0a48caeb287d545b9126 Mon Sep 17 00:00:00 2001 From: Win Date: Sat, 27 Apr 2024 15:54:14 +0700 Subject: [PATCH] Modified some code --- src/text-viewer-application.c | 3 + src/text-viewer-window.c | 101 +++++++++++++++++++++++++++++++++- src/text-viewer-window.ui | 6 ++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/text-viewer-application.c b/src/text-viewer-application.c index 64cde91..4527035 100644 --- a/src/text-viewer-application.c +++ b/src/text-viewer-application.c @@ -117,4 +117,7 @@ text_viewer_application_init (TextViewerApplication *self) gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.quit", (const char *[]) { "q", NULL }); + gtk_application_set_accels_for_action (GTK_APPLICATION (self), + "win.open", + (const char *[]) { "o", NULL }); } diff --git a/src/text-viewer-window.c b/src/text-viewer-window.c index d2ed76c..e76e331 100644 --- a/src/text-viewer-window.c +++ b/src/text-viewer-window.c @@ -28,11 +28,102 @@ struct _TextViewerWindow /* Template widgets */ AdwHeaderBar *header_bar; - GtkLabel *label; + GtkTextView *main_text_view; + GtkButton *open_button; }; G_DEFINE_FINAL_TYPE (TextViewerWindow, text_viewer_window, ADW_TYPE_APPLICATION_WINDOW) +static void +open_file_complete (GObject *source_object, + GAsyncResult *result, + TextViewerWindow *self) +{ + GFile *file = G_FILE (source_object); + + g_autofree char *contents = NULL; + gsize length = 0; + + g_autoptr (GError) error = NULL; + + g_file_load_contents_finish (file, + result, + &contents, + &length, + NULL, + &error); + g_autofree char *display_name = NULL; + g_autoptr (GFileInfo) info = g_file_query_info(file, + "standard::display-name", + G_FILE_QUERY_INFO_NONE, + NULL, + NULL); + if (info != NULL) { + display_name = g_strdup (g_file_info_get_attribute_string (info, "standard::display-name")); + } else { + display_name = g_file_get_basename (file); + } + + if (error != NULL) { + g_printerr ("Unable to open '%': %s\n", + g_file_peek_path (file), + error->message); + return; + } + + if (!g_utf8_validate (contents, length, NULL)) { + g_printerr ("Unable to load the contents of '%s': " + "the file is not encoded with UTF-8\n", + g_file_peek_path (file)); + return; + } + + GtkTextBuffer *buffer = gtk_text_view_get_buffer (self->main_text_view); + + gtk_text_buffer_set_text (buffer, contents, length); + + GtkTextIter start; + gtk_text_buffer_get_start_iter (buffer, &start); + gtk_text_buffer_place_cursor (buffer, &start); +} + +static void +open_file (TextViewerWindow *self, + GFile *file) +{ + g_file_load_contents_async (file, + NULL, + (GAsyncReadyCallback) open_file_complete, + self); +} + +static void on_open_response (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GtkFileDialog *dialog = GTK_FILE_DIALOG (source); + TextViewer *self = user_data; + + g_autoptr (GFile) file = gtk_file_dialog_open_finish (dialog, result, NULL); + + if (file != NULL) + open_file (self, file); +} + +static void +text_viewer_window__open_file_dialog (GAction *action G_GNUC_UNUSED, + GVariant *parameter G_GNUC_UNUSED, + TextViewerWindow *self) +{ + g_autoptr (GtkFileDialog) dialog = gtk_file_dialog_new (); + + gtk_file_dialog_open (dialog, + GTK_WINDOW (self), + NULL, + on_open_response, + self); +} + static void text_viewer_window_class_init (TextViewerWindowClass *klass) { @@ -40,11 +131,17 @@ text_viewer_window_class_init (TextViewerWindowClass *klass) gtk_widget_class_set_template_from_resource (widget_class, "/com/example/TextViewer/text-viewer-window.ui"); gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, header_bar); - gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, label); + gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, main_text_view); + gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, open_button); } static void text_viewer_window_init (TextViewerWindow *self) { gtk_widget_init_template (GTK_WIDGET (self)); + + g_autoptr (GSimpleAction) open_action = g_simple_action_new ("open", NULL); + g_signal_connect (open_action, "activate", G_CALLBACK (text_viewer_window__open_file_dialog), self); + g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (open_action)); + } diff --git a/src/text-viewer-window.ui b/src/text-viewer-window.ui index d73b567..a9ead3e 100644 --- a/src/text-viewer-window.ui +++ b/src/text-viewer-window.ui @@ -13,6 +13,12 @@ + + + Open + win.open + + True