Page 1 of 2

Is it possible to select text natively upon setting focus?

Posted: Wed Aug 31, 2011 6:14 pm
by JustinJack
Is there a Purebasic (platform independent) method available to select all the text in a StringGadget when it gains focus other than this?

Code: Select all

Select EventType()
  Case #PB_EventType_Focus
    SendMessage_(hAnyEditControl, #EM_SETSEL, 0, -1)  ;    <------------
EndSelect

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 6:21 pm
by Zach
edit: I misunderstood, sorry.

I don't know if there is, but best you could do for now is, if you know (or find out) which API commands you need to throw for which OS, you could use a test, to select the appropriate API to send, based on what OS the program is running on.

Most people use compiler directives. So when you compile the Program on Windows, it will include the Win32API, when it is compiled on Linux, it will use the GTK/whatever API, and on Mac, it will use whatever API is used on that platform. The other API commands will not be compiled into the program, so there is no wasted space/code, etc.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 6:28 pm
by Derren
The question is why?
If there's a sample text that you want to overwrite just set the GadgetText to "" upon focus.
If it's a normal input people might want to edit only parts of the stringGadget so your "select all" is only annyoing.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 6:37 pm
by JustinJack
Dear Derren:
The question is not why. If it was, I would have put the topic in this request for help as:
"Why should It be possible to select text natively upon setting focus?"
In this case, it is not annoying, in other cases it may be.

anyways, Ok, Zach. I was actually afraid of that. I just wondered if anybody knew and tricks. I though maybe using a GetGadgetText() there might do something sneaky I didn't know it did. I'm not familiar with ANY mac API's, and I'm about to have to get one and start messing with Obj-C, but the application I'm working on now I'd like to be in PB and platform inspecific. It seems like such an easy thing?! Crazy.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 8:36 pm
by Zach
My original reply was actually about using GetGadgetText(#Gadget), but there are several unknowns with this solution:

A) I Don't know what your intent is with selecting all of the text.. a copy/paste function? Or to delete it? Further, GetGadgetText will not highlight the text, but it would allow you to put the text into a variable, before clearing the StringGadget. This is how I am using it currently for my Text RPG GUI (user enters command, Hotkey setup for pressing Enter key, grabs text and stores in variable, clears text box, and will send text to my command parser to be parsed).

However I don't know of any way to have GetGadgetText() simply highlight, as its not really the intended function.

B) I have no idea how that would work in an Event logic loop. I.e does PB internally know you want to perform the GetText on the Gadget which received focus, or do you have to do further testing to identify the GadgetID of the Gadget which just came into Focus, etc.

But overall I don't think GetGadgetText() is the solution here.

I'm pretty sure the only way to do this properly, is to use platform specific API commands.


To be fair, I think that is a simple solution. Sure its a little extra work on your part, right? But let's consider the situation.
You can Google around to get the appropriate API for each platform, and stick some CompilerIF blocks into your code...

OR you can buy a Mac, learn Objective-C, and then write the Mac version of your program in Obj-C ??? Even if you were planning on buying the Mac, and learning Obj-C for other reasons, does it really make sense to rewrite the whole program in Obj-C, if all you need to do is stick a few extra compiler directives into your code? You'd likely need that Mac to compile the Mac version of your program also.. Unless there are good emulators out there (I've never tried emulating a Mac, like people have done with Linux/Windows VirtualPCs)


I think in this case it is more than fair to say PB has provided the features necessary to obtain cross-platform releases. In a lot of cases, what we are talking about is probably what is going on already behind the scenes in Purebasic. All their cross-platform features probably just substitute API calls for the appropriate Platform/GUI framework. The only difference in this case, is that you would be doing it yourself instead of Purebasic, during compile time.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 8:59 pm
by Derren
You see, people get all kinds of strange sounding ideas.
"How can I make my window hexagonal with a elliptic radiant from royal blue to red with alpha 0 and round buttons that make a beeping sound when you hover with the mouse"....
The question is always "why". And they never ask themself this question.
And if somebody else asks most of the time the problem can be solved in a better way which is most if the times easier to implement, too.

I never came across any GUI that necessarily needs the whole stringgadget highlighted.
Since you refuse to answer the question, as to why you'd want such a feature, your only choice is to ask in the linux and mac subforum.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 10:10 pm
by Zach
Sorry, but that is a rather arrogant stance to take.

You are essentially saying "I may know how to do what you want, but I am holding the information hostage until you satisfy my personal curiosity". That is plain wrong. Equally so if there really is no other way, but you choose to insinuate there may be.

It is not for others to judge whether someone is doing something right or wrong in their program, beyond judging whether they would personally use it or not themselves.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 10:51 pm
by MachineCode
Derren wrote:The question is why?
What an attitude! Anyway, the answer is obvious: so that the user has the option of repeating the same text, or typing new type without manually deleting it all first. Just like the PureBasic search/replace dialog box.

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 11:09 pm
by skywalk

Re: Is it possible to select text natively upon setting focu

Posted: Wed Aug 31, 2011 11:58 pm
by Derren
@MachineCode: The same can be achieved without selecting the whole text as I stated above.
That's why I asked the question.
If you guys think you already know everything, fine.

btw.
The gadget in the IDE search dialog is a combobox.

Code: Select all

OpenWindow(0,0,0,300,100,"",#PB_Window_SystemMenu)
ComboBoxGadget(0,0,0,300,20,#PB_ComboBox_Editable)
SetGadgetText(0,"Search And Replace Dialogue Behaviour")
SetActiveGadget(0)


Repeat
Until WaitWindowEvent(20) =#PB_Event_CloseWindow 
Hope that helps.

Re: Is it possible to select text natively upon setting focu

Posted: Thu Sep 01, 2011 2:03 am
by JustinJack
yup, anyways. The field i need this for is the area code field of a phone number. three digits. It's easier to type over three digits than to remove your hand from the number pad to backspace. I usually just either put a SendMessage_(GadgetID(xxx), #EM_SETSEL, 0, -1) Which does the trick in my PB EventLoop, or in my WndProc() after a WM_COMMAND / EN_SETFOCUS.

Zach: No this project isn't specifically for a Mac. I have to get an iPad app up and running so that's what the Obj-C is for.

Re: Is it possible to select text natively upon setting focu

Posted: Thu Sep 01, 2011 12:39 pm
by Derren
Well, either use the ComboBox, or do it as I suggested above.
If you don't like that and feel the need to highlight the text first, there's no way around OS-specific API.

Code: Select all

OpenWindow(0, 0, 0, 300, 100, "test",#PB_Window_SystemMenu)
StringGadget(0, 5, 5, 200, 20, "Phonenumber Area Code e.g. 123")

Repeat
	Select WaitWindowEvent(20)
		Case #PB_Event_CloseWindow
			End
			
		Case #PB_Event_Gadget
			If EventGadget()=0 And EventType()=#PB_EventType_Focus
				SetGadgetText(0, "")
			EndIf 
			
			
	EndSelect
ForEver

Re: Is it possible to select text natively upon setting focu

Posted: Thu Sep 01, 2011 4:39 pm
by JustinJack
I like your suggestion of the combobox, and I would do it if I was sure the combobox behavior was the same on all OSes. It seems to be the default combo-box coding it to select all the text in Microsofts stuff.

Code: Select all

hMainWnd = OpenWindow(1, 0,0, 500, 300, "test", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
dwExStyle = #Null
dwStyle = #WS_CHILD|#WS_VISIBLE|#WS_BORDER|#CBS_DROPDOWN
hCombo = CreateWindowEx_(dwExStyle, #WC_COMBOBOX, @"", dwStyle, 10, 10, 300, 20, WindowID(1), 1, GetModuleHandle_(#Null), 0)

hString = StringGadget(2, 10, 40, 300, 20, "")

Repeat : Until WaitWindowEvent() = 16
Event when I don't put the #CBS_DROPDOWN style It selects the text.
So I think y'all are right. Oh-well.

Re: Is it possible to select text natively upon setting focu

Posted: Thu Sep 01, 2011 6:14 pm
by kenmo
I was considering putting in a Feature Request for a SetGadgetSelection() function a couple days ago... (I have been using EM_SETSEL for a while, but lately I try to be cross-platform safe.)

Auto-selecting text boxes are common and useful - for example, the web browser you are reading this with. Click or tab over to the URL bar or search bar and it will all be selected (if it is like Firefox at least).

Re: Is it possible to select text natively upon setting focu

Posted: Thu Sep 01, 2011 6:18 pm
by JustinJack
I think that would be a good idea. I use it a lot, especially when the fields are numeric. Like dollar amounts or phone number area codes, or even two-digit state codes, where it's just not as easy to reach the backspace button.