Page 1 of 1

Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:11 am
by bbanelli
Greetings to all,

I've created small code snippet for placeholder text in string gadget.

Image

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_entry_set_placeholder_text(*entry, text.p-utf8)
    gtk_entry_get_placeholder_text(*entry)
  EndImport
CompilerEndIf

Procedure SetGadgetPlaceholder(Gadget.i, Text.s)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(Gadget), #EM_SETCUEBANNER, #True, Text)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_entry_set_placeholder_text(GadgetID(Gadget), Text)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "cell"), "setPlaceholderString:$", @Text)
  CompilerEndIf
  ProcedureReturn
EndProcedure

Procedure.s GetGadgetPlaceholder(Gadget.i)
  Protected.s Text = Space(1024)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(Gadget), #EM_GETCUEBANNER, @Text, StringByteLength(Text))
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
    Text = PeekS(gtk_entry_get_placeholder_text(GadgetID(Gadget)), -1, #PB_UTF8)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    Text = PeekS(CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "cell"), "placeholderString"), "UTF8String"), -1, #PB_UTF8)
  CompilerEndIf
  ProcedureReturn Text
EndProcedure

If OpenWindow(0, 0, 0, 220, 50, "Placeholder example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(1, 10, 10, 200, 20, "")
  SetGadgetPlaceholder(1, "Your Placeholder")
  Debug GetGadgetPlaceholder(1)
  Repeat : Event = WaitWindowEvent(1) : Until Event = #PB_Event_CloseWindow
EndIf
However, I can't seem to figure out how to get the placeholder text on OS X, can someone assist?
Updated thanks to wilbert to work on all OS X versions! :)

With my best,

Bruno

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:16 am
by wilbert
I tested you code on OSX but it seems to work fine.

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:19 am
by bbanelli
wilbert wrote:I tested you code on OSX but it seems to work fine.
Well, setting placeholder text does (or at least should) work fine, however, I need a procedure to GET that text somehow. :)

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:36 am
by wilbert
bbanelli wrote:Well, setting placeholder text does (or at least should) work fine, however, I need a procedure to GET that text somehow. :)
Oops, missed that point :oops:

Anyway, here's the solution :wink:

Code: Select all

  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    Text = PeekS(CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "placeholderString"), "UTF8String"), -1, #PB_UTF8)
  CompilerEndIf 

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:39 am
by bbanelli
Uf, thank you very much, this was way out of my league to figure out! :)

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 6:45 pm
by Wolfram
Crashes on 10.7.5 x86

[19:30:41] [ERROR] Line: 16
[19:30:41] [ERROR] Object does not respond to method "setPlaceholderString:" .

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 7:56 pm
by Shardik
Wolfram wrote:Crashes on 10.7.5 x86

[19:30:41] [ERROR] Line: 16
[19:30:41] [ERROR] Object does not respond to method "setPlaceholderString:" .
I obtain the same error message as observed by Wolfram with PB 5.31 x86 and x64 on:
- Snow Leopard
- Lion
- Mavericks

Works without problems on:
- Yosemite

The problem seems to be that the StringGadget is internally an NSTextField but setPlaceholderString: and placeholderString: are methods of NSTextFieldCell. Although the documentation for Snow Leopard and Mavericks states:
Apple's NSTextFieldCell reference wrote:All of the methods declared by this class are also declared by the NSTextField class, which uses NSTextFieldCell objects to draw and edit text. These NSTextField cover methods call the corresponding NSTextFieldCell methods.

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 8:09 pm
by bbanelli
Truth to be told, I have only tested it on latest 5.4 beta on 10.10.5. Is there a workaround for this issue?

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 9:29 pm
by wilbert
I only have 10.10.5. Does this make a difference on older OSX versions ?

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    gtk_entry_set_placeholder_text(*entry, text.p-utf8)
    gtk_entry_get_placeholder_text(*entry)
  EndImport
CompilerEndIf

Procedure SetGadgetPlaceholder(Gadget.i, Text.s)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(Gadget), #EM_SETCUEBANNER, #True, Text)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_entry_set_placeholder_text(GadgetID(Gadget), Text)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "cell"), "setPlaceholderString:$", @Text)
  CompilerEndIf
  ProcedureReturn
EndProcedure

Procedure.s GetGadgetPlaceholder(Gadget.i)
  Protected.s Text = Space(1024)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(Gadget), #EM_GETCUEBANNER, @Text, StringByteLength(Text))
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
    Text = PeekS(gtk_entry_get_placeholder_text(GadgetID(Gadget)), -1, #PB_UTF8)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    Text = PeekS(CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, GadgetID(Gadget), "cell"), "placeholderString"), "UTF8String"), -1, #PB_UTF8)
  CompilerEndIf
  ProcedureReturn Text
EndProcedure

If OpenWindow(0, 0, 0, 220, 50, "Placeholder example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(1, 10, 10, 200, 20, "")
  SetGadgetPlaceholder(1, "Your Placeholder")
  Debug GetGadgetPlaceholder(1)
  Repeat : Event = WaitWindowEvent(1) : Until Event = #PB_Event_CloseWindow
EndIf

Re: Getting placeholder text from StringGadget?

Posted: Wed Sep 30, 2015 11:00 pm
by Wolfram
Yup, it works.

…it's not magic, it Wilbert ;-)

Re: Getting placeholder text from StringGadget?

Posted: Thu Oct 01, 2015 3:00 pm
by Shardik
It works now also on Snow Leopard and should also work on the other versions. Thank you, Wilbert!

Re: Getting placeholder text from StringGadget?

Posted: Thu Oct 01, 2015 6:40 pm
by davido
Very nice.

Thank you. :D

Re: Getting placeholder text from StringGadget?

Posted: Thu Oct 01, 2015 7:58 pm
by Shardik
Bruno,

thank you very much for your nice and useful cross-platform example! Sorry for not having put my thanks into my first posting in this thread where it would have belonged to of course... :oops:

I have also added the link to your example (with Wilbert's Mac bugfix) to my collection of cross-platfrom API code examples.

Re: Getting placeholder text from StringGadget?

Posted: Thu Oct 01, 2015 10:08 pm
by bbanelli
Shardik wrote:Bruno,

thank you very much for your nice and useful cross-platform example! Sorry for not having put my thanks into my first posting in this thread where it would have belonged to of course... :oops:

I have also added the link to your example (with Wilbert's Mac bugfix) to my collection of cross-platfrom API code examples.
Hi Shardik,

trust me, I am the one to be grateful to both PB team and its whole community (not mentioning names because I would surely miss someone; but including yourself per se, particularly in Linux/OS X field) since I've learned so much here that I've (mostly :D) successfully started programming in development suits and languages other than PB! :)