]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/ghelpers.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / gtk / ghelpers.C
1 /**
2  * \file ghelpers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author John Spray
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 // Too hard to make concept checks work with this file
15 #ifdef _GLIBCXX_CONCEPT_CHECKS
16 #undef _GLIBCXX_CONCEPT_CHECKS
17 #endif
18 #ifdef _GLIBCPP_CONCEPT_CHECKS
19 #undef _GLIBCPP_CONCEPT_CHECKS
20 #endif
21
22 #include "ghelpers.h"
23
24 #include "lyxrc.h"
25 #include "funcrequest.h"
26 #include "debug.h"
27
28 #include "support/filetools.h"
29 #include "support/package.h"
30
31 #include <sstream>
32
33 using std::string;
34 using std::vector;
35
36 namespace lyx {
37 namespace frontend {
38
39 string const getLengthFromWidgets(Gtk::Adjustment const & adj, Gtk::ComboBoxText const & combo)
40 {
41         std::ostringstream os;
42         os << adj.get_value();
43         os << combo.get_active_text();
44         return os.str();
45 }
46
47
48 void setWidgetsFromLength(Gtk::Adjustment & adj, Gtk::ComboBoxText & combo, LyXLength const & length)
49 {
50         adj.set_value(length.value());
51
52         string unit = stringFromUnit(length.unit());
53         if (unit.empty())
54                 unit = getDefaultUnit();
55
56         comboBoxTextSet(combo,unit);
57 }
58
59
60 void populateUnitCombo(Gtk::ComboBoxText & combo, bool const userelative)
61 {
62         vector<string> units = buildLengthUnitList(userelative);
63
64         vector<string>::const_iterator it = units.begin();
65         vector<string>::const_iterator end = units.end();
66         for(; it != end; ++it)
67                 combo.append_text(*it);
68 }
69
70
71 int comboBoxTextSet(Gtk::ComboBoxText & combo, Glib::ustring target)
72 {
73         int const children = combo.get_model()->children().size();
74         for (int i = 0; i < children; i++) {
75                 combo.set_active(i);
76                 if (combo.get_active_text() == target)
77                         return 0;
78         }
79         return -1;
80 }
81
82
83 Gtk::BuiltinStockID getGTKStockIcon(FuncRequest const & func)
84 {
85         switch (func.action) {
86                 case LFUN_MENUWRITE: return Gtk::Stock::SAVE;
87                 case LFUN_MENUNEW: return Gtk::Stock::NEW;
88                 case LFUN_WRITEAS: return Gtk::Stock::SAVE_AS;
89                 case LFUN_CENTER: return Gtk::Stock::JUSTIFY_CENTER;
90                 case LFUN_TOCVIEW: return Gtk::Stock::INDEX;
91                 case LFUN_CLOSEBUFFER: return Gtk::Stock::CLOSE;
92                 case LFUN_QUIT: return Gtk::Stock::QUIT;
93                 case LFUN_UNDO: return Gtk::Stock::UNDO;
94                 case LFUN_REDO: return Gtk::Stock::REDO;
95                 case LFUN_PASTE: return Gtk::Stock::PASTE;
96                 case LFUN_PASTESELECTION: return Gtk::Stock::PASTE;
97                 case LFUN_CUT: return Gtk::Stock::CUT;
98                 case LFUN_COPY: return Gtk::Stock::COPY;
99                 case LFUN_BOLD: return Gtk::Stock::BOLD;
100                 case LFUN_ITAL: return Gtk::Stock::ITALIC;
101                 case LFUN_FILE_OPEN: return Gtk::Stock::OPEN;
102                 case LFUN_RECONFIGURE: return Gtk::Stock::REFRESH;
103                 case LFUN_DIALOG_SHOW:
104                         if (func.argument == "findreplace")
105                                 return Gtk::Stock::FIND_AND_REPLACE;
106                         else if (func.argument == "print")
107                                 return Gtk::Stock::PRINT;
108                         else if (func.argument == "spellchecker")
109                                 return Gtk::Stock::SPELL_CHECK;
110                         else if (func.argument == "prefs")
111                                 return Gtk::Stock::PREFERENCES;
112                         else
113                                 return Gtk::Stock::MISSING_IMAGE;
114                         break;
115                 default: return Gtk::Stock::MISSING_IMAGE;
116         }
117 }
118
119
120 string const getDefaultUnit()
121 {
122         switch (lyxrc.default_papersize) {
123                 case PAPER_DEFAULT: return "cm";
124                 case PAPER_USLETTER:
125                 case PAPER_LEGALPAPER:
126                 case PAPER_EXECUTIVEPAPER: return "in"; break;
127                 case PAPER_A3PAPER:
128                 case PAPER_A4PAPER:
129                 case PAPER_A5PAPER:
130                 case PAPER_B5PAPER: return "cm"; break;
131         }
132         // shut up compiler
133         return "cm";
134 }
135
136
137 void unitsComboFromLength(Gtk::ComboBox * combo,
138                            Gtk::TreeModelColumn<Glib::ustring> const & stringcol,
139                            LyXLength const & len,
140                            std::string const & defunit)
141 {
142         string unit = stringFromUnit(len.unit());
143         if (unit.empty())
144                 unit = defunit;
145
146         Gtk::TreeModel::iterator it = combo->get_model()->children().begin();
147         Gtk::TreeModel::iterator end = combo->get_model()->children().end();
148         for (; it != end ; ++it) {
149                 if ((*it)[stringcol] == unit) {
150                         combo->set_active(it);
151                         return;
152                 }
153         }
154
155         // Fallen through, we didn't find the target length!
156         combo->set_active(0);
157         lyxerr << "unitsComboFromLength: couldn't find "
158                 "target unit '" << unit << "'\n";
159 }
160
161
162 vector<string> const buildLengthUnitList(bool const userelative)
163 {
164         //vector<string> data(unit_name_gui, unit_name_gui + num_units);
165         vector<string> data;
166         if (userelative) {
167                 data = vector<string>(unit_name_gui, unit_name_gui + num_units);
168         } else {
169                 for (int i = 0; i < num_units; ++i) {
170                         string str(unit_name_gui[i]);
171                         if (str.find("%") == string::npos)
172                                 data.push_back(unit_name_gui[i]);
173                 }
174         }
175         return data;
176 }
177
178
179 string const findGladeFile(string const & name)
180 {
181         // First, search in the installation directories.
182
183         string filename = lyx::support::LibFileSearch("glade", name, "glade");
184
185         if (!filename.empty())
186                 return filename;
187
188         // Second, search in the src tree.
189         string const dir =
190                 lyx::support::AddPath(lyx::support::package().top_srcdir(),
191                                       "src/frontends/gtk/glade");
192
193         filename = lyx::support::ChangeExtension(name, ".glade");
194         filename = lyx::support::AddName(dir, filename);
195
196         if (!lyx::support::IsFileReadable(filename)) {
197                 lyxerr << "Unable to find glade file \"" << name
198                        << "\". libglade is going to crash..." << std::endl;
199         }
200
201         return filename;
202 }
203
204 } // namespace frontend
205 } // namespace lyx