Native ButtonIconGadget (icon+text)

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Native ButtonIconGadget (icon+text)

Post by Trond »

Unfortunately DrawText() renders muddy text in 4.41 RC1 at least, but apart from that it's pretty nice looking.

Code: Select all

; For 4.41

Procedure ButtonIconGadget(x, y, w, h, Text.s, Icon, Flags=0)

  Static Border = 10
  Static Padding = 10
  
  Protected Font = GetGadgetFont(#PB_Default)
  Protected Image
  Protected iw, ih    ; label image height width
  Protected icx, icy  ; icon x, y within label image
  Protected tw, th    ; text height width
  Protected  tx, ty   ; text x, y within label image
  Protected tcol = RGBA(0, 0, 0, 255)  ; text color
  
  ; Calculate coordinates
  iw = ImageWidth(Icon) + Padding
  ih = ImageHeight(Icon)
  icx = 0
  icy = 0
  
  tx = iw
  
  StartDrawing(ImageOutput(Image))
    DrawingFont(Font)
    tw = TextWidth(Text)
    th = TextHeight("Wg")
  StopDrawing()
  If th > ih
    ih = th
    icy + (th-ih)/2
  EndIf
  iw + tw
  
  ty = ih/2-th/2
  
  ; Create image
  Image = CreateImage(#PB_Any, iw, ih, 32)
  StartDrawing(ImageOutput(Image))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, iw, ih, RGBA(255, 255, 255, 0))
    DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
    DrawingFont(Font)
    DrawImage(ImageID(Icon), icx, icy)
    DrawText(tx, ty, Text, tcol)
    Box(25, 25, 25, 25, RGBA(255, 0, 0, 255))
  StopDrawing()
  
  
  G = ButtonImageGadget(#PB_Any, x, y, w, h, ImageID(Image), Flags)
  SetGadgetData(G, Image)
  ProcedureReturn G
EndProcedure

Procedure FreeButtonIcon(Button)
  FreeImage(GetGadgetData(Button))
EndProcedure


OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

UsePNGImageDecoder()
LoadImage(0, "c:\icon.png")
bi = ButtonIconGadget(10, 10, 200, 120, "I can do icon too", 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      DisableGadget(bi, 1)
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Native ButtonIconGadget (icon+text)

Post by rsts »

Nice one :D

Will be very useful. Thanks for sharing.

cheers
Post Reply