📄 ch34.htm
字号:
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<UL>
<LI><A HREF="#Heading1">- 34 -</A>
<UL>
<LI><A HREF="#Heading2">Motif Programming</A>
<UL>
<LI><A HREF="#Heading3">Writing Motif Applications</A>
<LI><A HREF="#Heading4">Naming Conventions</A>
<LI><A HREF="#Heading5">TIP</A>
<LI><A HREF="#Heading6">Writing Your First Motif Application</A>
<LI><A HREF="#Heading7">NOTE</A>
<LI><A HREF="#Heading8">Listing</A>
<LI><A HREF="#Heading9">34.1. A sample Motif application.</A>
<LI><A HREF="#Heading10">TIP</A>
<LI><A HREF="#Heading11">Compiling This Application</A>
<LI><A HREF="#Heading12">The Widget Hierarchy</A>
<UL>
<LI><A HREF="#Heading13">Core</A>
<LI><A HREF="#Heading14">XmPrimitive</A>
<LI><A HREF="#Heading15">XmManager</A>
</UL>
<LI><A HREF="#Heading16">The Label Widget</A>
<LI><A HREF="#Heading17">Listing</A>
<LI><A HREF="#Heading18">34.2. How to use a Label Widget.</A>
<LI><A HREF="#Heading19">Strings in Motif: Compound Strings</A>
<LI><A HREF="#Heading20">TIP</A>
<LI><A HREF="#Heading21">The XmPushButton Widget Class</A>
<LI><A HREF="#Heading22">TIP</A>
<LI><A HREF="#Heading23">TIP</A>
<LI><A HREF="#Heading24">The XmToggleButton Widget Class</A>
<LI><A HREF="#Heading25">Listing</A>
<LI><A HREF="#Heading26">34.3. Using ToggleButton in Motif.</A>
<LI><A HREF="#Heading27">Convenience Functions</A>
<LI><A HREF="#Heading28">Listing</A>
<LI><A HREF="#Heading29">34.4. Sample convenience function for creating a label on
a form.</A>
<LI><A HREF="#Heading30">Listing</A>
<LI><A HREF="#Heading31">34.5. Creating a label.</A>
<LI><A HREF="#Heading32">The List Widget</A>
<LI><A HREF="#Heading33">Listing</A>
<LI><A HREF="#Heading34">34.6. Using List Widgets.</A>
<LI><A HREF="#Heading35">XmScrollBar</A>
<LI><A HREF="#Heading36">Listing</A>
<LI><A HREF="#Heading37">34.7. Using the Scale Widget.</A>
<LI><A HREF="#Heading38">Text Widgets</A>
<LI><A HREF="#Heading39">XmBulletinBoard Widgets</A>
<LI><A HREF="#Heading40">XmRowColumn Widgets</A>
<LI><A HREF="#Heading41">Listing</A>
<LI><A HREF="#Heading42">34.8. Using RowColumn Widgets.</A>
<LI><A HREF="#Heading43">XmForm Widgets</A>
<LI><A HREF="#Heading44">TIP</A>
<LI><A HREF="#Heading45">TIP</A>
<LI><A HREF="#Heading46">Designing Layouts</A>
<LI><A HREF="#Heading47">Listing</A>
<LI><A HREF="#Heading48">34.9. Setting up a simple hierarchy.</A>
<LI><A HREF="#Heading49">Menus</A>
<UL>
<LI><A HREF="#Heading50">Pop-Up Menus</A>
</UL>
<LI><A HREF="#Heading51">Listing</A>
<LI><A HREF="#Heading52">34.10. Setting up pop-up menus.</A>
<UL>
<LI><A HREF="#Heading53">The Menu Bar</A>
</UL>
<LI><A HREF="#Heading54">Listing</A>
<LI><A HREF="#Heading55">34.11. Creating a menu bar.</A>
<LI><A HREF="#Heading56">Listing</A>
<LI><A HREF="#Heading57">34.12. Creating menu bars with pull-down menu items.</A>
<UL>
<LI><A HREF="#Heading58">The Options Menu</A>
<LI><A HREF="#Heading59">Accelerators and Mnemonics</A>
</UL>
<LI><A HREF="#Heading60">TIP</A>
<LI><A HREF="#Heading61">Dialog Boxes</A>
<LI><A HREF="#Heading62">Listing</A>
<LI><A HREF="#Heading63">34.13. Code fragment to confirm quit command.</A>
<UL>
<LI><A HREF="#Heading64">Modes of a Dialog Box</A>
</UL>
<LI><A HREF="#Heading65">Events</A>
<UL>
<LI><A HREF="#Heading66">Expose Events</A>
<LI><A HREF="#Heading67">Pointer Events</A>
<LI><A HREF="#Heading68">Keyboard Events</A>
<LI><A HREF="#Heading69">Window Crossing Events</A>
<LI><A HREF="#Heading70">Event Masks</A>
</UL>
<LI><A HREF="#Heading71">Listing</A>
<LI><A HREF="#Heading72">34.14. Tracking a pointer.</A>
<LI><A HREF="#Heading73">Managing the Queue</A>
<LI><A HREF="#Heading74">TIP</A>
<UL>
<LI><A HREF="#Heading75">Work Procedures</A>
<LI><A HREF="#Heading76">Using Timeouts</A>
</UL>
<LI><A HREF="#Heading77">Listing</A>
<LI><A HREF="#Heading78">34.15. Setting up cyclic timers.</A>
<UL>
<LI><A HREF="#Heading79">Handling Other Sources</A>
</UL>
<LI><A HREF="#Heading80">NOTE</A>
<LI><A HREF="#Heading81">The Graphics Context</A>
<LI><A HREF="#Heading82">Drawing Lines, Points, Arcs, and Polygons</A>
<UL>
<LI><A HREF="#Heading83">Drawing a Line</A>
</UL>
<LI><A HREF="#Heading84">Listing</A>
<LI><A HREF="#Heading85">34.16. Drawing lines and points.</A>
<UL>
<LI><A HREF="#Heading86">Drawing a Point</A>
<LI><A HREF="#Heading87">Drawing an Arc</A>
</UL>
<LI><A HREF="#Heading88">Using Fonts and FontLists</A>
<LI><A HREF="#Heading89">The X Color Model</A>
<LI><A HREF="#Heading90">Listing</A>
<LI><A HREF="#Heading91">34.17. Convenience function for getting colors.</A>
<LI><A HREF="#Heading92">Pixmaps, Bitmaps, and Images</A>
<LI><A HREF="#Heading93">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 34 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Motif Programming</FONT></H2>
<P><I>by Kamran Husain</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>n Writing Motif Applications
<P>
<LI>Designing Layouts
<P>
<LI>Menus
<P>
<LI>Dialog Boxes
<P>
<LI>Managing the Queue
<P>
<LI>The Graphics Context
<P>
<LI>Drawing Lines, Points, Arcs, and Polygons
<P>
<LI>Using Fonts and FontLists
<P>
<LI>Pixmaps, Bitmaps, and Images
</UL>
<P><BR>
This chapter will cover the following topics:
<UL>
<LI>The basics of writing Motif applications for Linux
<P>
<LI>Special naming conventions in Motif and X
<P>
<LI>Writing and compiling your first Motif application
<P>
<LI>Revisiting the Widget hierarchy
<P>
<LI>Using labels and strings in Motif
<P>
<LI>Using various common Widgets
<P>
<LI>Designing layout
<P>
<LI>Using menus
<P>
<LI>Dialog boxes
<P>
<LI>Event handling and other sources of input
<P>
<LI>Colors in X
<P>
<LI>Drawing lines, points, arcs, and polygons
</UL>
<P>A question you might be asking is "Why include a topic on a development system
that you have to pay for, when just about everything for Linux is free?" Well,
if you want to develop any applications for the Common Desktop Environment (CDE),
you should know how to program Motif applications. Linux is a mature enough system
to enable you this luxury of building portable applications. (Plus, the $150 or so
you pay for the Motif license will well pay for itself if you can do the work at
home on your Linux system rather than commuting!)
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">Writing Motif Applications</FONT></H3>
<P>This chapter introduces you to writing Motif applications. The information here
will not be limited to writing applications for Linux alone, because the concepts
in this chapter can be applied to other UNIX systems as well.</P>
<P>In programming Motif applications, you have to get used to programming in an event-driven
environment. A typical C application runs from start to finish at its own pace. When
it needs information, the application looks for this information from a source such
as a file or the keyboard and (almost always) gets the information as soon as it
asks for it. If the information is not available when the application asks for it,
the application either waits for it or displays an error message. Also, the order
of the incoming data is important for such applications; pieces of data that are
out of sequence may cause the application to behave strangely.</P>
<P>In the case of event-driven programming, an application must wait for events on
an input queue. The queue orders all incoming events in the order they are received.
The first message to come in from one end of a queue is the first one to leave the
queue. (Such queues are often called FIFOs, for First In, First Out.) An event can
be anything from a mouse click to a timeout notification.</P>
<P>Because events can come in at any time, and in no predefined order, they are referred
to as asynchronous events. That is, the order and time of arrival of each event is
not deterministic. The application must wait for an event to occur and then proceed
based on that event. Thus the term event-driven programming.</P>
<P>In the case of the X Window system, each X Window application has one input queue
for all of its incoming events. The application must wait for events on this input
queue. Similarly, a server waits for an event from a client and then responds based
on the type of event received. This event handling and other aspects of X programming
are handled by a toolkit called <TT>XtIntrinsics</TT>, or <TT>Xt</TT> for short.</P>
<P>In <TT>Xt</TT>, an application will typically run in a loop forever. This loop
is called an event loop. An application enters the loop by calling a function <TT>XtAppMainLoop()</TT>.
While in this event loop, an application will always wait for an event, When the
application receives an event, the application handles the event itself or almost
always "dispatches" the event to a window or Widget.</P>
<P>A Widget registers functions that it wants called when a type of event is received.
This function is called a callback function. In most cases, a callback function is
independent of the entire application. For example, some Widgets will redraw themselves
when a pointer button is clicked in their display area. In this case, they would
register a redraw callback function on a button click.</P>
<P><TT>Xt</TT> also supports actions, which enable applications to register a function
with <TT>Xt</TT>. An action is called when one or more sequences of specific event
types are received. For example, pressing Ctrl-X would call the <TT>exit</TT> function.
The mapping of the action to an event is handled via a translation table within <TT>Xt</TT>.
Functions that handle specific events are referred to as event handlers.</P>
<P>Look at Figure 34.1 to see how the toolkit exists with Motif. As you can see from
the connections in the figure, an application can get to the core <TT>Xlib</TT> functions
through three means: via Motif, via the <TT>Xt</TT> library, or directly. This flexible
hierarchy gives you many options when developing Motif applications because you are
at liberty to take full advantage of all functions in all three libraries. <BR>
<BR>
<A HREF="../art/34/34lnx01.jpg"><B>Figure 34.1.</B></A><I> The toolkit hierarchy
for X, <TT>Xt</TT>, and Motif.</I>
<H3 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">Naming Conventions</FONT></H3>
<P>By default, most <TT>Xlib</TT> functions begin with the letter X, but you should
not always rely on this being true for all functions. Several macros and functions
in the X Window system do not begin with X. For example, the names <TT>BlackColor</TT>
and <TT>WhiteColor</TT> are not macros. In general, though, if a name in <TT>Xlib</TT>
begins with X, it's probably a function. If a name begins with a capital letter (A
through Z), it's a macro.</P>
<P>With <TT>Xt</TT>, the naming conventions get better, but only slightly. In <TT>Xt</TT>,
macros are not differentiated from functions in any way.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading5<FONT COLOR="#000077"><B>TIP: </B></FONT>Do not rely on the
name of a toolkit function to give you information about whether it's a macro. Read
the manual to be absolutely sure.
<HR>
.
</DL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -