FreeBASIC GTK_TEXT_VIEW 示例

FreeBASIC GTK_TEXT_VIEW Example

我是新来的,不知道该怎么做。

我在 VB.NET 中完成了很多程序,但目前只用于 Windows 我也需要在 Linux 中工作,替代方案是用于 GUI 的 FreeBASIC 和 GTK。

有人可以举例说明如何在 FreeBASIC 中使用 GTKTextView 吗?

这是我想要工作的基地,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) 

这里是 XML Glade 文件

<?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>


感谢您的帮助

你好阅读 GTK_Text_View 在 FreeBASIC 中你必须使用类似 C

的语法

之前你必须设置 Var 和 Pointer 因为你不能直接读取 TextView 而只能读取 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

然后你必须从 glade 接口获取缓冲区

ReadBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(TextView_Left))

现在使用 Iter 了解文本开始和结束的偏移量,您可以从缓冲区中提取文本

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 是一个带有 * 的字符串变量,然后才能从指针中放置文本

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

解释结束。

您的代码缺少部分: 您可以复制并粘贴到您的程序中并编译以查看工作方式

'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