Added GtkCellAreaClass->apply_attributes()

Made gtk_cell_area_apply_attributes use a vfunc & signal so that
subclasses might do specialized things picking up attributes from the
model possibly for embedding widgets, and as specially for GtkTreeMenu
to connect to the signal and synchronize the menu sensitivity states
in a more convenient way than going through the GtkCellLayoutDataFunc.
This commit is contained in:
Tristan Van Berkom 2010-11-23 23:37:58 +09:00
parent d56babefb4
commit e53dc3479f
3 changed files with 119 additions and 71 deletions

View File

@ -56,6 +56,11 @@ static gint gtk_cell_area_real_event (GtkCellArea
GdkEvent *event, GdkEvent *event,
const GdkRectangle *cell_area, const GdkRectangle *cell_area,
GtkCellRendererState flags); GtkCellRendererState flags);
static void gtk_cell_area_real_apply_attributes (GtkCellArea *area,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gboolean is_expander,
gboolean is_expanded);
static void gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea *area, static void gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea *area,
GtkCellAreaContext *context, GtkCellAreaContext *context,
GtkWidget *widget, GtkWidget *widget,
@ -190,6 +195,7 @@ enum {
}; };
enum { enum {
SIGNAL_APPLY_ATTRIBUTES,
SIGNAL_ADD_EDITABLE, SIGNAL_ADD_EDITABLE,
SIGNAL_REMOVE_EDITABLE, SIGNAL_REMOVE_EDITABLE,
SIGNAL_FOCUS_CHANGED, SIGNAL_FOCUS_CHANGED,
@ -248,11 +254,12 @@ gtk_cell_area_class_init (GtkCellAreaClass *class)
object_class->set_property = gtk_cell_area_set_property; object_class->set_property = gtk_cell_area_set_property;
/* general */ /* general */
class->add = NULL; class->add = NULL;
class->remove = NULL; class->remove = NULL;
class->forall = NULL; class->forall = NULL;
class->event = gtk_cell_area_real_event; class->event = gtk_cell_area_real_event;
class->render = NULL; class->render = NULL;
class->apply_attributes = gtk_cell_area_real_apply_attributes;
/* geometry */ /* geometry */
class->create_context = NULL; class->create_context = NULL;
@ -269,6 +276,29 @@ gtk_cell_area_class_init (GtkCellAreaClass *class)
/* Signals */ /* Signals */
/**
* GtkCellArea::apply-attributes:
* @area: the #GtkCellArea to apply the attributes to
* @model: the #GtkTreeModel to apply the attributes from
* @iter: the #GtkTreeIter indicating which row to apply the attributes of
* @is_expander: whether the view shows children for this row
* @is_expanded: whether the view is currently showing the children of this row
*
* This signal is emitted whenever applying attributes to @area from @model
*/
cell_area_signals[SIGNAL_APPLY_ATTRIBUTES] =
g_signal_new (I_("apply-attributes"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkCellAreaClass, apply_attributes),
NULL, NULL,
_gtk_marshal_VOID__OBJECT_BOXED_BOOLEAN_BOOLEAN,
G_TYPE_NONE, 4,
GTK_TYPE_TREE_MODEL,
GTK_TYPE_TREE_ITER,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN);
/** /**
* GtkCellArea::add-editable: * GtkCellArea::add-editable:
* @area: the #GtkCellArea where editing started * @area: the #GtkCellArea where editing started
@ -556,6 +586,82 @@ gtk_cell_area_real_event (GtkCellArea *area,
return FALSE; return FALSE;
} }
static void
apply_cell_attributes (GtkCellRenderer *renderer,
CellInfo *info,
AttributeData *data)
{
CellAttribute *attribute;
GSList *list;
GValue value = { 0, };
gboolean is_expander;
gboolean is_expanded;
g_object_freeze_notify (G_OBJECT (renderer));
/* Whether a row expands or is presently expanded can only be
* provided by the view (as these states can vary across views
* accessing the same model).
*/
g_object_get (renderer, "is-expander", &is_expander, NULL);
if (is_expander != data->is_expander)
g_object_set (renderer, "is-expander", data->is_expander, NULL);
g_object_get (renderer, "is-expanded", &is_expanded, NULL);
if (is_expanded != data->is_expanded)
g_object_set (renderer, "is-expanded", data->is_expanded, NULL);
/* Apply the attributes directly to the renderer */
for (list = info->attributes; list; list = list->next)
{
attribute = list->data;
gtk_tree_model_get_value (data->model, data->iter, attribute->column, &value);
g_object_set_property (G_OBJECT (renderer), attribute->attribute, &value);
g_value_unset (&value);
}
/* Call any GtkCellLayoutDataFunc that may have been set by the user
*/
if (info->func)
info->func (GTK_CELL_LAYOUT (data->area), renderer,
data->model, data->iter, info->data);
g_object_thaw_notify (G_OBJECT (renderer));
}
static void
gtk_cell_area_real_apply_attributes (GtkCellArea *area,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gboolean is_expander,
gboolean is_expanded)
{
GtkCellAreaPrivate *priv;
AttributeData data;
GtkTreePath *path;
priv = area->priv;
/* Feed in data needed to apply to every renderer */
data.area = area;
data.model = tree_model;
data.iter = iter;
data.is_expander = is_expander;
data.is_expanded = is_expanded;
/* Go over any cells that have attributes or custom GtkCellLayoutDataFuncs and
* apply the data from the treemodel */
g_hash_table_foreach (priv->cell_info, (GHFunc)apply_cell_attributes, &data);
/* Update the currently applied path */
g_free (priv->current_path);
path = gtk_tree_model_get_path (tree_model, iter);
priv->current_path = gtk_tree_path_to_string (path);
gtk_tree_path_free (path);
}
static void static void
gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea *area, gtk_cell_area_real_get_preferred_height_for_width (GtkCellArea *area,
GtkCellAreaContext *context, GtkCellAreaContext *context,
@ -1422,50 +1528,6 @@ gtk_cell_area_attribute_disconnect (GtkCellArea *area,
} }
} }
static void
apply_cell_attributes (GtkCellRenderer *renderer,
CellInfo *info,
AttributeData *data)
{
CellAttribute *attribute;
GSList *list;
GValue value = { 0, };
gboolean is_expander;
gboolean is_expanded;
g_object_freeze_notify (G_OBJECT (renderer));
/* Whether a row expands or is presently expanded can only be
* provided by the view (as these states can vary across views
* accessing the same model).
*/
g_object_get (renderer, "is-expander", &is_expander, NULL);
if (is_expander != data->is_expander)
g_object_set (renderer, "is-expander", data->is_expander, NULL);
g_object_get (renderer, "is-expanded", &is_expanded, NULL);
if (is_expanded != data->is_expanded)
g_object_set (renderer, "is-expanded", data->is_expanded, NULL);
/* Apply the attributes directly to the renderer */
for (list = info->attributes; list; list = list->next)
{
attribute = list->data;
gtk_tree_model_get_value (data->model, data->iter, attribute->column, &value);
g_object_set_property (G_OBJECT (renderer), attribute->attribute, &value);
g_value_unset (&value);
}
/* Call any GtkCellLayoutDataFunc that may have been set by the user
*/
if (info->func)
info->func (GTK_CELL_LAYOUT (data->area), renderer,
data->model, data->iter, info->data);
g_object_thaw_notify (G_OBJECT (renderer));
}
/** /**
* gtk_cell_area_apply_attributes * gtk_cell_area_apply_attributes
* @area: a #GtkCellArea * @area: a #GtkCellArea
@ -1485,32 +1547,12 @@ gtk_cell_area_apply_attributes (GtkCellArea *area,
gboolean is_expander, gboolean is_expander,
gboolean is_expanded) gboolean is_expanded)
{ {
GtkCellAreaPrivate *priv;
AttributeData data;
GtkTreePath *path;
g_return_if_fail (GTK_IS_CELL_AREA (area)); g_return_if_fail (GTK_IS_CELL_AREA (area));
g_return_if_fail (GTK_IS_TREE_MODEL (tree_model)); g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
g_return_if_fail (iter != NULL); g_return_if_fail (iter != NULL);
priv = area->priv; g_signal_emit (area, cell_area_signals[SIGNAL_APPLY_ATTRIBUTES], 0,
tree_model, iter, is_expander, is_expanded);
/* Feed in data needed to apply to every renderer */
data.area = area;
data.model = tree_model;
data.iter = iter;
data.is_expander = is_expander;
data.is_expanded = is_expanded;
/* Go over any cells that have attributes or custom GtkCellLayoutDataFuncs and
* apply the data from the treemodel */
g_hash_table_foreach (priv->cell_info, (GHFunc)apply_cell_attributes, &data);
/* Update the currently applied path */
g_free (priv->current_path);
path = gtk_tree_model_get_path (tree_model, iter);
priv->current_path = gtk_tree_path_to_string (path);
gtk_tree_path_free (path);
} }
/** /**

View File

@ -99,6 +99,11 @@ struct _GtkCellAreaClass
const GdkRectangle *cell_area, const GdkRectangle *cell_area,
GtkCellRendererState flags, GtkCellRendererState flags,
gboolean paint_focus); gboolean paint_focus);
void (* apply_attributes) (GtkCellArea *area,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gboolean is_expander,
gboolean is_expanded);
/* Geometry */ /* Geometry */
GtkCellAreaContext *(* create_context) (GtkCellArea *area); GtkCellAreaContext *(* create_context) (GtkCellArea *area);

View File

@ -79,6 +79,7 @@ VOID:OBJECT
VOID:OBJECT,BOOLEAN VOID:OBJECT,BOOLEAN
VOID:OBJECT,BOXED,BOXED VOID:OBJECT,BOXED,BOXED
VOID:OBJECT,BOXED,UINT,UINT VOID:OBJECT,BOXED,UINT,UINT
VOID:OBJECT,BOXED,BOOLEAN,BOOLEAN
VOID:OBJECT,INT VOID:OBJECT,INT
VOID:OBJECT,INT,OBJECT VOID:OBJECT,INT,OBJECT
VOID:OBJECT,INT,INT VOID:OBJECT,INT,INT