From 1f87f1cb3cbb8f31b4c91c4412a088f178550b9e Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Sat, 27 Apr 2024 17:28:56 +0700 Subject: [PATCH] Added opening file + line column check --- src/text-viewer-window.c | 38 +++++++++++++++++++++++++++++++++----- src/text-viewer-window.ui | 9 +++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/text-viewer-window.c b/src/text-viewer-window.c index e76e331..5228f9f 100644 --- a/src/text-viewer-window.c +++ b/src/text-viewer-window.c @@ -30,6 +30,7 @@ struct _TextViewerWindow AdwHeaderBar *header_bar; GtkTextView *main_text_view; GtkButton *open_button; + GtkLabel *cursor_pos; }; G_DEFINE_FINAL_TYPE (TextViewerWindow, text_viewer_window, ADW_TYPE_APPLICATION_WINDOW) @@ -65,7 +66,7 @@ open_file_complete (GObject *source_object, } if (error != NULL) { - g_printerr ("Unable to open '%': %s\n", + g_printerr ("Unable to open '%s': %s\n", g_file_peek_path (file), error->message); return; @@ -97,12 +98,13 @@ open_file (TextViewerWindow *self, self); } -static void on_open_response (GObject *source, - GAsyncResult *result, - gpointer user_data) +static void +on_open_response (GObject *source, + GAsyncResult *result, + gpointer user_data) { GtkFileDialog *dialog = GTK_FILE_DIALOG (source); - TextViewer *self = user_data; + TextViewerWindow *self = user_data; g_autoptr (GFile) file = gtk_file_dialog_open_finish (dialog, result, NULL); @@ -133,6 +135,27 @@ text_viewer_window_class_init (TextViewerWindowClass *klass) gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, header_bar); gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, main_text_view); gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, open_button); + gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, cursor_pos); +} + +static void +text_viewer_window__update_cursor_position (GtkTextBuffer *buffer, + GParamSpec *pspec, + TextViewerWindow *self) +{ + int cursor_pos = 0; + + g_object_get (buffer, "cursor-position", &cursor_pos, NULL); + + GtkTextIter iter; + gtk_text_buffer_get_iter_at_offset (buffer, &iter, cursor_pos); + + g_autofree char *cursor_str = + g_strdup_printf ("Ln %d, Col %d", + gtk_text_iter_get_line (&iter) + 1, + gtk_text_iter_get_line_offset (&iter) + 1); + + gtk_label_set_text (self->cursor_pos, cursor_str); } static void @@ -144,4 +167,9 @@ text_viewer_window_init (TextViewerWindow *self) 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)); + GtkTextBuffer *buffer = gtk_text_view_get_buffer (self->main_text_view); + g_signal_connect (buffer, + "notify::cursor-position", + G_CALLBACK (text_viewer_window__update_cursor_position), + self); } diff --git a/src/text-viewer-window.ui b/src/text-viewer-window.ui index a9ead3e..9874bc9 100644 --- a/src/text-viewer-window.ui +++ b/src/text-viewer-window.ui @@ -27,6 +27,15 @@ primary_menu + + + Ln 0, Col 0 + + +