GetGadgetItemImage()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

GetGadgetItemImage()

Post by uwekel »

Hi,

recently we've got the SetGadgetItemImage() method, so now we can change the images very easy. Today i needed the GetGadgetItemImage() as well. Would you mind adding this to PB?

Best regards
Uwe
Last edited by uwekel on Wed May 08, 2013 8:22 pm, edited 1 time in total.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: GetGadgetItemImage()

Post by IdeasVacuum »

+1 Actually, seems strange that we haven't got it, don't normally have a Set without a Get.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: GetGadgetItemImage()

Post by Oma »

+1
Since the absence of GetGadgetItemImage() annoyed me every time I use GadgetItemImages(and makes me clueless how to solve), I'm joining in with the urgent request to add this feature.

Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: GetGadgetItemImage()

Post by mestnyi »

+1
User avatar
holzhacker
Enthusiast
Enthusiast
Posts: 123
Joined: Mon Mar 08, 2010 9:14 pm
Location: "Mens sana in corpore sano"
Contact:

Re: GetGadgetItemImage()

Post by holzhacker »

+1
Greetings and thanks!

Romerio Medeiros
romerio@gmail.com
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: GetGadgetItemImage()

Post by Paul »

Since there is still no GetGadgetItemImage() after 5 years, I'll bump this thread as well :)

+1
Image Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: GetGadgetItemImage()

Post by davido »

+1
DE AA EB
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: GetGadgetItemImage()

Post by BarryG »

+1
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: GetGadgetItemImage()

Post by RSBasic »

+1
Image
Image
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: GetGadgetItemImage()

Post by Mijikai »

+1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: GetGadgetItemImage()

Post by Shardik »

After more than 6 years of waiting for an implementation in PureBasic, I decided to program a cross-platform GetGadgetItemImage() procedure which I have tested successfully with PB 5.46 in both ASCII and Unicode mode on these operating systems:
- Linux Mint 19.1 x64 'Tessa' with Cinnamon using GTK2 and GTK3
- MacOS 10.6.8 'Snow Leopard'
- MacOS 10.14.5 'Mojave'
- Windows 7 x64 SP1
- Windows 10 x64 Version 1809

Code: Select all

EnableExplicit

UsePNGImageDecoder()

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  #G_TYPE_OBJECT = 20 << 2

  ImportC ""
    g_object_get_property(*Object.GObject, PropertyName.P-UTF8,
      *PropertyValue.GValue)
    g_type_check_instance_is_a(*Instance.GTypeInstance, *Type.GTypeClass)
    gtk_cell_layout_get_cells(*CellLayout)
    gtk_widget_get_realized(*Widget.GtkWidget)
  EndImport
CompilerEndIf

Procedure GetGadgetItemImage(ListIconID.I, Row.I)
  Protected Image.I

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected CellRenderer.I
      Protected CellRendererList.I
      Protected PixBuf.GValue
      Protected SelectedRow.I
      Protected *TreeColumn
      
      SelectedRow = GetGadgetState(ListIconID)
      
      ; ----- Select row with image to get the correct CellRenderer
      ;       for that row
      SetGadgetState(ListIconID, Row)
      
      *TreeColumn = gtk_tree_view_get_column_(GadgetID(ListIconID), 0)
      CellRendererList = gtk_cell_layout_get_cells(*TreeColumn)
      CellRenderer = g_list_nth_data_(CellRendererList, 0)
      
      If g_type_check_instance_is_a(CellRenderer,
                                    gtk_cell_renderer_pixbuf_get_type_())
        PixBuf\g_type = #G_TYPE_OBJECT
        g_object_get_property(CellRenderer, "pixbuf", @PixBuf)
        Image = g_value_get_object_(@PixBuf)
      EndIf
      
      SetGadgetState(ListIconID, SelectedRow)
      g_list_free_(CellRendererList)
    CompilerCase #PB_OS_MacOS
      Protected ColumnObject.I
      Protected ColumnObjectArray.I
      Protected ImageCell.I
     
      CocoaMessage(@ColumnObjectArray, GadgetID(ListIconID), "tableColumns")
      CocoaMessage(@ColumnObject, ColumnObjectArray, "objectAtIndex:", 0)
     
      If PeekS(CocoaMessage(0, CocoaMessage(0,
        ColumnObject, "identifier"), "UTF8String"), -1, #PB_UTF8) = "Image"
        ImageCell = CocoaMessage(0, GadgetID(ListIconID),
          "preparedCellAtColumn:", 0,
          "row:", 0)
       
        If ImageCell
          Image = CocoaMessage(0, ImageCell, "image")
        EndIf
      EndIf
    CompilerCase #PB_OS_Windows
      Protected ImageIndex.I
      Protected ImageListHandle.I
      Protected Item.LV_ITEM
     
      Item.LV_ITEM
      Item\Mask = #LVIF_IMAGE
      Item\iItem = Row
      SendMessage_(GadgetID(ListIconID), #LVM_GETITEM, 0, @Item)
      ImageIndex = Item\iImage
      ImageListHandle = SendMessage_(GadgetID(ListIconID), #LVM_GETIMAGELIST,
        #LVSIL_SMALL, 0)
      Image = ImageList_GetIcon_(ImageListHandle, ImageIndex, #ILD_TRANSPARENT)
  CompilerEndSelect
 
  ProcedureReturn Image
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png") = 0
  MessageRequester("Error",
    "Unable to load image CdPlayer.ico")
  End
EndIf

OpenWindow(0, 270, 100, 220, 130, "GetGadgetItemImage()")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 50,
  "Column 1", WindowWidth(0) - 52)
AddGadgetItem(0, -1, "Row with image")
TextGadget(1, 10, GadgetHeight(0) + 20, 100, 25, "Grabbed image:")
ImageGadget(2, 110, GadgetHeight(0) + 20, 16, 16, 0)
SetGadgetItemImage(0, 0, ImageID(0))

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ; ----- Wait until ListIconGadget is initialized
  While gtk_widget_get_realized(GadgetID(0)) = #False
   Delay(10)
  Wend
CompilerEndIf

SetGadgetState(2, GetGadgetItemImage(0, 0))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Update:
- I had to update the Linux part of procedure GetGadgetItemImage() because the old code didn't use the row parameter and always returned the image which was the last to have been rendered by the CellRenderer. The trick is to store the actual selected row in variable SelectedRow, select the wanted row (that will now be rendered again by the correct CellRenderer) and afterwards restore the selected row from variable SelectedRow (which may be -1, if no row was selected at all).
- I replaced the general code

Code: Select all

; ----- Wait until Window and Widgets are initialized (Necessary in Linux)
Repeat : Until WindowEvent() = 0
by the more target-oriented code

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ; ----- Wait until listIconGadget is initialized
  While gtk_widget_get_realized(GadgetID(0)) = #False
   Delay(10)
  Wend
CompilerEndIf
which only in Linux will wait until the ListIconGadget is initialized internally and the following call of GetGadgetItemImage() will safely work.
Last edited by Shardik on Fri Jul 12, 2019 6:28 pm, edited 4 times in total.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: GetGadgetItemImage()

Post by BarryG »

Shardik, thanks for that. Don't know why some simple requests like this get ignored for so long. It's quite depressing.

I also notice that in your #PB_OS_MacOS section you've got GadgetID(0). Shouldn't that be GadgetID(ListIconID)?
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: GetGadgetItemImage()

Post by Shardik »

BarryG wrote:Shardik, thanks for that. Don't know why some simple requests like this get ignored for so long. It's quite depressing.

I also notice that in your #PB_OS_MacOS section you've got GadgetID(0). Shouldn't that be GadgetID(ListIconID)?
Thank you for pinpointing me to these errors in the MacOS part. I have corrected them in my example above.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: GetGadgetItemImage()

Post by Andre »

@Shardik:
Thank you for this code example! :D
Also showing that it should possible to add a native implementation... ;-)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: GetGadgetItemImage()

Post by Olliv »

This is wrong : the right answer is 9 (1+1+1+1+1+1+1+1+1)

I'll test this work.
Post Reply