Page 1 of 1

Adding a lot of images to a ListIconGadget...

Posted: Tue Jul 27, 2010 10:26 pm
by Andre
The following code snippets are from my large project, where I load a list of country names with their related flag into a list in a ListIconGadget.

On Windows all is working fine, but on MacOS I get a lot GFX errors, when the flags should be displayed in front of the country names in the ListIcon.
The "trick" is: The flag image gets loaded, added to the listicon, and then freed. The same then for every of the >250 countries/flags...

Code: Select all

Procedure LoadFlagImage()
  Protected image.i
  If IsImage(0) 
    FreeImage(0)
  EndIf
  If IsImage(1)
    FreeImage(1)
  EndIf
  
  ;DisableDebugger    ; because else each missing flag would call a "Image filename not found" error"
  If LoadImage(0, path$ + "Flags/small/"+LCase(Countries()\ISO3166A2)+".png")
    CreateImage(1,16,16)
    StartDrawing(ImageOutput(1))               ; the loaded flags are 16x11 pixel only
      Box(0, 0, 16, 16, RGB(255, 255, 255))    ; so we draw a white background...
      DrawImage(ImageID(0), 0, 3)                       ; and paint the image on it
    StopDrawing()
    image = ImageID(1)
  Else 
    image = 0
    Debug "Image loading failed for: " + LCase(Countries()\ISO3166A2) + ".png"
  EndIf
  ;EnableDebugger   ; enable the debugger for the further program  
  ProcedureReturn image
EndProcedure       


; snippet from the code, which is calling the LoadFlagImage() routine and add the flag image to the ListIcon:
  ForEach Countries()
        image = LoadFlagImage()   ; Note: Countries() list must be active, so the ISO3166A2 field of is available
        AddGadgetItem(#Gadget_CountryList, index, Countries()\Name, image)
        index + 1
  Next
Now my question:
Is this a misbehaviour of PB / MacOS?

Or it's a "ugly" programming trick, which works on Windows but don't work on MacOS? In this case I must load every flag image into a separate ImageID, before adding the ImageIDs via AddGadgetItem() to the ListIcon...

Or do you have any other suggestions?

Re: Adding a lot of images to a ListIconGadget...

Posted: Thu Jul 29, 2010 10:12 pm
by IdeasVacuum
Hello Andre

I was wondering if something more simple would work:

Code: Select all

ForEach Countries()
        iImage = LoadImage(#PB_Any, path$ + "Flags/small/"+LCase(Countries()\ISO3166A2)+".png")
        AddGadgetItem(#Gadget_CountryList, index, Countries()\Name, iImage)
        index + 1
Next
Edit: Or:

Code: Select all

index = 1
ForEach Countries()
        LoadImage(index, path$ + "Flags/small/"+LCase(Countries()\ISO3166A2)+".png")
        AddGadgetItem(#Gadget_CountryList, index, Countries()\Name, index)
        index + 1
Next

Re: Adding a lot of images to a ListIconGadget...

Posted: Thu Jul 29, 2010 10:44 pm
by freak
> Or it's a "ugly" programming trick, which works on Windows but don't work on MacOS?

This is it. You should not free and object as long as you are still using it.

Re: Adding a lot of images to a ListIconGadget...

Posted: Tue Aug 03, 2010 9:36 pm
by Andre
freak wrote:> Or it's a "ugly" programming trick, which works on Windows but don't work on MacOS?

This is it. You should not free and object as long as you are still using it.
Ok, thanks for the clarification!

I have now changed my code to completely loading all flag images at program start, save the (#PB_Any created) ImageID to an array, and then easily use saved ImageIDs as image parameter of AddGadgetItem()...

Thanks! :-)