home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Apps.iso / LighthouseApps / Quantrix-2.3-MIHS / QAPI_ExportHTML-1.6-MIHS / ExportHTML.1.6 / QXExportHtml.m < prev    next >
Encoding:
Text File  |  1997-09-19  |  13.4 KB  |  484 lines

  1. // 
  2. // Based on the following example from LightHouse:
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. // Copyright 1994-1995 Lighthouse Design, Ltd. All rights reserved.
  5. //
  6. // QXExport.m
  7. //
  8. // THIS CODE IS FOR DEMONSTRATION PURPOSES ONLY!
  9. // You may freely copy, distribute and reuse the code in this example.  
  10. // Lighthouse disclaims any warranty of any kind, expressed or implied, as to 
  11. // its fitness for any particular use.
  12. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. //
  14. // QXExportHtml.m
  15. //
  16. // Copyright 1996 Rudolf B. Blazek, Michigan State University. 
  17. // blazek@stt.msu.edu
  18. // All rights reserved.
  19. //
  20. // See the ReadMe.rtf file for details.
  21. //
  22. //
  23. // Version 1.5 - November 26, 1996
  24. // Modified by sanguish@digifix.com
  25. // fixed:  empty cells no longer show up as a "."  it now inserts  
  26. // added:  support for font colors (although it doesn't seem to be settable 
  27. //           in the UI)
  28. //         support for alignment, only seems to work for fields explictly
  29. //           aligned.
  30. //         background colors for cells
  31. //
  32. //
  33. // See the ReadMe.rtf file for details.
  34. //
  35. //
  36. // Version 1.6 - September 19, 1997
  37. // Changes by blazek@stt.msu.edu
  38. // fixed:  color attributes work also for column labels now.
  39. //         row labels can be exported now.
  40. //         file name taken from the model name correctly.
  41. // added:  user interface - accessory view in SavePanel and info panel.
  42. //             - allows to set the HTML parameters.
  43. //         
  44. // See the ReadMe.rtf file for details and things to do.
  45. //
  46.  
  47.  
  48. #import <appkit/appkit.h>
  49. #import "QXExportHtml.h"
  50.  
  51. #define FIRST_VALUE -1
  52. #define DEFAULTS_OWNER "QuantrixExportHtml"
  53. #define FIRST_VALUE -1
  54.  
  55. static QXExportHtml* instance = nil;
  56.  
  57. static void *rcs_id = (rcs_id, "$Id: QXExportHtml.m,v 1.6 1997/8/30 00:02:55 blazek Exp $");
  58.  
  59. @implementation QXExportHtml
  60.  
  61. + q_bundleSelected:(NXBundle*)aBundle
  62. {
  63.     if (!instance) {
  64.     instance = [[[self class] alloc] init];
  65.     [instance setBundle:aBundle];
  66.     }
  67.     [instance menuSelected:self];
  68.     return self;
  69. }
  70.  
  71. - init
  72. {
  73.     [super init];
  74.     bundle = nil;
  75.     accessoryView = nil;
  76.     lastColorWell = 0;
  77.     qapiApp = qapiConnect();
  78.     return self;
  79. }
  80.  
  81. - free
  82. {
  83.     return [super free];
  84. }
  85.  
  86. - setBundle:(NXBundle*)aBundle
  87. {
  88.     bundle = aBundle;
  89.     return self;
  90. }
  91.  
  92. - loadInterfaceFileFrom:(NXBundle*)aBundle;
  93. {
  94.     char fileName[MAXPATHLEN+1];
  95.     char path[MAXPATHLEN+1];
  96.  
  97.     sprintf(fileName, "%s.nib", [[self class] name]);
  98.     if (![aBundle getPath:path forResource:fileName ofType:"nib"] ||
  99.     ![NXApp loadNibFile:path owner:self withNames:YES fromZone:[self zone]]) {
  100.     NXLogError("QXExportHtml: Could not load interface %s\n", fileName);
  101.     }
  102.     return self;
  103. }
  104.  
  105. - equalColorsOnOff:sender
  106. {
  107.     switch (lastColorWell) {
  108.     case 1: [self pageColorChanged:self];  break;
  109.     case 2: [self tableColorChanged:self]; break;
  110.         default: break;
  111.     }
  112.     return self;
  113. }
  114.  
  115. - pageColorChanged:sender
  116. {
  117.     lastColorWell = 1;
  118.     if ([equalColorsSwitch state])
  119.     [tableBgColorWell setColor:[pageBgColorWell color]]; 
  120.     return self;
  121. }
  122.  
  123. - tableColorChanged:sender
  124. {
  125.     lastColorWell = 2;
  126.     if ([equalColorsSwitch state])
  127.     [pageBgColorWell setColor:[tableBgColorWell color]]; 
  128.     return self;
  129. }
  130.  
  131. - registerDefaults
  132. {
  133.     static NXDefaultsVector myDefaults = {
  134.         {"RowLabels","Off"},
  135.         {"ColumnLabels","On"},
  136.         {"UseColors","On"},
  137.         {"EqualColors","On"},
  138.         {"BorderSize", "4"},
  139.         {"CellSpacing", "1"},
  140.         {"CellPadding", "1"},
  141.         {"LabelFontIncrease", "1"},
  142.         {"TableBackground","#FFFFFF"},
  143.         {"PageBackground","#FFFFFF"},
  144.         {NULL}
  145.     };
  146.     NXRegisterDefaults(DEFAULTS_OWNER, myDefaults);
  147.     [self restoreDefaults:self];
  148.     return self;
  149. }
  150.  
  151.  
  152. - saveDefaults:sender
  153. {
  154.     [self saveField:borderSizeField  into:"BorderSize"];
  155.     [self saveField:cellSpacingField into:"CellSpacing"];
  156.     [self saveField:cellPaddingField into:"CellPadding"];
  157.     [self saveField:headerFontIncreaseField into:"LabelFontIncrease"];
  158.  
  159.     [self saveSwitch:rowLabelsSwitch   into:"RowLabels"];
  160.     [self saveSwitch:colLabelsSwitch   into:"ColumnLabels"];
  161.     [self saveSwitch:colorAttribSwitch into:"UseColors"];
  162.     [self saveSwitch:equalColorsSwitch into:"EqualColors"];
  163.  
  164.     [self saveColorWell:tableBgColorWell  into:"TableBackground"];
  165.     [self saveColorWell:pageBgColorWell   into:"PageBackground"];
  166.  
  167.     return self;
  168. }
  169.  
  170. - restoreDefaults:sender
  171. {
  172.     NXUpdateDefaults();
  173.  
  174.     [self restoreField:borderSizeField  from:"BorderSize"];
  175.     [self restoreField:cellSpacingField from:"CellSpacing"];
  176.     [self restoreField:cellPaddingField from:"CellPadding"];
  177.     [self restoreField:headerFontIncreaseField from:"LabelFontIncrease"];
  178.  
  179.     [self restoreSwitch:rowLabelsSwitch   from:"RowLabels"];
  180.     [self restoreSwitch:colLabelsSwitch   from:"ColumnLabels"];
  181.     [self restoreSwitch:colorAttribSwitch from:"UseColors"];
  182.     [self restoreSwitch:equalColorsSwitch from:"EqualColors"];
  183.  
  184.     [self restoreColorWell:tableBgColorWell  from:"TableBackground"];
  185.     [self restoreColorWell:pageBgColorWell   from:"PageBackground"];
  186.  
  187.     [self pageColorChanged:self];
  188.  
  189.     return self;
  190. }
  191.  
  192. - saveField:aField into:(char*)name
  193. {
  194.     NXWriteDefault(DEFAULTS_OWNER, name, [aField stringValue]);
  195.     return self;
  196. }
  197.  
  198. - restoreField:aField from:(char*)name
  199. {
  200.     [aField setStringValue:NXGetDefaultValue(DEFAULTS_OWNER, name)];
  201.     [[aField target] takeIntValueFrom:aField];
  202.     return self;
  203. }
  204.  
  205. - saveSwitch:aSwitch into:(char*)name
  206. {
  207.     if ([aSwitch state])
  208.     NXWriteDefault(DEFAULTS_OWNER, name,"On");
  209.     else
  210.     NXWriteDefault(DEFAULTS_OWNER, name,"Off");    
  211.     return self;
  212. }
  213.  
  214. - restoreSwitch:aSwitch from:(char*)name
  215. {
  216.     if (strcmp(NXGetDefaultValue(DEFAULTS_OWNER, name),"On")==0)
  217.     [aSwitch setIntValue:1];
  218.     else [aSwitch setIntValue:0];
  219.     return self;
  220. }
  221.  
  222. - saveColorWell:aColorWell into:(char*)name
  223. {
  224.     NXWriteDefault(DEFAULTS_OWNER, name, [self HTMLcolor:[aColorWell color]]);
  225.     return self;
  226. }
  227.  
  228. - restoreColorWell:aColorWell from:(char*)name
  229. {
  230.     char htmlColor[1024];
  231.     int r,g,b;
  232.     float rf, gf, bf;
  233.     sprintf (htmlColor, "%s", NXGetDefaultValue(DEFAULTS_OWNER, name));
  234.     sscanf  (htmlColor, "#%02x%02x%02x", &r, &g, &b);
  235.     rf = r; gf = g; bf = b;
  236.     [aColorWell setColor:NXConvertRGBToColor(rf/255, gf/255, bf/255)];
  237.     return self;
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244. - menuSelected:sender
  245. {
  246.     SavePanel *savePanel = [SavePanel new];
  247.     id <QAPIModel> aModel = [[qapiApp q_currentView] q_model];
  248.     char path[1024], name[1024];
  249.     size_t position;
  250.  
  251.     if (!accessoryView) {
  252.     [self loadInterfaceFileFrom:bundle];
  253.     [self registerDefaults];
  254.     };
  255.  
  256.     [infoPanel setWorksWhenModal:YES];
  257. //    [infoPanel setFloatingPanel:YES];
  258.  
  259.     [savePanel setAccessoryView:accessoryView];
  260.     [savePanel setTitle:"Export Html Table"];
  261.     [savePanel setRequiredFileType:"html"];
  262.  
  263.     sprintf (name, "%s", [aModel q_name]);
  264.     position = strlen(name);
  265.     name[position-9]=0;
  266.     sprintf (path, "%s%s%s%s", [aModel q_path], "/../", name, ".html");
  267.     if ([savePanel runModalForDirectory:path file:name]) {
  268.     useRowLabels = [rowLabelsSwitch state];
  269.     useColLabels = [colLabelsSwitch state];
  270.     useColors    = [colorAttribSwitch state];
  271.     prevRow = FIRST_VALUE;
  272.     aStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  273.  
  274.         NXPrintf(aStream, "<HTML>\n\n<HEAD>\n<TITLE>");
  275.         NXPrintf(aStream, "Table Exported From Quantrix.app");
  276.         NXPrintf(aStream, "</TITLE>\n</HEAD>\n\n<BODY");
  277.         if (useColors) NXPrintf(aStream, " BGCOLOR=%s", 
  278.         [self HTMLcolor:[pageBgColorWell color]]);
  279.         NXPrintf(aStream, ">\n");
  280.         NXPrintf(aStream, "<TABLE BORDER=%i", [borderSizeField intValue]);
  281.         NXPrintf(aStream, " CELLSPACING=%i", [cellSpacingField intValue]);
  282.         NXPrintf(aStream, " CELLPADDING=%i", [cellPaddingField intValue]);
  283.         if (useColors) NXPrintf(aStream, " BGCOLOR=%s", 
  284.         [self HTMLcolor:[tableBgColorWell color]]);
  285.         NXPrintf(aStream, ">\n");
  286.  
  287.     [self exportHTMLData:qapiApp];
  288.  
  289.         NXPrintf(aStream, "\n</TABLE>");
  290.         NXPrintf(aStream, "\n</BODY>\n\n</HTML>\n");
  291.     NXSaveToFile(aStream, [savePanel filename]);
  292.     NXCloseMemory(aStream, NX_FREEBUFFER);
  293.     }
  294.     return self;
  295. }
  296.  
  297. - exportHTMLData:(id <QAPIApp>)anApp
  298. {
  299.     id <QAPIView> aView;
  300.     id <QAPITable> aTable;
  301.     id <QAPISelection> aSelection;
  302.     id <QAPINode> aCategory, anItem;
  303.     qs_categoryRange aRange;
  304.     int nItems, item;
  305.     char *rowLabel;
  306.  
  307.     aView = [anApp q_currentView];
  308.     aTable = [aView q_table];
  309.     aSelection = [aView q_alternateSelection];
  310.  
  311.     //  Row Labels;
  312.     if (useRowLabels) {
  313.     aCategory = [aTable q_categoryAt:1];
  314.     nItems = [aCategory q_itemCount];
  315.     aRange.start = [aCategory q_itemAt:0];
  316.     aRange.end = [aCategory q_itemAt:nItems-1];
  317.     [aSelection q_setCategoryRange:aRange clearOther:YES];
  318.     labelsArray = (char **) malloc (nItems * 4);
  319.     for (item=0; item < nItems; item++) {
  320.         anItem  = [aCategory q_itemAt:item];
  321.             rowLabel = (char *) malloc(1024);
  322.             sprintf(rowLabel,"%s", [self evaluateItem:anItem in:aSelection]);
  323.         labelsArray[item] = rowLabel;
  324.     }
  325.       }
  326.  
  327.     // Column Labels
  328.     aCategory = [aTable q_categoryAt:0];
  329.     nItems = [aCategory q_itemCount];
  330.     aRange.start = [aCategory q_itemAt:0];
  331.     aRange.end = [aCategory q_itemAt:nItems-1];
  332.     [aSelection q_setCategoryRange:aRange clearOther:YES];
  333.     if (useColLabels) {
  334.     NXPrintf(aStream, "<TR>\n");
  335.     if (useRowLabels) NXPrintf(aStream, "<TD></TD>");
  336.     for (item=0; item < nItems; item++) {
  337.         anItem = [aCategory q_itemAt:item];
  338.         NXPrintf(aStream, "%s", [self evaluateItem:anItem in:aSelection]);
  339.     }
  340.     NXPrintf(aStream, "</TR>\n");
  341.     }
  342.  
  343.     // All Table Cells
  344.     NXPrintf(aStream, "<TR>\n");
  345.     [aSelection q_iterateWithDelegate:self];
  346.     NXPrintf(aStream, "</TR>\n");
  347.  
  348.     if (useRowLabels) free (labelsArray);
  349.  
  350.     return self;
  351. }
  352.  
  353. - (char*)HTMLcolor:(NXColor)theColor;
  354. {
  355. // written by sanguish@digifix.com and moved by blazek@stt.msu.edu
  356.     int r,g,b;
  357.     char tempString[1024];
  358.  
  359.     r=(NXRedComponent(theColor)*255);
  360.     g=(NXGreenComponent(theColor)*255);
  361.     b=(NXBlueComponent(theColor)*255);
  362.     sprintf(tempString,"#%02X%02X%02X", r, g, b);
  363.     return tempString;
  364. }
  365.  
  366. - (char*)evaluateItem:(id<QAPINode>)anItem in:aSelection;
  367. {
  368. // modified by sanguish@digifix.com
  369. // copied here and further modified by blazek@stt.msu.edu
  370.  
  371.     NXColor theColor;
  372.     char outString[1024], align[1024], bgColor[1024], 
  373.     fontColor[1024], fontSize[1024];
  374.  
  375. //  if (![[aSelection q_itemFormatFor:anItem] q_isHidden])
  376.  
  377.     switch ([[aSelection q_itemFormatFor:anItem] q_textAlign]) {
  378.     case qk_leftAlign:   strcpy(align," ALIGN=LEFT");   break;
  379.     case qk_rightAlign:  strcpy(align," ALIGN=RIGHT");  break;
  380.     case qk_centerAlign: strcpy(align," ALIGN=CENTER"); break;
  381.     case qk_smartAlign:  strcpy(align,""); break;
  382.     }
  383.  
  384.     if (useColors) {
  385.     theColor=[[aSelection q_itemFormatFor:anItem] q_backgroundColor];
  386.     sprintf(bgColor," BGCOLOR=%s", [self HTMLcolor:theColor]);
  387.     theColor=[[aSelection q_itemFormatFor:anItem] q_textColor];
  388.     sprintf(fontColor," COLOR=%s", [self HTMLcolor:theColor]);
  389.     } else {
  390.     strcpy(bgColor, "");
  391.     strcpy(fontColor, "");
  392.     }
  393.  
  394. // end modified by sanguish@digifix.com
  395.  
  396.     sprintf(fontSize," SIZE=+%i", [headerFontIncreaseField intValue]);
  397.  
  398.     sprintf(outString, "<TD%s%s><FONT%s%s><B>%s</B></FONT></TD>", 
  399.     align, bgColor, fontSize, fontColor, [anItem q_name]);
  400.  
  401.     return outString;
  402.  
  403. }
  404.  
  405.  
  406. - (void)q_evaluateCell:(id<QAPICell>)aCell in:aSelection;
  407. {
  408.     qs_cellLocation location;
  409.     qenum_stringType stringType;
  410.     qenum_dataType dataType;
  411.     NXColor theColor;
  412.     char *rowLabel;
  413.  
  414.     dataType = [aCell q_dataType];
  415.     stringType = [[aSelection q_cellFormatFor:aCell] q_stringType];
  416.     location = [aCell q_location];
  417.     
  418.     if (prevRow == FIRST_VALUE) {
  419.     prevRow  = location.row;
  420.     if (useRowLabels) {
  421. //            rowLabel = (char *)[aStorage elementAt:prevRow];
  422.             rowLabel = labelsArray[prevRow];
  423.         NXPrintf(aStream, "%s", rowLabel);
  424.         free (rowLabel);
  425.     }
  426.     }
  427.     else if (location.row != prevRow) {
  428.     prevRow = location.row;
  429.     NXPrintf(aStream, "</TR>\n<TR>\n");
  430.     if (useRowLabels) {
  431. //            rowLabel = (char *)[aStorage elementAt:prevRow];
  432.             rowLabel = labelsArray[prevRow];
  433.         NXPrintf(aStream, "%s", rowLabel);
  434.         free (rowLabel);
  435.     }
  436.     }
  437.  
  438.  
  439. // modified by sanguish@digifix.com
  440. // see notes for later modifications by blazek@stt.msu.edu
  441.  
  442.     NXPrintf(aStream, "<TD");
  443.  
  444.     switch ([[aSelection q_cellFormatFor:aCell] q_textAlign]) {
  445.     case qk_leftAlign:   NXPrintf(aStream, " ALIGN=LEFT");   break;
  446.     case qk_rightAlign:  NXPrintf(aStream, " ALIGN=RIGHT");  break;
  447.     case qk_centerAlign: NXPrintf(aStream, " ALIGN=CENTER"); break;
  448.     case qk_smartAlign:  NXPrintf(aStream, ""); break;
  449.     }
  450.  
  451.     if (useColors) {    // condition added by blazek@stt.msu.edu
  452.     theColor=[[aSelection q_cellFormatFor:aCell] q_backgroundColor];
  453.     NXPrintf(aStream," BGCOLOR=%s", [self HTMLcolor:theColor]);
  454.             // Moved some code into HTMLcolor. blazek@stt.msu.edu
  455.     }
  456.  
  457.     NXPrintf(aStream, ">");
  458.  
  459.     if (useColors) {    // condition added by blazek@stt.msu.edu
  460.     theColor=[[aSelection q_cellFormatFor:aCell] q_textColor];
  461.     NXPrintf(aStream,"<FONT COLOR=%s>", [self HTMLcolor:theColor]);
  462.             // Moved some code into HTMLcolor. blazek@stt.msu.edu
  463.     }
  464.  
  465. // end modified by sanguish@digifix.com
  466.  
  467.     if (dataType == qk_empty || dataType == qk_error)
  468.     NXPrintf(aStream, " ");  
  469.         // fixed so that empty cells image properly sanguish@digifix.com
  470.     else
  471.     NXPrintf(aStream, "%s", [aCell q_formattedValue]);
  472.  
  473.     if (useColors) NXPrintf(aStream, "</FONT>");
  474.     NXPrintf(aStream, "</TD>");
  475.  
  476. }
  477.  
  478. - (void)q_evaluateNode:(id<QAPINode>)aNode in:aSelection
  479. {
  480. }
  481.  
  482. @end
  483.  
  484.