Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Apr 2014, 05:55
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Vala and Genie programming
Post new topic   Reply to topic View previous topic :: View next topic
Page 9 of 35 [517 Posts]   Goto page: Previous 1, 2, 3, ..., 7, 8, 9, 10, 11, ..., 33, 34, 35 Next
Author Message
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Sun 12 Apr 2009, 16:32    Post subject:  

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 Smile

If you still get errors, I would need the exact message.
Mark

_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
b100dian

Joined: 07 Apr 2009
Posts: 5

PostPosted: Sun 12 Apr 2009, 16:35    Post subject:  

You may not need a thread for updating the label. I think something along the lines of
Code:
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 ().
Back to top
View user's profile Send private message 
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Sun 12 Apr 2009, 18:05    Post subject:  

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 Very Happy

Mark

Code:
[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()
GtkThreads.tar.gz
Description 
gz

 Download 
Filename  GtkThreads.tar.gz 
Filesize  7.5 KB 
Downloaded  412 Time(s) 
Lobster1.tar.gz
Description 
gz

 Download 
Filename  Lobster1.tar.gz 
Filesize  6.32 KB 
Downloaded  416 Time(s) 

_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Sun 12 Apr 2009, 18:26    Post subject:  

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 Cool

_________________
Puppy WIKI
Back to top
View user's profile Send private message Visit poster's website 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 14 Apr 2009, 03:35    Post subject:  

http://puppylinux.asia/members/T/486_pet/vala-0.6.1-i486.pet
source date 12 april

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 7047
Location: Perth, Western Australia

PostPosted: Tue 14 Apr 2009, 21:26    Post subject:  

Genie bugs fixed, request for help, see my blog post:

http://puppylinux.com/blog/?viewDetailed=00656

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Wed 15 Apr 2009, 00:44    Post subject:  

Smile Just reporting in . . .

Mark,
Shadow rather liked your iso with Mono and Valide Smile
http://www.murga-linux.com/puppy/viewtopic.php?p=290970#290970
Valide in this configuration, maybe because you are using Opera, did not effect the browser. I am reluctant to use valide on anything but a test setup. Quite happy to use Geany or Mhedit for now in 4.2

Anyway I gave him a copy - hope it works on his system. He programs in c# - has some interesting routines. Will see what happens. Cool

_________________
Puppy WIKI
Back to top
View user's profile Send private message Visit poster's website 
MUguest

Joined: 09 Dec 2006
Posts: 73

PostPosted: Wed 15 Apr 2009, 03:15    Post subject:  

I uploaded the git version of Vala:
http://dotpups.de/puppy4/dotpups/Programming/Vala/vala-git-2009-04-15-i486.pet
Compiled in Puppy 4.12.

Source:
http://dotpups.de/puppy4/dotpups/Programming/Vala/sources/vala-git-2009-04-15-source.tgz

Barry wrote details, it includes new fixes for Genie:
http://puppylinux.com/blog/?viewDetailed=00656

Mark
Back to top
View user's profile Send private message 
Lobster
Official Crustacean


Joined: 04 May 2005
Posts: 15117
Location: Paradox Realm

PostPosted: Wed 15 Apr 2009, 04:58    Post subject:  

Thanks Mark - have upgraded.
will compile something in a moment to make sure it is working
Many thanks.

Am I right in thinking your source code should go in Puppy git?
http://git.puppylinux.ca/git/?p=puppy.git;a=summary
. . . and also maybe a Puppy style front end to git
could be written in Genie by some intrepid soul?

vala/genie are to be in 4.2.1 devx

I have also this stub at the ready for those able to simplify Pizzasgoods extensive info on git
http://www.puppylinux.org/wiki/development/git/developmentadvancedgit

and posted to pizzasgood thread
http://www.murga-linux.com/puppy/viewtopic.php?p=295347#295347

This would seem to be an ideal test opportunity on a number of levels.
Create / code / become gitters (so to speak)

Cool

_________________
Puppy WIKI
Back to top
View user's profile Send private message Visit poster's website 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 7047
Location: Perth, Western Australia

PostPosted: Wed 15 Apr 2009, 10:52    Post subject:  

This is Jamie McCracken's email address, heavily obfuscated, but you should be able to figure it out:

STARTjamieDOTmccrackATgeemailDOTcomEND

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Wed 22 Apr 2009, 18:25    Post subject:  

I attach a pet with a Automount program,
It shows well, how great Genie is to write daemons running in background.
To run it, libgee-0.15 must be installed.
It was made on request, a special case:

"when a usb stick is plugged in, it shall be mounted to /mnt/DMDS-USB/.
Then a script on that stick shall be executed automatically: /mnt/DMDS-USB/Check-usb.sh"


It will read an entry from /proc/.
This is a virtual filesystem containing info about your system.
So you can read the info like from files, but no harddisk activity occurs, as it is in memory, not on a disk.

The program consists of two parts in /usr/local/bin:

detectusbstick
detectusbstickmount


detectusbstick
this is a binary. Written in Genie.
The valac compiler translated it to C, then compiled it.
The source project for the vala-ide is in /usr/local/detectusbstick/.

This binary checks every 2 seconds a change in /proc/diskstats.
It will detect ANY external drive, so not only USBsticks, but also USB-harddrives.
I don't know yet, how to distinguish among different drives, like sticks or harddrives or CF cards.

If a new device beginning with "sd" (example: sdb) is found, it runs:
detectusbstickmount sdb
This is a shellscript, so it can be easily modified for own needs.
It tries to mount /dev/sdb
A Stick also could have more than 1 partition.
So if /dev/sdb fails, it uses /dev/sdb1

Then it runs /mnt/DMDS-USB/Check-usb.sh and pops up a small dialog, that allows to unmount the stick again.
The script contains some error handling, but I cannot guarantee, that all errors are catched.
So please test it carefully.

Hint:
To run it at startup, add it in the end of /root/.xinitrc, before $CURRENTWM is run.
Like this:
--------------------
#v2.11 GuestToo suggested this improvement...
detectusbstick &
which $CURRENTWM && exec $CURRENTWM
--------------------

I attach a pet.
And the source of main.gs, so that it can be quickly looked up here.

compilation:
valac main.gs --pkg=gee-1.0 -o detectusbstick
Code:
[indent=2]

//-- run a program in background --
def exec(thecmd : string)
  Process.spawn_command_line_async(thecmd)


def read_file_to_list(thefile : string) : list of string
  var l = new list of string
  var f = FileStream.open(thefile , "r")
  var a = new array of char[1024]
  while f.gets(a) is not null
    var s = (string)a
    //--special case: do not return all lines, only the "sdX" portion --
    if s.contains(" sd")
      var as = new array of string[2]
      as = s.split("sd" , 2)
      s = "sd" + as[1].substring(0,1)
      l.add(s)
  //FileStream.close(f)
  return l


init

  var thefile = "/proc/diskstats"
  var lines = new list of string
  var oldlines = new list of string
  var isnew = false
 
  oldlines = read_file_to_list(thefile)
  while true
    lines = read_file_to_list(thefile)
   
    for line in lines
      isnew = true
      for oldline in oldlines
        if line is oldline
          isnew = false
      if isnew is true
        print("%s" , line)
        exec("detectusbstickmount " + line)
        break

    oldlines = lines
    Thread.usleep(2000000)
   
 
 

Mark
detectusbstick01.pet
Description 
pet

 Download 
Filename  detectusbstick01.pet 
Filesize  5.22 KB 
Downloaded  379 Time(s) 

_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 7047
Location: Perth, Western Australia

PostPosted: Wed 22 Apr 2009, 21:08    Post subject:  

Quote:
This binary checks every 2 seconds a change in /proc/diskstats.
It will detect ANY external drive, so not only USBsticks, but also USB-harddrives.
I don't know yet, how to distinguish among different drives, like sticks or harddrives or CF cards.

Look at
/sys/block/sda/removable
...it will have '1' if kernel thinks it is removable, ie a flash drive, but will have '0' for a usb hard drive, at least that is how it is with my usb hd.

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Fri 01 May 2009, 11:16    Post subject:  

Here is an example of a treeview.
The code for vala and for genie is almost identical.

A screenshot is attached in the end.

I used these links to learn about it:
http://valadoc.org/?pkg=gtk+-2.0
http://valadoc.org/?pkg=gtk+-2.0&element=Gtk.TreeView
http://valadoc.org/?pkg=gtk+-2.0&element=Gtk.TreeViewColumn
http://valadoc.org/?pkg=gtk+-2.0&element=Gtk.TreeStore
http://valadoc.org/?pkg=gtk+-2.0&element=Gtk.ListStore

http://live.gnome.org/Vala/GTKSample

The vala samples looked pretty commplicated, also they do not deal with treemodel, but with listmodel (so you can show tables).
But both are related, so it helped.
The "final" help to write short code I then found for C#, that has a somewhat similar syntax:
http://mono-project.com/GtkSharp_TreeView_Tutorial

Genie:
Code:
[indent=4]

uses
    Gtk


init
    Gtk.init (ref args)

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
    window.set_default_size (300, 200)
    window.destroy += Gtk.main_quit
    //window.add (new Gtk.Label ("Hello World!"))



    //-- the treeview is the visible container --
    var tree = new TreeView ()
    tree.set_headers_visible (true);
    window.add (tree)
   
    //-- add a column to the treeview --
    var column = new TreeViewColumn ()
    column.set_title ("column title")

    var columncell = new CellRendererText ()
    column.pack_start(columncell,true)
    column.add_attribute(columncell, "text" , 0)
    tree.append_column (column)

    //-- the treestore is a model assigned to the treeview , containing the items --   
    var store1 = new TreeStore (1, typeof (string));
    tree.set_model(store1);
   
    //-- add some elements to the tree --
    iter : TreeIter
    store1.append( out iter,null)
    store1.set (iter, 0, "item 1")
 
    iter2 : TreeIter
    store1.append( out iter2,iter)
    store1.set (iter2, 0, "item 2")
 
 


    window.show_all ()
    Gtk.main ()



Vala:
Code:
using Gtk;

static int main (string[] args) {
    Gtk.init (ref args);

    var window = new Window (WindowType.TOPLEVEL);
    window.title = "Tree demo";
    window.set_default_size (300, 200);
    window.position = WindowPosition.CENTER;
    window.destroy += Gtk.main_quit;


    //-- the treeview is the visible container --
    var tree = new TreeView ();
    tree.set_headers_visible (true);
    window.add (tree);

    //-- add a column to the treeview --
    var column = new TreeViewColumn ();
    column.set_title ("column title");

    var columncell = new CellRendererText ();
    column.pack_start(columncell,true);
    column.add_attribute(columncell, "text" , 0);
    tree.append_column (column);


    //var store1 = new ListStore (2, typeof (bool), typeof (string));
 
    //-- the treestore is a model assigned to the treeview , containing the items --   
    var store1 = new TreeStore (1, typeof (string));
    tree.set_model(store1);

    //-- add some elements to the tree --
    TreeIter iter;
    store1.append( out iter,null);
    store1.set (iter, 0, "item 1");
 
    TreeIter iter2;
    store1.append( out iter2,iter);
    store1.set (iter2, 0, "item 2");
 
       
    window.show_all ();

    Gtk.main ();
    return 0;
}


Mark
treeview.jpg
 Description   
 Filesize   6.54 KB
 Viewed   1162 Time(s)

treeview.jpg


_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Fri 01 May 2009, 22:08    Post subject:  

Here is the Genie treeview slightly extended.
It now has event handlers, so the program prints information about the selected/collapsed/expanded nodes.

Code:
[indent=4]


uses
    Gtk

//-- global variables, so we can use them in the eventhandlers --
tree : TreeView
store1 : TreeStore


//-- action: a line in the tree was clicked --

def row_click()

    var selection = tree.get_selection()
    iter : TreeIter

    var rows = selection.get_selected_rows(null)
    if rows.length() is 0
        return
    selection.get_selected(null, out iter)
   
    var ret = ""
    tree.model.get( iter ,0 ,out ret)   
   
    print ("selected: %s" , ret)



//-- action: a line in the tree was expanded --

def row_expanded(iter : TreeIter , path : TreePath)

    var ret = ""
    store1.get(iter ,0 ,out ret)
   
    print ("expanded: %s" , ret)
    print ("path: %s" , path.to_string())


//-- action: a line in the tree was collapsed --

def row_collapsed(iter : TreeIter , path : TreePath)

    var ret = ""
    store1.get(iter ,0 ,out ret)
   
    print ("collapsed: %s" , ret)
    print ("path: %s" , path.to_string())

//-- main program --
init
    Gtk.init (ref args)

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
    window.set_default_size (300, 200)
    window.destroy += Gtk.main_quit
    //window.add (new Gtk.Label ("Hello World!"))



    //-- the treeview is the visible container --
    tree = new TreeView ()
    tree.set_headers_visible (true);
    window.add (tree)
   
    //-- set how many lines can be selected --
    var selection = tree.get_selection()
    selection.set_mode((SelectionMode)1)
   
    //0: GTK_SELECTION_NONE
    //1: GTK_SELECTION_SINGLE
    //2: GTK_SELECTION_BROWSE
    //3: GTK_SELECTION_MULTIPLE
   
   
    //-- add a column to the treeview --
    var column = new TreeViewColumn ()
    column.set_title ("column title")

    var columncell = new CellRendererText ()
    column.pack_start(columncell,true)
    column.add_attribute(columncell, "text" , 0)
    tree.append_column (column)

    //-- the treestore is a model assigned to the treeview , containing the items --   
    store1 = new TreeStore (1, typeof (string));
    tree.set_model(store1);
   
    //-- add some elements to the tree --
    iter : TreeIter
    store1.append( out iter,null)
    store1.set (iter, 0, "item 1")
 
    iter2 : TreeIter
    store1.append( out iter2,iter)
    store1.set (iter2, 0, "item 2")
   
    iter3 : TreeIter
    store1.append( out iter3,iter2)
    store1.set (iter3, 0, "item 3")
 
    //-- assign the eventhandlers for the tree --

    tree.row_expanded += row_expanded
    tree.row_collapsed += row_collapsed
    Signal.connect_after (tree, "button_release_event", row_click, null);


    window.show_all ()
    Gtk.main ()


Mark

_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
MU


Joined: 24 Aug 2005
Posts: 13642
Location: Karlsruhe, Germany

PostPosted: Fri 01 May 2009, 22:18    Post subject:  

And here is a more complex solution.
It consists of two sourcefiles.

tree.gs is a class to set up a treeview and add items.
This allows to keep the main.gs shorter.

This class supports pictures.
And as it is a class, you easily can define more than one tree.

As you can see on the screenshot, you also easily can simulate a "Clist" like this, thats to say a picturelist that is not showing a tree, but only some pictures with text one beneath the other.
Like the two elements with the text-icons.


Here is an extract of the usage:
Code:
//-- action: a line in tree1 was clicked --

def tree1_row_click()

    var ret = treeclass1.tree_click(mytree1)
    print ("selected is: %s" , ret)



//-- main program --

init


    Gtk.init (ref args)

    var window = new Gtk.Window (Gtk.WindowType.TOPLEVEL)
    window.set_default_size (300, 200)
    window.destroy += Gtk.main_quit
   

    var hbox = new Gtk.HBox(false , 1)
    hbox.border_width = 10
    hbox.spacing = 10
    window.add (hbox)


    //-- create a new tree --
   
    treeclass1 = new treeclass()
    mytree1 = treeclass1.treeview()
    hbox.pack_start (mytree1, true, true, 0);
   

    var item_A1 = treeclass1.add_root_item("test A1" )
    var item_A2 = treeclass1.add_item("test A2" , item_A1)
    treeclass1.set_image("text.png")
    var item_A3 = treeclass1.add_item("test A3" , item_A2)   
    var item_A4 = treeclass1.add_item("test A4" , item_A2)
     
    treeclass1.tree.row_expanded += tree1_row_expanded
    treeclass1.tree.row_collapsed += tree1_row_collapsed
    Signal.connect_after (treeclass1.tree, "button_release_event", tree1_row_click, null);


Mark
treeviewcomplex.jpg
 Description   
 Filesize   16.98 KB
 Viewed   1126 Time(s)

treeviewcomplex.jpg

TreeView-Genie-Complex.tar.gz
Description 
gz

 Download 
Filename  TreeView-Genie-Complex.tar.gz 
Filesize  27.17 KB 
Downloaded  390 Time(s) 

_________________
my recommended links
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 9 of 35 [517 Posts]   Goto page: Previous 1, 2, 3, ..., 7, 8, 9, 10, 11, ..., 33, 34, 35 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1511s ][ Queries: 13 (0.0200s) ][ GZIP on ]