gtk2/examples/application6/exampleappwin.c

106 lines
3.0 KiB
C
Raw Normal View History

#include <gtk/gtk.h>
#include "exampleapp.h"
#include "exampleappwin.h"
struct _ExampleAppWindow
2014-04-17 18:27:11 +00:00
{
GtkApplicationWindow parent;
2014-04-17 18:27:11 +00:00
GSettings *settings;
GtkWidget *stack;
GtkWidget *gears;
};
G_DEFINE_TYPE (ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW)
static void
example_app_window_init (ExampleAppWindow *win)
{
GtkBuilder *builder;
GMenuModel *menu;
2014-04-17 18:27:11 +00:00
gtk_widget_init_template (GTK_WIDGET (win));
builder = gtk_builder_new_from_resource ("/org/gtk/exampleapp/gears-menu.ui");
menu = G_MENU_MODEL (gtk_builder_get_object (builder, "menu"));
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (win->gears), menu);
g_object_unref (builder);
win->settings = g_settings_new ("org.gtk.exampleapp");
g_settings_bind (win->settings, "transition",
win->stack, "transition-type",
2014-04-17 18:27:11 +00:00
G_SETTINGS_BIND_DEFAULT);
}
static void
example_app_window_dispose (GObject *object)
{
2014-04-17 18:27:11 +00:00
ExampleAppWindow *win;
2014-04-17 18:27:11 +00:00
win = EXAMPLE_APP_WINDOW (object);
g_clear_object (&win->settings);
2014-04-17 18:27:11 +00:00
G_OBJECT_CLASS (example_app_window_parent_class)->dispose (object);
}
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
2014-04-17 18:27:11 +00:00
G_OBJECT_CLASS (class)->dispose = example_app_window_dispose;
2014-04-17 18:27:11 +00:00
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/window.ui");
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, stack);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, gears);
}
ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
2014-04-17 18:27:11 +00:00
return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
}
void
example_app_window_open (ExampleAppWindow *win,
GFile *file)
{
2020-07-24 18:40:36 +00:00
char *basename;
2014-04-17 18:27:11 +00:00
GtkWidget *scrolled, *view;
2020-07-24 18:40:36 +00:00
char *contents;
2014-04-17 18:27:11 +00:00
gsize length;
GtkTextBuffer *buffer;
GtkTextTag *tag;
GtkTextIter start_iter, end_iter;
basename = g_file_get_basename (file);
scrolled = gtk_scrolled_window_new ();
2014-04-17 18:27:11 +00:00
gtk_widget_set_hexpand (scrolled, TRUE);
gtk_widget_set_vexpand (scrolled, TRUE);
view = gtk_text_view_new ();
gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE);
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled), view);
gtk_stack_add_titled (GTK_STACK (win->stack), scrolled, basename, basename);
2014-04-17 18:27:11 +00:00
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
{
gtk_text_buffer_set_text (buffer, contents, length);
g_free (contents);
}
tag = gtk_text_buffer_create_tag (buffer, NULL, NULL);
g_settings_bind (win->settings, "font", tag, "font", G_SETTINGS_BIND_DEFAULT);
2014-04-17 18:27:11 +00:00
gtk_text_buffer_get_start_iter (buffer, &start_iter);
gtk_text_buffer_get_end_iter (buffer, &end_iter);
gtk_text_buffer_apply_tag (buffer, tag, &start_iter, &end_iter);
g_free (basename);
}