-Q2.A.3- set X11 resources for a wish application in an app-defaults file?

From: -II-  Tk Questions and Answers - How can I:

A2.A.3. Read the documentation for the option command.
Then you should consider something like the following - assume the program
name is xwf.

The following are two general purpose functions to put into a library:

# envVal envValName
#   Looks up the envValName environment variable and returns its
#   value, or {} if it does not exists
proc envVal {envValName} {
  global env
  if [info exists env($envValName)] {return $env($envValName)} {return {}}
}

# loadAppDefaults classNameList ?priority?
#   Searches for the app-default files corresponding to classNames in
#   the order specified by X Toolkit Intrinsics, and loads them with
#   the priority specified (default: startupFile).
proc loadAppDefaults {classNameList {priority startupFile}} {
  set filepath "[split [envVal XUSERFILESEARCHPATH] :] \
		[envVal XAPPLRESDIR] \
		[split [envVal XFILESEARCHPATH] :] \
		/usr/lib/X11"
  foreach i $classNameList {
    foreach j $filepath {
      if {[file exists $j/$i]} {
	option readfile $j/$i $priority; break
      }
    }
  }
}

# Now, here is what you would put into xwf:

option add Tk.BoldFont "*-lucida sans-Bold-R-Normal-*-100-*" widgetDefault
loadAppDefaults {xwf XWF} userDefault

This sets a program default, then load any defaults specified in the user's
default resources and finally any site or general app-defaults resource.  
Of course, you would want to add some xwf command line handling to allow 
the user to override things at execution time.

Chris Milam (address unknown) contributes the following modification
of loadAppDefaults which follows the X11R5 method of merging
app-default files from several sources.

# loadAppDefaults classNameList ?priority?
#   Searches for the app-default files corresponding to classNames in
#   the order specified by X Toolkit Intrinsics (R5), and loads them with
#   the priority specified (default: startupFile).
proc loadAppDefaults {classNameList {priority startupFile}} {
  set lang [envVal LANG]
  if {[string length $lang] > 0} { set lang /$lang }
  set filepath "\
                /usr/lib/X11${lang}/app-defaults \
                [split [envVal XFILESEARCHPATH] :] \
                [envVal XAPPLRESDIR]${lang} \
                [split [envVal XUSERFILESEARCHPATH] :] \
                "
  foreach i $classNameList {
    foreach j $filepath {
      if {[file exists $j/$i]} {
        option readfile $j/$i $priority;
      }
    }
  }
}

Parent document is top of "FAQ: comp.lang.tcl Tk Toolkit Usage Questions And Answers (1/1)"
Previous document is "-Q2.A.2- ,using a machine with less than 8 bit color, run?"
Next document is "-Q2.A.4- specify bitmap patterns on the command line instead of just as a file name?"