wxTextCtrl password entry with show&hide password option

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
specing
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Mar 08, 2018 11:44 pm

wxTextCtrl password entry with show&hide password option

Post by specing » Fri Mar 09, 2018 12:27 am

Is there any shortcut or should we create two wxTextCtrls, one with wxTE_PASSWORD, one without and switch between them when the show/hide password toggle is activated?

Documentation says wxTE_PASSWORD style can only be dynamically changed with wxGTK back-end.

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxTextCtrl password entry with show&hide password option

Post by doublemax » Fri Mar 09, 2018 11:17 am

I can't think of an easier way.

The alternative would be to catch all key presses, store the characters only internally and then replace it with an * for the actual visible content. But it would become quite complicated with handling cursor movement, selection, backspace, delete etc.
Use the source, Luke!

specing
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Mar 08, 2018 11:44 pm

Re: wxTextCtrl password entry with show&hide password option

Post by specing » Fri Mar 09, 2018 11:20 pm

I see. I will try abstracting this thing away.

catalin
Moderator
Moderator
Posts: 1525
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTextCtrl password entry with show&hide password option

Post by catalin » Sat Mar 10, 2018 7:05 am

It may be easier to recreate a text ctrl at every toggle event, having the same size, position, value, parent [, position in sizer], and maybe also use Freeze()/Thaw() on its parent when switching the 2 controls.

User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 429
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxTextCtrl password entry with show&hide password option

Post by xaviou » Sat Mar 10, 2018 5:41 pm

Hi
catalin wrote:It may be easier to recreate a text ctrl at every toggle event, having the same size, position, value, parent [, position in sizer], and maybe also use Freeze()/Thaw() on its parent when switching the 2 controls.
If you need an example, just have a look at this : this is exactly what I am doing.

Regards
My wxWidgets stuff web page : [email protected]'s wxStuff

specing
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Mar 08, 2018 11:44 pm

Re: wxTextCtrl password entry with show&hide password option

Post by specing » Sat Mar 10, 2018 9:26 pm

xaviou wrote:....this ...
Under which license can we look at your work?

specing
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Mar 08, 2018 11:44 pm

Re: wxTextCtrl password entry with show&hide password option

Post by specing » Sat Mar 10, 2018 9:34 pm

catalin wrote:It may be easier to recreate a text ctrl at every toggle event, having the same size, position, value, parent [, position in sizer], and maybe also use Freeze()/Thaw() on its parent when switching the 2 controls.
I don't know how to ensure that they'd have the same position in the sizer. But I could put two of them in and Show()/Hide() them when needed.

User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 429
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxTextCtrl password entry with show&hide password option

Post by xaviou » Sat Mar 10, 2018 10:25 pm

specing wrote:Under which license can we look at your work?
There is no particular license : you can do what you want with it.

It's here to help...
specing wrote:
catalin wrote:It may be easier to recreate a text ctrl at every toggle event, having the same size, position, value, parent [, position in sizer], and maybe also use Freeze()/Thaw() on its parent when switching the 2 controls.
I don't know how to ensure that they'd have the same position in the sizer. But I could put two of them in and Show()/Hide() them when needed.
It can also work : just don't forget to call "layout" on the containing sizer after the "show/hide" ones

Regards
Xav'
My wxWidgets stuff web page : [email protected]'s wxStuff

specing
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Mar 08, 2018 11:44 pm

Re: wxTextCtrl password entry with show&hide password option

Post by specing » Sun Mar 11, 2018 1:42 pm

xaviou wrote:There is no particular license : you can do what you want with it.
I'm pretty sure "no license" means all rights reserved under current copyright systems. If you want to dedicate those samples to the public domain, then explicitly pick such a license, e.g. Creative Commons Zero and follow their licensing practice (e.g. /LICENSE file and proper text in all headers). I'm also sure that using some person's "you can do what you want with it." quote on some forum where anyone can register with whatever name and claim they are the author of that software will not fly in a courtroom.

Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 349
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxTextCtrl password entry with show&hide password option

Post by Widgets » Thu Mar 21, 2019 7:36 pm

FWIW, I find that Xaviou's solution may work only if the password text control is the only or last control in a sizer, and even with moving it to the last box, I had no luck with in wxGridSizer.
The problem seems to be that, while a sizer has functions to add a control at a given index, I have not found a way to check at which index the current control was placed to that I could try to 'insert' it at the desired 'index', rather than append it with pSzr->add which says it will append the recreated text control
As it is adding the control seems to place it at some arbitrary point within the grid sizer - at least I can see no logic, because it does not even become the last control in the sizer :(
Even getting both size & position of the old window and using it when creating the new one does not make a difference.
It does seem that, for Windows at least, there is an alternative - will check it out.
viewtopic.php?t=15093
Environment: Win 10 64-bit & Ubuntu 16.04
MSVC Express 2010, 2015, 2017
wxWidgets 3.1
Fight SPAM - report it with wxSR

catalin
Moderator
Moderator
Posts: 1525
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTextCtrl password entry with show&hide password option

Post by catalin » Thu Mar 21, 2019 7:56 pm

Widgets wrote:
Thu Mar 21, 2019 7:36 pm
I have not found a way to check at which index the current control was placed to that I could try to 'insert' it at the desired 'index'
FWIW you should be able to achieve this without needing the index, by storing the wxSizerItem* returned by wxSizer::Add(...) and using its AssignWindow() when appropriate.

Widgets
Super wx Problem Solver
Super wx Problem Solver
Posts: 349
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxTextCtrl password entry with show&hide password option

Post by Widgets » Thu Mar 21, 2019 8:56 pm

the 'solution' may well be 'obvious' to you, but it is not to me and the example may well work under some (unspecified) conditions, but not under mine :?
Environment: Win 10 64-bit & Ubuntu 16.04
MSVC Express 2010, 2015, 2017
wxWidgets 3.1
Fight SPAM - report it with wxSR

Post Reply