home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / Gnuplot / Source / DataPane.m < prev    next >
Encoding:
Text File  |  1993-03-18  |  10.3 KB  |  452 lines

  1.  
  2. static char RCSId[]="$Id: DataPane.m,v 1.1.1.1 1993/03/18 03:31:34 davis Exp $";
  3.  
  4.  
  5. #import <appkit/Application.h>
  6. #import <appkit/Box.h>
  7. #import <appkit/Form.h>
  8. #import <appkit/OpenPanel.h>
  9. #import <appkit/View.h>
  10.  
  11. #import <sys/param.h>            /* MAXPATHLEN                   */
  12.  
  13. #import "CellScrollView.h"
  14. #import "DataPane.h"
  15. #import "FunctionObject.h"
  16. #import "MultipleSelectionMatrix.h"
  17. #import "Status.h"
  18. #import "SubCell.h"
  19.  
  20.  
  21. @interface DataPane (Private)
  22.  
  23. - _updateFunctionsMatrix;
  24. - _updateControlsForSelectedFunction;
  25.  
  26. @end
  27.  
  28.  
  29. @implementation DataPane
  30.  
  31.  
  32. - init
  33. {
  34.     [super init];
  35.  
  36.     [NXApp loadNibSection: "DataPane.nib"
  37.             owner: self
  38.         withNames: NO
  39.          fromZone: [self zone]];
  40.  
  41.     view = [window setContentView:[[View alloc] init]];
  42.     icon = "InspectorData.tiff";
  43.     functionObjects = nil;
  44.  
  45.  
  46.     /*  Initialize the CellScrollView of functions  */
  47.  
  48.     [functionsScrollView initMatrix:[SubCell class]];
  49.     functionsMatrix = [functionsScrollView cellMatrix];
  50.     [[functionsMatrix setAction:@selector(selectFunction:)] setTarget:self];
  51.  
  52.     return self;
  53. }
  54.  
  55.  
  56. /*  
  57.  *  Overridden from Pane.
  58.  */
  59. - selectControl:sender
  60. {
  61.     [functionsForm selectTextAt:0];
  62.     return self;
  63. }
  64.  
  65.  
  66. - updateStatus:aStatus doc:aDoc
  67. {
  68.     id oldStatus = status;
  69.  
  70.     /*  
  71.      *  We only bother updating if status has changed since we last 
  72.      *  updated, or if we just became the current pane (didSwap) -- 
  73.      *  assuming that status and aDoc are non-nil, of course.
  74.      */
  75.  
  76.     if ([super updateStatus:aStatus doc:aDoc]
  77.     && ((status != oldStatus) || didSwap)) {
  78.  
  79.     Window *viewWindow;
  80.  
  81.     [(viewWindow = [view window]) disableDisplay];
  82.  
  83.     functionObjects = [status functions];
  84.  
  85.     [dummyFormCellX setStringValue:[status dummyVar:X_TAG]];
  86.     [dummyFormCellY setStringValue:[status dummyVar:Y_TAG]];
  87.     [dummyFormCellY setEnabled:[status isThreeD]];
  88.     [self _updateFunctionsMatrix];
  89.  
  90.     if (![functionsMatrix selectedCell])
  91.         [functionsMatrix selectCellAt:0:0];
  92.     [self _updateControlsForSelectedFunction];
  93.  
  94.     [viewWindow reenableDisplay];
  95.  
  96.     [self perform:@selector(selectControl:)
  97.          with:self
  98.        afterDelay:1
  99.        cancelPrevious:YES];
  100.  
  101.     didSwap = NO;
  102.     return self;
  103.  
  104.     }
  105.  
  106.     return nil;
  107. }
  108.  
  109.  
  110. - didSwapIn:sender
  111. {
  112.     didSwap = YES;
  113.     return self;
  114. }
  115.  
  116.  
  117.  
  118. - doSetDummy:sender
  119. {
  120.     [status setDummyVar:[sender tag] to:[sender stringValue]];
  121.     return self;
  122. }
  123.  
  124.  
  125. - findDataFile:sender
  126. {
  127.     const char *const *files;
  128.     id openPanel;
  129.     char fullName[MAXPATHLEN];
  130.  
  131.     openPanel = [[OpenPanel new] allowMultipleFiles:YES];
  132.  
  133.     if ([openPanel runModalForTypes:NULL])
  134.         for (files = [openPanel filenames]; files && *files; files++) {
  135.  
  136.             sprintf (fullName, "%s/%s", [openPanel directory], *files);
  137.             [self addDataFile:fullName];
  138.  
  139.         }
  140.  
  141.     return self;
  142. }
  143.  
  144.  
  145. - selectFunction:sender
  146. {
  147.     if (![functionsMatrix selectedCell])
  148.     [functionsMatrix selectCellAt:0:0];
  149.     else {
  150.     [self _updateControlsForSelectedFunction];
  151.     [functionsForm selectTextAt:0];
  152.     }
  153.  
  154.     return self;
  155. }
  156.  
  157.  
  158. - deleteSelectedFunctions:sender
  159. {
  160.     int i;
  161.     int maxrow = [functionsMatrix cellCount] - 1;
  162.     int row = [functionsMatrix selectedRow];
  163.     int col = [functionsMatrix selectedCol];
  164.  
  165.     for (i = maxrow; i >= 0; i--) {
  166.         SubCell *cell = [functionsMatrix cellAt:i:0];
  167.         if ([cell isHighlighted]) {
  168.             /*
  169.              * If a cell is highlighted, remove (and free) the corresponding
  170.              * item from the list of SubObjects.
  171.              */
  172.             [[functionObjects removeObject:[cell subObject]] free];
  173.         maxrow--;
  174.         }
  175.     }
  176.  
  177.     [functionsScrollView loadCellsFrom:functionObjects];
  178.  
  179.     [functionsMatrix selectCellAt:(row > maxrow)? maxrow:row :col];
  180.     [functionsMatrix scrollCellToVisible:(row > maxrow)? maxrow:row :col];
  181.     [self selectFunction:self];
  182.  
  183.     /*  
  184.      *  We change the list of functions maintainted by Status without 
  185.      *  going through Status, so we need to make sure the changes are 
  186.      *  noticed. 
  187.      */
  188.     [status reportSettingsChange:self];
  189.  
  190.     return self;
  191. }
  192.  
  193.  
  194. - modifySelectedFunction:sender
  195. {
  196.     id cell = [functionsMatrix selectedCell];
  197.  
  198.     [[cell subObject] setStringValue: [functionsForm stringValue]];
  199.     [functionsScrollView loadCellsFrom:functionObjects];
  200.     [functionsMatrix selectCell:cell];
  201.     [self selectFunction:self];
  202.  
  203.     [status reportSettingsChange:self];
  204.     return self;
  205. }
  206.  
  207.  
  208. - addFunction:sender
  209. {
  210.     const char *stringValue;
  211.     int count;
  212.     FunctionObject *functionObject;
  213.  
  214.     stringValue = [functionsForm stringValue];
  215.  
  216.     if (![FunctionObject isAcceptableStringValue:stringValue])  {
  217.         [self selectFunction:self];
  218.         return nil;
  219.     }
  220.  
  221.     functionObject = [[FunctionObject allocFromZone: [self zone]]
  222.                                      initFromString: stringValue];
  223.     [functionObjects addObject:functionObject];
  224.     [functionsScrollView loadCellsFrom:functionObjects];
  225.   /*
  226.    *  Assumptions in this next line:
  227.    *    There are as many SubCells as there are functionObjects
  228.    *    We've added the new functionObject at the end of the list.
  229.    *    We want to display the functionObject we just added and highlight it.
  230.    *  In short, this is a hack.
  231.    *                                    -- CellScrollView, NeXT Example
  232.    */
  233.     count = [functionObjects count] - 1;
  234.     [functionsMatrix scrollCellToVisible:count :0];
  235.     [functionsMatrix selectCellAt:count:0];
  236.     [self selectFunction:self];
  237.  
  238.     [status reportSettingsChange:self];
  239.  
  240.     return self;
  241. }
  242.  
  243.  
  244. - setFunctionTitle:sender
  245. {
  246.     int i;
  247.     SubCell *cell;
  248.     BOOL didChange = NO;
  249.  
  250.     for (i = [functionsMatrix cellCount] - 1 ; i >= 0 ; i--) {
  251.         cell = [functionsMatrix cellAt:i:0];
  252.         if ([cell isHighlighted])  {
  253.             [[cell subObject] setTitle: [sender stringValue]];
  254.             didChange = YES;
  255.         }
  256.     }
  257.  
  258.     if (didChange)
  259.         [status reportSettingsChange:self];
  260.  
  261.     return self;
  262. }
  263.  
  264.  
  265. - setFunctionStyle:sender
  266. {
  267.     int i;
  268.     SubCell *cell;
  269.     BOOL didChange = NO;
  270.     id oldstatus = status;
  271.  
  272.     for (i = [functionsMatrix cellCount] - 1 ; i >= 0 ; i--) {
  273.         cell = [functionsMatrix cellAt:i:0];
  274.         if ([cell isHighlighted])  {
  275.             [[cell subObject] setStyle: [sender selectedTag]];
  276.             didChange = YES;
  277.         }
  278.     }
  279.  
  280.     if (didChange)
  281.         [status reportSettingsChange:self];
  282.  
  283.     [self selectFunction:self];
  284.  
  285.     return self;
  286. }
  287.  
  288.  
  289. - setFunctionPointsStyle:sender
  290. {
  291.     int i;
  292.     SubCell *cell;
  293.     BOOL didChange = NO;
  294.  
  295.     for (i = [functionsMatrix cellCount] - 1 ; i >= 0 ; i--) {
  296.         cell = [functionsMatrix cellAt:i:0];
  297.         if ([cell isHighlighted])  {
  298.             [[cell subObject] setPointsStyle: [sender selectedTag]];
  299.             didChange = YES;
  300.         }
  301.     }
  302.  
  303.     if (didChange)
  304.         [status reportSettingsChange:self];
  305.  
  306.     return self;
  307. }
  308.  
  309.  
  310. - setFunctionLineStyle:sender
  311. {
  312.     int i;
  313.     SubCell *cell;
  314.     BOOL didChange = NO;
  315.  
  316.     for (i = [functionsMatrix cellCount] - 1 ; i >= 0 ; i--) {
  317.         cell = [functionsMatrix cellAt:i:0];
  318.         if ([cell isHighlighted])  {
  319.             [[cell subObject] setLineStyle: [sender selectedTag]];
  320.             didChange = YES;
  321.         }
  322.     }
  323.  
  324.     if (didChange)
  325.         [status reportSettingsChange:self];
  326.  
  327.     return self;
  328. }
  329.  
  330.  
  331.  
  332. - addDataFile:(const char *)aPath
  333. {
  334.     int count;
  335.  
  336.     [doc addDataFile:aPath];
  337.     [functionsScrollView loadCellsFrom:functionObjects];
  338.  
  339.     /*
  340.      * Assumptions in this next line:
  341.      *    There are as many SubCells as there are functionObjects
  342.      *    We've added the new functionObject at the end of the list.
  343.      *    We want to display the functionObject we just added and highlight it.
  344.      * In short, this is a hack.
  345.      *                          -- CellScrollView, NeXT Example
  346.      */
  347.     count = [functionObjects count] - 1;
  348.     [functionsMatrix scrollCellToVisible:count :0];
  349.     [functionsMatrix selectCellAt:count:0];
  350.     [self selectFunction:self];
  351.  
  352.     return self;
  353. }
  354.  
  355.  
  356.  
  357. // Shuts up the compiler about unused RCSId
  358. - (const char *) rcsid
  359. {
  360.     return RCSId;
  361. }
  362.  
  363.  
  364. @end
  365.  
  366.  
  367. @implementation DataPane (Private)
  368.  
  369.  
  370. - _updateFunctionsMatrix
  371. {
  372.     int functionCount, counter = 0;
  373.     int fieldcount;
  374.     int row = [functionsMatrix selectedRow];
  375.     int col = [functionsMatrix selectedCol];
  376.  
  377.     [functionsScrollView loadCellsFrom:functionObjects];
  378.     if (functionCount = [functionObjects count]) {
  379.         for (counter = 0; counter < functionCount; counter++)
  380.             [[functionsMatrix cellAt:counter:0]
  381.                              setStringValue:[[functionObjects objectAt:counter]
  382.                                              stringValue]];
  383.     }
  384.  
  385.     fieldcount = [functionsMatrix cellCount];
  386.     while (counter < fieldcount)
  387.         [[functionsMatrix cellAt:counter++:0] setStringValue:""];
  388.  
  389.     [functionsMatrix selectCellAt:(row > fieldcount)? fieldcount:row :col];
  390.  
  391.     return self;
  392. }
  393.  
  394.  
  395. - _updateControlsForSelectedFunction
  396. {
  397.     int style = FUNCTION_NOSTYLE;
  398.     BOOL enabled;
  399.     BOOL multiple = [functionsMatrix multipleCellsSelected];
  400.     FunctionObject *f = [[functionsMatrix selectedCell] subObject];
  401.     Window *viewWindow;
  402.     Box *aBox;
  403.  
  404.     [(viewWindow = [view window]) disableDisplay];
  405.  
  406.     if (!multiple && f) {
  407.  
  408.     [functionsForm setStringValue: [f stringValue]];
  409.     [functionsTitleField setStringValue: [f title]];
  410.  
  411.     [functionsStyleMatrix selectCellWithTag: style = [f style]];
  412.     [pointsStyleMatrix selectCellWithTag: [f pointsStyle]];
  413.     [lineStyleMatrix selectCellWithTag: [f lineStyle]];
  414.  
  415.     } else {
  416.  
  417.     [functionsForm setStringValue: ""];
  418.     [functionsStyleMatrix selectCellWithTag: FUNCTION_NOSTYLE];
  419.     [pointsStyleMatrix selectCellWithTag: POINTS_NOSTYLE];
  420.     [lineStyleMatrix selectCellWithTag: LINE_NOSTYLE];
  421.  
  422.     }
  423.  
  424.  
  425.     [deleteFunctionButton setEnabled:f? YES: NO];
  426.     [modifyFunctionButton setEnabled:f? !multiple: NO];
  427.     [functionsTitleField setEnabled: f? !multiple: NO];
  428.  
  429.     [functionsStyleMatrix setEnabled:f? !multiple: NO];
  430.  
  431.     [pointsStyleMatrix setEnabled:enabled = (f && !multiple &&
  432.          ((style == FUNCTION_POINTS) || (style == FUNCTION_LINESPOINTS)))];
  433.     [pointsStyleLabel setEnabled:enabled];
  434.  
  435.     [lineStyleMatrix setEnabled:enabled = (f && !multiple &&
  436.           ((style == FUNCTION_LINES) || (style == FUNCTION_LINESPOINTS)))];
  437.     [lineStyleLabel setEnabled:enabled];
  438.  
  439.     enabled = [[aBox = [[functionsStyleMatrix superview] superview] cell]
  440.            isEnabled];
  441.     if (enabled != (f? !multiple: NO))
  442.     [[aBox cell] setEnabled: f? !multiple: NO];
  443.  
  444.     [viewWindow reenableDisplay];
  445.     [view display];
  446.  
  447.     return self;
  448. }
  449.  
  450.  
  451. @end
  452.