Shared library acces (GTK + SCINTILLA)

Linux specific questions.
ERICMAUDOUIT
Posts: 9
Joined: Mar 07, 2018 23:01

Shared library acces (GTK + SCINTILLA)

Postby ERICMAUDOUIT » Mar 04, 2021 14:07

Hello

I started trying to make scintilla work on a GTK app.
I am on linux (UBUNTU 20.10)
So I recovered scintilla and I compiled it (without any problem)
I installed 'libscintilla.so' in the directory of my application 'application / lib /'

Code: Select all

   '--------------------------------------------------------------------------
   #include once "gtk/gtk3.bi"   
   #include once "gdk/gdk3.bi"   
   '--------------------------------------------------------------------------
   #libpath "lib/"
   '#inclib "lexilla"
   #inclib "scintilla"
   '--------------------------------------------------------------------------
   #include once "include/scintilla_defines.bi"   
   #include once "include/scintilla.bi"   
   '--------------------------------------------------------------------------
   #define UNUSE_ATTR   __attribute__((unused))
   #define SSM(m, w, l) scintilla_send_message(sci, (m), (w), (l))
   
   SUB sci_setup(sci as ScintillaObject ptr)
      'scintilla_set_id(sci, 0)
      
      'SSM(SCI_SETCODEPAGE, SC_CP_UTF8, 0)
      'SSM(SCI_SETIMEINTERACTION, SC_IME_WINDOWED, 0)
      ''SSM(SCI_SETIMEINTERACTION, SC_IME_INLINE, 0)

      'SSM(SCI_STYLESETCHARACTERSET, STYLE_DEFAULT, SC_CHARSET_DEFAULT)
      ''SSM(SCI_STYLESETFONT, STYLE_DEFAULT, (sptr_t)"Monospace")
      'SSM(SCI_STYLESETFONT, STYLE_DEFAULT, cast(sptr_t, "Monospace"))
      'SSM(SCI_STYLESETSIZEFRACTIONAL, STYLE_DEFAULT, 11*SC_FONT_SIZE_MULTIPLIER)
      'SSM(SCI_STYLECLEARALL, 0, 0)

      'SSM(SCI_SETTABWIDTH, 4, 0)
      ''// show line number margin
      'SSM(SCI_SETMARGINWIDTHN, 0, (int)SSM(SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t)" 99"))
      '' hide bookmark and selection margin
      'SSM(SCI_SETMARGINWIDTHN, 1, 0)
      '' word wrap
      'SSM(SCI_SETWRAPMODE, SC_WRAP_WORD, 0)
      'SSM(SCI_SETWRAPVISUALFLAGS, SC_WRAPVISUALFLAG_END, 0)
      'SSM(SCI_SETWRAPINDENTMODE, SC_WRAPINDENT_INDENT, 0)

      'SSM(SCI_SETLEXER, SCLEX_CPP, 0)
      'SSM(SCI_SETKEYWORDS, 0, (sptr_t)"int char return")
      'SSM(SCI_STYLESETFORE, SCE_C_PREPROCESSOR, 0x0080ff)
      'SSM(SCI_STYLESETFORE, SCE_C_COMMENT, 0x008000)
      'SSM(SCI_STYLESETFORE, SCE_C_COMMENTLINE, 0x008000)
      'SSM(SCI_STYLESETFORE, SCE_C_NUMBER, 0x808000)
      'SSM(SCI_STYLESETFORE, SCE_C_WORD, 0xff0000)
      'SSM(SCI_STYLESETFORE, SCE_C_STRING, 0x800080)
      'SSM(SCI_STYLESETBOLD, SCE_C_OPERATOR, TRUE)
      'SSM(SCI_SETPROPERTY, (uptr_t)"styling.within.preprocessor", (sptr_t)"1")

      'SSM(SCI_INSERTTEXT, 0, (sptr_t)
                '"#include <gtk/gtk.h>\n"
                '"\n"
                '"int main(int argc, char **argv) {\n"
                '"   // show GTK version\n"
                '"   printf(\"GTK %d.%d.%d\\n\", GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);\n"
                '"   return 0;\n"
                '"}\n"
                '"\n"
               ')

      'char buf[128]
      'int length = snprintf(buf, sizeof(buf) - 1, "// GTK %d.%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION)
      'SSM(SCI_APPENDTEXT, length, (sptr_t)buf)
      
   END SUB
   
   FUNCTION exit_app(widget as GtkWidget ptr, _
                 event_ as GdkEvent ptr, _
                 user_data as gpointer) AS boolean
      gtk_main_quit()
      
      RETURN false
   END FUNCTION
   
   gtk_init(NULL, NULL)
   dim as GtkWidget ptr win
   dim as GtkWidget ptr editor
   
   win = gtk_window_new(GTK_WINDOW_TOPLEVEL)
   g_signal_connect(G_OBJECT(win), "delete_event", G_CALLBACK(@exit_app), NULL)

   gtk_window_set_title(GTK_WINDOW(win), "Scintilla GTK Test")
   gtk_window_set_default_size(GTK_WINDOW(win), 500, 300)

   editor = scintilla_new()
   gtk_container_add(GTK_CONTAINER(win), editor)
   'sci_setup(SCINTILLA(editor))
   
   gtk_widget_show_all(win)
   gtk_widget_grab_focus(editor)
   
   gtk_main()

   end 0


I compile with FREEBASIC (last version 1.07.02)
successful compilation

but when i execute the program

./testscintilla: error while loading shared libraries: libscintilla.so: cannot open shared object file: No such file or directory


------------------
(program exited with code: 127)
Press return to continue



library name problem?
directory?
way?
other library?
other?

Thank you in advance for your help
Cretin Ho
Posts: 167
Joined: Feb 04, 2021 13:01

Re: Shared library acces (GTK + SCINTILLA)

Postby Cretin Ho » Mar 04, 2021 14:18

You should try LD_LIBRARY_PATH.

LD_LIBRARY_PATH="path/to/the/directory/contains/your/scintilla/.so/file" ./testscintilla
Xusinboy Bekchanov
Posts: 393
Joined: Jul 26, 2018 18:28

Re: Shared library acces (GTK + SCINTILLA)

Postby Xusinboy Bekchanov » Mar 04, 2021 14:29

My advice: Study how Scintilla is used in geany, use it.
ERICMAUDOUIT
Posts: 9
Joined: Mar 07, 2018 23:01

Re: Shared library acces (GTK + SCINTILLA)

Postby ERICMAUDOUIT » Mar 04, 2021 16:13

Cretin Ho wrote:You should try LD_LIBRARY_PATH.

LD_LIBRARY_PATH="path/to/the/directory/contains/your/scintilla/.so/file" ./testscintilla


Thanks for your help but its run only in GTK2

i must search

(testscintilla:6141): Gtk-ERROR **: 17:06:03.813: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
Trace/breakpoint trap (core dumped)


------------------
(program exited with code: 133)
Press return to continue
ERICMAUDOUIT
Posts: 9
Joined: Mar 07, 2018 23:01

Re: Shared library acces (GTK + SCINTILLA)

Postby ERICMAUDOUIT » Mar 04, 2021 16:25

I found the solution by reading the documentation. (as if by chance)

by building with 'make GTK3 = 1' the library is accessible with GTK3

the magic of perseverance
ERICMAUDOUIT
Posts: 9
Joined: Mar 07, 2018 23:01

Re: Shared library acces (GTK + SCINTILLA)

Postby ERICMAUDOUIT » Apr 08, 2021 15:46

Hello
I'm looking for information again
I access 'SCINTILLA 4.4.6' without any problem, finally not on one point and not least, when I want to define a LEXER by 'SCI_SETLEXER'

Code: Select all

   '--------------------------------------------------------------------------
   #include once "gtk/gtk3.bi"   
   #include once "gdk/gdk3.bi"   
   '--------------------------------------------------------------------------
   #include once "include/scintilla.bi"   
   #include once "include/scintilla_defines.bi"   
   '--------------------------------------------------------------------------

   FUNCTION exit_app(w as GtkWidget ptr, e as GdkEventAny ptr, p as gpointer) AS integer
      gtk_main_quit()
      
      RETURN 0
   END FUNCTION

   dim as GtkWidget ptr       app
   dim as GtkWidget ptr       editor
   dim as ScintillaObject ptr   sci
   dim as integer          p

   gtk_init(0, 0)
   app = gtk_window_new(GTK_WINDOW_TOPLEVEL)
   gtk_window_set_position(GTK_WINDOW (app), GTK_WIN_POS_CENTER)
   gtk_window_set_default_size(GTK_WINDOW (app), 1200, 600)
   gtk_window_set_title (GTK_WINDOW (app), "My Notebook")
   editor = scintilla_new()
   sci = SCINTILLA(editor)

   gtk_container_add(GTK_CONTAINER(app), editor)
   g_signal_connect(G_OBJECT(app), "delete_event", G_CALLBACK(@exit_app), 0)

   scintilla_set_id(sci, 0)
   'gtk_widget_set_usize(editor, 500, 300)

   '#define SSM(m, w, l) scintilla_send_message(sci, m, w, l)

   SSM(SCI_STYLECLEARALL, 0, 0)
   
         p = scintilla_send_message (SCINTILLA(sci), SCI_GETLEXER, 0, 0)
         print p      'systematically returns a zero
         
   
         scintilla_send_message (SCINTILLA(sci), SCI_SETLEXER, SCLEX_FREEBASIC, 0)
         
         
         p = scintilla_send_message (SCINTILLA(sci), SCI_GETLEXER, 0, 0)
         print p       'systematically returns a zero
         
   dim as string   texte
   texte = "int main(int argc, char **argv) "
   SSM(SCI_INSERTTEXT, 0, cast(sptr_t, strptr(texte)))

   gtk_widget_show_all(app)
   gtk_widget_grab_focus(GTK_WIDGET(editor))
   gtk_main()



I tried with various other lexers and ditto
either I forgot something or?
in advance thank you for any help

finally I have my solution with 'DyLibLoad' 'DyLibSymbol' and 'DyLibFree'

Code: Select all

   '-----------------------------------------------------------------
   #define typeprocedureliblexilla Function _
                  (byval langage_ as wstring ptr) As uinteger ptr

   const constnomroutinecreatalexer = "CreateLexer"
   const constnomlibrairielexilla = "liblexilla_501.so"
   '-----------------------------------------------------------------
   Dim shared As Any Ptr                   librairielexilla
   dim shared as boolean                  librairielexillaOK
   Dim shared AS typeprocedureliblexilla       routinecreatelexer
         
   Dim shared As uinteger Ptr               lexerfreebasic
         
   '-----------------------------------------------------------------
   
   FUNCTION LOC_installlibrairielexilla() AS boolean
      librairielexillaOK = false
      librairielexilla = DyLibLoad(constnomlibrairielexilla)
      
      IF (librairielexilla) THEN
         routinecreatelexer = DyLibSymbol(librairielexilla, _
                              constnomroutinecreatalexer)
         IF (routinecreatelexer) THEN
            librairielexillaOK = true
            
            RETURN true
         END IF
      END IF
      
      RETURN false
   END FUNCTION

   LOC_installlibrairielexilla()
   
   
   IF (librairielexillaOK) THEN
      dim as string tmp = "freebasic"
      lexerfreebasic = routinecreatelexer (cast(wstring ptr,strptr(tmp)))
      
      print lexerfreebasic
            
      'scintilla_send_message(sci, SCI_SETILEXER, 0, lexerfreebasic)
   END IF

   DyLibFree(librairielexilla)

   '-----------------------------------------------------------------

ERICMAUDOUIT
Posts: 9
Joined: Mar 07, 2018 23:01

Re: Shared library acces (GTK + SCINTILLA)

Postby ERICMAUDOUIT » May 10, 2021 22:35

Hello good morning,

with this code

Code: Select all

   SUB EDITEUR.LOC_gestionassistanceedition( _
                     sci as ScintillaObject ptr, _
                     notify as SCNotification ptr)
      
      IF (notify->nmhdr.code = SCN_CHARADDED) THEN
         SELECT CASE notify->ch
            CASE 10
               dim as integer    poscurseurcourante
               dim as integer    lignecourante
               dim as integer    indentligneprec
               dim as string   mot
               
               poscurseurcourante = SSM(SCI_Getcurrentpos, 0, 0)
               lignecourante = SSM(SCI_linefromposition, poscurseurcourante, 0)
               poscurseurcourante = SSM(SCI_POSITIONFROMLINE, lignecourante - 1, 0)
               
               indentligneprec = SSM(SCI_getlineindentation, lignecourante - 1, 0)
               
               LOC_trouvemot(sci, poscurseurcourante, mot)
               
               print mot
               
               
         END SELECT
      END IF
   END SUB

   sub EDITEUR.LOC_trouvemot( _
                  sci as ScintillaObject ptr, _
                  iCurPos as Integer, _
                  mot as string)
      Dim As long             posdebutmot
      Dim As long             posfinmot
      Dim As SCI_TEXTRANGE      tr
      dim as string            tempo   
      dim as integer            l
      
      posdebutmot = (SSM(SCI_WORDSTARTPOSITION, iCurPos, true))
      posfinmot = (SSM(SCI_WORDENDPOSITION, iCurPos, true))
      
      If posfinmot > posdebutmot Then
         tr.chrg.cpMin = posdebutmot
         tr.chrg.cpMax = posfinmot
         l = posfinmot - posdebutmot + 1
         tempo = string(l, " ") + chr(0)
         tr.lpstrText = (strptr(tempo))
            
         l = SSM(SCI_GETTEXTRANGE, 0, cast(uptr_t, varptr(tr)))
            
         mot = UCASE(trim(*tempo , any chr(13) & chr(10) & chr(9) ))
      EndIf
   End sub


when the code is executed apparently on the line

Code: Select all

l = SSM (SCI_GETTEXTRANGE, 0, cast (uptr_t, varptr (tr)))
 


trigger an error

Segmentation fault (core dumped)

I can't determine if my mistake is

Code: Select all

tr.lpstrText = (strptr (tempo))

or

l = SSM (SCI_GETTEXTRANGE, 0, cast (uptr_t, varptr (tr)))


an idea thank you
badidea
Posts: 2307
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Shared library acces (GTK + SCINTILLA)

Postby badidea » May 10, 2021 23:03

varptr cannot be right.
From the wiki: "When the operand is of type String, the address of the internal string descriptor is returned. Use Operator Strptr (String pointer) to retrieve the address of the string data."
It is unlikely that Scintilla knows the freebasic string descriptor layout.

Also, it was my understanding the freebasic adds a chr(0) automatically. So in 'tempo = string(l, " ") + chr(0)' the chr(0) should not be needed.

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 4 guests