testheaderbar.c 5.59 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3
#include <gtk/gtk.h>

static const gchar css[] =
4
 ".main.background { "
5 6 7 8 9
 " background-image: -gtk-gradient (linear, center top, center bottom, "
 "      from (red), "
 "      to (blue)); "
 " border-width: 0px; "
 "}"
10
 ".titlebar.backdrop { "
11 12 13 14 15 16 17 18 19 20
 " background-image: none; "
 " background-color: @bg_color; "
 " border-radius: 10px 10px 0px 0px; "
 "}"
 ".titlebar { "
 " background-image: -gtk-gradient (linear, center top, center bottom, "
 "      from (white), "
 "      to (@bg_color)); "
 " border-radius: 10px 10px 0px 0px; "
 "}";
Matthias Clasen's avatar
Matthias Clasen committed
21

22 23 24 25 26 27 28 29 30
static void
on_bookmark_clicked (GtkButton *button, gpointer data)
{
  GtkWindow *window = GTK_WINDOW (data);
  GtkWidget *chooser;

  chooser = gtk_file_chooser_dialog_new ("File Chooser Test",
                                         window,
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
31
                                         "_Close",
32 33 34 35 36 37 38 39 40
                                         GTK_RESPONSE_CLOSE,
                                         NULL);

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

  gtk_widget_show (chooser);
}

41 42
static GtkWidget *header;

43 44 45
static void
change_subtitle (GtkButton *button, gpointer data)
{
46 47
  if (!GTK_IS_HEADER_BAR (header))
    return;
48

49
  if (gtk_header_bar_get_subtitle (GTK_HEADER_BAR (header)) == NULL)
50
    {
51
      gtk_header_bar_set_subtitle (GTK_HEADER_BAR (header), "(subtle subtitle)");
52 53 54
    }
  else
    {
55
      gtk_header_bar_set_subtitle (GTK_HEADER_BAR (header), NULL);
56 57 58
    }
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static void
toggle_fullscreen (GtkButton *button, gpointer data)
{
  GtkWidget *window = GTK_WIDGET (data);
  static gboolean fullscreen = FALSE;

  if (fullscreen)
    {
      gtk_window_unfullscreen (GTK_WINDOW (window));
      fullscreen = FALSE;
    }
  else
    {
      gtk_window_fullscreen (GTK_WINDOW (window));
      fullscreen = TRUE;
    }
}

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
static void
change_header (GtkButton *button, gpointer data)
{
  GtkWidget *window = GTK_WIDGET (data);
  GtkWidget *label;
  GtkWidget *widget;
  GtkWidget *image;
  GtkWidget *box;

  if (button && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
    {
      header = gtk_event_box_new ();
      gtk_style_context_add_class (gtk_widget_get_style_context (header), "titlebar");
      gtk_style_context_add_class (gtk_widget_get_style_context (header), "header-bar");
      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
      g_object_set (box, "margin", 10, NULL);
      gtk_container_add (GTK_CONTAINER (header), box);
      label = gtk_label_new ("Label");
      gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
      widget = gtk_level_bar_new ();
      gtk_level_bar_set_value (GTK_LEVEL_BAR (widget), 0.4);
      gtk_box_pack_start (GTK_BOX (box), widget, TRUE, TRUE, 0);
      gtk_widget_show_all (header);
    }
  else
    {
      header = gtk_header_bar_new ();
      gtk_style_context_add_class (gtk_widget_get_style_context (header), "titlebar");
      gtk_header_bar_set_title (GTK_HEADER_BAR (header), "Example header");

107
      widget = gtk_button_new_with_label ("_Close");
108
      gtk_button_set_use_underline (GTK_BUTTON (widget), TRUE);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      gtk_style_context_add_class (gtk_widget_get_style_context (widget), "suggested-action");
      g_signal_connect (widget, "clicked", G_CALLBACK (gtk_main_quit), NULL);

      gtk_header_bar_pack_end (GTK_HEADER_BAR (header), widget);

      widget= gtk_button_new ();
      image = gtk_image_new_from_icon_name ("bookmark-new-symbolic", GTK_ICON_SIZE_BUTTON);
      g_signal_connect (widget, "clicked", G_CALLBACK (on_bookmark_clicked), window);
      gtk_container_add (GTK_CONTAINER (widget), image);

      gtk_header_bar_pack_start (GTK_HEADER_BAR (header), widget);
      gtk_widget_show_all (header);
    }

  gtk_window_set_titlebar (GTK_WINDOW (window), header);
}

Matthias Clasen's avatar
Matthias Clasen committed
126 127 128 129 130 131 132 133 134 135 136 137 138
int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *footer;
  GtkWidget *button;
  GtkWidget *content;
  GtkCssProvider *provider;

  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
139
  gtk_style_context_add_class (gtk_widget_get_style_context (window), "main");
Matthias Clasen's avatar
Matthias Clasen committed
140 141 142 143 144 145 146 147

  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider, css, -1, NULL);
  gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (window),
                                             GTK_STYLE_PROVIDER (provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_USER);


148
  change_header (NULL, window);
Matthias Clasen's avatar
Matthias Clasen committed
149

150
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Matthias Clasen's avatar
Matthias Clasen committed
151 152
  gtk_container_add (GTK_CONTAINER (window), box);

153 154
  footer = gtk_action_bar_new ();
  gtk_action_bar_set_center_widget (GTK_ACTION_BAR (footer), gtk_check_button_new_with_label ("Middle"));
155 156
  button = gtk_toggle_button_new_with_label ("Custom");
  g_signal_connect (button, "clicked", G_CALLBACK (change_header), window);
157
  gtk_action_bar_pack_start (GTK_ACTION_BAR (footer), button);
158 159
  button = gtk_button_new_with_label ("Subtitle");
  g_signal_connect (button, "clicked", G_CALLBACK (change_subtitle), NULL);
160
  gtk_action_bar_pack_end (GTK_ACTION_BAR (footer), button);
161
  button = gtk_button_new_with_label ("Fullscreen");
162
  gtk_action_bar_pack_end (GTK_ACTION_BAR (footer), button);
163
  g_signal_connect (button, "clicked", G_CALLBACK (toggle_fullscreen), window);
Matthias Clasen's avatar
Matthias Clasen committed
164 165 166 167 168 169 170 171 172 173 174
  gtk_box_pack_end (GTK_BOX (box), footer, FALSE, FALSE, 0);

  content = gtk_image_new_from_icon_name ("start-here-symbolic", GTK_ICON_SIZE_DIALOG);
  gtk_image_set_pixel_size (GTK_IMAGE (content), 512);

  gtk_box_pack_start (GTK_BOX (box), content, FALSE, TRUE, 0);

  gtk_widget_show_all (window);

  gtk_main ();

175 176
  gtk_widget_destroy (window);

Matthias Clasen's avatar
Matthias Clasen committed
177 178
  return 0;
}