我正在开发某些应用程序,在其中我们通过调用“ gtk_render_option”,“ gtk_render_check”等来绘制“ RadioButton”,“ CheckButton”等小部件。我们的应用程序可以在gtk 3.18上正常运行,但是在gtk 3.22上,窗口小部件无法正确绘制。我已经完成了3.20中主题的更改,并尝试对应用程序进行一些更改,但是我无法完全理解需要做的事情。
我创建了一个小示例,其中使用gtk_render_option API绘制了“ RadioButton”。这在gtk-3.18中对我来说很好用,但是在gtk 3.22中引起了问题。我附上图像和代码以供参考。如果有人可以在“如何在gtk 3.22上进行此小型演示”方面指导我,那将是很好的。我自己应该可以照顾其余的人。我可以提供所需的任何进一步信息。
注意:我已经在gtk_style_context_add_class API中尝试了“ radio”和“ radiobutton”。两者都不适合我。
使用gtk 3.18输出
使用gtk 3.22输出
代码
#include <string.h>
#include <gtk/gtk.h>
static gboolean
draw_cb_options (GtkWidget *widget, cairo_t *cr)
{
GtkStyleContext *context;
context = gtk_widget_get_style_context (widget);
gtk_style_context_save (context);
gtk_style_context_add_class (context, "radio");
gtk_style_context_set_state (context, 0);
gtk_render_option (context, cr, 10, 50, 40, 40);
gtk_style_context_set_state (context, GTK_STATE_FLAG_CHECKED);
gtk_render_option (context, cr, 70, 50, 40, 40);
gtk_style_context_set_state (context, GTK_STATE_FLAG_INCONSISTENT);
gtk_render_option (context, cr, 120, 50, 40, 40);
gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
gtk_render_option (context, cr, 170, 50, 40, 40);
gtk_style_context_restore (context);
return TRUE;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkStyleContext *context;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "draw", G_CALLBACK (draw_cb_options), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
答案 0 :(得分:1)
GtkDrawingArea用于在调用:: draw处理程序之前隐式渲染主题背景。这已不再是这种情况。如果您依赖具有主题背景的背景,请从:: draw处理程序中调用
gtk_render_background()
。
即使在draw函数中调用gtk_render_background
也没有什么区别。下面的示例基于gtk3-demo的foreign draw,我尽了最大的努力简化了它,但仍然需要花很多时间才能完成所需的工作。
#include <gtk/gtk.h>
#include <string.h>
static GtkStyleContext *
get_style (GtkStyleContext *parent,
const char *selector)
{
GtkWidgetPath *path;
GtkStyleContext *context;
if (parent)
path = gtk_widget_path_copy (gtk_style_context_get_path (parent));
else
path = gtk_widget_path_new ();
gtk_widget_path_append_type (path, G_TYPE_NONE);
gtk_widget_path_iter_set_object_name (path, -1, selector);
context = gtk_style_context_new ();
gtk_style_context_set_path (context, path);
gtk_style_context_set_parent (context, parent);
/* Unfortunately, we have to explicitly set the state again here for it to take effect */
gtk_style_context_set_state (context, gtk_widget_path_iter_get_state (path, -1));
gtk_widget_path_unref (path);
return context;
}
static void
draw_style_common (GtkStyleContext *context,
cairo_t *cr,
gint x,
gint y,
gint width,
gint height,
gint *contents_x,
gint *contents_y,
gint *contents_width,
gint *contents_height)
{
GtkBorder margin, border, padding;
int min_width, min_height;
gtk_style_context_get_margin (context, gtk_style_context_get_state (context), &margin);
gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
gtk_style_context_get (context, gtk_style_context_get_state (context),
"min-width", &min_width,
"min-height", &min_height,
NULL);
x += margin.left;
y += margin.top;
width -= margin.left + margin.right;
height -= margin.top + margin.bottom;
width = MAX (width, min_width);
height = MAX (height, min_height);
gtk_render_background (context, cr, x, y, width, height);
gtk_render_frame (context, cr, x, y, width, height);
if (contents_x)
*contents_x = x + border.left + padding.left;
if (contents_y)
*contents_y = y + border.top + padding.top;
if (contents_width)
*contents_width = width - border.left - border.right - padding.left - padding.right;
if (contents_height)
*contents_height = height - border.top - border.bottom - padding.top - padding.bottom;
}
static void
query_size (GtkStyleContext *context,
gint *width,
gint *height)
{
GtkBorder margin, border, padding;
int min_width, min_height;
gtk_style_context_get_margin (context, gtk_style_context_get_state (context), &margin);
gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
gtk_style_context_get (context, gtk_style_context_get_state (context),
"min-width", &min_width,
"min-height", &min_height,
NULL);
min_width += margin.left + margin.right + border.left + border.right + padding.left + padding.right;
min_height += margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
*width = MAX (*width, min_width);
*height = MAX (*height, min_height);
}
static void
draw_radio (GtkWidget *widget,
cairo_t *cr,
gint x,
gint y,
GtkStateFlags state)
{
GtkStyleContext *button_context, *check_context;
gint contents_x, contents_y, contents_width, contents_height;
gint width = 0, height = 0;
/* This information is taken from the GtkRadioButton docs, see "CSS nodes" */
button_context = get_style (NULL, "radiobutton");
check_context = get_style (button_context, "radio");
gtk_style_context_set_state (check_context, state);
query_size (button_context, &width, &height);
query_size (check_context, &width, &height);
draw_style_common (button_context, cr, x, y, width, height, NULL, NULL, NULL, NULL);
draw_style_common (check_context, cr, x, y, width, height,
&contents_x, &contents_y, &contents_width, &contents_height);
gtk_render_option (check_context, cr, contents_x, contents_y, contents_width, contents_height);
g_object_unref (check_context);
g_object_unref (button_context);
}
static gboolean
draw_cb (GtkWidget *widget,
cairo_t *cr)
{
draw_radio (widget, cr, 10, 10, GTK_STATE_FLAG_NORMAL);
draw_radio (widget, cr, 50, 50, GTK_STATE_FLAG_CHECKED);
draw_radio (widget, cr, 100, 100, GTK_STATE_FLAG_INCONSISTENT);
draw_radio (widget, cr, 150, 150, GTK_STATE_FLAG_INSENSITIVE);
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *box, *window, *da;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_add (GTK_CONTAINER (window), box);
g_signal_connect (box, "draw", G_CALLBACK (draw_cb), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}