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