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;
126 // this should definitely be the other way around!!!
127 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
128 LyXLength const & len,
129 string const & default_unit)
132 updateWidgetsFromLengthString(input, choice,
133 string(), default_unit);
135 updateWidgetsFromLengthString(input, choice,
136 len.asString(), default_unit);
141 // Most of the code here is a poor duplication of the parser code
142 // which is in LyXLength. Use that instead
143 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
145 string const & default_unit)
148 lyx::Assert(input && input->objclass == FL_INPUT &&
149 choice && choice->objclass == FL_CHOICE);
152 fl_set_input(input, "");
153 int unitpos = 1; // xforms has Fortran-style indexing
154 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
155 string const text = fl_get_choice_item_text(choice,i+1);
157 lowercase(strip(frontStrip(text)))) {
162 fl_set_choice(choice, unitpos);
166 // The unit is presumed to begin at the first char a-z
167 string const tmp = lowercase(strip(frontStrip(str)));
169 string::const_iterator p = tmp.begin();
170 for (; p != tmp.end(); ++p) {
171 if (*p >= 'a' && *p <= 'z')
176 int unitpos = 1; // xforms has Fortran-style indexing
178 if (p == tmp.end()) {
183 string tmplen = string(tmp.begin(), p);
184 if (isStrDbl(tmplen))
186 string unit = string(p, tmp.end());
187 unit = subst(unit, "%", "%%");
189 for(int i = 0; i < fl_get_choice_maxitems(choice); ++i) {
190 string const text = fl_get_choice_item_text(choice,i+1);
191 if (unit == lowercase(strip(frontStrip(text)))) {
198 fl_set_input(input, len.c_str());
199 fl_set_choice(choice, unitpos);
202 void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
204 string const & default_unit)
206 updateWidgetsFromLength(input, choice,
207 LyXLength(str), default_unit);
211 void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
212 LyXLength const & len,
213 string const & default_unit)
216 lyx::Assert(input && input->objclass == FL_INPUT &&
217 choice && choice->objclass == FL_CHOICE);
220 fl_set_input(input, "");
221 fl_set_choice_text(choice, default_unit.c_str());
223 ostringstream buffer;
224 buffer << len.value();
225 fl_set_input(input, buffer.str().c_str());
226 fl_set_choice_text(choice, stringFromUnit(len.unit()));
232 // Take a string and add breaks so that it fits into a desired label width, w
233 string formatted(string const & sin, int w, int size, int style)
235 // FIX: Q: Why cant this be done by a one pass algo? (Lgb)
238 if (sin.empty()) return sout;
240 // breaks in up into a vector of individual words
241 vector<string> sentence;
243 for (string::const_iterator sit = sin.begin();
244 sit != sin.end(); ++sit) {
245 if ((*sit) == ' ' || (*sit) == '\n') {
247 sentence.push_back(word);
250 if ((*sit) == '\n') word += '\n';
257 // Flush remaining contents of word
258 if (!word.empty() ) sentence.push_back(word);
261 string line_plus_word;
262 for (vector<string>::const_iterator vit = sentence.begin();
263 vit != sentence.end(); ++vit) {
270 line_plus_word.erase();
274 if (!line_plus_word.empty() ) line_plus_word += ' ';
275 line_plus_word += word;
277 int const length = fl_get_string_width(style, size,
278 line_plus_word.c_str(),
279 int(line_plus_word.length()));
282 line_plus_word = word;
285 line = line_plus_word;
287 // Flush remaining contents of line
292 if (sout[sout.length() - 1] == '\n')
293 sout.erase(sout.length() - 1);
301 // sorted by hand to prevent LyXLex from complaining on read().
302 keyword_item xformTags[] = {
303 { "\\gui_background", FL_COL1 },
304 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
305 { "\\gui_buttonleft", FL_LEFT_BCOL },
306 { "\\gui_buttonright", FL_RIGHT_BCOL },
307 { "\\gui_buttontop", FL_TOP_BCOL },
308 { "\\gui_inactive", FL_INACTIVE },
309 { "\\gui_push_button", FL_YELLOW },
310 { "\\gui_selected", FL_MCOL },
311 { "\\gui_text", FL_BLACK }
315 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
320 bool XformsColor::read(string const & filename)
322 LyXLex lexrc(xformTags, xformCount);
323 if (!lexrc.setFile(filename))
326 while (lexrc.isOK()) {
327 int const le = lexrc.lex();
330 case LyXLex::LEX_UNDEF:
331 lexrc.printError("Unknown tag `$$Token'");
333 case LyXLex::LEX_FEOF:
340 if (!lexrc.next()) break;
341 col.r = lexrc.getInteger();
343 if (!lexrc.next()) break;
344 col.g = lexrc.getInteger();
346 if (!lexrc.next()) break;
347 col.b = lexrc.getInteger();
349 fl_mapcolor(le, col.r, col.g, col.b);
356 bool XformsColor::write(string const & filename)
358 ofstream os(filename.c_str());
362 os << "### This file is part of\n"
363 << "### ========================================================\n"
364 << "### LyX, The Document Processor\n"
366 << "### Copyright 1995 Matthias Ettrich\n"
367 << "### Copyright 1995-2001 The LyX Team.\n"
369 << "### ========================================================\n"
371 << "# This file is written by LyX, if you want to make your own\n"
372 << "# modifications you should do them from inside LyX and save\n"
375 for (int i = 0; i < xformCount; ++i) {
376 string const tag = xformTags[i].tag;
377 int const colorID = xformTags[i].code;
380 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
383 << color.r << " " << color.g << " " << color.b << "\n";
390 string RWInfo::error_message;
392 bool RWInfo::WriteableDir(string const & name)
394 error_message.erase();
396 if (!AbsolutePath(name)) {
397 error_message = N_("The absolute path is required.");
401 FileInfo const tp(name);
403 error_message = N_("Directory does not exist.");
407 if (!tp.writable()) {
408 error_message = N_("Cannot write to this directory.");
416 bool RWInfo::ReadableDir(string const & name)
418 error_message.erase();
420 if (!AbsolutePath(name)) {
421 error_message = N_("The absolute path is required.");
425 FileInfo const tp(name);
427 error_message = N_("Directory does not exist.");
431 if (!tp.readable()) {
432 error_message = N_("Cannot read this directory.");
440 bool RWInfo::WriteableFile(string const & name)
442 // A writeable file is either:
443 // * An existing file to which we have write access, or
444 // * A file that doesn't yet exist but that would exist in a writeable
447 error_message.erase();
450 error_message = N_("No file input.");
454 string const dir = OnlyPath(name);
455 if (!AbsolutePath(dir)) {
456 error_message = N_("The absolute path is required.");
466 error_message = N_("Directory does not exist.");
471 error_message = N_("Cannot write to this directory.");
476 if (dir == name || f.isDir()) {
477 error_message = N_("A file is required, not a directory.");
481 if (f.exist() && !f.writable()) {
482 error_message = N_("Cannot write to this file.");
490 bool RWInfo::ReadableFile(string const & name)
492 error_message.erase();
495 error_message = N_("No file input.");
499 string const dir = OnlyPath(name);
500 if (!AbsolutePath(dir)) {
501 error_message = N_("The absolute path is required.");
511 error_message = N_("Directory does not exist.");
516 error_message = N_("Cannot read from this directory.");
521 if (dir == name || f.isDir()) {
522 error_message = N_("A file is required, not a directory.");
527 error_message = N_("File does not exist.");
532 error_message = N_("Cannot read from this file.");