testadjustsize.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* testadjustsize.c
 * Copyright (C) 2010 Havoc Pennington
 *
 * Author:
 *      Havoc Pennington <hp@pobox.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

#include <gtk/gtk.h>

static GtkWidget *test_window;

enum {
  TEST_WIDGET_LABEL,
  TEST_WIDGET_VERTICAL_LABEL,
  TEST_WIDGET_WRAP_LABEL,
  TEST_WIDGET_ALIGNMENT,
  TEST_WIDGET_IMAGE,
  TEST_WIDGET_BUTTON,
  TEST_WIDGET_LAST
};

static GtkWidget *test_widgets[TEST_WIDGET_LAST];

static GtkWidget*
create_image (void)
{
40 41
  return gtk_image_new_from_icon_name ("document-open",
                                       GTK_ICON_SIZE_BUTTON);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
}

static GtkWidget*
create_label (gboolean vertical,
              gboolean wrap)
{
  GtkWidget *widget;

  widget = gtk_label_new ("This is a label, label label label");

  if (vertical)
    gtk_label_set_angle (GTK_LABEL (widget), 90);

  if (wrap)
    gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);

  return widget;
}

static GtkWidget*
create_button (void)
{
  return gtk_button_new_with_label ("BUTTON!");
}

static gboolean
68 69 70
on_draw_alignment (GtkWidget      *widget,
                   cairo_t        *cr,
                   void           *data)
71 72
{
  cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
73
  cairo_paint (cr);
74 75 76 77 78 79 80 81 82

  return FALSE;
}

static GtkWidget*
create_alignment (void)
{
  GtkWidget *alignment;

83
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
84
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
85
G_GNUC_END_IGNORE_DEPRECATIONS
86 87 88 89

  /* make the alignment visible */
  gtk_widget_set_redraw_on_allocate (alignment, TRUE);
  g_signal_connect (G_OBJECT (alignment),
90 91
                    "draw",
                    G_CALLBACK (on_draw_alignment),
92 93 94 95 96 97 98 99
                    NULL);

  return alignment;
}

static void
open_test_window (void)
{
100
  GtkWidget *grid;
101 102 103
  int i;

  test_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
104
  gtk_window_set_title (GTK_WINDOW (test_window), "Tests");
105 106 107 108 109 110 111 112 113 114 115 116 117

  g_signal_connect (test_window, "delete-event",
                    G_CALLBACK (gtk_main_quit), test_window);

  gtk_window_set_resizable (GTK_WINDOW (test_window), FALSE);

  test_widgets[TEST_WIDGET_IMAGE] = create_image ();
  test_widgets[TEST_WIDGET_LABEL] = create_label (FALSE, FALSE);
  test_widgets[TEST_WIDGET_VERTICAL_LABEL] = create_label (TRUE, FALSE);
  test_widgets[TEST_WIDGET_WRAP_LABEL] = create_label (FALSE, TRUE);
  test_widgets[TEST_WIDGET_BUTTON] = create_button ();
  test_widgets[TEST_WIDGET_ALIGNMENT] = create_alignment ();

118
  grid = gtk_grid_new ();
119

120
  gtk_container_add (GTK_CONTAINER (test_window), grid);
121 122 123

  for (i = 0; i < TEST_WIDGET_LAST; ++i)
    {
124
      gtk_grid_attach (GTK_GRID (grid), test_widgets[i], i % 3, i / 3, 1, 1);
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 180 181 182 183 184 185 186 187 188 189 190
    }

  gtk_widget_show_all (test_window);
}

static void
on_toggle_border_widths (GtkToggleButton *button,
                         void            *data)
{
  gboolean has_border;
  int i;

  has_border = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));

  for (i = 0; i < TEST_WIDGET_LAST; ++i)
    {
      if (GTK_IS_CONTAINER (test_widgets[i]))
        {
          gtk_container_set_border_width (GTK_CONTAINER (test_widgets[i]),
                                          has_border ? 50 : 0);
        }
    }
}

static void
on_set_small_size_requests (GtkToggleButton *button,
                            void            *data)
{
  gboolean has_small_size_requests;
  int i;

  has_small_size_requests = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));

  for (i = 0; i < TEST_WIDGET_LAST; ++i)
    {
      gtk_widget_set_size_request (test_widgets[i],
                                   has_small_size_requests ? 5 : -1,
                                   has_small_size_requests ? 5 : -1);
    }
}

static void
on_set_large_size_requests (GtkToggleButton *button,
                            void            *data)
{
  gboolean has_large_size_requests;
  int i;

  has_large_size_requests = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));

  for (i = 0; i < TEST_WIDGET_LAST; ++i)
    {
      gtk_widget_set_size_request (test_widgets[i],
                                   has_large_size_requests ? 200 : -1,
                                   has_large_size_requests ? 200 : -1);
    }
}

static void
open_control_window (void)
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *toggle;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
191
  gtk_window_set_title (GTK_WINDOW (window), "Controls");
192 193 194 195

  g_signal_connect (window, "delete-event",
                    G_CALLBACK (gtk_main_quit), window);

196
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  gtk_container_add (GTK_CONTAINER (window), box);

  toggle =
    gtk_toggle_button_new_with_label ("Containers have borders");
  g_signal_connect (G_OBJECT (toggle),
                    "toggled", G_CALLBACK (on_toggle_border_widths),
                    NULL);
  gtk_container_add (GTK_CONTAINER (box), toggle);

  toggle =
    gtk_toggle_button_new_with_label ("Set small size requests");
  g_signal_connect (G_OBJECT (toggle),
                    "toggled", G_CALLBACK (on_set_small_size_requests),
                    NULL);
  gtk_container_add (GTK_CONTAINER (box), toggle);

  toggle =
    gtk_toggle_button_new_with_label ("Set large size requests");
  g_signal_connect (G_OBJECT (toggle),
                    "toggled", G_CALLBACK (on_set_large_size_requests),
                    NULL);
  gtk_container_add (GTK_CONTAINER (box), toggle);


  gtk_widget_show_all (window);
}

224 225 226 227 228 229 230 231 232 233 234
#define TEST_WIDGET(outer) (gtk_bin_get_child (GTK_BIN (gtk_bin_get_child (GTK_BIN(outer)))))

static GtkWidget*
create_widget_visible_border (const char *text)
{
  GtkWidget *outer_box;
  GtkWidget *inner_box;
  GtkWidget *test_widget;
  GtkWidget *label;

  outer_box = gtk_event_box_new ();
235
  gtk_style_context_add_class (gtk_widget_get_style_context (outer_box), "black-bg");
236 237 238

  inner_box = gtk_event_box_new ();
  gtk_container_set_border_width (GTK_CONTAINER (inner_box), 5);
239
  gtk_style_context_add_class (gtk_widget_get_style_context (inner_box), "blue-bg");
240 241 242 243 244

  gtk_container_add (GTK_CONTAINER (outer_box), inner_box);


  test_widget = gtk_event_box_new ();
245
  gtk_style_context_add_class (gtk_widget_get_style_context (test_widget), "red-bg");
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

  gtk_container_add (GTK_CONTAINER (inner_box), test_widget);

  label = gtk_label_new (text);
  gtk_container_add (GTK_CONTAINER (test_widget), label);

  g_assert (TEST_WIDGET (outer_box) == test_widget);

  gtk_widget_show_all (outer_box);

  return outer_box;
}

static const char*
enum_to_string (GType enum_type,
                int   value)
{
  GEnumValue *v;

  v = g_enum_get_value (g_type_class_peek (enum_type), value);

  return v->value_nick;
}

static GtkWidget*
271 272
create_aligned (GtkAlign halign,
                GtkAlign valign)
273 274 275 276 277
{
  GtkWidget *widget;
  char *label;

  label = g_strdup_printf ("h=%s v=%s",
278 279
                           enum_to_string (GTK_TYPE_ALIGN, halign),
                           enum_to_string (GTK_TYPE_ALIGN, valign));
280 281 282 283

  widget = create_widget_visible_border (label);

  g_object_set (G_OBJECT (TEST_WIDGET (widget)),
284 285
                "halign", halign,
                "valign", valign,
286 287
                "hexpand", TRUE,
                "vexpand", TRUE,
288 289 290 291 292 293 294 295
                NULL);

  return widget;
}

static void
open_alignment_window (void)
{
296
  GtkWidget *grid;
297 298 299 300 301 302 303 304 305 306 307 308 309 310
  int i;
  GEnumClass *align_class;

  test_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (test_window), "Alignment");

  g_signal_connect (test_window, "delete-event",
                    G_CALLBACK (gtk_main_quit), test_window);

  gtk_window_set_resizable (GTK_WINDOW (test_window), TRUE);
  gtk_window_set_default_size (GTK_WINDOW (test_window), 500, 500);

  align_class = g_type_class_peek (GTK_TYPE_ALIGN);

311 312 313
  grid = gtk_grid_new ();
  gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
  gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
314

315
  gtk_container_add (GTK_CONTAINER (test_window), grid);
316 317 318 319 320 321 322 323 324 325

  for (i = 0; i < align_class->n_values; ++i)
    {
      int j;
      for (j = 0; j < align_class->n_values; ++j)
        {
          GtkWidget *child =
            create_aligned(align_class->values[i].value,
                           align_class->values[j].value);

326
          gtk_grid_attach (GTK_GRID (grid), child, i, j, 1, 1);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        }
    }

  gtk_widget_show_all (test_window);
}

static GtkWidget*
create_margined (const char *propname)
{
  GtkWidget *widget;

  widget = create_widget_visible_border (propname);

  g_object_set (G_OBJECT (TEST_WIDGET (widget)),
                propname, 15,
342 343
                "hexpand", TRUE,
                "vexpand", TRUE,
344 345 346 347 348 349 350 351
                NULL);

  return widget;
}

static void
open_margin_window (void)
{
352
  GtkWidget *box;
353 354
  int i;
  const char * margins[] = {
355 356
    "margin-start",
    "margin-end",
357 358 359 360 361 362 363 364 365 366 367 368 369
    "margin-top",
    "margin-bottom",
    "margin"
  };

  test_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (test_window), "Margin");

  g_signal_connect (test_window, "delete-event",
                    G_CALLBACK (gtk_main_quit), test_window);

  gtk_window_set_resizable (GTK_WINDOW (test_window), TRUE);

370
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
371

372
  gtk_container_add (GTK_CONTAINER (test_window), box);
373 374 375 376 377 378

  for (i = 0; i < (int) G_N_ELEMENTS (margins); ++i)
    {
      GtkWidget *child =
        create_margined(margins[i]);

379
      gtk_container_add (GTK_CONTAINER (box), child);
380 381 382 383 384
    }

  gtk_widget_show_all (test_window);
}

385 386 387 388 389 390 391 392 393 394
static void
open_valigned_label_window (void)
{
  GtkWidget *window, *box, *label, *frame;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  g_signal_connect (test_window, "delete-event",
                    G_CALLBACK (gtk_main_quit), test_window);

395
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
396 397 398
  gtk_widget_show (box);
  gtk_container_add (GTK_CONTAINER (window), box);

399 400 401
  label = gtk_label_new ("Both labels expand");
  gtk_widget_show (label);
  gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
402 403 404 405 406 407 408 409 410 411 412 413 414

  label = gtk_label_new ("Some wrapping text with width-chars = 15 and max-width-chars = 35");
  gtk_label_set_line_wrap  (GTK_LABEL (label), TRUE);
  gtk_label_set_width_chars  (GTK_LABEL (label), 15);
  gtk_label_set_max_width_chars  (GTK_LABEL (label), 35);

  gtk_widget_show (label);

  frame  = gtk_frame_new (NULL);
  gtk_widget_show (frame);
  gtk_container_add (GTK_CONTAINER (frame), label);

  gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
415
  gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
416 417 418 419 420 421

  gtk_box_pack_start (GTK_BOX (box), frame, TRUE, TRUE, 0);

  gtk_window_present (GTK_WINDOW (window));
}

422 423 424
int
main (int argc, char *argv[])
{
425 426
  GtkCssProvider *provider;

427 428
  gtk_init (&argc, &argv);

429 430 431 432 433 434 435 436 437 438
  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider,
    ".black-bg { background-color: black; }"
    ".red-bg { background-color: red; }"
    ".blue-bg { background-color: blue; }", -1, NULL);
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
                                             GTK_STYLE_PROVIDER (provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  g_object_unref (provider);
  
439 440 441
  if (g_getenv ("RTL"))
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

442 443
  open_test_window ();
  open_control_window ();
444 445
  open_alignment_window ();
  open_margin_window ();
446
  open_valigned_label_window ();
447 448 449 450 451

  gtk_main ();

  return 0;
}