]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
clear()->erase() ; lots of using directives for cxx
[lyx.git] / src / insets / insetcommand.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetcommand.h"
18 #include "debug.h"
19 #include "Painter.h"
20
21 using std::ostream;
22 using std::endl;
23
24
25 InsetCommand::InsetCommand()
26 {
27 }
28
29
30 InsetCommand::InsetCommand(string const & cmd, string const & arg, 
31                            string const & opt)
32         : command(cmd), options(opt), contents(arg)
33 {
34 }
35
36
37 int InsetCommand::ascent(Painter & pain, LyXFont const &) const
38 {
39         LyXFont font(LyXFont::ALL_SANE);
40         font.decSize();
41         
42         int width, ascent, descent;
43         string s = getScreenLabel();
44         
45         if (Editable()) {
46                 pain.buttonText(0, 0, s, font,
47                                 false, width, ascent, descent);
48         } else {
49                 pain.rectText(0, 0, s, font,
50                               LColor::commandbg, LColor::commandframe,
51                               false, width, ascent, descent);
52         }
53         return ascent;
54 }
55
56
57 int InsetCommand::descent(Painter & pain, LyXFont const &) const
58 {
59         LyXFont font(LyXFont::ALL_SANE);
60         font.decSize();
61         
62         int width, ascent, descent;
63         string s = getScreenLabel();
64         
65         if (Editable()) {
66                 pain.buttonText(0, 0, s, font,
67                                 false, width, ascent, descent);
68         } else {
69                 pain.rectText(0, 0, s, font,
70                               LColor::commandbg, LColor::commandframe,
71                               false, width, ascent, descent);
72         }
73         return descent;
74 }
75
76
77 int InsetCommand::width(Painter & pain, LyXFont const &) const
78 {
79         LyXFont font(LyXFont::ALL_SANE);
80         font.decSize();
81         
82         int width, ascent, descent;
83         string s = getScreenLabel();
84         
85         if (Editable()) {
86                 pain.buttonText(0, 0, s, font,
87                                 false, width, ascent, descent);
88         } else {
89                 pain.rectText(0, 0, s, font,
90                               LColor::commandbg, LColor::commandframe,
91                               false, width, ascent, descent);
92         }
93         return width + 4;
94 }
95
96
97 void InsetCommand::draw(Painter & pain, LyXFont const &,
98                         int baseline, float & x) const
99 {
100         // Draw it as a box with the LaTeX text
101         LyXFont font(LyXFont::ALL_SANE);
102         font.setColor(LColor::command).decSize();
103
104         int width;
105         string s = getScreenLabel();
106
107         if (Editable()) {
108                 pain.buttonText(int(x)+2, baseline, s, font, true, width);
109         } else {
110                 pain.rectText(int(x)+2, baseline, s, font,
111                               LColor::commandbg, LColor::commandframe,
112                               true, width);
113         }
114
115         x += width + 4;
116 }
117
118
119 // In lyxf3 this will be just LaTeX
120 void InsetCommand::Write(ostream & os) const
121 {
122         os << "LatexCommand " << getCommand() << "\n";
123 }
124
125
126 void InsetCommand::scanCommand(string const & cmd)
127 {
128         string tcommand, toptions, tcontents;
129
130         if (cmd.empty()) return;
131
132         enum { WS, Command, Option, Content } state = WS;
133         
134         // Used to handle things like \command[foo[bar]]{foo{bar}}
135         int nestdepth = 0;
136
137         for (string::size_type i = 0; i < cmd.length(); ++i) {
138                 char c = cmd[i];
139                 if ((state == Command && c == ' ') ||
140                     (state == Command && c == '[') ||
141                     (state == Command && c == '{')) {
142                         state = WS;
143                 }
144                 if ((state == Option  && c == ']') ||
145                     (state == Content && c == '}')) {
146                         if (nestdepth == 0) {
147                                 state = WS;
148                         } else {
149                                 --nestdepth;
150                         }
151                 }
152                 if ((state == Option  && c == '[') ||
153                     (state == Content && c == '{')) {
154                         ++nestdepth;
155                 }
156                 switch (state) {
157                 case Command:   tcommand += c; break;
158                 case Option:    toptions += c; break;
159                 case Content:   tcontents += c; break;
160                 case WS:
161                         if (c == '\\') {
162                                 state = Command;
163                         } else if (c == '[') {
164                                 state = Option;
165                                 nestdepth = 0; // Just to be sure
166                         } else if (c == '{') {
167                                 state = Content;
168                                 nestdepth = 0; // Just to be sure
169                         }
170                         break;
171                 }
172         }
173
174         // Don't mess with this.
175         if (!tcommand.empty()) command = tcommand;
176         if (!toptions.empty()) options = toptions;
177         if (!tcontents.empty()) setContents(tcontents); 
178                         // setContents is overloaded in InsetInclude
179
180         if (lyxerr.debugging(Debug::PARSER))
181                 lyxerr << "Command <" <<  cmd
182                        << "> == <" << getCommand()
183                        << "> == <" << getCmdName()
184                        << '|' << getContents()
185                        << '|' << getOptions() << '>' << endl;
186 }
187
188
189 // This function will not be necessary when lyx3
190 void InsetCommand::Read(LyXLex & lex)
191 {    
192         if (lex.EatLine()) {
193                 string t = lex.GetString();
194                 scanCommand(t);
195         } else
196                 lex.printError("InsetCommand: Parse error: `$$Token'");
197 }
198
199
200 int InsetCommand::Latex(ostream & os, bool /*fragile*/, bool/*fs*/) const
201 {
202         os << getCommand();
203         return 0;
204 }
205
206
207 int InsetCommand::Ascii(ostream &) const
208 {
209         return 0;
210 }
211
212
213 int InsetCommand::Linuxdoc(ostream &) const
214 {
215         return 0;
216 }
217
218
219 int InsetCommand::DocBook(ostream &) const
220 {
221         return 0;
222 }
223
224
225 Inset * InsetCommand::Clone() const
226 {
227         return new InsetCommand(command, contents, options);
228 }
229
230
231 string InsetCommand::getCommand() const
232 {       
233         string s;
234         if (!command.empty()) s += "\\"+command;
235         if (!options.empty()) s += "["+options+']';
236         s += "{"+contents+'}';
237         return s;
238 }