[ Previous | Next | Contents | Glossary | Home | Search ]
Motif 2.1 Widget Writer's Guide



Accessing Traits

As mentioned earlier in this chapter, there are two general ways for a widget to access the trait information of another widget:

  1. A widget can ask another widget if it holds a certain trait.

  2. Assuming that a widget does hold a certain trait, another widget can call that widget's trait methods.

    The following subsections describe both ways.

  3. Determine if a Widget Holds a Particular Trait

    Use the XmeTraitGet function to determine if a specified widget holds a certain trait. This function returns NULL if the specified widget does not hold the trait, and a non- NULL value (a trait record) if the widget does hold the trait. For example, a parent widget can use the following code to determine if its child widget holds the XmQTaccessTextual trait:

    if ( XmeTraitGet((XtPointer)a_widget_class, XmQTaccessTextual))
      /* Yes, a_widget_class renders a primary text block. */
    else
      /* a_widget_class does not render a primary text block.
    */

    Call Another Widget's Trait Method

    If a widget does hold a trait, the XmeTraitGet function returns a pointer to the appropriate trait structure variable. Another widget can use this returned pointer to call trait methods. For example, suppose that a parent of ExmString needs to determine the preferred string format of ExmString. The following code from the parent does just that:

    XmAccessTextualTrait  childs_trait_record;
    int                   preferred_string_format_of_child;
     
     /* Get a pointer to the trait structure variable. */
       childs_trait_record =
                            (XmQTaccessTextual)
    XmeTraitGet((XtPointer)a_widget_class,                                  
                                                                            
                                                                            
                                     XmQTaccessTextual);
     
     /* Use the returned pointer to call the child's
                    preferred_format trait method. */
    preferred_string_format_of_child =
         childs_trait_record->preferred_format((Widget)parent_widget);

    Note that, in some cases, the widget holding the trait has not defined a particular trait method; that is, the trait method is set to NULL. For that reason, the following code is an improvement over the previous example:

    /* Does this trait method exist? */
    if      (childs_trait_record->preferred_format !=
            (XmAccessTextualTraitRec) NULL)   {
    /* It does exist, so call the trait method. */
            preferred_string_format_of_child =
                   
    childs_trait_record->preferred_format((Widget)parent_widget);
            }

    [ Previous | Next | Contents | Glossary | Home | Search ]