Vala and Genie programming

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
b100dian
Posts: 5
Joined: Tue 07 Apr 2009, 22:35

#106 Post by b100dian »

So Vala/Genie are really on a good way, if they find interest even under such "critical" conditions

Thanks very much for your work, Mark
Well I am only another Vala user, but thanks for the thanks:)

However, I have one question regarding Vala/Genie, Puppy Linux and Gtkaml: I am in the process of re-writing the parsing to better integrate with Vala's parser and symbol resolver, and to expand Gtkaml's features.

Q: How do you feel about the libxml2 dependency (given the Puppy Linux constraints) and would a GLib/GMarkup approach be favored?

Thanks, and sorry for the offtopic.
Vlad

MUguest
Posts: 73
Joined: Sat 09 Dec 2006, 16:40

#107 Post by MUguest »

Hi Vlad,
I think libxml2 is in Puppy since ages, and will stay in it, as many programs depend on it. So it is ok, if you stay with it.
Mark

User avatar
Mr. Maxwell
Posts: 215
Joined: Sat 30 Aug 2008, 23:56
Location: Nebraska, USA

#108 Post by Mr. Maxwell »

I've got everything working except my GUI. :? All I need is a button and a text entry field, the problem is I have no idea how to 1) pack the widgets so the text entry field is on top of the button 2) how to make, display text, and delete text in a text entry field.

Thanks in advance.
[url=http://www.tribalwars.net/3389956.html]Super amazing game![/url]

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#109 Post by MU »

I attach an example.
It uses project.glade, that can be modified with Glade.

If you do not want to use libglade, I would need your program, so that I could add the code you miss to it.

Note, that you must compile it with libglade and gobject.
If you don't use the ValaIDE, compile it with:
valac -C --pkg gtk+-2.0 --pkg libglade-2.0 --pkg gmodule-2.0 main.gs

If you use the valaide, add those libraries in the options.
On my current system, the ide crashes, if I go to the options, so I added them with an editor to buttonandtext.vide.

Mark
Attachments
butonandtext.jpg
(5.03 KiB) Downloaded 1256 times
buttonandtext.tar.gz
(6.53 KiB) Downloaded 466 times
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#110 Post by MU »

Here is the same result, but now without libglade, coded "by hand" with the native Gtk bindings.

Code: Select all

[indent=2]

//-- declare global variables --
entry : Gtk.Entry


//-- define the methods --

def on_button_clicked ()

  entry.set_text("text changed!")


//-- main program --

init
  Gtk.init (ref args)

  //-- build the window --

  var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
  window.set_default_size (300, 10)
  window.destroy += Gtk.main_quit
  
  
  var vbox = new Gtk.VBox(false , 1)
  vbox.border_width = 10
  vbox.spacing = 10
  window.add (vbox)

  
  //-- the entry is global, so that it can be modified later --

  entry = new Gtk.Entry ()
  entry.set_editable(false)
  entry.set_text("hello")
  vbox.pack_start (entry, false, true, 0);


  var button = new Gtk.Button.with_label("ok")
  vbox.pack_start (button, false, true, 0);


  //-- define actions --

  button.clicked += on_button_clicked;


  //-- show the window, hand over to the Gtk mainloop --
  
  window.show_all ()
  Gtk.main ()

Mark
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

b100dian
Posts: 5
Joined: Tue 07 Apr 2009, 22:35

#111 Post by b100dian »

Here is the same result, but now without libglade, coded "by hand" with the native Gtk bindings.
(Shameless plug, I know... )

Here is the same code "by hand" with gtkaml (Mark, you once said you 'haven't tested the library')

Code: Select all

<Window xmlns="Gtk" xmlns:g="http://gtkaml.org/0.2"
  g:name="ButtonAndText" destroy="{Gtk.main_quit}" type="{WindowType.TOPLEVEL}">

  <VBox homogeneous="false" spacing="10" border-width="10">
    <Entry g:public="entry" editable="false" text="hello" expand="false" />
    <Button label="ok" expand="false" clicked='entry.text="text changed!"'/>
  </VBox>

<![CDATA[

  static int main (string [] args)
  {
    Gtk.init (ref args);
    var window = new ButtonAndText();
    window.set_default_size (300, 10);
    window.show_all ();
    Gtk.main ();
  }

]]>
</Window>
Compiles with:
gtkamlc --pkg gtk+-2.0 buttonandtext.gtkaml

(Unfortunately gtkaml only supports vala atm, not genie)

Vlad

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#112 Post by MU »

Great, thanks for the nice example, Vlad!

It contains a slight error, the return type must be void, or I get this error:
# gtkamlc --pkg gtk+-2.0 buttonandtext.gtkaml
buttonandtext.vala:11.3-11.17: error: missing return statement at end of method body
Segmentation fault
Here is the same code with void instead of int:

Code: Select all

<Window xmlns="Gtk" xmlns:g="http://gtkaml.org/0.2" 
  g:name="ButtonAndText" destroy="{Gtk.main_quit}" type="{WindowType.TOPLEVEL}"> 

  <VBox homogeneous="false" spacing="10" border-width="10"> 
    <Entry g:public="entry" editable="false" text="hello" expand="false" /> 
    <Button label="ok" expand="false" clicked='entry.text="text changed!"'/> 
  </VBox> 

<![CDATA[ 

  static void main (string [] args) 
  { 
    Gtk.init (ref args); 
    var window = new ButtonAndText(); 
    window.set_default_size (300, 10); 
    window.show_all (); 
    Gtk.main (); 
  } 

]]> 
</Window>
:D
Mark
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
Mr. Maxwell
Posts: 215
Joined: Sat 30 Aug 2008, 23:56
Location: Nebraska, USA

#113 Post by Mr. Maxwell »

I got my first complete Genie program done! It's a shakespeareain insulter! :lol:

Source:

Code: Select all

[indent=4]

uses
    GLib

//-- declare global variables --
entry : Gtk.Entry


//-- define the methods --

def on_button_clicked ()

    cola:array of string = {"artless", "bawdy", "beslubbering", "bootless", "churlish", "cockered", "clouted", "craven", "currish", "dankish", "dissembling", "droning", "errant", "fawning", "fobbing", "froward", "frothy", "gleeking", "goatish", "gorbellied", "impertinent", "infectious", "jarring", "loggerheaded", "lumpish", "mammering", "mangled", "mewling", "paunchy", "pribbling", "puking", "puny", "qualling", "rank", "reeky", "roguish", "ruttish", "saucy", "spleeny", "spongy", "surly", "tottering", "unmuzzled", "vain", "venomed", "villainous", "warped", "wayward", "weedy", "yeasty"}
    colb:array of string = {"base-court", "bat-fowling", "beef-witted", "beetle-headed", "boil-brained", "clapper-clawed", "clay-brained", "common-kissing", "crook-pated", "dismal-dreaming", "dizzy-eyed", "doghearted", "dread-bolted", "earth-vexing", "elf-skinned", "fat-kidneyed", "fen-sucked", "flap-mouthed", "fly-bitten", "folly-fallen", "fool-born", "full-gorged", "guts-griping", "half-faced", "hasty-witted", "hedge-born", "hell-hated", "idle-headed", "ill-breeding", "ill-nurtured", "knotty-pated", "milk-livered", "motley-minded", "onion-eyed", "plume-plucked", "pottle-deep", "pox-marked", "reeling-ripe", "rough-hewn", "rude-growing", "rump-fed", "shard-borne", "sheep-biting", "spur-galled", "swag-bellied", "tardy-gaited", "tickle-brained", "toad-spotted", "unchin-snouted", "weather-bitten"}
    colc:array of string = {"apple-john", "baggage", "barnacle", "bladder", "boar-pig", "bugbear", "bum-bailey", "canker-blossom", "clack-dish", "clotpole", "coxcomb", "codpiece", "death-token", "dewberry", "flap-dragon", "flax-wench", "flirt-gill", "foot-licker", "fustilarian", "giglet", "gudgeon", "haggard", "harpy", "hedge-pig", "horn-beast", "hugger-mugger", "joithead", "lewdster", "lout", "maggot-pie", "malt-worm", "mammet", "measle", "minnow", "miscreant", "moldwarp", "mumble-news", "nut-hook", "pigeon-egg", "pignut", "puttock", "pumpion", "ratsbane", "scut", "skainsmate", "strumpet", "varlot", "vassal", "whey-face", "wagtail"}

    insult:string = "Thou "
    insult = insult.concat(cola[GLib.Random.int_range(0, 49)], " ", colb[GLib.Random.int_range(0, 49)], " ", colc[GLib.Random.int_range(0, 49)], "!")
    entry.set_text(insult)

//-- main program --

init
    Gtk.init (ref args)

    //-- build the window --

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
    window.set_default_size (400, 10)
    window.destroy += Gtk.main_quit
 
 
    var vbox = new Gtk.VBox(false , 1)
    vbox.border_width = 10
    vbox.spacing = 10
    window.add (vbox)

 
    //-- the entry is global, so that it can be modified later --

    entry = new Gtk.Entry ()
    entry.set_editable(false)
    entry.set_text("")
    vbox.pack_start (entry, false, true, 0);


    var button = new Gtk.Button.with_label("Insult me again!")
    vbox.pack_start (button, false, true, 0);


    //-- define actions --

    button.clicked += on_button_clicked;
    on_button_clicked()


    //-- show the window, hand over to the Gtk mainloop --
 
    window.show_all ()
    Gtk.main ()
The source and binary are attached.[/code]
Attachments
shakespeareain_insulter.tar.gz
(6.98 KiB) Downloaded 473 times
[url=http://www.tribalwars.net/3389956.html]Super amazing game![/url]

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#114 Post by MU »

:lol: well done :D
Mark
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#115 Post by Lobster »

Looks simple - good :)

I have called that insulter is.gs
(why would anyone want to insult Shakespeare?) - ahem :oops:
This is what I get - have had similar problems with other gtk programs.
Am I missing a library (using Puppy 4.2.smp)?
Should I use Puppy 4.12 for Genie?

Code: Select all

# valac si.gs
si.gs:7.9-7.11: error: The symbol `Gtk' could not be found
entry : Gtk.Entry
        ^^^
Compilation failed: 1 error(s), 0 warning(s)
#
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
Mr. Maxwell
Posts: 215
Joined: Sat 30 Aug 2008, 23:56
Location: Nebraska, USA

#116 Post by Mr. Maxwell »

You have to call the compiler with the GTK package flag.

Code: Select all

valac --pkg gtk+-2.0 is.gs
[url=http://www.tribalwars.net/3389956.html]Super amazing game![/url]

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#117 Post by Lobster »

Thanks Mr. Maxwell

Now able to compile for gtk :)
We are now trying to combine the earlier random monkey generator
with your code as a basis

so far we can input text - say 'in'
but need another output window?
some way of putting the generated random letters in a second window . . .

Anyway the code we have so far:

Code: Select all

[indent=4]

//outputting random generated text from input and outputting result 

uses
    GLib

//-- declare global variables --
entry : Gtk.Entry


//-- define the methods --

def getRandomNumber(RangeFrom:int, RangeTo:int) : int  /* function to create random number between range */
    return GLib.Random.int_range(RangeFrom,RangeTo)

def getRandomChar() : char                             /* function to generate ascii codes from a-z and space (32) */
    num:int = getRandomNumber(0,27)
    if num == 0
        num = 32
    else
        num = num + 96
    return (char) num

def addRandomChar(myText:string) : string              /* function add text from command line */
    var retText = new StringBuilder
    retText.append(myText)
    retText.append_c(getRandomChar())
    return retText.str

def IsEmpty(theText:string) : bool
    if theText == null
        return true
    if theText == ""
        return true
    return false

def on_button_clicked ()
    theText:string = entry.get_text()
    if IsEmpty(theText) 
        entry.set_text("Please enter some text")
        return
    entry.set_text("running")
    theText = theText.down()
    myText:string = ""

    timer : Timer = new Timer()
    timer.start()

    do
        do
            myText = addRandomChar(myText)
        while theText.len() != myText.len()
        if theText != myText
            myText = ""
    while theText != myText
    entry.set_text(myText)

//-- main program --

init
    Gtk.init (ref args)

    //-- build the window --

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
    window.set_default_size (400, 50)
    window.destroy += Gtk.main_quit
 
    var vbox = new Gtk.VBox(false , 1)
    vbox.border_width = 10
    vbox.spacing = 10
    window.add (vbox)
 
    //-- the entry is global, so that it can be modified later --

    entry = new Gtk.Entry ()
    entry.set_editable(true)
    entry.set_text("")
    vbox.pack_start (entry, false, true, 0);

    var button = new Gtk.Button.with_label("Insult me again!")
    vbox.pack_start (button, false, true, 0);

    //-- define actions --

    button.clicked += on_button_clicked;
    on_button_clicked()

    //-- show the window, hand over to the Gtk mainloop --
 
    window.show_all ()
    Gtk.main ()


Any help or tips?
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#118 Post by MU »

I attach an example, Lobster.

I have one problem:
I would like to display the random words in the label in the second window.
But it does not get updated, until the whole function has finished.

It might be doable with threads, but I found no example yet, how to use threads in Genie.
Mark
Attachments
Lobster1.tar.gz
(6 KiB) Downloaded 397 times
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#119 Post by MU »

ah fine, threads can be used.
but together with gtk it is complicated.

such code works:

Code: Select all

def mythread2() : int
    for var i = 1 to 100 
        print ("%s" , "+++")
        Thread.usleep(100)

    return 0

def mythread3() : int
    for var i = 1 to 100
        print ("%s" , "------------")
        Thread.usleep(100)

    return 0

def static on_button_clicked () 


    //mythread()

    Thread.create( (ThreadFunc)mythread3, false);
    Thread.create( (ThreadFunc)mythread2, false);

must be used in a full program, and compiled with:
valac --thread --pkg gtk+-2.0 main.gs

This will print stuff like:
+++
------------
+++
+++
------------
+++
------------
So both methods run parallel.

Mark
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#120 Post by Lobster »

Thanks Mark - hopefully will look at it tomorrow with Shadow

Threads in particular are interesting - and the first example is using a library you are using in NewyearPup? - does not seem to be in 4.2 . . .

Really appreciate the help.
In fact I might suggest Shadow uses your Genie + Java ISO that might be ideal for him . . .
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#121 Post by MU »

you should be able to compile main.gs on every puppy, I used nothing special in it.
Just if you run the executable without recompiling it, you might get an error, as NYP uses more libs in general.
For this reason I compile programs I want to share usually in Puppy 4.12.
I just did not do it here, as for you it should be easy to compile it yourself :)

If you still get errors, I would need the exact message.
Mark
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

b100dian
Posts: 5
Joined: Tue 07 Apr 2009, 22:35

#122 Post by b100dian »

You may not need a thread for updating the label. I think something along the lines of

Code: Select all

if (Gtk.events_pending())
    Gtk.main_iteration ()
after each set_text would suffice.

However care must be taken not to enter a second (a third etc) that click handler (because main_iteration () will process clicks too, and you would have one click handler's main_iteration () call re-entering the click handler..)

Or, if your really want a thread, you have to 'post' the work (set_text) to be done in the UI thread using GLib.Idle.add ().

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#123 Post by MU »

Vlad,
oh, things can be so easy!
I attach a new "Lobster1.tar.gz", that works as you described.
I also added a lock.

There is a strange sideeffect though:
when I close one of both windows, the program does not exit, until the loop finished.
Maybe another check had to be added, but I'm too tired now.

I also just finished a full working solution using Threads and a Mutex.
Main.gs includes some comments.
I attach it, too, and post the source here, so that I quickly can look it up later.

Many thanks for "Gtk.main_iteration", I often missed such a simple solution. It will make life easier for me in future :D

Mark

Code: Select all

[indent=4] 


/*

  This example updates a gtkwindow, that executes a long loop
  Usually, during a loop, the window is not updated

  The solution is to use "threads" and a "mutex"
  
  !!!!! compile it in a console like this: !!!!!
  valac --thread --pkg gtk+-2.0 main.gs -o GtkThreads
  
  other examples:
  http://ubuntuforums.org/archive/index.php/t-323435.html (C)


  see also: http://valadoc.org/?pkg=glib-2.0&element=GLib.Thread
*/


//outputting random generated text from input and outputting result 

uses 
    GLib 

//-- declare global variables -- 
entry : Gtk.Entry 
label : Gtk.Label
mutex : Mutex
locked : bool

 
//-- define the methods -- 

def getRandomNumber(RangeFrom:int, RangeTo:int) : int  /* function to create random number between range */ 
    return GLib.Random.int_range(RangeFrom,RangeTo) 

def getRandomChar() : char                             /* function to generate ascii codes from a-z and space (32) */ 
    num:int = getRandomNumber(0,27) 
    if num == 0 
        num = 32 
    else 
        num = num + 96 
    return (char) num 

def addRandomChar(myText:string) : string              /* function add text from command line */ 
    var retText = new StringBuilder 
    retText.append(myText) 
    retText.append_c(getRandomChar()) 
    return retText.str 

def IsEmpty(theText:string) : bool 
    if theText == null 
        return true 
    if theText == "" 
        return true 
    return false 



def mythread() :int
    theText:string = entry.get_text() 
    
    if IsEmpty(theText) 
        entry.set_text("Please enter some text") 
        return 1
        
    locked = true
    label.set_text("running") 
    //Gdk.flush()


    theText = theText.down() 
    myText:string = "" 

    timer : Timer = new Timer() 
    timer.start() 


    do 
        do 
            myText = addRandomChar(myText) 
        while theText.len() != myText.len()

       
        mutex.lock ()
        Gdk.threads_enter()
        label.set_text(myText)
        Gdk.threads_leave()
        mutex.unlock()
    
        if theText != myText 
            myText = "" 
    while theText != myText 
    label.set_text( myText ) 
    
    locked = false
    return 0


def mythread2() : int
    for var i = 1 to 100 
        print ("%s" , "+++")
        Thread.usleep(100)

    return 0



def static on_button_clicked () 

    if locked == true
        print("%s" , "thread is locked")
        return

    try
        Thread.create( (ThreadFunc)mythread, false)
    except ex : GLib.ThreadError
        print ("threaderror!")
        
    //Thread.create( (ThreadFunc)mythread2, false)


    

//-- main program -- 

init

    

    Gdk.threads_init()
    Gtk.init (ref args) 
    mutex = new Mutex ()

    //-- build the first window -- 

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL) 
    window.set_default_size (400, 50) 
    window.destroy += Gtk.main_quit 
    window.set_title("userinput")
    window.move(100 , 100)
  
    var vbox = new Gtk.VBox(false , 1) 
    vbox.border_width = 10 
    vbox.spacing = 10 
    window.add (vbox) 
  
    //-- the entry is global, so that it can be modified later -- 

    entry = new Gtk.Entry () 
    entry.set_editable(true) 
    entry.set_text("test") 
    vbox.pack_start (entry, false, true, 0)

    var button = new Gtk.Button.with_label("enter some SHORT text (less 10 chars), then click here!") 
    vbox.pack_start (button, false, true, 0) 

    //-- define actions -- 

    button.clicked += on_button_clicked; 


    //------  window 2 -----------

    var window2 = new Gtk.Window (Gtk.WindowType.TOPLEVEL) 
    window2.set_default_size (400, 50) 
    window2.destroy += Gtk.main_quit 
    window2.set_title("results")
    window2.move(100 , 250)
    
    var vbox2 = new Gtk.VBox(false , 1) 
    vbox2.border_width = 10 
    vbox2.spacing = 10 
    window2.add (vbox2)
     
    label = new Gtk.Label ("")  
    vbox2.pack_start (label, false, true, 0)

    
    //-- show the window, hand over to the Gtk mainloop -- 

    window2.show_all ()  
    window.show_all () 

    Gdk.threads_enter()
    Gtk.main ()
    Gdk.threads_leave()
Attachments
GtkThreads.tar.gz
(7.5 KiB) Downloaded 598 times
Lobster1.tar.gz
(6.32 KiB) Downloaded 602 times
[url=http://murga-linux.com/puppy/viewtopic.php?p=173456#173456]my recommended links[/url]

User avatar
Lobster
Official Crustacean
Posts: 15522
Joined: Wed 04 May 2005, 06:06
Location: Paradox Realm
Contact:

#124 Post by Lobster »

Brilliant. Works.

I hope Shadow is impressed how open source can leap frog itself.
I will check if you do any updates and think about our next bit of coding . . .

Combining code plus GTK will now move things along 8)
Puppy Raspup 8.2Final 8)
Puppy Links Page http://www.smokey01.com/bruceb/puppy.html :D

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#125 Post by technosaurus »

Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply