PureBasic Forum
https://www.purebasic.fr/english/

GetGadgetItemImage()
https://www.purebasic.fr/english/viewtopic.php?f=3&t=54573
Page 2 of 2

Author:  BarryG [ Mon Jun 17, 2019 7:14 am ]
Post subject:  Re: GetGadgetItemImage()

Olliv wrote:
This is wrong : the right answer is 9 (1+1+1+1+1+1+1+1+1)
I'll test this work.

What?

Author:  RSBasic [ Mon Jun 17, 2019 8:15 am ]
Post subject:  Re: GetGadgetItemImage()

Andre wrote:
Also showing that it should possible to add a native implementation... ;-)

+1

Author:  Demivec [ Mon Jun 17, 2019 8:26 am ]
Post subject:  Re: GetGadgetItemImage()

Olliv wrote:
This is wrong : the right answer is 9 (1+1+1+1+1+1+1+1+1)
I'll test this work.

It is now 11 (op +1 +1 +1 +1 +1 +1 +1 +1 +1 * 2). :)

Author:  Shardik [ Sun Jul 14, 2019 10:08 am ]
Post subject:  Re: GetGadgetItemImage()

While my previous posting in this thread presented a cross-platform procedure GetGadgetItemImage() for the ListIconGadget, the following example demonstrates a cross-platform procedure GetGadgetItemImage() for the TreeGadget. The Linux part is identical to the ListIconGadget example while in MacOS and Windows the code is different. Especially the MacOS part in the following example is a hack. Although it works without problems with PB 5.46 in ASCII and Unicode mode in Snow Leopard (x86 PB compiler) and Mojave (x64 PB compiler), the code is dependent on PureBasic internals and may break in future versions. Therefore only a native implementation would be future-proof...
Code:
EnableExplicit

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(TreeGadgetID.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(TreeGadgetID)
      SetGadgetState(TreeGadgetID, Row)
      *TreeColumn = gtk_tree_view_get_column_(GadgetID(TreeGadgetID), 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(TreeGadgetID, SelectedRow)
      g_list_free_(CellRendererList)
    CompilerCase #PB_OS_MacOS
      Protected Item.I
     
      Item = CocoaMessage(0, GadgetID(TreeGadgetID), "itemAtRow:", Row)
      Image = PeekI(PeekI(PeekI(Item + SizeOf(Integer))) + SizeOf(Integer) * 3)
    CompilerCase #PB_OS_Windows
      Protected Bitmap.BITMAP
      Protected IconHandle.I
      Protected IconInfo.ICONINFO
      Protected Item.TVITEM
     
      Item\hItem = GadgetItemID(TreeGadgetID, Row)
      Item\mask = #TVIF_IMAGE
      SendMessage_(GadgetID(TreeGadgetID), #TVM_GETITEM, 0, @Item)
      IconHandle = ImageList_GetIcon_(SendMessage_(GadgetID(TreeGadgetID),
        #TVM_GETIMAGELIST, #TVSIL_NORMAL, 0), Item\iImage, 0)
     
      If GetIconInfo_(IconHandle, IconInfo)
        If GetObject_(IconInfo\hbmMask, SizeOf(BITMAP), Bitmap)
          Image = CreateImage(#PB_Any, Bitmap\bmWidth, Bitmap\bmHeight, 32)
         
          If Image
            If StartDrawing(ImageOutput(Image))
              Box(0, 0, Bitmap\bmWidth, Bitmap\bmHeight, #White)
              DrawImage(IconHandle, 0, 0)
              StopDrawing()
              Image = ImageID(Image)
            Else
              FreeImage(Image)
              Image = 0
            EndIf
          EndIf
        EndIf
      EndIf
  CompilerEndSelect
 
  ProcedureReturn Image
EndProcedure

If LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/Drive.bmp") = 0
  MessageRequester("Error", "Unable to load image Drive.bmp")
  End
ElseIf LoadImage(1, #PB_Compiler_Home + "examples/sources/Data/File.bmp") = 0
  MessageRequester("Error", "Unable to load image File.bmp")
  End
EndIf

OpenWindow(0, 270, 100, 220, 155, "GetGadgetItemImage()")
TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 70)
AddGadgetItem(0, -1, "Item 1", ImageID(0))
AddGadgetItem(0, -1, "Subitem 1.1", 0, 1)
AddGadgetItem(0, -1, "Subitem 1.2", ImageID(1), 1)
SetGadgetItemState(0, 0, #PB_Tree_Expanded)
TextGadget(1, 10, GadgetHeight(0) + 20, 150, 25, "Grabbed image row 0:")
ImageGadget(2, 165, GadgetHeight(0) + 22, 16, 16, 0)
TextGadget(3, 10, GadgetHeight(0) + 45, 150, 25, "Grabbed image row 2:")
ImageGadget(4, 165, GadgetHeight(0) + 45, 16, 16, 0)

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

; ----- Draw grabbed images
SetGadgetState(2, GetGadgetItemImage(0, 0))
SetGadgetState(4, GetGadgetItemImage(0, 2))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Author:  Karig1965 [ Sun Jun 21, 2020 5:15 pm ]
Post subject:  Re: GetGadgetItemImage()

This is an old thread, but I also want to ask Fred and company to add GetGadgetItemImage() to PureBasic, especially if any solution that Shardik or somebody else comes up with will be dependent on PureBasic internals.

(I just tried to call GetGadgetItemImage() on a TreeGadget and got the message "[COMPILER] Line 113: GetGadgetItemImage() is not a function, array, list, map or macro." I'm using PureBasic 5.72 (64-bit) on Linux Mint 19.)

Please add this. Why have SetGadgetItemImage() but not GetGadgetItemImage()?

Author:  Shardik [ Sun Jun 21, 2020 8:42 pm ]
Post subject:  Re: GetGadgetItemImage()

Karig1965 wrote:
(I just tried to call GetGadgetItemImage() on a TreeGadget and got the message "[COMPILER] Line 113: GetGadgetItemImage() is not a function, array, list, map or macro." I'm using PureBasic 5.72 (64-bit) on Linux Mint 19.)

Did you try my TreeGadget example from above? I just tested it on Linux Mint Cinnamon 19.3 'Tricia' x64 with PB 5.72 x64 and it works like a charm.

Because GetGadgetItemImage() is currently not implemented in PureBasic 5.72, you have to copy the ImportC block and the procedure GetGadgetItemImage() into your own code before being able to call GetGadgetItemImage().

Author:  Karig1965 [ Sun Jun 21, 2020 8:55 pm ]
Post subject:  Re: GetGadgetItemImage()

Shardik wrote:
Karig1965 wrote:
(I just tried to call GetGadgetItemImage() on a TreeGadget and got the message "[COMPILER] Line 113: GetGadgetItemImage() is not a function, array, list, map or macro." I'm using PureBasic 5.72 (64-bit) on Linux Mint 19.)

Did you try my TreeGadget example from above? I just tested it on Linux Mint Cinnamon 19.3 'Tricia' x64 with PB 5.72 x64 and it works like a charm.

Because GetGadgetItemImage() is currently not implemented in PureBasic 5.72, you have to copy the ImportC block and the procedure GetGadgetItemImage() into your own code before being able to call GetGadgetItemImage().


I haven't tried it. I thought about it, but if there's a fair chance that your code may stop working with a future release of PureBasic, I'd rather have a solution that's more future-proof. (*shrug*) Images in the tree gadget are a "nice to have", not a hard requirement for my project, and I can live without them.

Thanks anyway.

Page 2 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/