1 /** Collection of some useful xform helper functions
6 #include FORMS_H_LOCATION
13 #include "xform_helpers.h"
14 #include <fstream> // ofstream
16 #include "filedlg.h" // LyXFileDlg
17 #include "support/FileInfo.h"
18 #include "support/filetools.h"
19 #include "lyx_gui_misc.h" // WriteAlert
26 // Take a string and add breaks so that it fits into a desired label width, w
27 string formatted( string const & sin, int w, int size, int style )
30 if (sin.empty() ) return sout;
32 // break sin up into a vector of individual words
33 vector<string> sentence;
35 for (string::const_iterator sit = sin.begin();
36 sit != sin.end(); ++sit) {
37 if ((*sit) == ' ' || (*sit) == '\n') {
38 sentence.push_back(word);
44 // Flush remaining contents of word
45 if (!word.empty() ) sentence.push_back(word);
48 for (vector<string>::const_iterator vit = sentence.begin();
49 vit != sentence.end(); ++vit) {
50 if (!l1.empty() ) l1 += ' ';
52 int length = fl_get_string_width(style, size, l1.c_str(),
55 if (!sout.empty() ) sout += '\n';
62 // Flush remaining contents of line
64 if (!sout.empty() ) sout += '\n';
72 string const browseFile( string const & filename,
74 string const & pattern,
75 pair<string,string> const & dir1,
76 pair<string,string> const & dir2 )
78 string lastPath = ".";
79 if( !filename.empty() ) lastPath = OnlyPath(filename);
83 if( !dir1.second.empty() ) {
84 FileInfo fileInfo( dir1.second );
85 if( fileInfo.isOK() && fileInfo.isDir() )
86 fileDlg.SetButton( 0, _(dir1.first), dir1.second );
89 if( !dir2.second.empty() ) {
90 FileInfo fileInfo( dir2.second );
91 if( fileInfo.isOK() && fileInfo.isDir() )
92 fileDlg.SetButton( 1, _(dir2.first), dir2.second );
98 string p = fileDlg.Select(_(title),
100 pattern, OnlyFilename(filename) );
102 if (p.empty()) return p;
104 lastPath = OnlyPath(p);
106 if (p.find_first_of("#~$% ") != string::npos) {
107 WriteAlert(_("Filename can't contain any "
108 "of these characters:"),
109 _("space, '#', '~', '$' or '%'."));
121 // sorted by hand to prevent LyXLex from complaining on read().
123 keyword_item xformTags[] = {
124 { "\\gui_background", FL_COL1 },
125 { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
126 { "\\gui_buttonleft", FL_LEFT_BCOL },
127 { "\\gui_buttonright", FL_RIGHT_BCOL },
128 { "\\gui_buttontop", FL_TOP_BCOL },
129 { "\\gui_inactive", FL_INACTIVE },
130 { "\\gui_push_button", FL_YELLOW },
131 { "\\gui_selected", FL_MCOL },
132 { "\\gui_text", FL_BLACK }
136 static const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
139 bool XformColor::read(string const & filename)
141 LyXLex lexrc(xformTags, xformCount);
142 if (!lexrc.setFile(filename))
145 while (lexrc.IsOK()) {
146 int const le = lexrc.lex();
149 case LyXLex::LEX_UNDEF:
150 lexrc.printError("Unknown tag `$$Token'");
152 case LyXLex::LEX_FEOF:
159 if (!lexrc.next()) break;
160 col.r = lexrc.GetInteger();
162 if (!lexrc.next()) break;
163 col.g = lexrc.GetInteger();
165 if (!lexrc.next()) break;
166 col.b = lexrc.GetInteger();
168 fl_mapcolor(le, col.r, col.g, col.b);
175 bool XformColor::write(string const & filename)
177 ofstream os(filename.c_str());
181 os << "### This file is part of\n"
182 << "### ========================================================\n"
183 << "### LyX, The Document Processor\n"
185 << "### Copyright 1995 Matthias Ettrich\n"
186 << "### Copyright 1995-2000 The LyX Team.\n"
188 << "### ========================================================\n"
190 << "# This file is written by LyX, if you want to make your own\n"
191 << "# modifications you should do them from inside LyX and save\n"
194 for (int i = 0; i < xformCount; ++i) {
195 string const tag = xformTags[i].tag;
196 int const colorID = xformTags[i].code;
199 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
202 << color.r << " " << color.g << " " << color.b << "\n";
209 string RWInfo::error_message;
211 bool RWInfo::WriteableDir(string const & name)
213 error_message.erase();
215 if (!AbsolutePath(name)) {
216 error_message = N_("The absolute path is required.");
220 FileInfo const tp(name);
222 error_message = N_("Directory does not exist.");
226 if (!tp.writable()) {
227 error_message = N_("Cannot write to this directory.");
235 bool RWInfo::ReadableDir(string const & name)
237 error_message.erase();
239 if (!AbsolutePath(name)) {
240 error_message = N_("The absolute path is required.");
244 FileInfo const tp(name);
246 error_message = N_("Directory does not exist.");
250 if (!tp.readable()) {
251 error_message = N_("Cannot read this directory.");
259 bool RWInfo::WriteableFile(string const & name)
261 // A writeable file is either:
262 // * An existing file to which we have write access, or
263 // * A file that doesn't yet exist but that would exist in a writeable
266 error_message.erase();
269 error_message = N_("No file input.");
273 string const dir = OnlyPath(name);
274 if (!AbsolutePath(dir)) {
275 error_message = N_("The absolute path is required.");
285 error_message = N_("Directory does not exist.");
290 error_message = N_("Cannot write to this directory.");
295 if (dir == name || f.isDir()) {
296 error_message = N_("A file is required, not a directory.");
300 if (f.exist() && !f.writable()) {
301 error_message = N_("Cannot write to this file.");
309 bool RWInfo::ReadableFile(string const & name)
311 error_message.erase();
314 error_message = N_("No file input.");
318 string const dir = OnlyPath(name);
319 if (!AbsolutePath(dir)) {
320 error_message = N_("The absolute path is required.");
330 error_message = N_("Directory does not exist.");
335 error_message = N_("Cannot read from this directory.");
340 if (dir == name || f.isDir()) {
341 error_message = N_("A file is required, not a directory.");
346 error_message = N_("File does not exist.");
351 error_message = N_("Cannot read from this file.");