Recent

Author Topic: gtk3 and gdk_pixbuf_save()  (Read 2500 times)

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
gtk3 and gdk_pixbuf_save()
« on: October 23, 2019, 10:06:52 am »
Thought I'd have a little play with GTK3, particularly the TrayIcon.  Initially appeared quite easy to get it to work with LibAppIndicator but I have run into problems with the function gdk_pixbuf_save() that is derived from libgdk_pixbuf-2.0.so.0. Same library in both gtk2 and gtk3.

In gtk2 its accessed via Gdk2Pixbuf unit from -
 /usr/share/fpcsrc/3.0.4/packages/gtk2/src/gtk+/gdkpixbuf/gtk2pixbuf.pas

In gtk3 I  choose to 'use' LazGdkPixbuf2 unit from -
lcl/interfaces/gtk3/gtk3bindings/lazgdkpixbuf2.pas

and I wonder if thats was a mistake. Not wanting to bore everyon with TrayIcon issues, I reduced it down to the following code, its a form with nothing more than a TrayIcon that has had a icon and a menu assigned in the Object Inspector 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonFiddleIconClick(Sender: TObject);
  2. var
  3.   NewIcon: Pointer;
  4. begin
  5.   NewIcon := {%H-}Pointer(TrayIcon1.Icon.Handle);
  6.   gdk_pixbuf_save(NewIcon, PChar('icon.png'), 'png', nil, [nil]);
  7. end;
  8.  
Under GTK2 it works fine, writes out a png file. In GTK3 the gdk_pixbuf_save fails and we see a message -
Code: Pascal  [Select][+][-]
  1. gtk3test:596): GdkPixbuf-CRITICAL **: 18:06:31.164: gdk_pixbuf_savev: assertion 'GDK_IS_PIXBUF (pixbuf)' failed
It does not like my icon, apparently does not believe its a pixbuf. Apart from  gdk_pixbuf_save() is there some other way to dump the icon out to have a look at it ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: gtk3 and gdk_pixbuf_save()
« Reply #1 on: October 23, 2019, 10:54:35 am »
Handle is wrong in your case. It points to TObject not to PGdkPixbuf, you should use
TGtk3Image(TrayIcon1.Icon.Handle).Handle

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: gtk3 and gdk_pixbuf_save()
« Reply #2 on: October 23, 2019, 11:22:36 am »
Yep, thats it !  One more step....

Thanks zeljko !
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: gtk3 and gdk_pixbuf_save()
« Reply #3 on: October 23, 2019, 12:38:57 pm »
OK, struck another hard one. And not one I can bring out and identify....

Further in, we pass a menu to the LibAppIndicator3 using this function -

Code: Pascal  [Select][+][-]
  1. app_indicator_set_menu: procedure(self: PAppIndicator; menu: PGtkMenu); cdecl;
         
like this -
Code: Pascal  [Select][+][-]
  1. app_indicator_set_menu(GlobalAppIndicator, {%H-}PGtkMenu(FTrayIcon.PopUpMenu.Handle));
Where GlobalAppIndicator is a pointer to the AppIndicator itself (and thats OK) and then we pass a pointer to the menu handle. This generates -

Code: Pascal  [Select][+][-]
  1. (gtk3test:6832): libappindicator-CRITICAL **: 21:20:18.919: app_indicator_set_menu: assertion 'GTK_IS_MENU (menu)' failed
Again, its wanting the menu in some GTK3 format ?  I tried Pointer(TGTK3Menu(FTrayIcon.PopUpMenu.Handle)) but I am only guessing and guessing does not work !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: gtk3 and gdk_pixbuf_save()
« Reply #4 on: October 23, 2019, 02:39:27 pm »
TGtk3Widget
  TGtk3MenuShell
     TGtk3Menu
                      (FTrayIcon.PopUpMenu.Handle).Widget 

Just check first what is TGtk3 class.
writeln('TrayIcon popupMenu class is ',TGtk3Widget(FTrayIcon.PopUpMenu.Handle).ClassName);
and then use eg:
  TGtk3Menu(FTrayIcon.PopUpMenu.Handle).Widget <- note Widget , it's Gtk3 handle

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: gtk3 and gdk_pixbuf_save()
« Reply #5 on: October 24, 2019, 11:44:46 am »
Yep, thats it zeljko, I still needed to cast it with a PGtkMenu( but that works and now so does the whole thing.  I'll need to do some more tests but looks like I can make a patch to add TrayIcon to GTK3, excellent !

Thanks for your help.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018