#!/usr/bin/env python
_ = lambda x : x

import sys

try:
  import libglade
except ImportError, e:
  print __file__+': Missing own modules',e
  sys.exit(1)


import gtk

class InfoDialog(libglade.GladeWrapper):
  """ Package Information Dialog """

  def __init__(self, filename):
    """ Initialize the textview and buffers """

    libglade.GladeWrapper.__init__(self, filename, "infodialog1")    
    buffer1= self.packinfo1.get_buffer()
    buffer2= self.packinfo2.get_buffer()
    self.infodialog1.connect ("response", lambda d, r: d.hide())
    self.infodialog1.connect ("delete-event", self.on_delete_event)
    self.create_tags(buffer1)
    self.info_buffer= buffer1
    self.create_tags(buffer2)
    self.info_buffer2= buffer2
    

  def set_fonts(self, font):
    self.packinfo1.modify_font(font)
    self.packinfo2.modify_font(font)

  def show_pack_info(self, pack, config):
    """ 
    Show Package info:
    pack is the package
    config is a distribution configuration object
    """
    # Page 1
    buffer= self.info_buffer
    #     clear buffer
    buffer.set_text("", 0)

    iter = buffer.get_iter_at_offset(0)

    name= pack.get_property('name')
    buffer.insert_with_tags_by_name(iter, name +'\n', 'heading','center')
    props= [u'title',u'description',u'nfiles',u'size']

    for p in props:
      s= str(p) + ':'
      buffer.insert_with_tags_by_name(iter, s , 'bold')      
      s= unicode(pack.get_property(p)) + u'\n'
      buffer.insert_with_tags_by_name(iter, s.encode('utf-8') , 'word_wrap')


    rpath= u'\nRoot Path: '+ config.get_attribute('root') + '\n'
    buffer.insert_with_tags_by_name(iter, rpath , 'bold')      
    f='docfiles'
    s= unicode(f) + u'\n'
    buffer.insert_with_tags_by_name(iter, s , 'bold')      
    s= unicode(pack.print_files(f))
    buffer.insert_with_tags_by_name(iter, s , 'list')

    self.infodialog1.set_title(_('Package') +' ' + name)

    # Page 2
    buffer= self.info_buffer2
    #     clear buffer
    buffer.set_text("", 0)

    iter = buffer.get_iter_at_offset(0)

    name= pack.get_property('name')
    buffer.insert_with_tags_by_name(iter, name +'\n', 'heading','center')
    allprops= pack.list_properties()

    props= [u'name',u'title',u'description',u'nfiles',u'size',u'runfiles',u'docfiles',
            u'sourcefiles', u'timeinstalled', u'timepackaged']

    for p in allprops:
      if p not in props:
        s= unicode(p) + ':'
        buffer.insert_with_tags_by_name(iter, s , 'bold')      
        s= unicode(pack.get_property(p)) + u'\n'
        buffer.insert_with_tags_by_name(iter, s.encode('utf-8') , 'word_wrap')

    s= pack.get_time_installed(full=True)
    if s:
      buffer.insert_with_tags_by_name(iter, 'timeinstalled: ' , 'bold')      
      buffer.insert_with_tags_by_name(iter, str(s) +'\n' , 'word_wrap')

    s= pack.get_time_packaged(True)
    if s:
      buffer.insert_with_tags_by_name(iter, 'timepackaged: '  , 'bold')      
      buffer.insert_with_tags_by_name(iter, str(s) + '\n', 'word_wrap')

    filetype= [u'runfiles',u'sourcefiles']
    buffer.insert_with_tags_by_name(iter, rpath , 'bold')      
    for f in filetype:
      s= unicode(f) + u'\n'
      buffer.insert_with_tags_by_name(iter, s , 'bold')      
      s= unicode(pack.print_files(f))
      buffer.insert_with_tags_by_name(iter, s , 'list')

    response=self.infodialog1.show()



  def create_tags(self, text_buffer):
    '''
    Create a bunch of tags. Note that it's also possible to
    create tags with gtk.text_tag_new() then add them to the
    tag table for the buffer, text_buffer.create_tag() is
    just a convenience function. Also note that you don't have
    to give tags a name; pass None for the name to create an
    anonymous tag.

    In any real app, another useful optimization would be to create
    a GtkTextTagTable in advance, and reuse the same tag table for
    all the buffers with the same tag set, instead of creating
    new copies of the same tags for every buffer.

    Tags are assigned default priorities in order of addition to the
    tag table. That is, tags created later that affect the same text
    property affected by an earlier tag will override the earlier
    tag. You can modify tag priorities with
    gtk.text_tag_set_priority().
    '''
    import pango
    text_buffer.create_tag("heading",
                           weight=pango.WEIGHT_BOLD, size=15*pango.SCALE
                           )

    text_buffer.create_tag("italic", style=pango.STYLE_ITALIC)

    text_buffer.create_tag("bold", weight=pango.WEIGHT_BOLD)

    # points times the pango.SCALE factor
#     text_buffer.create_tag("big", size=20 * pango.SCALE)

#     text_buffer.create_tag("xx-small", scale=pango.SCALE_XX_SMALL)

#     text_buffer.create_tag("x-large", scale=pango.SCALE_X_LARGE)

#     text_buffer.create_tag("monospace", family="monospace")

#     text_buffer.create_tag("blue_foreground", foreground="blue")

#     text_buffer.create_tag("red_background", background="red")

#     text_buffer.create_tag("big_gap_before_line", pixels_above_lines=30)

#     text_buffer.create_tag("big_gap_after_line", pixels_below_lines=30)

#     text_buffer.create_tag("double_spaced_line", pixels_inside_wrap=10)

    text_buffer.create_tag("word_wrap", wrap_mode=gtk.WRAP_WORD)

#     text_buffer.create_tag("char_wrap", wrap_mode=gtk.WRAP_CHAR)

#     text_buffer.create_tag("no_wrap", wrap_mode=gtk.WRAP_NONE)

    text_buffer.create_tag("center", justification=gtk.JUSTIFY_CENTER)

#     text_buffer.create_tag("right_justify", justification=gtk.JUSTIFY_RIGHT)

#     text_buffer.create_tag("wide_margins", left_margin=50, right_margin=50)

    text_buffer.create_tag("list", left_margin=50)

#     text_buffer.create_tag("strikethrough", strikethrough=True)

#     text_buffer.create_tag("underline",
#                 underline=pango.UNDERLINE_SINGLE)

#     text_buffer.create_tag("double_underline",
#                 underline=pango.UNDERLINE_DOUBLE)

#     text_buffer.create_tag("superscript",
#                            rise=10 * pango.SCALE,      # 10 pixels
#                            size=8 * pango.SCALE)       #  8 points

#     text_buffer.create_tag("subscript",
#                            rise=-10 * pango.SCALE,     # 10 pixels
#                            size=8 * pango.SCALE)       #  8 points


  def on_delete_event(self, widget, event):
    return True


