Page 1 of 1
[PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 12:34 pm
by marcoagpinto
Hello!
When I have a string gadget with some text and set the focus to it, the cursor appears at the beginning and not at the end of the gadget:
Code: Select all
; Word field
TextGadget(#TEXT_WINDOW_ADD_EDIT_WORD,0+6,0+6+2+1,80,16,"Word:")
t$="Test"
StringGadget(#TEXTBOX_WINDOW_ADD_EDIT_WORD,0+6+100-15-15-20,0+6,320-10-20-40+20,20+3,t$)
SetActiveGadget(#TEXTBOX_WINDOW_ADD_EDIT_WORD)
Notice the last line which sets the focus, but the cursor appears at the beginning and not at the end.
Is it normal?
Windows 8.1 Pro x64.
Thanks!
Kind regards,
>Marco A.G.Pinto
---------------
EDIT: "last line" not "last night". Sorry!
Re: [PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 12:49 pm
by PB
> Is it normal?
Yes.
But remember, what's normal for you is abnormal for someone else.
You say it's a bug because it's at the start... but I say that's correct
because it's not at the end. Who's right?
The good news is that Windows lets you set the cursor position:
Code: Select all
OpenWindow(0,200,250,450,200,"test",#PB_Window_SystemMenu)
sg=StringGadget(1,20,20,100,21,"1234567890")
SendMessage_(sg,#EM_SETSEL,5,5) ; Position 5.
SetActiveGadget(1)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Re: [PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 12:58 pm
by marcoagpinto
PB: I am working on cross-platform, so I can't use the code you provided
Thanks anyway
Kind regards from your friend, the silly,
Marco A.G.Pinto
---------------
Re: [PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 1:03 pm
by Danilo
marcoagpinto wrote:PB: I am working on cross-platform, so I can't use the code you provided
We requested things like getting and setting the cursor position and selected text start+end in StringGadgets several times,
but nothing happened. Do it yourself or choose another programming language. PB just does not have this kind of "advanced" functionality built-in.
Re: [PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 1:48 pm
by c4s
marcoagpinto wrote:PB: I am working on cross-platform, so I can't use the code you provided
Try the following (until it's officially implemented), should be fully cross-platform:
http://www.purebasic.fr/english/viewtop ... 87#p394987
Re: [PB5.22] Possible bug getting focus in string gadget?
Posted: Wed Mar 26, 2014 6:20 pm
by Shardik
Marco,
I already gave you
here the hint to take a look into my
list of cross-platform API examples. I know that it's somewhat hidden but the admins and mods don't seem to think it important enough to move that posting to a more prominent place or even make it sticky...
In that list you would have found links to code examples which get and set the cursor in a StringGadget. The code example thankfully linked to by c4s does even a bit more because it not only gets or sets the cursor but also returns selected text or does select text.
The example from my cross-platform list is a bit outdated because the Mac part uses the Carbon framework which is deprecated since PB 5.20. I therefore modified it for the current Cocoa framework on MacOS X:
Code: Select all
EnableExplicit
Procedure SetCursorPosition(StringGadgetID.I, CursorPosition.I)
SetActiveGadget(StringGadgetID)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(StringGadgetID), #EM_SETSEL, CursorPosition, CursorPosition)
CompilerCase #PB_OS_Linux
gtk_editable_set_position_(GadgetID(StringGadgetID), CursorPosition)
CompilerCase #PB_OS_MacOS
Protected Range.NSRange
Protected TextView.I
Range\location = CursorPosition
Range\length = 0
TextView = CocoaMessage(0, GadgetID(StringGadgetID), "currentEditor")
If TextView
CocoaMessage(0, TextView, "setSelectedRange:@", @Range)
EndIf
CompilerEndSelect
EndProcedure
Procedure.I GetCursorPosition(StringGadgetID.I)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ProcedureReturn SendMessage_(GadgetID(StringGadgetID), #EM_GETSEL, 0, 0) >> 16
CompilerCase #PB_OS_Linux
ProcedureReturn gtk_editable_get_position_(GadgetID(StringGadgetID))
CompilerCase #PB_OS_MacOS
Protected CursorPosition.I
Protected TextView.I = CocoaMessage(0, GadgetID(StringGadgetID), "currentEditor")
If TextView
CursorPosition = CocoaMessage(0, CocoaMessage(0,
CocoaMessage(0, TextView, "selectedRanges"),
"objectAtIndex:", 0), "rangeValue")
EndIf
ProcedureReturn CursorPosition
CompilerEndSelect
EndProcedure
OpenWindow(0, 270, 100, 350, 70, "StringGadget")
StringGadget(0, 10, 20, WindowWidth(0) - 20, 25,
"The quick brown fox jumps over the lazy dog.")
SetCursorPosition(0, 16) ; Set cursor in front of 'fox'
Debug "Current cursor postion: " + Str(GetCursorPosition(0))
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow