Webgadget icon

Advanced game related topics
User avatar
a_carignan
User
User
Posts: 81
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Webgadget icon

Post by a_carignan »

Hello, I would like to know how the icon of a web page to put it on a tab for example.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Webgadget icon

Post 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
Last edited by BarryG on Sat Jul 31, 2021 7:03 am, edited 1 time in total.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Webgadget icon

Post 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
Image Image
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Webgadget icon

Post 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?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Webgadget icon

Post 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 :)
Image Image
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Webgadget icon

Post 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:
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Webgadget icon

Post 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.
User avatar
a_carignan
User
User
Posts: 81
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Webgadget icon

Post by a_carignan »

Thanks for your help. :D
Post Reply