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