Added opening file + line column check

This commit is contained in:
Win 2024-04-27 17:28:56 +07:00
parent 88da55cd1e
commit 1f87f1cb3c
2 changed files with 42 additions and 5 deletions

View File

@ -30,6 +30,7 @@ struct _TextViewerWindow
AdwHeaderBar *header_bar; AdwHeaderBar *header_bar;
GtkTextView *main_text_view; GtkTextView *main_text_view;
GtkButton *open_button; GtkButton *open_button;
GtkLabel *cursor_pos;
}; };
G_DEFINE_FINAL_TYPE (TextViewerWindow, text_viewer_window, ADW_TYPE_APPLICATION_WINDOW) 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) { if (error != NULL) {
g_printerr ("Unable to open '%': %s\n", g_printerr ("Unable to open '%s': %s\n",
g_file_peek_path (file), g_file_peek_path (file),
error->message); error->message);
return; return;
@ -97,12 +98,13 @@ open_file (TextViewerWindow *self,
self); self);
} }
static void on_open_response (GObject *source, static void
on_open_response (GObject *source,
GAsyncResult *result, GAsyncResult *result,
gpointer user_data) gpointer user_data)
{ {
GtkFileDialog *dialog = GTK_FILE_DIALOG (source); 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); 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, header_bar);
gtk_widget_class_bind_template_child (widget_class, TextViewerWindow, main_text_view); 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, 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 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_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)); 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);
} }

View File

@ -27,6 +27,15 @@
<property name="menu-model">primary_menu</property> <property name="menu-model">primary_menu</property>
</object> </object>
</child> </child>
<child type="end">
<object class="GtkLabel" id="cursor_pos">
<property name="label">Ln 0, Col 0</property>
<style>
<class name="dim-label" />
<class name="numeric" />
</style>
</object>
</child>
</object> </object>
</child> </child>
<property name="content"> <property name="content">