#  Show Unicode code points plugin
#  coding: utf-8
#
#  Copyright © 2008  Santhosh Thottingal
#  Released under the GPLV3+ license

import os
import gtk
import gedit
import pango
from externaltools.ElementTree import Element


class RunShowCodepointPlugin(gedit.Plugin):
  """
  A simple plugin that shows the unicode code points of the selected text
  """

  def run_document(self, action, window):
    """
    Just Do It!
    """
    # read in the first line of the buffer...
    buff  = window.get_active_view().get_buffer()
    pango_context= window.get_active_view().get_pango_context()
    panel = window.get_data("ExternalToolsPluginWindowData")._output_buffer
    try:
            start, end = buff.get_selection_bounds()
    except ValueError:
            start, end = buff.get_bounds()
    data  = buff.get_text(start, end)        
    # now check if it contains a bang line...
    if data:
		panel.write(repr((data).decode("utf8")))
		panel.write("\n")
		
	  
  def activate(self, window):
    """
    Setup stuff
    """
    actions = [
      ('RunShowCode', gtk.STOCK_EXECUTE, 'Show Unicode Code Points', 'F12', 'Show Unicode Code Points', self.run_document),
    ]
    # store per window data in the window object
    windowdata = {}
    window.set_data('RunShowCodepointPluginWindowDataKey', windowdata)
    windowdata['action_group'] = gtk.ActionGroup('GeditRunShowCodepointPluginActions')
    windowdata['action_group'].add_actions(actions, window)
    manager = window.get_ui_manager()
    manager.insert_action_group(windowdata['action_group'], -1)
    ui_str = """
    <ui>
      <menubar name="MenuBar">
        <menu name="ToolsMenu" action="Tools">
          <placeholder name="ToolsOps_3">
            <menuitem name="RunShowCode" action="RunShowCode"/>
            <separator/>
          </placeholder>
        </menu>
      </menubar>
    </ui>
    """
    windowdata['ui_id'] = manager.add_ui_from_string(ui_str)
    window.set_data('RunShowCodepointPluginInfo', windowdata)

  def deactivate(self, window):
    """
    Teardown stuff
    """
    windowdata = window.get_data('RunShowCodepointPluginWindowDataKey')
    manager    = window.get_ui_manager()
    manager.remove_ui(windowdata['ui_id'])
    manager.remove_action_group(windowdata['action_group'])

  def update_ui(self, window):
    """
    UI Callback
    """
    view       = window.get_active_view()
    windowdata = window.get_data('RunShowCodepointPluginWindowDataKey')
    windowdata['action_group'].set_sensitive(bool(view)) # and view.get_editable()))
