如何使用 GTK/C 为笔记本选项卡创建关闭按钮?
How do I create a close button for notebook tab with GTK/C?
我正在寻找一种方法来为笔记本标签创建一个小的关闭按钮。
下面是为 GTK 2.24、GTK 3 和 4 创建一个大关闭按钮的常用方法:
GtkWidget *head, *content, *image, *btn, *label = gtk_label_new ("Title");
// create empty boxes
#if GTK_CHECK_VERSION (3,0,0)
head = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
#else
head = gtk_hbox_new (FALSE, 0);
content = gtk_vbox_new (FALSE, 0);
#endif
// add text to box
#if GTK_CHECK_VERSION (4,0,0)
gtk_box_append (GTK_BOX (head), label);
#else
gtk_box_pack_start (GTK_BOX (head), label, FALSE, FALSE, 0);
#endif
// create close button
#if GTK_CHECK_VERSION (4,0,0)
btn = gtk_button_new_from_icon_name ("gtk-close");
gtk_button_set_has_frame (GTK_BUTTON (btn), FALSE);
#else
btn = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (btn), gtk_image_new_from_icon_name ("gtk-close", GTK_ICON_SIZE_MENU));
gtk_button_set_relief (GTK_BUTTON (btn), GTK_RELIEF_NONE);
#endif
#if GTK_CHECK_VERSION (3,20,0)
gtk_widget_set_focus_on_click (btn, FALSE);
#else
gtk_button_set_focus_on_click (GTK_BUTTON (btn), FALSE);
#endif
// add button to box
#if GTK_CHECK_VERSION (4,0,0)
gtk_box_append (GTK_BOX (head), btn);
#else
gtk_box_pack_start (GTK_BOX (head), btn, FALSE, FALSE, 0);
#endif
#if !GTK_CHECK_VERSION (4,0,0)
gtk_widget_show_all (head);
#endif
// add boxes to notebook
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), content, head);
gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), content, TRUE);
问题,按钮过大:
与mate-terminal
或geany
相比:
有人有好主意吗?
所以一个解决方案:
#if GTK_CHECK_VERSION (4,0,0)
GtkCssProvider* provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "* { padding:0; }", -1);
gtk_style_context_add_provider (
gtk_widget_get_style_context (GTK_WIDGET (btn)),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#elif GTK_CHECK_VERSION (3,0,0)
GtkCssProvider* provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "* { padding:0; }", -1, NULL);
gtk_style_context_add_provider (
gtk_widget_get_style_context (GTK_WIDGET (btn)),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#else
gtk_widget_set_name (btn, "close-button");
gtk_rc_parse_string ("style \"close-button\"\n"
"{\n"
"GtkWidget::focus-padding = 0\n"
"GtkWidget::focus-line-width = 0\n"
"xthickness = 0\n"
"ythickness = 0\n"
"}\n"
"widget \"*.close-button\" style \"close-button\"");
#endif
我正在寻找一种方法来为笔记本标签创建一个小的关闭按钮。 下面是为 GTK 2.24、GTK 3 和 4 创建一个大关闭按钮的常用方法:
GtkWidget *head, *content, *image, *btn, *label = gtk_label_new ("Title");
// create empty boxes
#if GTK_CHECK_VERSION (3,0,0)
head = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
#else
head = gtk_hbox_new (FALSE, 0);
content = gtk_vbox_new (FALSE, 0);
#endif
// add text to box
#if GTK_CHECK_VERSION (4,0,0)
gtk_box_append (GTK_BOX (head), label);
#else
gtk_box_pack_start (GTK_BOX (head), label, FALSE, FALSE, 0);
#endif
// create close button
#if GTK_CHECK_VERSION (4,0,0)
btn = gtk_button_new_from_icon_name ("gtk-close");
gtk_button_set_has_frame (GTK_BUTTON (btn), FALSE);
#else
btn = gtk_button_new ();
gtk_button_set_image (GTK_BUTTON (btn), gtk_image_new_from_icon_name ("gtk-close", GTK_ICON_SIZE_MENU));
gtk_button_set_relief (GTK_BUTTON (btn), GTK_RELIEF_NONE);
#endif
#if GTK_CHECK_VERSION (3,20,0)
gtk_widget_set_focus_on_click (btn, FALSE);
#else
gtk_button_set_focus_on_click (GTK_BUTTON (btn), FALSE);
#endif
// add button to box
#if GTK_CHECK_VERSION (4,0,0)
gtk_box_append (GTK_BOX (head), btn);
#else
gtk_box_pack_start (GTK_BOX (head), btn, FALSE, FALSE, 0);
#endif
#if !GTK_CHECK_VERSION (4,0,0)
gtk_widget_show_all (head);
#endif
// add boxes to notebook
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), content, head);
gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook), content, TRUE);
问题,按钮过大:
与mate-terminal
或geany
相比:
有人有好主意吗?
所以一个解决方案:
#if GTK_CHECK_VERSION (4,0,0)
GtkCssProvider* provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "* { padding:0; }", -1);
gtk_style_context_add_provider (
gtk_widget_get_style_context (GTK_WIDGET (btn)),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#elif GTK_CHECK_VERSION (3,0,0)
GtkCssProvider* provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "* { padding:0; }", -1, NULL);
gtk_style_context_add_provider (
gtk_widget_get_style_context (GTK_WIDGET (btn)),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#else
gtk_widget_set_name (btn, "close-button");
gtk_rc_parse_string ("style \"close-button\"\n"
"{\n"
"GtkWidget::focus-padding = 0\n"
"GtkWidget::focus-line-width = 0\n"
"xthickness = 0\n"
"ythickness = 0\n"
"}\n"
"widget \"*.close-button\" style \"close-button\"");
#endif