<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <!-- $Id: PROGRAMMING_TIPS.html,v 1.6 2001/05/05 10:23:30 amai Exp $ --> <!-- initially created from "toplevel"/PROGRAMMING_TIPS --> <HEAD> <TITLE>PROGRAMMING TIPS</TITLE> </HEAD> <body bgcolor="#ffffff" text="#000000"> <H1 ALIGN=CENTER> Programming Tips </H1> This is a start at a list of programming tips for Motif - not LessTif specific. It's not intended to be a comprehensive guide, but rather a collection of useful techniques that are often overlooked and can really speed Motif development and execution of Motif programs. <P> Since it's so short at the moment, there's not much in the way of overall organization, but as the list grows, we'll fix that. <HR> <OL> <LI> <BIG>What's the preferred way to specify arguments (i.e. XtSetArg, etc)?</BIG> <BR> There are two answers to this question depending on if the resources actually need to be specified before/during creation, or if they can be specified after creation. For resources which must be set prior to creation, the preferred method is this: <PRE> int n; Arg args[10]; Widget button; n=0; XtSetArg(args[n], XmNx, 100); n++; XtSetArg(args[n], XmNy, 120); n++; button = XmCreatePushButton(parent, "Button", args, n); </PRE> The advantage of this is that you don't have to keep count of the arguments yourself, which is prone to error. Note that you can't use args[n++] because this is a macro which will be expanded to have more than one n++ in it. A disadvantage to this is that you may overflow your static args array if you add to many args. Fortunately, when this happens it almost always causes immediate problems (i.e. a core dump) which means that the error will usually be caught fairly quickly. For resources which may be set after creation, the preferred method as follows: <PRE> button = XmCreatePushButton(parent, "Button", NULL, 0); XtVaSetValues(button, XmNx, 100, XmNy, 120, NULL); </PRE> This eliminates any possibility of overflowing the Arg array, and is easier to type and read anyway. This method should be used for all resources except those which must be set during widget creation. <P> <LI> <BIG>When should widgets be managed?</BIG><BR> Manager widgets should not be managed until *after* all the children are added. This is because geometry calculations are expensive operations (timewise) and if the manager widget is managed it must re-negotiate the geometry of all its children after each new child is added, instead of doing it only once after all children are added. </OL> <!-- ++++++++++++++++++++++ Generic HTML footer ++++++++++++++++++++++ --> <HR> <address> <a href="http://validator.w3.org/check/referer"><img src="images/vh32.jpg" height=31 width=88 align=right border=0 alt="Valid HTML 3.2!"></a> <a href="feedback.html">Feedback</a><br> <SMALL> Last modified on $Date: 2001/05/05 10:23:30 $ </SMALL> </address> </BODY> </HTML>