Solved: GtkImage tutorial example

Windows specific questions.
Post Reply
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Solved: GtkImage tutorial example

Post by exagonx »

Hi, for work reasons I had to abandon FreeBASIC for a while now I have started programming again, I use GTK as a framework because it is the same on linux and windows, in this study I am trying to understand how to use GTKImage but the examples I find are all in C ++ and Python, I'm not familiar with one or the other, is there anyone who can help me understand how to use this GTK Widjet with FreeBASIC?

I want use a button for load or use the function "Command" for command line prompt where I want type "ImageView image.png"

Its some one can explain how use GTKImage ?

This is the BAS Code ImageView.bas

Code: Select all

CONST PROJ_NAME = "imageview" 
CONST PROJ_DESC = "Unknown"
CONST PROJ_VERS = "0.1" 
CONST PROJ_YEAR = "2020"
CONST PROJ_AUTH = "exagonx" 
CONST PROJ_MAIL = "exagonx@hotmail.com" 
CONST PROJ_WEBS = "http://exagonx.altervista.org" 
CONST PROJ_LICE = "Unknown" 





	#IF DEFINED(__FB_WIN32__)
	#LIBPATH "C:\msys64\mingw64\lib" 
	#ELSE
	#LIBPATH "/usr/lib"
	#ENDIF
    
    
    #DEFINE __USE_GTK3__ 
    #INCLUDE "gtk/gtk.bi"
    gtk_init(@__FB_ARGC__, @__FB_ARGV__)
    #INCLUDE "libintl.bi" 
    bindtextdomain(PROJ_NAME, EXEPATH & "/locale")
    bind_textdomain_codeset(PROJ_NAME, "UTF-8")
    textdomain(PROJ_NAME) 

 
    'Load UI from XML File '
    
SCOPE
  VAR er = gtk_check_version_(3, 0, 0)
  IF er THEN
    print " Error (GTK-Version):"
    print *er
    END 1
  END IF
END SCOPE

DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
  ImageView, Button_Close, ShowImage

XML = gtk_builder_new()

SCOPE
DIM AS GError PTR meld
IF 0 = gtk_builder_add_from_file(XML, "ImageView.ui", @meld) THEN
  WITH *meld
    print "Error (GTK-Builder):"
    print *.message
  END WITH
  g_error_free(meld)
  END 2
END IF
END SCOPE

ImageView = gtk_builder_get_object(XML, "ImageView")
Button_Close = gtk_builder_get_object(XML, "Button_Close")
ShowImage = gtk_builder_get_object(XML, "ShowImage")

SUB on_Button_Close_clicked CDECL ALIAS "on_Button_Close_clicked" ( _
  BYVAL button AS GtkButton PTR, _
  BYVAL user_data AS gpointer) EXPORT

end

END SUB

    
    
    
    gtk_builder_connect_signals(XML, 0) 
    gtk_widget_show_all(GTK_WIDGET(ImageView)) 
    gtk_main() 
    g_object_unref(XML) 
This is the UI code ImageView.ui

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="ImageView">
    <property name="name">ImageView</property>
    <property name="can_focus">False</property>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkLayout">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="Button_Close">
            <property name="label" translatable="yes">Close</property>
            <property name="name">Button_Close</property>
            <property name="width_request">100</property>
            <property name="height_request">40</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_Button_Close_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">362</property>
            <property name="y">257</property>
          </packing>
        </child>
        <child>
          <object class="GtkImage" id="ShowImage">
            <property name="name">ShowImage</property>
            <property name="width_request">400</property>
            <property name="height_request">200</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="stock">gtk-missing-image</property>
          </object>
          <packing>
            <property name="x">5</property>
            <property name="y">30</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Thank you for help
Last edited by exagonx on Nov 12, 2020 17:32, edited 3 times in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: GtkImage tutorial example

Post by Tourist Trap »

Hello,

I've no gtk, and unfortunately no time here to install anything for testing your try. Probably somebody will do in a few time. Anyway reading your code I came accross a typo:

Code: Select all

WITH *meld
    print "Error (GTK-Builder):"
    print *.message

I'm pretty sure that you meant "PRINT .message", not "PRINT *.message".
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GtkImage tutorial example

Post by exagonx »

Tourist Trap wrote:Hello,

I've no gtk, and unfortunately no time here to install anything for testing your try. Probably somebody will do in a few time. Anyway reading your code I came accross a typo:

Code: Select all

WITH *meld
    print "Error (GTK-Builder):"
    print *.message

I'm pretty sure that you meant "PRINT .message", not "PRINT *.message".
Thank you for your Interest This part of Code work correctly and you have to use the *.message or not work .

this code is Object oriented .

I have asked a example of how use a GTK Widget at moment this code work perfectly.
exagonx
Posts: 314
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: GtkImage tutorial example

Post by exagonx »

Thank you for anyone was interessed, I found a way from a friend in Stackoverflow

adding a button with name "Button_ShowImage" with Glade
then add a signal in Clicked "on_Button_ShowImage_Clicked"

Code: Select all

'declare the button
DIM SHARED AS GObject PTR Button_ShowImage

'Before Sub'
Button_ShowImage = gtk_builder_get_object(XML, "Button_ShowImage")

SUB on_Button_ShowImage_clicked CDECL ALIAS "on_Button_ShowImage_clicked" ( _
  BYVAL menuitem AS GtkMenuItem PTR, _
  BYVAL user_data AS gpointer) EXPORT

'this function load the selected file like image'
gtk_image_set_from_file(GTK_IMAGE(ShowImage), "myfile.png")

END SUB
This way work correctly

I leave the Post Open in case some one want add something better than mine.
Post Reply