The Crystal Programming Language Forum

GUI scripting

The Gambas project has a nice jitted scripter which can be called from crystal for small GUIs, here just a textarea. Lets see if we can talk to this ‘alien’ objects and types in a more direct way.
Should we call this crybas ? Hope it is useful.


#Define GUI in an heredoc
gui =<<-'GUI'
use "gb.form"
DIM Line$ As String
DIM myForm AS Form
With myForm = New Form
   .Title = "Text Viewer" 
   .Arrangement = Arrange.Fill 
   .AutoResize = False
   .width = 600
   .height = 300
   .show
End with
DIM myText As New TextArea(myForm)
file.in.blocking = false
DO
  Line$ = Read -1024       'read up to 1024 Bytes from stdin
  myText.Insert(Line$)     'insert text into text area
  wait 0.01                'give time to Gambas events
LOOP
GUI
File.write("./gui.gbs",gui)
spawn do; begin  
    cmd = "gbs3 ./gui.gbs"
    Process.run(cmd, shell: true) do | p |
         p.input.puts("hello textarea")     
         p.input.puts("crystal+gambas = crybas")                
       end 
    end
end
sleep
1 Like

It would probably be more efficient to write some C bindings to it (if possible) rather than spawning a sub process. Given most higher level languages can do this I’m not sure what you’re trying to show here? Not really getting any benefit from using Crystal in this case.

The benefit could be:
-to re-use a VB like GUI designer for crystal projects
-or just script small GUIs when you do not need the GUI designer(just 2 or 3 GUI elements)
Crystals semantik and syntax is much more compact and modern. I dont want to re-envent the wheel. I need an object bridge between the crystal <-> gambas side. I can imagine to write a text based object bridge(key,value pairs) for GUI objects and GUI events based on sockets or pipes in some evenings.

Setting Properties of a Gambas GUI Object like: Textarea1.Text=“some text”
already works. This is done by doing live object introspection by the received object name e.g. a Textarea has 106 symbols for properties,methods and events

Received data <–
Textarea1.Text,2020-05-21 11:46:37 +02:00
Key,val: Textarea1.Text 2020-05-21 11:46:37 +02:00

Objname: Textarea1
Prop: Text
(TextArea 0x55e41302e6c8)
TextArea1:
.,H,W,X,Y,Id,Cut,Pos,Tag,Top,_new,Copy,Drag,Drop,Font,Grab,Hide,Left,Line,Move,Name,Next,Redo,Show,Text,Undo,Wrap,:Drag,:Drop,:Menu
,Clear,Lower,Mouse,Paste,Proxy,Raise,ToPos,Width,:Enter,:Leave,Action,Border,Column,Cursor,Delete,Design,Expand,Handle,Height,Ignor
e,Insert,Length,Parent,Resize,Select,ToLine,Window,:Change,:Cursor,_Family,_Parent,Enabled,Hovered,Refresh,ScreenX,ScreenY,Tooltip,
Visible,:MouseUp,_Similar,CursorAt,HasFocus,Previous,ReadOnly,Reparent,Selected,SetFocus,ToColumn,Tracking,Unselect,:DblClick,:Drag
Move,:GotFocus,:KeyPress,Alignment,PopupMenu,ScrollBar,SelectAll,Selection,:DragLeave,:LostFocus,:MouseDown,:MouseDrag,:MouseMove,_
IsControl,Background,Foreground,MoveScaled,NoTabFocus,:KeyRelease,:MouseWheel,_Properties,_DefaultSize,ResizeScaled,_DefaultEvent,E
nsureVisible,
Symbols found: 106

Events for Button pressed are working, I just send “Button nn Clicked” to crystal.

#connect to Gambas GUI via TCP
require "socket"
GUI_SERVER="localhost"
GUI_PORT=9090
class GuiClient
  def send (object,value)
    client = TCPSocket.new(GUI_SERVER,GUI_PORT)
    client << object + "," + value + "\n"
    response = client.gets
    client.close
    puts 
  end    
end
mygui = GuiClient.new
#send key,value pairs for GUI Objects based on GTK or QT 
mygui.send "Textarea1.Text","hello-textarea"
sleep 5.seconds
mygui.send "Textarea1.Text","hello-textarea-more-text"
loop do 
    mygui.send "Textarea1.Text",Time.local.to_s
    sleep 2.seconds
end
sleep


Event for Button clicks forwarded like this:
def receive
    spawn do  #receiving fiber,handle GUI events
        loop do    
            event = @client.not_nil!.gets
            case event 
            when "Button1 clicked"
              @sending=false
            when "Button2 clicked"
              @sending=true    
            end
          end
        end
    end
end

A Gambas TCP GUI Server Demo App is available.
Download: https://gambas.one/gambasfarm/?id=821&action=search
Connect with any Telnet client or with your Crystal App.
https://www.bitkistl.com/2020/05/gambas3-gui-server.html