summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoStyleStack.h
blob: 4fbc179371fb27080549112adce04e890bd92132 (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
/* This file is part of the KDE project
   Copyright (c) 2003 Lukas Tinkl <[email protected]>
   Copyright (c) 2003 David Faure <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KOSTYLESTACK_H
#define KOSTYLESTACK_H


#include <tqvaluelist.h>
#include <tqdom.h>
#include <tqvaluestack.h>

#include <kdemacros.h>

#include "koffice_export.h"

/**
 *  @brief This class implements a stack for the different styles of an object.
 *
 *  There can be several styles that are valid for one object. For example
 *  a textobject on a page has styles 'pr3' and 'P7' and a paragraph in
 *  that textobject has styles 'P1' and 'T3'. And some styles even have
 *  tqparent-styles...
 *
 *  If you want to know if there is, for example,  the attribute 'fo:font-family'
 *  for this paragraph, you have to look into style 'T3', 'P1', 'P7' and 'pr3'.
 *  When you find this attribute in one style you have to stop processing the list
 *  and take the found attribute for this object.
 *
 *  This is what this class does. You can push styles on the stack while walking
 *  through the xml-tree to your object and then ask the stack if any of the styles
 *  provides a certain attribute. The stack will search from top to bottom, i.e.
 *  in our example from 'T3' to 'pr3' and return the first occurrence of the wanted
 *  attribute.
 *
 *  So this is some sort of inheritance where the styles on top of the stack overwrite
 *  the same attribute of a lower style on the stack.
 *
 *  In general though, you wouldn't use push/pop directly, but KoOasisLoadingContext::fillStyleStack
 *  or KoOasisLoadingContext::addStyles to automatically push a style and all its
 *  tqparent styles onto the stack.
 */
class KOFFICECORE_EXPORT  KoStyleStack
{
public:
    /**
     * Create a OASIS style stack
     */
    KoStyleStack();
    /**
     * Create a style stack based on other namespaces than OASIS - used for OOo-1.1 import.
     */
    KoStyleStack( const char* styleNSURI, const char* foNSURI );
    virtual ~KoStyleStack();

    /**
     * Clears the complete stack.
     */
    void clear();

    /**
     * Save the current state of the stack. Any items added between
     * this call and its corresponding restore() will be removed when calling restore().
     */
    void save();

    /**
     * Restore the stack to the state it was at the corresponding save() call.
     */
    void restore();

    /**
     * Removes the style on top of the stack.
     */
    void pop();

    /**
     * Pushes the new style onto the stack.
     */
    void push( const TQDomElement& style );

    /**
     * Check if any of the styles on the stack has an attribute called 'name'-'detail'
     * where detail is e.g. left, right, top or bottom.
     * This allows to also find 'name' alone (e.g. padding implies padding-left, padding-right etc.)
     */
    bool hasAttribute( const TQString& name, const TQString& detail = TQString() ) const KDE_DEPRECATED;

    /**
     * Search for the attribute called 'name', starting on top of the stack,
     * and return it.
     */
    TQString attribute( const TQString& name, const TQString& detail = TQString() ) const KDE_DEPRECATED;

    /**
     * Check if any of the styles on the stack has an attribute called 'name'-'detail'
     * where detail is e.g. left, right, top or bottom.
     * This allows to also find 'name' alone (e.g. padding implies padding-left, padding-right etc.)
     */
    bool hasAttributeNS( const char* nsURI, const char* localName, const char* detail = 0 ) const;

    /**
     * Search for the attribute called 'name', starting on top of the stack,
     * and return it.
     */
    TQString attributeNS( const char* nsURI, const char* localName, const char* detail = 0 ) const;

    /**
     * Check if any of the styles on the stack has a child node called 'name'.
     */
    bool hasChildNode( const TQString & name ) const KDE_DEPRECATED;

    /**
     * Search for a child node called 'name', starting on top of the stack,
     * and return it.
     */
    TQDomElement childNode( const TQString & name ) const KDE_DEPRECATED;

    /**
     * Check if any of the styles on the stack has a child element called 'localName' in the namespace 'nsURI'.
     */
    bool hasChildNodeNS( const char* nsURI, const char* localName ) const;

    /**
     * Search for a child element which has a child element called 'localName'
     * in the namespace 'nsURI' starting on top of the stack,
     * and return it.
     */
    TQDomElement childNodeNS( const char* nsURI, const char* localName ) const;

    /**
     * Special case for the current font size, due to special handling of fo:font-size="115%".
     */
    double fontSize() const;

    /**
     * Return the name of the style specified by the user,
     * i.e. not an auto style.
     * This is used to know e.g. which user-style is associated with the current paragraph.
     * There could be none though.
     */
    TQString userStyleName( const TQString& family ) const;

    /**
     * Return the display name of the style specified by the user,
     * i.e. not an auto style
     */
    TQString userStyleDisplayName( const TQString& family ) const;

    /**
     * Set the type of properties that will be looked for.
     * For instance setTypeProperties("paragraph") will make hasAttribute() and attribute()
     * look into "paragraph-properties".
     * If @p typeProperties is 0, the stylestack is resetted to look for "properties"
     * as it does by default.
     */
    void setTypeProperties( const char* typeProperties );

private:
    bool isUserStyle( const TQDomElement& e, const TQString& family ) const;

private:
    /// For save/restore: stack of "marks". Each mark is an index in m_stack.
    TQValueStack<int> m_marks;

    /**
     * We use TQValueList instead of TQValueStack because we need access to all styles
     * not only the top one.
     */
    TQValueList<TQDomElement> m_stack;

    TQCString m_propertiesTagName;

    const char* m_styleNSURI;
    const char* m_foNSURI;

    class KoStyleStackPrivate;
    KoStyleStackPrivate *d;

    // forbidden
    void operator=( const KoStyleStack& );
    KoStyleStack( const KoStyleStack& );
};

#endif /* KOSTYLESTACK_H */