在Thunar中有使用exo_mutual_binding_new_full 和 exo_binding_new进行控件属性绑定,原型是:
/* 互相绑定, object1和object2的属性值通过transform和reverse_transform互相影响 */ ExoMutualBinding* exo_mutual_binding_new_full (GObject *object1, const gchar *property1, GObject *object2, const gchar *property2, /* property1为src, property2为dest */ ExoBindingTransform transform, /* property2为src, property1为dest */ ExoBindingTransform reverse_transform, GDestroyNotify destroy_notify, gpointer user_data);
/* 简化的绑定,当src_property改变时直接将dst_property的值也改变 */ ExoBinding* exo_binding_new (GObject *src_object, const gchar *src_property, GObject *dst_object, const gchar *dst_property);
先来看看关于exo_mutual_binding_new_full的代码片段:
./thunar-preferences-dialog.c
GtkWidget *combo; combo = gtk_combo_box_new_text (); gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Icon View")); gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Detailed List View")); gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Compact List View")); gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Last Active View")); // 将dialog->preferences的"default-view"属性值和combo的 "active"属性值进行关联 exo_mutual_binding_new_full (G_OBJECT (dialog->preferences), "default-view", G_OBJECT (combo), "active", transform_view_string_to_index, transform_view_index_to_string, NULL, NULL);
// dialog->preferences的"default-view"属性值改变后combo的 "active"属性值同时变化 static gboolean transform_view_string_to_index (const GValue *src_value, GValue *dst_value, gpointer user_data) { GType type; type = g_type_from_name (g_value_get_string (src_value)); if (type == THUNAR_TYPE_ICON_VIEW) g_value_set_int (dst_value, 0); else if (type == THUNAR_TYPE_DETAILS_VIEW) g_value_set_int (dst_value, 1); else if (type == THUNAR_TYPE_COMPACT_VIEW) g_value_set_int (dst_value, 2); else g_value_set_int (dst_value, 3); return TRUE; }
// combo的 "active"属性值改变后dialog->preferences的"default-view"属性值也同时变化 static gboolean transform_view_index_to_string (const GValue *src_value, GValue *dst_value, gpointer user_data) { switch (g_value_get_int (src_value)) { case 0: g_value_set_static_string (dst_value, g_type_name (THUNAR_TYPE_ICON_VIEW)); break; case 1: g_value_set_static_string (dst_value, g_type_name (THUNAR_TYPE_DETAILS_VIEW)); break; case 2: g_value_set_static_string (dst_value, g_type_name (THUNAR_TYPE_COMPACT_VIEW)); break; default: g_value_set_static_string (dst_value, g_type_name (G_TYPE_NONE)); break; } return TRUE; }
在./thunar-preferences.c 中对属性"default-view"进行定义。
这里会用到函数,原型是:
GParamSpec* g_param_spec_string (const gchar *name, const gchar *nick, const gchar *blurb, const gchar *default_value, GParamFlags flags);
enum { PROP_0, PROP_DEFAULT_VIEW, PROP_LAST_COMPACT_VIEW_ZOOM_LEVEL, //... } static void thunar_preferences_class_init (ThunarPreferencesClass *klass) { GObjectClass *gobject_class; gobject_class = G_OBJECT_CLASS (klass); gobject_class->finalize = thunar_preferences_finalize; gobject_class->get_property = thunar_preferences_get_property; gobject_class->set_property = thunar_preferences_set_property; /** * ThunarPreferences:default-view: * * The name of the widget class, which should be used for the * view pane in new #ThunarWindow<!---->s or "void" to use the * last selected view from the "last-view" preference. **/ g_object_class_install_property (gobject_class, PROP_DEFAULT_VIEW,//属性ID,一般使用枚举,防止值重复 g_param_spec_string ("default-view",//属性名 "DefaultView",//属性描述,随便写 "default-view",//属性描述,随便写 "void",//设定初始值 EXO_PARAM_READWRITE));//设定属性是可读写的 /** * ThunarPreferences:last-compact-view-zoom-level: * * The last selected #ThunarZoomLevel for the #ThunarCompactView. **/ g_object_class_install_property (gobject_class, PROP_LAST_COMPACT_VIEW_ZOOM_LEVEL, g_param_spec_enum ("last-compact-view-zoom-level", "LastCompactViewZoomLevel", "last-compact-view-zoom-level", THUNAR_TYPE_ZOOM_LEVEL, THUNAR_ZOOM_LEVEL_SMALLEST, EXO_PARAM_READWRITE)); }
那么需要取得"default-view"属性时,可以参考下面方式:
./thunar-window.c
gchar *type_name; GType type; /* determine the default view */ g_object_get (G_OBJECT (window->preferences), "default-view", &type_name, NULL); type = g_type_from_name (type_name); g_free (type_name);
另外,thunar的常用值可以在文件docs/README.thunarrc中找到