summaryrefslogtreecommitdiff
path: root/sw/source/uibase/config/uinums.cxx
blob: 40de91d8d00c38a98e4ce07917f4d57e1ab68168 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <hintids.hxx>
#include <svl/urihelper.hxx>
#include <osl/thread.h>
#include <unotools/pathoptions.hxx>
#include <tools/stream.hxx>
#include <sfx2/docfile.hxx>
#include <svl/itemiter.hxx>
#include <editeng/brushitem.hxx>

#include <tools/resid.hxx>
#include <fmtornt.hxx>
#include <swtypes.hxx>
#include <wrtsh.hxx>
#include <uinums.hxx>
#include <poolfmt.hxx>
#include <charfmt.hxx>
#include <frmatr.hxx>

#include <unomid.h>

using namespace ::com::sun::star;

#define VERSION_30B     ((sal_uInt16)250)
#define VERSION_31B     ((sal_uInt16)326)
#define VERSION_40A     ((sal_uInt16)364)
#define VERSION_53A     ((sal_uInt16)596)
#define ACT_NUM_VERSION VERSION_53A

#define CHAPTER_FILENAME "chapter.cfg"

/*
 Description:   Saving a rule
 Parameter:     rCopy -- the rule to save
                    nIdx -- position, where the rule is to be saved.
                        An old rule at that position will be overwritten.
*/

SwBaseNumRules::SwBaseNumRules( const OUString& rFileName )
    :
    sFileName( rFileName ),
    nVersion(0),
    bModified( false )
{
    Init();
}

SwBaseNumRules::~SwBaseNumRules()
{
    if( bModified )
    {
        SvtPathOptions aPathOpt;
        OUString sNm( aPathOpt.GetUserConfigPath() + "/" + sFileName );
        INetURLObject aTempObj(sNm);
        sNm = aTempObj.GetFull();
        SfxMedium aStrm( sNm, STREAM_WRITE | STREAM_TRUNC |
                                        STREAM_SHARE_DENYALL );
        Store( *aStrm.GetOutStream() );
    }

    for( sal_uInt16 i = 0; i < nMaxRules; ++i )
        delete pNumRules[i];
}

void  SwBaseNumRules::Init()
{
    for(sal_uInt16 i = 0; i < nMaxRules; ++i )
        pNumRules[i] = 0;

    OUString sNm( sFileName );
    SvtPathOptions aOpt;
    if( aOpt.SearchFile( sNm, SvtPathOptions::PATH_USERCONFIG ))
    {
        SfxMedium aStrm( sNm, STREAM_STD_READ );
        Load( *aStrm.GetInStream() );
    }
}

void SwBaseNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
{
    OSL_ENSURE(nIdx < nMaxRules, "Array der NumRules ueberindiziert.");
    if( !pNumRules[nIdx] )
        pNumRules[nIdx] = new SwNumRulesWithName( rCopy );
    else
        *pNumRules[nIdx] = rCopy;
}

bool SwBaseNumRules::Store(SvStream &rStream)
{
    rStream.WriteUInt16( ACT_NUM_VERSION );
        // Write, what positions are occupied by a rule
        // Then write each of the rules
    for(sal_uInt16 i = 0; i < nMaxRules; ++i)
    {
        if(pNumRules[i])
        {
            rStream.WriteUChar(  sal_True );
            pNumRules[i]->Store( rStream );
        }
        else
            rStream.WriteUChar(  sal_False );
    }
    return true;
}

int SwBaseNumRules::Load(SvStream &rStream)
{
    int         rc = 0;

    rStream.ReadUInt16( nVersion );

    // due to a small but serious mistake, PreFinal writes the same VERION_40A as SP2
    // #55402#
    if(VERSION_40A == nVersion)
    {
        OSL_FAIL("Version 364 is not clear #55402#");
    }
    else if( VERSION_30B == nVersion || VERSION_31B == nVersion ||
             ACT_NUM_VERSION >= nVersion )
    {
        unsigned char bRule = sal_False;
        for(sal_uInt16 i = 0; i < nMaxRules; ++i)
        {
            rStream.ReadUChar( bRule );
            if(bRule)
                pNumRules[i] = new SwNumRulesWithName( rStream, nVersion );
        }
    }
    else
    {
        rc = 1;
    }

    return rc;
}

SwChapterNumRules::SwChapterNumRules() :
    SwBaseNumRules(OUString(CHAPTER_FILENAME))
{
}

 SwChapterNumRules::~SwChapterNumRules()
{
}

void SwChapterNumRules::ApplyNumRules(const SwNumRulesWithName &rCopy, sal_uInt16 nIdx)
{
    bModified = true;
    SwBaseNumRules::ApplyNumRules(rCopy, nIdx);
}

SwNumRulesWithName::SwNumRulesWithName( const SwNumRule &rCopy,
                                        const OUString &rName )
    : maName(rName)
{
    for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
    {
        const SwNumFmt* pFmt = rCopy.GetNumFmt( n );
        if( pFmt )
            aFmts[ n ] = new _SwNumFmtGlobal( *pFmt );
        else
            aFmts[ n ] = 0;
    }
}

SwNumRulesWithName::SwNumRulesWithName( const SwNumRulesWithName& rCopy )
{
    memset( aFmts, 0, sizeof( aFmts ));
    *this = rCopy;
}

SwNumRulesWithName::~SwNumRulesWithName()
{
    for( int n = 0; n < MAXLEVEL; ++n )
        delete aFmts[ n ];
}

const SwNumRulesWithName& SwNumRulesWithName::operator=(const SwNumRulesWithName &rCopy)
{
    if( this != &rCopy )
    {
        maName = rCopy.maName;
        for( int n = 0; n < MAXLEVEL; ++n )
        {
            delete aFmts[ n ];

            _SwNumFmtGlobal* pFmt = rCopy.aFmts[ n ];
            if( pFmt )
                aFmts[ n ] = new _SwNumFmtGlobal( *pFmt );
            else
                aFmts[ n ] = 0;
        }
    }
    return *this;
}

SwNumRulesWithName::SwNumRulesWithName( SvStream &rStream, sal_uInt16 nVersion )
{
    rtl_TextEncoding eEncoding = osl_getThreadTextEncoding();
    maName = rStream.ReadUniOrByteString(eEncoding);

    char c;
    for(sal_uInt16 n = 0; n < MAXLEVEL; ++n )
    {
        if( VERSION_30B == nVersion )
            c = 1;
        // due to a small but serious mistake, PreFinal writes the same VERION_40A as SP2
        // #55402#
        else if(nVersion < VERSION_40A && n > 5)
            c = 0;
        else
            rStream.ReadChar( c );

        if( c )
            aFmts[ n ] = new _SwNumFmtGlobal( rStream, nVersion );
        else
            aFmts[ n ] = 0;
    }
}

void SwNumRulesWithName::MakeNumRule( SwWrtShell& rSh, SwNumRule& rChg ) const
{
    // #i89178#
    rChg = SwNumRule( maName, numfunc::GetDefaultPositionAndSpaceMode() );
    rChg.SetAutoRule( false );
    _SwNumFmtGlobal* pFmt;
    for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
        if( 0 != ( pFmt = aFmts[ n ] ) )
        {
            SwNumFmt aNew;
            pFmt->ChgNumFmt( rSh, aNew );
            rChg.Set( n, aNew );
        }
}

void SwNumRulesWithName::Store( SvStream &rStream )
{
    rtl_TextEncoding eEncoding = osl_getThreadTextEncoding();
    rStream.WriteUniOrByteString(maName, eEncoding);

    for( sal_uInt16 n = 0; n < MAXLEVEL; ++n )
    {
        _SwNumFmtGlobal* pFmt = aFmts[ n ];
        if( pFmt )
        {
            rStream.WriteChar( (char)1 );
            pFmt->Store( rStream );
        }
        else
            rStream.WriteChar( (char)0 );
    }
}

SwNumRulesWithName::_SwNumFmtGlobal::_SwNumFmtGlobal( const SwNumFmt& rFmt )
    : aFmt( rFmt ), nCharPoolId( USHRT_MAX )
{
    // relative gaps?????

    SwCharFmt* pFmt = rFmt.GetCharFmt();
    if( pFmt )
    {
        sCharFmtName = pFmt->GetName();
        nCharPoolId = pFmt->GetPoolFmtId();
        if( pFmt->GetAttrSet().Count() )
        {
            SfxItemIter aIter( pFmt->GetAttrSet() );
            const SfxPoolItem *pCurr = aIter.GetCurItem();
            while( true )
            {
                aItems.push_back( pCurr->Clone() );
                if( aIter.IsAtEnd() )
                    break;
                pCurr = aIter.NextItem();
            }
        }

        aFmt.SetCharFmt( 0 );
    }
}

SwNumRulesWithName::_SwNumFmtGlobal::_SwNumFmtGlobal( const _SwNumFmtGlobal& rFmt )
    :
    aFmt( rFmt.aFmt ),
    sCharFmtName( rFmt.sCharFmtName ),
    nCharPoolId( rFmt.nCharPoolId )
{
    for( sal_uInt16 n = rFmt.aItems.size(); n; )
        aItems.push_back( rFmt.aItems[ --n ].Clone() );
}

SwNumRulesWithName::_SwNumFmtGlobal::_SwNumFmtGlobal( SvStream& rStream,
                                                        sal_uInt16 nVersion )
    : nCharPoolId( USHRT_MAX )
{
    rtl_TextEncoding eEncoding = osl_getThreadTextEncoding();
    {
        sal_uInt16 nUS;
        short nShort;
        sal_Char cChar;
        bool bFlag;
        OUString sStr;

        rStream.ReadUInt16( nUS );           aFmt.SetNumberingType((sal_Int16)nUS );
        if( VERSION_53A > nVersion )
        {
            rStream.ReadChar( cChar );       aFmt.SetBulletChar( cChar );
        }
        else
        {
            rStream.ReadUInt16( nUS );       aFmt.SetBulletChar( nUS );
        }

        rStream.ReadCharAsBool( bFlag );     aFmt.SetIncludeUpperLevels( bFlag );

        if( VERSION_30B == nVersion )
        {
            sal_Int32 nL;
            rStream.ReadChar( cChar );       aFmt.SetStart( (sal_uInt16)cChar );

            sStr = rStream.ReadUniOrByteString(eEncoding);
            aFmt.SetPrefix( sStr );
            sStr = rStream.ReadUniOrByteString(eEncoding);
            aFmt.SetSuffix( sStr );
            rStream.ReadUInt16( nUS );        aFmt.SetNumAdjust( SvxAdjust( nUS ) );
            rStream.ReadInt32( nL );          aFmt.SetLSpace( lNumIndent );
            rStream.ReadInt32( nL );          aFmt.SetFirstLineOffset( (short)nL );
        }
        else                // old start-value was a Byte
        {
            rStream.ReadUInt16( nUS );         aFmt.SetStart( nUS );
            sStr = rStream.ReadUniOrByteString(eEncoding);
            aFmt.SetPrefix( sStr );
            sStr = rStream.ReadUniOrByteString(eEncoding);
            aFmt.SetSuffix( sStr );
            rStream.ReadUInt16( nUS );         aFmt.SetNumAdjust( SvxAdjust( nUS ) );
            rStream.ReadUInt16( nUS );         aFmt.SetAbsLSpace( nUS );
            rStream.ReadInt16( nShort );       aFmt.SetFirstLineOffset( nShort );
            rStream.ReadUInt16( nUS );         aFmt.SetCharTextDistance( nUS );
            rStream.ReadInt16( nShort );       aFmt.SetLSpace( nShort );
            rStream.ReadCharAsBool( bFlag );
        }

        sal_uInt16  nFamily;
        sal_uInt16  nCharSet;
        short   nWidth;
        short   nHeight;
        sal_uInt16  nPitch;
        OUString aName;

        aName = rStream.ReadUniOrByteString(eEncoding);
        rStream.ReadUInt16( nFamily ).ReadUInt16( nCharSet ).ReadInt16( nWidth ).ReadInt16( nHeight ).ReadUInt16( nPitch );

        if( !aName.isEmpty() )
        {
            vcl::Font aFont( static_cast<FontFamily>(nFamily), Size( nWidth, nHeight ) );
            aFont.SetName( aName );
            aFont.SetCharSet( (rtl_TextEncoding)nCharSet );
            aFont.SetPitch( (FontPitch)nPitch );

            aFmt.SetBulletFont( &aFont );
        }
        else
            nCharSet = RTL_TEXTENCODING_SYMBOL;

        if( VERSION_53A > nVersion )
        {
            sal_Char cEncoded(aFmt.GetBulletChar());
            aFmt.SetBulletChar(OUString(&cEncoded, 1, nCharSet).toChar());
        }
    }

    if( VERSION_30B != nVersion )
    {
        sal_uInt16 nItemCount;
        rStream.ReadUInt16( nCharPoolId );
        sCharFmtName = rStream.ReadUniOrByteString(eEncoding);
        rStream.ReadUInt16( nItemCount );

        while( nItemCount-- )
        {
            sal_uInt16 nWhich, nVers;
            rStream.ReadUInt16( nWhich ).ReadUInt16( nVers );
            aItems.push_back( GetDfltAttr( nWhich )->Create( rStream, nVers ) );
        }
    }

    if( VERSION_40A == nVersion && SVX_NUM_BITMAP == aFmt.GetNumberingType() )
    {
        sal_uInt8 cF;
        sal_Int32 nWidth(0), nHeight(0);

        rStream.ReadInt32( nWidth ).ReadInt32( nHeight );

        Size aSz(nWidth, nHeight);

        rStream.ReadUChar( cF );
        if( cF )
        {
            SvxBrushItem* pBrush = 0;
            SwFmtVertOrient* pVOrient = 0;
            sal_uInt16 nVer;

            if( cF & 1 )
            {
                rStream.ReadUInt16( nVer );
                pBrush = (SvxBrushItem*)GetDfltAttr( RES_BACKGROUND )
                                        ->Create( rStream, nVer );
            }

            if( cF & 2 )
            {
                rStream.ReadUInt16( nVer );
                pVOrient = (SwFmtVertOrient*)GetDfltAttr( RES_VERT_ORIENT )
                                        ->Create( rStream, nVer );
            }
            sal_Int16 eOrient = text::VertOrientation::NONE;
            if(pVOrient)
                eOrient = (sal_Int16)pVOrient->GetVertOrient();
            aFmt.SetGraphicBrush( pBrush, &aSz, pVOrient ? &eOrient : 0 );
        }
    }
}

SwNumRulesWithName::_SwNumFmtGlobal::~_SwNumFmtGlobal()
{
}

void SwNumRulesWithName::_SwNumFmtGlobal::Store( SvStream& rStream )
{
    rtl_TextEncoding eEncoding = osl_getThreadTextEncoding();
    {
        OUString aName;
        sal_uInt16 nFamily = FAMILY_DONTKNOW, nCharSet = 0, nPitch = 0;
        short  nWidth = 0, nHeight = 0;

        const vcl::Font* pFnt = aFmt.GetBulletFont();
        if( pFnt )
        {
            aName = pFnt->GetName();
            nFamily = (sal_uInt16)pFnt->GetFamily();
            nCharSet = (sal_uInt16)pFnt->GetCharSet();
            nWidth = (short)pFnt->GetSize().Width();
            nHeight = (short)pFnt->GetSize().Height();
            nPitch = (sal_uInt16)pFnt->GetPitch();
        }

        rStream.WriteUInt16( aFmt.GetNumberingType() )
               .WriteUInt16( aFmt.GetBulletChar() )
               .WriteUChar( aFmt.GetIncludeUpperLevels() > 0 )
               .WriteUInt16( aFmt.GetStart() );
        rStream.WriteUniOrByteString( aFmt.GetPrefix(), eEncoding );
        rStream.WriteUniOrByteString( aFmt.GetSuffix(), eEncoding );
        rStream.WriteUInt16( aFmt.GetNumAdjust() )
               .WriteInt16( aFmt.GetAbsLSpace() )
               .WriteInt16( aFmt.GetFirstLineOffset() )
               .WriteInt16( aFmt.GetCharTextDistance() )
               .WriteInt16( aFmt.GetLSpace() )
               .WriteUChar( sal_False );//aFmt.IsRelLSpace();
        rStream.WriteUniOrByteString( aName, eEncoding );
        rStream.WriteUInt16( nFamily )
               .WriteUInt16( nCharSet )
               .WriteInt16( nWidth )
               .WriteInt16( nHeight )
               .WriteUInt16( nPitch );
    }
    rStream.WriteUInt16( nCharPoolId );
    rStream.WriteUniOrByteString( sCharFmtName, eEncoding );
    rStream.WriteUInt16( aItems.size() );

    for( sal_uInt16 n = aItems.size(); n; )
    {
        SfxPoolItem* pItem = &aItems[ --n ];
        sal_uInt16 nIVers = pItem->GetVersion( SOFFICE_FILEFORMAT_50 );
        OSL_ENSURE( nIVers != USHRT_MAX,
                "Was'n das: Item-Version USHRT_MAX in der aktuellen Version" );
        rStream.WriteUInt16( pItem->Which() )
               .WriteUInt16( nIVers );
        pItem->Store( rStream, nIVers );
    }

    // Extensions for 40A

    if( SVX_NUM_BITMAP == aFmt.GetNumberingType() )
    {
        rStream.WriteInt32( aFmt.GetGraphicSize().Width() )
               .WriteInt32( aFmt.GetGraphicSize().Height() );
        sal_uInt8 cFlg = ( 0 != aFmt.GetBrush() ? 1 : 0 ) +
                    ( 0 != aFmt.GetGraphicOrientation() ? 2 : 0 );
        rStream.WriteUChar( cFlg );

        if( aFmt.GetBrush() )
        {
            sal_uInt16 nVersion = aFmt.GetBrush()->GetVersion( SOFFICE_FILEFORMAT_50 );
            rStream.WriteUInt16( nVersion );
            aFmt.GetBrush()->Store( rStream, nVersion );
        }
        if( aFmt.GetGraphicOrientation() )
        {
            sal_uInt16 nVersion = aFmt.GetGraphicOrientation()->GetVersion( SOFFICE_FILEFORMAT_50 );
            rStream.WriteUInt16( nVersion );
            aFmt.GetGraphicOrientation()->Store( rStream, nVersion );
        }
    }
}

void SwNumRulesWithName::_SwNumFmtGlobal::ChgNumFmt( SwWrtShell& rSh,
                            SwNumFmt& rNew ) const
{
    SwCharFmt* pFmt = 0;
    if( !sCharFmtName.isEmpty() )
    {
        // at first, look for the name
        sal_uInt16 nArrLen = rSh.GetCharFmtCount();
        for( sal_uInt16 i = 1; i < nArrLen; ++i )
        {
            pFmt = &rSh.GetCharFmt( i );
            if (pFmt->GetName()==sCharFmtName)
                // exists, so leave attributes as they are!
                break;
            pFmt = 0;
        }

        if( !pFmt )
        {
            if( IsPoolUserFmt( nCharPoolId ) )
            {
                pFmt = rSh.MakeCharFmt( sCharFmtName );
                pFmt->SetAuto( false );
            }
            else
                pFmt = rSh.GetCharFmtFromPool( nCharPoolId );

            if( !pFmt->GetDepends() )       // set attributes
                for( sal_uInt16 n = aItems.size(); n; )
                    pFmt->SetFmtAttr( aItems[ --n ] );
        }
    }
    ((SwNumFmt&)aFmt).SetCharFmt( pFmt );
    rNew = aFmt;
    if( pFmt )
        ((SwNumFmt&)aFmt).SetCharFmt( 0 );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */