Modified some code
This commit is contained in:
parent
b33fde7850
commit
88da55cd1e
|
@ -117,4 +117,7 @@ text_viewer_application_init (TextViewerApplication *self)
|
|||
gtk_application_set_accels_for_action (GTK_APPLICATION (self),
|
||||
"app.quit",
|
||||
(const char *[]) { "<primary>q", NULL });
|
||||
gtk_application_set_accels_for_action (GTK_APPLICATION (self),
|
||||
"win.open",
|
||||
(const char *[]) { "<Ctrl>o", NULL });
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
||||
}
|
||||
|
|
|
@ -13,6 +13,12 @@
|
|||
<object class="AdwToolbarView">
|
||||
<child type="top">
|
||||
<object class="AdwHeaderBar" id="header_bar">
|
||||
<child type="start">
|
||||
<object class="GtkButton" id="open_button">
|
||||
<property name="label">Open</property>
|
||||
<property name="action-name">win.open</property>
|
||||
</object>
|
||||
</child>
|
||||
<child type="end">
|
||||
<object class="GtkMenuButton">
|
||||
<property name="primary">True</property>
|
||||
|
|
Loading…
Reference in New Issue