forked from Araq/wxnim
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I convert WxString to string? #23
Comments
Here's an approach: #24 (comment) |
@AllenDang here's a solution that supports unicode. import "../../wxnim/wx", "../../wxnim/genui"
{.experimental.}
var
textCtrl: ptr WxTextCtrl
func stringFromWxString(s: WxString): string =
#[
Context about wxString handling:
- wxString class reference, particularly .ToUTF8() method
https://docs.wxwidgets.org/3.0/classwx_string.html
- wxScopedCharTypeBuffer<T> reference, particularly .data() method
https://docs.wxwidgets.org/3.0/classwx_scoped_char_type_buffer.html#a7cd7ba0ab32e9f63779f602c2bdfd9b8
Nim emit pragma for embedding C++ code:
- https://nim-lang.github.io/Nim/manual.html#implementation-specific-pragmas-emit-pragma
C++11 limits on implicit conversion from const char* to char*
- https://stackoverflow.com/questions/48554625/vs-2017-doesnt-implicitly-convert-const-char-to-char/48554786
- https://en.cppreference.com/w/cpp/language/const_cast
]#
var r: cstring
{.emit: """
`r` = const_cast<char*>((const char*)s.ToUTF8().data());
""".}
result = $r #convert cstring to string
proc buttonClicked(e: var WxCommandEvent) =
var
theText = stringFromWxString(textCtrl.getValue())
echo theText
genui:
mainFrame % Frame(title = "Hello World"):
Panel | BoxSizer(orient = wxHorizontal):
StaticBox(label = "Basic controls") | StaticBoxSizer(orient = wxVertical):
textCtrl % TextCtrl
theButton % Button -> (wxEVT_BUTTON, buttonClicked): "Click me"
mainFrame.show()
runMainLoop() |
Looks good to me, you can also put the emit on a single line if you want: {.emit: "`r` = const_cast<char*>((const char*)s.ToUTF8().data());".} Could you create a PR out of this? |
… On Sun, Jan 31, 2021 at 3:50 AM PMunch ***@***.***> wrote:
Looks good to me, you can also put the emit on a single line if you want:
{.emit: "`r` = const_cast<char*>((const char*)s.ToUTF8().data());".}
Could you create a PR out of this?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#23 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAACGKBZDI76PKVLCVI7OUTS4U7X3ANCNFSM4WFBU42Q>
.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to get the value of a TextCtrl, by using
But I got an error states
error: cannot initialize a variable of type 'NCSTRING' (aka 'char *') with an rvalue of type 'const char *'
.I'm new to nim, this maybe a very trivial issue, but I cannot find any answer from google.
Here is the minimal code.
The text was updated successfully, but these errors were encountered: