Skip to content
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

Open
AllenDang opened this issue Jan 16, 2021 · 4 comments
Open

How can I convert WxString to string? #23

AllenDang opened this issue Jan 16, 2021 · 4 comments

Comments

@AllenDang
Copy link

I'm trying to get the value of a TextCtrl, by using

var val = textCtrl.getValue()
var str = val.cStr().asCString()
echo str

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.

import "wxnim/wx", "wxnim/genui"

{.experimental.}

var
  textCtrl: ptr WxTextCtrl
  button: ptr WxButton

proc buttonClicked(e: var WxCommandEvent) {.cdecl.} =
  var val = textCtrl.getValue()
  var str = val.cStr().asCString()
  echo str

genui:
  mainFrame % Frame(title = "Hello World"):
    Panel | BoxSizer(orient = wxHorizontal):
      StaticBox(label = "Basic controls") | StaticBoxSizer(orient = wxVertical):
        textCtrl % TextCtrl
        button % Button -> (wxEVTBUTTON, buttonClicked): "Click me"

mainFrame.show()
runMainLoop()
@joubertnel
Copy link

Here's an approach: #24 (comment)

@joubertnel
Copy link

joubertnel commented Jan 30, 2021

@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()

@PMunch
Copy link
Owner

PMunch commented Jan 31, 2021

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?

@joubertnel
Copy link

joubertnel commented Jan 31, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants