xmlvb.netgladefreebasic

FreeBASIC GTK_TEXT_VIEW Example


Im New here and with no Idea of how do.

I Done many programs in VB.NET but only for Windows at moment I need to work in Linux too and the alternative is FreeBASIC and GTK for GUI.

Can someone make an example of How Use GTKTextView in FreeBASIC ?

This is a base where I want work, FreeBASIC Code with XML Glade


' START CONST '
' This part make a Const when can be used inside the program


CONST PROJ_NAME = "GTK_TEXT_VIEW" 


' END CONST '

' When you compile  the program he need path of external library 
' then you can  define the path inside the program
' If the case are (__FB_WIN32__) Mean Windows
    #IF DEFINED(__FB_WIN32__)
    #LIBPATH "C:\msys64\mingw64\lib" 
    
' If you use anothe OS with different Path then you can put here like
' this default path of Linux    
    #ELSE
    #LIBPATH "/usr/lib"
    #ENDIF

' END define Library PATH


' DEFINE the GTK Header where you can use the GTK Library

' This Line define the GTK Version 3.00
    #DEFINE __USE_GTK3__  

' Including the GTK Header 
    #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) 
    
'--------------------------------------------------------------------------'
' Now we ave to set the object to bind 

SCOPE
  VAR er = gtk_check_version_(3, 24, 0)
  IF er THEN
    ?"Error (GTK-Version):"
    ?*er
    END 1
  END IF
END SCOPE

DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR MainWindow, TextView_Left, TextView_Right, _
ButtonLeftToRight, ButtonRightToLeft, ButtonWriteInsideLeft, _
ButtonWriteInsideRight, ButtonExit


XML = gtk_builder_new()

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

MainWindow  = gtk_builder_get_object(XML, "MainWindow")
TextView_Left = gtk_builder_get_object(XML, "TextView_Left")
TextView_Right = gtk_builder_get_object(XML, "TextView_Right")
ButtonLeftToRight = gtk_builder_get_object(XML, "ButtonLeftToRight")
ButtonRightToLeft = gtk_builder_get_object(XML, "ButtonRightToLeft")
ButtonWriteInsideLeft = gtk_builder_get_object(XML, "ButtonWriteInsideLeft")
ButtonWriteInsideRight = gtk_builder_get_object(XML, "ButtonWriteInsideRight")
ButtonExit = gtk_builder_get_object(XML, "ButtonExit")

'End Define Var and Objects


'Now we ave to make the Handler

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



END SUB

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



END SUB

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



END SUB

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



END SUB

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

end

END SUB


    gtk_builder_connect_signals(XML, 0) 
    gtk_widget_show_all(GTK_WIDGET(MainWindow)) 
    gtk_main() 
    g_object_unref(XML) 

And here the XML Glade File

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkWindow" id="MainWindow">
    <property name="can-focus">False</property>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <child>
          <object class="GtkScrolledWindow" id="Scroll_Left">
            <property name="width-request">200</property>
            <property name="height-request">200</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkTextView" id="TextView_Left">
                <property name="visible">True</property>
                <property name="can-focus">True</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="x">5</property>
            <property name="y">25</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow" id="Scroll_Right">
            <property name="width-request">200</property>
            <property name="height-request">200</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkTextView" id="TextView_Right">
                <property name="visible">True</property>
                <property name="can-focus">True</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="x">220</property>
            <property name="y">25</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ButtonLeftToRight">
            <property name="label" translatable="yes">Left To Right</property>
            <property name="width-request">100</property>
            <property name="height-request">30</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_ButtonLeftToRight_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">10</property>
            <property name="y">230</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ButtonRightToLeft">
            <property name="label" translatable="yes">Right To Left</property>
            <property name="width-request">100</property>
            <property name="height-request">30</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_ButtonRightToLeft_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">120</property>
            <property name="y">230</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ButtonExit">
            <property name="label" translatable="yes">Exit</property>
            <property name="width-request">100</property>
            <property name="height-request">30</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_ButtonExit_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">320</property>
            <property name="y">270</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ButtonWriteInsideRight">
            <property name="label" translatable="yes">Write Right</property>
            <property name="width-request">100</property>
            <property name="height-request">30</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_ButtonWriteInsideRight_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">120</property>
            <property name="y">270</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="ButtonWriteInsideLeft">
            <property name="label" translatable="yes">Write Left</property>
            <property name="width-request">100</property>
            <property name="height-request">30</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
            <signal name="clicked" handler="on_ButtonWriteInsideLeft_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">10</property>
            <property name="y">270</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel">
            <property name="width-request">35</property>
            <property name="height-request">25</property>
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">Left</property>
          </object>
          <packing>
            <property name="x">5</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel">
            <property name="width-request">40</property>
            <property name="height-request">25</property>
            <property name="visible">True</property>
            <property name="can-focus">False</property>
            <property name="label" translatable="yes">Right</property>
          </object>
          <packing>
            <property name="x">220</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>


Thank you for your Help


Solution

  • Hello for read GTK_Text_View in FreeBASIC you have to use a syntax like C

    Before you have to set the Var and Pointer because you cant read directly the TextView but only the Buffer.

    Dim As String ReadSource_Text ' Variable where you put the text 
    Dim as zstring PTR WriteSource_Text ' Pointer where you store the Text Buffer
    
    DIM AS GtkTextBuffer PTR ReadBuffer ' Pointer to the GTK Text Buffer
    

    then you have to get the buffer from glade interface

    ReadBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Left))
    

    And now using the Iter for know the Offset where start and end the text you can extract the text from buffer

    DIM AS GtkTextIter start_, end_

    gtk_text_buffer_get_iter_at_offset(ReadBuffer, @start_, 0)
    gtk_text_buffer_get_iter_at_offset(ReadBuffer, @end_, -1)
    ' 0 is the start, and -1 are the end of the buffer 
    ' you can have a little part of the buffer like start 50 end 55
        'This store in ReadSource_Text var the buffer readed  
        ReadSource_Text = *gtk_text_buffer_get_text(ReadBuffer, @start_, @end_, TRUE)
    

    ReadSource_Text is a String variable with * before you can put text from a pointer

    ReadSource_Text = *gtk_text_buffer_get_text(ReadBuffer, @start_, @end_, TRUE)
    

    End Explanation.

    There you have your code with missing part: You can copy and paste inside to your program and compile for see how work

    'We start with the first Handler
    SUB on_ButtonLeftToRight_clicked CDECL ALIAS "on_ButtonLeftToRight_clicked" ( _
      BYVAL menuitem AS GtkMenuItem PTR, _
      BYVAL user_data AS gpointer) EXPORT
    dim MyString as String
    MyString = Read_Text( "left", "right", "")
    
    END SUB
    
    SUB on_ButtonRightToLeft_clicked CDECL ALIAS "on_ButtonRightToLeft_clicked" ( _
      BYVAL menuitem AS GtkMenuItem PTR, _
      BYVAL user_data AS gpointer) EXPORT
    dim MyString as String
    MyString = Read_Text( "right", "left", "")
    
    
    
    END SUB
    
    SUB on_ButtonWriteInsideLeft_clicked CDECL ALIAS "on_ButtonWriteInsideLeft_clicked" ( _
      BYVAL menuitem AS GtkMenuItem PTR, _
      BYVAL user_data AS gpointer) EXPORT
    
    dim MyString as String
    MyString = Read_Text( "intext", "left", "This is my Hello for")
    
    
    END SUB
    
    SUB on_ButtonWriteInsideRight_clicked CDECL ALIAS "on_ButtonWriteInsideRight_clicked" ( _
      BYVAL menuitem AS GtkMenuItem PTR, _
      BYVAL user_data AS gpointer) EXPORT
    
    dim MyString as String
    MyString = Read_Text( "intext", "right", "This is my Hello")
    
    END SUB
    
    SUB on_ButtonExit_clicked CDECL ALIAS "on_ButtonExit_clicked" ( _
      BYVAL menuitem AS GtkMenuItem PTR, _
      BYVAL user_data AS gpointer) EXPORT
    
    end
    
    END SUB
    
    'I created a function to avoid replicating long codes on each handler
    'I wrote the code fast and crude but I hope it's understandable.
    'The function is called with syntax MyString = Read_Text( "left", "right", "intext")
    Function Read_Text( ByVal Source as String, ByVal Target as String, ByVal InText As String) As String
    
    
    Dim As String ReadSource_Text ' Variable where you put the text 
    Dim as zstring PTR WriteSource_Text ' Pointer where you store the Text Buffer
    
    DIM AS GtkTextBuffer PTR ReadBuffer ' Pointer to the GTK Text Buffer
    
    ' This Condition are for select the source left, right, or intext your personal string
    if Source = "left" then ReadBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Left))
    if Source = "right" then ReadBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Right))
    
    
    
    
    
    ' if you want use your surce string instead  here the third option
    if Source = "intext" then
        'This store in ReadSource_Text var the text inside the var InText
        ReadSource_Text = InText
    Else
    ' Now we ave to set the Offset from buffer and then get the stored text
    DIM AS GtkTextIter start_, end_
    
    gtk_text_buffer_get_iter_at_offset(ReadBuffer, @start_, 0)
    gtk_text_buffer_get_iter_at_offset(ReadBuffer, @end_, -1)
    ' 0 is the start, and -1 are the end of the buffer 
    ' you can have a little part of the buffer like start 50 end 55
        'This store in ReadSource_Text var the buffer readed  
        ReadSource_Text = *gtk_text_buffer_get_text(ReadBuffer, @start_, @end_, TRUE)
    end if
    
    ' If you want use the text live string var then we ave to convert from Pointer to string
    WriteSource_Text = StrPtr(ReadSource_Text)
    
    ' Now we make a TextBuffer where whe use to write in target textview
    DIM AS GtkTextBuffer PTR WriteBuffer 
    
    if Target = "left" then WriteBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Left))
    if Target = "right" then WriteBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Right))
    if Target = "out" then Return ReadSource_Text ' if you want use the text out from function
    
    gtk_text_buffer_set_text(WriteBuffer, WriteSource_Text, -1) ' and the we store the text inside the textbuffer
    
    
    
    End Function