vb6runtime 0.2.0

VB6 runtime library - value system, type conversions, and standard library implementations
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
//! # `IMEStatus` Function
//!
//! Returns an `Integer` indicating the current `Input Method Editor` (`IME`) mode of Microsoft Windows.
//!
//! ## Syntax
//!
//! ```vb
//! IMEStatus()
//! ```
//!
//! ## Parameters
//!
//! None
//!
//! ## Return Value
//!
//! Returns an `Integer` representing the current `IME` mode:
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | vbIMENoOp | 0 | No `IME` installed or `IME` is disabled |
//! | vbIMEOn | 1 | `IME` is on (active) |
//! | vbIMEOff | 2 | `IME` is off (inactive) |
//! | vbIMEDisable | 3 | `IME` is disabled |
//! | vbIMEHiragana | 4 | Double-byte Hiragana mode |
//! | vbIMEKatakanHalf | 5 | Single-byte Katakana mode |
//! | vbIMEKatakanaFull | 6 | Double-byte Katakana mode |
//! | vbIMEAlphaHalf | 7 | Single-byte Alphanumeric mode |
//! | vbIMEAlphaFull | 8 | Double-byte Alphanumeric mode |
//! | vbIMEHangulHalf | 9 | Single-byte Hangul mode |
//! | vbIMEHangulFull | 10 | Double-byte Hangul mode |
//!
//! ## Remarks
//!
//! The `IMEStatus` function provides information about the `Input Method Editor`:
//!
//! - Returns the current state of the `IME` for the active window
//! - `IME` is used primarily for Asian language input (Japanese, Chinese, Korean)
//! - Only meaningful on systems with `IME` support installed
//! - Returns `vbIMENoOp` (0) if no `IME` is installed or available
//! - The return value reflects the `IME` state at the moment the function is called
//! - Can be used to detect if the user is in native language input mode
//! - Useful for applications that need to work with multibyte character sets
//! - The actual modes available depend on the installed `IME` and Windows version
//!
//! ## Typical Uses
//!
//! 1. **IME Detection**: Check if an `IME` is installed and active
//! 2. **Input Mode Validation**: Verify the user is in the correct input mode
//! 3. **Localization**: Adjust application behavior based on `IME` state
//! 4. **Data Entry**: Ensure proper input mode for specific fields
//! 5. **User Guidance**: Provide instructions based on current `IME` mode
//! 6. **Form Validation**: Check input mode before processing data
//!
//! ## Basic Usage Examples
//!
//! ```vb
//! ' Example 1: Check if IME is available
//! Sub CheckIME()
//!     If IMEStatus() = vbIMENoOp Then
//!         MsgBox "No IME is installed"
//!     Else
//!         MsgBox "IME is available"
//!     End If
//! End Sub
//!
//! ' Example 2: Check if IME is active
//! Sub CheckIMEActive()
//!     If IMEStatus() = vbIMEOn Then
//!         MsgBox "IME is currently on"
//!     Else
//!         MsgBox "IME is currently off"
//!     End If
//! End Sub
//!
//! ' Example 3: Display current IME mode
//! Sub DisplayIMEMode()
//!     Dim mode As Integer
//!     mode = IMEStatus()
//!     Debug.Print "Current IME mode: " & mode
//! End Sub
//!
//! ' Example 4: Detect Japanese input mode
//! Function IsJapaneseInput() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     IsJapaneseInput = (status = vbIMEHiragana Or _
//!                        status = vbIMEKatakanHalf Or _
//!                        status = vbIMEKatakanaFull)
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ```vb
//! ' Pattern 1: Check if IME is enabled
//! Function IsIMEEnabled() As Boolean
//!     IsIMEEnabled = (IMEStatus() <> vbIMENoOp And IMEStatus() <> vbIMEDisable)
//! End Function
//!
//! ' Pattern 2: Determine if using double-byte characters
//! Function IsDoubleByte() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     
//!     Select Case status
//!         Case vbIMEHiragana, vbIMEKatakanaFull, vbIMEAlphaFull, vbIMEHangulFull
//!             IsDoubleByte = True
//!         Case Else
//!             IsDoubleByte = False
//!     End Select
//! End Function
//!
//! ' Pattern 3: Get IME mode description
//! Function GetIMEModeDescription() As String
//!     Select Case IMEStatus()
//!         Case vbIMENoOp
//!             GetIMEModeDescription = "No IME"
//!         Case vbIMEOn
//!             GetIMEModeDescription = "IME On"
//!         Case vbIMEOff
//!             GetIMEModeDescription = "IME Off"
//!         Case vbIMEDisable
//!             GetIMEModeDescription = "IME Disabled"
//!         Case vbIMEHiragana
//!             GetIMEModeDescription = "Hiragana"
//!         Case vbIMEKatakanHalf
//!             GetIMEModeDescription = "Half-width Katakana"
//!         Case vbIMEKatakanaFull
//!             GetIMEModeDescription = "Full-width Katakana"
//!         Case vbIMEAlphaHalf
//!             GetIMEModeDescription = "Half-width Alphanumeric"
//!         Case vbIMEAlphaFull
//!             GetIMEModeDescription = "Full-width Alphanumeric"
//!         Case vbIMEHangulHalf
//!             GetIMEModeDescription = "Half-width Hangul"
//!         Case vbIMEHangulFull
//!             GetIMEModeDescription = "Full-width Hangul"
//!         Case Else
//!             GetIMEModeDescription = "Unknown mode"
//!     End Select
//! End Function
//!
//! ' Pattern 4: Validate input mode for specific field
//! Function ValidateInputMode(expectedMode As Integer) As Boolean
//!     ValidateInputMode = (IMEStatus() = expectedMode)
//! End Function
//!
//! ' Pattern 5: Detect Asian language input
//! Function IsAsianLanguageInput() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     
//!     ' Check for Japanese or Korean modes
//!     IsAsianLanguageInput = (status >= vbIMEHiragana And status <= vbIMEHangulFull)
//! End Function
//!
//! ' Pattern 6: Check for alphanumeric mode
//! Function IsAlphanumericMode() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     IsAlphanumericMode = (status = vbIMEAlphaHalf Or status = vbIMEAlphaFull)
//! End Function
//!
//! ' Pattern 7: Determine input width
//! Function GetInputWidth() As String
//!     Select Case IMEStatus()
//!         Case vbIMEKatakanHalf, vbIMEAlphaHalf, vbIMEHangulHalf
//!             GetInputWidth = "Half-width"
//!         Case vbIMEHiragana, vbIMEKatakanaFull, vbIMEAlphaFull, vbIMEHangulFull
//!             GetInputWidth = "Full-width"
//!         Case Else
//!             GetInputWidth = "N/A"
//!     End Select
//! End Function
//!
//! ' Pattern 8: Check if IME is in active input mode
//! Function IsActiveInputMode() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     ' Active modes are those actively converting input
//!     IsActiveInputMode = (status >= vbIMEHiragana And status <= vbIMEHangulFull)
//! End Function
//!
//! ' Pattern 9: Detect Katakana mode
//! Function IsKatakanaMode() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     IsKatakanaMode = (status = vbIMEKatakanHalf Or status = vbIMEKatakanaFull)
//! End Function
//!
//! ' Pattern 10: Check for Korean input
//! Function IsKoreanInput() As Boolean
//!     Dim status As Integer
//!     status = IMEStatus()
//!     IsKoreanInput = (status = vbIMEHangulHalf Or status = vbIMEHangulFull)
//! End Function
//! ```
//!
//! ## Advanced Usage Examples
//!
//! ```vb
//! ' Example 1: IME mode monitor
//! Public Class IMEMonitor
//!     Private m_lastStatus As Integer
//!     
//!     Public Sub Initialize()
//!         m_lastStatus = IMEStatus()
//!     End Sub
//!     
//!     Public Function HasChanged() As Boolean
//!         Dim currentStatus As Integer
//!         currentStatus = IMEStatus()
//!         
//!         If currentStatus <> m_lastStatus Then
//!             m_lastStatus = currentStatus
//!             HasChanged = True
//!         Else
//!             HasChanged = False
//!         End If
//!     End Function
//!     
//!     Public Function GetCurrentMode() As String
//!         GetCurrentMode = GetIMEModeDescription()
//!     End Function
//! End Class
//!
//! ' Example 2: TextBox IME validator
//! Private Sub txtName_GotFocus()
//!     ' For name field, we want half-width alphanumeric or Katakana
//!     If IMEStatus() <> vbIMEAlphaHalf And _
//!        IMEStatus() <> vbIMEKatakanHalf And _
//!        IMEStatus() <> vbIMEOff Then
//!         MsgBox "Please switch to half-width alphanumeric or Katakana mode", _
//!                vbInformation, "Input Mode"
//!     End If
//! End Sub
//!
//! ' Example 3: Language-aware input handler
//! Function ProcessInput(userInput As String) As String
//!     Dim imeMode As Integer
//!     imeMode = IMEStatus()
//!     
//!     Select Case imeMode
//!         Case vbIMEHiragana, vbIMEKatakanaFull, vbIMEKatakanHalf
//!             ' Japanese input - special processing
//!             ProcessInput = ProcessJapaneseText(userInput)
//!             
//!         Case vbIMEHangulHalf, vbIMEHangulFull
//!             ' Korean input - special processing
//!             ProcessInput = ProcessKoreanText(userInput)
//!             
//!         Case Else
//!             ' Standard processing
//!             ProcessInput = userInput
//!     End Select
//! End Function
//!
//! ' Example 4: Form-wide IME status display
//! Private Sub tmrIMEStatus_Timer()
//!     ' Update status bar with current IME mode
//!     Dim status As Integer
//!     status = IMEStatus()
//!     
//!     StatusBar1.Panels(1).Text = "IME: " & GetIMEModeDescription()
//!     
//!     ' Change indicator color based on mode
//!     If status = vbIMENoOp Or status = vbIMEOff Then
//!         StatusBar1.Panels(1).Picture = LoadPicture(App.Path & "\imeoff.ico")
//!     Else
//!         StatusBar1.Panels(1).Picture = LoadPicture(App.Path & "\imeon.ico")
//!     End If
//! End Sub
//!
//! ' Example 5: Data validation with IME awareness
//! Function ValidateNameField(fieldValue As String) As Boolean
//!     Dim imeMode As Integer
//!     imeMode = IMEStatus()
//!     
//!     ' For Japanese systems, allow Katakana or alphanumeric
//!     If imeMode = vbIMEHiragana Then
//!         MsgBox "Please use Katakana or alphanumeric for names", vbExclamation
//!         ValidateNameField = False
//!         Exit Function
//!     End If
//!     
//!     ' Additional validation
//!     If Len(Trim$(fieldValue)) = 0 Then
//!         ValidateNameField = False
//!     Else
//!         ValidateNameField = True
//!     End If
//! End Function
//!
//! ' Example 6: IME-aware search
//! Function PerformSearch(searchTerm As String) As Collection
//!     Dim results As New Collection
//!     Dim searchMode As String
//!     
//!     ' Determine search strategy based on IME mode
//!     Select Case IMEStatus()
//!         Case vbIMEHiragana, vbIMEKatakanaFull, vbIMEKatakanHalf
//!             searchMode = "Japanese"
//!             ' Use Japanese-specific search algorithm
//!             Set results = SearchJapanese(searchTerm)
//!             
//!         Case vbIMEHangulHalf, vbIMEHangulFull
//!             searchMode = "Korean"
//!             ' Use Korean-specific search algorithm
//!             Set results = SearchKorean(searchTerm)
//!             
//!         Case Else
//!             searchMode = "Standard"
//!             ' Use standard search
//!             Set results = SearchStandard(searchTerm)
//!     End Select
//!     
//!     Set PerformSearch = results
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! The `IMEStatus` function rarely raises errors:
//!
//! - Returns `vbIMENoOp` (0) on systems without `IME` support
//! - Does not raise errors if `IME` is not available
//! - Always returns a valid `Integer` value
//! - No error handling typically required
//!
//! ```vb
//! ' Safe to call without error handling
//! Dim status As Integer
//! status = IMEStatus()
//! ```
//!
//! ## Performance Considerations
//!
//! - **Fast Operation**: `IMEStatus` is a very fast system query
//! - **No Overhead**: Minimal performance impact even when called frequently
//! - **Real-time Monitoring**: Safe to call in timer events for status updates
//! - **No Caching Needed**: The function is efficient enough to call directly
//!
//! ## Best Practices
//!
//! 1. **System Compatibility**: Always check for `vbIMENoOp` before assuming `IME` functionality
//! 2. **User Guidance**: Provide clear instructions when specific `IME` modes are required
//! 3. **Non-intrusive**: Don't force `IME` mode changes; suggest them to the user
//! 4. **Status Display**: Show current `IME` mode in status bars for user awareness
//! 5. **Localization**: Use `IMEStatus` to adapt UI for different language inputs
//! 6. **Testing**: Test on both `IME`-enabled and non-`IME` systems
//! 7. **Documentation**: Document `IME` mode requirements for specific fields
//!
//! ## Platform and Version Notes
//!
//! - Available in all VB6 versions
//! - Returns meaningful values only on Windows with `IME` support
//! - `IME` modes depend on installed Windows language packs
//! - Japanese Windows: Hiragana, Katakana modes available
//! - Korean Windows: Hangul modes available
//! - Chinese Windows: May have different mode constants
//! - Western Windows without `IME`: Typically returns `vbIMENoOp`
//!
//! ## Limitations
//!
//! - Only detects `IME` state, cannot change it (use `SendKeys` or Windows API for that)
//! - Return values depend on installed `IME` and language packs
//! - Some `IME` modes may not be available on all systems
//! - Does not detect which specific `IME` software is being used
//! - Limited to Windows `IME` implementation
//! - Cannot distinguish between different Chinese `IME` modes
//! - Return value reflects system state at call time (may change immediately after)
//!
//! ## Related Functions and Properties
//!
//! - `IMEMode` property: Sets/gets the `IME` mode for controls
//! - `SendKeys`: Can be used to change `IME` mode via keyboard shortcuts
//! - Windows API functions for `IME` control (`ImmGetContext`, etc.)
//!
//! ## `IME` Mode Constants Reference
//!
//! ```vb
//! Public Const vbIMENoOp = 0         ' No IME
//! Public Const vbIMEOn = 1           ' IME On
//! Public Const vbIMEOff = 2          ' IME Off  
//! Public Const vbIMEDisable = 3      ' IME Disabled
//! Public Const vbIMEHiragana = 4     ' Hiragana
//! Public Const vbIMEKatakanHalf = 5  ' Half Katakana
//! Public Const vbIMEKatakanaFull = 6 ' Full Katakana
//! Public Const vbIMEAlphaHalf = 7    ' Half Alphanumeric
//! Public Const vbIMEAlphaFull = 8    ' Full Alphanumeric
//! Public Const vbIMEHangulHalf = 9   ' Half Hangul
//! Public Const vbIMEHangulFull = 10  ' Full Hangul
//! ```

use crate::error::VBResult;
use crate::value::VBVariant;

/// Returns an `Integer` indicating the current IME mode.
///
/// Always returns `vbIMENoOp` (0). Full IME detection is not yet
/// implemented — platform-specific support should be added later.
pub fn imestatus() -> VBResult<VBVariant> {
    // TODO: detect the real IME mode via platform APIs (e.g. Windows
    // ImmGetContext / ImmGetOpenStatus) and return the appropriate
    // vbIME* constant.  For now, return vbIMENoOp (0) which is the
    // safe default on systems without an IME.
    Ok(VBVariant::from_integer(0))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn returns_zero() {
        assert_eq!(imestatus().unwrap(), VBVariant::from_integer(0));
    }
}