exampleappwin.c 3.03 KB
Newer Older
1 2
#include <gtk/gtk.h>

Matthias Clasen's avatar
Matthias Clasen committed
3 4 5
#include "exampleapp.h"
#include "exampleappwin.h"

6 7 8
struct _ExampleAppWindow
{
  GtkApplicationWindow parent;
Matthias Clasen's avatar
Matthias Clasen committed
9 10
};

11
typedef struct _ExampleAppWindowPrivate ExampleAppWindowPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
12

13 14 15 16
struct _ExampleAppWindowPrivate
{
  GSettings *settings;
  GtkWidget *stack;
Matthias Clasen's avatar
Matthias Clasen committed
17 18 19 20 21 22 23
};

G_DEFINE_TYPE_WITH_PRIVATE(ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);

static void
example_app_window_init (ExampleAppWindow *win)
{
24
  ExampleAppWindowPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
25

26 27 28
  priv = example_app_window_get_instance_private (win);
  gtk_widget_init_template (GTK_WIDGET (win));
  priv->settings = g_settings_new ("org.gtk.exampleapp");
Matthias Clasen's avatar
Matthias Clasen committed
29

30 31 32
  g_settings_bind (priv->settings, "transition",
                   priv->stack, "transition-type",
                   G_SETTINGS_BIND_DEFAULT);
Matthias Clasen's avatar
Matthias Clasen committed
33 34 35 36 37
}

static void
example_app_window_dispose (GObject *object)
{
38 39
  ExampleAppWindow *win;
  ExampleAppWindowPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
40

41 42
  win = EXAMPLE_APP_WINDOW (object);
  priv = example_app_window_get_instance_private (win);
Matthias Clasen's avatar
Matthias Clasen committed
43

44
  g_clear_object (&priv->settings);
Matthias Clasen's avatar
Matthias Clasen committed
45

46
  G_OBJECT_CLASS (example_app_window_parent_class)->dispose (object);
Matthias Clasen's avatar
Matthias Clasen committed
47 48 49 50 51
}

static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
52
  G_OBJECT_CLASS (class)->dispose = example_app_window_dispose;
Matthias Clasen's avatar
Matthias Clasen committed
53

54 55
  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
                                               "/org/gtk/exampleapp/window.ui");
56
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), ExampleAppWindow, stack);
Matthias Clasen's avatar
Matthias Clasen committed
57 58 59 60 61
}

ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
62
  return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
63 64 65 66 67 68
}

void
example_app_window_open (ExampleAppWindow *win,
                         GFile            *file)
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  ExampleAppWindowPrivate *priv;
  gchar *basename;
  GtkWidget *scrolled, *view;
  gchar *contents;
  gsize length;
  GtkTextBuffer *buffer;
  GtkTextTag *tag;
  GtkTextIter start_iter, end_iter;

  priv = example_app_window_get_instance_private (win);
  basename = g_file_get_basename (file);

  scrolled = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scrolled);
  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_widget_show (view);
  gtk_container_add (GTK_CONTAINER (scrolled), view);
  gtk_stack_add_titled (GTK_STACK (priv->stack), scrolled, basename, basename);

  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 (priv->settings, "font", tag, "font", G_SETTINGS_BIND_DEFAULT);

  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);
Matthias Clasen's avatar
Matthias Clasen committed
108
}