exampleappwin.c 7.56 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 9 10 11 12 13 14 15 16 17
struct _ExampleAppWindow
{
  GtkApplicationWindow parent;

  GSettings *settings;
  GtkWidget *stack;
  GtkWidget *search;
  GtkWidget *searchbar;
  GtkWidget *searchentry;
  GtkWidget *gears;
  GtkWidget *sidebar;
  GtkWidget *words;
Matthias Clasen's avatar
Matthias Clasen committed
18 19
};

20
G_DEFINE_TYPE (ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW)
Matthias Clasen's avatar
Matthias Clasen committed
21 22

static void
23 24
search_text_changed (GtkEntry         *entry,
                     ExampleAppWindow *win)
Matthias Clasen's avatar
Matthias Clasen committed
25
{
26 27 28 29 30 31 32 33 34 35 36
  const gchar *text;
  GtkWidget *tab;
  GtkWidget *view;
  GtkTextBuffer *buffer;
  GtkTextIter start, match_start, match_end;

  text = gtk_entry_get_text (entry);

  if (text[0] == '\0')
    return;

37
  tab = gtk_stack_get_visible_child (GTK_STACK (win->stack));
38 39 40 41 42 43 44 45 46 47 48 49
  view = gtk_bin_get_child (GTK_BIN (tab));
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));

  /* Very simple-minded search implementation */
  gtk_text_buffer_get_start_iter (buffer, &start);
  if (gtk_text_iter_forward_search (&start, text, GTK_TEXT_SEARCH_CASE_INSENSITIVE,
                                    &match_start, &match_end, NULL))
    {
      gtk_text_buffer_select_range (buffer, &match_start, &match_end);
      gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (view), &match_start,
                                    0.0, FALSE, 0.0, 0.0);
    }
Matthias Clasen's avatar
Matthias Clasen committed
50 51 52 53 54 55
}

static void
find_word (GtkButton        *button,
           ExampleAppWindow *win)
{
56
  const gchar *word;
Matthias Clasen's avatar
Matthias Clasen committed
57

58
  word = gtk_button_get_label (button);
59
  gtk_entry_set_text (GTK_ENTRY (win->searchentry), word);
Matthias Clasen's avatar
Matthias Clasen committed
60 61 62 63 64
}

static void
update_words (ExampleAppWindow *win)
{
65 66 67 68 69 70 71 72
  GHashTable *strings;
  GHashTableIter iter;
  GtkWidget *tab, *view, *row;
  GtkTextBuffer *buffer;
  GtkTextIter start, end;
  GList *children, *l;
  gchar *word, *key;

73
  tab = gtk_stack_get_visible_child (GTK_STACK (win->stack));
74 75 76 77

  if (tab == NULL)
    return;

78 79 80 81 82 83 84 85 86 87 88 89
  view = gtk_bin_get_child (GTK_BIN (tab));
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));

  strings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

  gtk_text_buffer_get_start_iter (buffer, &start);
  while (!gtk_text_iter_is_end (&start))
    {
      while (!gtk_text_iter_starts_word (&start))
        {
          if (!gtk_text_iter_forward_char (&start))
            goto done;
Matthias Clasen's avatar
Matthias Clasen committed
90
        }
91 92 93 94 95 96 97 98
      end = start;
      if (!gtk_text_iter_forward_word_end (&end))
        goto done;
      word = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
      g_hash_table_add (strings, g_utf8_strdown (word, -1));
      g_free (word);
      start = end;
    }
Matthias Clasen's avatar
Matthias Clasen committed
99 100

done:
101
  children = gtk_container_get_children (GTK_CONTAINER (win->words));
102
  for (l = children; l; l = l->next)
103
    gtk_container_remove (GTK_CONTAINER (win->words), GTK_WIDGET (l->data));
104 105 106 107 108 109 110 111
  g_list_free (children);

  g_hash_table_iter_init (&iter, strings);
  while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
    {
      row = gtk_button_new_with_label (key);
      g_signal_connect (row, "clicked",
                        G_CALLBACK (find_word), win);
112
      gtk_container_add (GTK_CONTAINER (win->words), row);
113 114 115
    }

  g_hash_table_unref (strings);
Matthias Clasen's avatar
Matthias Clasen committed
116 117 118
}

static void
119 120 121
visible_child_changed (GObject          *stack,
                       GParamSpec       *pspec,
                       ExampleAppWindow *win)
Matthias Clasen's avatar
Matthias Clasen committed
122
{
123
  if (gtk_widget_in_destruction (GTK_WIDGET (stack)))
124 125
    return;

126
  gtk_search_bar_set_search_mode (GTK_SEARCH_BAR (win->searchbar), FALSE);
127
  update_words (win);
Matthias Clasen's avatar
Matthias Clasen committed
128 129 130 131 132 133 134
}

static void
words_changed (GObject          *sidebar,
               GParamSpec       *pspec,
               ExampleAppWindow *win)
{
135
  update_words (win);
Matthias Clasen's avatar
Matthias Clasen committed
136 137 138 139 140
}

static void
example_app_window_init (ExampleAppWindow *win)
{
141 142 143 144 145
  GtkBuilder *builder;
  GMenuModel *menu;
  GAction *action;

  gtk_widget_init_template (GTK_WIDGET (win));
146
  win->settings = g_settings_new ("org.gtk.exampleapp");
147

148 149
  g_settings_bind (win->settings, "transition",
                   win->stack, "transition-type",
150 151
                   G_SETTINGS_BIND_DEFAULT);

152 153
  g_settings_bind (win->settings, "show-words",
                   win->sidebar, "reveal-child",
154 155
                   G_SETTINGS_BIND_DEFAULT);

156 157
  g_object_bind_property (win->search, "active",
                          win->searchbar, "search-mode-enabled",
158 159
                          G_BINDING_BIDIRECTIONAL);

160
  g_signal_connect (win->sidebar, "notify::reveal-child",
161
                    G_CALLBACK (words_changed), win);
162 163 164

  builder = gtk_builder_new_from_resource ("/org/gtk/exampleapp/gears-menu.ui");
  menu = G_MENU_MODEL (gtk_builder_get_object (builder, "menu"));
165
  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (win->gears), menu);
166 167
  g_object_unref (builder);

168
  action = g_settings_create_action (win->settings, "show-words");
169 170
  g_action_map_add_action (G_ACTION_MAP (win), action);
  g_object_unref (action);
Matthias Clasen's avatar
Matthias Clasen committed
171 172 173 174 175
}

static void
example_app_window_dispose (GObject *object)
{
176
  ExampleAppWindow *win;
Matthias Clasen's avatar
Matthias Clasen committed
177

178
  win = EXAMPLE_APP_WINDOW (object);
Matthias Clasen's avatar
Matthias Clasen committed
179

180
  g_clear_object (&win->settings);
Matthias Clasen's avatar
Matthias Clasen committed
181

182
  G_OBJECT_CLASS (example_app_window_parent_class)->dispose (object);
Matthias Clasen's avatar
Matthias Clasen committed
183 184 185 186 187
}

static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
188 189 190 191
  G_OBJECT_CLASS (class)->dispose = example_app_window_dispose;

  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
                                               "/org/gtk/exampleapp/window.ui");
192

193 194 195 196 197 198 199
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, stack);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, search);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, searchbar);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, searchentry);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, gears);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, words);
  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), ExampleAppWindow, sidebar);
200 201 202 203

  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), search_text_changed);
  gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), visible_child_changed);

Matthias Clasen's avatar
Matthias Clasen committed
204 205 206 207 208
}

ExampleAppWindow *
example_app_window_new (ExampleApp *app)
{
209
  return g_object_new (EXAMPLE_APP_WINDOW_TYPE, "application", app, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
210 211 212 213 214 215
}

void
example_app_window_open (ExampleAppWindow *win,
                         GFile            *file)
{
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  gchar *basename;
  GtkWidget *scrolled, *view;
  gchar *contents;
  gsize length;
  GtkTextBuffer *buffer;
  GtkTextTag *tag;
  GtkTextIter start_iter, end_iter;

  basename = g_file_get_basename (file);

  scrolled = gtk_scrolled_window_new (NULL, NULL);
  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_container_add (GTK_CONTAINER (scrolled), view);
233
  gtk_stack_add_titled (GTK_STACK (win->stack), scrolled, basename, basename);
234 235 236 237 238 239 240 241 242 243

  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);
244
  g_settings_bind (win->settings, "font",
245 246 247 248 249 250 251 252 253
                   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);

254
  gtk_widget_set_sensitive (win->search, TRUE);
255 256

  update_words (win);
Matthias Clasen's avatar
Matthias Clasen committed
257
}