home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / SoundAndMusic / Hyperupic / Hyperupic.app / Help.m < prev    next >
Text File  |  1992-08-10  |  12KB  |  381 lines

  1. /* 
  2.  * Help.m, a help object to manage and display RTF help files.
  3.  * The help object owns its own nib section "Help.nib" which has a
  4.  * Help panel - with an NXBrowser to display the help topics, and
  5.  * a scrolling text view to display the help files.  The help files are
  6.  * all stored as RTF text files in a directory called Help within the
  7.  * app wrapper.  At init time, the Help object loads the browser with 
  8.  * names of all the files found in the Help directory.  When a name is
  9.  * chosen from the browser, the help object opens a stream on that file
  10.  * and read the rich text into the text object. The help object also
  11.  * responds to request for context-sensitive help, by trying to find an
  12.  * appropriate help file for the view that was moused down in.  See the
  13.  * helpForObject: method for more detailed explanation of that.
  14.  * This object is a useful addition to any program, because all users
  15.  * appreciate help!
  16.  *
  17.  * Author: Julie Zelenski, NeXT Developer Support
  18.  * You may freely copy, distribute and reuse the code in this example.  
  19.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  20.  * its fitness for any particular use.
  21.  */
  22.  
  23. #import "Help.h"
  24. #import "HyperupicApp.h"
  25. #import <appkit/Button.h>
  26. #import <appkit/Cell.h>
  27. #import <appkit/Matrix.h>
  28. #import <appkit/MenuCell.h>
  29. #import <appkit/NXBrowser.h>
  30. #import <appkit/NXBrowserCell.h>
  31. #import <appkit/ScrollView.h>
  32. #import <appkit/Text.h>
  33. #import <dpsclient/wraps.h>
  34. #import <sys/dir.h> //for getdirentries()
  35. #import <libc.h>     
  36.  
  37.  
  38. @implementation Help:Object
  39.  
  40.  
  41. - init 
  42. /* For newly created help object, loads the nib section with the Help
  43.  * panel which has the topics browser and a scrolling text view for 
  44.  * displaying help files.  Gets the appDirectory from NXApp, finds help 
  45.  * directory in app wrapper.
  46.  */
  47. {
  48.     sprintf(helpDirectory,"%s/%s",[NXApp appDirectory],"Help");
  49.     sprintf(noHelpFile,"%s/%s",helpDirectory,"No Help.rtf");
  50.     helpPanel = [NXApp loadNibSection:"Help.nib" owner:self];
  51.     return self;
  52. }
  53.  
  54. - setHelpBrowser:anObject;
  55. /* Sets the helpBrowser outlet, and calls on the browser to load up.
  56.  */
  57. {
  58.     helpBrowser = anObject;
  59.     [helpBrowser setDelegate:self];
  60.     [helpBrowser loadColumnZero];
  61.     return self;
  62. }
  63.  
  64. /* TARGET/ACTION METHODS */
  65.  
  66. - generalHelp:sender;
  67. /* This is the target/action method for the "Help" menu item.  This method 
  68.  * will show the "general help" file.
  69.  */
  70. {
  71.     [self showHelpFile:"01-Introduction"];
  72.     return self;
  73. }
  74.  
  75. - browserHit:sender
  76. /* This is the target/action method from the help topics browser.  When
  77.  * a help topic is selected, this method will show the help file for that
  78.  * topic.
  79.  */
  80. {   
  81.     [self showHelpFile:[[[sender matrixInColumn:0] selectedCell] stringValue]];
  82.     return self;
  83. }
  84.  
  85.  
  86. - print:sender;
  87. /* This method is called by the Print menu cell in the main menu.  It will 
  88.  * print the current help file.
  89.  */
  90. {
  91.     [[helpScrollView docView] printPSCode:sender];
  92.     return self;
  93. }
  94.  
  95.  
  96. /* HELP METHODS */
  97.  
  98. - helpForWindow:window;
  99. /* If this window is the main menu, it will ask to display the help file
  100.  * for "Main Menu." (I didn't want to call the Main Menu help file "BusyBox 
  101.  * Menu")  Else it gets the help file for <window title> <window name>
  102.  * "Info Panel" or "Document Menu" for example.
  103.  */
  104. {
  105.     char filename[MAXPATHLEN];
  106.  
  107.     if (window == [NXApp mainMenu])
  108.         sprintf(filename,"Main Menu");
  109.     else 
  110.         sprintf(filename,"%s %s",[window title],[window name]);
  111.     [self showHelpFile:filename];
  112.     return self;
  113. }
  114.  
  115. - helpForView:view atPoint:(NXPoint *)aPt;
  116. /* Gives help for the specified view, which was hit with a Control-mouseDown 
  117.  * at aPt.  Gives feedback to the user which view was hit by framing the
  118.  * bounds of the view with a gray rectangle.  This is done with instance
  119.  * drawing and erased after the helpfile is read and displayed.  If the view
  120.  * is a matrix, the method makes the effort to find the particular cell which
  121.  * was hit and to only frame that cell.
  122.  * This method calls on the method helpForObject which will figure out which
  123.  * help file to display.
  124.  */
  125. {  
  126.     int row,column;
  127.     NXRect b;
  128.     NXPoint p;
  129.     id cell = nil;
  130.  
  131.     [view getBounds:&b];
  132.     if ([view isKindOf:[Matrix class]]) {
  133.         p = *aPt;
  134.     [view convertPoint:&p fromView:nil];
  135.     if (cell = [view getRow:&row andCol:&column forPoint:&p])
  136.         [view getCellFrame:&b at:row :column];
  137.     }
  138.     [view lockFocus];
  139.     PSnewinstance();
  140.     PSsetinstance(YES);
  141.     PSsetgray(NX_DKGRAY);
  142.     NXFrameRectWithWidth(&b,1.0);
  143.     [[view window] flushWindow];
  144.     PSsetinstance(NO);
  145.     if (cell) 
  146.         [self helpForObject:cell];
  147.     else if ([[view window] contentView] == view) 
  148.         [self helpForWindow:[view window]];
  149.     else
  150.         [self helpForObject:view];
  151.     PSnewinstance();
  152.     [view unlockFocus];
  153.     return self;
  154. }
  155.  
  156.  
  157. - helpForObject:object;
  158. /* The method tries to cons together a file name that represents help
  159.  * for the given object.  It makes no assumptions about the tags or
  160.  * titles of the views, rather it employs a general strategy.  For many
  161.  * objects, the name is simply used ("ScrollView","TextField"). For Cells,
  162.  * it strips Cell from the name, for our purposes, TextFieldCell is
  163.  * the same as a TextField.  For Buttons (and ButtonCells), it uses icon + name
  164.  * (radio Button, popUp Button).  For MenuCells, it figures out where it is
  165.  * submenu or a item.  Items display help for their parent menu, (using 
  166.  * helpForWindow: method), submenus use title + menu (Format Menu, Find Menu).
  167.  * What is neat about this general scheme is that all views, windows, menu
  168.  * respond to the control-click, not just the ones I created. Bring up the 
  169.  * Print panel and control-click on the Resolution popup, and you will see 
  170.  * the help file for popups!  Control-click on the Save Panel, you get help for 
  171.  * Save Panel!  This is probably not the way that a real application would 
  172.  * implement help.  It happens to work for mine, because I want the same
  173.  * help file to be displayed for every popup list, the same file for every 
  174.  * button.  In your app, different button have various functions, and you 
  175.  * would want to display different help files for different buttons.  You 
  176.  * will probably devise a scheme using unique tags or titles, a more specific
  177.  * way to determine what help file to display.
  178.  */
  179. {
  180.     char filename[MAXPATHLEN];
  181.     char *suffix;
  182.     int len;
  183.  
  184.     sprintf(filename,"%s",[object name]);
  185.     if ([object isKindOf:[Button class]] || [object isKindOf:[Cell class]]) {
  186.      if ([object icon]) 
  187.          sprintf(filename,"%s %s",[object icon],[object name]);
  188.      if ([object isKindOf:[Cell class]]) {
  189.          len = strlen(filename);
  190.          suffix = filename + (len-4)*sizeof(char);
  191.          if (strcmp("Cell",suffix)==0) {
  192.          filename[len-4] = '\0';
  193.         }
  194.     }
  195.     }
  196.     if ([object isKindOf:[MenuCell class]]) {
  197.         if ([object icon] && (strcmp([object icon],"menuArrow")==0))
  198.         sprintf(filename,"%s %s",[object title],"Menu");
  199.     else {
  200.         return [self helpForWindow:[[object controlView] window]];
  201.     }
  202.     }
  203.     [self showHelpFile:filename];
  204.     return self;
  205. }
  206.  
  207. - showHelpFile:(const char*)filename;
  208. /* Tries to open a stream for the specified RTF text file in the Help 
  209.  * directory so the text object can readRichText.  Also selects the
  210.  * filename in the browser of help topics.  If the filename doesn't exist,
  211.  * it will select and display the "no help" file.  It also brings the
  212.  * help panel to the front.
  213.  */
  214. {
  215.    NXStream *stream;
  216.    char helpFile[MAXPATHLEN];
  217.    static NXPoint origin = {0.0,0.0};
  218.  
  219.     if (![self browser:helpBrowser selectCell:filename inColumn:0])
  220.         [self browser:helpBrowser selectCell:"No Help" inColumn:0];
  221.     sprintf(helpFile,"%s/%s.rtf",helpDirectory,filename);
  222.     if ((stream = NXMapFile(helpFile,NX_READONLY)) == NULL)
  223.         stream = NXMapFile(noHelpFile,NX_READONLY);
  224.     if (stream != NULL) {
  225.         [helpPanel disableFlushWindow];
  226.         [[helpScrollView docView] readRichText:stream]; 
  227.     [[helpScrollView docView] scrollPoint:&origin];
  228.     [[helpPanel reenableFlushWindow] flushWindow];
  229.         NXCloseMemory(stream,NX_FREEBUFFER);
  230.     }
  231.     [helpPanel orderFront:self];
  232.     return self;
  233. }
  234.  
  235.  
  236. /* BROWSER DELEGATE METHODS */
  237.  
  238.  
  239. #define CHUNK 127
  240. static char **addFile(const char *file, int length, char **list, int count)
  241. /* Adds the specified filename to the list of filenames.  It allocates 
  242.  * more memory in chunks as needed.
  243.  */
  244. {
  245.     char *suffix;
  246.     
  247.     if (!list) list = (char **)malloc(CHUNK*sizeof(char *));
  248.     if (suffix = rindex(file,'.')) 
  249.         *suffix  = '\0';     /* strip rtf suffix */
  250.     list[count] = (char *)malloc((length+1)*sizeof(char));
  251.     strcpy(list[count], file);
  252.     count++;
  253.     if (!(count% CHUNK)) {
  254.     list = (char **)realloc(list,(((count/CHUNK)+1)*CHUNK)*sizeof(char *));
  255.     }
  256.     list[count] = NULL;
  257.     return list;
  258. }
  259.  
  260. static void freeList(char **list)
  261. /* Frees the array of filenames
  262.  */
  263.  {
  264.     char **strings;
  265.  
  266.     if (list) {
  267.     strings = list;
  268.     while (*strings) free(*strings++);
  269.     free(list);
  270.     }
  271. }
  272.  
  273. static BOOL isOk(const char *s)
  274. /* checks to make sure the filename is not NULL and to verify that it is
  275.  * not a "dot"--hidden file.
  276.  */
  277. {
  278.     return (!s[0] || s[0] == '.') ? NO : YES;
  279. }
  280.  
  281. static int caseInsensitiveCompare(void *arg1, void *arg2)
  282. /* Compares the two arguments without regard for case using strcasecmp().
  283. */
  284. {
  285.     char *string1, *string2;
  286.  
  287.     string1 = *((char **)arg1);
  288.     string2 = *((char **)arg2);
  289.     return strcasecmp(string1,string2);
  290. }
  291.  
  292. static char **fileList;
  293.  
  294. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  295. /* This delegate method goes out to the help directory and gets a list
  296.  * of all the files in that directory.  It creates a list of file names
  297.  * for the static variable fileList, and will load the filenames into the 
  298.  * browser on demand (lazy loading).
  299.  */
  300. {
  301.     long basep;
  302.     char *buf;
  303.     struct direct *dp;
  304.     char **list = NULL;
  305.     int cc, fd, fileCount = 0;
  306.     char dirbuf[8192];
  307.  
  308.     if ((fd = open(helpDirectory, O_RDONLY, 0644)) > 0) {
  309.     cc = getdirentries(fd, (buf = dirbuf), 8192, &basep);
  310.     while (cc) {
  311.         dp = (struct direct *)buf;
  312.         if (isOk(dp->d_name)) {
  313.         list = addFile(dp->d_name, dp->d_namlen, list, fileCount++);
  314.         }
  315.         buf += dp->d_reclen;
  316.         if (buf >= dirbuf + cc) {
  317.         cc = getdirentries(fd, (buf = dirbuf), 8192, &basep);
  318.         }
  319.     }
  320.     close(fd);
  321.     if (list) qsort(list,fileCount,sizeof(char *),caseInsensitiveCompare);
  322.     }
  323.     freeList(fileList);
  324.     fileList = list;
  325.     return fileCount;
  326. }
  327.  
  328. - browser:sender loadCell:cell atRow:(int)row inColumn:(int)column
  329. /* This delegate method loads the cell for a given row.  The stringValue
  330.  * for that row comes from the fileList.
  331.  */
  332. {
  333.     if (fileList) {
  334.     [cell setStringValueNoCopy:fileList[row]];
  335.     [cell setLeaf:YES];
  336.     }
  337.     return self;
  338. }
  339.  
  340.  
  341. - (BOOL)browser:sender selectCell:(const char *)title inColumn:(int)column
  342. /* This delegate method selects the cell with the given title.  If it finds
  343.  * a cell with that title, it verifies that it has a file entry in the 
  344.  * fileList, forces the loading of the cell, selects it (highlights) and
  345.  * scrolls the browser so the cell is visible.  It returns a boolean value
  346.  * which indicates whether the cell was found.
  347.  */
  348. {
  349.     int row;
  350.     id matrix;
  351.  
  352.     if (title) {
  353.     matrix = [sender matrixInColumn:column];
  354.     if (!fileList) return NO;
  355.     for (row = [matrix cellCount]-1; row >= 0; row--) {
  356.         if (fileList[row] && !strcmp(title, fileList[row])) {
  357.         [sender getLoadedCellAtRow:row inColumn:column];
  358.         [matrix selectCellAt:row :0];
  359.         [matrix scrollCellToVisible:row :0];
  360.         return YES;
  361.         }
  362.     }
  363.     }
  364.     return NO;
  365. }
  366.  
  367.  
  368. /* WINDOW DELEGATE METHODS */
  369.  
  370. - windowWillResize:sender toSize:(NXSize *)frameSize;
  371. /* This method constrains the Help Panel to a reasonable minimum size
  372.  * when the user resizes the panel.
  373.  */
  374. {
  375.     frameSize->width = MAX(frameSize->width,400.0);
  376.     frameSize->height = MAX(frameSize->height,350.0);
  377.     return self;
  378. }
  379.  
  380.  
  381. @end