It is currently Sun Mar 24, 2019 4:00 am

All times are UTC + 1 hour

Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Re: [PB5.22] Possible bug getting focus in string gadget?
PostPosted: Wed Mar 26, 2014 6:20 pm 
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1596
Location: Germany

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:

Procedure SetCursorPosition(StringGadgetID.I, CursorPosition.I)

  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)

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")

      ProcedureReturn CursorPosition

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))

Until WaitWindowEvent() = #PB_Event_CloseWindow

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour

Who is online

Users browsing this forum: No registered users and 9 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye