]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
Enable the external inset to handle unknown templates gracefully.
[lyx.git] / src / insets / insetref.C
1 /**
2  * \file insetref.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS
9  */
10 #include <config.h>
11
12
13 #include "insetref.h"
14 #include "buffer.h"
15 #include "funcrequest.h"
16 #include "debug.h"
17 #include "gettext.h"
18 #include "LaTeXFeatures.h"
19 #include "frontends/LyXView.h"
20 #include "frontends/Dialogs.h"
21 #include "BufferView.h"
22 #include "support/lstrings.h"
23
24 using std::ostream;
25
26 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
27         : InsetCommand(p), isLatex(buf.isLatex())
28 {}
29
30
31 InsetRef::InsetRef(InsetRef const & ir)
32         : InsetCommand(ir), isLatex(ir.isLatex)
33 {
34 }
35
36
37 InsetRef::~InsetRef()
38 {
39         InsetCommandMailer("ref", *this).hideDialog();
40 }
41
42
43 dispatch_result InsetRef::localDispatch(FuncRequest const & cmd)
44 {
45         switch (cmd.action) {
46         case LFUN_INSET_EDIT:
47                 // Eventually trigger dialog with button 3 not 1
48                 if (cmd.button() == mouse_button::button3)
49                         cmd.view()->owner()->
50                                 dispatch(FuncRequest(LFUN_REF_GOTO, getContents()));
51                 if (cmd.button() == mouse_button::button1)
52                         InsetCommandMailer("ref", *this).showDialog(cmd.view());
53                 return DISPATCHED;
54
55         default:
56                 return InsetCommand::localDispatch(cmd);
57         }
58 }
59
60
61 string const InsetRef::getScreenLabel(Buffer const *) const
62 {
63         string temp;
64         for (int i = 0; !types[i].latex_name.empty(); ++ i)
65                 if (getCmdName() == types[i].latex_name) {
66                         temp = _(types[i].short_gui_name);
67                         break;
68                 }
69         temp += getContents();
70
71         if (!isLatex
72            && !getOptions().empty()) {
73                 temp += "||";
74                 temp += getOptions();
75         }
76         return temp;
77 }
78
79
80 int InsetRef::latex(Buffer const *, ostream & os,
81                     LatexRunParams const &) const
82 {
83         if (getOptions().empty())
84                 os << escape(getCommand());
85         else {
86                 InsetCommandParams p(getCmdName(), getContents(), "");
87                 os << escape(p.getCommand());
88         }
89         return 0;
90 }
91
92
93 int InsetRef::ascii(Buffer const *, ostream & os, int) const
94 {
95         os << '[' << getContents() << ']';
96         return 0;
97 }
98
99
100 int InsetRef::linuxdoc(Buffer const *, ostream & os) const
101 {
102         os << "<ref id=\"" << getContents()
103            << "\" name=\"" << getOptions() << "\" >";
104         return 0;
105 }
106
107
108 int InsetRef::docbook(Buffer const *, ostream & os, bool) const
109 {
110         if (getOptions().empty()) {
111                 os << "<xref linkend=\"" << getContents() << "\">";
112         } else {
113                 os << "<link linkend=\"" << getContents()
114                    << "\">" << getOptions() << "</link>";
115         }
116
117         return 0;
118 }
119
120
121 void InsetRef::validate(LaTeXFeatures & features) const
122 {
123         if (getCmdName() == "vref" || getCmdName() == "vpageref")
124                 features.require("varioref");
125         else if (getCmdName() == "prettyref")
126                 features.require("prettyref");
127 }
128
129
130 InsetRef::type_info InsetRef::types[] = {
131         { "ref",       N_("Standard"),              N_("Ref: ")},
132         { "eqref",     N_("Equation"),              N_("EqRef: ")},
133         { "pageref",   N_("Page Number"),           N_("Page: ")},
134         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
135         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
136         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
137         { "", "", "" }
138 };
139
140
141 int InsetRef::getType(string const & name)
142 {
143         for (int i = 0; !types[i].latex_name.empty(); ++i)
144                 if (name == types[i].latex_name)
145                         return i;
146         return 0;
147 }
148
149
150 string const & InsetRef::getName(int type)
151 {
152         return types[type].latex_name;
153 }