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