#!/usr/bin/python

from gimpfu import *
import gimpui
import gtk

def testGrid(timg, tdrawable):
    d = gtk.Dialog('Test Grid PDB', None,
                   gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                   (gtk.STOCK_CLOSE, gtk.RESPONSE_ACCEPT))

    addDimensionAdjuster(d, timg,
                         'spacing',
                         pdb.gimp_image_grid_get_spacing,
                         pdb.gimp_image_grid_set_spacing,
                         1, MAX_IMAGE_SIZE)
    addDimensionAdjuster(d, timg,
                         'offset',
                         pdb.gimp_image_grid_get_offset,
                         pdb.gimp_image_grid_set_offset,
                         -MAX_IMAGE_SIZE, MAX_IMAGE_SIZE)

    addColorAdjuster(d, timg,
                     'fg color',
                     pdb.gimp_image_grid_get_fgcolor,
                     pdb.gimp_image_grid_set_fgcolor)
    addColorAdjuster(d, timg,
                     'bg color',
                     pdb.gimp_image_grid_get_bgcolor,
                     pdb.gimp_image_grid_set_bgcolor)

    l = gtk.Label('Grid Style')
    d.vbox.pack_start(l, False, True, 0)
    l.show()
    c = gtk.combo_box_new_text()
    c.insert_text(GRID_DOTS         , 'GRID_DOTS')
    c.insert_text(GRID_INTERSECTIONS, 'GRID_INTERSECTIONS')
    c.insert_text(GRID_ON_OFF_DASH  , 'GRID_ON_OFF_DASH')
    c.insert_text(GRID_DOUBLE_DASH  , 'GRID_DOUBLE_DASH')
    c.insert_text(GRID_SOLID        , 'GRID_SOLID')
    d.vbox.pack_start(c, False, True, 0)
    c.set_active(pdb.gimp_image_grid_get_style(timg))
    c.connect('changed',
              lambda w, c: pdb.gimp_image_grid_set_style(timg, c.get_active()), c)
    c.show()

    d.run()
    d.destroy()


def addDimensionAdjuster(dialog, timg, label, getter, setter, min, max):
    h, v = getter(timg)

    l = gtk.Label('Horizontal ' + label)
    dialog.vbox.pack_start(l, False, True, 0)
    l.show()
    a = gtk.Adjustment(value=h,
                       lower=min,
                       upper=max,
                       step_incr=1,
                       page_incr=10)
    s1 = gtk.SpinButton(a)
    dialog.vbox.pack_start(s1, False, True, 0)
    s1.show()

    l = gtk.Label('Vertical ' + label)
    dialog.vbox.pack_start(l, False, True, 0)
    l.show()
    a = gtk.Adjustment(value=v,
                       lower=min,
                       upper=max,
                       step_incr=1,
                       page_incr=10)
    s2 = gtk.SpinButton(a)
    dialog.vbox.pack_start(s2, False, True, 0)
    s2.show()

    s1.connect('value-changed',
               lambda s1, s2, timg, setter:
                   setter(timg, s1.get_value(), s2.get_value()),
               s2, timg, setter)
    s2.connect('value-changed',
               lambda s2, s1, timg, setter:
                   setter(timg, s1.get_value(), s2.get_value()),
               s1, timg, setter)


def dimensionChanged(s1, s2, timg, setter):
    setter(timg, s1.get_value(), s2.get_value())


def addColorAdjuster(dialog, timg, label, getter, setter):
    l = gtk.Label(label)
    dialog.vbox.pack_start(l, False, True, 0)
    l.show()
    b = gimpui.ColourSelector(getter(timg))
    dialog.vbox.pack_start(b, False, True, 0)
    b.connect('notify::style',
              lambda w, s, b: setter(timg, b.get_value()), b)
    b.show()


register(
    'GridTest',
    'A test for the image grid PDB API',
    'help_text',
    'Sylvain FORET',
    'Sylvain FORET',
    '2004',
    '<Image>/Image/Grid PDB API Test',
    'RGB*, GRAY*',
    [], [],
    testGrid)

main()

