使用 GTK >=3.16 的组合框单元格布局渲染器的默认颜色
Using default color for combo box cell layout renderer with GTK >=3.16
我使用一个组合框,其项目是动态创建的,黑色组合框内的标题也是如此,可能会或可能不会出现,具体取决于当前的选择。它看起来像这样:
单元格布局渲染器的代码是这样的(只是为了概念,我下面的问题对细节不感兴趣):
void option_list_with_headlines
(G_GNUC_UNUSED GtkCellLayout *cell_layout,
GtkCellRenderer *action_option_combo_box_renderer,
GtkTreeModel *action_option_combo_box_model,
GtkTreeIter *action_option_combo_box_iter,
G_GNUC_UNUSED gpointer data) {
gchar *action_option_combo_item;
GdkRGBA normal_fg_color, normal_bg_color;
gboolean headline;
gtk_style_context_get_color (gtk_widget_get_style_context (action_option),
GTK_STATE_NORMAL, &normal_fg_color);
gtk_style_context_get_background_color (gtk_widget_get_style_context
(action_option), GTK_STATE_NORMAL, &normal_bg_color);
gtk_tree_model_get (action_option_combo_box_model,
action_option_combo_box_iter, ACTION_OPTION_COMBO_ITEM,
&action_option_combo_item, -1);
headline = g_regex_match_simple ("Add|Choose",
action_option_combo_item, G_REGEX_ANCHORED, 0);
g_object_set (action_option_combo_box_renderer,
"foreground-rgba", (headline) ? &((GdkRGBA) { 1.0, 1.0, 1.0, 1.0 }) :
&normal_fg_color, "background-rgba"
(g_str_has_prefix(action_option_combo_item, "Choose")) ?
&((GdkRGBA) { 0.31, 0.31, 0.79, 1.0 }) :
((g_str_has_prefix (action_option_combo_item, "Add")) ?
&((GdkRGBA) { 0.0, 0.0, 0.0, 1.0 }) : &normal_bg_color),
"sensitive", !headline, NULL);
// Cleanup
g_free (action_option_combo_item);
}
关于这个函数我的问题是:
从 Gtk >=3.16 开始,我不应该再使用 gtk_style_context_get_background_color
。但是我该怎么做才能在组合框项目列表中将颜色设置为默认颜色,就像我在上图中对 "Name" 和 "Prompt" 所做的那样?目前我使用 g_object_set
以及我用 gtk_style_context_get_background_color
和 GTK_STATE_NORMAL 收集的颜色作为参数。
事实证明解决方案非常简单:我根本不需要检索默认背景颜色;使用 g_object_set
:
时,将 background-rgba
的值设置为 NULL 就足够了
g_object_set (action_option_combo_box_renderer, "background-rgba", NULL, ...)
我不知道这是可能的;我假设 background-rgba 总是需要某种价值。
我使用一个组合框,其项目是动态创建的,黑色组合框内的标题也是如此,可能会或可能不会出现,具体取决于当前的选择。它看起来像这样:
单元格布局渲染器的代码是这样的(只是为了概念,我下面的问题对细节不感兴趣):
void option_list_with_headlines
(G_GNUC_UNUSED GtkCellLayout *cell_layout,
GtkCellRenderer *action_option_combo_box_renderer,
GtkTreeModel *action_option_combo_box_model,
GtkTreeIter *action_option_combo_box_iter,
G_GNUC_UNUSED gpointer data) {
gchar *action_option_combo_item;
GdkRGBA normal_fg_color, normal_bg_color;
gboolean headline;
gtk_style_context_get_color (gtk_widget_get_style_context (action_option),
GTK_STATE_NORMAL, &normal_fg_color);
gtk_style_context_get_background_color (gtk_widget_get_style_context
(action_option), GTK_STATE_NORMAL, &normal_bg_color);
gtk_tree_model_get (action_option_combo_box_model,
action_option_combo_box_iter, ACTION_OPTION_COMBO_ITEM,
&action_option_combo_item, -1);
headline = g_regex_match_simple ("Add|Choose",
action_option_combo_item, G_REGEX_ANCHORED, 0);
g_object_set (action_option_combo_box_renderer,
"foreground-rgba", (headline) ? &((GdkRGBA) { 1.0, 1.0, 1.0, 1.0 }) :
&normal_fg_color, "background-rgba"
(g_str_has_prefix(action_option_combo_item, "Choose")) ?
&((GdkRGBA) { 0.31, 0.31, 0.79, 1.0 }) :
((g_str_has_prefix (action_option_combo_item, "Add")) ?
&((GdkRGBA) { 0.0, 0.0, 0.0, 1.0 }) : &normal_bg_color),
"sensitive", !headline, NULL);
// Cleanup
g_free (action_option_combo_item);
}
关于这个函数我的问题是:
从 Gtk >=3.16 开始,我不应该再使用 gtk_style_context_get_background_color
。但是我该怎么做才能在组合框项目列表中将颜色设置为默认颜色,就像我在上图中对 "Name" 和 "Prompt" 所做的那样?目前我使用 g_object_set
以及我用 gtk_style_context_get_background_color
和 GTK_STATE_NORMAL 收集的颜色作为参数。
事实证明解决方案非常简单:我根本不需要检索默认背景颜色;使用 g_object_set
:
background-rgba
的值设置为 NULL 就足够了
g_object_set (action_option_combo_box_renderer, "background-rgba", NULL, ...)
我不知道这是可能的;我假设 background-rgba 总是需要某种价值。