Cursor in StringGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cursor in StringGadget

Post by Shardik »

WilliamL wrote:Hey, Shardik! What about the Mac?
Dear William,
unfortunately the only machine I don't have at work is a Mac... :lol:

But Windows (NT, XP, 7), AIX, z/OS (Mainframe), Linux (SLES, OpenSuse, Debian, Ubuntu,
Kubuntu, Xubuntu) and a VMWare 4.1 ESX cluster I would have available for testing purposes... :wink:

At home I have expanded my above example for MacOS X support:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  ImportC ""
    SetControlData(Control, ControlPartCode, TagName, BufferSize, *Buffer)
  EndImport

  #kControlEditTextPart = 5
  #kControlEditTextSelectionTag = 'sele'

  Structure ControlEditTextSelectionRec
    selStart.W
    selEnd.W
  EndStructure
CompilerEndIf

OpenWindow(0, 100, 200, 400, 200, "", #PB_Window_MinimizeGadget)
StringGadget(0, 50, 50, 200, 20, "example")
Text$ = GetGadgetText(0)
SetActiveGadget(0)

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    TextSelection.ControlEditTextSelectionRec
    TextSelection\selStart = Len(Text$)
    TextSelection\selEnd = Len(Text$)
    SetControlData(GadgetID(0), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
  CompilerCase #PB_OS_Linux
    gtk_editable_set_position_(GadgetID(0), -1)
  CompilerCase #PB_OS_Windows
    SendMessage_(GadgetID(0), #EM_SETSEL, Len(Text$), Len(Text$))
CompilerEndSelect

Repeat
  EventID = WaitWindowEvent()   
Until EventID = #PB_Event_CloseWindow
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cursor in StringGadget

Post by Shardik »

WilliamL wrote:

Code: Select all

gtk_editable_set_position_(GadgetID(0), -1)
..and does the -1 indicate the position? If so then do other values put the cursor somewhere else within the string?
Yes, but you can use gtk_editable_set_position() only in Linux because it is a Linux API function:
(http://library.gnome.org/devel/gtk/stab ... t-position)
GTK+ Reference Manual wrote:Sets the cursor position in the editable to the given value.

The cursor is displayed before the character with the given (base 0) index in the contents of the editable. The value must be less than or equal to the number of characters in the editable. A value of -1 indicates that the position should be set after the last character of the editable. Note that position is in characters, not in bytes.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Cursor in StringGadget

Post by WilliamL »

Thanks Shardik,

It works perfectly! And like the Linux example the same command can be used to select part or all of the text. This is a work-around for SetSelect for the Mac and I will add it to my list.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply