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