#!/usr/bin/python

"""
Implements a macro recorder for The Gimp.

It works as follows:
 1) Register the recorder as extension without parameters.
    <Toolbox>/Record ...
    Register some <Image>/... menu entries for recorded macros ?
 2) User invokes Record. After letting the user select a filename
    this registers a temporary procedure which get's called for
    every pdb from than on until the user closes the Record dialog.
 3) Remove temporary procedure. Write out the recorded macro.
 4) Maybe map the recorded macro to one of the <Image> entries above

Copyright (c) 2000, Hans Breuer <Hans@Breuer.org>
"""
from gimpfu import *
RECORD_FN_NAME = "extension_record_this_call"

fout = None

# Recording functions
def record_start(name) :
	"Gets called to start macro recording"
	global fout
	fout = open("d:\\graph\\gimp\\plug-ins\\" + name + ".log", "w")

def record_this_call(s) :
	"Installed as temporary pdb procedure. Gets called to record single function calls."
	#print ":", s
	if fout :
		fout.write(s + "\n")
		fout.flush()

def record_end() :
	"Gets called to end macro recording."
	if fout :
		fout.close()

def my_idle() :
	import gtk
	# ?? gtk.gdk_threads_enter()
	#print "."
	#gimp.run_temp()
	#gimp.extension_process(0)
	# ?? gtk.gdk_threads_leave()
	return FALSE
	
def my_install_temp_proc(func_name,blurb, help, author, copyright,
					   date, menupath, imagetypes,
					   plugin_type, params, results,
					   function) :
					   
	gimp.install_temp_proc(func_name,blurb, help, author, copyright,
					   date, menupath, imagetypes,
					   plugin_type, params, results)
	import gimpfu
	gimpfu._registered_plugins_[func_name] = (blurb, help, author, copyright,
					   date, menupath, imagetypes,
					   plugin_type, params, results,
					   function)

# Standard Gimp Plug-In functions
def python_fu_record () :
	"""Let the user select a macro name and install a temporary procedure
	   which gets called while this function is running. When it finishes
	   write out the recorded macro.
	"""
	import gtk
	import gimpui

	dialog = gtk.GtkDialog()
	dialog.set_title("Record Macro")

	box = gtk.GtkVBox()
	# NOT dialog.add(box)
	dialog.vbox.pack_start(box)
	box.show()

	# create a text entry field 
	entry = gtk.GtkEntry()
	entry.set_text("unnamed")
	box.pack_start(entry)
	entry.show()

	def record_clicked(button, win=dialog, entry=entry):
		import gtk
		name = entry.get_text ()
		#entry.set_editable(FALSE)
		entry.set_sensitive(FALSE)
		button.set_sensitive(FALSE)
		record_start(name)
		# register extension_record_this_call
		my_install_temp_proc (	RECORD_FN_NAME,
							"THIS IS CALLED INTERNALY ONLY!",
							"record a single pdb call",
							"Hans Breuer <Hans@Breuer.Org>",
							"Hans Breuer",
							"2000",
							None, # no menu path
							"", # don't care for image type
							TEMPORARY,
							[
								(PF_INT32, "run_mode", "non interactive"),
								(PF_STRING, "name", "function name called")
							],
							[], # no results needed
							record_this_call) #gimpfu.py eats run_mode !
	
		win.func = [my_idle, 0]
		win.func[1] = gtk.idle_add(my_idle)
		# FIXME: does this help ???
		#gimp.extension_ack()

	def done_clicked(button, win=dialog, user_data=None):
		import gtk
		win.hide()
		gtk.idle_remove(win.func[1])
		win.func[1] = 0
		# remove temporary procedure
		gimp.uninstall_temp_proc(RECORD_FN_NAME)
		# ToDO: ???
		record_end()
		gtk.mainquit()
		
	b = gtk.GtkButton("Record")
	b.set_flags(gtk.CAN_DEFAULT)
	dialog.action_area.pack_start(b)
	b.grab_default()
	b.connect("clicked", record_clicked)
	b.show()

	b = gtk.GtkButton("Done")
	b.set_flags(gtk.CAN_DEFAULT)
	dialog.action_area.pack_start(b)
	b.connect("clicked", done_clicked)
	b.show()

	dialog.show()
	# run the main loop
	gtk.mainloop()
	dialog.destroy()

	return None # done it

def _run(func_name, params):
	# need to overwrite gimpfu._run to delegate calls to temp_proc
	print "_run", func_name
	if RECORD_FN_NAME == func_name :
		print params
		apply (record_this_call, params)
		pass
	else :
		import gimpfu
		gimpfu._run(func_name, params)

register(
	"python_fu_record",
	"Records a macro in Python.",
	"Records a macro - better: every pdb call made while the recorder is"
	"running. Writes the result to a new Python macro/plug-in.",
	"Hans Breuer",
	"Hans Breuer <Hans@Breuer.Org>",
	"2000",
	"<Toolbox>/Xtns/Python-Fu/Record ...",
	"",
	[
	  # Parameters need to be defined interactive
	],
	[],
	python_fu_record)

main()
#import gimpfu
#gimp.main(None, None, gimpfu._query, _run)
