]> Pileus Git - ~andy/gtk/blob - examples/bloatpad.c
Merge bgo593793-filechooser-recent-folders-master branch.
[~andy/gtk] / examples / bloatpad.c
1 #include <gtk/gtk.h>
2
3 static void
4 new_window (GApplication *app,
5             GFile        *file)
6 {
7   GtkWidget *window, *scrolled, *view;
8
9   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
10   gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (app));
11   gtk_window_set_title (GTK_WINDOW (window), "Bloatpad");
12   scrolled = gtk_scrolled_window_new (NULL, NULL);
13   view = gtk_text_view_new ();
14   gtk_container_add (GTK_CONTAINER (scrolled), view);
15   gtk_container_add (GTK_CONTAINER (window), scrolled);
16
17   if (file != NULL)
18     {
19       gchar *contents;
20       gsize length;
21
22       if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
23         {
24           GtkTextBuffer *buffer;
25
26           buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
27           gtk_text_buffer_set_text (buffer, contents, length);
28           g_free (contents);
29         }
30     }
31
32   gtk_widget_show_all (GTK_WIDGET (window));
33 }
34
35 static void
36 bloat_pad_activate (GApplication *application)
37 {
38   new_window (application, NULL);
39 }
40
41 static void
42 bloat_pad_open (GApplication  *application,
43                 GFile        **files,
44                 gint           n_files,
45                 const gchar   *hint)
46 {
47   gint i;
48
49   for (i = 0; i < n_files; i++)
50     new_window (application, files[i]);
51 }
52
53 typedef GtkApplication BloatPad;
54 typedef GtkApplicationClass BloatPadClass;
55
56 G_DEFINE_TYPE (BloatPad, bloat_pad, GTK_TYPE_APPLICATION)
57
58 static void
59 bloat_pad_finalize (GObject *object)
60 {
61   G_OBJECT_CLASS (bloat_pad_parent_class)->finalize (object);
62 }
63
64 static void
65 bloat_pad_init (BloatPad *app)
66 {
67 }
68
69 static void
70 bloat_pad_class_init (BloatPadClass *class)
71 {
72   G_OBJECT_CLASS (class)->finalize= bloat_pad_finalize;
73
74   G_APPLICATION_CLASS (class)->activate = bloat_pad_activate;
75   G_APPLICATION_CLASS (class)->open = bloat_pad_open;
76 }
77
78 BloatPad *
79 bloat_pad_new (void)
80 {
81   g_type_init ();
82
83   return g_object_new (bloat_pad_get_type (),
84                        "application-id", "org.gtk.Test.bloatpad",
85                        "flags", G_APPLICATION_HANDLES_OPEN,
86                        NULL);
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92   BloatPad *bloat_pad;
93   int status;
94
95   bloat_pad = bloat_pad_new ();
96   status = g_application_run (G_APPLICATION (bloat_pad), argc, argv);
97   g_object_unref (bloat_pad);
98
99   return status;
100 }