Page 1 of 1

Webgadget icon

Posted: Fri Jul 30, 2021 11:15 am
by a_carignan
Hello, I would like to know how the icon of a web page to put it on a tab for example.

Re: Webgadget icon

Posted: Fri Jul 30, 2021 12:15 pm
by BarryG
Download the icon from the webpage, load and convert it to an image, apply that image ID to the panel tab.

Like this for Windows (with error-checking if no favicon exists):

Code: Select all

Procedure LoadImageEx(imgnum,file$,depth=32)
  Interface nIDXSurfaceFactory
    QueryInterface(a,b) : AddRef() : Release() : CreateSurface(a,b,c,d,e,f,g,h) : CreateFromDDSurface(a,b,c,d,e,f) : LoadImage(a.p-bstr,b,c,d,e,f)
    LoadImageFromStream(a,b,c,d,e,f) : CopySurfaceToNewFormat(a,b,c,d,e) : CreateD3DRMTexture(a,b,c,d,e) : BitBlt(a,b,c,d,e)
  EndInterface
  result=CoInitialize_(0)
  If result=#S_FALSE Or result=#S_OK
    #DXLOCKF_READ=0 : #CLSCTX_INPROC_SERVER=1
    CoCreateInstance_(?CLSID_DXTransformFactory,0,#CLSCTX_INPROC_SERVER,?IID_IDXTransformFactory,@dxtf.IDXTransformFactory)
    If dxtf
      dxtf\QueryService(?IID_IDXSurfaceFactory,?IID_IDXSurfaceFactory,@dxsf.nIDXSurfaceFactory)
      If dxsf
        dxsf\LoadImage(file$,0,0,0,?IID_IDXSurface,@surf.IDXSurface)
        If surf
          surf\LockSurfaceDC(0,#INFINITE,#DXLOCKF_READ,@lock.IDXDCLock)
          If lock
            DC=lock\GetDC()
            If DC
              GetClipBox_(DC,re.rect)
              If imgnum=#PB_Any
                result=CreateImage(#PB_Any,re\right,re\bottom,depth) : imgnum=result
              Else
                result=CreateImage(imgnum,re\right,re\bottom,depth)
              EndIf
              If result
                DestDC=StartDrawing(ImageOutput(imgnum))
                If DestDC : ok=BitBlt_(DestDC,0,0,re\right,re\bottom,DC,0,0,#SRCCOPY) : StopDrawing() : EndIf
                If ok=#False : FreeImage(imgnum) : Else : ok=result : EndIf
              EndIf
            EndIf
            Lock\Release()
          EndIf
          surf\Release()
        EndIf
        dxsf\Release()
      EndIf
      dxtf\Release()
    EndIf
  EndIf
  CoUninitialize_()
  ProcedureReturn ok
  DataSection
  CLSID_DXTransformFactory:
    Data.l $D1FE6762
    Data.w $FC48,$11D0
    Data.b $88,$3A,$3C,$8B,$00,$C1,$00,$00
  IID_IDXTransformFactory:
    Data.l $6A950B2B
    Data.w $A971,$11D1
    Data.b $81,$C8,$00,$00,$F8,$75,$57,$DB
  IID_IDXSurfaceFactory:
    Data.l $144946F5
    Data.w $C4D4,$11D1
    Data.b $81,$D1,$00,$00,$F8,$75,$57,$DB
  IID_IDXSurface:
    Data.l $B39FD73F
    Data.w $E139,$11D1
    Data.b $90,$65,$00,$C0,$4F,$D9,$18,$9D
  EndDataSection
EndProcedure

InitNetwork()
ReceiveHTTPFile("https://www.purebasic.com/favicon.ico","d:\favicon.ico")
icon=LoadImageEx(0,"d:\favicon.ico")
If icon=0 ; Not a valid image, or favicon didn't exist on website.
  icon=LoadIcon_(0,#IDI_EXCLAMATION)
EndIf

OpenWindow(0,200,200,300,100,"test",#PB_Window_SystemMenu)
PanelGadget(0,10,10,280,80)
AddGadgetItem(0,-1,"Empty tab")
AddGadgetItem(0,-1,"Tab with icon")
CloseGadgetList()

SetGadgetItemImage(0,1,icon)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Webgadget icon

Posted: Fri Jul 30, 2021 6:45 pm
by Paul
or keep it simple for cross platform...

Code: Select all

UsePNGImageDecoder() 
InitNetwork()
tmp.s=GetTemporaryDirectory()

If ReceiveHTTPFile("https://www.reddit.com/favicon.ico",tmp+"favicon.png")
  LoadImage(0,tmp+"favicon.png")  
  
  OpenWindow(0,200,200,300,100,"test",#PB_Window_SystemMenu)
  PanelGadget(0,10,10,280,80)
  AddGadgetItem(0,-1,"Empty tab")
  AddGadgetItem(0,-1,"Tab with icon")  
  SetGadgetItemImage(0,1,ImageID(0))  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf

Re: Webgadget icon

Posted: Sat Jul 31, 2021 1:23 am
by BarryG
What the? Why does this work? Why is an ICO image loadable as a PNG image? You can just save an ICO with a different extension to make it work?

Re: Webgadget icon

Posted: Sat Jul 31, 2021 2:04 am
by Paul
BarryG wrote: Sat Jul 31, 2021 1:23 am What the? Why does this work? Why is an ICO image loadable as a PNG image?
If you look at the ico file that is sent from reddit, it is actually a png. That's why you can load it as a png :)

Re: Webgadget icon

Posted: Sat Jul 31, 2021 6:30 am
by Marc56us
BarryG wrote: Sat Jul 31, 2021 1:23 am What the? Why does this work? Why is an ICO image loadable as a PNG image? You can just save an ICO with a different extension to make it work?
Whatever his extension name, a favicon.ico file can be a .ico, .png, .svg, .gif, ...
(Most often we use png or jpg 16x16 to be compatible with all browsers)
https://en.wikipedia.org/wiki/Favicon
:wink:

Re: Webgadget icon

Posted: Sat Jul 31, 2021 7:02 am
by BarryG
Thanks for the explanation. Not all favicons are going to be like that, though; so my code may be better for overall compatibility because it loads all image types. Paul's code for Reddit only works because Reddit's favicon is actually a PNG; but other sites might not be. PureBasic.com also has no favicon at all, so that has to be taken into account.

So my (edited with error-checking) code is still better overall, to ensure 100% compatibility.

Re: Webgadget icon

Posted: Sun Aug 01, 2021 12:11 pm
by a_carignan
Thanks for your help. :D