Multiline Text Editing Widget (1)

gtk GtkTextView 使用尝试 GtkTextView:代表了窗口中可见的文本框,用来显示GtkTextBuffer. GtkTextBuffer:文本框正文的缓冲区,文本框文字的插入、删除都是对这一类变量进行操作。 GtkTextIter:保存文字在buffer中位置的结构。 GtkTextMark:A position in the buffer preserved across buffer modification 阅读详情

Multiline Text Editing Widget
        From: http://www.bravegnu.org/gtktext/

Vijay Kumar B. vijaykumar@bravegnu.org

$Name: GTKTEXT_0_2_0 $ $Id: gtktext.docbook,v 1.4 2005/05/23 01:39:32 vijay Exp $
Other Formats: [DocBook Tar ball] [HTML Tar ball] [Single HTML] [TXT]

1. Simple Multiline Text Widget

In this section, you will learn how to create a simple multiline text widget, using GtkTextView, for data entry.

The widget itself is created using

GtkWidget *gtk_text_view_new( void );

When the GtkTextView widget is created this way, a GtkTextBuffer object associated with this widget is also created. The GtkTextBuffer is responsible for storing the text and associated attributes, while the GtkTextView widget is responsible for displaying the text ie. it provides an I/O interface to buffer.

All text modification operations are related to the buffer object. The buffer associated with a GtkTextView widget can be obtained using

GtkTextBuffer *gtk_text_view_get_buffer( GtkTextView *text_view );

Some common operations performed on a simple multiline text widget are, setting the entire text and reading the entire text from the buffer. The entire text of the buffer can be set using

void gtk_text_buffer_set_text( GtkTextBuffer *buffer, 
                               const gchar   *text,
                               gint          len );
The len should be specified when the text contains '/0'. When the text does not contain '/0'and is terminated by a '/0', len could be -1.

Getting the entire text of a buffer, could be a little more complicated than setting the entire text of the buffer. You will have to understand iterators. Iterators are objects that represent positions between two characters in a buffer. Iters in a buffer can be obtained using many different functions, but for our simple case the following functions can be used to get the iters at the start and end of the buffer.

void gtk_text_buffer_get_start_iter( GtkTextBuffer *buffer,
                                     GtkTextIter   *iter );
void gtk_text_buffer_get_end_iter( GtkTextBuffer *buffer,
                                   GtkTextIter   *iter );

Unlike other objects which are created using constructor like functions returning pointers to the objects, the GtkTextIter objects are created by instantiating the structure itself i.e they are allocated on the stack. So new GtkTextIter objects are created as follows

GtkTextIter start_iter;
GtkTextIter end_iter;
Pointers to the iterators are then passed to the above functions for initialization.

The initialized iters, can be used in the following function to retrieve the entire contents of the buffer.

gchar *gtk_text_buffer_get_text( GtkTextBuffer     *buffer, 
                                 const GtkTextIter *start,
                                 const GtkTextIter *end,
                                 gboolean          include_hidden_chars );
The include_hidden_chars is used to specify whether are not to include text that has the invisible attribute set. Text attributes and how to set them will be discussed later in this tutorial. The returned string is dynamically allocated and should be freed using g_free.

Below is a sample program, that implements the simple multiline text widget. The program assigns a default text to the buffer. The text in the buffer can be modified by the user and when the close button is pressed it prints the contents of the buffer and quits.

#include <gtk/gtk.h>

void
on_window_destroy (GtkWidget *widget, gpointer data)
{
  gtk_main_quit ();
}

/* Callback for close button */
void
on_button_clicked (GtkWidget *button, GtkTextBuffer *buffer)
{
  GtkTextIter start;
  GtkTextIter end;

  gchar *text;

  /* Obtain iters for the start and end of points of the buffer */
  gtk_text_buffer_get_start_iter (buffer, &start);
  gtk_text_buffer_get_end_iter (buffer, &end);

  /* Get the entire buffer text. */
  text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

  /* Print the text */
  g_print ("%s", text);

  g_free (text);

  gtk_main_quit ();
}

int 
main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *text_view;
  GtkWidget *button;
  GtkTextBuffer *buffer;
  
  gtk_init (&argc, &argv);

  /* Create a Window. */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Simple Multiline Text Input");

  /* Set a decent default size for the window. */
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  g_signal_connect (G_OBJECT (window), "destroy", 
                    G_CALLBACK (on_window_destroy),
                    NULL);

  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  /* Create a multiline text widget. */
  text_view = gtk_text_view_new ();
  gtk_box_pack_start (GTK_BOX (vbox), text_view, 1, 1, 0);

  /* Obtaining the buffer associated with the widget. */
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
  /* Set the default buffer text. */ 
  gtk_text_buffer_set_text (buffer, "Hello Text View!", -1);
  
  /* Create a close button. */
  button = gtk_button_new_with_label ("Close");
  gtk_box_pack_start (GTK_BOX (vbox), button, 0, 0, 0);
  g_signal_connect (G_OBJECT (button), "clicked", 
                    G_CALLBACK (on_button_clicked),
                    buffer);
  
  gtk_widget_show_all (window);

  gtk_main ();
  return 0;
}

1.1. More on Iterators

A point to note about text iterators - iterators are not valid indefinitely. Whenever the buffer is modified in a way that affects the number of characters in the buffer, all outstanding iterators become invalid. You will have to re-obtain iterators to use them. To preserve positions across buffer modifications the GtkTextMark can be used. Text marks will be discussed later in this tutorial.

1.2. UTF-8 and GTK+

GTK+ handles text in UTF-8 format. For the uninitiated, the UTF-8 is an ASCII compatible multi-byte unicode encoding. The thing to be noted is that one character can be encoded as multiple bytes. The GTK+ manual uses the term offset for character counts, and uses the term index for byte counts. The len argument of the gtk_text_buffer_set_text function is the length of the text in bytes.

1.3. Other Editing Functions

The following function can be used to delete text from a buffer.

void gtk_text_buffer_delete( GtkTextBuffer *buffer,
                             GtkTextIter   *start,
                             GtkTextIter   *end );
Since this function modifies the buffer, all outstanding iterators become invalid after a call to this function. But, the iterators passed to the function are re-initialized to point to the location where the text was deleted.

The following function can be used to insert text into a buffer at a position specified by an iterator.

void gtk_text_buffer_insert( GtkTextBuffer *buffer,
                             GtkTextIter   *iter,
                             const gchar   *text,
                             gint          len );
The len argument is similar to the len argument in gtk_text_buffer_set_text function. As with gtk_text_buffer_delete, the buffer is modified and hence all outstanding iterators become invalid, and start and end are re-initialized. Hence the same iterator can be used for a series of consecutive inserts.

The following function can be used to insert text at the current cursor position.

void gtk_text_buffer_insert_at_cursor( GtkTextBuffer *buffer,
                                       const gchar   *text,
                                       gint          len );

1.4. Other Functions to Obtain Iters

The following function can be used to get iterators at the beginning and end of the buffer in one go.

void gtk_text_buffer_get_bounds( GtkTextBuffer *buffer,
                                 GtkTextIter   *start,
                                 GtkTextIter   *end );

A variant of the above function can be used to obtain iterators at the beginning and end of the current selection.

void gtk_text_buffer_selection_bounds( GtkTextBuffer *buffer,
                                       GtkTextIter   *start,
                                       GtkTextIter   *end );

The following functions can be used to obtain an iterator at a specified character offset into the buffer, at the start of the given line, at an character offset into a given line, or at a byte offset into a given line, respectively.

void gtk_text_buffer_get_iter_at_offset( GtkTextBuffer *buffer,
                                         GtkTextIter   *iter,
                                         gint          char_offset);
void gtk_text_buffer_get_iter_at_line( GtkTextBuffer *buffer,
                                       GtkTextIter   *iter,
                                       gint          line_number);
void gtk_text_buffer_get_iter_at_line_offset( GtkTextBuffer *buffer,
                                              GtkTextIter   *iter,
                                              gint          line_no,
                                              gint          offset );
void gtk_text_buffer_get_iter_at_line_index( GtkTextBuffer *buffer,
                                             GtkTextIter   *iter,
                                             gint          line_no,
                                             gint          index );

星辰超级智能体TeleClaw桌面版深度体验:央企出品的本地AI生产力助手 中国电信星辰超级智能体TeleClaw桌面版是一款本地化AI生产力助手,主打数据安全、自主高效和深度协同。作为程序员,作者实测其核心功能包括本地数据处理、长期记忆、定时任务和代码开发辅助,特别适合敏感数据处理和日常办公自动化。相比在线AI工具,TeleClaw在数据安全、合规性和自动化能力方面优势明显,但技能生态和响应速度仍有提升空间。总体评价为务实高效的本地AI助手,尤其适合对数据安全有要求的企业用户。 阅读详情

相关推荐

Windows版MySQL8.4.2LTS解压直用(下载免安装-绿色-项目打包直接使用-含卸载)

安装前运行微软常用运行库全集(也可能称为依赖)关于MySQL8中相关参数配置,可通过。选择8.5.2LTS长期服务版。选中密码,右键会直接复制。

xzzteach的博客 1395

GTK 缓冲区

转发http://blog.sina.com.cn/s/blog_712a04260101atmq.html Gtk+ 学习笔记19  --文本框构件 (2013-04-13 22:31:03) 转载▼ 标签: it 分类: Linux   GtkTextView:代表了窗口中可见的文本框,用来显示GtkTextBuffer.

博主回家睡觉了。。。 2254

python全栈工程师完整版视频教程

python全栈工程师完整版视频教程,全套52.38G,92天课程,包含免密视频、课件、项目代码,课程目录:1.全栈开发-基础篇 第1-39天2.全栈开发-前端篇 第40-50天3.全栈开发-web框架篇 第51-69天4.全栈开发-项目实战篇 第70-92天

14-6 使用 glade 完成布局

1. 使用glade 新增对象及信号 前端:使用 glade 修改布局与信号。例如,在 glade 中新建对象 GtkTextBuffer,在将其 id 命名为 button2_data,内容为 “你好,中国!”。 设置构件按钮2点击信号的回调函数为 print_hello2,且在调用该函数时传入button2_data 参数。 2. 实现代码与glade 新增对象及信号的链接 后端:修改代码,实现前端修改的样式。具体修改流程为: ...

gltzlike的博客 678

GTK+图形化应用程序开发学习笔记(二十一)—文本框构建

 GTK+图形化应用程序开发学习笔记(二十一)—文本框构建 文本框也叫多行文本输入框,是gtk+较常用的构件,也是较复杂的一个构件。它被分为以下几部分: GtkTextView:代表了窗口中可见的文本框,用来显示GtkTextBuffer.GtkTextBuffer:文本框正文的缓冲区,文本框文字的插入、删除都是对这一类变量进行操作。GtkTextIter:保存文字在buffe

深之JohnChen的专栏 7664

GTK+图形化应用程序开发学习笔记(二十一)—文本框构建(3)

四、文本框缓冲区文本的获得和设置       我们可以用函数gtk_text_buffer_get_text来获得文本框缓冲区的文本,用gtk_text_buffer_set_text来设置它。 10.名称::gtk_text_buffer_get_text功能:文本框缓冲区文本的获得头文件:

紫侠的天空 1806

Multiline Text Editing Widget (4)

From: http://www.bravegnu.org/gtktext/x253.htmlPrevNext4. SearchingIn this section, you will learn how to search through a text buffer. Along the way you will learn about marks, as well. We will start

Duzy Chan 2210

Multiline Text Editing Widget (5)

From: http://www.bravegnu.org/gtktext/x346.htmlPrevNext5. Examining and Modifying TextExamining and modifying text is yet another common operation performed on text buffers. Examples: converting a sel

Duzy Chan 1671

Multiline Text Editing Widget (7)

From: http://www.bravegnu.org/gtktext/x471.htmlPrevNext7. Buffer and Window CoordinatesSometimes it is necessary to know the position of the text cursor on the screen, or the word in a buffer under th

Duzy Chan 1765

Multiline Text Editing Widget (6)

From: http://www.bravegnu.org/gtktext/x417.htmlPrevNext6. Images/WidgetsA text buffer can hold images and anchor location for widgets. In this section, you will learn how to insert images and widgets.

Duzy Chan 1797

Multiline Text Editing Widget (8)

From: http://www.bravegnu.org/gtktext/x534.htmlPrevNext8. Final Notes8.1. GtkTextView in gtk-demoThe gtk-demo which is distributed along the GTK+ library contains a demo on the GtkTextView and friends

Duzy Chan 1408

Multiline Text Editing Widget (2)

From: http://www.bravegnu.org/gtktext/x92.htmlPrevNext2. Formatted Text in GtkTextViewGtkTextView can also be used to display formatted text. This usually involves creating tags which represent a grou

Duzy Chan 1961

gtk+学习笔记(七)

今天被一个文本框坑了,基本设置什么的都对,但是就是无法显示中文,按钮名称都可以显示中文,先介绍下文本框的基本函数吧. GtkWidget *gtk_text_view_new(void);新建一个文本框,gtk_text_view_new创建一个新的缓冲区,如果你没有在创建之前调用函数gtk_text_view_set_buffer来设置这个缓冲区,那么系统将创建一个空的默认的缓冲区,我们可以用...

weixin_33807284的博客 189

学习使用GTK+ 5.GTK+常用物件及API(文本)

学习使用GTK+ 5.GTK+常用物件及API(文本)<br />最后的叶子学习使用GTK+ 系列的第 6 篇 (本系列共6篇)学习使用GTK+学习使用GTK+ 0.概述学习使用GTK+ 1.构建GTK+编译环境学习使用GTK+ 2.GTK+“Hello World”——对GTK+运作机制的通俗介绍学习使用GTK+ 3.使用GtkBuilder、界面设计器Glade和其他GTK+组件学习使用GTK+ 4.GTK+常用物件及API(窗口)学习使用GTK+ 5.GTK+常用物件及API(文本)<br />这

uunubt的专栏 1465

Gstreamer官方教程汇总基本教程5---GUI toolkit integration

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33939843的博客 336

gtk 程序设计(c语言版),采用GTK进行简单聊天程序客户端的编写,使用C语言。

http://blog.csdn.net/xbl1986/article/details/3458740#include/*全局变量声明区-----------------------------------------------------------------*/GtkWidget *window/*定义主窗口*/,*Send_scrolled_win/*定义发送滚动窗口*/,*Rcv_s...

weixin_31440829的博客 835

GTK+ Reference Manual

GTK+ Reference Manual http://man.chinaunix.net/develop/GTK+/2.6/gtkGTK+ Reference Manual for GTK+ 2.6

深之JohnChen的专栏 2902

PYGTK 的线程刷新界面的完美解决

问题: 你有一个长的处理过程, 该过程将诸塞GTK主循环对事件的控制, 导致界面在处理过程中没有响应, 更糟糕的是, 即使你想在处理过程中刷新界面, 提示进度都毫无用处, 因为它是诸塞的线程, 要么你看不见任何更新, 要么等到处理完成时他们一股脑儿都显示出来. 显然这不是我们想要的.解决: google 了无数文章, 各种方法齐上阵, 但都显得麻烦, 最后, pygtk 的官方Q&A文档给

georgehuzz的专栏 1729

采用GTK进行简单聊天程序客户端的编写,使用C语言。

本程序主要参考了网上的一些程序和自己手头的一些书,有一个西邮的同学的在CU上的博客参考的最多了。也看到这个东西别人有做过的,自己做只是想学习学习,也希望大家共同学习,一起搞,呵呵。首先粘贴第一个版本的程序:(因为我比较笨,所以我是一个喜欢写注释而且注释超级多的人,呵呵)#include/*全局变量声明区------------------------------------------------

xbl1986的专栏 7594
上一篇: GNU Emacs 命令列表
下一篇: Multiline Text Editing Widget (2)
Yazy
博客等级 码龄22年 0粉丝 17原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值