2017-09-01 92 views
0

我正在研究一个简单的程序,它使用GTK +/Glade/C编程语言打印使用打印对话框在textview中编写的文本。用GTK +/C打印textView

这里是源:

的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

没有超级简单的方法来简单地打印一些文本(也许有一些库,但没有我知道的)。相反,您需要自己处理图形布局。这是通过连接begin_pagedraw_page信号使用GtkPrintOperation完成的。你会得到每个页面的回调,所以你可以使用Cairo来绘制页面。为了帮助处理所有复杂的文本布局细节,建议您使用Pango。然后,由您根据打印机使用的纸张大小选择字体大小和所有内容。这需要多大的复杂程度取决于您的需求。