[ Usenet FAQs | Search | Web FAQs | Documents | RFC Index ]
Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page
Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 103) What if I have problems with the backspace/delete keys?
Next Document: 105) How can put Text in overstrike mode instead of insert?
-
Search the FAQ Archives
Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page
Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 103) What if I have problems with the backspace/delete keys?
Next Document: 105) How can put Text in overstrike mode instead of insert?
104) How can I use a file as the text source for a Text widget?
Answer: You can't do it directly like you can with the Athena Text widget.
Instead, read the text from the file into a string (all of it!) and then use
XmTextSetString. Alternatively, read blocks of characters and add them at the
end of the text using XmTextInsertString. The following is an excerpt from
Dan Heller's "file_browser.c":
/* file_browser.c -- use a ScrolledText object to view the
* contents of arbitrary files chosen by the user from a
* FileSelectionDialog or from a single-line text widget.
*/
struct stat statb;
/* make sure the file is a regular text file and open it */
if (stat(filename, &statb) == -1 ||
(statb.st_mode & S_IFMT) != S_IFREG ||
!(fp = fopen(filename, "r"))) {
if ((statb.st_mode & S_IFMT) == S_IFREG)
perror(filename); /* send to stderr why we can't read it */
else
fprintf(stderr, "%s: not a regular file\n", filename);
XtFree(filename);
return;
}
/* put the contents of the file in the Text widget by allocating
* enough space for the entire file, reading the file into the
* allocated space, and using XmTextFieldSetString() to show the file.
*/
if (!(text = XtMalloc((unsigned)(statb.st_size+1)))) {
fprintf(stderr, "Can't alloc enough space for %s", filename);
XtFree(filename);
fclose(fp);
return;
}
if (!fread(text, sizeof(char), statb.st_size+1, fp))
fprintf(stderr, "Warning: may not have read entire file!\n");
text[statb.st_size] = 0; /* be sure to NULL-terminate */
/* insert file contents in Text widget */
XmTextSetString(text_w, text);
Top Document: Motif FAQ (Part 4 of 9)
Previous Document: 103) What if I have problems with the backspace/delete keys?
Next Document: 105) How can put Text in overstrike mode instead of insert?
Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Part8 - Part9 - Single Page
[ Usenet FAQs | Search | Web FAQs | Documents | RFC Index ]
Send corrections/additions to the FAQ Maintainer:
kenton@rahul.net (Ken Lee)
Last Update July 24 2008 @ 00:14 AM