exampleappwin.c 2.05 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1
2
3
4
#include "exampleapp.h"
#include "exampleappwin.h"
#include <gtk/gtk.h>

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

10
11
12
struct _ExampleAppWindowClass
{
  GtkApplicationWindowClass parent_class;
Matthias Clasen's avatar
Matthias Clasen committed
13
14
};

15
typedef struct _ExampleAppWindowPrivate ExampleAppWindowPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
16

17
18
19
struct _ExampleAppWindowPrivate
{
  GtkWidget *stack;
Matthias Clasen's avatar
Matthias Clasen committed
20
21
22
23
24
25
26
};

G_DEFINE_TYPE_WITH_PRIVATE(ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW);

static void
example_app_window_init (ExampleAppWindow *win)
{
27
  gtk_widget_init_template (GTK_WIDGET (win));
Matthias Clasen's avatar
Matthias Clasen committed
28
29
30
31
32
}

static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
33
34
  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
                                               "/org/gtk/exampleapp/window.ui");
35
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, stack);
Matthias Clasen's avatar
Matthias Clasen committed
36
37
38
39
40
}

ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
41
  return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
42
43
44
45
46
47
}

void
example_app_window_open (ExampleAppWindow *win,
                         GFile            *file)
{
48
49
50
51
52
  ExampleAppWindowPrivate *priv;
  gchar *basename;
  GtkWidget *scrolled, *view;
  gchar *contents;
  gsize length;
Matthias Clasen's avatar
Matthias Clasen committed
53

54
55
  priv = example_app_window_get_instance_private (win);
  basename = g_file_get_basename (file);
Matthias Clasen's avatar
Matthias Clasen committed
56

57
58
59
60
61
62
63
64
65
66
  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);
Matthias Clasen's avatar
Matthias Clasen committed
67

68
69
70
  if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
    {
      GtkTextBuffer *buffer;
Matthias Clasen's avatar
Matthias Clasen committed
71

72
73
74
75
      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
      gtk_text_buffer_set_text (buffer, contents, length);
      g_free (contents);
    }
Matthias Clasen's avatar
Matthias Clasen committed
76

77
  g_free (basename);
Matthias Clasen's avatar
Matthias Clasen committed
78
}