]> git.lyx.org Git - features.git/blob - src/frontends/gtk/ghelpers.C
9dd1ed7541565a36679a640b575b579ce18600c7
[features.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 #include "ghelpers.h"
15
16 #include "lyxrc.h"
17 #include "funcrequest.h"
18 #include "debug.h"
19
20 #include "support/filetools.h"
21 #include "support/path_defines.h"
22
23 using std::string;
24 using std::vector;
25
26 namespace lyx {
27 namespace frontend {
28
29 Gtk::BuiltinStockID getGTKStockIcon(FuncRequest const & func)
30 {
31         switch (func.action) {
32
33                 case LFUN_MENUWRITE: return Gtk::Stock::SAVE;
34                 case LFUN_MENUNEW: return Gtk::Stock::NEW;
35                 case LFUN_WRITEAS: return Gtk::Stock::SAVE_AS;
36
37                 case LFUN_CENTER: return Gtk::Stock::JUSTIFY_CENTER;
38                 case LFUN_TOCVIEW: return Gtk::Stock::INDEX;
39                 case LFUN_CLOSEBUFFER: return Gtk::Stock::CLOSE;
40                 case LFUN_QUIT: return Gtk::Stock::QUIT;
41                 case LFUN_UNDO: return Gtk::Stock::UNDO;
42                 case LFUN_REDO: return Gtk::Stock::REDO;
43                 case LFUN_PASTE: return Gtk::Stock::PASTE;
44                 case LFUN_PASTESELECTION: return Gtk::Stock::PASTE;
45                 case LFUN_CUT: return Gtk::Stock::CUT;
46                 case LFUN_COPY: return Gtk::Stock::COPY;
47                 case LFUN_BOLD: return Gtk::Stock::BOLD;
48                 case LFUN_ITAL: return Gtk::Stock::ITALIC;
49                 case LFUN_FILE_OPEN: return Gtk::Stock::OPEN;
50                 case LFUN_RECONFIGURE: return Gtk::Stock::REFRESH;
51                 case LFUN_DIALOG_SHOW:
52                         if (func.argument == "findreplace")
53                                 return Gtk::Stock::FIND_AND_REPLACE;
54                         else if (func.argument == "print")
55                                 return Gtk::Stock::PRINT;
56                         else if (func.argument == "spellchecker")
57                                 return Gtk::Stock::SPELL_CHECK;
58                         else if (func.argument == "prefs")
59                                 return Gtk::Stock::PREFERENCES;
60                         else
61                                 return Gtk::Stock::MISSING_IMAGE;
62                         break;
63                 default: return Gtk::Stock::MISSING_IMAGE;
64         }
65 }
66
67 string const getDefaultUnit()
68 {
69         switch (lyxrc.default_papersize) {
70                 case PAPER_DEFAULT: return "cm";
71                 case PAPER_USLETTER:
72                 case PAPER_LEGALPAPER:
73                 case PAPER_EXECUTIVEPAPER: return "in"; break;
74                 case PAPER_A3PAPER:
75                 case PAPER_A4PAPER:
76                 case PAPER_A5PAPER:
77                 case PAPER_B5PAPER: return "cm"; break;
78         }
79 }
80
81
82 void unitsComboFromLength(Gtk::ComboBox * combo,
83                            Gtk::TreeModelColumn<Glib::ustring> const & stringcol,
84                            LyXLength const & len,
85                            std::string defunit)
86 {
87         string unit = stringFromUnit(len.unit());
88         if (unit.empty())
89                 unit = defunit;
90
91         Gtk::TreeModel::iterator it = combo->get_model()->children().begin();
92         Gtk::TreeModel::iterator end = combo->get_model()->children().end();
93         for (; it != end ; ++it) {
94                 if ((*it)[stringcol] == unit) {
95                         combo->set_active(it);
96                         return;
97                 }
98         }
99
100         // Fallen through, we didn't find the target length!
101         combo->set_active(0);
102         lyxerr << "unitsComboFromLength: couldn't find "
103                 "target unit '" << unit << "'\n";
104 }
105
106
107 vector<string> const buildLengthUnitList()
108 {
109         vector<string> data(unit_name_gui, unit_name_gui + num_units);
110
111         return data;
112 }
113
114
115 vector<string> const buildLengthNoRelUnitList()
116 {
117         vector<string> data;
118         for (int i = 0; i < num_units; ++i) {
119                 string str(unit_name_gui[i]);
120                 if (str.find("%") == -1)
121                         data.push_back(unit_name_gui[i]);
122         }
123
124         return data;
125 }
126
127
128 string const findGladeFile(string const & name)
129 {
130         // First, search in the installation directories.
131
132         string filename = lyx::support::LibFileSearch("glade", name, "glade");
133
134         if (!filename.empty())
135                 return filename;
136
137         // Second, search in the src tree.
138         string const dir =
139                 lyx::support::AddPath(lyx::support::top_srcdir(),
140                                       "src/frontends/gtk/glade");
141
142         filename = lyx::support::ChangeExtension(name, ".glade");
143         filename = lyx::support::AddName(dir, filename);
144
145         if (!lyx::support::IsFileReadable(filename)) {
146                 lyxerr << "Unable to find glade file \"" << name
147                        << "\". libglade is going to crash..." << std::endl;
148         }
149
150         return filename;
151 }
152
153 } // namespace frontend
154 } // namespace lyx