[PB5.22] Possible bug getting focus in string gadget?

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1039
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

[PB5.22] Possible bug getting focus in string gadget?

Post 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!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: [PB5.22] Possible bug getting focus in string gadget?

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
marcoagpinto
Addict
Addict
Posts: 1039
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: [PB5.22] Possible bug getting focus in string gadget?

Post by marcoagpinto »

PB: I am working on cross-platform, so I can't use the code you provided :cry:

Thanks anyway

Kind regards from your friend, the silly,
Marco A.G.Pinto
---------------
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: [PB5.22] Possible bug getting focus in string gadget?

Post by Danilo »

marcoagpinto wrote:PB: I am working on cross-platform, so I can't use the code you provided :cry:
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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: [PB5.22] Possible bug getting focus in string gadget?

Post by c4s »

marcoagpinto wrote:PB: I am working on cross-platform, so I can't use the code you provided :cry:
Try the following (until it's officially implemented), should be fully cross-platform: http://www.purebasic.fr/english/viewtop ... 87#p394987
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [PB5.22] Possible bug getting focus in string gadget?

Post 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
Post Reply