Page 1 of 1

Transparent TextGadget

Posted: Sun Aug 07, 2011 5:53 am
by spacebuddy
Hey All,

Is there anyway to make a textgadget or a HyperLinkGadget transparent?

I have a bitmap in the background and when I put a text gadget on the bitmap it has a box around it. :(

Thanks

Re: Transparent TextGadget

Posted: Sun Aug 14, 2011 9:18 pm
by Shardik
spacebuddy wrote:Is there anyway to make a textgadget or a HyperLinkGadget transparent?

I have a bitmap in the background and when I put a text gadget on the bitmap it has a box around it. :(
It should be possible but it requires an enormous amount of API code.
You would have to create your Window and all your Gadgets with API
calls and you wouldn't be able to use PB's functions to get and set the
Gadgets' attributes but have to use API calls instead of them. You would
even have to implement a callback for manipulating background color...

A much more simple approach would be to draw directly into your
background image with DrawText() instead of using a TextGadget.
Although of course this still wouldn't solve your problem with the
HyperLinkGadget. You would have to implement your own by using
and manipulating text drawn with DrawText()...

Code: Select all

OpenWindow(0, 200, 100, 256, 256, "With background image")

If LoadImage(0, #PB_Compiler_Home + "/examples/sources/Data/Background.bmp")
  LoadFont(0, "Apple Chancery", 28, #PB_Font_Bold)

  If StartDrawing(ImageOutput(0))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(FontID(0))
    DrawText(20, 100, "Transparent Text", RGB(0, 0, 0))
    StopDrawing()
  EndIf

  ImageGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), ImageID(0))

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Transparent TextGadget

Posted: Sun Aug 14, 2011 9:37 pm
by Shardik
To give you an impression how complicated the change of the background
color might become, I present an example which changes the background
color of a CheckBoxGadget. Unfortunately I haven't been able to implement
this in a standard PB window. PB seems to manipulate background colors
internally so that all my changes of background color of Window and Gadgets
are overdrawn by PB's default background. Hence the need to create even the
window and its closing callback via API calls:

Code: Select all

EnableExplicit

ImportC ""
  CreateCheckBoxControl(WindowRef.L, BoundsRectangle.L, CFStringTitle.L, InitialState.L, AutoToggle.L, *ControlRef)
  CreateNewWindow(WindowClass.L, Attributes.L, *Bounds, *WindowRef)
  DisposeEventHandlerUPP(EventHandlerUPP.L)
  NewControlColorUPP(ControlColorProcPtr.L)
  QuitApplicationEventLoop()
  RGBBackColor(RGBColor.L)
  RunApplicationEventLoop()
  SetControlColorProc(ControlRef.L, ControlColorUPP.L)
  ShowControl(ControlRef.L)
EndImport

#kEventClassWindow = 'wind'
#kEventWindowClose = 72
#kMovableModalWindowClass = 4
#kWindowCloseBoxAttribute = 1 << 0
#kWindowStandardHandlerAttribute = 1 << 25
#kControlMsgSetUpBackground = 23

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

Structure Rect
  Top.W
  Left.W
  Bottom.W
  Right.W
EndStructure

Structure RGBColor
  Red.U
  Green.U
  Blue.U
EndStructure

Structure ControlBackgroundRec
  BitDepth.W
  IsColorDevice.B
EndStructure

Procedure ControlColorCallback(ControlRef.L, Message.W, ColorDepth.W, DrawInColor.L)
  Protected BackgroundColor.RGBColor

  BackgroundColor\Red = $B0B0
  BackgroundColor\Green = $E0E0
  BackgroundColor\Blue = $E6E6

  If Message = #kControlMsgSetUpBackground
    RGBBackColor(@BackgroundColor)
  EndIf
EndProcedure

Procedure WindowCloseHandler(*NextEventHandler, EventRef.L, UserData.L)
  QuitApplicationEventLoop()
  ProcedureReturn 0
EndProcedure

Define Bounds.Rect
Define CheckBoxRef.L
Define ControlColorUPP.L
Define WindowCloseHandlerUPP.L
Define WindowRef.L

Dim EventTypes.EventTypeSpec(0)

Bounds\Top = 100
Bounds\Left = 270
Bounds\Bottom = 140
Bounds\Right = 460

If CreateNewWindow(#kMovableModalWindowClass, #kWindowCloseBoxAttribute | #kWindowStandardHandlerAttribute, @Bounds, @WindowRef) = 0
  WindowCloseHandlerUPP = NewEventHandlerUPP_(@WindowCloseHandler())
  EventTypes(0)\EventClass = #kEventClassWindow
  EventTypes(0)\EventKind  = #kEventWindowClose
  InstallEventHandler_(GetWindowEventTarget_(WindowRef), WindowCloseHandlerUPP, 1, @EventTypes(), 0, 0)
  ShowWindow_(WindowRef)

  Bounds\Left   = 10
  Bounds\Top    = 10
  Bounds\Right  = 180
  Bounds\Bottom = 30

  If CreateCheckBoxControl(WindowRef, @Bounds, CFStringCreateWithCString_(0, "Customized CheckBox", 0), 0, #True, @CheckBoxRef) = 0
    ControlColorUPP = NewControlColorUPP(@ControlColorCallback())
    SetControlColorProc(CheckBoxRef, ControlColorUPP)
    ShowControl(CheckBoxRef)
  EndIf

  RunApplicationEventLoop()
  DisposeEventHandlerUPP(WindowCloseHandlerUPP)
EndIf