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 "frontends/FileDialog.h"
18 #include "support/FileInfo.h"
19 #include "support/filetools.h"
20 #include "lyx_gui_misc.h" // WriteAlert
27 // Set an FL_OBJECT to activated or deactivated
28 void setEnabled(FL_OBJECT * ob, bool enable)
31 fl_activate_object(ob);
32 fl_set_object_lcol(ob, FL_BLACK);
34 fl_deactivate_object(ob);
35 fl_set_object_lcol(ob, FL_INACTIVE);
40 // Take a string and add breaks so that it fits into a desired label width, w
41 string formatted(string const & sin, int w, int size, int style)
44 if (sin.empty()) return sout;
46 // break sin up into a vector of individual words
47 vector<string> sentence;
49 for (string::const_iterator sit = sin.begin();
50 sit != sin.end(); ++sit) {
51 if ((*sit) == ' ' || (*sit) == '\n') {
52 sentence.push_back(word);
54 if ((*sit) == '\n') word += '\n';
61 // Flush remaining contents of word
62 if (!word.empty() ) sentence.push_back(word);
64 string line, line_plus_word;
65 for (vector<string>::const_iterator vit = sentence.begin();
66 vit != sentence.end(); ++vit) {
72 word = word.substr(1);
73 line_plus_word.erase();
77 if (!line_plus_word.empty() ) line_plus_word += ' ';
78 line_plus_word += word;
80 int length = fl_get_string_width(style, size,
81 line_plus_word.c_str(),
82 int(line_plus_word.length()));
85 line_plus_word = word;
88 line = line_plus_word;
90 // Flush remaining contents of line
95 if (sout[sout.length()-1] == '\n')
96 sout.erase(sout.length()-1);
102 string const browseFile(LyXView * lv, string const & filename,
103 string const & title,
104 string const & pattern,
105 pair<string,string> const & dir1,
106 pair<string,string> const & dir2)
108 string lastPath = ".";
109 if (!filename.empty()) lastPath = OnlyPath(filename);
111 FileDialog fileDlg(lv, title, LFUN_SELECT_FILE_SYNC, dir1, dir2);
113 FileDialog::Result result;
116 result = fileDlg.Select(lastPath, pattern, OnlyFilename(filename));
118 if (result.second.empty())
119 return result.second;
121 lastPath = OnlyPath(result.second);
123 if (result.second.find_first_of("#~$% ") == string::npos)
126 WriteAlert(_("Filename can't contain any "
127 "of these characters:"),
128 _("space, '#', '~', '$' or '%'."));
131 return result.second;
137 // sorted by hand to prevent LyXLex from complaining on read().
138 keyword_item xformTags[] = {
139 { "\\gui_background", FL_COL1 },
140 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
141 { "\\gui_buttonleft", FL_LEFT_BCOL },
142 { "\\gui_buttonright", FL_RIGHT_BCOL },
143 { "\\gui_buttontop", FL_TOP_BCOL },
144 { "\\gui_inactive", FL_INACTIVE },
145 { "\\gui_push_button", FL_YELLOW },
146 { "\\gui_selected", FL_MCOL },
147 { "\\gui_text", FL_BLACK }
151 const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
156 bool XformsColor::read(string const & filename)
158 LyXLex lexrc(xformTags, xformCount);
159 if (!lexrc.setFile(filename))
162 while (lexrc.IsOK()) {
163 int const le = lexrc.lex();
166 case LyXLex::LEX_UNDEF:
167 lexrc.printError("Unknown tag `$$Token'");
169 case LyXLex::LEX_FEOF:
176 if (!lexrc.next()) break;
177 col.r = lexrc.GetInteger();
179 if (!lexrc.next()) break;
180 col.g = lexrc.GetInteger();
182 if (!lexrc.next()) break;
183 col.b = lexrc.GetInteger();
185 fl_mapcolor(le, col.r, col.g, col.b);
192 bool XformsColor::write(string const & filename)
194 ofstream os(filename.c_str());
198 os << "### This file is part of\n"
199 << "### ========================================================\n"
200 << "### LyX, The Document Processor\n"
202 << "### Copyright 1995 Matthias Ettrich\n"
203 << "### Copyright 1995-2000 The LyX Team.\n"
205 << "### ========================================================\n"
207 << "# This file is written by LyX, if you want to make your own\n"
208 << "# modifications you should do them from inside LyX and save\n"
211 for (int i = 0; i < xformCount; ++i) {
212 string const tag = xformTags[i].tag;
213 int const colorID = xformTags[i].code;
216 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
219 << color.r << " " << color.g << " " << color.b << "\n";
226 string RWInfo::error_message;
228 bool RWInfo::WriteableDir(string const & name)
230 error_message.erase();
232 if (!AbsolutePath(name)) {
233 error_message = N_("The absolute path is required.");
237 FileInfo const tp(name);
239 error_message = N_("Directory does not exist.");
243 if (!tp.writable()) {
244 error_message = N_("Cannot write to this directory.");
252 bool RWInfo::ReadableDir(string const & name)
254 error_message.erase();
256 if (!AbsolutePath(name)) {
257 error_message = N_("The absolute path is required.");
261 FileInfo const tp(name);
263 error_message = N_("Directory does not exist.");
267 if (!tp.readable()) {
268 error_message = N_("Cannot read this directory.");
276 bool RWInfo::WriteableFile(string const & name)
278 // A writeable file is either:
279 // * An existing file to which we have write access, or
280 // * A file that doesn't yet exist but that would exist in a writeable
283 error_message.erase();
286 error_message = N_("No file input.");
290 string const dir = OnlyPath(name);
291 if (!AbsolutePath(dir)) {
292 error_message = N_("The absolute path is required.");
302 error_message = N_("Directory does not exist.");
307 error_message = N_("Cannot write to this directory.");
312 if (dir == name || f.isDir()) {
313 error_message = N_("A file is required, not a directory.");
317 if (f.exist() && !f.writable()) {
318 error_message = N_("Cannot write to this file.");
326 bool RWInfo::ReadableFile(string const & name)
328 error_message.erase();
331 error_message = N_("No file input.");
335 string const dir = OnlyPath(name);
336 if (!AbsolutePath(dir)) {
337 error_message = N_("The absolute path is required.");
347 error_message = N_("Directory does not exist.");
352 error_message = N_("Cannot read from this directory.");
357 if (dir == name || f.isDir()) {
358 error_message = N_("A file is required, not a directory.");
363 error_message = N_("File does not exist.");
368 error_message = N_("Cannot read from this file.");