Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将小型gtk3样本从gtk 3.18迁移到gtk 3.22_Gtk_Gtk3 - Fatal编程技术网

将小型gtk3样本从gtk 3.18迁移到gtk 3.22

将小型gtk3样本从gtk 3.18迁移到gtk 3.22,gtk,gtk3,Gtk,Gtk3,我正在开发一些应用程序,我们通过调用“gtk_render_option”、“gtk_render_check”等来绘制“RadioButton”、“CheckButton”等小部件。我们的应用程序在GTK3.18上运行良好,但在GTK3.22上,小部件绘制不正确。我已经经历了3.20中对主题所做的更改,并尝试在我的应用程序中进行一些更改,但我无法确切理解我需要做什么 我创建了一个小示例,在其中使用gtk_render_选项API绘制“RadioButton”。这在gtk-3.18中对我来说很好

我正在开发一些应用程序,我们通过调用“gtk_render_option”、“gtk_render_check”等来绘制“RadioButton”、“CheckButton”等小部件。我们的应用程序在GTK3.18上运行良好,但在GTK3.22上,小部件绘制不正确。我已经经历了3.20中对主题所做的更改,并尝试在我的应用程序中进行一些更改,但我无法确切理解我需要做什么

我创建了一个小示例,在其中使用gtk_render_选项API绘制“RadioButton”。这在gtk-3.18中对我来说很好,但在gtk 3.22中会导致问题。我附上图像和代码以供参考。如果这里有人能指导我“如何让这个小演示在gtk 3.22上工作”,那就太好了。我应该可以自己照顾剩下的。我可以提供所需的任何进一步信息

注意:我在gtk_style_context_add_类API中尝试了“单选”和“单选按钮”。这两个都不适合我

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;
}
#包括
#包括
静态gboolean
绘图选项(GtkWidget*小部件,cairo\u t*cr)
{
GtkStyleContext*上下文;
context=gtk_widget_get_style_context(widget);
gtk_样式_上下文_保存(上下文);
gtk_样式_上下文_添加_类(上下文,“收音机”);
gtk_样式_上下文_设置_状态(上下文,0);
gtk_渲染_选项(上下文、cr、10、50、40、40);
gtk_样式_上下文_设置_状态(上下文、gtk_状态_标志已选中);
gtk_渲染_选项(上下文、cr、70、50、40、40);
gtk_样式_上下文_设置_状态(上下文、gtk_状态_标志_不一致);
gtk_渲染_选项(上下文、cr、120、50、40、40);
gtk_样式_上下文_设置_状态(上下文,gtk_状态_标志不敏感);
gtk_渲染_选项(上下文、cr、170、50、40、40);
gtk_样式_上下文_还原(上下文);
返回TRUE;
}
int main(int argc,char*argv[])
{
GtkWidget*窗口;
GtkStyleContext*上下文;
gtk_init(&argc,&argv);
窗口=gtk_窗口_新建(gtk_窗口_顶层);
g_信号连接(窗口,“绘制”,g_回调(绘制cb_选项),空);
gtk_小部件_全部显示(窗口);
gtk_main();
返回0;
}
根据:

GtkDrawingArea用于在调用::draw处理程序之前隐式呈现主题背景。现在已经不是这样了。如果您依赖于提供主题背景,请从::draw处理程序调用
gtk_render_background()

甚至在draw函数中调用
gtk_render_background
,也没有什么不同。下面的示例是基于gtk3演示的,我尽了最大努力简化了它,但它仍然需要经过很多过程才能完成您所需要的

#包括
#包括
静态GtkStyleContext*
获取样式(GtkStyleContext*父级,
常量字符*选择器)
{
GtkWidgetPath*路径;
GtkStyleContext*上下文;
如果(家长)
路径=gtk_小部件_路径_复制(gtk_样式_上下文_获取_路径(父));
其他的
path=gtk_widget_path_new();
gtk_控件_路径_附加_类型(路径,G_类型_无);
gtk_小部件_路径_iter_设置_对象_名称(路径,-1,选择器);
context=gtk_style_context_new();
gtk_样式_上下文_设置_路径(上下文,路径);
gtk_样式_上下文_集合_父项(上下文,父项);
/*不幸的是,我们必须在这里再次显式设置状态,以使其生效*/
gtk_样式_上下文_设置_状态(上下文,gtk_控件_路径_iter_获取_状态(路径,-1));
gtk_widget_path_unref(路径);
返回上下文;
}
静态空隙
绘制样式公共(GtkStyleContext*context,
开罗,
金特x,
金蒂,
基特宽度,
基特高度,
gint*内容物,
金特*内容物,
gint*内容物与宽度,
基特*内容物(单位高度)
{
GtkBorder页边距、边框、填充;
最小宽度,最小高度;
gtk_样式_上下文_获取_边距(上下文、gtk_样式_上下文_获取_状态(上下文)和边距);
gtk_样式_上下文_获取_边框(上下文、gtk_样式_上下文_获取_状态(上下文)和边框);
gtk_样式_上下文_获取_填充(上下文、gtk_样式_上下文_获取_状态(上下文)和填充);
gtk_style_context_get(上下文,gtk_style_context_get_state(上下文),
“最小宽度”和“最小宽度”,
“最小高度”和“最小高度”,
无效);
x+=边距。左;
y+=页边距.top;
宽度-=margin.left+margin.right;
高度-=margin.top+margin.bottom;
宽度=最大(宽度,最小宽度);
高度=最大(高度,最小高度);
gtk_渲染_背景(上下文、cr、x、y、宽度、高度);
gtk_渲染_帧(上下文、cr、x、y、宽度、高度);
如果(内容)
*contents_x=x+border.left+padding.left;
如果(内容)
*contents_y=y+border.top+padding.top;
如果(内容和宽度)
*contents_width=width-border.left-border.right-padding.left-padding.right;
如果(内容和高度)
*contents_height=height-border.top-border.bottom-padding.top-padding.bottom;
}
静态空隙
查询大小(GtkStyleContext*context,
基特*宽度,
基特*高度)
{
GtkBorder页边距、边框、填充;
最小宽度,最小高度;
gtk_样式_上下文_获取_边距(上下文、gtk_样式_上下文_获取_状态(上下文)和边距);
gtk_样式_上下文_获取_边框(上下文、gtk_样式_上下文_获取_状态(上下文)和边框);
gtk_样式_上下文_获取_填充(上下文、gtk_样式_上下文_获取_状态(上下文)和填充);
gtk_style_context_get(上下文,gtk_style_context_get_state(上下文),
“最小宽度”和“最小宽度”,
“最小高度”和“最小高度”,
无效);
最小宽度+=margin.left+margin.right+border.left+border.right+padding.left+padding.right;
最小高度+=margin.top+margin.bo
#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;
}