Getting placeholder text from StringGadget?

Mac OSX specific forum
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Getting placeholder text from StringGadget?

Post 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
Last edited by bbanelli on Thu Oct 01, 2015 12:18 am, edited 5 times in total.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Getting placeholder text from StringGadget?

Post by wilbert »

I tested you code on OSX but it seems to work fine.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Getting placeholder text from StringGadget?

Post 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. :)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Getting placeholder text from StringGadget?

Post 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 
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Getting placeholder text from StringGadget?

Post by bbanelli »

Uf, thank you very much, this was way out of my league to figure out! :)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Getting placeholder text from StringGadget?

Post 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:" .
macOS Catalina 10.15.7
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Getting placeholder text from StringGadget?

Post 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.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Getting placeholder text from StringGadget?

Post 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?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Getting placeholder text from StringGadget?

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Getting placeholder text from StringGadget?

Post by Wolfram »

Yup, it works.

…it's not magic, it Wilbert ;-)
macOS Catalina 10.15.7
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Getting placeholder text from StringGadget?

Post by Shardik »

It works now also on Snow Leopard and should also work on the other versions. Thank you, Wilbert!
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Getting placeholder text from StringGadget?

Post by davido »

Very nice.

Thank you. :D
DE AA EB
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Getting placeholder text from StringGadget?

Post 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.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Getting placeholder text from StringGadget?

Post 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! :)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Post Reply