wxdragon 0.9.15

Safe Rust bindings for wxWidgets via the wxDragon C wrapper
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Internationalization (i18n) support using wxWidgets' translations system.
//!
//! This module provides access to wxWidgets' built-in translation system,
//! which uses gettext-style .mo/.po files for message catalogs.
//!
//! # Example
//! ```rust,no_run
//! use wxdragon::prelude::*;
//!
//! // Set up translations
//! let translations = Translations::new();
//! translations.set_language(Language::French);
//!
//! // Add catalog lookup path
//! add_catalog_lookup_path_prefix("./locale");
//!
//! // Load message catalog
//! if translations.add_catalog("myapp") {
//!     // Set as global translations
//!     Translations::set_global(translations);
//! }
//!
//! // Later, translate strings
//! let hello = translate("Hello");
//! ```

use crate::language::Language;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::c_char;
use wxdragon_sys as ffi;

/// A translations manager for internationalization support.
///
/// `Translations` wraps wxWidgets' wxTranslations class, which manages
/// loading and lookup of message catalogs for UI translation.
///
/// # Singleton Pattern
/// wxWidgets uses a global translations instance. You can:
/// - Use `Translations::get()` to access the current global instance
/// - Create a new instance with `Translations::new()` and set it as global
///   with `Translations::set_global()`
///
/// # Example
/// ```rust,no_run
/// use wxdragon::prelude::*;
///
/// // Create and configure translations
/// let translations = Translations::new();
/// translations.set_language(Language::German);
/// translations.add_catalog("myapp");
///
/// // Set as the global translations instance
/// Translations::set_global(translations);
///
/// // Now translations will be used automatically
/// ```
pub struct Translations {
    ptr: *mut ffi::wxd_Translations_t,
    owned: bool,
    // Marker to make this type !Send and !Sync since wxWidgets is not thread-safe
    _marker: PhantomData<*const ()>,
}

impl Translations {
    /// Get the global translations instance.
    ///
    /// Returns `Some(Translations)` if a global instance exists, `None` otherwise.
    /// The returned instance is not owned (won't be destroyed when dropped).
    pub fn get() -> Option<Self> {
        let ptr = unsafe { ffi::wxd_Translations_Get() };
        if ptr.is_null() {
            None
        } else {
            Some(Self {
                ptr,
                owned: false,
                _marker: PhantomData,
            })
        }
    }

    /// Create a new translations instance.
    ///
    /// The instance is owned and will be destroyed when dropped,
    /// unless it's set as the global instance via `set_global()`.
    pub fn new() -> Self {
        let ptr = unsafe { ffi::wxd_Translations_Create() };
        Self {
            ptr,
            owned: true,
            _marker: PhantomData,
        }
    }

    /// Set this translations instance as the global instance.
    ///
    /// This transfers ownership to wxWidgets. The instance will be
    /// managed by wxWidgets and destroyed when a new global instance
    /// is set or the application exits.
    ///
    /// After calling this, the `Translations` instance no longer owns
    /// the underlying pointer.
    pub fn set_global(mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::wxd_Translations_Set(self.ptr) };
            self.owned = false;
        }
    }

    /// Set the language for translations using a `Language` enum value.
    ///
    /// This determines which message catalog language is used.
    pub fn set_language(&self, lang: Language) {
        if self.ptr.is_null() {
            return;
        }
        unsafe { ffi::wxd_Translations_SetLanguage(self.ptr, lang.as_i32()) };
    }

    /// Set the language for translations using a language string.
    ///
    /// The string should be a language code like "en", "en_US", "fr_FR", etc.
    pub fn set_language_str(&self, lang: &str) {
        if self.ptr.is_null() {
            return;
        }
        let c_lang = match CString::new(lang) {
            Ok(s) => s,
            Err(_) => return,
        };
        unsafe { ffi::wxd_Translations_SetLanguageStr(self.ptr, c_lang.as_ptr()) };
    }

    /// Add a message catalog for the given domain.
    ///
    /// The domain is typically the application or library name.
    /// Returns `true` if the catalog was successfully loaded.
    ///
    /// Uses `Language::English` as the default message ID language.
    pub fn add_catalog(&self, domain: &str) -> bool {
        self.add_catalog_with_lang(domain, Language::English)
    }

    /// Add a message catalog with explicit source language.
    ///
    /// The `msg_id_language` specifies what language the original
    /// strings in the source code are in. This helps wxWidgets
    /// find the best translation when the exact target language
    /// isn't available.
    pub fn add_catalog_with_lang(&self, domain: &str, msg_id_language: Language) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_domain = match CString::new(domain) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_Translations_AddCatalog(self.ptr, c_domain.as_ptr(), msg_id_language.as_i32()) }
    }

    /// Add the standard wxWidgets message catalog.
    ///
    /// This loads wxWidgets' own translations for standard UI elements
    /// like "OK", "Cancel", etc.
    pub fn add_std_catalog(&self) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        unsafe { ffi::wxd_Translations_AddStdCatalog(self.ptr) }
    }

    /// Check if a catalog for the given domain is loaded.
    pub fn is_loaded(&self, domain: &str) -> bool {
        if self.ptr.is_null() {
            return false;
        }
        let c_domain = match CString::new(domain) {
            Ok(s) => s,
            Err(_) => return false,
        };
        unsafe { ffi::wxd_Translations_IsLoaded(self.ptr, c_domain.as_ptr()) }
    }

    /// Get a translated string.
    ///
    /// Returns the translated string if found, otherwise `None`.
    ///
    /// # Arguments
    /// * `orig` - The original string to translate
    /// * `domain` - Optional domain to search in (use empty string for default)
    pub fn get_string(&self, orig: &str, domain: &str) -> Option<String> {
        if self.ptr.is_null() {
            return None;
        }
        let c_orig = CString::new(orig).ok()?;
        let c_domain = CString::new(domain).ok()?;

        // First get the length
        let len = unsafe {
            ffi::wxd_Translations_GetTranslatedString(self.ptr, c_orig.as_ptr(), c_domain.as_ptr(), std::ptr::null_mut(), 0)
        };

        if len < 0 {
            return None;
        }

        // Now get the actual string
        let mut buf: Vec<c_char> = vec![0; len as usize + 1];
        unsafe {
            ffi::wxd_Translations_GetTranslatedString(self.ptr, c_orig.as_ptr(), c_domain.as_ptr(), buf.as_mut_ptr(), buf.len())
        };

        Some(unsafe { CStr::from_ptr(buf.as_ptr()).to_string_lossy().to_string() })
    }

    /// Get a plural-form translated string.
    ///
    /// Returns the appropriate plural form based on the count `n`.
    ///
    /// # Arguments
    /// * `singular` - The singular form of the string
    /// * `plural` - The plural form of the string
    /// * `n` - The count used to determine plural form
    /// * `domain` - Optional domain to search in (use empty string for default)
    pub fn get_plural_string(&self, singular: &str, plural: &str, n: u32, domain: &str) -> Option<String> {
        if self.ptr.is_null() {
            return None;
        }
        let c_singular = CString::new(singular).ok()?;
        let c_plural = CString::new(plural).ok()?;
        let c_domain = CString::new(domain).ok()?;

        // First get the length
        let len = unsafe {
            ffi::wxd_Translations_GetTranslatedPluralString(
                self.ptr,
                c_singular.as_ptr(),
                c_plural.as_ptr(),
                n,
                c_domain.as_ptr(),
                std::ptr::null_mut(),
                0,
            )
        };

        if len < 0 {
            return None;
        }

        // Now get the actual string
        let mut buf: Vec<c_char> = vec![0; len as usize + 1];
        unsafe {
            ffi::wxd_Translations_GetTranslatedPluralString(
                self.ptr,
                c_singular.as_ptr(),
                c_plural.as_ptr(),
                n,
                c_domain.as_ptr(),
                buf.as_mut_ptr(),
                buf.len(),
            )
        };

        Some(unsafe { CStr::from_ptr(buf.as_ptr()).to_string_lossy().to_string() })
    }

    /// Get a header value from a catalog.
    ///
    /// Standard headers include "Content-Type", "Plural-Forms", etc.
    pub fn get_header_value(&self, header: &str, domain: &str) -> Option<String> {
        if self.ptr.is_null() {
            return None;
        }
        let c_header = CString::new(header).ok()?;
        let c_domain = CString::new(domain).ok()?;

        let len = unsafe {
            ffi::wxd_Translations_GetHeaderValue(self.ptr, c_header.as_ptr(), c_domain.as_ptr(), std::ptr::null_mut(), 0)
        };

        if len < 0 {
            return None;
        }

        let mut buf: Vec<c_char> = vec![0; len as usize + 1];
        unsafe {
            ffi::wxd_Translations_GetHeaderValue(self.ptr, c_header.as_ptr(), c_domain.as_ptr(), buf.as_mut_ptr(), buf.len())
        };

        Some(unsafe { CStr::from_ptr(buf.as_ptr()).to_string_lossy().to_string() })
    }

    /// Get the best available translation for a domain.
    ///
    /// Returns the language code of the best available translation,
    /// or `None` if no translations are available.
    pub fn get_best_translation(&self, domain: &str) -> Option<String> {
        self.get_best_translation_with_lang(domain, Language::English)
    }

    /// Get the best available translation for a domain with explicit source language.
    pub fn get_best_translation_with_lang(&self, domain: &str, msg_id_language: Language) -> Option<String> {
        if self.ptr.is_null() {
            return None;
        }
        let c_domain = CString::new(domain).ok()?;

        let len = unsafe {
            ffi::wxd_Translations_GetBestTranslation(
                self.ptr,
                c_domain.as_ptr(),
                msg_id_language.as_i32(),
                std::ptr::null_mut(),
                0,
            )
        };

        if len < 0 {
            return None;
        }

        let mut buf: Vec<c_char> = vec![0; len as usize + 1];
        unsafe {
            ffi::wxd_Translations_GetBestTranslation(
                self.ptr,
                c_domain.as_ptr(),
                msg_id_language.as_i32(),
                buf.as_mut_ptr(),
                buf.len(),
            )
        };

        Some(unsafe { CStr::from_ptr(buf.as_ptr()).to_string_lossy().to_string() })
    }

    /// Get all available translations for a domain.
    ///
    /// Returns a list of language codes for which translations are available.
    pub fn get_available_translations(&self, domain: &str) -> Vec<String> {
        if self.ptr.is_null() {
            return Vec::new();
        }
        let c_domain = match CString::new(domain) {
            Ok(s) => s,
            Err(_) => return Vec::new(),
        };

        // First get the count
        let count =
            unsafe { ffi::wxd_Translations_GetAvailableTranslations(self.ptr, c_domain.as_ptr(), std::ptr::null_mut(), 0, 0) };

        if count <= 0 {
            return Vec::new();
        }

        // Allocate buffers
        let string_buf_len: usize = 32; // Language codes are short
        let mut buffers: Vec<Vec<c_char>> = (0..count).map(|_| vec![0 as c_char; string_buf_len]).collect();
        let mut ptrs: Vec<*mut c_char> = buffers.iter_mut().map(|b| b.as_mut_ptr()).collect();

        unsafe {
            ffi::wxd_Translations_GetAvailableTranslations(
                self.ptr,
                c_domain.as_ptr(),
                ptrs.as_mut_ptr(),
                count as usize,
                string_buf_len,
            )
        };

        // Convert to Strings
        buffers
            .iter()
            .filter_map(|buf| {
                let cstr = unsafe { CStr::from_ptr(buf.as_ptr()) };
                let s = cstr.to_string_lossy().to_string();
                if s.is_empty() { None } else { Some(s) }
            })
            .collect()
    }
}

impl Default for Translations {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for Translations {
    fn drop(&mut self) {
        if self.owned && !self.ptr.is_null() {
            unsafe { ffi::wxd_Translations_Destroy(self.ptr) };
        }
    }
}

/// Add a catalog lookup path prefix.
///
/// This adds a directory to search for translation files (.mo files).
/// The path should contain subdirectories named by language code
/// (e.g., "fr", "de", "es") containing the .mo files.
///
/// # Example
/// ```rust,no_run
/// use wxdragon::translations::add_catalog_lookup_path_prefix;
///
/// // Add ./locale as a search path
/// // Translation files should be in ./locale/fr/LC_MESSAGES/myapp.mo etc.
/// add_catalog_lookup_path_prefix("./locale");
/// ```
pub fn add_catalog_lookup_path_prefix(prefix: &str) {
    let c_prefix = match CString::new(prefix) {
        Ok(s) => s,
        Err(_) => return,
    };
    unsafe { ffi::wxd_FileTranslationsLoader_AddCatalogLookupPathPrefix(c_prefix.as_ptr()) };
}

/// Translate a string using the global translations instance.
///
/// Returns the translated string if translations are set up and
/// a translation exists, otherwise returns the original string.
///
/// # Example
/// ```rust,no_run
/// use wxdragon::translations::translate;
///
/// let hello = translate("Hello");
/// ```
pub fn translate(s: &str) -> String {
    if let Some(translations) = Translations::get()
        && let Some(translated) = translations.get_string(s, "")
    {
        return translated;
    }
    s.to_string()
}

/// Translate a plural string using the global translations instance.
///
/// Returns the appropriate plural form based on the count `n`.
/// If translations aren't available, returns `singular` if n == 1,
/// otherwise returns `plural`.
///
/// # Example
/// ```rust,no_run
/// use wxdragon::translations::translate_plural;
///
/// let file_count = 5;
/// let msg = translate_plural("1 item selected", "%d items selected", file_count);
/// ```
pub fn translate_plural(singular: &str, plural: &str, n: u32) -> String {
    if let Some(translations) = Translations::get()
        && let Some(translated) = translations.get_plural_string(singular, plural, n, "")
    {
        return translated;
    }
    if n == 1 { singular.to_string() } else { plural.to_string() }
}

/// Information about a language.
///
/// Wraps `wxLanguageInfo`. This structure provides details about a language
/// supported by wxWidgets, such as its description and canonical name.
#[derive(Clone, Copy)]
pub struct LanguageInfo {
    ptr: *const ffi::wxd_LanguageInfo_t,
}

impl LanguageInfo {
    /// Get the user-readable description of the language (e.g. "French").
    pub fn description(&self) -> String {
        if self.ptr.is_null() {
            return String::new();
        }
        let len = unsafe { ffi::wxd_LanguageInfo_GetDescription(self.ptr, std::ptr::null_mut(), 0) };
        if len < 0 {
            return String::new();
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_LanguageInfo_GetDescription(self.ptr, buf.as_mut_ptr() as *mut _, buf.len());
        }
        // Remove null terminator
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        String::from_utf8_lossy(&buf).to_string()
    }

    /// Get the canonical name of the language (e.g. "fr_FR").
    pub fn canonical_name(&self) -> String {
        if self.ptr.is_null() {
            return String::new();
        }
        let len = unsafe { ffi::wxd_LanguageInfo_GetCanonicalName(self.ptr, std::ptr::null_mut(), 0) };
        if len < 0 {
            return String::new();
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_LanguageInfo_GetCanonicalName(self.ptr, buf.as_mut_ptr() as *mut _, buf.len());
        }
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        String::from_utf8_lossy(&buf).to_string()
    }

    /// Get the native description of the language (e.g. "Français").
    pub fn native_description(&self) -> String {
        if self.ptr.is_null() {
            return String::new();
        }
        let len = unsafe { ffi::wxd_LanguageInfo_GetDescriptionNative(self.ptr, std::ptr::null_mut(), 0) };
        if len < 0 {
            return String::new();
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_LanguageInfo_GetDescriptionNative(self.ptr, buf.as_mut_ptr() as *mut _, buf.len());
        }
        // Remove null terminator
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        String::from_utf8_lossy(&buf).to_string()
    }
}

/// Locale-related helper functions.
///
/// Provides access to wxWidgets' locale database to look up language names and information.
pub struct Locale;

impl Locale {
    /// Get the English name of the given language (e.g. "French").
    pub fn get_language_name(lang: Language) -> Option<String> {
        let len = unsafe { ffi::wxd_Locale_GetLanguageName(lang.as_i32(), std::ptr::null_mut(), 0) };
        if len < 0 {
            return None;
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_Locale_GetLanguageName(lang.as_i32(), buf.as_mut_ptr() as *mut _, buf.len());
        }
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        Some(String::from_utf8_lossy(&buf).to_string())
    }

    /// Get the canonical name of the given language (e.g. "fr_FR").
    pub fn get_language_canonical_name(lang: Language) -> Option<String> {
        let len = unsafe { ffi::wxd_Locale_GetLanguageCanonicalName(lang.as_i32(), std::ptr::null_mut(), 0) };
        if len < 0 {
            return None;
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_Locale_GetLanguageCanonicalName(lang.as_i32(), buf.as_mut_ptr() as *mut _, buf.len());
        }
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        Some(String::from_utf8_lossy(&buf).to_string())
    }

    /// Find language info from a locale string (e.g. "fr", "en_US").
    pub fn find_language_info(locale: &str) -> Option<LanguageInfo> {
        let c_locale = CString::new(locale).ok()?;
        let ptr = unsafe { ffi::wxd_Locale_FindLanguageInfo(c_locale.as_ptr()) };
        if ptr.is_null() { None } else { Some(LanguageInfo { ptr }) }
    }

    /// Get the language info for the given language id.
    pub fn get_language_info(lang: Language) -> Option<LanguageInfo> {
        let ptr = unsafe { ffi::wxd_Locale_GetLanguageInfo(lang.as_i32()) };
        if ptr.is_null() { None } else { Some(LanguageInfo { ptr }) }
    }

    /// Get the system default language.
    pub fn get_system_language() -> Language {
        let lang_id = unsafe { ffi::wxd_Locale_GetSystemLanguage() };
        Language::from_i32(lang_id).unwrap_or(Language::Unknown)
    }
}

/// Represents a UI locale.
///
/// Wraps `wxUILocale`. This class provides access to the current UI locale settings.
pub struct UILocale {
    ptr: *mut ffi::wxd_UILocale_t,
}

impl UILocale {
    /// Get the object corresponding to the current locale.
    pub fn get_current() -> Self {
        let ptr = unsafe { ffi::wxd_UILocale_GetCurrent() };
        Self { ptr }
    }

    /// Get the locale name.
    pub fn get_name(&self) -> String {
        if self.ptr.is_null() {
            return String::new();
        }
        let len = unsafe { ffi::wxd_UILocale_GetName(self.ptr, std::ptr::null_mut(), 0) };
        if len < 0 {
            return String::new();
        }
        let mut buf = vec![0u8; len as usize + 1];
        unsafe {
            ffi::wxd_UILocale_GetName(self.ptr, buf.as_mut_ptr() as *mut _, buf.len());
        }
        // Remove null terminator
        if let Some(last) = buf.last()
            && *last == 0
        {
            buf.pop();
        }
        String::from_utf8_lossy(&buf).to_string()
    }

    /// Get the language info for this locale.
    pub fn get_info(&self) -> Option<LanguageInfo> {
        if self.ptr.is_null() {
            return None;
        }
        let lang_id = unsafe { ffi::wxd_UILocale_GetLanguage(self.ptr) };
        Locale::get_language_info(Language::from_i32(lang_id).unwrap_or(Language::Unknown))
    }
}

impl Drop for UILocale {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::wxd_UILocale_Destroy(self.ptr) };
        }
    }
}