13 September 2006

Pychess 0.2

I've released version 0.2 of Pychess, and created a googlepages page for it: http://pychess.googlepages.com/

From the page:

I've released version 0.2 of Pychess. It is not 100% bug free, but perhaps 95% :). I'll close anything posted in the issue tracker, but I will now also look forward into some of the purposed features.

I thought of giving it some kind of cool name, like the big projects, but couldn't think of anything better than "the flying beaver", which google tells me is already taken, so I skipped it.

Feel very free to post bug reports and to join the project :D

13 August 2006

Svg in python

After hours of googling, and finding stuff like import cairo.svg, import rsvg and import gtk.librsvg, I found out, that the newest versions of gdk simply understand svg out of the box.

pixbuf = gtk.gdk.pixbuf_new_from_file("black-bishop.svg") will do it all! Sweet.

Conclusion

After I've tried BloGTK, Drivel and gnome-blog-poster, I must say none of them did the job satisfying.

None of them were able to do a title, and none of them could do English spellchecking.

Drivel had a nice "edit old post" function, but gnome-blog-poster had a gnome-panel applet, so I find that it might be the best solution of the three.

However as none of them were able to post titles to posts, they weren't really useful. I guess I have to just use the web interface for now.

Print piping in python

print > open("file","w"), "File content" print >> sys.stdout, "Hello world" What a nice feature of pythons. Had thought this might be possible for a while, but never got the actual syntax. Having programmed php for a time, it is good to be back to python, where you haven't even got to put "\n" in the end of the prints :)

04 July 2006

System colors in gtk

I just found out, after an hour of searcing, how to get "system colors" in gtk. Only tried with pygtk, but I guess bindings does it the same.

First you have to get a GtkStyle object. If you extend GtkWidget you can get it using self.get_style(), else you can get it from e.g. a button: gtk.Button().get_style()

The GtkStyle object contains a lot of GtkStyleHelper objects. More precisely it has: ['base', 'base_gc', 'bg', 'bg_gc', 'bg_pixmap', 'dark', 'dark_gc', 'fg', 'fg_gc', 'light', 'light_gc', 'mid', 'mid_gc', 'text', 'text_aa', 'text_aa_gc', 'text_gc']

Each of these has different colors depending on the state. The states are ['STATE_ACTIVE', 'STATE_INSENSITIVE', 'STATE_NORMAL', 'STATE_PRELIGHT', 'STATE_SELECTED']

When you know that, you should be able to draw with a system color, e.g. with cairo.

context = widget.window.cairo_create()
context.rectangle(0,0,10,10)
context.set_source_color(self.get_style().dark[gtk.STATE_NORMAL])
context.fill_preserve()

Some other code:

>>> [m for m in dir(gtk) if m.startswith("STATE")]
['STATE_ACTIVE', 'STATE_INSENSITIVE', 'STATE_NORMAL', 'STATE_PRELIGHT', 'STATE_SELECTED']
>>> [m for m in dir(gtk.Button().get_style()) if type(getattr(gtk.Button().get_style(), m)) == type(gtk.Button().get_style().light)]
['base', 'base_gc', 'bg', 'bg_gc', 'bg_pixmap', 'dark', 'dark_gc', 'fg', 'fg_gc', 'light', 'light_gc', 'mid', 'mid_gc', 'text', 'text_aa', 'text_aa_gc', 'text_gc']

Update:

I've crafted a small pygtk script bringing up a palete of the colors of the current gtktheme. Code is:

#!/usr/bin/python

import pygtk
pygtk.require("2.0")
import gtk, pango

w = gtk.Button()

states = [m for m in dir(gtk) if m.startswith("STATE_")]
styles = [m for m in dir(w.get_style()) if type(getattr(w.get_style(), m)) == type(w.get_style().light)]
styles = [m for m in styles if not m.endswith("_gc")]

class CairoBoard(gtk.DrawingArea):
    def __init__(self):
        gtk.DrawingArea.__init__(self)
        self.connect("expose_event", self.expose)

    def expose(self, widget, event):
        context = widget.window.cairo_create()
        context.rectangle(event.area.x, event.area.y,
                          event.area.width, event.area.height)
        context.clip()
        self.draw(context)
        return False
    
    def draw (self, context):
        for x in range(len(states)):
            for y in range(len(styles)):
                style = getattr(self.get_style(), styles[y])
                state = getattr(gtk, states[x])
                
                color = style[state]
                if color == None or type(color) != gtk.gdk.Color:
                    color = gtk.gdk.Color(0,0,0)
                context.set_source_color(color)
                context.rectangle(x*150,y*50,150,50)
                
                context.fill_preserve()
                context.new_path()
                
                if color.red + color.green + color.blue < 65535:
                    context.set_source_color(gtk.gdk.Color(65535,65535,65535))
                else: context.set_source_color(gtk.gdk.Color(0,0,0))
                
                cstring = "#"
                for col in color.red, color.green, color.blue:
                    h = hex(col/256)[2:]
                    if len(h) == 1:
                        h = "0"+h
                    cstring += h
                    
                layout = self.create_pango_layout ("%s\n%s\n%s" % \
                        (styles[y], states[x][6:], cstring))
                layout.set_font_description(pango.FontDescription("Sans Serif 10"))
                context.move_to(x*150,y*50)
                context.show_layout(layout)

                context.fill_preserve()
                context.new_path()

window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.add(CairoBoard())
window.set_default_size(len(states)*150, len(styles)*50)
window.show_all()
gtk.main()