]> git.lyx.org Git - features.git/blob - src/Bullet.h
4ad38431cd687db4afd959e60b84fa6201e65f9c
[features.git] / src / Bullet.h
1 // -*- C++ -*-
2 /* This is the bullet class definition file.
3  * This file is part of
4  * ====================================================== 
5  *
6  *           LyX, The Document Processor
7  *
8  *           Copyright 1995 Matthias Ettrich
9  *           Copyright 1995-2000 The LyX Team.
10  *
11  *           This file Copyright 1997-1999
12  *           Allan Rae
13  * ====================================================== */
14
15 #ifndef BULLET_H
16 #define BULLET_H
17
18 #ifdef __GNUG__
19 #pragma interface
20 #endif
21
22 #include "LString.h"
23
24 #include "support/LAssert.h"
25
26 ///
27 class Bullet {
28 public:
29         ///
30         Bullet(const int f = -1, const int c = -1, const int s = -1);
31
32         ///
33         Bullet(string const &);
34
35         ///
36         void setCharacter(int);
37         ///
38         void setFont(int);
39         ///
40         void setSize(int);
41         ///
42         void setText(string const &);
43         ///
44         int getCharacter() const;
45         ///
46         int getFont() const;
47         ///
48         int getSize() const;
49         ///
50         string getText() const;
51         ///
52         string getText();
53         ///
54         char const * c_str() const;
55         ///
56         Bullet & operator = (Bullet const &);
57         ///
58         friend bool operator==(Bullet const &, Bullet const &);
59         ///
60         friend bool operator!=(Bullet const & b1, Bullet const & b2) {
61                 return !(b1 == b2);
62         }
63 protected:
64 #ifdef ENABLE_ASSERTIONS
65         void testInvariant() const {
66                 Assert(font >= MIN);
67                 Assert(font < FONTMAX);
68                 Assert(character >= MIN);
69                 Assert(character < CHARMAX);
70                 Assert(size >= MIN);
71                 Assert(size < SIZEMAX);
72                 Assert(user_text >= -1);
73                 Assert(user_text <= 1);
74                 // now some relational/operational tests
75                 if (user_text == 1) {
76                         Assert(font == -1 && (character == -1 && size == -1));
77                         //        Assert(!text.empty()); // this isn't necessarily an error
78                 }
79                 //      else if (user_text == -1) {
80                 //        Assert(!text.empty()); // this also isn't necessarily an error
81                 //      }
82                 //      else {
83                 //        // user_text == 0
84                 //        Assert(text.empty()); // not usually true
85                 //      }
86         }
87 #endif
88 private:
89         /**
90            This enum makes adding additional panels or changing panel sizes
91            easier. Since you only need change these values for all tests to
92            be correct for the new values.
93            
94            Note: MAX means the size of the array so to test you need:
95            (x < MAX)  *not* (x <= MAX)
96         */
97         enum {
98                 ///
99                 MIN = -1,
100                 ///
101                 FONTMAX = 6,
102                 ///
103                 CHARMAX = 36,
104                 ///
105                 SIZEMAX = 10
106         };
107         
108         ///
109         void generateText();
110         ///
111         static string bulletSize(short int);
112         ///
113         static string bulletEntry(short int, short int);
114         
115         ///
116         short font;
117         ///
118         short character;
119         ///
120         short size;
121         
122         // size, character and font are array indices to access 
123         // the predefined arrays of LaTeX equivalent strings.
124         
125         /** flag indicates if user has control of text (1)
126             or if I can use it to generate strings (0)
127             or have already (-1)
128         */
129         short user_text; 
130         
131         //NOTE: Arranging these four shorts above to be together
132         //      like this should ensure they are in a single cache line
133         
134         /** text may contain a user-defined LaTeX symbol command
135             or one generated internally from the font, character
136             and size settings.
137         */
138         string text;
139 };
140
141
142 /*----------------Inline Bullet Member Functions------------------*/
143
144 inline Bullet::Bullet(string const & t) 
145         :  font(MIN), character(MIN), size(MIN), user_text(1), text(t)
146 {
147 #ifdef ENABLE_ASSERTIONS
148         testInvariant();
149 #endif
150 }
151
152
153 inline void Bullet::setCharacter(int c)
154 {
155         if (c < MIN || c >= CHARMAX) {
156                 character = MIN;
157         } else {
158                 character = c;
159         }
160         user_text = 0;
161 #ifdef ENABLE_ASSERTIONS
162         testInvariant();
163 #endif
164 }
165
166
167 inline void Bullet::setFont(int f)
168 {
169         if (f < MIN || f >= FONTMAX) {
170                 font = MIN;
171         } else {
172                 font = f;
173         }
174         user_text = 0;
175 #ifdef ENABLE_ASSERTIONS
176         testInvariant();
177 #endif
178 }
179
180
181 inline void Bullet::setSize(int s)
182 {
183         if (s < MIN || s >= SIZEMAX) {
184                 size = MIN;
185         } else {
186                 size = s;
187         }
188         user_text = 0;
189 #ifdef ENABLE_ASSERTIONS
190         testInvariant();
191 #endif
192 }
193
194
195 inline void Bullet::setText(string const & t)
196 {
197         font = character = size = MIN;
198         user_text = 1;
199         text = t;
200 #ifdef ENABLE_ASSERTIONS
201         testInvariant();
202 #endif
203 }
204
205
206 inline int Bullet::getCharacter() const
207 {
208         return character;
209 }
210
211
212 inline int Bullet::getFont() const
213 {
214         return font;
215 }
216
217
218 inline int Bullet::getSize() const
219 {
220         return size;
221 }
222
223
224 inline string Bullet::getText() const
225 {
226         return text;
227 }
228
229
230 inline Bullet & Bullet::operator=(Bullet const & b)
231 {
232 #ifdef ENABLE_ASSERTIONS
233         b.testInvariant();
234 #endif
235         font = b.font;
236         character = b.character;
237         size = b.size;
238         user_text = b.user_text;
239         text = b.text;
240 #ifdef ENABLE_ASSERTIONS
241         this->testInvariant();
242 #endif
243         return *this;
244 }
245
246
247 inline char const * Bullet::c_str() const
248 {
249         return this->getText().c_str();
250 }
251
252
253 /*-----------------End Bullet Member Functions-----------------*/
254
255 extern
256 Bullet const ITEMIZE_DEFAULTS[];
257
258 #endif /* BULLET_H_ */