[Gambas-user] Set/GetProperty/Find Method feature request

Peter Bauer pebauer68 at gmail.com
Sat May 23 20:26:53 CEST 2020


This doesn't have any sense at all in Gambas.
What do you want to achieve?

I already wrote a small Gambas App (gui_server)
https://gambas.one/gambasfarm/?id=821&action=search
https://forum.crystal-lang.org/t/state-plans-of-gui-development-with-crystal/1615/28
which allows to use many Gambas GUI objects from other scripting languages
like Ruby, Python and Crystal(compiled Ruby like language).
Currently I walk the controls and classes with a for each loop to make all
Objects and Properties available to GUI clients(TCP socket connection) by
their Name. But I  am not happy with the performance of my code. I started
with the Controls TextArea and Buttons, some events already work fine.

Example code in crystal language:
#connect to Gambas GUI via TCP
require "socket"
GUI_SERVER="localhost"
GUI_PORT=9090

class GuiClient
  property send_time = true
  @client = TCPSocket.new    #define var
  def connect
    @client = TCPSocket.new(GUI_SERVER,GUI_PORT)
  rescue err
    puts err.message
    exit
  end

def send (object,value)
    puts "send(object,value)"
    @client << object + "," + value + "\n"  #send to GUI_SERVER
    sleep 0.1
end

def send (object)
  puts "send(object)"
  @client << object + "\n"  #send to GUI_SERVER
  sleep 0.1
end

def disconnect
  @client.close
end

def receive  #handle events

spawn do
        loop do
            event = @client.gets
            puts event
            case event
            when "Button1_clicked"  #stop button
              @send_time=false
            when "Button2_clicked"  #continue button
              @send_time=true
            when "Button3_clicked"  #clear textarea button
              send "textarea1.text",""
            when "Button4_clicked"  #upcase textarea.text
              send "Textarea1.text" #request this value from gui elemen
              response = @client.gets
              puts "Response: ",response
              send "Textarea1.text",(response.not_nil! + "\n").upcase  #do
upcase on string in textarea1
            when "Button5_clicked"  #upcase textarea.text
              send "Textarea1.text" #request this value from gui element
              response = @client.gets
              puts "Response: ",response
              send "Textarea1.text",(response.not_nil! + "\n").downcase
 #do downcase on string in textarea1
            when "FMain_closed"     #Appilcation Window closed by user
            exit
            else
            end
        end
    end
  end
end
#start the gambas gui server demo app
spawn do; system("~/src_gambas/gui_server/gui_server.gambas"); end
sleep 2   #give some time for startup
mygui = GuiClient.new
mygui.connect
mygui.receive
mygui.send "textlabel1.text","crystal + gambas = crybas"
mygui.send "button1.text","stop"
mygui.send "button2.text","continue"
#mygui.send "button3.enabled","0"  #disable a button
mygui.send "button3.text","clear texrarea"
mygui.send "button4.text","do Text.upcase"
mygui.send "button5.text","do Text.downcase"
#send key,value pairs for GUI Objects based on GTK or QT
mygui.send "FMain.Text","Crybas"   #set title of Application
mygui.send "Textarea1.Text","hello-textarea"

loop do   #Error writing to socket: Broken pipe when server closes
       mygui.send "Textbox1.Text",Time.local.to_s if mygui.send_time #
display time in the gui
       sleep 1
    end

sleep

Simple example:
I want to be able to query any Object like TextBox1.Text for the
current String value.

BR,
Peter

Object introspection in Gambas code:

Public Sub full_instrospect(clsname As String)
   Dim cls As Class
   Dim count, pos As Integer
   Dim oo As Object
   Dim string$, result As String
   Dim propname, objname, clsn As String

   'lets see which class it could be   'textbox1.text
   Print "clsname: "; clsname
   pos = InStr(clsname, ".")
   If pos > 0 Then
        clsn = Left$(clsname, pos - 1)                         '
button1.foreground\n
        propname = Trim$(Right$(clsname, Len(clsname) - pos))  ' foreground
        objname = clsn
        clsname = Left$(clsn, -1)     'remove number           ' button1

        Else
        clsname = Trim$(clsname)
        If IsNumber(Right$(clsname)) Then
        clsname = Left$(clsname, -1)  'remove number           ' button1
        Endif
      Endif

   'cls = Class.load("Textarea")
   Try cls = Class.Load(clsname)
   If Error Then
     Print "full_instrospect() "; Error.Text
     Print #rsock, "full_instrospect() "; Error.Text
     Return
   Endif

   Print "Controls: "; Me.Controls.Count            'number of controls

   For Each oo In Me.Controls
        'Print "Current object: "; oo.name
        If Not propname Then Print #rsock, " "; Trim$(oo.name)
        If UCase$(oo.name) = UCase$(clsname)
           Print "Found cls object: "; clsname
           Break
        Else If UCase$(oo.name) = UCase$(objname)
           Print "Found cls.object object: "; clsname; " "; objname
           Try result = Object.GetProperty(oo, propname)
           If Error Then
               Print #rsock, "full_instrospect() "; Error.Text
               Return
           Endif
           Print #rsock, result
           Return          ' found button1.foreground property
        Else
           'Print "full_introspec(Object): "; clsname; " not matching"
        Endif
  Next

'oo = Me.Children[0]     'ref to instance of TextArea1, this app has just
one control which is a Textarea
  Print oo                '(TextArea 0x559201bb78c8)
  Print oo.Name; ":"      'TextArea1
  Textarea1_object = oo   'set object ref in a global var

  count = 0
  For Each string$ In cls.Symbols      'iterate over all symbols
     Print string$; ",";
     Print #rsock, string$; ",";
     count += 1
  Next
  Print "\nSymbols found: "; count; "\n"
  Print #rsock, "\nSymbols found: "; count; "\n"
  count = 0

 For Each string$ In cls.Symbols
         Print "\nTRY: "; count; " ";
         If Not objname Then Print #rsock, "\nTRY: "; clsname; " "; count;
" ";
         Try result = Object.GetProperty(oo, string$)
             If result Not Begins ":" 'is an event !
                    Print "Symbol: "; string$; " Value: "; result; "";
                    If Not objname Then Print #rsock, "Symbol: "; string$;
" Value: "; result; "";
                    Endif
         If Error Then
           Print " Error: "; Error.code; " "; Error.Text; " ";
           Print #rsock, " Error: "; Error.code; " "; Error.Text; " ";
         Endif
         count += 1
     Next
     Print #rsock, "\n"
 End
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20200523/4365c176/attachment-0001.html>


More information about the User mailing list