I am having some problems with reparenting

I have a program which allows you to tab several sourceview widgets (glorified textview widget), and there are 5 options for each sourceview. So if you edit 10 text files, you would get 50 sourceview widgets, and that is very expensive. Instead, I want to reuse the 10 sourceviews. An easy way to do this is via reparenting.

However, some wierd things happen when you go back to a tab you previously visited. The sourceview becomes detached and more or less floats over the window it is meant to be a child of. The problem goes away if you resize the window. As a temporary fix I increase and decrease the window size by one, sort of like jiggling it. Uncomment lines 124-131 to activate it.

I have attached a minimal example which indicates what the problem is.

Anyone have any idea what may be causing this?

Thanks in advanced


Peyman Askari


#!/usr/bin/env python

import sys
try:
	import pygtk
	pygtk.require("2.0")
except:
	pass
try:
	import gtk
	import gtk.glade
except:
	sys.exit(1)
import gtksourceview

topology_blocked=[True]
window=gtk.Window()
debug=[False]

def create_text_view(editable=True):
	"""Create the text entry widget
	"""
	
	#create source view widget
	textView=gtksourceview.SourceView()

	#make sure it is editable
	textView.set_editable(editable)

	#update its properties
	textView.set_show_line_numbers(True)
	textView.set_show_line_markers(False)
	
	#create source buffer to replace text buffer
	sourceBuffer=gtksourceview.SourceBuffer()
	textView.set_buffer(sourceBuffer)

	#update source buffer properties
	sourceBuffer.set_highlight(True)
	sourceBuffer.set_check_brackets(True)
	sourceBuffer.set_max_undo_levels(100)

	#need a languages manager to find Python language
	languagesManager=gtksourceview.SourceLanguagesManager()
	L=languagesManager.get_available_languages()

	#cycle over all languages and look for 'Python'
	for i in L:
		if i.get_name()=='Python':
			sourceLanguage=i
			break

	#assign the appropriate language to the source buffer
	sourceBuffer.set_language(sourceLanguage)

	#save a reference to the text box
	#widgets['user_defined_connection_function_text_view']=textView

	#create the scrolled window
	scrolledWindow=gtk.ScrolledWindow()

	#set the scrolled window policies
	scrolledWindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

	#first add the vBox to the scrolled windows
	scrolledWindow.add(textView)
	
	#call show on the widgets
	textView.show()
	scrolledWindow.show()

	#set a minimum size
	scrolledWindow.set_size_request(100,100)
	
	return scrolledWindow

def create_development_notebook(editable=True):
	"""Create topology with one tab, filled with a text view widget
	"""

	#the notebook as well as the labels for each tab
	notebook=gtk.Notebook()

	#set tabs to the left hand side
	notebook.set_tab_pos(gtk.POS_TOP)
	
	for i in range(3):
		label=gtk.Label("Unsaved Document %d*"%i)

		#append labels, and pass the page entries
		notebook.append_page(create_text_view(editable=editable),label)

	#call show on the notebook
	notebook.show()

	#put it in a vbox as it gets returned to a notebook append page method
	vBox=gtk.VBox()
	vBox.pack_start(notebook,expand=True,fill=True,padding=0)
	
	vBox.show()
	return (vBox,notebook)

def development_notebook_switch_page(notebook,data=None,current_page=-1):
	"""Each time the page is changed, must ensure the v_paned window is attached to current page
	"""
	global topology_blocked
	global window
	global debug
	if not topology_blocked[0]:
		old_v_box=notebook.get_nth_page(notebook.get_current_page())
		print old_v_box
		label=old_v_box.get_children()[0]
		print 'child->',old_v_box.get_children()[0]
		#label.unparent()
		#old_v_box.remove(label)
		#print 'child->',old_v_box.get_children()[0]

		new_v_box=notebook.get_nth_page(current_page)
		#new_v_box.pack_start(label,expand=False,fill=False,padding=0)
		label.reparent(new_v_box)

		print new_v_box
		
		#UNCOMMENT THE BELOW TO TEMPORARILY FIX PROBLEM
		#if debug[0]:
		#	window.resize(window.get_size()[0]+1,window.get_size()[1]+1)
		#	debug[0]=not debug[0]
		#else:
		#	window.resize(window.get_size()[0]-1,window.get_size()[1]-1)
		#	debug[0]=not debug[0]		
		#UNCOMMENT THE ABOVE TO TEMPORARILY FIX PROBLEM
		

def make_notebook():
	global topology_blocked
	global window
	

	window.connect('destroy',on_main_window_destroy)
	window.connect('delete_event',on_main_window_delete_event)

	notebook=gtk.Notebook()
	notebook.set_tab_pos(gtk.POS_BOTTOM)
	L1=gtk.Label('Causes Problems')
	L2=gtk.Label('Causes Problems')
	L3=gtk.Label('Causes Problems')

	notebook.append_page(create_development_notebook()[0],L1)
	notebook.append_page(gtk.VBox(),L2)
	notebook.append_page(gtk.VBox(),L3)

	notebook.set_current_page(0)

	#notebook.get_nth_page(0).pack_start(gtk.Label('one'),expand=False,fill=False,padding=0)

	#connect the signals
	notebook.connect('switch-page',development_notebook_switch_page)

	window.add(notebook)

	window.show_all()

	topology_blocked[0]=False

def on_main_window_delete_event(widget,data=None):
	"""Return false so the window gets destroyed
	"""
	return False

def on_main_window_destroy(widget,data=None):
	"""Return false so the window gets destroyed
	"""
	gtk.main_quit()

if __name__ == '__main__':

	try:
		make_notebook()


		gtk.main()
	except KeyboardInterrupt:
		sys.exit(1)



_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to