Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198771
  • 博文数量: 60
  • 博客积分: 3269
  • 博客等级: 中校
  • 技术积分: 648
  • 用 户 组: 普通用户
  • 注册时间: 2005-09-21 10:48
文章存档

2012年(6)

2011年(6)

2010年(30)

2009年(8)

2007年(6)

2005年(4)

我的朋友

分类: C/C++

2010-12-01 11:47:28

    网络上搜的一篇翻译文章,简单介绍了GtkHTML的用法,如果写文件缓冲式的浏览器参考这个是足够了。首先向作者致意,向翻译者致意。文中有发布者的相关链接,希望大家支持。
 
GtkHTML Tutorial
Programing with GtkHTML(上)
2009年07月16日 — arxccv
本文下篇地址:
Abstract
摘要
This tutorial will give attendees an overview of GtkHTML’s widget features and capabilities, advise them when to use it and when not.
这个教程将给使用者一个概述GtkHTML组件的特性和功能,当他们不知道如何使用时给与提醒
The usage of GtkHTML will be demonstrated by examples with ascending complexity.
GtkHTML的使用方法将在通过由简到难的例子中得到表现
Examples will show how to use GtkHTML filled with simple HTML content, HTML with images, with embedded widgets and how to print with GtkHTML.
例子将展示怎样使用GtkHTML填充简单的HTML内容,HTML中的图片,嵌入的组件很怎样通过GtkHTML打印
Various signals will be discussed. It will also be shown how to avoid flickering when content is dynamically changing. The final part will give an overview of applications already using gtkhtml and what could be expected in the future.
将要讨论各种信号,它也将说明如何避免当表现的内容动态改变时出现闪烁。最终部分将给出一个已经使用gtkhtml的应用程序的概要和将来可能的预期
Table of Contents
Features overview
Examples compiling
Hello world
Inlined images
Embedded widgets
Dynamic content
How to avoid flickering
Current state and future
The END
Features overview
特征概要
GtkHTML is a Gtk+ widget inherited from GtkLayout. GtkHTML provides these basic features:
GtkHTML是一个继承了GtkLayout的一个Gtk+组件.GtkHTML提供了下面这些基本的特征
HTML document rendering and printing
HTML文档的渲染很打印
Gtk+ container - can embed Gtk+ widgets
Gtk+集成-能嵌入Gtk+组件
HTML document editing
HTML文档编辑
This paper is going to explain how to use GtkHTML to display HTML and how to compose a user interface layout with HTML. Editing will not be covered by this tutorial.
这一页将要解释怎样使用GtkHTML显示HTML以及怎样使用HTML撰写一个用户布局接口
GtkHTML doesn’t support JavaScript, CSS or DOM. If you need CSS 1,2 and/or DOM 1,2, then GtkHTML2 developed by CodeFactory is what you are looking for.
GtkHTML不提供JavaScript,CSS或者DOM,如果你需要CSS 1,2或者DOM1,2,那么你需要使用GtkHTML2 的CodeFactory开发
The following applications show various usages of GtkHTML’s features:
下面的应用程序显示GtkHTML的各种通常特征
Evolution uses GtkHTML to display and print mail messages and also for mail message composing (HTML editing)
Evolution使用GtkHTML去显示和打印邮件信息,也提供邮件撰写,(HTML编辑)
RedCarpet and GNOME Control Center uses GtkHTML for User Interface layouting
RedCarpet和GNOME控制中心使用GtkHTML为用户提供布局接口
GnuCash uses GtkHTML for displaying and printing of reports
GnuCash使用GtkHTML显示和打印报表
To compile the following examples you have to have gtkhtml and gtkhtml-devel packages installed. CFLAGS and LDFLAGS could be obtained using the gnome-config script.
要编译下面的例子你必须安装有gtkhtml和gtkhtml-devel包.CFLAGS和LDFLAGS可以获得需要使用的gnome-config脚本
Here is simple Makefile, which could be used for compiling the examples:
这里是一个简单的能够被使用来编译的Makefile例子:
EXAMPLES = example1 example2 example3 example4 example5
CC = gcc
CFLAGS = `gnome-config gtkhtml --cflags`
LDFLAGS = `gnome-config gtkhtml --libs`
all: $(EXAMPLES)
Hello world
Lets start with a simple “Hello world!” example. The following program creates a Gnome application window filled with GtkHTML widget loaded with simple content defined in the html_source C string.
让我们开始一个简单的”Hello world!”例子。下面的程序创建了一个Gnome应用程序窗体,并且填充了一个加载了定义在html_source_source中的C字符串的简单内容的GtkHTML组件
Example 1.
#include
#include
const gchar *html_source = "Hello world!";
gint
main (gint argc, gchar *args [])
{
 GtkWidget *app;
 GtkWidget *html;
 GtkWidget *scrolled_window;
     /* prepare our environment, we need gnome and gconf */
 /* 初始化我们的环境, 我们需要gnome和gconf */
 gnome_init ("Example_1", "1.0", argc, args);
 gconf_init (argc, args);
 /* create GtkHTML widget with preloaded content */
     /* 创建GtkHTML组件并且预加载内容*/
 html = gtk_html_new_from_string (html_source, -1);
 /* create GNOME app and put GtkHTML in scrolled window in it */
        /*创建GNOME app并且把包含有GtkHTML的scrolled window组件放入*/
 app = gnome_app_new ("Example_1", "Example 1");
 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 gtk_container_add (GTK_CONTAINER (scrolled_window), html);
 gnome_app_set_contents (GNOME_APP (app), scrolled_window);
 gtk_window_set_default_size (GTK_WINDOW (app), 320, 80);
 gtk_widget_show_all (app);
 /* run the main loop */
 gtk_main ();
}
 
Figure 1. Screenshot of running Example 1
图1。运行例1后的截图
In this example we use the
在这个例子中我们使用了
GtkWidget *gtk_html_new_from_string (const gchar *str, gint len);
constructor which creates a new GtkHTML widget loaded with the HTML document stored in str parameter. len is the length of str argument, if -1 is passed, then length is not used and is computed in the constructor. (The reason for this is to avoid repeatedly computing of length)
创建一个新的GtkHTML组件加载预存了HTML文档的str参数来实现构造。len是str参数的长度,如果传入-1,长度将不会被使用,构造时将被计算。(原因是因为这样能避免每次都进行长度的计算)
The next thing to note is that we have packed the GtkHTML widget into a scrolled window. This is pretty important. Older versions of GtkHTML were even segfaulting when the widget was not packed into a GtkScrolledWindow widget.
下一个需要注意的事情是我们将GtkHTML组件放入一个可滚动窗体。这是非常重要的。在老版本中当GtkHTML这个组件不放入一个GtkScrolledWindow组件时会发生segfaulting(段落分割问题)
Also note, that GtkHTML requires GNOME libraries and GConf, thus you need to initialize them before the first call to the GtkHTML library and use gnome.h and gtkhtml/gtkhtml.h include files.
另外注意,GtkHTML包含有GNOME类库和GConf,因而你必须在初始化他们以前首先包含GtkHTML类库很使用gnome.h,gtkhtml/gtkhtml.h包含文件

--------------------------------------------------------------------------------
Inlined images
内嵌图片
In the previous example we have shown how to display simple HTML document in GtkHTML widget. Now we modify this example so it could display HTML with inlined images. Lets modify html_source to:
在上一个例子中我们知道如何在GtkHTML组件中显示简单的HTML文档。现在我修改这个例子让它能够显示HTML中内嵌的图片。让我们修改html_source为:
const gchar *html_source = "Hello world!"
""
If you try to compile and run the modified example, you will find out that instead of a smiley image, an empty rectangle is displayed. The GtkHTML widget itself does not handle loading of a data linked by an URL (i.e. file:smiley.png picture).
如果你试图编译很运行这个修改后的例子,你将发现一个空白的矩形代替了笑脸图片被显示。GtkHTML组件本身不能加载处理一个URL链接的数据(例如:文件smiley.png图片)
When GtkHTML requires data from an URL, it emits the url_requested signal. We need to provide a signal handler which will load data from the requested URL location. The following function handles retrieval of data from URLs in the form file:filename. In our case it will load a smiley PNG image.
当GtkHTML需要从一个URL来的数据时,它将释放一个url_requested信号。将要加载从URL请求的主机过来的数据时,我们必须提供信号处理。跟着函数从URL指向的文件:文件名取回数据并且处理。在我们的计划里它将加载一个笑脸PNG图片
/* url_requested signal handler which is able to provide data for file:* url's
 * in our case for inlined image(s)
 */
static void
url_requested (GtkHTML *html, const char *url, GtkHTMLStream *stream)
{
 int fd;
 if (url && !strncmp (url, "file:", 5)) {
  url += 5;
  fd = open (url, O_RDONLY);
  if (fd != -1) {
   gchar *buf;
   size_t size;
   buf = alloca (8192);
   while ((size = read (fd, buf, 8192)) > 0) {
    gtk_html_stream_write (stream, buf, size);
   }
   gtk_html_stream_close (stream,
            size == -1
            ? GTK_HTML_STREAM_ERROR
            : GTK_HTML_STREAM_OK);
   close (fd);
   return;
  }
 }
 gtk_html_stream_close (stream, GTK_HTML_STREAM_ERROR);
}
The signal is connected after widget creation and before content loading:
组件被创造后信号将被连接,然后加载内容
 html = gtk_html_new ();
 gtk_signal_connect (GTK_OBJECT (html), "url_requested",
                            GTK_SIGNAL_FUNC (url_requested), NULL);
 gtk_html_load_from_string (GTK_HTML (html), html_source, -1);
Note that we have used the gtk_html_new constructor and to load content, we used the gtk_html_load_from_string function. The reason for this is that we want to have the url_request signal handler connected before the content is loaded. Definition of newly used functions:
注意这里我们使用了gtk_html_new构造,我们使用的是gtk_load_from_string函数加载内容。原因是因为我需要在url_request信号句柄连接后加载内容。新定义使用的函数:
GtkWidget * gtk_html_new              ();
void        gtk_html_load_from_string (GtkHTML     *html,
                                       const gchar *str,
                                       gint         len);
Additional header files to be included:
附件被包含的头文件
#include
#include
#include
With older version of GtkHTML, the following must also be called:
GtkHTML的旧版本必要如下先调用:
gdk_rgb_init ();
function.
函数Now our example is complete and the following picture shows Example 2 running.
现在我的例子完成了,下面的图片是例2运行显示:
 
Figure 2. Screenshot of running Example 2
Complete example sources are available at
例子完整的源码可以在下载
Posted in GtkHTML Tutorial.
 
Programing with GtkHTML (下)
2009年07月20日 — arxccv
Embedded widgets
嵌入式组件
In this section I will show you how to embed Gtk+ widgets into a GtkHTML widget. I will extend the previous example by embedding a Close button, which when clicked quits the example.
在这节里我将展示如何将Gtk+组件嵌入到GtkHTML组件中去。我将延续上一个例子,并嵌入一个关闭按钮,当点击时将退出的例子
First we need to insert button to HTML source. This is achieved by the OBJECT tag with the attribute CLASSID. The following string is appended to the html_source string.
首先我们必须插入一个按钮到HTML资源中。然后使用这个标签对象的CLASSID属性来实现。然后将下面的字符串附加到html_source字符串中去。
"

Closebutton"
The next step is to provide Gtk+ button to GtkHTML. This is also done using signals. While GtkHTML parses its content, whenever it finds an OBJECT tag, it emits an object_requested signal. The following code implements our new signal handler, which creates the Close button:
下一步将提供Gtk+按钮到GtkHTML。这里也要使用信号。GtkHTML将分析字符串内容,直到它找到了一个对象的标签为止,此时它将释放一个object_requested信号。下面的代码实现了我们新创建的关闭按钮的信号处理
static void
close_button_clicked ()
{
 gtk_main_quit ();
}
static gboolean
object_requested (GtkHTML *html, GtkHTMLEmbedded *ew)
{
 GtkWidget *button;
 if (!strcmp (ew->classid, "close_button") == 0)
  return FALSE;
 button = gtk_button_new_with_label ("Close");
 gtk_signal_connect (GTK_OBJECT (button), "clicked",
                            GTK_SIGNAL_FUNC (close_button_clicked), NULL);
 gtk_container_add (GTK_CONTAINER (ew), button);
 gtk_widget_show (button);
 return TRUE;
}
Note that the handler has a boolean return value and returns TRUE if we have provided tje requested widget and FALSE if for some reason we cannot provide it. In such a case the HTML content between the opening and closing OBJECT tags will be used instead.
注意这个处理将返回一个布尔值,如果是我们规定的那个组件的请求结果将发回TRUE,如果因为一些原因我们不能支持他将返回FALSE.在这种情况下,HTML开始和关闭对象标签之间的内容将被代替
The next thing to notice is that the provided widget should already be shown (gtk_widget_show call) and added into a GtkHTMLEmbedded widget container, which is passed to callback as the 2nd (ew) parameter.
然后要注意的事情是这里提供的组件应当已经显示(使用gtk_widget_show调用)并且被添加进一个GtkHTMLEmbedded组件集中,并且作为第二个参数传递给回调函数
Finally, we connect our object_requested callback just after GtkHTML widget creation.
最终,我连接我的object_requested回调函数将在GtkHTML组件被创建后。
 gtk_signal_connect (GTK_OBJECT (html), "object_requested",
       GTK_SIGNAL_FUNC (object_requested), NULL);
For Embedded widgets manipulation we need to include this header file:
因为嵌入组件的操作,所以我们必须包含这个头文件
#include
This is all we need for our Embedded widget example. The following picture shows Example 3 running.
这是我们所需要的嵌入组件的例子。下面的图片展示了例3的运行后
 
Figure 3. Screenshot of running Example 3
Dynamic content
动态内容
In this section I would like to show you a simple example of dynamically reloaded GtkHTML content. Real applications typically use more complicated HTML source, which will go outside the scope of this paper. So please take this just as a weak image.
在这一节我想展示一个简单的动态重载GtkHTML内容的例子。真正的应用程序通常使用更多的复制的HTML资源,当然这些将超出了这片教程的深度。所以这里只是让你有一个入门的印象
For better image run RedCarpet and observe how dynamically created User Interfaces could work and look.
比较好的例子是运行RedCarpet并且观察如何动态创建可以工作和观看的用户接口。
My intention is to show you how dynamically created content and User Interfaces could work and to call your attention to possible problems which is explained in the next section. On you can find more examples, which I have found too long to include in this document.
我的打算是在下一节展示如何动态创建内容和可以工作的用户接口,并且解决你有可能遇到的问题。
Lets extend example 2 using this code:
让我延续例2使用的代码
static gboolean
load_content (GtkWidget *html)
{
 GString *padding_nbsps;
 gchar *dynamic_source;
 static gint pos = 0, direction = 1;
 gint i;
 padding_nbsps = g_string_new (NULL);
 for (i = 0; i < pos; i ++)
  g_string_append (padding_nbsps, " ");
 pos += direction;
 if (pos == 30 || pos == 0)
  direction = - direction;
 dynamic_source = g_strdup_printf (html_source, padding_nbsps->str);
 gtk_html_load_from_string (GTK_HTML (html), dynamic_source, -1);
 g_free (dynamic_source);
 g_string_free (padding_nbsps, TRUE);
 gtk_timeout_add (50, (GtkFunction) load_content, html);
 return FALSE;
}
and replace the gtk_html_load_from_string call with load_content.
并且改调用gtk_html_load_from_string为调用load_content.
The following picture illustrates Example 4 running (smiley picture is shown in different positions related to different time).
下面的图片说明了例4运行情况(笑脸图片根据不同的时间显示在不同的位置)
 
Figure 4. Screenshot of running Example 4
How to avoid flickering
When you dynamically create HTML content for a GtkHTML widget which is then reloaded, in some cases (when you use the streaming API to load HTML content and write it in several pieces) you could experience unpleasant flickering. This is because GtkHTML was designed for progressive loads and tries to redraw its content progressively and thus repeatedly.
当你为GtkHTML组件动态创建HTML内容并重载时,在一些事情(当你使用流API去加载HTML内容并写人一部分时)上,你可能会遇到不愉快的闪烁问题。这是因为GtkHTML被设计成为逐步加载,此时它会试图逐步重绘所有内容,并且反复执行。
There are two ways to reduce or completely avoid GtkHTML’s flickering.
这里有两个方法去减少或者完全避免GtkHTML的闪烁
The first way is to set GtkHTML to blocking mode, where first (re)draw will happen after all streams are fully loaded. This means that GtkHTML will wait until all of the content and inlined images are loaded. Also note that blocking mode helps in cases where the IMG HTML tag does not have WIDTH and HEIGHT attributes specified.
第一个方法是当所有的流数据全部加载以后的第一次(重复)绘制将要发生时将GtkHTML设置成锁定模式,这个意思是指GtkHTML将要等待,直 到所有内容和内联图片的全部加载。还需要注意,在IMG HTML标签没有指定长度和高度属性时的情况使用这种锁定模式能获得很大帮助。
To switch GtkHTML to blocking mode use
选定GtkHTML锁定模式使用:
void gtk_html_set_blocking (GtkHTML  *html,
       gboolean  block);
The Second method is to preload images ahead. This is extremely useful in cases where you are reloading HTML content many times and repeatedly use the same images. To preload images, use
第二种模式是开始时预处理图片。当你重载HTML内容许多次并且反复使用同一图片时这是很有用的。反复使用图片,使用
void gtk_html_image_preload (GtkHTML     *html,
                             const gchar *url);
Once you don’t need a preloaded image and want resources to be freed, use
当你不需要一张已经反复使用的图片并且要释放资源,使用
void gtk_html_image_unref (GtkHTML     *html,
                           const gchar *url);
Image preloading avoids image reloading and redrawing which saves your CPU cycles and also reduces redrawing to a minimum.
图片预加载避免图片重载和重绘时能节省你的CPU运算周期,也能最大限度地避免重绘
If we return to the previous example, reducing flicker and unneeded image reloading and redrawing is achieved by inserting the following code before the load_content call
如果我们返回到上一个例子,在调用load_content被调用以前插入以下代码将实现减少闪烁,不需要的图片的重载与重绘。
 gtk_html_image_preload (GTK_HTML (html), "file:smiley.png");
 gtk_html_set_blocking (GTK_HTML (html), TRUE);

--------------------------------------------------------------------------------
Current state and future
当前状态和将来
At the time of writing this tutorial, GtkHTML is available in the 1.0 stable version and development continues on the unstable 1.1 version. Some of the features indicated here are only available in the unstable version, namely the functions used to reduce flicker and images reloading.
在写这个教程时,GtkHTML可用的稳定版本是1.0,并且正在开发的不稳定版本是1.1。在这里提到的某些特征比如减少闪烁与图片重载的函数的使用仅仅可用在不稳定版本,
The stable version of GtkHTML can be downloaded from and unstable from . It is also possible to get gtkhtml sources directly from GNOME CVS, module gtkhtml, HEAD branch for unstable and gtkhtml-1-0-branch branch for stable. For more information about using CVS please visit http://developer.gnome.org/tools/cvs.html .
GtkHTML稳定版本能从 下载,不稳定版本从 。也可以直接从GNOME CVS获取gtkhtml源代码,gtkhtml的模块,进入不稳定的分支,以及稳定的gtkhtml-1-0-branch分支。更多相关信息请使用CVS访问http://developer.gnome.org/tools/cvs.html .
Today GtkHTML is implemented for GNOME 1.4. A port to GNOME 2.0 is planned. Development of GtkHTML is closely tied to Evolution development, so porting will most probably happen at the same time as the Evolution port.
如今GtkHTML将实施从GNOME1.4.A 移植到GNOME2.0的计划。与GtkHTML开发息息相关的是Evolution的开发,所以将要移植的时间可能发生在与Evolution移植的相同时间。
People sometimes ask about JavaScript and CSS support. Those features are not planned to be implemented. If you need those, look at GtkHTML2.
人们有时会问关于JavaScript和CSS支持,这些特性不在实施的计划中,如果你需要这些,请看GtkHTML2

--------------------------------------------------------------------------------
The END
GtkHTML is brought to you by:
Authors of KHTMLW widget, which has been used as base for GtkHTML
Josip A. Gracin
Martin Jones
Torben Weis
Authors
Jonas Borgstr
Anders Carlsson
Radek Doul
Larry Ewing
Miguel de Icaza
Ettore Perazzoli
Contributors
Martijn van Beers
Richard Hult
Elliot Lee
Havoc Pennington
Ariel Rios
and many more…I would like to thank my friend Jeff Stedfast for his corrections and my friend Jakub Steiner for a smiley image 
The END.
Posted in GtkHTML Tutorial.
阅读(1249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~