Page 1 of 1

TextGadget drop shadow?

Posted: Sun Dec 22, 2019 10:35 am
by Rinzwind
Anyway to do this with macOS functionality?

I'm guessing something with NSShadow or NSShadowAttributeName... but clueless. Also guessing can't use UILabel. TextGadget is based on NSTextView?

Re: TextGadget drop shadow?

Posted: Sun Dec 22, 2019 11:59 am
by wilbert

Code: Select all

Procedure SetGadgetShadow(Gadget, hOffset, vOffset, blur, color)
  
  Protected.CGSize ShadowOffset
  Protected.CGFloat ShadowBlurRadius, R,G,B,A
  Protected.i Shadow, ShadowColor
  
  ShadowOffset\width = hOffset
  ShadowOffset\height = vOffset
  ShadowBlurRadius = blur
  R = Red(Color)
  G = Green(Color)
  B = Blue(Color)
  A = Alpha(Color)
  
  Shadow = CocoaMessage(0, 0, "NSShadow new")
  ShadowColor = CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@", @R, 
                             "green:@", @G,
                             "blue:@", @B,
                             "alpha:@", @A)
  
  CocoaMessage(0, Shadow, "setShadowOffset:@", @ShadowOffset)
  CocoaMessage(0, Shadow, "setShadowBlurRadius:@", @ShadowBlurRadius)
  CocoaMessage(0, Shadow, "setShadowColor:", ShadowColor)
  CocoaMessage(0, GadgetID(Gadget), "setWantsLayer:", #YES)
  CocoaMessage(0, GadgetID(Gadget), "setShadow:", Shadow)
  CocoaMessage(0, Shadow, "release")
  
EndProcedure




If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadget(0, 10,  10, 250, 20, "TextGadget Standard (Left)")
  TextGadget(1, 10,  70, 250, 20, "TextGadget Center", #PB_Text_Center)
  TextGadget(2, 10,  40, 250, 20, "TextGadget Right", #PB_Text_Right)
  TextGadget(3, 10, 100, 250, 20, "TextGadget Border", #PB_Text_Border)
  TextGadget(4, 10, 130, 250, 20, "TextGadget Center + Border", #PB_Text_Center | #PB_Text_Border)
  
  SetGadgetShadow(0, 4, 4, 4, $800000ff)
  SetGadgetShadow(3, 2, 2, 4, $8000ff00)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: TextGadget drop shadow?

Posted: Sun Dec 22, 2019 12:34 pm
by Rinzwind
Exceeded expectations. Fast and correct. So glad this forum (and Wilbert 8) ) exists ;)

Re: TextGadget drop shadow?

Posted: Sun Dec 22, 2019 4:07 pm
by Justin
I had to put:

Code: Select all

CocoaMessage(0, GadgetID(Gadget), "setWantsLayer:", #YES)
Just before:

Code: Select all

CocoaMessage(0, GadgetID(Gadget), "setShadow:", Shadow)
Otherwise it did not work here.
PB5.70 MacOS 10.12.6

Re: TextGadget drop shadow?

Posted: Sun Dec 22, 2019 4:13 pm
by wilbert
Justin wrote:Otherwise it did not work here.
PB5.70 MacOS 10.12.6
The Apple docs do mention that if the view does not have a layer, setting a border has no effect.
So you are probably right about this and to be sure I updated the code to include this line.
On macOS 10.15 however it also seems to work without this line so that's why I didn't notice it.

Re: TextGadget drop shadow?

Posted: Sun Dec 22, 2019 4:28 pm
by Justin
Maybe is the PB version, I am using 5.70, have you tried in 5.71:

Code: Select all

  Debug CocoaMessage(0, GadgetID(Gadget), "wantsLayer")
Before setting it? Here it shows 0.