rhl55.htm

来自「linux的初学电子书」· HTM 代码 · 共 1,689 行 · 第 1/3 页

HTM
1,689 行
字号

<FONT SIZE=4 COLOR="#FF0000"><B>Creating the Window</B></FONT></CENTER></H4>

<BR>

<P>Now create the window with the colormap and the best visual for PEX. The way to do this is as follows:

<BR>

<PRE>

<FONT COLOR="#000080">XStandardColormap colormap;

Colormap PEX_colormap;

/* open display as before */

screen = DefaultScreen(display);

visual = DefaultVisual(display,screen);

depth = DefaultDepth(display,screen);

status = GetStdColormap(display, screen,

visual, depth,

&amp;colormap);

if (status == True)

PEX_colormap = colormap;

blue = AllocNamedColor(display, PEX_colormap,

&quot;Blue&quot;, 0L);

white = AllocNamedColor(display, PEX_colormap,

&quot;White&quot;, 0L);

n = 0;

XtSetArg(args[n],XmNvisual, visual); n++;

XtSetArg(args[n],XmNdepth, depth); n++;

XtSetArg(args[n],XmNcolormap, colormap); n++;

XtSetArg(args[n],XmNallowResize, True); n++;

XtSetArg(args[n],XmNmapWhenManaged, False); n++;

XtSetArg(args[n],XmNbackground, blue); n++;

XtSetArg(args[n],XmNforeground, white); n++;

XtSetArg(args[n],XmNargc, cp_argc); n++;

XtSetArg(args[n],XmNargv, cp_argv); n++;

XtSetArg(args[n],XmNheight, 400); n++;

XtSetArg(args[n],XmNwidth, 400); n++;

toplevel = XtAppCreateShell(NULL, &quot;peX&quot;,

applicationShellWidgetClass, display, args, n);

mainWindow = XmCreateMainWindow(toplevel, &quot;mainWin&quot;, NULL, 0);

drawMe = XmCreateDrawingArea(mainWindow,

&quot;pexdraw&quot;, args, n);</FONT></PRE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>If you are unfamiliar with programming in Motif or X, please refer to <A HREF="tppmsgs/msgs0.htm#33" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl32.htm">Chapter 32</A>, &quot;Motif for Programmers.&quot;</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The application requires the toplevel shell. For this shell, you have to manually set its visual, screen, display, and colormap. Note that the resize resource is set to True and mapWhenManaged is set to False. You have to set these values via the XtArgs 
args array at creation time for this to work. The toplevel shell is where you create the main window to place your Motif widgets. After you have created the main window with the XmCreateMainWindow function call, you create the drawing area called drawMe on 
top of this window.

<BR>

<P>Next, you add callbacks to the drawing area widget to allow for redrawing. Add the callbacks to the drawing area for the following types of events: resize, expose, and input.

<BR>

<P>The complete code for a very simple application is shown in Listing 55.2.

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 55.2. A sample PEX application with Motif.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080">#include &lt;Xm/Xm.h&gt;

#include &lt;Xm/DrawingA.h&gt;

#include &lt;Xm/MainW.h&gt;

#include &lt;Xm/RowColumn.h&gt;

#include &lt;X11/PEX5/PEXlib.h&gt;

#include &lt;stdio.h&gt;

int bailout(char *str)

{

printf (&quot;\n %s&quot;, str);

exit(1);

}

int pex_set_line_color(Display *dpy, PEXRenderer p,

float r, float g, float b)

{

PEXColor pc;

pc.rgb.red = r;

pc.rgb.green = g;

pc.rgb.blue = b;

PEXSetLineColor(dpy, p, PEXOCRender, PEXColorTypeRGB, &amp;pc);

}

void doSamplePEX( Display *dpy, Window win, PEXRenderer ren)

{

PEXCoord coords[10];

PEXBeginRendering(dpy, win, ren);

PEXSetLineWidth(dpy, ren, PEXOCRender, 8.0);

pex_set_line_color(dpy, ren, 0.5, 0.5, 1.0);

coords[0].x = 0.3; coords[0].y = 0.3; coords[0].z = 0.0;

coords[1].x = 0.3; coords[1].y = 0.6; coords[1].z = 0.0;

coords[2].x = 0.6; coords[2].y = 0.6; coords[2].z = 0.0;

coords[3].x = 0.6; coords[3].y = 0.3; coords[3].z = 0.0;

PEXPolyline(dpy, ren, PEXOCRender, 4, coords);

pex_set_line_color(dpy, ren, 1.5, 0.5, 1.5);

coords[0].x = 0.3; coords[0].y = 0.3; coords[0].z = 0.5;

coords[1].x = 0.3; coords[1].y = 0.6; coords[1].z = 0.5;

coords[2].x = 0.6; coords[2].y = 0.6; coords[2].z = 0.5;

coords[3].x = 0.6; coords[3].y = 0.3; coords[3].z = 0.5;

PEXPolyline(dpy, ren, PEXOCRender, 4, coords);

PEXEndRendering(dpy, win, ren);

XFlush(dpy); /* important */

}

void quitBtn( Widget w, void *p, void *pp)

{

exit(0);

}

void drawBtn( Widget w, XmDrawingAreaCallbackStruct *sp,

XtPointer *client_data)

{

Dimension wd, ht;

PEXRenderer *rp;

if (sp == NULL) return;

switch(sp-&gt;reason)

{

case XmCR_EXPOSE:

if (sp-&gt;event-&gt;xexpose.count == 0)

{

rp = (PEXRenderer *)client_data;

doSamplePEX(XtDisplay(w), XtWindow(w), *rp);

}

break;

case XmCR_INPUT:

break;

}

}

int

pexInit(Display *dpy, PEXExtensionInfo **pexparms)

{

int err;

char errorMsg[PEXErrorStringLength+1];

PEXExtensionInfo *pex_info;

err = PEXInitialize(dpy, pexparms,

PEXErrorStringLength, errorMsg);

if (err) return False;

pex_info = (PEXExtensionInfo *)(*pexparms);

if( (pex_info-&gt;subset_info &amp; PEXImmediateMode) ||

((pex_info-&gt;subset_info &amp; 0xffff) == PEXCompleteImplementation))

{

return True;

}

return False;

}

int main(int argc, char *argv[])

{

Widget parent;

XtAppContext app_context;

int cp_size;

int cp_argc;

int cp_argv;

int status;

int screen;

int depth;

int fore;

int bkg;

int n;

Arg wars[20];

Visual *visual;

Colormap colormap;

XStandardColormap std_cmp;

PEXRendererAttributes pex_attr;

Display *dpy;

PEXExtensionInfo *pexParms;

PEXRenderer ren;

Widget mainw;

Widget filemenu;

Widget menubar;

Widget exitBtn;

Widget drawme;

XtToolkitInitialize();

app_context = XtCreateApplicationContext();

cp_argc = argc;

cp_size = argc * (sizeof(char *));

cp_argv = (char **)XtMalloc(cp_size);

memcpy(cp_argv, argv, cp_size);

dpy = XtOpenDisplay(app_context, NULL, NULL,

&quot;pexSample&quot;, NULL, 0, &amp;argc, argv);

if (dpy == (Display *) NULL)

bailout(&quot;Cannot open display&quot;);

status = pexInit(dpy,&amp;pexParms);

if (status == False) bailout(&quot;Cannot use PEX&quot;);

screen = DefaultScreen(dpy);

visual = DefaultVisual(dpy, screen);

depth = DefaultDepth(dpy,screen);

status = GetStdColormap(dpy, screen, visual, depth, &amp;std_cmp);

colormap = std_cmp.colormap;

bkg = BlackPixel(dpy,screen);

fore = WhitePixel(dpy,screen);

n = 0;

XtSetArg(wars[n], XmNvisual, visual); n++;

XtSetArg(wars[n], XmNdepth, depth); n++;

XtSetArg(wars[n], XmNcolormap, colormap); n++;

XtSetArg(wars[n], XmNbackground, bkg); n++;

XtSetArg(wars[n], XmNborderColor, fore); n++;

XtSetArg(wars[n], XmNargc, cp_argc); n++;

XtSetArg(wars[n], XmNargv,cp_argv); n++;

XtSetArg(wars[n], XmNallowResize, True); n++;

XtSetArg(wars[n], XmNmappedWhenManaged, False); n++;

XtSetArg(wars[n], XmNwidth, 300); n++;

XtSetArg(wars[n], XmNheight, 300); n++;

parent = XtAppCreateShell(NULL,&quot;pexSample&quot;,

applicationShellWidgetClass, dpy, wars, n);

n = 0;

mainw = XmCreateMainWindow(parent, &quot;mainwindow&quot;, wars, n);

n = 0;

XtSetArg(wars[n], XmNresizePolicy, XmRESIZE_ANY); n++;

XtSetArg(wars[n], XmNbackground, bkg); n++;

XtSetArg(wars[n], XmNborderColor, fore); n++;

drawme = XmCreateDrawingArea(mainw, &quot;da&quot;, wars, n);

XtAddCallback(drawme, XmNexposeCallback,

(XtCallbackProc)drawBtn, (XtPointer) &amp;ren);

XtAddCallback(drawme, XmNinputCallback,

(XtCallbackProc)drawBtn, (XtPointer) &amp;ren);

XtAddCallback(drawme, XmNresizeCallback,

(XtCallbackProc)drawBtn, (XtPointer) &amp;ren);

XtManageChild(drawme);

XtManageChild(mainw);

XtRealizeWidget(parent);

pex_set_color_approx(dpy,XtWindow(drawme), &amp;std_cmp, &amp;pex_attr);

XtMapWidget(parent);

ren = PEXCreateRenderer(XtDisplay(mainw), XtWindow(drawme),

PEXRAColorApproxTable, &amp;pex_attr);

if (ren == 0) {

printf(&quot;\n Bad renderer \n&quot;); exit (1);

}

XtAppMainLoop(app_context);

return(0);

}</FONT></PRE>

<P>A few points to note about Listing 55.2 are that the immediate mode was used for rendering on the screen. PEX-SI provides no support for double buffering. This is a serious bug because lack of double buffering hinders performance.

<BR>

<P>Even worse, the PEX-SI API assumes that the client desires an XClearArea on the window before each frame is drawn. This causes unnecessary flickering while the screen is cleared and redrawn on all primitive drawing calls. What should have been done was 
to provide an end-of-render procedure hook, with the default hook installed to do a clear area function call.

<BR>

<P>Individual vendors (because of market pressure) have provided their own solutions to the double buffering problem. (Most PHIGS workstations do double buffering. If you do immediate mode you get single buffering along with the PEX-SI's XClearArea call.)

<BR>

<P>A final word about adding too many features in an X server. The more you add to the X server, the more memory it chews up in your system. Unless you absolutely require PEX (or other) support, do not add it to your X server, especially if RAM is 8MB or 
less. The PEX support for Linux added about 420KB on my system, which is not a lot, but it does start adding up. Also, the overhead of PEX applications tend to make my 8MB, 486/33 somewhat slow when running PEX demos, which leads me to believe that PEX on 
Linux with less than 16MB is not worth the hassle. If you are serious about PEX on Linux, get a faster machine and put gobs of memory on it. The performance improved very dramatically on a 486/66 with 32MB of RAM.

<BR>

<BR>

<A NAME="E68E426"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Where To Look for More Information</B></FONT></CENTER></H3>

<BR>

<P>If you would like more information and examples of source code for PEX, check out these FTP sites:

<BR>

<UL>

<LI><A HREF="tppmsgs/msgs0.htm#85" tppabs="ftp://export.lcs.mit.edu/">export.lcs.mit.edu</A> in /R5contrib/contrib-R5fixes has several tar files of source code for PHIGS and PEX including a 3-D drawing program.

<BR>

<BR>

<LI><A HREF="tppmsgs/msgs1.htm#179" tppabs="http://www.x.org/">http://www.x.org</A> is a good place to start looking for more information on X.

<BR>

<BR>

</UL>

<BR>

<A NAME="E68E427"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>

<BR>

<P>This chapter has been a whirlwind tour of PEX. The topic of PEX could be a book in itself. In fact, there are several texts available that go into excruciating detail about PEX. You should have learned the following information from this chapter:

<BR>

<UL>

<LI>How to create a new X server with your specifications. Building your own server with LinkKit is far easier than trying to re-create the entire X distribution. There are fewer memory requirements, and the build takes up less disk space and time to 
compile and link.

<BR>

<BR>

<LI>How to install the new server. The LinkKit does this for you automatically and gives you options to clean your directories, and so on.

<BR>

<BR>

<LI>How to check for the extensions in the current server.

<BR>

<BR>

<LI>How to interface Motif with PEX.

<BR>

<BR>

<LI>The trade-offs of having too many extensions in X server, such as loss of memory and speed for added functionality.

<BR>

<BR>

</UL>

<P>Where to look for more information about PEX and Linux.<A NAME="I2"></A>

<P ALIGN=LEFT>

<A HREF="rhl54.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl54.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>

<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="rhl56.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl56.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>


</BODY></HTML>



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?