On 2011-05-07 Mojca Miklavec <mojca.miklavec.li...@gmail.com> wrote:

> On Fri, May 6, 2011 at 21:08, Marco wrote:
>
> > On 2011-05-06 Mojca Miklavec wrote:
> >
> >> > Looks like context needs a package manager.
> >>
> >> [...]  but I  admit that  I miss  some GUI  (but then
> >> again I have no idea how to write a portable GUI).
> >
> > There   are  several   possibilities,  GTK+   is  well
> > documented,  ported to  many  platforms and  operating
> > systems (However, maybe not  to all, ConTeXt supports)
> > and has Lua bindings.
>
> If you can prepare a very simple example and explain how
> to compile it on windows, mac and windows ...

A simple example  of a GUI is attached and  may serve as a
starting  point (questions  to  the  example maybe  better
off-list, since it's not at all ConTeXt related).

I don't  know which  packages are  required on  windows or
macos. On  linux and solaris  it is sufficient  to install
GTK and the lua-gtk bindings.


Marco
#! /usr/bin/env lua

Mainwin = {}
Mainwin.__index = Mainwin

List = {
    'Just',
    'some',
    'random',
    'stuff',
    'for',
    'demonstration',
}

local function UpdateProgressBar(bar)
    local oldFrac = gtk.progress_bar_get_fraction(bar)
    local frac    = 1/#List

    if (oldFrac + frac >= 1)
    then
        frac = 0
        oldFrac = 1
        gtk.main_quit()
    end

    gtk.progress_bar_set_fraction(bar, oldFrac + frac)
end

function Progressbar()
    local progress = gtk.progress_bar_new()
    gtk.progress_bar_set_text(progress, "Processing…")
    gtk.progress_bar_set_fraction(progress, 0)
    return progress
end

function Mainwin.new()
    local self = {}
    setmetatable(self, Mainwin)

    -- Set main window size, title, …
    self.w = gtk.window_new(gtk.WINDOW_TOPLEVEL)
    self.w:set_title("My Program")
    self.w:connect('destroy', gtk.main_quit)
    self.w:set_default_size(400, 400)

    -- Vertical box
    local vbox = gtk.vbox_new(false, 10)
    vbox:set_border_width(5)
    self.w:add(vbox)

    -- Tool bar
    local toolbar = gtk.toolbar_new()
    vbox:pack_start(toolbar, false, true, 0)

    -- Tool bar: items
    local items = {
        { "gtk-go-forward",  StartProcessing },
        { "SEPARATOR", nil },
        { "gtk-quit", gtk.main_quit } }

    -- Tool bar: add items to tool bar
    for _, item in pairs(items) do
        local stock   = item[1]
        local handler = item[2]
        local button, id

        if stock == 'SEPARATOR' then
            button = gtk.separator_tool_item_new()
        else
            button = gtk.tool_button_new_from_stock(stock)
            id = button:connect("clicked", handler, self)
        end
        toolbar:insert(button, -1)
    end

    -- Progress bar
    progressbar = Progressbar()
    vbox:pack_start(progressbar, false, true, 0)

    -- Scroll window
    local scrollwindow = gtk.scrolled_window_new(nil, nil)
    vbox:pack_start(scrollwindow, true, true, 0)

    -- Tree view
    local treeview = gtk.tree_view_new()
    scrollwindow:add(treeview)

    -- create store
    local store = gtk.tree_store_new(3,
        glib.TYPE_INT,          -- [0] some ID
        glib.TYPE_STRING,       -- [1] path / file name
        glib.TYPE_STRING)       -- [2] Status
    treeview:set_model(store)

    -- column with text
    local r, c
    r = gtk.cell_renderer_text_new()

    c = gtk.tree_view_column_new_with_attributes("File", r, "text", 1, nil)
    treeview:append_column(c)

    c = gtk.tree_view_column_new_with_attributes("Status", r, "text", 2, nil)
    treeview:append_column(c)

    -- add some items
    local iter1 = gtk.new "TreeIter"

    for i = 1, #List
    do
        store:append(iter1, nil)
        store:set(iter1, 0, i, 1, List[i], -1)
    end

    self.w:show_all()
    return self
end



function StartProcessing()
    DoStartProcessing_c = gnome.closure(DoStartProcessing)
    glib.timeout_add(200,
                     DoStartProcessing_c,
                     DoStartProcessing)
end

function DoStartProcessing()
    UpdateProgressBar(progressbar)
    return true
end


------------------
-- Main Program --
------------------

require "gtk"
mainwin = Mainwin.new()
gtk.main()
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to