XVisual Interface Builder Programming Hints

XVisual Interface Builder Programming Hints

The purpose of this document is to give you some hints on programming with the XIB and how you can get objects to interact with each other.

This file is likely to grow in size as I think of other things to tell you about :)

Getting information from List Objects

The xvList object passes a pointer to an XawListReturnStruct as calldata to it's select callback. This struct has the following construction:

	
	struct { 
		String string;
		int list_index;
	}; 

To get information from it in a callback, use something like:


	void alist_callback(Widget aw, XtPointer client, XtPointer call)
	{
		XawListReturnStruct *listptr=(XawListReturnStruct *) call;
		int number=listptr->list_index;
		strcpy(buffer, listptr->string);
		...
	}

Basically, thats it. Remember that the widget will automatically hightlight the list element for you. Note also that the callback is NOT called if you un-highlight a previously selected list member by clicking on it again.

Linking xvDirList & xvFileList objects

To link a directory list to a file list so that the file list changes when the directory list is changed, use something like:



	// This is a xvDirList callback ...
	void dirlist_select(Widget aw, XtPointer client, XtPointer call)
	{
		char *temp=dirlist.GetSelectedItem(call, XVRELATIVEPATH);
		strcpy(pathbuffer, dirlist.Directory(temp));
		int dirstatus=dirlist.SetNewPath(pathbuffer, XVDIRLIST, FALSE);
		int filestatus=filelist.SetNewPath(pathbuffer, XVFILELIST,FALSE);
	}

The third parameter indicates whether hidden files should be listed and is a matter of personal choice.

Scrollbars

By default, button 2 (middle button on a 3-button mouse) allows you to smooth scroll the scrollbar's thumb. If you don't have a 3-button mouse, or you can't get the middle button to work, re-define the scrollbar's translation table to allow button 1 or 3 to work this way.

How can you set the translation table? Use the scrollbar's X_map event...


	void scroller_X_Map (...)
	{
		XtTranslations t;
		t=XtParseTranslationTable(
		   ": StartScroll(Continuous) MoveThumb() NotifyThumb() \n\
		    : MoveThumb() NotifyThumb()");
		scroller.OverrideTranslations(t);
	}

If you want to use button 3, simply substitute the '1' for a '3'.

Now you need to be able to read the position of the scrollbar, so use the jump callback:


	void scroller_jump(...)
	{
		float t= * (float *) calldata;
		// now t has a value between 0.0 and 1.0 - 
		// a percentage of the distance..
	}

You can then use this value to determine how much you have scrolled the thumb.

Using one callback for several buttons...

You can use the client data parameter of the xvButton or xvRepeater init method (or constructor) to get it to pass a string to the callback function. If you have several objects using the same callback, you then cast the client data XtPointer to a char * and strcmp the value to find out which button was pressed...

Using Toggles

If an xvToggle object is a member of a group, then you can often interrogate any member toggle of the group to find out which one is currently active.

Using Graphics and Graphics Contexts

Every xvWindow object supports a wide range of graphics methods. These include simple stuff like point plotting, line drawing, rectangle plotting, etc. When using graphics methods, you must also set up one or more Graphics Contexts which are simply control blocks which determine how graphics will be drawn, what colours to use, etc. All Graphics Context (GC) methods are prefixed GC and graphics methods are prefixed G.

GC colour methods such as GCSetForeground require a Pixel value to be passed to them, so you can use the stringToPixel method to convert colour name strings to Pixel values (it will allocate the colour in the ColourMap if it isn't already there)...


	window.GCSetForeground(window.stringToPixel("SkyBlue"));
This example sets the GC foreground value to the pixel value of SkyBlue in the colourmap.

Using bitmaps in an application

Objects such as buttons, labels, etc allow you to display a bitmap instead of text (or a bitmap and text as of version 1.0.3-BETA). There are some things you have to bear in mind when using bitmaps.

Firstly, the name of the bitmap determines how it will be interpreted. If the filename is of the format bitmapname.xbm then it will be interpreted as an ordinary X11 bitmap. If the filename is of the format bitmapname.xpm then it is assumed that this is a colour pixmap and is treated as such.

Secondly, the format of the file: for .xbm files, the standard X11 format is used... i.e.

Where the filename is bitmapname.xbm... the internal structure of the file will be..

and for .xpm files where the filename is bitmapname.xpm, the bitmapname_xpm label is used.

Note that with this version of the XIB, the files used must have the file format name .xbm or .xpm to be interpreted correctly.

Using Code modules in your applications

The XIB allows you to create code Modules in the File menu. When using these code segements, there are a couple of things you should bear in mind.

1. For each code module .cc file, declare a corresponding .h file which contains extern declarations for functions or variables that you want to export to the rest of the application. Any .h files that you declare are imported by the XIB into the main application header file.

2. If you want functions in your code modules to access data or objects in the application, include the following lines at the start of each .cc module...


	#include X11/xv/xv.h
	#include "appname.h"
Note that the first include line must also include the angle braces which are not shown here.

The first line includeds the xv header file and the second includes the application's own main header file. Strictly speaking, the appname.h file includes the xv.h file but you may want to include this line anyway...


Return to the XVisual Homepage


Steve Carrie. steve@phoenix.bim.napier.ac.uk

Last Update 24/05/96 at 10:00 hrs