]> git.lyx.org Git - lyx.git/blob - src/lyxtextclass.C
Painter and scrollbar API patches
[lyx.git] / src / lyxtextclass.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "lyxtextclass.h"
19 #include "debug.h"
20 #include "lyxlex.h"
21
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24 #include "support/lyxfunctional.h"
25 #include "support/filetools.h"
26
27 #include <algorithm>
28
29 using std::endl;
30 using std::find_if;
31 using std::remove_if;
32 using std::ostream;
33
34
35 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
36                            string const & desc)
37         : name_(fn), latexname_(cln), description_(desc)
38 {
39         outputType_ = LATEX;
40         columns_ = 1;
41         sides_ = OneSide;
42         secnumdepth_ = 3;
43         tocdepth_ = 3;
44         pagestyle_ = "default";
45         maxcounter_ = LABEL_COUNTER_CHAPTER;
46         defaultfont_ = LyXFont(LyXFont::ALL_SANE);
47         opt_fontsize_ = "10|11|12";
48         opt_pagestyle_ = "empty|plain|headings|fancy";
49         provides_ = nothing;
50         loaded = false;
51 }
52
53
54 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
55 {
56         lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
57         if (!lay.Read(lexrc, *this)) {
58                 // Resolve fonts
59                 lay.resfont = lay.font;
60 #ifndef INHERIT_LANGUAGE
61                 lay.resfont.realize(defaultfont());
62                 lay.reslabelfont = lay.labelfont;
63                 lay.reslabelfont.realize(defaultfont());
64 #else
65                 lay.resfont.realize(defaultfont(), default_language);
66                 lay.reslabelfont = lay.labelfont;
67                 lay.reslabelfont.realize(defaultfont(), default_language);
68 #endif
69                 return false; // no errors
70         }
71         lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
72         return true;
73 }
74
75
76 enum TextClassTags {
77         TC_OUTPUTTYPE = 1,
78         TC_INPUT,
79         TC_STYLE,
80         TC_DEFAULTSTYLE,
81         TC_NOSTYLE,
82         TC_COLUMNS,
83         TC_SIDES,
84         TC_PAGESTYLE,
85         TC_DEFAULTFONT,
86         TC_MAXCOUNTER,
87         TC_SECNUMDEPTH,
88         TC_TOCDEPTH,
89         TC_CLASSOPTIONS,
90         TC_PREAMBLE,
91         TC_PROVIDESAMSMATH,
92         TC_PROVIDESNATBIB,
93         TC_PROVIDESMAKEIDX,
94         TC_PROVIDESURL,
95         TC_LEFTMARGIN,
96         TC_RIGHTMARGIN
97 };
98
99
100 // Reads a textclass structure from file.
101 bool LyXTextClass::Read(string const & filename, bool merge)
102 {
103         keyword_item textClassTags[] = {
104                 { "classoptions",    TC_CLASSOPTIONS },
105                 { "columns",         TC_COLUMNS },
106                 { "defaultfont",     TC_DEFAULTFONT },
107                 { "defaultstyle",    TC_DEFAULTSTYLE },
108                 { "input",           TC_INPUT },
109                 { "leftmargin",      TC_LEFTMARGIN },
110                 { "maxcounter",      TC_MAXCOUNTER },
111                 { "nostyle",         TC_NOSTYLE },
112                 { "outputtype",      TC_OUTPUTTYPE },
113                 { "pagestyle",       TC_PAGESTYLE },
114                 { "preamble",        TC_PREAMBLE },
115                 { "providesamsmath", TC_PROVIDESAMSMATH },
116                 { "providesmakeidx", TC_PROVIDESMAKEIDX },
117                 { "providesnatbib",  TC_PROVIDESNATBIB },
118                 { "providesurl",     TC_PROVIDESURL },
119                 { "rightmargin",     TC_RIGHTMARGIN },
120                 { "secnumdepth",     TC_SECNUMDEPTH },
121                 { "sides",           TC_SIDES },
122                 { "style",           TC_STYLE },
123                 { "tocdepth",        TC_TOCDEPTH }
124         };
125
126         if (!merge)
127                 lyxerr[Debug::TCLASS] << "Reading textclass "
128                                       << MakeDisplayPath(filename)
129                                       << endl;
130         else
131                 lyxerr[Debug::TCLASS] << "Reading input file "
132                                      << MakeDisplayPath(filename)
133                                      << endl;
134
135         LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
136         bool error = false;
137
138         lexrc.setFile(filename);
139         if (!lexrc.isOK()) error = true;
140
141         // parsing
142         while (lexrc.isOK() && !error) {
143                 int le = lexrc.lex();
144                 switch (le) {
145                 case LyXLex::LEX_FEOF:
146                         continue;
147
148                 case LyXLex::LEX_UNDEF:
149                         lexrc.printError("Unknown TextClass tag `$$Token'");
150                         error = true;
151                         continue;
152                 default: break;
153                 }
154                 switch (static_cast<TextClassTags>(le)) {
155                 case TC_OUTPUTTYPE:   // output type definition
156                         readOutputType(lexrc);
157                         break;
158
159                 case TC_INPUT: // Include file
160                         if (lexrc.next()) {
161                                 string tmp = LibFileSearch("layouts",
162                                                             lexrc.getString(),
163                                                             "layout");
164
165                                 if (Read(tmp, true)) {
166                                         lexrc.printError("Error reading input"
167                                                          "file: "+tmp);
168                                         error = true;
169                                 }
170                         }
171                         break;
172
173                 case TC_DEFAULTSTYLE:
174                         if (lexrc.next()) {
175                                 string const name = subst(lexrc.getString(),
176                                                           '_', ' ');
177                                 defaultlayout_ = name;
178                         }
179                         break;
180
181                 case TC_STYLE:
182                         if (lexrc.next()) {
183                                 string const name = subst(lexrc.getString(),
184                                                     '_', ' ');
185                                 if (hasLayout(name)) {
186                                         LyXLayout & lay =
187                                                 const_cast<LyXLayout &>(operator[](name));
188                                         error = do_readStyle(lexrc, lay);
189                                 } else {
190                                         LyXLayout lay;
191                                         lay.setName(name);
192                                         if (!(error = do_readStyle(lexrc, lay)))
193                                                 layoutlist.push_back(lay);
194                                         if (defaultlayout_.empty()) {
195                                                 // We do not have a default
196                                                 // layout yet, so we choose
197                                                 // the first layout we
198                                                 // encounter.
199                                                 defaultlayout_ = name;
200                                         }
201                                 }
202                         }
203                         else {
204                                 lexrc.printError("No name given for style: `$$Token'.");
205                                 error = true;
206                         }
207                         break;
208
209                 case TC_NOSTYLE:
210                         if (lexrc.next()) {
211                                 string const style = subst(lexrc.getString(),
212                                                      '_', ' ');
213                                 if (!delete_layout(style))
214                                         lyxerr << "Cannot delete style `" << style << "'" << endl;
215 //                                      lexrc.printError("Cannot delete style"
216 //                                                       " `$$Token'");
217                         }
218                         break;
219
220                 case TC_COLUMNS:
221                         if (lexrc.next())
222                                 columns_ = lexrc.getInteger();
223                         break;
224
225                 case TC_SIDES:
226                         if (lexrc.next()) {
227                                 switch (lexrc.getInteger()) {
228                                 case 1: sides_ = OneSide; break;
229                                 case 2: sides_ = TwoSides; break;
230                                 default:
231                                         lyxerr << "Impossible number of page"
232                                                 " sides, setting to one."
233                                                << endl;
234                                         sides_ = OneSide;
235                                         break;
236                                 }
237                         }
238                         break;
239
240                 case TC_PAGESTYLE:
241                         lexrc.next();
242                         pagestyle_ = strip(lexrc.getString());
243                         break;
244
245                 case TC_DEFAULTFONT:
246                         defaultfont_.lyxRead(lexrc);
247                         if (!defaultfont_.resolved()) {
248                                 lexrc.printError("Warning: defaultfont should "
249                                                  "be fully instantiated!");
250 #ifndef INHERIT_LANGUAGE
251                                 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
252 #else
253                                 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
254                                                      default_language);
255 #endif
256                         }
257                         break;
258
259                 case TC_MAXCOUNTER:
260                         readMaxCounter(lexrc);
261                         break;
262
263                 case TC_SECNUMDEPTH:
264                         lexrc.next();
265                         secnumdepth_ = lexrc.getInteger();
266                         break;
267
268                 case TC_TOCDEPTH:
269                         lexrc.next();
270                         tocdepth_ = lexrc.getInteger();
271                         break;
272
273                         // First step to support options
274                 case TC_CLASSOPTIONS:
275                         readClassOptions(lexrc);
276                         break;
277
278                 case TC_PREAMBLE:
279                         preamble_ = lexrc.getLongString("EndPreamble");
280                         break;
281
282                 case TC_PROVIDESAMSMATH:
283                         if (lexrc.next() && lexrc.getInteger())
284                                 provides_ |= amsmath;
285                         break;
286
287                 case TC_PROVIDESNATBIB:
288                         if (lexrc.next() && lexrc.getInteger())
289                                 provides_ |= natbib;
290                         break;
291
292                 case TC_PROVIDESMAKEIDX:
293                         if (lexrc.next() && lexrc.getInteger())
294                                 provides_ |= makeidx;
295                         break;
296
297                 case TC_PROVIDESURL:
298                         if (lexrc.next() && lexrc.getInteger())
299                                 provides_ |= url;
300                         break;
301
302                 case TC_LEFTMARGIN:     // left margin type
303                         if (lexrc.next())
304                                 leftmargin_ = lexrc.getString();
305                         break;
306
307                 case TC_RIGHTMARGIN:    // right margin type
308                         if (lexrc.next())
309                                 rightmargin_ = lexrc.getString();
310                         break;
311                 }
312         }
313
314         if (!merge) { // we are at top level here.
315                 lyxerr[Debug::TCLASS] << "Finished reading textclass "
316                                       << MakeDisplayPath(filename)
317                                       << endl;
318                 if (defaultlayout_.empty()) {
319                         lyxerr << "Error: Textclass '" << name_
320                                << "' is missing a defaultstyle." << endl;
321                         error = true;
322                 }
323         } else
324                 lyxerr[Debug::TCLASS] << "Finished reading input file "
325                                       << MakeDisplayPath(filename)
326                                       << endl;
327
328         return error;
329 }
330
331
332 void LyXTextClass::readOutputType(LyXLex & lexrc)
333 {
334         keyword_item outputTypeTags[] = {
335                 { "docbook", DOCBOOK },
336                 { "latex", LATEX },
337                 { "linuxdoc", LINUXDOC },
338                 { "literate", LITERATE }
339         };
340
341         pushpophelper pph(lexrc, outputTypeTags, LITERATE);
342
343         int le = lexrc.lex();
344         switch (le) {
345         case LyXLex::LEX_UNDEF:
346                 lexrc.printError("Unknown output type `$$Token'");
347                 return;
348         case LATEX:
349         case LINUXDOC:
350         case DOCBOOK:
351         case LITERATE:
352                 outputType_ = static_cast<OutputType>(le);
353                 break;
354         default:
355                 lyxerr << "Unhandled value " << le
356                        << " in LyXTextClass::readOutputType." << endl;
357
358                 break;
359         }
360 }
361
362
363 enum MaxCounterTags {
364         MC_COUNTER_CHAPTER = 1,
365         MC_COUNTER_SECTION,
366         MC_COUNTER_SUBSECTION,
367         MC_COUNTER_SUBSUBSECTION,
368         MC_COUNTER_PARAGRAPH,
369         MC_COUNTER_SUBPARAGRAPH,
370         MC_COUNTER_ENUMI,
371         MC_COUNTER_ENUMII,
372         MC_COUNTER_ENUMIII,
373         MC_COUNTER_ENUMIV
374 };
375
376
377 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
378 {
379         keyword_item maxCounterTags[] = {
380                 {"counter_chapter", MC_COUNTER_CHAPTER },
381                 {"counter_enumi", MC_COUNTER_ENUMI },
382                 {"counter_enumii", MC_COUNTER_ENUMII },
383                 {"counter_enumiii", MC_COUNTER_ENUMIII },
384                 {"counter_enumiv", MC_COUNTER_ENUMIV },
385                 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
386                 {"counter_section", MC_COUNTER_SECTION },
387                 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
388                 {"counter_subsection", MC_COUNTER_SUBSECTION },
389                 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
390         };
391
392         pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
393         int le = lexrc.lex();
394         switch (le) {
395         case LyXLex::LEX_UNDEF:
396                 lexrc.printError("Unknown MaxCounter tag `$$Token'");
397                 return;
398         default: break;
399         }
400         switch (static_cast<MaxCounterTags>(le)) {
401         case MC_COUNTER_CHAPTER:
402                 maxcounter_ = LABEL_COUNTER_CHAPTER;
403                 break;
404         case MC_COUNTER_SECTION:
405                 maxcounter_ = LABEL_COUNTER_SECTION;
406                 break;
407         case MC_COUNTER_SUBSECTION:
408                 maxcounter_ = LABEL_COUNTER_SUBSECTION;
409                 break;
410         case MC_COUNTER_SUBSUBSECTION:
411                 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
412                 break;
413         case MC_COUNTER_PARAGRAPH:
414                 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
415                 break;
416         case MC_COUNTER_SUBPARAGRAPH:
417                 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
418                 break;
419         case MC_COUNTER_ENUMI:
420                 maxcounter_ = LABEL_COUNTER_ENUMI;
421                 break;
422         case MC_COUNTER_ENUMII:
423                 maxcounter_ = LABEL_COUNTER_ENUMII;
424                 break;
425         case MC_COUNTER_ENUMIII:
426                 maxcounter_ = LABEL_COUNTER_ENUMIII;
427                 break;
428         case MC_COUNTER_ENUMIV:
429                 maxcounter_ = LABEL_COUNTER_ENUMIV;
430                 break;
431         }
432 }
433
434
435 enum ClassOptionsTags {
436         CO_FONTSIZE = 1,
437         CO_PAGESTYLE,
438         CO_OTHER,
439         CO_END
440 };
441
442
443 void LyXTextClass::readClassOptions(LyXLex & lexrc)
444 {
445         keyword_item classOptionsTags[] = {
446                 {"end", CO_END },
447                 {"fontsize", CO_FONTSIZE },
448                 {"other", CO_OTHER },
449                 {"pagestyle", CO_PAGESTYLE }
450         };
451
452         lexrc.pushTable(classOptionsTags, CO_END);
453         bool getout = false;
454         while (!getout && lexrc.isOK()) {
455                 int le = lexrc.lex();
456                 switch (le) {
457                 case LyXLex::LEX_UNDEF:
458                         lexrc.printError("Unknown ClassOption tag `$$Token'");
459                         continue;
460                 default: break;
461                 }
462                 switch (static_cast<ClassOptionsTags>(le)) {
463                 case CO_FONTSIZE:
464                         lexrc.next();
465                         opt_fontsize_ = strip(lexrc.getString());
466                         break;
467                 case CO_PAGESTYLE:
468                         lexrc.next();
469                         opt_pagestyle_ = strip(lexrc.getString());
470                         break;
471                 case CO_OTHER:
472                         lexrc.next();
473                         options_ = lexrc.getString();
474                         break;
475                 case CO_END:
476                         getout = true;
477                         break;
478                 }
479         }
480         lexrc.popTable();
481 }
482
483
484 LyXFont const & LyXTextClass::defaultfont() const
485 {
486         return defaultfont_;
487 }
488
489
490 string const & LyXTextClass::leftmargin() const
491 {
492         return leftmargin_;
493 }
494
495
496 string const & LyXTextClass::rightmargin() const
497 {
498         return rightmargin_;
499 }
500
501
502 bool LyXTextClass::hasLayout(string const & n) const
503 {
504         string const name = (n.empty() ? defaultLayoutName() : n);
505
506         return find_if(layoutlist.begin(), layoutlist.end(),
507                        lyx::compare_memfun(&LyXLayout::name, name))
508                 != layoutlist.end();
509 }
510
511
512 LyXLayout const & LyXTextClass::operator[](string const & n) const
513 {
514         lyx::Assert(!n.empty());
515
516         if (n.empty())
517                 lyxerr << "Operator[] called with empty n" << endl;
518
519         string const name = (n.empty() ? defaultLayoutName() : n);
520
521         static string lastLayoutName;
522         static LayoutList::difference_type lastLayoutIndex;
523
524         if (name == lastLayoutName)
525                 return layoutlist[lastLayoutIndex];
526
527         LayoutList::const_iterator cit =
528                 find_if(layoutlist.begin(),
529                         layoutlist.end(),
530                         lyx::compare_memfun(&LyXLayout::name, name));
531
532         if (cit == layoutlist.end()) {
533                 lyxerr << "We failed to find the layout '" << name
534                        << "' in the layout list. You MUST investigate!"
535                        << endl;
536
537                 // we require the name to exist
538                 lyx::Assert(false);
539         }
540
541         lastLayoutName = name;
542         lastLayoutIndex = std::distance(layoutlist.begin(), cit);
543
544         return *cit;
545 }
546
547
548 bool LyXTextClass::delete_layout(string const & name)
549 {
550         if (name == defaultLayoutName())
551                 return false;
552
553         LayoutList::iterator it =
554                 remove_if(layoutlist.begin(), layoutlist.end(),
555                           lyx::compare_memfun(&LyXLayout::name, name));
556         LayoutList::iterator end = layoutlist.end();
557         bool const ret = (it != end);
558         layoutlist.erase(it, end);
559         return ret;
560 }
561
562
563 // Load textclass info if not loaded yet
564 bool LyXTextClass::load() const
565 {
566         if (loaded)
567                 return true;
568
569         // Read style-file
570         string const real_file = LibFileSearch("layouts", name_, "layout");
571
572         if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
573                 lyxerr << "Error reading `"
574                        << MakeDisplayPath(real_file)
575                        << "'\n(Check `" << name_
576                        << "')\nCheck your installation and "
577                         "try Options/Reconfigure..." << endl;
578                 loaded = false;
579         }
580         loaded = true;
581         return loaded;
582 }
583
584
585 string const LyXTextClass::defaultLayoutName() const
586 {
587         // This really should come from the actual layout... (Lgb)
588         return defaultlayout_;
589 }
590
591
592 LyXLayout const & LyXTextClass::defaultLayout() const
593 {
594         return operator[](defaultLayoutName());
595 }
596
597
598 string const & LyXTextClass::name() const
599 {
600         return name_;
601 }
602
603
604 string const & LyXTextClass::latexname() const
605 {
606         const_cast<LyXTextClass*>(this)->load();
607         return latexname_;
608 }
609
610
611 string const & LyXTextClass::description() const
612 {
613         return description_;
614 }
615
616
617 string const & LyXTextClass::opt_fontsize() const
618 {
619         return opt_fontsize_;
620 }
621
622
623 string const & LyXTextClass::opt_pagestyle() const
624 {
625         return opt_pagestyle_;
626 }
627
628
629 string const & LyXTextClass::options() const
630 {
631         return options_;
632 }
633
634
635 string const & LyXTextClass::pagestyle() const
636 {
637         return pagestyle_;
638 }
639
640
641 string const & LyXTextClass::preamble() const
642 {
643         return preamble_;
644 }
645
646
647 LyXTextClass::PageSides LyXTextClass::sides() const
648 {
649         return sides_;
650 }
651
652
653 int LyXTextClass::secnumdepth() const
654 {
655         return secnumdepth_;
656 }
657
658
659 int LyXTextClass::tocdepth() const
660 {
661         return tocdepth_;
662 }
663
664
665 OutputType LyXTextClass::outputType() const
666 {
667         return outputType_;
668 }
669
670
671 bool LyXTextClass::provides(LyXTextClass::Provides p) const
672 {
673         return provides_ & p;
674 }
675
676
677 unsigned int LyXTextClass::columns() const
678 {
679         return columns_;
680 }
681
682
683 int LyXTextClass::maxcounter() const
684 {
685         return maxcounter_;
686 }
687
688
689 int LyXTextClass::size() const
690 {
691         return layoutlist.size();
692 }
693
694
695 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
696 {
697         switch (p) {
698         case LyXTextClass::OneSide:
699                 os << "1";
700                 break;
701         case LyXTextClass::TwoSides:
702                 os << "2";
703                 break;
704         }
705         return os;
706 }