testdialog.c 12.8 KB
Newer Older
1 2
#include <gtk/gtk.h>

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
static void
show_message_dialog1 (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = GTK_WIDGET (gtk_message_dialog_new (parent,
                                               GTK_DIALOG_MODAL|
                                               GTK_DIALOG_DESTROY_WITH_PARENT|
                                               GTK_DIALOG_USE_HEADER_BAR,
                                               GTK_MESSAGE_INFO,
                                               GTK_BUTTONS_OK,
                                               "Oops! Something went wrong."));
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            "Unhandled error message: SSH program unexpectedly exited");

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
static void
show_message_dialog1a (GtkWindow *parent)
{
  GtkWidget *dialog;
  GtkWidget *image;

  dialog = GTK_WIDGET (gtk_message_dialog_new (parent,
                                               GTK_DIALOG_MODAL|
                                               GTK_DIALOG_DESTROY_WITH_PARENT|
                                               GTK_DIALOG_USE_HEADER_BAR,
                                               GTK_MESSAGE_INFO,
                                               GTK_BUTTONS_OK,
                                               "The system network services are not compatible with this version."));

  image = gtk_image_new_from_icon_name ("computer-fail", GTK_ICON_SIZE_DIALOG);
  gtk_widget_show (image);
38
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
39
  gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
40
  G_GNUC_END_IGNORE_DEPRECATIONS;
41 42 43 44 45 46


  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void
show_message_dialog2 (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = GTK_WIDGET (gtk_message_dialog_new (parent,
                                               GTK_DIALOG_MODAL|
                                               GTK_DIALOG_DESTROY_WITH_PARENT|
                                               GTK_DIALOG_USE_HEADER_BAR,
                                               GTK_MESSAGE_INFO,
                                               GTK_BUTTONS_NONE,
                                               "Empty all items from Wastebasket?"));
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            "All items in the Wastebasket will be permanently deleted");
  gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
                          "Cancel", GTK_RESPONSE_CANCEL,
                          "Empty Wastebasket", GTK_RESPONSE_OK,
                          NULL);  

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

70 71 72 73 74 75 76 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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
static void
show_color_chooser (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = gtk_color_chooser_dialog_new ("Builtin", parent);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
show_color_chooser_generic (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = g_object_new (GTK_TYPE_COLOR_CHOOSER_DIALOG,
                         "title", "Generic Builtin",
                         "transient-for", parent,
                         NULL);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
add_content (GtkWidget *dialog)
{
  GtkWidget *label;

  label = gtk_label_new ("content");
  g_object_set (label, "margin", 50, NULL);
  gtk_widget_show (label);

  gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), label);
}

static void
add_buttons (GtkWidget *dialog)
{
  gtk_dialog_add_button (GTK_DIALOG (dialog), "Done", GTK_RESPONSE_OK);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
}

static void
show_dialog (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = gtk_dialog_new_with_buttons ("Simple", parent, 
					GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
				        "Close", GTK_RESPONSE_CLOSE,
                                        NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
show_dialog_with_header (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = gtk_dialog_new_with_buttons ("With Header", parent, 
					GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_USE_HEADER_BAR,
				        "Close", GTK_RESPONSE_CLOSE,
                                        NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
show_dialog_with_buttons (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = gtk_dialog_new_with_buttons ("With Buttons", parent, 
					GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
				        "Close", GTK_RESPONSE_CLOSE,
				        "Frob", 25,
                                        NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
show_dialog_with_header_buttons (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = gtk_dialog_new_with_buttons ("Header & Buttons", parent, 
					GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_USE_HEADER_BAR,
				        "Close", GTK_RESPONSE_CLOSE,
				        "Frob", 25,
                                        NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static void
show_dialog_with_header_buttons2 (GtkWindow *parent)
{
  GtkBuilder *builder;
  GtkWidget *dialog;

  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "dialog.ui", NULL);
  dialog = (GtkWidget *)gtk_builder_get_object (builder, "dialog");
  g_object_unref (builder);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
typedef struct {
  GtkDialog parent;
} MyDialog;

typedef struct {
  GtkDialogClass parent_class;
} MyDialogClass;

G_DEFINE_TYPE (MyDialog, my_dialog, GTK_TYPE_DIALOG);

static void
my_dialog_init (MyDialog *dialog)
{
  gtk_widget_init_template (GTK_WIDGET (dialog));
}

static void
my_dialog_class_init (MyDialogClass *class)
{
  gchar *buffer;
  gsize size;
  GBytes *bytes;

218 219 220
  if (!g_file_get_contents ("mydialog.ui", &buffer, &size, NULL))
    g_error ("Template file mydialog.ui not found");

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  bytes = g_bytes_new_static (buffer, size);
  gtk_widget_class_set_template (GTK_WIDGET_CLASS (class), bytes);
  g_bytes_unref (bytes);
}

static void
show_dialog_from_template (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = g_object_new (my_dialog_get_type (),
                         "title", "Template",
                         "transient-for", parent,
                         NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void
show_dialog_flex_template (GtkWindow *parent)
{
  GtkWidget *dialog;
  gboolean use_header;

  g_object_get (gtk_settings_get_default (),
                "gtk-dialogs-use-header", &use_header,
                NULL);
  dialog = g_object_new (my_dialog_get_type (),
                         "title", "Flexible Template",
                         "transient-for", parent,
                         "use-header-bar", use_header,
                         NULL);

  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

typedef struct {
  GtkDialog parent;

  GtkWidget *content;
} MyDialog2;

typedef struct {
  GtkDialogClass parent_class;
} MyDialog2Class;

G_DEFINE_TYPE (MyDialog2, my_dialog2, GTK_TYPE_DIALOG);

static void
my_dialog2_init (MyDialog2 *dialog)
{
  gtk_widget_init_template (GTK_WIDGET (dialog));
}

static void
my_dialog2_class_init (MyDialog2Class *class)
{
  gchar *buffer;
  gsize size;
  GBytes *bytes;

288 289 290
  if (!g_file_get_contents ("mydialog2.ui", &buffer, &size, NULL))
    g_error ("Template file mydialog2.ui not found");

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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  bytes = g_bytes_new_static (buffer, size);
  gtk_widget_class_set_template (GTK_WIDGET_CLASS (class), bytes);
  g_bytes_unref (bytes);

  gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), MyDialog2, content);
}

static void
show_dialog_from_template_with_header (GtkWindow *parent)
{
  GtkWidget *dialog;

  dialog = g_object_new (my_dialog2_get_type (),
                         "transient-for", parent,
                         "use-header-bar", TRUE,
                         NULL);

  add_buttons (dialog);
  add_content (dialog);

  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *box;
  GtkWidget *button;

  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  gtk_widget_set_halign (vbox, GTK_ALIGN_FILL);
  gtk_widget_set_valign (vbox, GTK_ALIGN_CENTER);
  gtk_widget_show (vbox);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  
  box = gtk_flow_box_new ();
  gtk_flow_box_set_selection_mode (GTK_FLOW_BOX (box), GTK_SELECTION_NONE);
  gtk_widget_set_hexpand (box, TRUE);
  gtk_widget_show (box);
  gtk_container_add (GTK_CONTAINER (vbox), box);

340 341 342 343 344
  button = gtk_button_new_with_label ("Message dialog");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_message_dialog1), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

345 346 347 348 349
  button = gtk_button_new_with_label ("Message with icon");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_message_dialog1a), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

350 351 352 353 354 355
  button = gtk_button_new_with_label ("Confirmation dialog");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_message_dialog2), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Builtin");
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  button = gtk_button_new_with_label ("Builtin");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_color_chooser), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Generic Builtin");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_color_chooser_generic), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Simple");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("With Header");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_with_header), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("With Buttons");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_with_buttons), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Header & Buttons");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_with_header_buttons), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

386 387 388 389 390 391
  button = gtk_button_new_with_label ("Header & Buttons & Builder");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_with_header_buttons2), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Template");
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  button = gtk_button_new_with_label ("Template");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_from_template), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Template With Header");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_from_template_with_header), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_button_new_with_label ("Flexible Template");
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (show_dialog_flex_template), window);
  gtk_widget_show (button);
  gtk_container_add (GTK_CONTAINER (box), button);

  button = gtk_check_button_new_with_label ("Dialogs have headers");
  g_object_bind_property (gtk_settings_get_default (), "gtk-dialogs-use-header",
                          button, "active",
                          G_BINDING_BIDIRECTIONAL|G_BINDING_SYNC_CREATE);
  gtk_widget_show (button);
  gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
  gtk_container_add (GTK_CONTAINER (vbox), button);

415 416 417 418 419 420
  button = gtk_spinner_new ();
  gtk_spinner_start (GTK_SPINNER (button));
  gtk_widget_show (button);
  gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
  gtk_container_add (GTK_CONTAINER (vbox), button);

421 422 423 424 425 426
  gtk_widget_show (window);
  gtk_main ();
  
  return 0;
}