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 »

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: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxTextCtrl password entry with show&hide password option

Post by doublemax »

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 »

I see. I will try abstracting this thing away.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTextCtrl password entry with show&hide password option

Post by catalin »

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: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxTextCtrl password entry with show&hide password option

Post by xaviou »

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 : X@v'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 »

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 »

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: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Re: wxTextCtrl password entry with show&hide password option

Post by xaviou »

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 : X@v'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 »

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
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxTextCtrl password entry with show&hide password option

Post by Widgets »

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/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxTextCtrl password entry with show&hide password option

Post by catalin »

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
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: wxTextCtrl password entry with show&hide password option

Post by Widgets »

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/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Post Reply