2010-09-05 03:40:32 +00:00
|
|
|
/* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-09-05 03:40:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *test_window;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TEST_WIDGET_LABEL,
|
|
|
|
TEST_WIDGET_WRAP_LABEL,
|
|
|
|
TEST_WIDGET_IMAGE,
|
|
|
|
TEST_WIDGET_BUTTON,
|
|
|
|
TEST_WIDGET_LAST
|
|
|
|
};
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static gboolean done = FALSE;
|
2010-09-05 03:40:32 +00:00
|
|
|
static GtkWidget *test_widgets[TEST_WIDGET_LAST];
|
|
|
|
|
|
|
|
static GtkWidget*
|
|
|
|
create_image (void)
|
|
|
|
{
|
2017-11-15 00:43:13 +00:00
|
|
|
return gtk_image_new_from_icon_name ("document-open");
|
2010-09-05 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget*
|
2017-04-25 15:26:49 +00:00
|
|
|
create_label (gboolean wrap)
|
2010-09-05 03:40:32 +00:00
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
widget = gtk_label_new ("This is a label, label label label");
|
|
|
|
|
|
|
|
if (wrap)
|
2019-08-22 10:41:39 +00:00
|
|
|
gtk_label_set_wrap (GTK_LABEL (widget), TRUE);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget*
|
|
|
|
create_button (void)
|
|
|
|
{
|
|
|
|
return gtk_button_new_with_label ("BUTTON!");
|
|
|
|
}
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2020-03-06 17:05:43 +00:00
|
|
|
gboolean *is_done = data;
|
2020-02-10 03:24:47 +00:00
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
*is_done = TRUE;
|
2020-02-10 03:24:47 +00:00
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2010-09-05 03:40:32 +00:00
|
|
|
static void
|
|
|
|
open_test_window (void)
|
|
|
|
{
|
2011-09-27 20:02:00 +00:00
|
|
|
GtkWidget *grid;
|
2010-09-05 03:40:32 +00:00
|
|
|
int i;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
test_window = gtk_window_new ();
|
2010-09-05 16:59:29 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (test_window), "Tests");
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (test_window, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
gtk_window_set_resizable (GTK_WINDOW (test_window), FALSE);
|
|
|
|
|
|
|
|
test_widgets[TEST_WIDGET_IMAGE] = create_image ();
|
2017-04-25 15:26:49 +00:00
|
|
|
test_widgets[TEST_WIDGET_LABEL] = create_label (FALSE);
|
|
|
|
test_widgets[TEST_WIDGET_WRAP_LABEL] = create_label (TRUE);
|
2010-09-05 03:40:32 +00:00
|
|
|
test_widgets[TEST_WIDGET_BUTTON] = create_button ();
|
|
|
|
|
2011-09-27 20:02:00 +00:00
|
|
|
grid = gtk_grid_new ();
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (test_window), grid);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
for (i = 0; i < TEST_WIDGET_LAST; ++i)
|
|
|
|
{
|
2011-09-27 20:02:00 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), test_widgets[i], i % 3, i / 3, 1, 1);
|
2010-09-05 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:06:33 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (test_window));
|
2010-09-05 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2010-09-05 16:59:29 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Controls");
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), toggle);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
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);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), toggle);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
|
2022-11-29 12:06:33 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (window));
|
2010-09-05 03:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:32:24 +00:00
|
|
|
#define TEST_WIDGET(outer) (gtk_overlay_get_child (GTK_OVERLAY (gtk_overlay_get_child (GTK_OVERLAY (outer)))))
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
static GtkWidget*
|
|
|
|
create_widget_visible_border (const char *text)
|
|
|
|
{
|
|
|
|
GtkWidget *outer_box;
|
|
|
|
GtkWidget *inner_box;
|
|
|
|
GtkWidget *test_widget;
|
|
|
|
GtkWidget *label;
|
|
|
|
|
2017-08-01 22:49:42 +00:00
|
|
|
outer_box = gtk_overlay_new ();
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (outer_box, "black-bg");
|
2010-09-05 16:59:29 +00:00
|
|
|
|
2017-08-01 22:49:42 +00:00
|
|
|
inner_box = gtk_overlay_new ();
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (inner_box, "blue-bg");
|
2010-09-05 16:59:29 +00:00
|
|
|
|
2020-08-12 04:55:33 +00:00
|
|
|
gtk_overlay_set_child (GTK_OVERLAY (outer_box), inner_box);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
|
2017-08-01 22:49:42 +00:00
|
|
|
test_widget = gtk_overlay_new ();
|
2020-04-10 16:08:16 +00:00
|
|
|
gtk_widget_add_css_class (test_widget, "red-bg");
|
2010-09-05 16:59:29 +00:00
|
|
|
|
2020-08-12 04:55:33 +00:00
|
|
|
gtk_overlay_set_child (GTK_OVERLAY (inner_box), test_widget);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
label = gtk_label_new (text);
|
2020-08-12 04:55:33 +00:00
|
|
|
gtk_overlay_set_child (GTK_OVERLAY (test_widget), label);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
g_assert (TEST_WIDGET (outer_box) == test_widget);
|
|
|
|
|
|
|
|
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*
|
2010-09-15 22:37:20 +00:00
|
|
|
create_aligned (GtkAlign halign,
|
|
|
|
GtkAlign valign)
|
2010-09-05 16:59:29 +00:00
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
char *label;
|
|
|
|
|
|
|
|
label = g_strdup_printf ("h=%s v=%s",
|
2010-09-15 22:37:20 +00:00
|
|
|
enum_to_string (GTK_TYPE_ALIGN, halign),
|
|
|
|
enum_to_string (GTK_TYPE_ALIGN, valign));
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
widget = create_widget_visible_border (label);
|
|
|
|
|
|
|
|
g_object_set (G_OBJECT (TEST_WIDGET (widget)),
|
2010-09-15 22:37:20 +00:00
|
|
|
"halign", halign,
|
|
|
|
"valign", valign,
|
2011-09-27 20:02:00 +00:00
|
|
|
"hexpand", TRUE,
|
|
|
|
"vexpand", TRUE,
|
2010-09-05 16:59:29 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_alignment_window (void)
|
|
|
|
{
|
2011-09-27 20:02:00 +00:00
|
|
|
GtkWidget *grid;
|
2010-09-05 16:59:29 +00:00
|
|
|
int i;
|
|
|
|
GEnumClass *align_class;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
test_window = gtk_window_new ();
|
2010-09-05 16:59:29 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (test_window), "Alignment");
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (test_window, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-09-27 20:02:00 +00:00
|
|
|
grid = gtk_grid_new ();
|
|
|
|
gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
|
|
|
|
gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (test_window), grid);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-09-27 20:02:00 +00:00
|
|
|
gtk_grid_attach (GTK_GRID (grid), child, i, j, 1, 1);
|
2010-09-05 16:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:06:33 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (test_window));
|
2010-09-05 16:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-09-27 20:02:00 +00:00
|
|
|
"hexpand", TRUE,
|
|
|
|
"vexpand", TRUE,
|
2010-09-05 16:59:29 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_margin_window (void)
|
|
|
|
{
|
2011-09-27 20:02:00 +00:00
|
|
|
GtkWidget *box;
|
2010-09-05 16:59:29 +00:00
|
|
|
int i;
|
2020-02-26 01:57:20 +00:00
|
|
|
const char *margins[] = {
|
2013-11-14 22:31:17 +00:00
|
|
|
"margin-start",
|
|
|
|
"margin-end",
|
2010-09-05 16:59:29 +00:00
|
|
|
"margin-top",
|
2020-02-26 01:57:20 +00:00
|
|
|
"margin-bottom"
|
2010-09-05 16:59:29 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
test_window = gtk_window_new ();
|
2010-09-05 16:59:29 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (test_window), "Margin");
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (test_window, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
gtk_window_set_resizable (GTK_WINDOW (test_window), TRUE);
|
|
|
|
|
2011-09-27 20:02:00 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (test_window), box);
|
2010-09-05 16:59:29 +00:00
|
|
|
|
|
|
|
for (i = 0; i < (int) G_N_ELEMENTS (margins); ++i)
|
|
|
|
{
|
|
|
|
GtkWidget *child =
|
|
|
|
create_margined(margins[i]);
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), child);
|
2010-09-05 16:59:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:06:33 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (test_window));
|
2010-09-05 16:59:29 +00:00
|
|
|
}
|
|
|
|
|
2010-09-27 12:15:16 +00:00
|
|
|
static void
|
|
|
|
open_valigned_label_window (void)
|
|
|
|
{
|
|
|
|
GtkWidget *window, *box, *label, *frame;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2010-09-27 12:15:16 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (test_window, "destroy", G_CALLBACK (quit_cb), &done);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
2010-10-22 15:04:46 +00:00
|
|
|
label = gtk_label_new ("Both labels expand");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), label);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("Some wrapping text with width-chars = 15 and max-width-chars = 35");
|
2019-08-22 10:41:39 +00:00
|
|
|
gtk_label_set_wrap (GTK_LABEL (label), TRUE);
|
2010-09-27 12:15:16 +00:00
|
|
|
gtk_label_set_width_chars (GTK_LABEL (label), 15);
|
|
|
|
gtk_label_set_max_width_chars (GTK_LABEL (label), 35);
|
|
|
|
|
|
|
|
frame = gtk_frame_new (NULL);
|
2020-05-01 17:41:22 +00:00
|
|
|
gtk_frame_set_child (GTK_FRAME (frame), label);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
|
|
|
gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
|
2010-10-22 15:04:46 +00:00
|
|
|
gtk_widget_set_halign (frame, GTK_ALIGN_CENTER);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), frame);
|
2010-09-27 12:15:16 +00:00
|
|
|
|
|
|
|
gtk_window_present (GTK_WINDOW (window));
|
|
|
|
}
|
|
|
|
|
2010-09-05 03:40:32 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2014-10-08 02:04:26 +00:00
|
|
|
GtkCssProvider *provider;
|
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2014-10-08 02:04:26 +00:00
|
|
|
provider = gtk_css_provider_new ();
|
2023-05-16 20:22:01 +00:00
|
|
|
gtk_css_provider_load_from_string (provider,
|
2014-10-08 02:04:26 +00:00
|
|
|
".black-bg { background-color: black; }"
|
|
|
|
".red-bg { background-color: red; }"
|
2023-05-16 20:22:01 +00:00
|
|
|
".blue-bg { background-color: blue; }");
|
2017-10-31 01:35:21 +00:00
|
|
|
gtk_style_context_add_provider_for_display (gdk_display_get_default (),
|
|
|
|
GTK_STYLE_PROVIDER (provider),
|
|
|
|
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
2014-10-08 02:04:26 +00:00
|
|
|
g_object_unref (provider);
|
2023-05-16 20:22:01 +00:00
|
|
|
|
2011-03-30 06:25:12 +00:00
|
|
|
if (g_getenv ("RTL"))
|
|
|
|
gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
|
|
|
|
|
2010-09-05 03:40:32 +00:00
|
|
|
open_test_window ();
|
|
|
|
open_control_window ();
|
2010-09-05 16:59:29 +00:00
|
|
|
open_alignment_window ();
|
|
|
|
open_margin_window ();
|
2010-09-27 12:15:16 +00:00
|
|
|
open_valigned_label_window ();
|
2010-09-05 03:40:32 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2010-09-05 03:40:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|