application.c 16.1 KB
Newer Older
1 2 3 4 5

#include "config.h"

#include <gtk/gtk.h>

6 7 8 9 10 11 12 13 14 15
typedef GtkApplication DemoApplication;
typedef GtkApplicationClass DemoApplicationClass;

G_DEFINE_TYPE (DemoApplication, demo_application, GTK_TYPE_APPLICATION)

typedef struct {
  GtkApplicationWindow parent_instance;

  GtkWidget *message;
  GtkWidget *infobar;
16 17 18
  GtkWidget *status;
  GtkWidget *menutool;
  GMenuModel *toolmenu;
19
  GtkTextBuffer *buffer;
20 21 22 23 24

  int width;
  int height;
  gboolean maximized;
  gboolean fullscreen;
25 26 27 28 29
} DemoApplicationWindow;
typedef GtkApplicationWindowClass DemoApplicationWindowClass;

G_DEFINE_TYPE (DemoApplicationWindow, demo_application_window, GTK_TYPE_APPLICATION_WINDOW)

30 31
static void create_window (GApplication *app, const char *contents);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void
show_action_dialog (GSimpleAction *action)
{
  const gchar *name;
  GtkWidget *dialog;

  name = g_action_get_name (G_ACTION (action));

  dialog = gtk_message_dialog_new (NULL,
                                   GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_INFO,
                                   GTK_BUTTONS_CLOSE,
                                   "You activated action: \"%s\"",
                                    name);

  g_signal_connect (dialog, "response",
                    G_CALLBACK (gtk_widget_destroy), NULL);

  gtk_widget_show (dialog);
}

static void
show_action_infobar (GSimpleAction *action,
                     GVariant      *parameter,
56
                     gpointer       data)
57
{
58
  DemoApplicationWindow *window = data;
59 60 61 62 63 64 65 66 67
  gchar *text;
  const gchar *name;
  const gchar *value;

  name = g_action_get_name (G_ACTION (action));
  value = g_variant_get_string (parameter, NULL);

  text = g_strdup_printf ("You activated radio action: \"%s\".\n"
                          "Current value: %s", name, value);
68 69
  gtk_label_set_text (GTK_LABEL (window->message), text);
  gtk_widget_show (window->infobar);
70 71 72 73
  g_free (text);
}

static void
74 75 76
activate_action (GSimpleAction *action,
                 GVariant      *parameter,
                 gpointer       user_data)
77 78 79 80
{
  show_action_dialog (action);
}

81
static void
82 83 84
activate_new (GSimpleAction *action,
              GVariant      *parameter,
              gpointer       user_data)
85 86 87 88 89 90 91 92
{
  GApplication *app = user_data;

  create_window (app, NULL);
}

static void
open_response_cb (GtkNativeDialog *dialog,
93 94
                  gint             response_id,
                  gpointer         user_data)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
  GtkFileChooserNative *native = user_data;
  GApplication *app = g_object_get_data (G_OBJECT (native), "app");
  GtkWidget *message_dialog;
  GFile *file;
  char *contents;
  GError *error = NULL;

  if (response_id == GTK_RESPONSE_ACCEPT)
    {
      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (native));

      if (g_file_load_contents (file, NULL, &contents, NULL, NULL, &error))
        {
          create_window (app, contents);
          g_free (contents);
        }
      else
        {
          message_dialog = gtk_message_dialog_new (NULL,
                                                   GTK_DIALOG_DESTROY_WITH_PARENT,
                                                   GTK_MESSAGE_ERROR,
                                                   GTK_BUTTONS_CLOSE,
                                                   "Error loading file: \"%s\"",
                                                   error->message);
          g_signal_connect (message_dialog, "response",
                            G_CALLBACK (gtk_widget_destroy), NULL);
          gtk_widget_show (message_dialog);
          g_error_free (error);
        }
    }

127
  gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (native));
128 129 130 131 132
  g_object_unref (native);
}


static void
133 134 135
activate_open (GSimpleAction *action,
               GVariant      *parameter,
               gpointer       user_data)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
{
  GApplication *app = user_data;
  GtkFileChooserNative *native;

  native = gtk_file_chooser_native_new ("Open File",
                                        NULL,
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        "_Open",
                                        "_Cancel");

  g_object_set_data_full (G_OBJECT (native), "app", g_object_ref (app), g_object_unref);
  g_signal_connect (native,
                    "response",
                    G_CALLBACK (open_response_cb),
                    native);

  gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static void
activate_toggle (GSimpleAction *action,
                 GVariant      *parameter,
                 gpointer       user_data)
{
  GVariant *state;

  show_action_dialog (action);

  state = g_action_get_state (G_ACTION (action));
  g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
  g_variant_unref (state);
}

static void
activate_radio (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  show_action_infobar (action, parameter, user_data);

  g_action_change_state (G_ACTION (action), parameter);
}

static void
activate_about (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  GtkWidget *window = user_data;

  const gchar *authors[] = {
    "Peter Mattis",
    "Spencer Kimball",
    "Josh MacDonald",
    "and many more...",
    NULL
  };

  const gchar *documentors[] = {
    "Owen Taylor",
    "Tony Gale",
    "Matthias Clasen <mclasen@redhat.com>",
    "and many more...",
    NULL
  };

  gtk_show_about_dialog (GTK_WINDOW (window),
                         "program-name", "GTK+ Code Demos",
                         "version", g_strdup_printf ("%s,\nRunning against GTK+ %d.%d.%d",
                                                     PACKAGE_VERSION,
                                                     gtk_get_major_version (),
                                                     gtk_get_minor_version (),
                                                     gtk_get_micro_version ()),
Matthias Clasen's avatar
Matthias Clasen committed
209
                         "copyright", "(C) 1997-2013 The GTK+ Team",
210 211 212 213 214
                         "license-type", GTK_LICENSE_LGPL_2_1,
                         "website", "http://www.gtk.org",
                         "comments", "Program to demonstrate GTK+ functions.",
                         "authors", authors,
                         "documenters", documentors,
215
                         "logo-icon-name", "gtk3-demo",
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
                         "title", "About GTK+ Code Demos",
                         NULL);
}

static void
activate_quit (GSimpleAction *action,
               GVariant      *parameter,
               gpointer       user_data)
{
  GtkApplication *app = user_data;
  GtkWidget *win;
  GList *list, *next;

  list = gtk_application_get_windows (app);
  while (list)
    {
      win = list->data;
      next = list->next;

      gtk_widget_destroy (GTK_WIDGET (win));

      list = next;
    }
}

static void
242 243
update_statusbar (GtkTextBuffer         *buffer,
                  DemoApplicationWindow *window)
244 245 246 247 248 249 250
{
  gchar *msg;
  gint row, col;
  gint count;
  GtkTextIter iter;

  /* clear any previous message, underflow is allowed */
251
  gtk_statusbar_pop (GTK_STATUSBAR (window->status), 0);
252 253 254 255 256 257 258 259 260 261 262 263 264

  count = gtk_text_buffer_get_char_count (buffer);

  gtk_text_buffer_get_iter_at_mark (buffer,
                                    &iter,
                                    gtk_text_buffer_get_insert (buffer));

  row = gtk_text_iter_get_line (&iter);
  col = gtk_text_iter_get_line_offset (&iter);

  msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document",
                         row, col, count);

265
  gtk_statusbar_push (GTK_STATUSBAR (window->status), 0, msg);
266 267 268 269 270

  g_free (msg);
}

static void
271 272 273 274
mark_set_callback (GtkTextBuffer         *buffer,
                   const GtkTextIter     *new_location,
                   GtkTextMark           *mark,
                   DemoApplicationWindow *window)
275
{
276
  update_statusbar (buffer, window);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

static void
change_theme_state (GSimpleAction *action,
                    GVariant      *state,
                    gpointer       user_data)
{
  GtkSettings *settings = gtk_settings_get_default ();

  g_object_set (G_OBJECT (settings),
                "gtk-application-prefer-dark-theme",
                g_variant_get_boolean (state),
                NULL);

  g_simple_action_set_state (action, state);
}

static void
change_titlebar_state (GSimpleAction *action,
                       GVariant      *state,
                       gpointer       user_data)
{
  GtkWindow *window = user_data;

  gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window),
                                               g_variant_get_boolean (state));

  g_simple_action_set_state (action, state);
}

static void
change_radio_state (GSimpleAction *action,
                    GVariant      *state,
                    gpointer       user_data)
{
  g_simple_action_set_state (action, state);
}

static GActionEntry app_entries[] = {
316 317
  { "new", activate_new, NULL, NULL, NULL },
  { "open", activate_open, NULL, NULL, NULL },
318 319 320 321 322 323 324 325 326 327 328
  { "save", activate_action, NULL, NULL, NULL },
  { "save-as", activate_action, NULL, NULL, NULL },
  { "quit", activate_quit, NULL, NULL, NULL },
  { "dark", activate_toggle, NULL, "false", change_theme_state }
};

static GActionEntry win_entries[] = {
  { "titlebar", activate_toggle, NULL, "false", change_titlebar_state },
  { "shape", activate_radio, "s", "'oval'", change_radio_state },
  { "bold", activate_toggle, NULL, "false", NULL },
  { "about", activate_about, NULL, NULL, NULL },
329
  { "file1", activate_action, NULL, NULL, NULL },
330 331 332 333
  { "logo", activate_action, NULL, NULL, NULL }
};

static void
334
clicked_cb (GtkWidget *widget, DemoApplicationWindow *window)
335
{
336
  gtk_widget_hide (window->infobar);
337 338
}

339 340 341 342 343 344 345
static void
startup (GApplication *app)
{
  GtkBuilder *builder;
  GMenuModel *appmenu;
  GMenuModel *menubar;

346 347
  G_APPLICATION_CLASS (demo_application_parent_class)->startup (app);

348
  builder = gtk_builder_new ();
349
  gtk_builder_add_from_resource (builder, "/application_demo/menus.ui", NULL);
350 351 352 353 354 355 356 357 358 359

  appmenu = (GMenuModel *)gtk_builder_get_object (builder, "appmenu");
  menubar = (GMenuModel *)gtk_builder_get_object (builder, "menubar");

  gtk_application_set_app_menu (GTK_APPLICATION (app), appmenu);
  gtk_application_set_menubar (GTK_APPLICATION (app), menubar);

  g_object_unref (builder);
}

360
static void
361
create_window (GApplication *app,
362
               const char   *content)
363
{
364
  DemoApplicationWindow *window;
365

366 367 368
  window = (DemoApplicationWindow *)g_object_new (demo_application_window_get_type (),
                                                  "application", app,
                                                  NULL);
369 370
  if (content)
    gtk_text_buffer_set_text (window->buffer, content, -1);
371 372

  gtk_window_present (GTK_WINDOW (window));
373 374
}

375 376 377 378 379 380
static void
activate (GApplication *app)
{
  create_window (app, NULL);
}

381 382
static void
demo_application_init (DemoApplication *app)
383
{
384 385
  GSettings *settings;
  GAction *action;
386

387
  settings = g_settings_new ("org.gtk.Demo");
388 389 390 391 392

  g_action_map_add_action_entries (G_ACTION_MAP (app),
                                   app_entries, G_N_ELEMENTS (app_entries),
                                   app);

393 394 395
  action = g_settings_create_action (settings, "color");

  g_action_map_add_action (G_ACTION_MAP (app), action);
396 397

  g_object_unref (settings);
398 399 400 401 402 403 404 405 406 407 408 409
}

static void
demo_application_class_init (DemoApplicationClass *class)
{
  GApplicationClass *app_class = G_APPLICATION_CLASS (class);

  app_class->startup = startup;
  app_class->activate = activate;
}

static void
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
demo_application_window_store_state (DemoApplicationWindow *win)
{
  GSettings *settings;

  settings = g_settings_new ("org.gtk.Demo");
  g_settings_set (settings, "window-size", "(ii)", win->width, win->height);
  g_settings_set_boolean (settings, "maximized", win->maximized);
  g_settings_set_boolean (settings, "fullscreen", win->fullscreen);
  g_object_unref (settings);
}

static void
demo_application_window_load_state (DemoApplicationWindow *win)
{
  GSettings *settings;

  settings = g_settings_new ("org.gtk.Demo");
  g_settings_get (settings, "window-size", "(ii)", &win->width, &win->height);
  win->maximized = g_settings_get_boolean (settings, "maximized");
  win->fullscreen = g_settings_get_boolean (settings, "fullscreen");
  g_object_unref (settings);
}

static void
demo_application_window_init (DemoApplicationWindow *window)
435
{
436 437 438 439 440 441 442 443 444 445 446 447 448
  GtkWidget *menu;

  window->width = -1;
  window->height = -1;
  window->maximized = FALSE;
  window->fullscreen = FALSE;

  gtk_widget_init_template (GTK_WIDGET (window));

  menu = gtk_menu_new_from_model (window->toolmenu);
  gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (window->menutool), menu);

  g_action_map_add_action_entries (G_ACTION_MAP (window),
449
                                   win_entries, G_N_ELEMENTS (win_entries),
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                                   window);
}

static void
demo_application_window_constructed (GObject *object)
{
  DemoApplicationWindow *window = (DemoApplicationWindow *)object;

  demo_application_window_load_state (window);

  gtk_window_set_default_size (GTK_WINDOW (window), window->width, window->height);

  if (window->maximized)
    gtk_window_maximize (GTK_WINDOW (window));

  if (window->fullscreen)
    gtk_window_fullscreen (GTK_WINDOW (window));

  G_OBJECT_CLASS (demo_application_window_parent_class)->constructed (object);
}

static void
demo_application_window_size_allocate (GtkWidget     *widget,
                                       GtkAllocation *allocation)
{
  DemoApplicationWindow *window = (DemoApplicationWindow *)widget;

  GTK_WIDGET_CLASS (demo_application_window_parent_class)->size_allocate (widget, allocation);

  if (!window->maximized && !window->fullscreen)
480
    gtk_window_get_size (GTK_WINDOW (window), &window->width, &window->height);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
}

static gboolean
demo_application_window_state_event (GtkWidget           *widget,
                                     GdkEventWindowState *event)
{
  DemoApplicationWindow *window = (DemoApplicationWindow *)widget;
  gboolean res = GDK_EVENT_PROPAGATE;

  if (GTK_WIDGET_CLASS (demo_application_window_parent_class)->window_state_event)
    res = GTK_WIDGET_CLASS (demo_application_window_parent_class)->window_state_event (widget, event);

  window->maximized = (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED) != 0;
  window->fullscreen = (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) != 0;

  return res;
}

static void
demo_application_window_destroy (GtkWidget *widget)
{
  DemoApplicationWindow *window = (DemoApplicationWindow *)widget;

  demo_application_window_store_state (window);

  GTK_WIDGET_CLASS (demo_application_window_parent_class)->destroy (widget);
507
}
508

509 510 511
static void
demo_application_window_class_init (DemoApplicationWindowClass *class)
{
512
  GObjectClass *object_class = G_OBJECT_CLASS (class);
513 514
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

515 516 517 518 519 520
  object_class->constructed = demo_application_window_constructed;

  widget_class->size_allocate = demo_application_window_size_allocate;
  widget_class->window_state_event = demo_application_window_state_event;
  widget_class->destroy = demo_application_window_destroy;

521
  gtk_widget_class_set_template_from_resource (widget_class, "/application_demo/application.ui");
522 523
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, message);
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, infobar);
524
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, status);
525
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, buffer);
526 527
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, menutool);
  gtk_widget_class_bind_template_child (widget_class, DemoApplicationWindow, toolmenu);
528 529 530 531
  gtk_widget_class_bind_template_callback (widget_class, clicked_cb);
  gtk_widget_class_bind_template_callback (widget_class, update_statusbar);
  gtk_widget_class_bind_template_callback (widget_class, mark_set_callback);
}
532

533 534 535 536 537 538 539
int
main (int argc, char *argv[])
{
  GtkApplication *app;

  app = GTK_APPLICATION (g_object_new (demo_application_get_type (),
                                       "application-id", "org.gtk.Demo2",
540
                                       "flags", G_APPLICATION_HANDLES_OPEN,
541
                                       NULL));
542

543
  return g_application_run (G_APPLICATION (app), 0, NULL);
544
}