我正在开发一个简单的程序,使用GTK + / Glade / C编程语言使用打印对话框打印文本视图中的文本。
以下是来源:
ui.glade文件:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkTextBuffer" id="textbuffer1"/>
<object class="GtkWindow" id="window1">
<property name="width_request">440</property>
<property name="height_request">250</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTextView" id="textview1">
<property name="width_request">416</property>
<property name="height_request">186</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="buffer">textbuffer1</property>
</object>
<packing>
<property name="x">13</property>
<property name="y">13</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Print</property>
<property name="width_request">145</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="x">284</property>
<property name="y">210</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Main.c源代码:
#include <gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/************************ widgets variables **************************/
GtkBuilder *builder;
GtkWidget *window;
GtkButton *button1;
GtkTextView *textview1;
GtkTextBuffer *textbuffer1;
/************************ Printing button ***************************/
void on_button1_clicked()
{
//I'm blocked here
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder,"ui.glade",NULL);
/************************** Getting widgets from UI file *****************************/
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
textview1 = GTK_WIDGET(gtk_builder_get_object(builder, "textview1"));
textbuffer1 = GTK_WIDGET(gtk_builder_get_object(builder, "textbuffer1"));
button1 = GTK_WIDGET(gtk_builder_get_object(builder, "button1"));
/************************** Connecting button signal **************************************/
g_signal_connect (G_OBJECT (button1), "clicked", (GCallback)on_button1_clicked, NULL);
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
所以我在编码打印功能时被阻止了,在这种情况下是on_button1_clicked
。
我曾尝试阅读有关GtkPrintOperation的GTK文档,但这对我来说并不是很有帮助,因为初学者+使用C和GTK这个特殊主题的教程很少见!
答案 0 :(得分:0)
没有超级简单的方法来简单地打印一些文本(可能有一些库,但我不知道)。相反,您需要自己处理图形布局。通过连接begin_page
和draw_page
信号,可以使用GtkPrintOperation完成此操作。您将获得每个页面的回调,以便您可以使用Cairo绘制页面。为了帮助解决文本布局的所有复杂细节,建议您使用Pango。然后,您可以根据打印机使用的纸张尺寸选择字体大小和所有内容。这需要多么复杂取决于您的需求。