[alt.toolkits.xview] Help! with scrolling lists and xv_create

sz@cci632.cci.com (Stephen Zehl) (05/25/91)

-

    I'm looking for a little help with scrolling lists under OpenWindows 
(XView).  I'm using DevGuide to generate C code stubs of scrolling lists, 
I'm having trouble getting the information into the lists.

    Volume Seven of the XView Programming Manual gives two examples of how
to hard code these choices into the code on pages 155 and 157.  As shown
below:

    xv_create(panel, PANEL_LIST, 
	PANEL_LIST_STRINGS,  "One", "Two", "Three", NULL,
	NULL);

	This is fine if you know what you want in the list ahead of time, but
what if you want to read in a list of files from a directory?  I figured I
could just substitute a pointer to an array of strings in place of the
strings themselves as shown below:

    static char	*mylist[3] = { "One", "Two", "Three" };

    xv_create(panel, PANEL_LIST, 
	PANEL_LIST_STRINGS,  *mylist, NULL,
	NULL);

	This will give me the first string in the list, but not the rest.
And if I tack the NULL onto the list itself and remove it from after *mylist,
I get a segmentation fault and a core in my lap.

	If anyone out there has worked on this, Please, Please send me some
e-mail with an explaination of how you did this.  I'd really appreciate it.

	Thanks again,
				Steve.

 CCCC  CCCC IIII     Stephen Zehl           Internet: sz@cci.com
CC    CC     II   Computer Consoles. Inc.     Usenet: uupsi!cci632!sz
CC    CC     II    Rochester, New York.         or  : uunet!ccicpg!cci632!sz
 CCCC  CCCC IIII       DISCLAIMER: I speak for myself, not for my employer.

	

brentb@montagne.Eng.Sun.COM (Brent Browning) (05/28/91)

In article <1991May24.211132.10282@cci632.cci.com> sz@cci632.cci.com (Stephen Zehl) writes:
>	This is fine if you know what you want in the list ahead of time, but
>what if you want to read in a list of files from a directory?  I figured I
>could just substitute a pointer to an array of strings in place of the
>strings themselves as shown below:
>
>    static char	*mylist[3] = { "One", "Two", "Three" };

   PANEL_LIST_STRINGS must *really* have a NULL terminated list
of strings, *not* a char ** pointing at a NULL terminated list
of strings.

   I've included a sample program that does just what you
need to do.  Type in a filename, then hit the load button.  It
will load that file into the scrolling list.  Hitting the
save button saves it out to a file.

   This particular example was written for someone else.  It
also saves the contents of the scrolling list when the application
is quit from the window menu.

Brent Browning			Internet: brentb@Eng.Sun.COM
Sun Microsystems, Inc		UUCP: ...!sun!brentb
2550 Garcia Ave. MTV 01-40	Phone: (415) 336-5573
Mountain View, CA 94043

--------8<--------8<---- Cut here ----8<--------8<--------
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <xview/xview.h>
#include <xview/panel.h>

void		load_file();
void		save_file();
void		load_list_from_file();
void		save_list_to_file();
Notify_value	destroy_func();

Xv_opaque	File_name;
Xv_opaque	File_list;

main(argc, argv)
	int	argc;
	char	**argv;
{
	Xv_opaque	frame;
	Xv_opaque	panel;

	xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
	
	frame = xv_create(NULL, FRAME, NULL);
	notify_interpose_destroy_func(frame, destroy_func);

	panel = xv_create(frame, PANEL,
		PANEL_LAYOUT, PANEL_VERTICAL,
		NULL);

	File_name = xv_create(panel, PANEL_TEXT,
		PANEL_VALUE_DISPLAY_LENGTH, 30,
		PANEL_VALUE_STORED_LENGTH, 80,
		PANEL_LABEL_STRING, "File:",
		PANEL_VALUE, "TEST",
		NULL);
	
	File_list = xv_create(panel, PANEL_LIST,
		PANEL_LIST_WIDTH, 250,
		PANEL_LIST_DISPLAY_ROWS, 10,
		PANEL_CHOOSE_ONE, TRUE,
		PANEL_CHOOSE_NONE, FALSE,
		NULL);

	xv_set(panel, PANEL_LAYOUT, PANEL_HORIZONTAL, NULL);

	xv_create(panel, PANEL_BUTTON,
		PANEL_NEXT_ROW, -1,
		PANEL_LABEL_STRING, "Load From File",
		PANEL_NOTIFY_PROC, load_file,
		NULL);

	xv_create(panel, PANEL_BUTTON,
		PANEL_LABEL_STRING, "Save To File",
		PANEL_NOTIFY_PROC, save_file,
		NULL);

	window_fit(panel);
	window_fit(frame);
	xv_main_loop(frame);
}

void
load_file(item, event)
	Panel_item	item;
	Event		*event;
{
	load_list_from_file();
}

void
save_file(item, event)
	Panel_item	item;
	Event		*event;
{
	save_list_to_file();
}

Notify_value
destroy_func(frame, status)
	Frame		frame;
	Destroy_status	status;
{
	if (status == DESTROY_CLEANUP)
		save_list_to_file();

	return notify_next_destroy_func(frame, status);
}

void
load_list_from_file()
{
	int	i = 0;
	FILE	*fp;
	char	buf[MAXPATHLEN];
	char	*file = (char *)xv_get(File_name, PANEL_VALUE);

	if ((fp = fopen(file, "r")) == NULL) {
		perror(file);
		return;
	}

	for (i = 0; fgets(buf, MAXPATHLEN, fp); i++) {
		buf[strlen(buf)-1] = '\0';

		xv_set(File_list, PANEL_LIST_INSERT, i,
				  PANEL_LIST_STRING, i, buf,
				  NULL);
	}

	fclose(fp);
}

void
save_list_to_file()
{
	int	i;
	int	n;
	FILE	*fp;
	char	*file = (char *)xv_get(File_name, PANEL_VALUE);

	if ((fp = fopen(file, "w")) == NULL) {
		perror(file);
		return;
	}

	n = xv_get(File_list, PANEL_LIST_NROWS);

	for (i = 0; i < n; i++) {
		fprintf(fp, "%s\n",
			(char *)xv_get(File_list, PANEL_LIST_STRING, i));
	}

	fclose(fp);
}

--
Brent Browning			Internet: brentb@Eng.Sun.COM
Sun Microsystems, Inc		UUCP: ...!sun!brentb
2550 Garcia Ave. MTV 01-40	Phone: (415) 336-5573
Mountain View, CA 94043