1 /** Collection of some useful xform helper functions
6 #include FORMS_H_LOCATION
8 #include <fstream> // ofstream
12 #pragma implementation
15 #include "xforms_helpers.h"
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h" // frontStrip, strip
21 #include "support/LAssert.h"
27 // Extract shortcut from <ident>|<shortcut> string
28 char const * flyx_shortcut_extract(char const * sc)
30 // Find '|' in the sc and return the string after that.
31 register char const * sd = sc;
32 while (sd[0]!= 0 && sd[0] != '|') ++sd;
36 //lyxerr << sd << endl;
43 // Extract identifier from <ident>|<shortcut> string
44 char const * flyx_ident_extract(char const * sc)
46 register char const * se = sc;
47 while (se[0]!= 0 && se[0] != '|') ++se;
49 if (se[0] == 0) return sc;
51 char * sb = new char[se - sc + 1];
53 register char const * sd = sc;
63 // Set an FL_OBJECT to activated or deactivated
64 void setEnabled(FL_OBJECT * ob, bool enable)
67 fl_activate_object(ob);
68 fl_set_object_lcol(ob, FL_BLACK);
70 fl_deactivate_object(ob);
71 fl_set_object_lcol(ob, FL_INACTIVE);
76 // Given an fl_choice, create a vector of its entries
77 vector<string> const getVectorFromChoice(FL_OBJECT * ob)
80 if (!ob || ob->objclass != FL_CHOICE)
83 for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
84 string const text = fl_get_choice_item_text(ob, i+1);
85 vec.push_back(strip(frontStrip(text)));
92 // Given an fl_browser, create a vector of its entries
93 vector<string> const getVectorFromBrowser(FL_OBJECT * ob)
96 if (!ob || ob->objclass != FL_BROWSER)
99 for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
100 string const text = fl_get_browser_line(ob, i+1);
101 vec.push_back(strip(frontStrip(text)));
108 string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
111 lyx::Assert(input && input->objclass == FL_INPUT &&
112 choice && choice->objclass == FL_CHOICE);
114 string const length = strip(frontStrip(fl_get_input(input)));
118 string unit = strip(frontStrip(fl_get_choice_text(choice)));
119 unit = subst(unit, "%%", "%");
121 return length + unit;
125 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
127 string const & default_unit)
130 lyx::Assert(input && input->objclass == FL_INPUT &&
131 choice && choice->objclass == FL_CHOICE);
134 fl_set_input(input, "");
135 int unitpos = 1; // xforms has Fortran-style indexing
136 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
137 string const text = fl_get_choice_item_text(choice,i+1);
139 lowercase(strip(frontStrip(text)))) {
144 fl_set_choice(choice, unitpos);
148 // The unit is presumed to begin at the first char a-z
149 string const tmp = lowercase(strip(frontStrip(str)));
151 string::const_iterator p = tmp.begin();
152 for (; p != tmp.end(); ++p) {
153 if (*p >= 'a' && *p <= 'z')
158 int unitpos = 1; // xforms has Fortran-style indexing
160 if (p == tmp.end()) {
165 string tmplen = string(tmp.begin(), p);
166 if (isStrDbl(tmplen))
168 string unit = string(p, tmp.end());
169 unit = subst(unit, "%", "%%");
171 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
172 string const text = fl_get_choice_item_text(choice,i+1);
173 if (unit == lowercase(strip(frontStrip(text)))) {
180 fl_set_input(input, len.c_str());
181 fl_set_choice(choice, unitpos);
184 // Take a string and add breaks so that it fits into a desired label width, w
185 string formatted(string const & sin, int w, int size, int style)
187 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
190 if (sin.empty()) return sout;
192 // breaks in up into a vector of individual words
193 vector<string> sentence;
195 for (string::const_iterator sit = sin.begin();
196 sit != sin.end(); ++sit) {
197 if ((*sit) == ' ' || (*sit) == '\n') {
199 sentence.push_back(word);
202 if ((*sit) == '\n') word += '\n';
209 // Flush remaining contents of word
210 if (!word.empty() ) sentence.push_back(word);
213 string line_plus_word;
214 for (vector<string>::const_iterator vit = sentence.begin();
215 vit != sentence.end(); ++vit) {
222 line_plus_word.erase();
226 if (!line_plus_word.empty() ) line_plus_word += ' ';
227 line_plus_word += word;
229 int const length = fl_get_string_width(style, size,
230 line_plus_word.c_str(),
231 int(line_plus_word.length()));
234 line_plus_word = word;
237 line = line_plus_word;
239 // Flush remaining contents of line
244 if (sout[sout.length() - 1] == '\n')
245 sout.erase(sout.length() - 1);
253 // sorted by hand to prevent LyXLex from complaining on read().
254 keyword_item xformTags[] = {
255 { "\\gui_background", FL_COL1 },
256 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
257 { "\\gui_buttonleft", FL_LEFT_BCOL },
258 { "\\gui_buttonright", FL_RIGHT_BCOL },
259 { "\\gui_buttontop", FL_TOP_BCOL },
260 { "\\gui_inactive", FL_INACTIVE },
261 { "\\gui_push_button", FL_YELLOW },
262 { "\\gui_selected", FL_MCOL },
263 { "\\gui_text", FL_BLACK }
267 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
272 bool XformsColor::read(string const & filename)
274 LyXLex lexrc(xformTags, xformCount);
275 if (!lexrc.setFile(filename))
278 while (lexrc.isOK()) {
279 int const le = lexrc.lex();
282 case LyXLex::LEX_UNDEF:
283 lexrc.printError("Unknown tag `$$Token'");
285 case LyXLex::LEX_FEOF:
292 if (!lexrc.next()) break;
293 col.r = lexrc.getInteger();
295 if (!lexrc.next()) break;
296 col.g = lexrc.getInteger();
298 if (!lexrc.next()) break;
299 col.b = lexrc.getInteger();
301 fl_mapcolor(le, col.r, col.g, col.b);
308 bool XformsColor::write(string const & filename)
310 ofstream os(filename.c_str());
314 os << "### This file is part of\n"
315 << "### ========================================================\n"
316 << "### LyX, The Document Processor\n"
318 << "### Copyright 1995 Matthias Ettrich\n"
319 << "### Copyright 1995-2001 The LyX Team.\n"
321 << "### ========================================================\n"
323 << "# This file is written by LyX, if you want to make your own\n"
324 << "# modifications you should do them from inside LyX and save\n"
327 for (int i = 0; i < xformCount; ++i) {
328 string const tag = xformTags[i].tag;
329 int const colorID = xformTags[i].code;
332 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
335 << color.r << " " << color.g << " " << color.b << "\n";
342 string RWInfo::error_message;
344 bool RWInfo::WriteableDir(string const & name)
346 error_message.erase();
348 if (!AbsolutePath(name)) {
349 error_message = N_("The absolute path is required.");
353 FileInfo const tp(name);
355 error_message = N_("Directory does not exist.");
359 if (!tp.writable()) {
360 error_message = N_("Cannot write to this directory.");
368 bool RWInfo::ReadableDir(string const & name)
370 error_message.erase();
372 if (!AbsolutePath(name)) {
373 error_message = N_("The absolute path is required.");
377 FileInfo const tp(name);
379 error_message = N_("Directory does not exist.");
383 if (!tp.readable()) {
384 error_message = N_("Cannot read this directory.");
392 bool RWInfo::WriteableFile(string const & name)
394 // A writeable file is either:
395 // * An existing file to which we have write access, or
396 // * A file that doesn't yet exist but that would exist in a writeable
399 error_message.erase();
402 error_message = N_("No file input.");
406 string const dir = OnlyPath(name);
407 if (!AbsolutePath(dir)) {
408 error_message = N_("The absolute path is required.");
418 error_message = N_("Directory does not exist.");
423 error_message = N_("Cannot write to this directory.");
428 if (dir == name || f.isDir()) {
429 error_message = N_("A file is required, not a directory.");
433 if (f.exist() && !f.writable()) {
434 error_message = N_("Cannot write to this file.");
442 bool RWInfo::ReadableFile(string const & name)
444 error_message.erase();
447 error_message = N_("No file input.");
451 string const dir = OnlyPath(name);
452 if (!AbsolutePath(dir)) {
453 error_message = N_("The absolute path is required.");
463 error_message = N_("Directory does not exist.");
468 error_message = N_("Cannot read from this directory.");
473 if (dir == name || f.isDir()) {
474 error_message = N_("A file is required, not a directory.");
479 error_message = N_("File does not exist.");
484 error_message = N_("Cannot read from this file.");