Compare commits

...

4 Commits

Author SHA1 Message Date
Tristan Van Berkom
7f691b2df2 Allow single line GtkSpreadTable
Added exception for single line spread table and
updated test case to allow configuration to a single line.
2010-10-11 21:45:42 +09:00
Tristan Van Berkom
7db09805f6 Added gtk_spread_table_get_child_line
Gets the line index in which a child would be positioned
if the table were to be allocated size in the opposing
orientation of the table.

For instance, if the table is oriented vertically,
this function will return the child's column if
the table were to be allocated size width.
2010-10-09 00:54:30 +09:00
Tristan Van Berkom
a5f74b6279 Changed GtkSpreadTable algorithm to be a binary search for the best height
This has some exceptions:
   - We still ignore the height of a widget that spans the entire column
     (i.e. large images dont force the whole table to try to be as tall
     as the image itself)
   - In this new algorithm we fill in to the best height from left to
     right which can leave trailing empty columns; in this case we place
     only a single widget in each trailing column.
2010-10-08 21:39:55 +09:00
Tristan Van Berkom
794c4c9ec7 Added GtkSpreadTable widget and test case.
GtkSpreadTable positions its children by distributing them as
evenly as possible across a fixed number of rows or columns.

When oriented vertically the GtkSpreadTable will list its
children in order from top to bottom in columns and request
the smallest height as possible regardless of differences in
child sizes.
2010-10-06 17:34:06 +09:00
7 changed files with 1634 additions and 1 deletions

View File

@@ -285,6 +285,7 @@ gtk_public_h_sources = \
gtksocket.h \
gtkspinbutton.h \
gtkspinner.h \
gtkspreadtable.h \
gtkstatusbar.h \
gtkstatusicon.h \
gtkstock.h \
@@ -558,6 +559,7 @@ gtk_base_c_sources = \
gtksocket.c \
gtkspinbutton.c \
gtkspinner.c \
gtkspreadtable.c \
gtkstatusbar.c \
gtkstatusicon.c \
gtkstock.c \

View File

@@ -167,6 +167,7 @@
#include <gtk/gtksocket.h>
#include <gtk/gtkspinbutton.h>
#include <gtk/gtkspinner.h>
#include <gtk/gtkspreadtable.h>
#include <gtk/gtkstatusbar.h>
#include <gtk/gtkstatusicon.h>
#include <gtk/gtkstock.h>

View File

@@ -3080,6 +3080,20 @@ gtk_spinner_stop
#endif
#endif
#if IN_HEADER(__GTK_SPREAD_TABLE_H__)
#if IN_FILE(__GTK_SPREAD_TABLE_C__)
gtk_spread_table_get_horizontal_spacing
gtk_spread_table_get_lines
gtk_spread_table_get_type
gtk_spread_table_get_vertical_spacing
gtk_spread_table_insert_child
gtk_spread_table_new
gtk_spread_table_set_horizontal_spacing
gtk_spread_table_set_lines
gtk_spread_table_set_vertical_spacing
#endif
#endif
#if IN_HEADER(__GTK_STATUSBAR_H__)
#if IN_FILE(__GTK_STATUSBAR_C__)
gtk_statusbar_get_context_id

1128
gtk/gtkspreadtable.c Normal file

File diff suppressed because it is too large Load Diff

89
gtk/gtkspreadtable.h Normal file
View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2010 Openismus GmbH
*
* Authors:
* Tristan Van Berkom <tristanvb@openismus.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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GTK_SPREAD_TABLE_H__
#define __GTK_SPREAD_TABLE_H__
#include <gtk/gtkcontainer.h>
G_BEGIN_DECLS
#define GTK_TYPE_SPREAD_TABLE (gtk_spread_table_get_type ())
#define GTK_SPREAD_TABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SPREAD_TABLE, GtkSpreadTable))
#define GTK_SPREAD_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_SPREAD_TABLE, GtkSpreadTableClass))
#define GTK_IS_SPREAD_TABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SPREAD_TABLE))
#define GTK_IS_SPREAD_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_SPREAD_TABLE))
#define GTK_SPREAD_TABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SPREAD_TABLE, GtkSpreadTableClass))
typedef struct _GtkSpreadTable GtkSpreadTable;
typedef struct _GtkSpreadTablePrivate GtkSpreadTablePrivate;
typedef struct _GtkSpreadTableClass GtkSpreadTableClass;
struct _GtkSpreadTable
{
GtkContainer parent_instance;
/*< private >*/
GtkSpreadTablePrivate *priv;
};
struct _GtkSpreadTableClass
{
GtkContainerClass parent_class;
};
GType gtk_spread_table_get_type (void) G_GNUC_CONST;
GtkWidget *gtk_spread_table_new (GtkOrientation orientation,
guint lines);
void gtk_spread_table_insert_child (GtkSpreadTable *table,
GtkWidget *child,
gint index);
guint gtk_spread_table_get_child_line (GtkSpreadTable *table,
GtkWidget *child,
gint size);
void gtk_spread_table_set_lines (GtkSpreadTable *table,
guint lines);
guint gtk_spread_table_get_lines (GtkSpreadTable *table);
void gtk_spread_table_set_horizontal_spacing (GtkSpreadTable *table,
guint spacing);
guint gtk_spread_table_get_horizontal_spacing (GtkSpreadTable *table);
void gtk_spread_table_set_vertical_spacing (GtkSpreadTable *table,
guint spacing);
guint gtk_spread_table_get_vertical_spacing (GtkSpreadTable *table);
G_END_DECLS
#endif /* __GTK_SPREAD_TABLE_H__ */

View File

@@ -91,7 +91,8 @@ noinst_PROGRAMS = $(TEST_PROGS) \
testtooltips \
testexpander \
testvolumebutton \
testwrapbox
testwrapbox \
testspreadtable
if USE_X11
noinst_PROGRAMS += testapplication testerrors
@@ -175,6 +176,7 @@ testgrouping_DEPENDENCIES = $(TEST_DEPS)
testtooltips_DEPENDENCIES = $(TEST_DEPS)
testvolumebutton_DEPENDENCIES = $(TEST_DEPS)
testwrapbox_DEPENDENCIES = $(TEST_DEPS)
testspreadtable_DEPENDENCIES = $(TEST_DEPS)
testwindows_DEPENDENCIES = $(TEST_DEPS)
testexpander_DEPENDENCIES = $(TEST_DEPS)
@@ -246,6 +248,7 @@ testgrouping_LDADD = $(LDADDS)
testtooltips_LDADD = $(LDADDS)
testvolumebutton_LDADD = $(LDADDS)
testwrapbox_LDADD = $(LDADDS)
testspreadtable_LDADD = $(LDADDS)
testwindows_LDADD = $(LDADDS)
testexpander_LDADD = $(LDADDS)
@@ -348,6 +351,9 @@ testvolumebutton_SOURCES = \
testwrapbox_SOURCES = \
testwrapbox.c
testspreadtable_SOURCES = \
testspreadtable.c
testoffscreen_SOURCES = \
gtkoffscreenbox.c \
gtkoffscreenbox.h \

393
tests/testspreadtable.c Normal file
View File

@@ -0,0 +1,393 @@
/* testspreadtable.c
* Copyright (C) 2010 Openismus GmbH
*
* Author:
* Tristan Van Berkom <tristanvb@openismus.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
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gtk/gtk.h>
enum {
IMAGE_NONE,
IMAGE_SMALL,
IMAGE_LARGE,
IMAGE_HUGE
};
#define INITIAL_HSPACING 2
#define INITIAL_VSPACING 2
#define INITIAL_LINES 3
#define INITIAL_HALIGN GTK_ALIGN_FILL
#define INITIAL_IMAGE IMAGE_NONE
#define INITIAL_IMAGE_INDEX 10
static GtkWidget *paper = NULL;
static GtkAlign child_halign = INITIAL_HALIGN;
static int test_image = INITIAL_IMAGE;
static int test_image_index = INITIAL_IMAGE_INDEX;
static void
populate_spread_table_wrappy (GtkSpreadTable *spread_table)
{
GList *children, *l;
GtkWidget *widget, *frame;
gint i;
const gchar *strings[] = {
"These are", "some wrappy label", "texts", "of various", "lengths.",
"They should always be", "shown", "consecutively. Except it's",
"hard to say", "where exactly the", "label", "will wrap", "and where exactly",
"the actual", "container", "will wrap.", "This label is really really really long !",
"Let's add some more", "labels to the",
"mix. Just to", "make sure we", "got something to work", "with here."
};
/* Remove all children first */
children = gtk_container_get_children (GTK_CONTAINER (paper));
for (l = children; l; l = l->next)
{
GtkWidget *child = l->data;
gtk_container_remove (GTK_CONTAINER (paper), child);
}
g_list_free (children);
for (i = 0; i < G_N_ELEMENTS (strings); i++)
{
widget = gtk_label_new (strings[i]);
frame = gtk_frame_new (NULL);
gtk_widget_show (widget);
gtk_widget_show (frame);
gtk_container_add (GTK_CONTAINER (frame), widget);
gtk_label_set_line_wrap (GTK_LABEL (widget), TRUE);
gtk_label_set_line_wrap_mode (GTK_LABEL (widget), PANGO_WRAP_WORD);
gtk_label_set_width_chars (GTK_LABEL (widget), 10);
gtk_widget_set_halign (frame, child_halign);
gtk_spread_table_insert_child (GTK_SPREAD_TABLE (spread_table), frame, -1);
}
/* Insert an image into the mix */
if (test_image)
{
widget = gtk_image_new_from_file ("apple-red.png");
switch (test_image)
{
case IMAGE_SMALL:
gtk_widget_set_size_request (widget, 100, 100);
break;
case IMAGE_LARGE:
gtk_widget_set_size_request (widget, 150, 200);
break;
case IMAGE_HUGE:
gtk_widget_set_size_request (widget, 200, 300);
break;
default:
break;
}
frame = gtk_frame_new (NULL);
gtk_widget_show (widget);
gtk_widget_show (frame);
gtk_container_add (GTK_CONTAINER (frame), widget);
gtk_spread_table_insert_child (GTK_SPREAD_TABLE (spread_table), frame, test_image_index);
}
}
static void
orientation_changed (GtkComboBox *box,
GtkSpreadTable *paper)
{
GtkOrientation orientation = gtk_combo_box_get_active (box);
gtk_orientable_set_orientation (GTK_ORIENTABLE (paper), orientation);
}
static void
lines_changed (GtkSpinButton *button,
gpointer data)
{
gint lines = gtk_spin_button_get_value_as_int (button);
gtk_spread_table_set_lines (GTK_SPREAD_TABLE (paper), lines);
}
static void
spacing_changed (GtkSpinButton *button,
gpointer data)
{
GtkOrientation orientation = GPOINTER_TO_INT (data);
gint state = gtk_spin_button_get_value_as_int (button);
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_spread_table_set_horizontal_spacing (GTK_SPREAD_TABLE (paper), state);
else
gtk_spread_table_set_vertical_spacing (GTK_SPREAD_TABLE (paper), state);
}
static void
halign_changed (GtkComboBox *box,
GtkSpreadTable *paper)
{
child_halign = gtk_combo_box_get_active (box);
populate_spread_table_wrappy (GTK_SPREAD_TABLE (paper));
}
static void
test_image_changed (GtkComboBox *box,
GtkSpreadTable *paper)
{
test_image = gtk_combo_box_get_active (box);
populate_spread_table_wrappy (GTK_SPREAD_TABLE (paper));
}
static void
test_image_index_changed (GtkSpinButton *button,
gpointer data)
{
test_image_index = gtk_spin_button_get_value_as_int (button);
populate_spread_table_wrappy (GTK_SPREAD_TABLE (paper));
}
static GtkWidget *
create_window (void)
{
GtkWidget *window;
GtkWidget *hbox, *vbox, *widget;
GtkWidget *swindow, *frame, *expander;
GtkWidget *paper_cntl, *items_cntl;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
hbox = gtk_hbox_new (FALSE, 2);
vbox = gtk_vbox_new (FALSE, 6);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
gtk_widget_show (vbox);
gtk_widget_show (hbox);
gtk_container_add (GTK_CONTAINER (window), hbox);
gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
frame = gtk_frame_new ("SpreadTable");
gtk_widget_show (frame);
gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
swindow = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_widget_show (swindow);
gtk_container_add (GTK_CONTAINER (frame), swindow);
paper = gtk_spread_table_new (GTK_ORIENTATION_VERTICAL, INITIAL_LINES);
gtk_spread_table_set_vertical_spacing (GTK_SPREAD_TABLE (paper), INITIAL_VSPACING);
gtk_spread_table_set_horizontal_spacing (GTK_SPREAD_TABLE (paper), INITIAL_HSPACING);
gtk_widget_show (paper);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (swindow), paper);
/* Add SpreadTable test control frame */
expander = gtk_expander_new ("SpreadTable controls");
gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
paper_cntl = gtk_vbox_new (FALSE, 2);
gtk_widget_show (paper_cntl);
gtk_widget_show (expander);
gtk_container_add (GTK_CONTAINER (expander), paper_cntl);
gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 0);
/* Add Orientation control */
widget = gtk_combo_box_new_text ();
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Horizontal");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Vertical");
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 1);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the spread_table orientation");
gtk_box_pack_start (GTK_BOX (paper_cntl), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (orientation_changed), paper);
/* Add horizontal/vertical spacing controls */
hbox = gtk_hbox_new (FALSE, 2);
gtk_widget_show (hbox);
widget = gtk_label_new ("H Spacing");
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
widget = gtk_spin_button_new_with_range (0, 30, 1);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), INITIAL_HSPACING);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the horizontal spacing between children");
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (spacing_changed), GINT_TO_POINTER (GTK_ORIENTATION_HORIZONTAL));
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (spacing_changed), GINT_TO_POINTER (GTK_ORIENTATION_HORIZONTAL));
gtk_box_pack_start (GTK_BOX (paper_cntl), hbox, FALSE, FALSE, 0);
hbox = gtk_hbox_new (FALSE, 2);
gtk_widget_show (hbox);
widget = gtk_label_new ("V Spacing");
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
widget = gtk_spin_button_new_with_range (0, 30, 1);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), INITIAL_VSPACING);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the vertical spacing between children");
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (spacing_changed), GINT_TO_POINTER (GTK_ORIENTATION_VERTICAL));
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (spacing_changed), GINT_TO_POINTER (GTK_ORIENTATION_VERTICAL));
gtk_box_pack_start (GTK_BOX (paper_cntl), hbox, FALSE, FALSE, 0);
/* Add lines controls */
hbox = gtk_hbox_new (FALSE, 2);
gtk_widget_show (hbox);
widget = gtk_label_new ("Lines");
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
widget = gtk_spin_button_new_with_range (1, 30, 1);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), INITIAL_LINES);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the horizontal spacing between children");
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (lines_changed), NULL);
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (lines_changed), NULL);
gtk_box_pack_start (GTK_BOX (paper_cntl), hbox, FALSE, FALSE, 0);
/* Add test items control frame */
expander = gtk_expander_new ("Test item controls");
gtk_expander_set_expanded (GTK_EXPANDER (expander), TRUE);
items_cntl = gtk_vbox_new (FALSE, 2);
gtk_widget_show (items_cntl);
gtk_widget_show (expander);
gtk_container_add (GTK_CONTAINER (expander), items_cntl);
gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 0);
/* Add child halign control */
widget = gtk_combo_box_new_text ();
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Fill");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Start");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "End");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Center");
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), INITIAL_HALIGN);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the children's halign property");
gtk_box_pack_start (GTK_BOX (items_cntl), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (halign_changed), paper);
/* Add image control */
widget = gtk_combo_box_new_text ();
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "None");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Small");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Large");
gtk_combo_box_append_text (GTK_COMBO_BOX (widget), "Huge");
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), INITIAL_IMAGE);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Use an image to test the container");
gtk_box_pack_start (GTK_BOX (items_cntl), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (test_image_changed), paper);
/* Add horizontal/vertical spacing controls */
hbox = gtk_hbox_new (FALSE, 2);
gtk_widget_show (hbox);
widget = gtk_label_new ("Image index");
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
widget = gtk_spin_button_new_with_range (0, 25, 1);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), INITIAL_IMAGE_INDEX);
gtk_widget_show (widget);
gtk_widget_set_tooltip_text (widget, "Set the child list index for the optional test image");
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (items_cntl), hbox, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (test_image_index_changed), GINT_TO_POINTER (GTK_ORIENTATION_HORIZONTAL));
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (test_image_index_changed), GINT_TO_POINTER (GTK_ORIENTATION_HORIZONTAL));
populate_spread_table_wrappy (GTK_SPREAD_TABLE (paper));
gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
return window;
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = create_window ();
g_signal_connect (window, "delete-event",
G_CALLBACK (gtk_main_quit), window);
gtk_widget_show (window);
gtk_main ();
return 0;
}