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