Adding a lot of images to a ListIconGadget...

Mac OSX specific forum
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2149
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Adding a lot of images to a ListIconGadget...

Post 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?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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
Last edited by IdeasVacuum on Fri Jul 30, 2010 4:02 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
freak
PureBasic Team
PureBasic Team
Posts: 5950
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

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

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2149
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

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

Post 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! :-)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply