dontpanic92 / wxGo

Golang wxWidgets Wrapper
Other
404 stars 51 forks source link

How to apply colours to TextAttr? #69

Closed KenJean closed 5 years ago

KenJean commented 5 years ago

Trying to figure out how to do colour highlighting in TextCtrl.

For example:

text = wx.NewTextCtrl(myFrame, wx.ID_ANY, "Test\n", wxDefaultPosition, wx.DefaultSize, wx.TE_MULTILINE)

text.SetDefaultStyle(wx.TextAttr(wx.RED))
text.AppendText("Red text\n")
text.SetDefaultStyle(wx.TextAttr(wx.NullColour, wx.LIGHT_GREY))
text.AppendText("Red on grey text\n")
text.SetDefaultStyle(wx.TextAttr(wx.BLUE))
text.AppendText("Blue on grey text\n")

wx.RED, wx.LIGHT_GREY and wx.BLUE do not exist in wxGo.

I understand the SetTextColour component does not take a wxColour argument and must take a SwigIsColour argument instead. How do I construct a SwigIsColour object ?? Everything I've found so far converts TO a wxColour object and not FROM a wxColour object.

Thanks in advance.

⇒ KJ

dontpanic92 commented 5 years ago

Please use wx.GetBLACK()/wx.GetLIGHT_GREY() to get predefined colors.

To create a color, you can use wx.NewColour

KenJean commented 5 years ago

Thanks so much for the quick response. I tried all sorts of other combinations (wx.ToColour, etc.) but hadn't come across what you've just taught me. Following your advice, I tried the following:

style := textctrl.GetDefaultStyle()
style.SetTextColour(wx.NewColour(wx.GetRED()))
textctrl.SetDefaultStyle(style)
textctrl.AppendText("Red text\n")

This code compiles and runs (yay !!!) but I do NOT get red text. I am on Arch Linux, by the way, in case that matters.

I've been combing through the source code without making much progress. I can sort it out when the wrapper functions follow the original wxWidgets/wxPython API closely, but once I hit all the Swig stuff, I get hopelessly lost trying to sort out what I'm supposed to do.

Thanks again. Any further ideas what I could be doing wrong?

⇒ KJ

dontpanic92 commented 5 years ago

Please have a try:

    textCtrl.SetDefaultStyle(wx.NewTextAttrT(wx.GetRED()))
    textCtrl.AppendText("Red text\n")
    textCtrl.SetDefaultStyle(wx.NewTextAttrT(wx.NullColour, wx.GetLIGHT_GREY()))
    textCtrl.AppendText("Red on grey text\n")
    textCtrl.SetDefaultStyle(wx.NewTextAttrT(wx.GetBLUE()))
    textCtrl.AppendText("Blue on grey text\n")
KenJean commented 5 years ago

Yes !!!!!!! That works !!! I think I understand what I have to do now: I need to create a new TextAttr object whenever I need to change text attributes, rather than attempt to change an existing one.

Many, many thanks for teaching me how to do this. I shall mark this issue solved and closed