Gtk3 `set_fixed_height_from_font` 不会生成正确高度的单元格
Gtk3 `set_fixed_height_from_font` does not produce cells of correct height
我正在尝试在 Gtk3 中创建一个树视图,使每一行的大小相当于两行文本的大小。以下是一个最小的工作示例:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 100, 100);
/* init store */
GtkListStore *store;
store = gtk_list_store_new(1, G_TYPE_STRING);
/* add items */
int COL = 0;
GtkTreeIter iter1, iter2;
gtk_list_store_append(store, &iter1);
gtk_list_store_set(store, &iter1, COL, "hello", -1);
gtk_list_store_append(store, &iter2);
gtk_list_store_set(store, &iter2, COL, "world", -1);
/* make tree view */
GtkWidget *list;
GtkWidget *vbox;
vbox = gtk_vbox_new(FALSE, 0);
list = gtk_tree_view_new();
gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
/* make column */
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes("Items",
renderer, "text", COL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
/********* This doesn't work as expected! *********/
gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer),2);
/* main */
g_signal_connect(G_OBJECT (window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
我正在使用 set_fixed_height_from_font
根据字体设置高度。现在上面产生
其中有大小为 1 而不是 2 的条目。
这是 Gtk 中的错误,还是我做错了什么?
set_fixed_height_form_font
在 Gtk2 中工作,但在 Gtk3 中表现不同。
我的解决方案(在 OCaml 界面中,如最初发布的那样)是根据 cell_renderer_text.get_size:
明确计算高度
let default_font = "11"
let calc_font_height ?(ypad = 0) (num_lines : int) : int =
let fm = Cairo_pango.Font_map.get_default () in
let ctx = Cairo_pango.Font_map.create_context fm in
let fd = Pango.Font.from_string default_font in
let metrics = Pango.Context.get_metrics ctx fd None in
let ascent = Pango.Font.get_ascent metrics in
let descent = Pango.Font.get_descent metrics in
GPango.to_pixels((GPango.from_pixels ypad) + (num_lines * (ascent + descent)))
然后我们设置属性
(GTree.cell_renderer_text [`FONT default_font; `YPAD 2;
`HEIGHT (Font.calc_font_height ~ypad:2 2)],
["text",col])
我正在尝试在 Gtk3 中创建一个树视图,使每一行的大小相当于两行文本的大小。以下是一个最小的工作示例:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 100, 100);
/* init store */
GtkListStore *store;
store = gtk_list_store_new(1, G_TYPE_STRING);
/* add items */
int COL = 0;
GtkTreeIter iter1, iter2;
gtk_list_store_append(store, &iter1);
gtk_list_store_set(store, &iter1, COL, "hello", -1);
gtk_list_store_append(store, &iter2);
gtk_list_store_set(store, &iter2, COL, "world", -1);
/* make tree view */
GtkWidget *list;
GtkWidget *vbox;
vbox = gtk_vbox_new(FALSE, 0);
list = gtk_tree_view_new();
gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
/* make column */
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes("Items",
renderer, "text", COL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
/********* This doesn't work as expected! *********/
gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer),2);
/* main */
g_signal_connect(G_OBJECT (window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
我正在使用 set_fixed_height_from_font
根据字体设置高度。现在上面产生
其中有大小为 1 而不是 2 的条目。
这是 Gtk 中的错误,还是我做错了什么?
set_fixed_height_form_font
在 Gtk2 中工作,但在 Gtk3 中表现不同。
我的解决方案(在 OCaml 界面中,如最初发布的那样)是根据 cell_renderer_text.get_size:
明确计算高度let default_font = "11"
let calc_font_height ?(ypad = 0) (num_lines : int) : int =
let fm = Cairo_pango.Font_map.get_default () in
let ctx = Cairo_pango.Font_map.create_context fm in
let fd = Pango.Font.from_string default_font in
let metrics = Pango.Context.get_metrics ctx fd None in
let ascent = Pango.Font.get_ascent metrics in
let descent = Pango.Font.get_descent metrics in
GPango.to_pixels((GPango.from_pixels ypad) + (num_lines * (ascent + descent)))
然后我们设置属性
(GTree.cell_renderer_text [`FONT default_font; `YPAD 2;
`HEIGHT (Font.calc_font_height ~ypad:2 2)],
["text",col])