]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
another fix
[lyx.git] / src / insets / insetert.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1998 The LyX Team.
7  *
8  *======================================================*/
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetert.h"
17 #include "insettext.h"
18
19 #include "buffer.h"
20 #include "BufferView.h"
21 #include "debug.h"
22 #include "funcrequest.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "lyxfont.h"
26 #include "lyxlex.h"
27 #include "lyxrow.h"
28 #include "lyxtext.h"
29 #include "WordLangTuple.h"
30
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34
35 #include "support/LOstream.h"
36
37
38 using std::ostream;
39 using std::min;
40 using std::endl;
41
42 using lyx::pos_type;
43
44
45 void InsetERT::init()
46 {
47         setButtonLabel();
48         labelfont = LyXFont(LyXFont::ALL_SANE);
49         labelfont.decSize();
50         labelfont.decSize();
51         labelfont.setColor(LColor::latex);
52         setInsetName("ERT");
53 }
54
55
56 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
57         : InsetCollapsable(bp, collapsed)
58 {
59         if (collapsed)
60                 status_ = Collapsed;
61         else
62                 status_ = Open;
63         init();
64 }
65
66
67 InsetERT::InsetERT(InsetERT const & in, bool same_id)
68         : InsetCollapsable(in, same_id), status_(in.status_)
69 {
70         init();
71 }
72
73
74 Inset * InsetERT::clone(Buffer const &, bool same_id) const
75 {
76         return new InsetERT(*const_cast<InsetERT *>(this), same_id);
77 }
78
79
80 InsetERT::InsetERT(BufferParams const & bp,
81                    Language const * l, string const & contents, bool collapsed)
82         : InsetCollapsable(bp, collapsed)
83 {
84         if (collapsed)
85                 status_ = Collapsed;
86         else
87                 status_ = Open;
88
89         LyXFont font(LyXFont::ALL_INHERIT, l);
90 #ifdef SET_HARD_FONT
91         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
92         font.setColor(LColor::latex);
93 #endif
94
95         string::const_iterator cit = contents.begin();
96         string::const_iterator end = contents.end();
97         pos_type pos = 0;
98         for (; cit != end; ++cit) {
99                 inset.paragraph()->insertChar(pos++, *cit, font);
100         }
101         // the init has to be after the initialization of the paragraph
102         // because of the label settings (draw_label for ert insets).
103         init();
104 }
105
106
107 InsetERT::~InsetERT()
108 {
109         hideDialog();
110 }
111
112
113 void InsetERT::read(Buffer const * buf, LyXLex & lex)
114 {
115         bool token_found = false;
116         if (lex.isOK()) {
117                 lex.next();
118                 string const token = lex.getString();
119                 if (token == "status") {
120                         lex.next();
121                         string const tmp_token = lex.getString();
122
123                         if (tmp_token == "Inlined") {
124                                 status(0, Inlined);
125                         } else if (tmp_token == "Collapsed") {
126                                 status(0, Collapsed);
127                         } else {
128                                 // leave this as default!
129                                 status(0, Open);
130                         }
131
132                         token_found = true;
133                 } else {
134                         lyxerr << "InsetERT::Read: Missing 'status'-tag!"
135                                    << endl;
136                         // take countermeasures
137                         lex.pushToken(token);
138                 }
139         }
140 #if 0
141 #warning this should be really short lived only for compatibility to
142 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
143         if (lex.isOK()) {
144                 lex.next();
145                 string const token = lex.getString();
146                 if (token == "collapsed") {
147                         lex.next();
148                         collapsed_ = lex.getBool();
149                 } else {
150                         // Take countermeasures
151                         lex.pushToken(token);
152                 }
153         }
154 #endif
155         inset.read(buf, lex);
156
157 #ifdef SET_HARD_FONT
158         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
159         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
160         font.setColor(LColor::latex);
161         Paragraph * par = inset.paragraph();
162         while (par) {
163                 pos_type siz = par->size();
164                 for (pos_type i = 0; i < siz; ++i) {
165                         par->setFont(i, font);
166                 }
167                 par = par->next();
168         }
169 #endif
170
171         if (!token_found) {
172                 if (collapsed_) {
173                         status(0, Collapsed);
174                 } else {
175                         status(0, Open);
176                 }
177         }
178         setButtonLabel();
179 }
180
181
182 void InsetERT::write(Buffer const * buf, ostream & os) const
183 {
184         string st;
185
186         switch (status_) {
187         case Open:
188                 st = "Open";
189                 break;
190         case Collapsed:
191                 st = "Collapsed";
192                 break;
193         case Inlined:
194                 st = "Inlined";
195                 break;
196         }
197
198         os << getInsetName() << "\n"
199            << "status "<< st << "\n";
200
201         //inset.writeParagraphData(buf, os);
202         string const layout(buf->params.getLyXTextClass().defaultLayoutName());
203         Paragraph * par = inset.paragraph();
204         while (par) {
205                 os << "\n\\layout " << layout << "\n";
206                 pos_type siz = par->size();
207                 for (pos_type i = 0; i < siz; ++i) {
208                         Paragraph::value_type c = par->getChar(i);
209                         switch (c) {
210                         case Paragraph::META_INSET:
211                         case Paragraph::META_HFILL:
212                                 lyxerr << "Element is not allowed in insertERT"
213                                        << endl;
214                         case Paragraph::META_NEWLINE:
215                                 os << "\n\\newline \n";
216                                 break;
217                         case '\\':
218                                 os << "\n\\backslash \n";
219                                 break;
220                         default:
221                                 os << c;
222                                 break;
223                         }
224                 }
225                 par = par->next();
226         }
227 }
228
229
230 string const InsetERT::editMessage() const
231 {
232         return _("Opened ERT Inset");
233 }
234
235
236 bool InsetERT::insertInset(BufferView *, Inset *)
237 {
238         return false;
239 }
240
241
242 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
243 {
244         // if selectall is activated then the fontchange was an outside general
245         // fontchange and this messages is not needed
246         if (!selectall)
247                 Alert::alert(_("Impossible Operation!"),
248                            _("Not permitted to change font-types inside ERT-insets!"),
249                            _("Sorry."));
250 }
251
252
253 void InsetERT::updateStatus(BufferView * bv, bool swap) const
254 {
255         if (status_ != Inlined) {
256                 if (collapsed_) {
257                         status(bv, swap ? Open : Collapsed);
258                 } else {
259                         status(bv, swap ? Collapsed : Open);
260                 }
261         }
262 }
263
264 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
265 {
266         if (button == mouse_button::button3)
267                 return;
268
269         if (status_ == Inlined) {
270                 if (!bv->lockInset(this))
271                         return;
272                 inset.edit(bv, x, y, button);
273         } else {
274                 InsetCollapsable::edit(bv, x, y, button);
275         }
276         set_latex_font(bv);
277         updateStatus(bv);
278 }
279
280
281 Inset::EDITABLE InsetERT::editable() const
282 {
283         if (status_ == Collapsed)
284                 return IS_EDITABLE;
285         return HIGHLY_EDITABLE;
286 }
287
288
289 void InsetERT::edit(BufferView * bv, bool front)
290 {
291         InsetCollapsable::edit(bv, front);
292         updateStatus(0);
293         set_latex_font(bv);
294 }
295
296
297 void InsetERT::lfunMousePress(FuncRequest const & cmd)
298 {
299         if (status_ == Inlined)
300                 inset.localDispatch(cmd);
301         else
302                 InsetCollapsable::localDispatch(cmd);
303 }
304
305
306 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
307 {
308         BufferView * bv = cmd.view();
309
310         if (cmd.button() == mouse_button::button3) {
311                 showInsetDialog(bv);
312                 return true;
313         }
314
315         if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
316             (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
317                 updateStatus(bv, true);
318         } else {
319                 LyXFont font(LyXFont::ALL_SANE);
320                 FuncRequest cmd1 = cmd;
321                 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
322
323                 // inlined is special - the text appears above
324                 // button_bottom_y
325                 if (status_ == Inlined)
326                         inset.localDispatch(cmd1);
327                 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
328                         cmd1.y -= ascent_collapsed() + descent_collapsed();
329                         inset.localDispatch(cmd1);
330                 }
331         }
332         return false;
333 }
334
335
336 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
337 {
338         if (status_ == Inlined)
339                 inset.localDispatch(cmd);
340         else
341                 InsetCollapsable::localDispatch(cmd);
342 }
343
344
345 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
346                     bool /*free_spc*/) const
347 {
348         Paragraph * par = inset.paragraph();
349         int lines = 0;
350         while (par) {
351                 pos_type siz = par->size();
352                 for (pos_type i = 0; i < siz; ++i) {
353                         Paragraph::value_type c = par->getChar(i);
354                         switch (c) {
355                         case Paragraph::META_NEWLINE:
356                                 os << '\n';
357                                 ++lines;
358                                 break;
359                         default:
360                                 os << c;
361                                 break;
362                         }
363                 }
364                 par = par->next();
365                 if (par) {
366                         os << "\n\n";
367                         lines += 2;
368                 }
369         }
370
371         return lines;
372 }
373
374
375 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
376 {
377         return 0;
378 }
379
380
381 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
382 {
383         Paragraph * par = inset.paragraph();
384         int lines = 0;
385         while (par) {
386                 pos_type siz = par->size();
387                 for (pos_type i = 0; i < siz; ++i) {
388                         Paragraph::value_type c = par->getChar(i);
389                         switch (c) {
390                         case Paragraph::META_NEWLINE:
391                                 os << '\n';
392                                 ++lines;
393                                 break;
394                         default:
395                                 os << c;
396                                 break;
397                         }
398                 }
399                 par = par->next();
400                 if (par) {
401                         os << "\n";
402                         lines ++;
403                 }
404         }
405
406         return lines;
407 }
408
409
410 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
411 {
412         Paragraph * par = inset.paragraph();
413         int lines = 0;
414         while (par) {
415                 pos_type siz = par->size();
416                 for (pos_type i = 0; i < siz; ++i) {
417                         Paragraph::value_type c = par->getChar(i);
418                         switch (c) {
419                         case Paragraph::META_NEWLINE:
420                                 os << '\n';
421                                 ++lines;
422                                 break;
423                         default:
424                                 os << c;
425                                 break;
426                         }
427                 }
428                 par = par->next();
429                 if (par) {
430                         os << "\n";
431                         lines ++;
432                 }
433         }
434
435         return lines;
436 }
437
438
439 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
440 {
441         Inset::RESULT result = DISPATCHED_NOUPDATE;
442         BufferView * bv = cmd.view();
443
444         if (inset.paragraph()->empty()) {
445                 set_latex_font(bv);
446         }
447
448         switch (cmd.action) {
449                 case LFUN_MOUSE_PRESS:
450                         lfunMousePress(cmd);
451                         return DISPATCHED;
452
453                 case LFUN_MOUSE_MOTION:
454                         lfunMouseMotion(cmd);
455                         return DISPATCHED;
456
457                 case LFUN_MOUSE_RELEASE:
458                         lfunMouseRelease(cmd);
459                         return DISPATCHED;
460
461                 case LFUN_LAYOUT:
462                         bv->owner()->setLayout(inset.paragraph()->layout()->name());
463                         break;
464
465                 default:
466                         result = InsetCollapsable::localDispatch(cmd);
467         }
468
469         switch (cmd.action) {
470         case LFUN_BREAKPARAGRAPH:
471         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
472         case LFUN_BACKSPACE:
473         case LFUN_BACKSPACE_SKIP:
474         case LFUN_DELETE:
475         case LFUN_DELETE_SKIP:
476         case LFUN_DELETE_LINE_FORWARD:
477         case LFUN_CUT:
478                 set_latex_font(bv);
479                 break;
480
481         default:
482                 break;
483         }
484         return result;
485 }
486
487
488 string const InsetERT::get_new_label() const
489 {
490         string la;
491         pos_type const max_length = 15;
492         pos_type const p_siz = inset.paragraph()->size();
493         pos_type const n = min(max_length, p_siz);
494         int i = 0;
495         int j = 0;
496         for(; i < n && j < p_siz; ++j) {
497                 if (inset.paragraph()->isInset(j))
498                         continue;
499                 la += inset.paragraph()->getChar(j);
500                 ++i;
501         }
502         if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
503                 la += "...";
504         }
505         if (la.empty()) {
506                 la = _("ERT");
507         }
508         return la;
509 }
510
511
512 void InsetERT::setButtonLabel() const
513 {
514         if (status_ == Collapsed) {
515                 setLabel(get_new_label());
516         } else {
517                 setLabel(_("ERT"));
518         }
519 }
520
521
522 bool InsetERT::checkInsertChar(LyXFont & /* font */)
523 {
524 #ifdef SET_HARD_FONT
525         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
526         font = f;
527         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
528         font.setColor(LColor::latex);
529 #endif
530         return true;
531 }
532
533
534 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
535 {
536         if (!inlined())
537                 return InsetCollapsable::ascent(bv, font);
538
539         return inset.ascent(bv, font);
540 }
541
542
543 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
544 {
545         if (!inlined())
546                 return InsetCollapsable::descent(bv, font);
547
548         return inset.descent(bv, font);
549 }
550
551
552 int InsetERT::width(BufferView * bv, LyXFont const & font) const
553 {
554         if (!inlined())
555                 return InsetCollapsable::width(bv, font);
556
557         return inset.width(bv, font);
558 }
559
560
561 void InsetERT::draw(BufferView * bv, LyXFont const & f,
562                     int baseline, float & x, bool cleared) const
563 {
564         Painter & pain = bv->painter();
565
566         button_length = width_collapsed();
567         button_top_y = -ascent(bv, f);
568         button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
569                 descent_collapsed();
570
571         if (!isOpen()) {
572                 draw_collapsed(pain, baseline, x);
573                 return;
574         }
575
576         float old_x = x;
577
578         if (!owner())
579                 x += static_cast<float>(scroll());
580
581         if (!cleared && (inset.need_update == InsetText::FULL ||
582                          inset.need_update == InsetText::INIT ||
583                          top_x != int(x) ||
584                          top_baseline != baseline))
585         {
586                 // we don't need anymore to clear here we just have to tell
587                 // the underlying LyXText that it should do the RowClear!
588                 inset.setUpdateStatus(bv, InsetText::FULL);
589                 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
590                 return;
591         }
592
593         top_x = int(x);
594         topx_set = true;
595         top_baseline = baseline;
596
597         int const bl = baseline - ascent(bv, f) + ascent_collapsed();
598
599         if (inlined()) {
600                 inset.draw(bv, f, baseline, x, cleared);
601         } else {
602                 draw_collapsed(pain, bl, old_x);
603                 inset.draw(bv, f,
604                                    bl + descent_collapsed() + inset.ascent(bv, f),
605                                    x, cleared);
606         }
607         need_update = NONE;
608 }
609
610
611 void InsetERT::set_latex_font(BufferView * /* bv */)
612 {
613 #ifdef SET_HARD_FONT
614         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
615
616         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
617         font.setColor(LColor::latex);
618
619         inset.getLyXText(bv)->setFont(bv, font, false);
620 #endif
621 }
622
623
624 // attention this function can be called with bv == 0
625 void InsetERT::status(BufferView * bv, ERTStatus const st) const
626 {
627         if (st != status_) {
628                 status_ = st;
629                 need_update = FULL;
630                 switch (st) {
631                 case Inlined:
632                         if (bv)
633                                 inset.setUpdateStatus(bv, InsetText::INIT);
634                         break;
635                 case Open:
636                         collapsed_ = false;
637                         setButtonLabel();
638                         break;
639                 case Collapsed:
640                         collapsed_ = true;
641                         setButtonLabel();
642                         if (bv)
643                                 bv->unlockInset(const_cast<InsetERT *>(this));
644                         break;
645                 }
646                 if (bv)
647                         bv->updateInset(const_cast<InsetERT *>(this), false);
648         }
649 }
650
651
652 bool InsetERT::showInsetDialog(BufferView * bv) const
653 {
654         bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
655         return true;
656 }
657
658
659 void InsetERT::open(BufferView * bv)
660 {
661         if (!collapsed_)
662                 return;
663         status(bv, Open);
664 }
665
666
667 void InsetERT::close(BufferView * bv) const
668 {
669         if (status_ == Collapsed || status_ == Inlined)
670                 return;
671
672         status(bv, Collapsed);
673 }
674
675
676 WordLangTuple const
677 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
678 {
679         bv->unlockInset(const_cast<InsetERT *>(this));
680         return WordLangTuple();
681 }
682
683
684 void InsetERT::getDrawFont(LyXFont & font) const
685 {
686         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
687         font = f;
688         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
689         font.setColor(LColor::latex);
690 }
691
692
693 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
694 {
695         int w = InsetCollapsable::getMaxWidth(bv, in);
696         if (status_ != Inlined || w < 0)
697                 return w;
698         LyXText * text = inset.getLyXText(bv);
699         int rw = text->firstRow()->width();
700         if (!rw)
701                 rw = w;
702         rw += 40;
703         if (!text->firstRow()->next() && rw < w)
704                 return -1;
705         return w;
706 }
707
708
709 void InsetERT::update(BufferView * bv, LyXFont const & font,
710                       bool reinit)
711 {
712         if (inset.need_update & InsetText::INIT ||
713                 inset.need_update & InsetText::FULL)
714         {
715                 setButtonLabel();
716         }
717         InsetCollapsable::update(bv, font, reinit);
718 }