Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
libspelling-wrapper.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5#ifndef INKSCAPE_UI_LIBSPELLING_WRAPPER_H
6#define INKSCAPE_UI_LIBSPELLING_WRAPPER_H
7
8#include <vector>
9
10#include <libspelling.h>
11#include <glibmm/ustring.h>
12#include <giomm/actiongroup.h>
13#include <giomm/menumodel.h>
14
15#include "util/delete-with.h"
16#include "util/gobjectptr.h"
17
18namespace Inkscape::UI {
19
20template <typename T, typename F>
21void foreach(GPtrArray *arr, F &&f)
22{
23 g_ptr_array_foreach(arr, +[] (void *ptr, void *data) {
24 auto t = reinterpret_cast<T *>(ptr);
25 auto f = reinterpret_cast<F *>(data);
26 f->operator()(t);
27 }, &f);
28}
29
30template <typename F>
31void foreach(char **strs, F &&f)
32{
33 if (!strs) {
34 return;
35 }
36 for (int i = 0; strs[i]; i++) {
37 f(strs[i]);
38 }
39}
40
41template <typename F>
42inline void list_language_names_and_codes(SpellingProvider *provider, F &&cb)
43{
44#if SPELLING_CHECK_VERSION(0, 3, 0)
45 auto const languages = Util::delete_with<g_object_unref>(spelling_provider_list_languages(provider));
46 auto const n_items = g_list_model_get_n_items(languages.get());
47
48 for (auto i = 0U; i < n_items; ++i) {
49 auto const language = static_cast<SpellingLanguage *>(g_list_model_get_item(languages.get(), i));
50 auto const name = spelling_language_get_name(language);
51 auto const code = spelling_language_get_code(language);
52 cb(name, code);
53 }
54#else
55 // TODO: remove when libspelling has API stability
56 auto const languages = Util::delete_with<g_ptr_array_unref>(spelling_provider_list_languages(provider));
57 foreach
58 <SpellingLanguageInfo>(languages.get(), [&](auto language) {
59 auto const name = spelling_language_info_get_name(language);
60 auto const code = spelling_language_info_get_code(language);
61 cb(name, code);
62 });
63#endif
64}
65
66inline auto list_corrections_c(SpellingChecker *checker, char const *word)
67{
68 return Util::delete_with<g_strfreev>(spelling_checker_list_corrections(checker, word));
69}
70
71inline auto list_corrections(SpellingChecker *checker, char const *word)
72{
73 std::vector<Glib::ustring> result;
74
75 foreach(list_corrections_c(checker, word).get(), [&] (auto correction) {
76 result.emplace_back(correction);
77 });
78
79 return result;
80}
81
82inline auto spelling_text_buffer_adapter_create(GtkSourceBuffer *buffer, SpellingChecker *checker)
83{
84 return Util::GObjectPtr(spelling_text_buffer_adapter_new(buffer, checker));
85}
86
87inline auto get_menu_model(SpellingTextBufferAdapter &adapter)
88{
89 return Glib::wrap(spelling_text_buffer_adapter_get_menu_model(&adapter), true);
90}
91
92inline auto as_action_group(SpellingTextBufferAdapter &adapter)
93{
94 return Glib::wrap(G_ACTION_GROUP(&adapter), true);
95}
96
97inline void set_enabled(SpellingTextBufferAdapter &adapter, bool enabled)
98{
99 spelling_text_buffer_adapter_set_enabled(&adapter, enabled);
100}
101
102} // namespace Inkscape::UI
103
104#endif // INKSCAPE_UI_LIBSPELLING_WRAPPER_H
A smart pointer that shares ownership of a GObject.
Definition gobjectptr.h:18
Css & result
Ad-hoc smart pointer useful when interfacing with C code.
User interface code.
Definition desktop.h:113
void set_enabled(SpellingTextBufferAdapter &adapter, bool enabled)
auto list_corrections(SpellingChecker *checker, char const *word)
auto list_corrections_c(SpellingChecker *checker, char const *word)
auto spelling_text_buffer_adapter_create(GtkSourceBuffer *buffer, SpellingChecker *checker)
auto get_menu_model(SpellingTextBufferAdapter &adapter)
auto as_action_group(SpellingTextBufferAdapter &adapter)
void list_language_names_and_codes(SpellingProvider *provider, F &&cb)
static const Point data[]
Glib::ustring name
Definition toolbars.cpp:55