]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
437924d0acb2a6a69c9eabddea8dffcfec19b503
[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-1999 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 "lyxdraw.h"
19 #include "debug.h"
20
21 InsetCommand::InsetCommand()
22 {
23 }
24
25
26 InsetCommand::InsetCommand(string const & cmd, string const & arg, 
27                            string const & opt)
28         : command(cmd), options(opt), contents(arg)
29 {
30 }
31
32
33 int InsetCommand::Ascent(LyXFont const & font) const
34 {
35         LyXFont f = font;
36         f.decSize();
37         return f.maxAscent() + 3;
38 }
39
40
41 int InsetCommand::Descent(LyXFont const & font) const
42 {
43         LyXFont f = font;
44         f.decSize();
45         return f.maxDescent() + 3;
46 }
47
48
49 int InsetCommand::Width(LyXFont const & font) const
50 {
51         LyXFont f = font;
52         f.decSize();
53         string s = getScreenLabel();
54         return 10 + f.stringWidth(s);
55 }
56
57
58 void InsetCommand::Draw(LyXFont font, LyXScreen & scr,
59                       int baseline, float & x)
60 {
61         // Draw it as a box with the LaTeX text
62         x += 3;
63
64         scr.fillRectangle(gc_lighted,
65                           int(x), baseline - Ascent(font) + 1,
66                           Width(font) - 6,
67                           Ascent(font) + Descent(font)-2);
68         // Tell whether this slows down the drawing  (ale)
69         // lets draw editable and non-editable insets differently
70         if (Editable()) {
71                 int y = baseline - Ascent(font)+1, w = Width(font)-6,
72                         h = (Ascent(font)+Descent(font)-2);
73                 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
74         } else {
75                 scr.drawRectangle(gc_note_frame,
76                                   int(x), baseline - Ascent(font)+1,
77                                   Width(font)-6,
78                                   Ascent(font)+Descent(font)-2); 
79         }
80         string s = getScreenLabel();
81         LyXFont f = font;
82         f.decSize();
83         f.setColor(LyXFont::NONE);
84         f.setLatex(LyXFont::OFF);
85         scr.drawString(f, s, baseline, int(x+2));
86
87         x +=  Width(font) - 3;
88 }
89
90
91 // In lyxf3 this will be just LaTeX
92 void InsetCommand::Write(FILE * file)
93 {
94         fprintf(file, "LatexCommand %s\n", getCommand().c_str());
95 }
96
97
98 void InsetCommand::scanCommand(string const & cmd)
99 {
100         string tcommand, toptions, tcontents;
101
102         if (cmd.empty()) return;
103
104         enum { WS, Command, Option, Content } state = WS;
105         
106         // Used to handle things like \command[foo[bar]]{foo{bar}}
107         int nestdepth = 0;
108
109         for (string::size_type i = 0; i < cmd.length(); ++i) {
110                 char c = cmd[i];
111                 if ((state == Command && c == ' ') ||
112                     (state == Command && c == '[') ||
113                     (state == Command && c == '{')) {
114                         state = WS;
115                 }
116                 if ((state == Option  && c == ']') ||
117                     (state == Content && c == '}')) {
118                         if (nestdepth == 0) {
119                                 state = WS;
120                         } else {
121                                 nestdepth--;
122                         }
123                 }
124                 if ((state == Option  && c == '[') ||
125                     (state == Content && c == '{')) {
126                         nestdepth++;
127                 }
128                 switch (state) {
129                 case Command:   tcommand += c; break;
130                 case Option:    toptions += c; break;
131                 case Content:   tcontents += c; break;
132                 case WS:
133                         if (c == '\\') {
134                                 state = Command;
135                         } else if (c == '[') {
136                                 state = Option;
137                                 nestdepth = 0; // Just to be sure
138                         } else if (c == '{') {
139                                 state = Content;
140                                 nestdepth = 0; // Just to be sure
141                         }
142                         break;
143                 }
144         }
145
146         // Don't mess with this.
147         if (!tcommand.empty()) command = tcommand;
148         if (!toptions.empty()) options = toptions;
149         if (!tcontents.empty()) setContents(tcontents); 
150                         // setContents is overloaded in InsetInclude
151
152         if (lyxerr.debugging(Debug::PARSER))
153                 lyxerr << "Command <" <<  cmd
154                        << "> == <" << getCommand()
155                        << "> == <" << getCmdName()
156                        << '|' << getContents()
157                        << '|' << getOptions() << '>' << endl;
158 }
159
160
161 // This function will not be necessary when lyx3
162 void InsetCommand::Read(LyXLex & lex)
163 {    
164         if (lex.EatLine()) {
165                 string t = lex.GetString();
166                 scanCommand(t);
167         } else
168                 lex.printError("InsetCommand: Parse error: `$$Token'");
169 }
170
171
172 int InsetCommand::Latex(FILE * file, signed char /*fragile*/)
173 {
174         fprintf(file, "%s", getCommand().c_str());
175         return 0;
176 }
177
178
179 int InsetCommand::Latex(string & file, signed char /*fragile*/)
180 {
181         file += getCommand();
182         return 0;
183 }
184
185
186 int InsetCommand::Linuxdoc(string &/*file*/)
187 {
188         return 0;
189 }
190
191
192 int InsetCommand::DocBook(string &/*file*/)
193 {
194         return 0;
195 }
196
197
198 Inset * InsetCommand::Clone() const
199 {
200         return new InsetCommand(command, contents, options);
201 }
202
203
204 string InsetCommand::getCommand() const
205 {       
206         string s;
207         if (!command.empty()) s += "\\"+command;
208         if (!options.empty()) s += "["+options+']';
209         s += "{"+contents+'}';
210         return s;
211 }