#!/usr/bin/python
# -*- coding: utf-8 -*-

import gtk, pango
import gtksourceview2 as gtksourceview

#class HtmlCode(gtk.ScrolledWindow):
#	"""Affichage du code HTML"""
"""	def __init__(self):
		gtk.ScrolledWindow.__init__(self)
		self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		
		# Création de la zone de texte
		self.sourcebuffer = gtk.TextBuffer()
		
		self.sourceview = gtk.TextView(self.sourcebuffer)
		self.sourceview.set_wrap_mode(gtk.WRAP_WORD_CHAR)
		self.sourceview.modify_font(pango.FontDescription("Monospace"))
		
		# Ajout de la zone de texte dans la ScrolledWindow
		self.add_with_viewport(self.sourceview)
		
	def set_text(self, text):
		self.sourcebuffer.set_text(text)
		
	def get_text(self):
		return self.sourcebuffer.get_text(self.sourcebuffer.get_start_iter(), self.sourcebuffer.get_end_iter(), True)"""


class HtmlCode(gtk.ScrolledWindow):
	"""Affichage du code HTML"""
	def __init__(self):
		# Modification des dossiers ou pygtksourceview va chercher les thèmes et les langages
		self.langmanager = gtksourceview.LanguageManager()
		self.langmanager.set_search_path(['lib/sourceview/syntax',])
		
		self.stylemanager = gtksourceview.StyleSchemeManager()
		self.stylemanager.set_search_path(['lib/sourceview/style',])
		
		gtk.ScrolledWindow.__init__(self)
		self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		
		# Création de la zone de texte
		self.sourcebuffer = gtksourceview.Buffer()
		self.sourcebuffer.set_language(self.langmanager.get_language('html'))
		self.sourcebuffer.set_style_scheme(self.stylemanager.get_scheme('geany'))
		
		self.sourceview = gtksourceview.View(self.sourcebuffer)
		self.sourceview.set_tab_width(4)
		self.sourceview.set_indent_on_tab(True)
		self.sourceview.set_auto_indent(True)
		self.sourceview.set_highlight_current_line(True)
		self.sourceview.set_wrap_mode(gtk.WRAP_WORD_CHAR)
		self.sourceview.modify_font(pango.FontDescription("Monospace"))
		
		# Ajout de la zone de texte dans la ScrolledWindow
		self.add_with_viewport(self.sourceview)
	
	def undo(self):
		self.sourcebuffer.undo()
		
	def redo(self):
		self.sourcebuffer.redo()
		
	def select_all(self):
		self.sourcebuffer.select_range(self.sourcebuffer.get_start_iter(), self.sourcebuffer.get_end_iter())
		
	def cut(self):
		self.sourcebuffer.cut_clipboard(gtk.clipboard_get(), self.sourceview.get_editable())
		
	def copy(self):
		self.sourcebuffer.copy_clipboard(gtk.clipboard_get())
		
	def paste(self):
		self.sourcebuffer.paste_clipboard(gtk.clipboard_get(), None, self.sourceview.get_editable())
		
	def delete_selection(self):
		self.sourcebuffer.delete_selection(True, self.sourceview.get_editable())
		
	def set_text(self, text):
		self.sourcebuffer.set_text(text)
		
	def get_text(self):
		return self.sourcebuffer.get_text(self.sourcebuffer.get_start_iter(), self.sourcebuffer.get_end_iter(), True)
