]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormTexinfo.C
a8357cd64da7e27ba92538ca861f750a17f278ce
[lyx.git] / src / frontends / xforms / FormTexinfo.C
1 /**
2  * \file FormTexinfo.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Herbert Voss <voss@lyx.org>
7  * \date 2001-10-01
8  */
9
10 #include <config.h>
11 #include <fstream>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "xformsBC.h"
18 #include "ControlTexinfo.h"
19 #include "FormTexinfo.h"
20 #include "form_texinfo.h"
21 #include "gettext.h"
22 #include "debug.h"
23 #include "helper_funcs.h"
24 #include "xforms_helpers.h"
25 #include "support/lstrings.h"
26 #include "support/filetools.h"
27 #include "support/LAssert.h"
28
29 extern string user_lyxdir; // home of *Files.lst
30     
31 namespace {
32
33 string const bstFilename("bstFiles.lst");
34 string const clsFilename("clsFiles.lst");
35 string const styFilename("styFiles.lst");
36 string const errorMessage =_("Missing filelist. try Rescan");
37
38 // C function wrapper, required by xforms.
39 extern "C"
40 int C_FormTexinfoFeedbackCB(FL_OBJECT * ob, int event,
41                             FL_Coord, FL_Coord, int, void *)
42 {
43         // Note that the return value is important in the pre-emptive handler.
44         // Don't return anything other than 0.
45
46         lyx::Assert(ob);
47         // Don't Assert this one, as it can happen quite reasonably when things
48         // are being deleted in the d-tor.
49         //Assert(ob->form);
50         if (!ob->form) return 0;
51
52         FormTexinfo * pre =
53                 static_cast<FormTexinfo*>(ob->form->u_vdata);
54         pre->feedbackCB(ob, event);
55         return 0;
56 }
57         
58 void setPreHandler(FL_OBJECT * ob)
59 {
60         lyx::Assert(ob);
61         fl_set_object_prehandler(ob, C_FormTexinfoFeedbackCB);
62 }
63  
64 } // namespace anon
65
66
67 typedef FormCB<ControlTexinfo, FormDB<FD_form_texinfo> > base_class;
68 FormTexinfo::FormTexinfo(ControlTexinfo & c)
69         : base_class(c, _("TeX Infos")),
70           warningPosted(false), activeStyle(FormTexinfo::cls)
71 {}
72
73
74 void FormTexinfo::build() {
75         dialog_.reset(build_texinfo());
76         // courier medium
77         fl_set_browser_fontstyle(dialog_->browser,FL_FIXED_STYLE);
78         // with Path is default
79         fl_set_button(dialog_->button_fullPath, 1);
80         updateStyles(FormTexinfo::cls);
81
82         setPreHandler(dialog_->button_rescan);
83         setPreHandler(dialog_->button_view);
84         setPreHandler(dialog_->button_texhash);
85         setPreHandler(dialog_->button_fullPath);
86         setPreHandler(dialog_->browser);
87         setPreHandler(dialog_->radio_cls);
88         setPreHandler(dialog_->radio_sty);
89         setPreHandler(dialog_->radio_bst);
90         setPreHandler(dialog_->message);
91         setPreHandler(dialog_->help);
92 }
93
94
95 ButtonPolicy::SMInput FormTexinfo::input(FL_OBJECT * ob, long) {
96         if (ob == dialog_->help) {
97                 controller().help();
98
99         } else if (ob == dialog_->radio_cls) {
100                 updateStyles(FormTexinfo::cls); 
101
102         } else if (ob == dialog_->radio_sty) {
103                 updateStyles(FormTexinfo::sty); 
104
105         } else if (ob == dialog_->radio_bst) {
106                 updateStyles(FormTexinfo::bst); 
107
108         } else if (ob == dialog_->button_rescan) {
109                 // build new *Files.lst
110                 controller().rescanStyles();
111                 updateStyles(activeStyle);
112
113         } else if (ob == dialog_->button_fullPath) {
114                 setEnabled(dialog_->button_view,
115                            fl_get_button(dialog_->button_fullPath));
116                 updateStyles(activeStyle);
117
118         } else if (ob == dialog_->button_texhash) {
119                 // makes only sense if the rights are set well for
120                 // users (/var/lib/texmf/ls-R)
121                 controller().runTexhash();
122                 // update files in fact of texhash
123                 controller().rescanStyles();
124
125         } else if (ob == dialog_->button_view) {
126                 unsigned int selection = fl_get_browser(dialog_->browser);
127                 // a valid entry?
128                 if (selection > 0) {
129                         controller().viewFile( 
130                                 fl_get_browser_line(dialog_->browser,
131                                                     selection));
132                 }
133         }
134
135         return ButtonPolicy::SMI_VALID;
136 }
137
138
139 namespace {
140
141 string const sortEntries(string & str_in)
142 {
143         std::vector<string> dbase = getVectorFromString(str_in,"\n");
144         std::sort(dbase.begin(), dbase.end());  // sort entries
145         return getStringFromVector(dbase,"\n");
146 }
147
148 string const getContents(string const filename, bool withFullPath)
149 {
150         string fileContents = GetFileContents(AddName(user_lyxdir,filename));
151         // everything ok?
152         if (!fileContents.empty()) {
153                 if (withFullPath)
154                         return(sortEntries(fileContents));
155                 else {
156                         int Entries = 1;
157                         string dummy = OnlyFilename(token(fileContents,'\n',1));
158                         string contents = dummy;
159                         do {
160                                 dummy = OnlyFilename(token(fileContents,'\n',++Entries));
161                                 contents += ("\n"+dummy);
162                         } while (!dummy.empty());
163                         return(sortEntries(contents));
164                 }
165         } else
166                 return errorMessage;
167 }
168
169 } // namespace anon
170
171
172 void FormTexinfo::updateStyles(FormTexinfo::texFileSuffix whichStyle)
173 {
174         fl_clear_browser(dialog_->browser); 
175
176         bool const withFullPath = fl_get_button(dialog_->button_fullPath);
177
178         switch (whichStyle) {
179         case FormTexinfo::bst: 
180         {
181                 string const str = getContents(bstFilename, withFullPath);
182                 fl_add_browser_line(dialog_->browser, str.c_str());
183                 break;
184
185         }
186         case FormTexinfo::cls: 
187         {
188                 string const str = getContents(clsFilename, withFullPath);
189                 fl_add_browser_line(dialog_->browser, str.c_str());
190                 break;
191         }
192         case FormTexinfo::sty:
193         {
194                 string const str = getContents(styFilename, withFullPath);
195                 fl_add_browser_line(dialog_->browser, str.c_str());
196                 break;
197         }
198         }
199
200         activeStyle = whichStyle;
201 }
202
203
204 // preemptive handler for feedback messages
205 void FormTexinfo::feedbackCB(FL_OBJECT * ob, int event)
206 {
207         lyx::Assert(ob);
208
209         switch (event) {
210         case FL_ENTER:
211                 warningPosted = false;
212                 feedback(ob);
213                 break;
214
215         case FL_LEAVE:
216                 if (!warningPosted) {
217                         fl_set_object_label(dialog_->message, "");
218                         fl_redraw_object(dialog_->message);
219                 }
220                 break;
221
222         default:
223                 break;
224         }
225 }
226
227
228 void FormTexinfo::feedback(FL_OBJECT * ob)
229 {
230         lyx::Assert(ob);
231
232         string str;
233
234         if (ob == dialog_->button_rescan) {
235                 str = _("run rescan ...");
236
237         } else if (ob == dialog_->button_fullPath) {
238                 str = _("View full path or only file name");
239
240         } else if (ob == dialog_->button_texhash) {
241                 str = _("run texhash and rescan...");
242
243         } else if (ob == dialog_->button_view) {
244                 str = _("select a file to view");
245
246         }
247         
248         fl_set_object_label(dialog_->message, str.c_str());
249         fl_redraw_object(dialog_->message);
250 }