3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author Herbert Voss <voss@lyx.org>
14 #pragma implementation
18 #include "ControlTexinfo.h"
19 #include "FormTexinfo.h"
20 #include "form_texinfo.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"
29 extern string user_lyxdir; // home of *Files.lst
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");
38 // C function wrapper, required by xforms.
40 int C_FormTexinfoFeedbackCB(FL_OBJECT * ob, int event,
41 FL_Coord, FL_Coord, int, void *)
43 // Note that the return value is important in the pre-emptive handler.
44 // Don't return anything other than 0.
47 // Don't Assert this one, as it can happen quite reasonably when things
48 // are being deleted in the d-tor.
50 if (!ob->form) return 0;
53 static_cast<FormTexinfo*>(ob->form->u_vdata);
54 pre->feedbackCB(ob, event);
58 void setPreHandler(FL_OBJECT * ob)
61 fl_set_object_prehandler(ob, C_FormTexinfoFeedbackCB);
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)
74 void FormTexinfo::build() {
75 dialog_.reset(build_texinfo());
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);
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);
95 ButtonPolicy::SMInput FormTexinfo::input(FL_OBJECT * ob, long) {
96 if (ob == dialog_->help) {
99 } else if (ob == dialog_->radio_cls) {
100 updateStyles(FormTexinfo::cls);
102 } else if (ob == dialog_->radio_sty) {
103 updateStyles(FormTexinfo::sty);
105 } else if (ob == dialog_->radio_bst) {
106 updateStyles(FormTexinfo::bst);
108 } else if (ob == dialog_->button_rescan) {
109 // build new *Files.lst
110 controller().rescanStyles();
111 updateStyles(activeStyle);
113 } else if (ob == dialog_->button_fullPath) {
114 setEnabled(dialog_->button_view,
115 fl_get_button(dialog_->button_fullPath));
116 updateStyles(activeStyle);
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();
125 } else if (ob == dialog_->button_view) {
126 unsigned int selection = fl_get_browser(dialog_->browser);
129 controller().viewFile(
130 fl_get_browser_line(dialog_->browser,
135 return ButtonPolicy::SMI_VALID;
141 string const sortEntries(string & str_in)
143 std::vector<string> dbase = getVectorFromString(str_in,"\n");
144 std::sort(dbase.begin(), dbase.end()); // sort entries
145 return getStringFromVector(dbase,"\n");
148 string const getContents(string const filename, bool withFullPath)
150 string fileContents = GetFileContents(AddName(user_lyxdir,filename));
152 if (!fileContents.empty()) {
154 return(sortEntries(fileContents));
157 string dummy = OnlyFilename(token(fileContents,'\n',1));
158 string contents = dummy;
160 dummy = OnlyFilename(token(fileContents,'\n',++Entries));
161 contents += ("\n"+dummy);
162 } while (!dummy.empty());
163 return(sortEntries(contents));
172 void FormTexinfo::updateStyles(FormTexinfo::texFileSuffix whichStyle)
174 fl_clear_browser(dialog_->browser);
176 bool const withFullPath = fl_get_button(dialog_->button_fullPath);
178 switch (whichStyle) {
179 case FormTexinfo::bst:
181 string const str = getContents(bstFilename, withFullPath);
182 fl_add_browser_line(dialog_->browser, str.c_str());
186 case FormTexinfo::cls:
188 string const str = getContents(clsFilename, withFullPath);
189 fl_add_browser_line(dialog_->browser, str.c_str());
192 case FormTexinfo::sty:
194 string const str = getContents(styFilename, withFullPath);
195 fl_add_browser_line(dialog_->browser, str.c_str());
200 activeStyle = whichStyle;
204 // preemptive handler for feedback messages
205 void FormTexinfo::feedbackCB(FL_OBJECT * ob, int event)
211 warningPosted = false;
216 if (!warningPosted) {
217 fl_set_object_label(dialog_->message, "");
218 fl_redraw_object(dialog_->message);
228 void FormTexinfo::feedback(FL_OBJECT * ob)
234 if (ob == dialog_->button_rescan) {
235 str = _("run rescan ...");
237 } else if (ob == dialog_->button_fullPath) {
238 str = _("View full path or only file name");
240 } else if (ob == dialog_->button_texhash) {
241 str = _("run texhash and rescan...");
243 } else if (ob == dialog_->button_view) {
244 str = _("select a file to view");
248 fl_set_object_label(dialog_->message, str.c_str());
249 fl_redraw_object(dialog_->message);