home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Kits / MiscTableScroll-138.1 / Palettes / MiscTableScroll / Framework / MiscExporter.M < prev    next >
Encoding:
Text File  |  1998-03-31  |  7.6 KB  |  231 lines

  1. //=============================================================================
  2. //
  3. //    Copyright (C) 1996,1997,1998 by Paul S. McCarthy and Eric Sunshine.
  4. //        Written by Paul S. McCarthy and Eric Sunshine.
  5. //                All Rights Reserved.
  6. //
  7. //    This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the authors
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "License.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //
  14. //=============================================================================
  15. //-----------------------------------------------------------------------------
  16. // MiscExporter.M
  17. //
  18. //    Object that exports the contents of an MiscTableScroll in
  19. //    various useful formats.
  20. //
  21. // TODO:
  22. //    * Genericize for use with Matrix, DBTableView.
  23. //    * Add more export formats: rtf0, Lotus 1-2-3, Quattro, Excel, etc.
  24. //    * Maybe provide option for <CR><LF> line terminators.
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. // $Id: MiscExporter.M,v 1.7 98/03/29 23:43:56 sunshine Exp $
  28. // $Log:    MiscExporter.M,v $
  29. // Revision 1.7  98/03/29  23:43:56  sunshine
  30. // v138.1: #import was missing "MiscTableScroll/" for public header.
  31. // Interface to MiscExporterAccessoryView changed slightly.
  32. // 
  33. // Revision 1.6  97/04/01  07:46:13  sunshine
  34. // v0.125.5: Ported to OPENSTEP 4.2 prerelease for NT.
  35. // No longer sends -close and -makeKeyAndOrderFront: messages to NSSavePanel
  36. // since it doesn't respond to them under NT.
  37. // 
  38. // Revision 1.5  97/03/10  10:27:31  sunshine
  39. // v113.1: For OpenStep conformance, many 'col' methods rename to 'column'.
  40. //-----------------------------------------------------------------------------
  41. #import "MiscExporterPrivate.h"
  42. #import "MiscExporterAccessoryView.h"
  43. #import    <MiscTableScroll/MiscTableScroll.h>
  44. extern "Objective-C" {
  45. #import    <AppKit/NSSavePanel.h>
  46. }
  47. extern "C" {
  48. #import    <errno.h>
  49. }
  50.  
  51.  
  52. //=============================================================================
  53. // MiscExporter
  54. //=============================================================================
  55. @implementation MiscExporter
  56.  
  57. - (MiscExportFormat)getExportFormat        { return exportFormat; }
  58. - (MiscExportTitleMode)getRowExportTitleMode    { return rowTitleMode; }
  59. - (MiscExportTitleMode)getColumnExportTitleMode    { return columnTitleMode; }
  60. - (MiscExportGridMode)getRowExportGridMode    { return rowGrid; }
  61. - (MiscExportGridMode)getColumnExportGridMode    { return columnGrid; }
  62.  
  63. - (void)setExportFormat:(MiscExportFormat)x        { exportFormat = x; }
  64. - (void)setRowExportTitleMode:(MiscExportTitleMode)x    { rowTitleMode = x; }
  65. - (void)setColumnExportTitleMode:(MiscExportTitleMode)x    { columnTitleMode = x; }
  66. - (void)setRowExportGridMode:(MiscExportGridMode)x    { rowGrid = x; }
  67. - (void)setColumnExportGridMode:(MiscExportGridMode)x    { columnGrid = x; }
  68.  
  69.  
  70. //-----------------------------------------------------------------------------
  71. // rowTitleCharWidth:
  72. //-----------------------------------------------------------------------------
  73. - (int)rowTitleCharWidth:(int)nrows
  74.     {
  75.     int max_width = 0;
  76.     if (rowTitleMode != MISC_EXPORT_TITLES_OFF)
  77.     for (int r = 0; r < nrows; r++)
  78.         {
  79.         int const len = [row_title( r, tableScroll ) length];
  80.         if (max_width < len)
  81.         max_width = len;
  82.         }
  83.     return max_width;
  84.     }
  85.  
  86.  
  87. //-----------------------------------------------------------------------------
  88. // makeColMap:
  89. //-----------------------------------------------------------------------------
  90. - (int*)makeColMap:(int)ncols
  91.     {
  92.     int* col_map = (int*) malloc( ncols * sizeof(*col_map) );
  93.     for (int c = 0; c < ncols; c++)
  94.     col_map[c] = col_at( c, tableScroll );
  95.     return col_map;
  96.     }
  97.  
  98.  
  99. //-----------------------------------------------------------------------------
  100. // exportToFile:
  101. //-----------------------------------------------------------------------------
  102. - (void)exportToFile:(FILE*)fp
  103.     {
  104.     switch (exportFormat)
  105.     {
  106.     default:
  107.     case MISC_EXPORT_ASCII_FIXED:     [self exportFixed:fp];        break;
  108.     case MISC_EXPORT_ASCII_TAB:     [self exportTab:fp];        break;
  109.     case MISC_EXPORT_ASCII_DELIMITED:[self exportDelimited:fp]; break;
  110.     case MISC_EXPORT_DBF:         [self exportDBF:fp];        break;
  111.     }
  112.     }
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // exportToFilename:
  117. //-----------------------------------------------------------------------------
  118. - (int)exportToFilename:(NSString*)nm
  119.     {
  120.     int rc = 0;
  121.     FILE* fp = fopen( [nm lossyCString], "wb" );
  122.     if (fp == 0)
  123.     rc = errno;
  124.     else
  125.     {
  126.     [self exportToFile:fp];
  127.     rc = fclose( fp );
  128.     }
  129.     return rc;
  130.     }
  131.  
  132.  
  133. //-----------------------------------------------------------------------------
  134. // createAccessoryView
  135. //-----------------------------------------------------------------------------
  136. - (MiscExporterAccessoryView*)createAccessoryView
  137.     {
  138.     return [[[MiscExporterAccessoryView alloc]
  139.     initWithFormat:exportFormat
  140.     rowTitle:rowTitleMode
  141.     colTitle:columnTitleMode
  142.     rowGrid:rowGrid
  143.     colGrid:columnGrid]
  144.     autorelease];
  145.     }
  146.  
  147.  
  148. //-----------------------------------------------------------------------------
  149. // getValuesFromAccessoryView:
  150. //-----------------------------------------------------------------------------
  151. - (void)getValuesFromAccessoryView:(MiscExporterAccessoryView*)accessory
  152.     {
  153.     [self setExportFormat:[accessory format]];
  154.     [self setRowExportTitleMode:[accessory rowTitleMode]];
  155.     [self setColumnExportTitleMode:[accessory columnTitleMode]];
  156.     [self setRowExportGridMode:[accessory rowGrid]];
  157.     [self setColumnExportGridMode:[accessory colGrid]];
  158.     }
  159.  
  160.  
  161. //-----------------------------------------------------------------------------
  162. // exportTableScroll:
  163. //-----------------------------------------------------------------------------
  164. - (int)exportTableScroll:(MiscTableScroll*)ts
  165.     {
  166.     int rc = -1;
  167.     if (ts != 0 && [ts numberOfColumns] > 0)
  168.     {
  169.     tableScroll = ts;
  170.     NSSavePanel* panel = [NSSavePanel savePanel];
  171.     MiscExporterAccessoryView* accessory = [self createAccessoryView];
  172.  
  173.     [panel setDelegate:self];
  174.     [panel setAccessoryView:[accessory view]];
  175.     if ([panel runModal] == NSOKButton)
  176.         {
  177.         [self getValuesFromAccessoryView:accessory];
  178.         NSString* name = [panel filename];
  179.         if ((rc = [self exportToFilename:name]) != 0)
  180.         NSRunAlertPanel( @"Error", @"Cannot open %@.\n%s",
  181.             @"OK", nil, nil, name, strerror(rc));
  182.         }
  183.     [panel setAccessoryView:0];
  184.     [panel setDelegate:0];
  185.     }
  186.     return rc;
  187.     }
  188.  
  189.  
  190. //-----------------------------------------------------------------------------
  191. // exportTableScroll:toFilename:
  192. //-----------------------------------------------------------------------------
  193. - (int)exportTableScroll:(MiscTableScroll*)ts toFilename:(NSString*)nm
  194.     {
  195.     int rc = -1;
  196.     if (nm != 0 && ts != 0 && [ts numberOfColumns] > 0)
  197.     {
  198.     tableScroll = ts;
  199.     rc = [self exportToFilename:nm];
  200.     }
  201.     return rc;
  202.     }
  203.  
  204.  
  205. //-----------------------------------------------------------------------------
  206. // init
  207. //-----------------------------------------------------------------------------
  208. - (id)init
  209.     {
  210.     [super init];
  211.     rowTitleMode = MISC_EXPORT_TITLES_ROW_DEFAULT;
  212.     columnTitleMode = MISC_EXPORT_TITLES_COL_DEFAULT;
  213.     rowGrid = MISC_EXPORT_GRID_ROW_DEFAULT;
  214.     columnGrid = MISC_EXPORT_GRID_COL_DEFAULT;
  215.     return self;
  216.     }
  217.  
  218.  
  219. //-----------------------------------------------------------------------------
  220. // + commonInstance
  221. //-----------------------------------------------------------------------------
  222. + (MiscExporter*)commonInstance
  223.     {
  224.     static MiscExporter* obj = 0;
  225.     if (obj == 0)
  226.     obj = [[self alloc] init];
  227.     return obj;
  228.     }
  229.  
  230. @end
  231.