|
用Xlib库进行基本图形编程
目录
1、前言
2、X Window系统的客户服务器模式
3、GUI编程-同步化的编程模型
4、基本的Xlib概念
1、X Display
2、GC-图形上下文
3、对象句柄
4、Xlib 结构体的内存分配
5、事件
5、编译给予Xlib的程序
6、打开和关闭一个连接到X服务器的连接
7、检查关于Display的基本信息
8、创建一个简单的窗口-我们的“hello world”程序
9、在窗口中绘画
1、分配图形上下文(GC)
2、基本绘图-点,线,框,圆...
10、X事件
1、使用事件遮罩给事件型别注册
2、接收事件-撰写事件循环
3、暴露事件
4、获得用户输入
1、鼠标按钮点击和释放事件
2、鼠标移动事件
3、鼠标指针进入和离开事件
4、键盘焦点
5、键盘按下和释放事件
5、X事件-完整的例子
11、处理文本和字体
1、字体结构
2、载入字体
3、把字体赋给图形上下文
4、在窗口中绘出文本
12、窗口阶层
1、根,父和子窗口
2、事件传播
13、和窗口管理器交互
1、窗口属性
2、设置窗口名和图标名
3、设置最佳窗口尺寸
4、设置窗口管理器的杂项
5、设置应用程序的图标
14、简单窗口操作
1、映射和取消映射窗口
2、在屏幕上移动窗口
3、缩放窗口
4、改变窗口的堆叠次序-提高会放低
5、标识会取消标识窗口
6、获得窗口的信息
15、使用颜色来绘出彩虹
1、色表
2、分配和释放色表
3、分配和释放单个颜色
4、用颜色绘画
16、X Bitmap和Pixmap
1、什么是X Bitmap?什么又是X Pixmap?
2、从文件中载入Bitmap
3、在窗口中画出Bitmap
4、创建Pixmap
5、在窗口中画出Pixmap
6、释放Pixmap
17、鼠标光标的杂事
1、创建和释放鼠标光标
2、设置窗口的鼠标光标
Preface
前言
This tutorial is the first in a series of "would-be" tutorials about graphical programming
in the X window environment. By itself, it is useless. A real X programmer usually uses a
much higher level of abstraction, such as using Motif (or its free version, lesstiff), GTK,
QT and similar libraries. However, we need to start somewhere. More than this, knowing how
things work down below is never a bad idea.
该教程是“可能会有”的关于在X Window环境下进行图形化编程的教程的第一个系列。其自身是用处不大
的。一个真正的X程序员一般使用一个更高层次的抽象,比如用Motif(或者它的的免费版本,lesstiff),
GTK,QT和类似的库。然而,我们需要从一个地方开始入手。不仅如此,知道表象之下的事情是如何工作
的决不会是坏的主意。
After reading this tutorial, one would be able to write very simple graphical programs, but
not programs with a descent user interface. For such programs, one of the previously
mentioned libraries would be used.
在读完本教程后,你可能能够些简单的图形程序,但是那不会是一个有良好用户界面的程序。对于这样的
程序,也许就要用到上述的库中的某一个。
The Client And Server Model Of The X Window System
X Window系统的客户和服务器模型
The X window system was developed with one major goal - flexibility. The idea was that the
way things look is one thing, but the way things work is another matter. Thus, the lower
levels provide the tools required to draw windows, handle user input, allow drawing graphics
using colors (or black and white screens), etc. To this point, a decision was made to
separate the system into two parts. A client that decides what to do, and a server that
actually draws on the screen and reads user input in order to send it to the client for
processing.
X window系统开发之初有一个最大的目标-灵活性。想法是这样的东西看上如如何一回事,东西如何工作
的又是另外一回事。因而,底层提供在画窗口,处理用户输入,允许使用颜色画图形(或者黑白屏幕)等动
作中需要的工作。就这点决定了把系统分为两个部分。客户决定作什么,而服务器实际在屏幕上画图并读
出用户输入以发给客户进行处理。
This model is the complete opposite of what one is used to when dealing with clients and
servers. In our case, the user seats near the machine controlled by the server, while the
client might be running on a remote machine. The server controls the screen, mouse and
keyboard. A client may connect to the server, request that it draws a window (or several
windows), and ask the server to send it any input the user sends to these windows. Thus,
several clients may connect to a single X server - one might be running an email software,
one running a WWW browser, etc. When input it sent by the user to some window, the server
sends a message to the client controlling this window for processing. The client decides
what to do with this input, and sends the server requests for drawing in the window.
这个模型正好和人们在客户和服务器中所习惯的行为相反。在我们的例子,用户坐在由服务器控制的机器
旁边,而客户可能运行于一个远程的机器上。服务器控制屏幕,鼠标和键盘。客户可能连接到了服务器,
发出画一个(或者多个)窗口的请求,并要求服务器把任何用户发送给这些窗口的输入给他。因而,几个客
户可能连接到了同一个X服务器-一个可能在运行email软件,一个可能在运行WWW浏览器,等等。当由用
户发送输入给某些窗口时,服务器向控制这些窗口的客户发送消息以供处理。客户决定对输入作什么,并
给服务器发送请求来在窗口中绘图。
The whole session is carried out using the X message protocol. This protocol was originally
carried over the TCP/IP protocol suite, allowing the client to run on any machine connected
to the same network that the server is. Later on the X servers were extended to allow
clients running on the local machine more optimized access to the server (note that an X
protocol message may be several hundreds of KB in size), such as using shared memory, or
using Unix domain sockets (a method for creating a logical channel on a Unix system between
two processes).
整个会话过程是用X消息协议执行的。该协议最初时由TCP/IP协议包执行的,允许客户运行于任何和服务
器连接在相同网络上的机器上。后来,X服务器被扩展为允许客户运行在本地机器上更优的访问服务器(注
意到X协议消息可能有几百KB那么大),比如使用共享内存,或者使用Unix域sockets(一个在Unix系统上的
两个进程间创建逻辑通道的方法)。
GUI programming - the Asynchronous Programming Model
GUI编程-同步化的编程模型
Unlike conventional computer programs, that carry some serial nature, a GUI program usually
uses an asynchronous programming model, also known as "event-driven programming". This means
that that program mostly sits idle, waiting for events sent by the X server, and then acts
upon these events. An event may say "The user pressed the 1st button mouse in spot x,y", or
"the window you control needs to be redrawn". In order for the program to be responsive to
the user input, as well as to refresh requests, it needs to handle each event in a rather
short period of time (e.g. less than 200 milliseconds, as a rule of thumb).
不同于包含某种顺序化执行内质的传统的计算机程序。GUI程序通常使用同步化的编程模型,也被称为“
事件驱动编程”。这个意味着程序大部分时候时闲着的,等待由X服务器发送的事件,然后根据这些事件
作出反应。事件可能时”用户在点x,y处按下第一个按钮“,或者时”你控制的串口需要重画“。为了程
序能够响应用户输入以及刷新请求,它需要在一个相当短的时间内处理每个事件(比如作为一个大体的规
则,小于200毫秒)。
This also implies that the program may not perform operations that might take a long time
while handling an event (such as opening a network connection to some remote server, or
connecting to a database server, or even performing a long file copy operation). Instead, it
needs to perform all these operations in an asynchronous manner. This may be done by using
various asynchronous models to perfor |
|