again GTK

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

again GTK

Post by newdep »

An adjusted example that shows how easy it is to program in newlisp
directly with GTK2+.. The import part can be put inside a module so that
only leaves the real coding..which is actualy pretty small...just to mention it.. ;-)

Code: Select all

#!/usr/bin/newlisp
# Example of direct gtk2+ programming in newlisp

# predefine libraries for platform
(set 'GTK     "libgtk-x11-2.0.so")
(set 'GDK     "libgdk-x11-2.0.so")
(set 'GOBJECT "libgobject-2.0.so.0")

# get functions from library's
(import GOBJECT "g_signal_connect_data")
(import GDK "gdk_color_parse")

(dolist (L '(
 "gtk_init"
 "gtk_main"
 "gtk_exit"
 "gtk_table_new"
 "gtk_progress_bar_new"
 "gtk_table_attach_defaults"
 "gtk_events_pending"
 "gtk_main_iteration"
 "gtk_progress_bar_set_fraction"
 "gtk_container_add"
 "gtk_widget_show_all"
 "gtk_widget_set_size_request"
 "gtk_widget_modify_fg"
 "gtk_widget_modify_bg"
 "gtk_window_new"
 "gtk_window_set_position"
 "gtk_window_set_resizable"
 "gtk_window_set_title"
 "gtk_window_set_decorated"

 )) (import GTK L))

# callback to exit program
(define (_exit_) (gtk_exit 0) (exit))

# create a window
(gtk_init 0 0)
(set 'WIN (gtk_window_new 0))
(gtk_window_set_title WIN "different time")
(gtk_widget_set_size_request WIN 200 32)
(gtk_window_set_resizable WIN 0)
(gtk_window_set_position  WIN 2)
(gtk_window_set_decorated WIN 1)
(g_signal_connect_data WIN "delete-event" (callback 0 '_exit_) 0 0 0)

# progress bars
(setq tbl (gtk_table_new 100 100 1 ))
(gtk_container_add WIN tbl)
(setq pb1 (gtk_progress_bar_new))
(gtk_table_attach_defaults tbl pb1 1 100  1 10)
(setq pb2 (gtk_progress_bar_new ))
(gtk_table_attach_defaults tbl pb2 1 100 11 20)
(setq pb3  (gtk_progress_bar_new))
(gtk_table_attach_defaults tbl pb3 1 100 21 30)

# colors (gtk2+)
(gdk_color_parse "#000000"  black)
(gtk_widget_modify_bg pb1 0 black)
(gtk_widget_modify_bg pb2 0 black)
(gtk_widget_modify_bg pb3 0 black)
(gtk_widget_modify_bg WIN 0 black)


(gtk_widget_show_all WIN)

(setq event 0)
(while (= (integer event) 0)

    (while (= (integer (gtk_events_pending)) 1) (gtk_main_iteration))
    (sleep 50)
        (gtk_progress_bar_set_fraction pb3 (div (float (date (date-value) 0 "%H")) 24))
        (gtk_progress_bar_set_fraction pb2 (div (float (date (date-value) 0 "%M")) 60))
        (gtk_progress_bar_set_fraction pb1 (div (float (date (date-value) 0 "%S")) 60)) ))

(_exit_)
Last edited by newdep on Fri Jul 24, 2009 7:12 am, edited 2 times in total.
-- (define? (Cornflakes))

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

woohoo! I knew it had to be easy. I like that dolist idiom you used for importing the symbols.

Ted

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

I think I need to make an request to the community ;-)

We might need to do this together.. and thats creating a Module of GTK2+.
For 1 person to do so its quite a job and not realy fun.
There are already some snippets here and there but not an official full
module for GTK that can be added to the newlisp module section.

So What if a few people here could help out creating the Module for GTK2+ ?

If you interested drop in here..
The only thing we need to do is to splitt up the GTK content every person takes on..
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

I would like to make 1 big import list of all available functions from the GTK2+ library.

For those willing to tune and develop the GTK2+ could be more integrated into newlisp by the creation of default functions for GTK2+. Like default window setups , color management, event & widget handlings etc.. all that makes GTK2+ into a newlisp module.
Just like Lutz did with the java GS actualy..
(But this can only be done when library calls are all included)


Btw.. Its there a way to "(ab)use" the Wiki for this? creating modules?
Or is that realy only suitable for documentation?
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Basicly all the GTK symbols are already there..
they are in the gtk.symbols file.. (GTK 2.16.5)

Reading that file completly as a module isnt that wise, thouse
contructing pre-defined newlisp functions for GTK is a better idea.

Ill start with a global window setup function for GTK and
extent the Module (official documented with newlispDoc)..


BTW... it wil be done FOOP wise!
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

On the Fly use of GTK

Post by newdep »

This is an "on the fly import" use of gtk. No pre-import of library functions!
The import is done via the function %%
and can be used with any library you want to import


I had to chose between a namespace naming, making symbols from the library confusing.
(I wanted the original symbol naming)
or adding an extra function in from of the symbols, in this case it became %%

So actualty you can use any symbols from the gtk library without
having it pre-defined. This is..it seems there are a few library calls
that need caling including arguments, but i havnt encountered those
yet.. If..then Ill need to adjust this function.

Im not yet fully satisfied yet (im very fond with visual display ;-)
with this way of symbol calling but its at least a cleaner code result.
Perhpas the %% should be "@"..anyway have a look.. :)

Code: Select all

#!/usr/bin/newlisp
# Example of direct gtk programming in newlisp
# this example is an "on the fly import"

# predefine libraries for platform
(setq GTKLIB  "libgtk-x11-2.0.so")
(setq GDKLIB  "libgdk-x11-2.0.so")
(setq GOBJECT "libgobject-2.0.so.0")
(setq SYMBOLS '())

;; load from the predefined library's, only once!
(define-macro (%%)
 (and (not (find (first (args)) SYMBOLS))
        (or (import GTKLIB  (string (first (args))))
            (import GDKLIB  (string (first (args))))
            (import GOBJECT (string (first (args)))))
      (push (string (first (args))) SYMBOLS))
 (eval (args)))

# callback to exit program
(define (_exit_) (%% gtk_exit 0) (exit))

# create a window
(%% gtk_init 0 0)
(set 'WIN (%% gtk_window_new 0))
(%% gtk_window_set_title WIN "different time")
(%% gtk_widget_set_size_request WIN 200 32)
(%% gtk_window_set_resizable WIN 0)
(%% gtk_window_set_position  WIN 2)
(%% gtk_window_set_decorated WIN 1)
(%% g_signal_connect_data WIN "delete-event" (callback 0 '_exit_) 0 0 0)

# progress bars
(setq tbl (%% gtk_table_new 100 100 1 ))
(%% gtk_container_add WIN tbl)
(setq pb1 (%% gtk_progress_bar_new))
(%% gtk_table_attach_defaults tbl pb1 1 100  1 10)
(setq pb2 (%% gtk_progress_bar_new ))
(%% gtk_table_attach_defaults tbl pb2 1 100 11 20)
(setq pb3 (%% gtk_progress_bar_new))
(%% gtk_table_attach_defaults tbl pb3 1 100 21 30)

# colors (gtk2+)
(%% gdk_color_parse "#000000"  black)
(%% gtk_widget_modify_bg pb1 0 black)
(%% gtk_widget_modify_bg pb2 0 black)
(%% gtk_widget_modify_bg pb3 0 black)
(%% gtk_widget_modify_bg WIN 0 black)

(%% gtk_widget_show_all WIN)

(setq event 0)
(while (= (integer event) 0)

    (while (= (integer (%% gtk_events_pending)) 1) (%% gtk_main_iteration))
    (sleep 50)
        (%% gtk_progress_bar_set_fraction pb3 (div (float (date (date-value) 0 "%H")) 24))
        (%% gtk_progress_bar_set_fraction pb2 (div (float (date (date-value) 0 "%M")) 60))
        (%% gtk_progress_bar_set_fraction pb1 (div (float (date (date-value) 0 "%S")) 60)) ))

(_exit_)




-- (define? (Cornflakes))

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

Another new idiom to digest; where can I read up on %%?

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

In the defined macro on top of the code ;-)
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

While lurking in the GTK api i came to the conclusion that actualy porting
the API is a bad idea.. Its so wide and big that the best way to use it is
by direct calls. What can be done if putting all those predefines into a module
that makes life more easier.. (gtk-server.org already has that at hand)..

I though perhpas making a widget based GS lib like the java GS is but
then again you predefine it for the user..which is actualy not far from
doing it yourself in GTK.

The most intresting goal is how to be as transparent as possible while
calling C library's from within newlisp. And how to reduce workload.
.... Ill be thinking ;-)
-- (define? (Cornflakes))

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

I'm confused... (So what's new about that ;0)

I thought the gtk server project already has newLISP bindings?

http://www.gtk-server.org/

In fact, newLISP is the only langage with example code for all supported systems:
http://www.gtk-server.org/examples.html

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

I'm surprised there is no Debian or Ubuntu package for gtk-server.

Ted

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

Looking around the net, another GUI frame work to consider is Lua's IUP:
http://www.tecgraf.puc-rio.br/iup/
IUP is a portable toolkit for building graphical user interfaces. It offers a configuration API in three basic languages: C, Lua and LED. IUP's purpose is to allow a program to be executed in different systems without any modification, therefore it is highly portable. Its main advantages are:

* high performance, due to the fact that it uses native interface elements.
* fast learning by the user, due to the simplicity of its API.

http://en.wikipedia.org/wiki/IUP_(software)
It provides this functionality by binding Lua with its C/C++ code, or simply writing C to the API. It supports calling native Windows graphics, native Motif/Lesstif or GTK+ elements, or the developers' own Canvas Draw elements from the Lua scripts or natively in a C/C++ application.
The C version of the IUP interface and IUP being designed for use by us GUI dummies ;) it might be a better fit for an "offical" newLISP GUI...

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

I'm surprised there is no Debian or Ubuntu package for gtk-server.
Well, me too ;-)

But I have access to Ubuntu nowadays, I can make a package if you like.

For newLisp this is not needed anymore, because with GTK2 you can import the functions on the fly. Not sure if it works for all functions though. Anyway, two links with other examples I made some time ago:

http://www.turtle.dds.nl/newlisp/tables.lsp
http://www.turtle.dds.nl/newlisp/fractal.lsp

For another example, and other GTK stuff, look here: http://newlispfanclub.alh.net/forum/vie ... php?t=2488

Regards
Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

For newLisp this is not needed anymore, because with GTK2 you can import the functions on the fly.
For computing intensive programs the GTK-server approach may still have advantages, because it runs in a different process or even on a different computer when using TCP/IP as the comm. channel. One could run newLISP and the application on a remote very fast platform and run GTK-server serving the GUI and user interaction on the local desktop.

I did a small modification to guiserver.lsp to be able to start the GUI and newLISP app. manually on different computers. This will be in the next version. In the old newLISP days the same could be done with the old Tcl/Tk GUI. X-windows invented this whole approach a few decades ago.

Locked