因为家里电脑是Mac系统,所以就拿Mac系统来示范。
要注意的是gtk+2.0和3.0对字体的处理是有一些区别的:
1.后者使用的是pango的机制,我们这里以gtk+3.0为基础。
2.两者调用Font对话框的方法也不一样,前者用gtk_font_selection_dialog_new来建立对话框,还要设置button_ok按钮的回调函数,比较繁琐;后者用gtk_font_chooser_dialog_new建立对话框,可以直接取得字体名称,比较简洁。这里同样以3.0为基础。
下面上代码:
#include <gtk/gtk.h>
#include <string.h>
#define UI_PATH "./entry.ui"
#define MAIN_WINDOW "window"
#define ENTRY "entry"
#define CHK_EDITABLE "chk_editable"
#define CHK_VISIBLE "chk_visible"
static GtkWidget *w;
static GtkWidget *s_entry;
static GtkWidget *s_dialog_font = NULL;
void font_selection_ok(GtkWidget *button,gpointer user_data){
GtkStyle *style;
//GdkFont *font;
//gchar *font_name = gtk_font_selection_dialog_get_font_name(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
//printf("%s:font_name is %s\n",__func__,font_name);
/*
font = gtk_font_selection_dialog_get_font(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
if(font){
style = gtk_style_copy(gtk_widget_get_style(s_entry));
gdk_font_unref(style->font);
style->font = font;
gdk_font_ref(style->font);
gtk_widget_set_style(s_entry,style);
}
*/
}
void font_changed(GtkWidget *self,gpointer user_data){
s_dialog_font = gtk_font_chooser_dialog_new("Font Selection Dialog :)",GTK_WINDOW(w));
gtk_window_set_transient_for(GTK_WINDOW(s_dialog_font),GTK_WINDOW(w));
gtk_window_set_destroy_with_parent(GTK_WINDOW(s_dialog_font),TRUE);
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(s_dialog_font),NULL);
if(gtk_dialog_run(GTK_DIALOG(s_dialog_font)) == GTK_RESPONSE_OK){
gchar *font_name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(s_dialog_font));
printf("%s:font_name is %s\n",__func__,font_name);
PangoFontDescription *pfd = pango_font_description_from_string(font_name);
//gtk_widget_modify_font(s_entry,pfd);
gtk_widget_override_font(s_entry,pfd);
pango_font_description_free(pfd);
}
gtk_widget_destroy(s_dialog_font);
/*
if(!s_dialog_font){
s_dialog_font = gtk_font_selection_dialog_new("Font Selection Dialog :)");
g_return_if_fail(GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font));
//gtk_window_position(GTK_WINDOW(s_dialog_font),GTK_WIN_POS_MOUSE);
gtk_window_set_position(GTK_WINDOW(s_dialog_font),GTK_WIN_POS_MOUSE);
g_signal_connect(s_dialog_font,"destroy",
G_CALLBACK(gtk_widget_destroyed),&s_dialog_font);
g_signal_connect(gtk_font_selection_dialog_get_ok_button(
GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font)),
"clicked",G_CALLBACK(font_selection_ok),NULL);
g_signal_connect_swapped(gtk_font_selection_dialog_get_cancel_button(
GTK_IS_FONT_SELECTION_DIALOG(s_dialog_font)),
"clicked",G_CALLBACK(gtk_widget_destroy),s_dialog_font);
}
//if(!GTK_WIDGET_VISIBLE(s_dialog_font))
if(!gtk_widget_get_visible(s_dialog_font))
gtk_widget_show(s_dialog_font);
else
gtk_widget_destroy(s_dialog_font);
*/
}
void editable_toggled(GtkWidget *self,GtkEntry *entry){
//gtk_entry_set_editable(entry,GTK_TOGGLE_BUTTON(self)->active);
printf("%s:enter function\n",__func__);
gtk_editable_set_editable((GtkEditable*)entry,gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(self)));
}
void visible_toggled(GtkWidget *self,GtkEntry *entry){
//gtk_entry_set_visibility(entry,GTK_TOGGLE_BUTTON(self)->active);
printf("%s:enter function\n",__func__);
gtk_entry_set_visibility(entry,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self)));
}
void entry_changed(GtkWidget *self,gpointer user_data){
gchar *text = (gchar*)gtk_entry_get_text(GTK_ENTRY(self));
printf("%s:text is %s\n",__func__,text);
text = g_ascii_strup(text,strlen(text));
gtk_entry_set_text(GTK_ENTRY(self),text);
}
int main(int argc,char **argv){
//GtkWidget *w;
GError *error = NULL;
gtk_init(&argc,&argv);
GtkBuilder *builder = gtk_builder_new();
if(!gtk_builder_add_from_file(builder,UI_PATH,&error)){
g_critical("Couldn't load builder file: %s",error->message);
g_error_free(error);
}
w = GTK_WIDGET(gtk_builder_get_object(builder,MAIN_WINDOW));
if(!w){
g_critical("Widget \"%s\" is missing in file %s",MAIN_WINDOW,UI_PATH);
}
gtk_window_set_title(GTK_WINDOW(w),"clist todo list");
gtk_window_set_default_size(GTK_WINDOW(w),300,100);
gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(w),5);
s_entry = GTK_WIDGET(gtk_builder_get_object(builder,ENTRY));
gtk_builder_connect_signals(builder,NULL);
g_object_unref(builder);
gtk_widget_show_all(w);
gtk_main();
return 0;
}
entry.ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window">
<property name="height_request">100</property>
<property name="can_focus">False</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">10</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="column_spacing">5</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">大写化</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="max_width_chars">50</property>
<property name="shadow_type">etched-out</property>
<property name="progress_pulse_step">0.089999999999999997</property>
<property name="secondary_icon_stock">gtk-help</property>
<property name="secondary_icon_tooltip_markup" translatable="yes">输入文本自动转换为大写</property>
<signal name="changed" handler="entry_changed" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_font">
<property name="label">gtk-select-font</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="font_changed" swapped="no"/>
</object>
<packing>
<property name="left_attach">3</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xscale">0</property>
<property name="yscale">0</property>
<child>
<object class="GtkCheckButton" id="chk_editable">
<property name="label" translatable="yes">Editable</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="editable_toggled" object="entry" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xscale">0</property>
<property name="yscale">0</property>
<child>
<object class="GtkCheckButton" id="chk_visible">
<property name="label" translatable="yes">Visible</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="visible_toggled" object="entry" swapped="no"/>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xscale">0.5</property>
<property name="yscale">0</property>
<child>
<object class="GtkButton" id="btn_ok">
<property name="label" translatable="yes">O K</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="gtk_main_quit" swapped="yes"/>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
运行效果如下: