wxdragon-sys 0.9.18

Raw FFI bindings to libwxdragon (which statically links wxWidgets).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <wx/wxprec.h>
#include <wx/wx.h>
#include "../include/wxdragon.h"
#include <wx/translation.h>
#include <wx/intl.h>
#include <wx/uilocale.h>
#include <wx/arrstr.h>

// A wxTranslationsLoader that forwards to Rust callbacks. The C++ side is a
// dumb trampoline: it only builds the wxWidgets objects the callbacks cannot
// (wxMsgCatalog / wxArrayString); all loader policy lives in Rust.
class WxdRustTranslationsLoader : public wxTranslationsLoader
{
public:
    WxdRustTranslationsLoader(const wxd_RustTranslationsLoader_vtable* vtable,
                              void* user_data)
        : m_vtable(*vtable), m_user_data(user_data)
    {
    }

    ~WxdRustTranslationsLoader() override
    {
        if (m_vtable.destroy)
            m_vtable.destroy(m_user_data);
    }

    // Context passed as the `sink` to load_catalog; see the
    // wxd_TranslationsCatalogSink contract in wxd_translations.h.
    struct CatalogSink
    {
        const wxString* domain;
        wxMsgCatalog* catalog;
    };

    static void emit_catalog(void* sink, const uint8_t* data, size_t len)
    {
        if (!sink || !data)
            return;
        CatalogSink* s = reinterpret_cast<CatalogSink*>(sink);
        // Non-owning view is safe per the contract: consumed synchronously here,
        // wxMsgCatalog does not retain the buffer.
        s->catalog = wxMsgCatalog::CreateFromData(
            wxScopedCharBuffer::CreateNonOwned(
                reinterpret_cast<const char*>(data), len),
            *s->domain);
    }

    wxMsgCatalog* LoadCatalog(const wxString& domain,
                              const wxString& lang) override
    {
        if (!m_vtable.load_catalog)
            return nullptr;
        const wxScopedCharBuffer domainUtf8 = domain.utf8_str();
        const wxScopedCharBuffer langUtf8 = lang.utf8_str();
        CatalogSink sink{ &domain, nullptr };
        m_vtable.load_catalog(m_user_data, domainUtf8.data(), langUtf8.data(),
                              &sink, &emit_catalog);
        return sink.catalog;
    }

    wxArrayString GetAvailableTranslations(const wxString& domain) const override
    {
        wxArrayString langs;
        if (m_vtable.available) {
            const wxScopedCharBuffer domainUtf8 = domain.utf8_str();
            m_vtable.available(m_user_data, domainUtf8.data(),
                               reinterpret_cast<wxd_ArrayString_t*>(&langs));
        }
        return langs;
    }

private:
    wxd_RustTranslationsLoader_vtable m_vtable;
    void* m_user_data;
};

extern "C" {

wxd_Translations_t*
wxd_Translations_Get()
{
    return reinterpret_cast<wxd_Translations_t*>(wxTranslations::Get());
}

void
wxd_Translations_Set(wxd_Translations_t* translations)
{
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    wxTranslations::Set(wx_translations);
}

wxd_Translations_t*
wxd_Translations_Create()
{
    wxTranslations* translations = new wxTranslations();
    return reinterpret_cast<wxd_Translations_t*>(translations);
}

void
wxd_Translations_Destroy(wxd_Translations_t* translations)
{
    if (!translations)
        return;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    delete wx_translations;
}

void
wxd_Translations_SetLanguage(wxd_Translations_t* translations, int lang)
{
    if (!translations)
        return;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    wx_translations->SetLanguage(static_cast<wxLanguage>(lang));
}

void
wxd_Translations_SetLanguageStr(wxd_Translations_t* translations,
                                const char* lang)
{
    if (!translations)
        return;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    wx_translations->SetLanguage(wxString::FromUTF8(lang ? lang : ""));
}

bool
wxd_Translations_AddCatalog(wxd_Translations_t* translations,
                           const char* domain,
                           int msg_id_language)
{
    if (!translations || !domain)
        return false;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    return wx_translations->AddCatalog(wxString::FromUTF8(domain),
                                       static_cast<wxLanguage>(msg_id_language));
}

bool
wxd_Translations_AddStdCatalog(wxd_Translations_t* translations)
{
    if (!translations)
        return false;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    return wx_translations->AddStdCatalog();
}

bool
wxd_Translations_IsLoaded(wxd_Translations_t* translations, const char* domain)
{
    if (!translations || !domain)
        return false;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    return wx_translations->IsLoaded(wxString::FromUTF8(domain));
}

int
wxd_Translations_GetTranslatedString(wxd_Translations_t* translations,
                                     const char* orig,
                                     const char* domain,
                                     char* buffer,
                                     size_t buffer_len)
{
    if (!translations || !orig)
        return -1;

    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);

    wxString wx_domain;
    if (domain && domain[0] != '\0') {
        wx_domain = wxString::FromUTF8(domain);
    }

    const wxString* result =
        wx_translations->GetTranslatedString(wxString::FromUTF8(orig),
                                             wx_domain.empty() ? wxString()
                                                               : wx_domain);
    if (!result)
        return -1;

    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(*result, buffer,
                                                       buffer_len);
}

int
wxd_Translations_GetTranslatedPluralString(wxd_Translations_t* translations,
                                           const char* singular,
                                           const char* plural,
                                           unsigned int n,
                                           const char* domain,
                                           char* buffer,
                                           size_t buffer_len)
{
    if (!translations || !singular)
        return -1;

    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);

    wxString wx_domain;
    if (domain && domain[0] != '\0') {
        wx_domain = wxString::FromUTF8(domain);
    }

    // wxTranslations::GetTranslatedString with n parameter looks up the
    // appropriate plural form from the catalog. The plural string argument
    // is only used as a fallback when no translation is found.
    const wxString* result = wx_translations->GetTranslatedString(
        wxString::FromUTF8(singular),
        n,
        wx_domain.empty() ? wxString() : wx_domain);

    if (!result) {
        // No translation found - return the appropriate fallback
        if (plural && n != 1) {
            return (int)wxd_cpp_utils::copy_wxstring_to_buffer(
                wxString::FromUTF8(plural), buffer, buffer_len);
        }
        return -1;
    }

    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(*result, buffer,
                                                       buffer_len);
}

int
wxd_Translations_GetHeaderValue(wxd_Translations_t* translations,
                                const char* header,
                                const char* domain,
                                char* buffer,
                                size_t buffer_len)
{
    if (!translations || !header)
        return -1;

    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);

    wxString wx_domain;
    if (domain && domain[0] != '\0') {
        wx_domain = wxString::FromUTF8(domain);
    }

    wxString result = wx_translations->GetHeaderValue(
        wxString::FromUTF8(header),
        wx_domain.empty() ? wxString() : wx_domain);

    if (result.empty())
        return -1;

    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(result, buffer,
                                                       buffer_len);
}

int
wxd_Translations_GetBestTranslation(wxd_Translations_t* translations,
                                    const char* domain,
                                    int msg_id_language,
                                    char* buffer,
                                    size_t buffer_len)
{
    if (!translations || !domain)
        return -1;

    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);

    wxString result = wx_translations->GetBestTranslation(
        wxString::FromUTF8(domain), static_cast<wxLanguage>(msg_id_language));

    if (result.empty())
        return -1;

    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(result, buffer,
                                                       buffer_len);
}

int
wxd_Translations_GetAvailableTranslations(wxd_Translations_t* translations,
                                          const char* domain,
                                          char** langs_buffer,
                                          size_t buffer_count,
                                          size_t string_buffer_len)
{
    if (!translations || !domain)
        return 0;

    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);

    wxArrayString langs =
        wx_translations->GetAvailableTranslations(wxString::FromUTF8(domain));

    int count = (int)langs.GetCount();

    // If buffer provided, fill it in
    if (langs_buffer && buffer_count > 0 && string_buffer_len > 0) {
        size_t to_copy = (size_t)count < buffer_count ? (size_t)count
                                                      : buffer_count;
        for (size_t i = 0; i < to_copy; i++) {
            if (langs_buffer[i]) {
                wxd_cpp_utils::copy_wxstring_to_buffer(langs[i], langs_buffer[i],
                                                       string_buffer_len);
            }
        }
    }

    return count;
}

void
wxd_Translations_SetRustLoader(wxd_Translations_t* translations,
                               const wxd_RustTranslationsLoader_vtable* vtable,
                               void* user_data)
{
    if (!translations || !vtable)
        return;
    wxTranslations* wx_translations =
        reinterpret_cast<wxTranslations*>(translations);
    // wxTranslations takes ownership of the loader and deletes it (calling our
    // destructor, which releases user_data via vtable.destroy).
    wx_translations->SetLoader(
        new WxdRustTranslationsLoader(vtable, user_data));
}

void
wxd_FileTranslationsLoader_AddCatalogLookupPathPrefix(const char* prefix)
{
    if (!prefix)
        return;
    wxFileTranslationsLoader::AddCatalogLookupPathPrefix(
        wxString::FromUTF8(prefix));
}

// --- Locale Functions Implementation ---

int
wxd_Locale_GetLanguageName(int lang, char* buffer, size_t buffer_len)
{
    wxString name = wxLocale::GetLanguageName(static_cast<wxLanguage>(lang));
    if (name.empty())
        return -1;
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(name, buffer, buffer_len);
}

int
wxd_Locale_GetLanguageCanonicalName(int lang, char* buffer, size_t buffer_len)
{
    const wxLanguageInfo* info = wxLocale::GetLanguageInfo(static_cast<wxLanguage>(lang));
    if (!info)
        return -1;
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(info->CanonicalName, buffer, buffer_len);
}

const wxd_LanguageInfo_t*
wxd_Locale_FindLanguageInfo(const char* locale)
{
    if (!locale)
        return nullptr;
    const wxLanguageInfo* info = wxLocale::FindLanguageInfo(wxString::FromUTF8(locale));
    return reinterpret_cast<const wxd_LanguageInfo_t*>(info);
}

const wxd_LanguageInfo_t*
wxd_Locale_GetLanguageInfo(int lang)
{
    const wxLanguageInfo* info = wxLocale::GetLanguageInfo(static_cast<wxLanguage>(lang));
    return reinterpret_cast<const wxd_LanguageInfo_t*>(info);
}

int
wxd_Locale_GetSystemLanguage()
{
    return wxLocale::GetSystemLanguage();
}

// --- LanguageInfo Functions Implementation ---

int
wxd_LanguageInfo_GetDescription(const wxd_LanguageInfo_t* info, char* buffer, size_t buffer_len)
{
    if (!info)
        return -1;
    const wxLanguageInfo* wx_info = reinterpret_cast<const wxLanguageInfo*>(info);
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(wx_info->Description, buffer, buffer_len);
}

int
wxd_LanguageInfo_GetDescriptionNative(const wxd_LanguageInfo_t* info, char* buffer, size_t buffer_len)
{
    if (!info)
        return -1;
    const wxLanguageInfo* wx_info = reinterpret_cast<const wxLanguageInfo*>(info);
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(wx_info->DescriptionNative, buffer, buffer_len);
}

int
wxd_LanguageInfo_GetCanonicalName(const wxd_LanguageInfo_t* info, char* buffer, size_t buffer_len)
{
    if (!info)
        return -1;
    const wxLanguageInfo* wx_info = reinterpret_cast<const wxLanguageInfo*>(info);
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(wx_info->CanonicalName, buffer, buffer_len);
}

// --- UILocale Functions Implementation ---

wxd_UILocale_t*
wxd_UILocale_GetCurrent()
{
    // wxUILocale::GetCurrent() returns a reference to the current locale
    // We create a copy to store in our opaque pointer
    const wxUILocale& current = wxUILocale::GetCurrent();
    return reinterpret_cast<wxd_UILocale_t*>(new wxUILocale(current));
}

void
wxd_UILocale_Destroy(wxd_UILocale_t* locale)
{
    if (locale) {
        delete reinterpret_cast<wxUILocale*>(locale);
    }
}

int
wxd_UILocale_GetName(const wxd_UILocale_t* locale, char* buffer, size_t buffer_len)
{
    if (!locale)
        return -1;
    const wxUILocale* wx_locale = reinterpret_cast<const wxUILocale*>(locale);
    return (int)wxd_cpp_utils::copy_wxstring_to_buffer(wx_locale->GetName(), buffer, buffer_len);
}

int
wxd_UILocale_GetLanguage(const wxd_UILocale_t* locale)
{
    if (!locale)
        return 0; // wxLANGUAGE_UNKNOWN is 0 in Rust enum but 1 in C++? 
        // Rust: Default=0, Unknown=1. 
        // wxWidgets: wxLANGUAGE_UNKNOWN is usually 1. wxLANGUAGE_DEFAULT is 0.
        // Let's use the constant if possible, or 0/1. 
        // Actually, let's just return the int value.
        
    const wxUILocale* wx_locale = reinterpret_cast<const wxUILocale*>(locale);
    
    // Try to find via name
    const wxLanguageInfo* info = wxLocale::FindLanguageInfo(wx_locale->GetName());
    if (info)
        return info->Language;
        
    return 1; // wxLANGUAGE_UNKNOWN
}

} // extern "C"