gtk+修改控件文本字体一例
因为家里电脑是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>
运行效果如下:
gtk+修改控件文本字体一例的更多相关文章
- Web控件文本框Reset的功能
在前一篇中<怎样实现Web控件文本框Reset的功能>http://www.cnblogs.com/insus/p/4120889.html Insus.NET只实现了文本框的功能.单个或 ...
- Delphi: 获取控件文本宽度(像素)
为适应多语言,需要对界面控件大小.位置多动态改变,因此需要根据其Caption计算实际像素大小. 找资料未有易用现成的,遂参数其它方法,写以函数处之,代码如下: uses TypInfo; funct ...
- 控件包含代码块(即 <% ... %>),因此无法修改控件集合
错误: “/”应用程序中的服务器错误. 控件包含代码块(即 <% ... %>),因此无法修改控件集合. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解 ...
- selenium修改控件属性
起因:在进行退出的时候,控件属性显示为不可显示,于是在界面上是不可以点击的,如果想点击这个按钮,只能通过修改控件属性,进行点击 上图看控件,正常来说,点击坐边的用户名,下拉会出现一个logout,退出 ...
- asp.net 中的那些编译错误(1):控件包含代码块(即<% ... %>),因此无法修改控件集合
在编译页面的时候出现:控件包含代码块(即 <% ... %>),因此无法修改控件集合错误 一般原因是: 在<head runat="server">< ...
- WPF编程:textbox控件文本框数据显示最后一行
WPF编程:textbox控件文本框数据显示最后一行 TextBox控件在接收大量数据的时候,滚动条一般在最上方,如何使滚动条随着数据的接收而向下滚动呢?比如有一个TextBox'控件txbRecvD ...
- WPF-学习笔记 动态修改控件Margin的值
原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...
- 将字符串映射为Delphi控件名,批量修改控件属性
http://blog.sina.com.cn/s/blog_4dfbd07c01000a81.html 将字符串映射为Delphi控件名,批量修改控件属性 (2007-10-08 14:50:51) ...
- win32 修改Edit控件文本颜色与背景色
#define WM_CTLCOLORMSGBOX 0x0132 #define WM_CTLCOLOREDIT 0x0133 //编辑控件Edit #define WM_CTLCOLORLISTBO ...
随机推荐
- C++三目运算符的增强
<p>// 在C语言中表达式的结果放在寄存器中 // 在C语言中,表达式的返回值是变量的值 // 在C++中,表达式返回的是变量的本身</p><pre name=&quo ...
- JQuery实战---窗口效果
在前面的相关博文中,小编对jquery的相关知识进行了简单的总结,关于jquery的很多小的知识点,都需要我们自己去动手和实践,一行行代码都需要我们自己亲自动手去敲,今天我们继续来学习jquery的相 ...
- C语言--static修饰变量
Static在C语言里面有两个作用,第一个是修饰变量,第二个是修饰函数. 1.Static修饰变量 按照作用范围的不同,变量分为局部变量和全局变量.如果用static修饰变量,不论这个变量是全局的还是 ...
- Mybatis执行CachingExecutor(六)
前面几篇博客我们介绍了Excutor及抽象类BaseExecutor和实现类SimpleExecutor.BatchExecutor和ReuseExecutor: 博客列表: Mybatis执行Exe ...
- 百度地图API的学习
我们可以进入百度API的网站学习百度地图API:http://dev.baidu.com/wiki/map/index.php,看完这些你应该基本上会掌握了,还有一些显示地图中一些很神奇的效果,需要一 ...
- 海量数据挖掘MMDS week6: 支持向量机Support-Vector Machines,SVM
http://blog.csdn.net/pipisorry/article/details/49445387 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- Jquery EasyUI +Ajax +Json +一般处理程序 实现数据的前台与后台的交互 --- 善良公社项目
经过上一篇博客,本节主要是来看实现的功能是后台的数据通过json数据传过来,前台修改的数据再传回数据库之后页面再次更新table中的数据: 图示: 实例:前台的代码 <%--表格显示区--%&g ...
- SecureCRT设置linux终端显示颜色
在linux系统上,我们使用终端时,对于文件或目录会显示不同的颜色.而SecureCRT默认显示的颜色是单一的,我们该如何让其像linux一样显示个性化颜色呢. 使用SecureCRT登录 linux ...
- Oracle R12 多组织访问的控制 - MOAC(Multi-Org Access Control)
什么是MOAC MOAC(Multi-Org Access Control)为多组织访问控制,是Oracle EBS R12的重要新功能,它可以实现在一个Responsibility下对多个Opera ...