]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GSpellchecker.C
enable Font cache only for MacOSX and inline width() for other platform.
[lyx.git] / src / frontends / gtk / GSpellchecker.C
1 /**
2  * \file GSpellchecker.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
16 #endif
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
19 #endif
20
21 #include "GSpellchecker.h"
22 #include "controllers/ControlSpellchecker.h"
23
24 #include "ghelpers.h"
25
26 #include "support/convert.h"
27
28 using std::string;
29
30 namespace lyx {
31 namespace frontend {
32
33 GSpellchecker::GSpellchecker(Dialog & parent)
34         : GViewCB<ControlSpellchecker, GViewGladeB>(parent, lyx::to_utf8(_("Spellchecker")), false)
35 {}
36
37
38 void GSpellchecker::doBuild()
39 {
40         string const gladeName = findGladeFile("spellcheck");
41         xml_ = Gnome::Glade::Xml::create(gladeName);
42
43         Gtk::Button * button;
44         xml_->get_widget("Close", button);
45         setCancel(button);
46
47         xml_->get_widget("Suggestions", suggestionsview_);
48         xml_->get_widget("Unknown", unknownentry_);
49         xml_->get_widget("Replacement", replacemententry_);
50         xml_->get_widget("Progress", progress_);
51
52         listCols_.add(listCol_);
53         suggestionsstore_ = Gtk::ListStore::create(listCols_);
54         suggestionsview_->set_model(suggestionsstore_);
55         suggestionsview_->append_column("Suggestion", listCol_);
56         suggestionssel_ = suggestionsview_->get_selection();
57
58         // Single click in suggestion list
59         suggestionssel_->signal_changed().connect(
60                 sigc::mem_fun(*this, &GSpellchecker::onSuggestionSelection));
61
62         // Double click in suggestion list
63         suggestionsview_->signal_row_activated().connect(
64                 sigc::mem_fun(*this, &GSpellchecker::onSuggestionActivate));
65
66         // Because it's like a Replace button when double clicked
67         bcview().addReadOnly(suggestionsview_);
68
69         xml_->get_widget("Replace", button);
70         bcview().addReadOnly(button);
71         button->signal_clicked().connect(
72                 sigc::bind<bool>(
73                         sigc::mem_fun(*this, &GSpellchecker::onReplace), false));
74
75         xml_->get_widget("ReplaceAll", button);
76         bcview().addReadOnly(button);
77         button->signal_clicked().connect(
78                 sigc::bind<bool>(
79                         sigc::mem_fun(*this, &GSpellchecker::onReplace), true));
80
81         xml_->get_widget("Ignore", ignorebutton_);
82         ignorebutton_->signal_clicked().connect(
83                 sigc::mem_fun(*this, &GSpellchecker::onIgnore));
84
85         xml_->get_widget("IgnoreAll", button);
86         button->signal_clicked().connect(
87                 sigc::mem_fun(*this, &GSpellchecker::onIgnoreAll));
88
89         xml_->get_widget("Add", button);
90         button->signal_clicked().connect(
91                 sigc::mem_fun(*this, &GSpellchecker::onAdd));
92 }
93
94
95 void GSpellchecker::show()
96 {
97         if (!window()) {
98                 build();
99         }
100         bcview().refreshReadOnly();
101         controller().check();
102         if (!controller().getWord().empty())
103                 window()->show();
104 }
105
106 void GSpellchecker::partialUpdate(int s)
107 {
108         ControlSpellchecker::State const state =
109                 static_cast<ControlSpellchecker::State>(s);
110
111         if (state == ControlSpellchecker::SPELL_FOUND_WORD) {
112                 string word = controller().getWord();
113                 Glib::ustring utfword = Glib::locale_to_utf8(word);
114                 unknownentry_->set_text(utfword);
115                 replacemententry_->set_text(utfword);
116
117                 // Get the list of suggestions
118                 suggestionsstore_->clear();
119                 while (!(word = controller().getSuggestion()).empty()) {
120                         utfword = Glib::locale_to_utf8(word);
121                         (*suggestionsstore_->append())[listCol_] = utfword;
122                 }
123
124                 if (readOnly())
125                         // In readonly docs the user must just be browsing through
126                         ignorebutton_->grab_focus();
127                 else
128                         // In general we expect the user to type their replacement
129                         replacemententry_->grab_focus();
130         }
131
132         int const progress = controller().getProgress();
133         if (progress != 0) {
134                 progress_->set_fraction(float(progress)/100.0f);
135                 progress_->set_text(convert<string>(progress) + "% " + lyx::to_utf8(_("checked")));
136         }
137 }
138
139
140 void GSpellchecker::onSuggestionActivate(
141         Gtk::TreeModel::Path const & path,
142         Gtk::TreeViewColumn * /*col*/)
143 {
144         Glib::ustring const suggestion =
145                 (*suggestionsstore_->get_iter(path))[listCol_];
146
147         if (!suggestion.empty())
148                 controller().replace(suggestion);
149 }
150
151
152 void GSpellchecker::onSuggestionSelection()
153 {
154         Glib::ustring const suggestion =
155                 (*suggestionssel_->get_selected())[listCol_];
156
157         if (!suggestion.empty())
158                 replacemententry_->set_text(suggestion);
159 }
160
161
162 void GSpellchecker::onIgnore()
163 {
164         controller().check();
165 }
166
167
168 void GSpellchecker::onIgnoreAll()
169 {
170         controller().ignoreAll();
171 }
172
173
174 void GSpellchecker::onAdd()
175 {
176         controller().insert();
177 }
178
179
180 void GSpellchecker::onReplace(bool const all)
181 {
182         Glib::ustring const replacement = replacemententry_->get_text();
183         if (all)
184                 controller().replaceAll(replacement);
185         else
186                 controller().replace(replacement);
187 }
188
189
190 } // namespace frontend
191 } // namespace lyx