Xref: utzoo comp.windows.open-look:1529 alt.toolkits.xview:193 comp.windows.misc:2077 comp.windows.x:36888 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!sun-barr!newstop!exodus!montagne.Eng.Sun.COM!brentb From: brentb@montagne.Eng.Sun.COM (Brent Browning) Newsgroups: comp.windows.open-look,alt.toolkits.xview,comp.windows.misc,comp.windows.x Subject: Re: Help! with scrolling lists and xv_create Keywords: scrolling lists, xv_create Message-ID: <14063@exodus.Eng.Sun.COM> Date: 28 May 91 06:17:43 GMT References: <1991May24.211132.10282@cci632.cci.com> Sender: news@exodus.Eng.Sun.COM Followup-To: comp.windows.open-look Organization: Sun Microsystems, Mt. View, Ca. Lines: 169 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 #include #include #include #include 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