Added tests to reflect proper treatment of background area.

CellAreaScaffold now also reflects how cell_area should be
passed to gtk_cell_area_activate() and gtk_cell_area_event()
and how the background area for gtk_cell_area_renderer() should
be created.
This commit is contained in:
Tristan Van Berkom 2010-11-12 21:55:28 +09:00
parent 47d55c4a4b
commit 51b75ef44b
4 changed files with 368 additions and 30 deletions

View File

@ -2359,7 +2359,7 @@ gtk_cell_area_set_cell_margin_left (GtkCellArea *area,
{
priv->cell_border.left = margin;
g_object_notify (G_OBJECT (area), "margin-left");
g_object_notify (G_OBJECT (area), "cell-margin-left");
}
}
@ -2385,7 +2385,7 @@ gtk_cell_area_set_cell_margin_right (GtkCellArea *area,
{
priv->cell_border.right = margin;
g_object_notify (G_OBJECT (area), "margin-right");
g_object_notify (G_OBJECT (area), "cell-margin-right");
}
}
@ -2411,7 +2411,7 @@ gtk_cell_area_set_cell_margin_top (GtkCellArea *area,
{
priv->cell_border.top = margin;
g_object_notify (G_OBJECT (area), "margin-top");
g_object_notify (G_OBJECT (area), "cell-margin-top");
}
}
@ -2437,7 +2437,7 @@ gtk_cell_area_set_cell_margin_bottom (GtkCellArea *area,
{
priv->cell_border.bottom = margin;
g_object_notify (G_OBJECT (area), "margin-bottom");
g_object_notify (G_OBJECT (area), "cell-margin-bottom");
}
}

View File

@ -152,6 +152,10 @@ struct _CellAreaScaffoldPrivate {
GdkRectangle edit_rect;
gulong editing_started_id;
gulong remove_editable_id;
gint row_spacing;
gint indent;
};
enum {
@ -166,8 +170,6 @@ enum {
static guint scaffold_signals[N_SIGNALS] = { 0 };
#define ROW_SPACING 2
#define DIRECTION_STR(dir) \
((dir) == GTK_DIR_TAB_FORWARD ? "tab forward" : \
(dir) == GTK_DIR_TAB_BACKWARD ? "tab backward" : \
@ -213,8 +215,6 @@ cell_area_scaffold_init (CellAreaScaffold *scaffold)
priv->remove_editable_id =
g_signal_connect (priv->area, "remove-editable",
G_CALLBACK (remove_editable_cb), scaffold);
}
static void
@ -429,6 +429,7 @@ cell_area_scaffold_draw (GtkWidget *widget,
GtkOrientation orientation;
GtkTreeIter iter;
gboolean valid;
GdkRectangle background_area;
GdkRectangle render_area;
GtkAllocation allocation;
gint i = 0;
@ -448,6 +449,19 @@ cell_area_scaffold_draw (GtkWidget *widget,
render_area.width = allocation.width;
render_area.height = allocation.height;
background_area = render_area;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
render_area.x = priv->indent;
render_area.width -= priv->indent;
}
else
{
render_area.y = priv->indent;
render_area.height -= priv->indent;
}
valid = gtk_tree_model_get_iter_first (priv->model, &iter);
while (valid)
{
@ -460,27 +474,65 @@ cell_area_scaffold_draw (GtkWidget *widget,
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
render_area.height = data->size;
render_area.height = data->size;
background_area.height = render_area.height;
background_area.y = render_area.y;
if (i == 0)
{
background_area.height += priv->row_spacing / 2;
background_area.height += priv->row_spacing % 2;
}
else if (i == priv->row_data->len - 1)
{
background_area.y -= priv->row_spacing / 2;
background_area.height += priv->row_spacing / 2;
}
else
{
background_area.y -= priv->row_spacing / 2;
background_area.height += priv->row_spacing;
}
}
else
else /* GTK_ORIENTATION_VERTICAL */
{
render_area.width = data->size;
render_area.width = data->size;
background_area.width = render_area.height;
background_area.x = render_area.x;
if (i == 0)
{
background_area.width += priv->row_spacing / 2;
background_area.width += priv->row_spacing % 2;
}
else if (i == priv->row_data->len - 1)
{
background_area.x -= priv->row_spacing / 2;
background_area.width += priv->row_spacing / 2;
}
else
{
background_area.x -= priv->row_spacing / 2;
background_area.width += priv->row_spacing;
}
}
gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
gtk_cell_area_render (priv->area, priv->iter, widget, cr,
&render_area, &render_area, flags,
&background_area, &render_area, flags,
(have_focus && i == priv->focus_row));
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
render_area.y += data->size;
render_area.y += ROW_SPACING;
render_area.y += priv->row_spacing;
}
else
{
render_area.x += data->size;
render_area.x += ROW_SPACING;
render_area.x += priv->row_spacing;
}
i++;
@ -627,6 +679,9 @@ cell_area_scaffold_get_preferred_width (GtkWidget *widget,
request_all_base (scaffold);
gtk_cell_area_iter_get_preferred_width (priv->iter, minimum_size, natural_size);
*minimum_size += priv->indent;
*natural_size += priv->indent;
}
else
{
@ -666,7 +721,7 @@ cell_area_scaffold_get_preferred_height_for_width (GtkWidget *widget,
memset (request_array->data, 0x0, n_rows * sizeof (RowData));
/* Gather each contextual size into the request array */
get_row_sizes (scaffold, request_array, for_size);
get_row_sizes (scaffold, request_array, for_size - priv->indent);
/* Sum up the size and add some row spacing */
for (i = 0; i < n_rows; i++)
@ -676,7 +731,7 @@ cell_area_scaffold_get_preferred_height_for_width (GtkWidget *widget,
full_size += data->size;
}
full_size += MAX (0, n_rows -1) * ROW_SPACING;
full_size += MAX (0, n_rows -1) * priv->row_spacing;
g_array_free (request_array, TRUE);
@ -708,6 +763,9 @@ cell_area_scaffold_get_preferred_height (GtkWidget *widget,
request_all_base (scaffold);
gtk_cell_area_iter_get_preferred_height (priv->iter, minimum_size, natural_size);
*minimum_size += priv->indent;
*natural_size += priv->indent;
}
else
{
@ -747,7 +805,7 @@ cell_area_scaffold_get_preferred_width_for_height (GtkWidget *widget,
memset (request_array->data, 0x0, n_rows * sizeof (RowData));
/* Gather each contextual size into the request array */
get_row_sizes (scaffold, request_array, for_size);
get_row_sizes (scaffold, request_array, for_size - priv->indent);
/* Sum up the size and add some row spacing */
for (i = 0; i < n_rows; i++)
@ -757,7 +815,7 @@ cell_area_scaffold_get_preferred_width_for_height (GtkWidget *widget,
full_size += data->size;
}
full_size += MAX (0, n_rows -1) * ROW_SPACING;
full_size += MAX (0, n_rows -1) * priv->row_spacing;
g_array_free (request_array, TRUE);
@ -934,6 +992,17 @@ cell_area_scaffold_button_press (GtkWidget *widget,
event_area.width = allocation.width;
event_area.height = allocation.height;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
event_area.x = priv->indent;
event_area.width -= priv->indent;
}
else
{
event_area.y = priv->indent;
event_area.height -= priv->indent;
}
valid = gtk_tree_model_get_iter_first (priv->model, &iter);
while (valid)
{
@ -954,7 +1023,7 @@ cell_area_scaffold_button_press (GtkWidget *widget,
}
event_area.y += data->size;
event_area.y += ROW_SPACING;
event_area.y += priv->row_spacing;
}
else
{
@ -971,7 +1040,7 @@ cell_area_scaffold_button_press (GtkWidget *widget,
}
event_area.x += data->size;
event_area.x += ROW_SPACING;
event_area.x += priv->row_spacing;
}
i++;
@ -1053,6 +1122,17 @@ cell_area_scaffold_activate (CellAreaScaffold *scaffold)
cell_area.width = allocation.width;
cell_area.height = allocation.height;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
cell_area.x = priv->indent;
cell_area.width -= priv->indent;
}
else
{
cell_area.y = priv->indent;
cell_area.height -= priv->indent;
}
valid = gtk_tree_model_get_iter_first (priv->model, &iter);
while (valid)
{
@ -1072,9 +1152,9 @@ cell_area_scaffold_activate (CellAreaScaffold *scaffold)
}
if (orientation == GTK_ORIENTATION_HORIZONTAL)
cell_area.y += data->size + ROW_SPACING;
cell_area.y += data->size + priv->row_spacing;
else
cell_area.x += data->size + ROW_SPACING;
cell_area.x += data->size + priv->row_spacing;
i++;
valid = gtk_tree_model_iter_next (priv->model, &iter);
@ -1298,3 +1378,64 @@ cell_area_scaffold_get_model (CellAreaScaffold *scaffold)
return priv->model;
}
void
cell_area_scaffold_set_row_spacing (CellAreaScaffold *scaffold,
gint spacing)
{
CellAreaScaffoldPrivate *priv;
g_return_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold));
priv = scaffold->priv;
if (priv->row_spacing != spacing)
{
priv->row_spacing = spacing;
gtk_widget_queue_resize (GTK_WIDGET (scaffold));
}
}
gint
cell_area_scaffold_get_row_spacing (CellAreaScaffold *scaffold)
{
CellAreaScaffoldPrivate *priv;
g_return_val_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold), 0);
priv = scaffold->priv;
return priv->row_spacing;
}
void
cell_area_scaffold_set_indentation (CellAreaScaffold *scaffold,
gint indent)
{
CellAreaScaffoldPrivate *priv;
g_return_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold));
priv = scaffold->priv;
if (priv->indent != indent)
{
priv->indent = indent;
gtk_widget_queue_resize (GTK_WIDGET (scaffold));
}
}
gint
cell_area_scaffold_get_indentation (CellAreaScaffold *scaffold)
{
CellAreaScaffoldPrivate *priv;
g_return_val_if_fail (IS_CELL_AREA_SCAFFOLD (scaffold), 0);
priv = scaffold->priv;
return priv->indent;
}

View File

@ -56,13 +56,23 @@ struct _CellAreaScaffoldClass
};
GType cell_area_scaffold_get_type (void) G_GNUC_CONST;
GtkWidget *cell_area_scaffold_new (void);
GType cell_area_scaffold_get_type (void) G_GNUC_CONST;
GtkWidget *cell_area_scaffold_new (void);
GtkCellArea *cell_area_scaffold_get_area (CellAreaScaffold *scaffold);
void cell_area_scaffold_set_model (CellAreaScaffold *scaffold,
GtkTreeModel *model);
GtkTreeModel *cell_area_scaffold_get_model (CellAreaScaffold *scaffold);
void cell_area_scaffold_set_row_spacing (CellAreaScaffold *scaffold,
gint spacing);
gint cell_area_scaffold_get_row_spacing (CellAreaScaffold *scaffold);
void cell_area_scaffold_set_indentation (CellAreaScaffold *scaffold,
gint indent);
gint cell_area_scaffold_get_indentation (CellAreaScaffold *scaffold);
GtkCellArea *cell_area_scaffold_get_area (CellAreaScaffold *scaffold);
void cell_area_scaffold_set_model (CellAreaScaffold *scaffold,
GtkTreeModel *model);
GtkTreeModel *cell_area_scaffold_get_model (CellAreaScaffold *scaffold);
G_END_DECLS

View File

@ -168,6 +168,8 @@ simple_cell_area (void)
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "CellArea expand and alignments");
scaffold = simple_scaffold ();
hbox = gtk_hbox_new (FALSE, 4);
@ -325,7 +327,7 @@ cell_edited (GtkCellRendererToggle *cell_renderer,
}
static GtkWidget *
focus_scaffold (void)
focus_scaffold (gboolean color_bg)
{
GtkTreeModel *model;
GtkWidget *scaffold;
@ -346,6 +348,9 @@ focus_scaffold (void)
gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area), renderer, TRUE, FALSE);
gtk_cell_area_attribute_connect (area, renderer, "text", FOCUS_COLUMN_NAME);
if (color_bg)
g_object_set (G_OBJECT (renderer), "cell-background", "red", NULL);
g_signal_connect (G_OBJECT (renderer), "edited",
G_CALLBACK (cell_edited), scaffold);
@ -354,6 +359,9 @@ focus_scaffold (void)
gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area), renderer, FALSE, TRUE);
gtk_cell_area_attribute_connect (area, renderer, "active", FOCUS_COLUMN_CHECK);
if (color_bg)
g_object_set (G_OBJECT (renderer), "cell-background", "green", NULL);
g_signal_connect (G_OBJECT (renderer), "toggled",
G_CALLBACK (cell_toggled), scaffold);
@ -362,9 +370,15 @@ focus_scaffold (void)
"wrap-mode", PANGO_WRAP_WORD,
"wrap-width", 150,
NULL);
if (color_bg)
g_object_set (G_OBJECT (renderer), "cell-background", "blue", NULL);
gtk_cell_area_box_pack_start (GTK_CELL_AREA_BOX (area), renderer, FALSE, TRUE);
gtk_cell_area_attribute_connect (area, renderer, "text", FOCUS_COLUMN_STATIC_TEXT);
gtk_cell_area_add_focus_sibling (area, focus_renderer, sibling_renderer);
return scaffold;
}
@ -394,7 +408,9 @@ focus_cell_area (void)
hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
scaffold = focus_scaffold ();
gtk_window_set_title (GTK_WINDOW (window), "Focus and editable cells");
scaffold = focus_scaffold (FALSE);
frame = gtk_frame_new (NULL);
gtk_widget_show (frame);
@ -422,6 +438,7 @@ focus_cell_area (void)
G_CALLBACK (orientation_changed), scaffold);
widget = gtk_check_button_new_with_label ("Focus Sibling");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
@ -434,6 +451,175 @@ focus_cell_area (void)
}
/*******************************************************
* Background Area *
*******************************************************/
static void
cell_spacing_changed (GtkSpinButton *spin_button,
CellAreaScaffold *scaffold)
{
GtkCellArea *area = cell_area_scaffold_get_area (scaffold);
gint value;
value = (gint)gtk_spin_button_get_value (spin_button);
gtk_cell_area_box_set_spacing (GTK_CELL_AREA_BOX (area), value);
}
static void
row_spacing_changed (GtkSpinButton *spin_button,
CellAreaScaffold *scaffold)
{
gint value;
value = (gint)gtk_spin_button_get_value (spin_button);
cell_area_scaffold_set_row_spacing (scaffold, value);
}
static void
cell_margins_changed (GtkSpinButton *spin_button,
CellAreaScaffold *scaffold)
{
GtkCellArea *area = cell_area_scaffold_get_area (scaffold);
gint value;
value = (gint)gtk_spin_button_get_value (spin_button);
gtk_cell_area_set_cell_margin_left (area, value);
gtk_cell_area_set_cell_margin_right (area, value);
gtk_cell_area_set_cell_margin_top (area, value);
gtk_cell_area_set_cell_margin_bottom (area, value);
gtk_widget_queue_resize (GTK_WIDGET (scaffold));
}
static void
indentation_changed (GtkSpinButton *spin_button,
CellAreaScaffold *scaffold)
{
gint value;
value = (gint)gtk_spin_button_get_value (spin_button);
cell_area_scaffold_set_indentation (scaffold, value);
}
static void
background_area (void)
{
GtkWidget *window, *widget, *label, *main_vbox;
GtkWidget *scaffold, *frame, *vbox, *hbox;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
hbox = gtk_hbox_new (FALSE, 4);
main_vbox = gtk_vbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_widget_show (main_vbox);
gtk_container_add (GTK_CONTAINER (window), main_vbox);
gtk_window_set_title (GTK_WINDOW (window), "Background Area");
label = gtk_label_new ("In this example, row spacing gets devided into the background area, "
"column spacing is added between each background area, indentation is "
"prepended space distributed to the background area, individual cell margins "
"are also distributed to the background area for every cell.");
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_label_set_width_chars (GTK_LABEL (label), 40);
gtk_widget_show (label);
gtk_box_pack_start (GTK_BOX (main_vbox), label, FALSE, FALSE, 0);
scaffold = focus_scaffold (TRUE);
frame = gtk_frame_new (NULL);
gtk_widget_show (frame);
gtk_widget_set_valign (frame, GTK_ALIGN_CENTER);
gtk_widget_set_halign (frame, GTK_ALIGN_FILL);
gtk_container_add (GTK_CONTAINER (frame), scaffold);
gtk_box_pack_end (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
/* Now add some controls */
vbox = gtk_vbox_new (FALSE, 4);
gtk_widget_show (vbox);
gtk_box_pack_end (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
widget = gtk_combo_box_text_new ();
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), "Horizontal");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (widget), "Vertical");
gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "changed",
G_CALLBACK (orientation_changed), scaffold);
widget = gtk_spin_button_new_with_range (0, 10, 1);
label = gtk_label_new ("Cell spacing");
hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_widget_show (label);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (cell_spacing_changed), scaffold);
widget = gtk_spin_button_new_with_range (0, 10, 1);
label = gtk_label_new ("Row spacing");
hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_widget_show (label);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (row_spacing_changed), scaffold);
widget = gtk_spin_button_new_with_range (0, 10, 1);
label = gtk_label_new ("Cell Margins");
hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_widget_show (label);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (cell_margins_changed), scaffold);
widget = gtk_spin_button_new_with_range (0, 30, 1);
label = gtk_label_new ("Intentation");
hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_widget_show (label);
gtk_widget_show (widget);
gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (widget), "value-changed",
G_CALLBACK (indentation_changed), scaffold);
gtk_widget_show (window);
}
int
main (int argc, char *argv[])
{
@ -441,6 +627,7 @@ main (int argc, char *argv[])
simple_cell_area ();
focus_cell_area ();
background_area ();
gtk_main ();