summaryrefslogtreecommitdiff
path: root/patches/OOO_1_1/qpro-cxx.diff
blob: f03194195eaad22ffbc1d3948c8a4425b5899216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
--- /dev/null	2004-08-25 23:04:59.000000000 +0530
+++ sc/source/filter/qpro/qpro.cxx	2004-12-15 14:43:15.017599352 +0530
@@ -0,0 +1,420 @@
+#ifdef PCH
+#include "filt_pch.hxx"
+#endif
+
+#pragma hdrstop
+
+//------------------------------------------------------------------------
+
+#include <stdio.h>
+#include <sfx2/docfile.hxx>
+#include <rtl/math.hxx>
+
+
+#include "qproform.hxx"
+#include "qpro.hxx"
+
+#include <tools/color.hxx>
+#include "scitems.hxx"
+#include <svx/algitem.hxx>
+#include <svx/align.hxx>
+#include <svx/udlnitem.hxx>
+#include <svx/wghtitem.hxx>
+#include <svx/postitem.hxx>
+#include <svx/crsditem.hxx>
+#include <svx/fhgtitem.hxx>
+#include <svx/fontitem.hxx>
+#include <svx/colritem.hxx>
+#include <map>
+
+#include "global.hxx"
+#include "sc.hrc" 
+#include "globstr.hrc"
+#include "attrib.hxx"
+#include "stlsheet.hxx"
+#include "stlpool.hxx"
+#include "docpool.hxx" 
+#include "patattr.hxx"
+#include "flttools.hxx"
+#include "filter.hxx"
+#include "scerrors.hxx"
+#include "document.hxx"
+#include "cell.hxx"
+#include "math.h"
+
+extern std::map<UINT16, ScPatternAttr> aPatternPool;
+
+BiffReader::BiffReader( SfxMedium & rMedium ) :
+               mnId(0),
+               mnLength(0),
+               mnOffset(0),
+               meCharSet( RTL_TEXTENCODING_ISO_8859_1 ) // FIXME detect as we parse
+{
+       mpStream = rMedium.GetInStream();
+       if( mpStream )
+               mpStream->SetBufferSize( 65536 );
+}
+
+BiffReader::~BiffReader()
+{
+       if( mpStream )
+               mpStream->SetBufferSize( 0 );
+}
+
+SvStream& BiffReader::operator>>( sal_uInt8 &rByte )
+{
+       *mpStream >> rByte;
+}
+
+SvStream& BiffReader::operator>>( sal_Int8 &rByte )
+{
+       *mpStream >> rByte;
+}
+
+SvStream& BiffReader::operator>>(sal_uInt16 &rWord)
+{
+       *mpStream >> rWord;
+}
+
+bool BiffReader::nextRecord()
+{
+       if( !recordsLeft() )
+               return false;
+
+       if( EndOfFile )
+               return false;
+
+       sal_uInt32 nPos = mpStream->Tell();
+       if( nPos != mnOffset + mnLength )
+               mpStream->Seek( mnOffset + mnLength );
+
+       mnLength = mnId = 0;
+       *mpStream >> mnId >> mnLength;
+
+       mnOffset = mpStream->Tell();
+#ifdef DEBUG
+       fprintf( stderr, "Read record 0x%x length 0x%x at offset 0x%x\n",
+                        mnId, mnLength, mnOffset );
+
+ #if 1  // rather verbose
+       int len = mnLength;
+       while (len > 0) {
+               int i, chunk = len < 16 ? len : 16;
+               unsigned char data[16];
+               mpStream->Read( data, chunk );
+
+               for (i = 0; i < chunk; i++)
+                       fprintf( stderr, "%.2x ", data[i] );
+               fprintf( stderr, "| " );
+               for (i = 0; i < chunk; i++)
+                       fprintf( stderr, "%c", data[i] < 127 && data[i] > 30 ? data[i] : '.' );
+               fprintf( stderr, "\n" );
+
+               len -= chunk;
+       }
+       mpStream->Seek( mnOffset );
+#endif 
+#endif
+       return true;
+}
+
+void BiffReader::readString( String &rString, sal_uInt16 nLength )
+{
+       sal_Char* pText = new sal_Char[ nLength + 1 ];
+       mpStream->Read( pText, nLength );
+       pText[ nLength ] = 0;
+       rString = String( pText, meCharSet );
+}
+
+void BiffReader::SetFormat( ScDocument *pDoc, sal_uInt8 nCol, sal_uInt16 nRow, sal_uInt8 nTab, sal_uInt16 nStyle ){
+       ScPatternAttr aPattern(pDoc->GetPool()); 
+       SfxItemSet& rItemSet = aPattern.GetItemSet();
+       
+       sal_uInt8 nTmp = Align[ nStyle ];
+       sal_uInt8 nHor = ( nTmp & 0x07 );
+       sal_uInt8 nVer = ( nTmp & 0x18 );
+       sal_uInt8 nOrient = ( nTmp & 0x60 );
+ 
+
+       // Horizontal Alignment
+       SvxHorJustifyItem ejustify = SVX_HOR_JUSTIFY_STANDARD;
+       switch( nHor )
+       {
+
+            case 0x00:
+                       ejustify = SVX_HOR_JUSTIFY_STANDARD;
+                       break;
+  
+            case 0x01: 
+                       ejustify = SVX_HOR_JUSTIFY_LEFT;
+                       break;
+  
+            case 0x02: 
+                       ejustify = SVX_HOR_JUSTIFY_CENTER;
+                       break;
+
+            case 0x03:
+                       ejustify = SVX_HOR_JUSTIFY_RIGHT;
+                       break;
+ 
+            case 0x04: 
+                       ejustify = SVX_HOR_JUSTIFY_BLOCK;
+                       break;
+
+         }
+	  rItemSet.Put( ejustify );
+
+	// Vertical Alignment
+        SvxVerJustifyItem verJustify = SVX_VER_JUSTIFY_STANDARD;
+        switch( nVer )
+        {
+         
+           case 0x00:
+                     verJustify = SVX_VER_JUSTIFY_BOTTOM;
+                     break;
+            
+           case 0x08:
+                     verJustify = SVX_VER_JUSTIFY_CENTER;
+                     break; 
+           
+           case 0x10:
+                     verJustify = SVX_VER_JUSTIFY_TOP;
+                     break;
+      
+         }
+         rItemSet.Put( verJustify ); 
+         
+	 // Orientation
+         SvxOrientationItem orientItem = SVX_ORIENTATION_STANDARD;
+         switch( nOrient )
+         {
+
+           case 0x20: 
+                      orientItem = SVX_ORIENTATION_TOPBOTTOM;
+                      break;
+ 
+         } 
+	 rItemSet.Put( orientItem );
+
+        // Wrap cell contents
+        if( nTmp & 0x80 )
+        {
+            SfxBoolItem WrapItem( ATTR_LINEBREAK );
+            WrapItem.SetValue( TRUE );
+            rItemSet.Put( WrapItem );
+        }
+
+        // Font Attributes
+	sal_uInt16 nTmpFnt = FontRecord[ Font[ nStyle ] ];
+        BOOL bIsBold, bIsItalic, bIsUnderLine, bIsStrikeThrough;
+
+        bIsBold = ( nTmpFnt & 0x0001 );
+        bIsItalic = ( nTmpFnt & 0x0002 );
+        bIsUnderLine = ( nTmpFnt & 0x0004 );
+        bIsStrikeThrough = (nTmpFnt & 0x0020 );
+
+        if( bIsBold )
+             rItemSet.Put( SvxWeightItem( WEIGHT_BOLD,ATTR_FONT_WEIGHT) );
+        if( bIsItalic )
+             rItemSet.Put( SvxPostureItem( ITALIC_NORMAL, ATTR_FONT_POSTURE ) );
+        if( bIsUnderLine )
+             rItemSet.Put( SvxUnderlineItem( UNDERLINE_SINGLE, ATTR_FONT_UNDERLINE ) );
+        if( bIsStrikeThrough )
+             rItemSet.Put( SvxCrossedOutItem( STRIKEOUT_SINGLE, ATTR_FONT_CROSSEDOUT ) ); 
+     
+	String fntName = FontType[ Font[ nStyle ] ];
+        rItemSet.Put( SvxFontItem( FAMILY_SYSTEM, fntName, EMPTY_STRING ) );
+
+	pDoc->ApplyPattern( nCol, nRow, nTab, aPattern );
+} 
+
+ColorData BiffReader::MapIntToColor( sal_uInt16 ColorId )
+{
+       ColorData fColor;
+       switch( ColorId )
+       {
+               case 0x0000: fColor = COL_WHITE;
+                             break;
+  
+               case 0x1000: fColor = COL_LIGHTGRAY;
+                             break;
+
+               case 0x2000: fColor = COL_GRAY;
+                             break;
+
+               case 0x3000: fColor = COL_BLACK;
+                             break;
+
+               case 0x4000: fColor = COL_RED;
+                             break;
+   
+                case 0x5000: fColor = COL_GREEN;
+                             break;
+
+               case 0x6000: fColor = COL_BLUE;
+                            break;
+
+               case 0x7000: fColor = COL_CYAN;
+                             break;
+
+               case 0x8000: fColor = COL_MAGENTA;
+                             break;
+
+               case 0x9000: fColor = COL_YELLOW;
+                             break;
+
+               case 0xb000: fColor = COL_LIGHTGREEN;
+                             break;
+        }
+       
+       return fColor;
+}
+      
+readQProSheet( BiffReader &rReader, ScDocument *pDoc, sal_uInt16 nTab )
+{
+       sal_uInt8  nCol, nDummy;
+       sal_uInt16 nRow;
+       sal_uInt16 nStyle;
+       bool bEndOfSheet = false;
+
+#ifdef DEBUG
+       fprintf( stderr, "Read sheet (%d)\n", nTab );
+#endif
+
+       while( !bEndOfSheet && rReader.nextRecord() ) {
+               switch( rReader.getId() )
+               {
+               case 0x000f: { // Label cell
+                       String aLabel;
+                       rReader >> nCol >> nDummy >> nRow >> nStyle >> nDummy;
+                       rReader.readString( aLabel, rReader.getLength() - 7 );
+#ifdef DEBUG
+                       fprintf( stderr, "Put Cell %d %d %d '%s'\n",
+                                        nCol, nRow, nTab,
+                                        (const sal_Char *)rtl::OUStringToOString( aLabel, RTL_TEXTENCODING_UTF8 ) );
+#endif
+                       nStyle = ( nStyle & 0xfff8 ) >> 3;
+                       rReader.SetFormat( pDoc, nCol, nRow, nTab, nStyle );
+                       pDoc->PutCell( nCol, nRow, nTab, new ScStringCell( aLabel ), (BOOL) TRUE );
+                       break;
+               }
+               case 0x00cb: // End of sheet;
+                       bEndOfSheet = true;
+                       break;
+		
+               case 0x000c: // Blank cell
+                        rReader >> nCol >> nDummy >> nRow >> nStyle; 
+                        rReader.SetFormat( pDoc, nCol, nRow, nTab, nStyle );
+			break; 
+               
+               case 0x000d:  { // Integer cell
+			sal_uInt16 nValue;
+			rReader >> nCol >> nDummy >> nRow >> nStyle >> nValue;	
+ 			ScValueCell* aInteger = new ScValueCell( ( double ) nValue ); 
+#ifdef DEBUG
+                        fprintf( stderr, "Put Cell %d %d %d %f\n", nCol, nRow, nTab, nValue );
+#endif
+			rReader.SetFormat( pDoc, nCol, nRow, nTab, nStyle );
+			pDoc->PutCell(nCol ,nRow, nTab ,aInteger,(BOOL) TRUE);
+                        break;
+                       }
+
+               case 0x000e: { // Floating point cell
+                        double nValue;
+                        rReader >> nCol >> nDummy >> nRow >> nStyle >> nValue;
+                        ScValueCell* aFloat = new ScValueCell( nValue );
+#ifdef DEBUG
+                        fprintf( stderr, "Put Cell %d %d %d %f\n", nCol, nRow, nTab, nValue );
+#endif
+                        rReader.SetFormat( pDoc, nCol, nRow, nTab, nStyle );
+			pDoc->PutCell( nCol, nRow, nTab, aFloat, (BOOL) TRUE );
+                       } 
+
+	       case 0x0010: { // Formula cell
+                        double nValue;
+                        sal_uInt16 nState, nLen; 
+                        rReader >> nCol >> nDummy >> nRow >> nStyle >> nValue >> nState >> nLen;
+                        ScAddress aAddr( nCol, nRow, nTab );
+                        const ScTokenArray *pErg;
+                        QProToSc aConv( rReader );
+                        aConv.Reset( aAddr );
+                        aConv.Convert( pErg, nLen );
+                        ScFormulaCell *pZelle = new ScFormulaCell( pDoc, aAddr, pErg );
+#ifdef DEBUG
+                        fprintf( stderr, "Put Cell %d %d %d %f\n", nCol, nRow, nTab, nValue );
+#endif
+                        pZelle->AddRecalcMode( RECALCMODE_ONLOAD_ONCE );
+			rReader.SetFormat( pDoc, nCol, nRow, nTab, nStyle );
+                        pDoc->PutCell( nCol, nRow, nTab, pZelle, ( BOOL ) TRUE );
+			break; } 
+    
+	       }
+       }
+       return eERR_OK;
+}
+
+FltError ScImportQuattroPro( SfxMedium &rMedium, ScDocument *pDoc )
+{
+       FltError   eRet = eERR_OK;
+       BiffReader aReader( rMedium );
+       sal_uInt16 nVersion;
+       int nTab = 0, i = 1, j = 1;
+       aReader.EndOfFile = FALSE;
+
+       if( !aReader.recordsLeft() )
+               return eERR_OPEN;
+
+       while( aReader.nextRecord() && eRet == eERR_OK) {
+               switch( aReader.getId() )
+               {
+               case 0x0000: // Begginning of file
+                       aReader >> nVersion;
+                       break;
+
+               case 0x00ca: // Beginning of sheet
+                       if( nTab < 26 )
+                       {
+                               String aName;
+                               aName.Append( sal_Unicode( 'A' + nTab ) );
+                               pDoc->InsertTab( nTab, aName );
+                       }
+                       eRet = readQProSheet( aReader, pDoc, nTab );
+                       nTab++;
+                       break;
+
+               case 0x0001: // End of file
+                       aReader.EndOfFile = TRUE;
+                       break;
+          
+               case 0x00ce: { // Attribute cell
+                       sal_uInt8 nFormat, nAlign, nFont;
+                       sal_Int16 nTextColor, nColor;
+                       aReader >> nFormat >> nAlign >> nColor >> nFont;
+                       aReader.Align[ i ] = nAlign;
+                       aReader.Font[ i ] = nFont;
+                       nTextColor = nColor & 0xf000;
+                       aReader.FontColor[ i ] = nTextColor;
+                       i++;
+                       break;
+                       }
+                                                                                                                  
+                case 0x00cf: { // Font description
+                      sal_uInt16 nPtSize, nFontAttr;
+		      String Label;
+                      aReader >> nPtSize >> nFontAttr;
+                      aReader.FontSize[ j ] = nPtSize;
+                      aReader.FontRecord[ j ] = nFontAttr;
+                      aReader.readString( Label, aReader.getLength() - 4 );
+                      aReader.FontType[ j ] = Label;
+                      j++;
+                      break;
+                     }
+
+               }
+       }
+       pDoc->CalcAfterLoad();
+       return eRet;
+}
+
+
+