class HighlightProperty(object):
    def __init__(self, property_name, property):
        self.__create_widgets()

        self.property_name = property_name

        self.property_label = property[0].capitalize()
        self.example = property[1]
        self.bold = property[2]
        self.italic = property[3]
        self.underline = property[4]

        self.text_color = property[5]
        self.highlight_color = property[6]

        self.__connect_buttons()

    def __create_widgets(self):
        self.property_name_label = HIGEntryLabel("")
        self.example_label = HIGEntryLabel("")
        self.bold_tg_button = HIGToggleButton("", gtk.STOCK_BOLD)
        self.italic_tg_button = HIGToggleButton("", gtk.STOCK_ITALIC)
        self.underline_tg_button = HIGToggleButton("", gtk.STOCK_UNDERLINE)
        self.text_color_button = HIGButton(
                _("Text"), stock=gtk.STOCK_SELECT_COLOR)
        self.highlight_color_button = HIGButton(
                _("Highlight"), stock=gtk.STOCK_SELECT_COLOR)

    def __connect_buttons(self):
        self.bold_tg_button.connect("toggled", self.update_example)
        self.italic_tg_button.connect("toggled", self.update_example)
        self.underline_tg_button.connect("toggled", self.update_example)

        self.text_color_button.connect("clicked", self.text_color_dialog)
        self.highlight_color_button.connect(
                "clicked", self.highlight_color_dialog)

    ####################################
    # Text color dialog

    def text_color_dialog(self, widget):
        color_dialog = gtk.ColorSelectionDialog(
                "%s %s" % (self.label, _("text color")))
        color_dialog.colorsel.set_current_color(self.text_color)

        color_dialog.ok_button.connect(
                "clicked", self.text_color_dialog_ok, color_dialog)
        color_dialog.cancel_button.connect(
                "clicked", self.text_color_dialog_cancel, color_dialog)
        color_dialog.connect(
                "delete-event", self.text_color_dialog_close, color_dialog)

        color_dialog.run()

    def text_color_dialog_ok(self, widget, color_dialog):
        self.text_color = color_dialog.colorsel.get_current_color()
        color_dialog.destroy()
        self.update_example()

    def text_color_dialog_cancel(self, widget, color_dialog):
        color_dialog.destroy()

    def text_color_dialog_close(self, widget, extra, color_dialog):
        color_dialog.destroy()

    #########################################
    # Highlight color dialog
    def highlight_color_dialog(self, widget):
        color_dialog = gtk.ColorSelectionDialog(
                "%s %s" % (self.property_name, _("highlight color")))
        color_dialog.colorsel.set_current_color(self.highlight_color)

        color_dialog.ok_button.connect(
                "clicked", self.highlight_color_dialog_ok, color_dialog)
        color_dialog.cancel_button.connect(
                "clicked", self.highlight_color_dialog_cancel,
                color_dialog)
        color_dialog.connect(
                "delete-event", self.highlight_color_dialog_close,
                color_dialog)

        color_dialog.run()

    def highlight_color_dialog_ok(self, widget, color_dialog):
        self.highlight_color = color_dialog.colorsel.get_current_color()
        color_dialog.destroy()
        self.update_example()

    def highlight_color_dialog_cancel(self, widget, color_dialog):
        color_dialog.destroy()

    def highlight_color_dialog_close(self, widget, extra, color_dialog):
        color_dialog.destroy()

    def update_example(self, widget=None):
        start = 0
        end = len(self.example)

        attributes = pango.AttrList()

        attributes.insert(
                pango.AttrForeground(self.text_color.red,
                    self.text_color.green, self.text_color.blue, start, end))
        attributes.insert(pango.AttrBackground(self.highlight_color.red,
                                               self.highlight_color.green,
                                               self.highlight_color.blue,
                                               start, end))

        # Bold verification
        if self.bold_tg_button.get_active():
            attributes.insert(pango.AttrWeight(pango.WEIGHT_HEAVY, start, end))
        else:
            attributes.insert(
                    pango.AttrWeight(pango.WEIGHT_NORMAL, start, end))

        # Italic verification
        if self.italic_tg_button.get_active():
            attributes.insert(pango.AttrStyle(pango.STYLE_ITALIC, start, end))
        else:
            attributes.insert(pango.AttrStyle(pango.STYLE_NORMAL, start, end))

        # Underline verification
        if self.underline_tg_button.get_active():
            attributes.insert(
                    pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end))
        else:
            attributes.insert(
                    pango.AttrUnderline(pango.UNDERLINE_NONE, start, end))

        self.example_label.set_attributes(attributes)

    def show_bold(self, widget):
        self.example_label.set_markup("<>")

    def get_example(self):
        return self.example_label.get_text()

    def set_example(self, example):
        self.example_label.set_text(example)

    def get_bold(self):
        if self.bold_tg_button.get_active():
            return 1
        return 0

    def set_bold(self, bold):
        self.bold_tg_button.set_active(bold)

    def get_italic(self):
        if self.italic_tg_button.get_active():
            return 1
        return 0

    def set_italic(self, italic):
        self.italic_tg_button.set_active(italic)

    def get_underline(self):
        if self.underline_tg_button.get_active():
            return 1
        return 0

    def set_underline(self, underline):
        self.underline_tg_button.set_active(underline)

    def get_label(self):
        return self.property_name_label.get_text()

    def set_label(self, label):
        self.property_name_label.set_text(label)

    label = property(get_label, set_label)
    example = property(get_example, set_example)
    bold = property(get_bold, set_bold)
    italic = property(get_italic, set_italic)
    underline = property(get_underline, set_underline)
class HighlightProperty(object):
    def __init__(self, property_name, property):
        self.__create_widgets()

        self.property_name = property_name

        self.property_label = property[0].capitalize()
        self.example = property[1]
        self.bold = property[2]
        self.italic = property[3]
        self.underline = property[4]

        self.text_color = property[5]
        self.highlight_color = property[6]

        self.__connect_buttons()

    def __create_widgets(self):
        self.property_name_label = HIGEntryLabel("")
        self.example_label = HIGEntryLabel("")
        self.bold_tg_button = HIGToggleButton("", gtk.STOCK_BOLD)
        self.italic_tg_button = HIGToggleButton("", gtk.STOCK_ITALIC)
        self.underline_tg_button = HIGToggleButton("", gtk.STOCK_UNDERLINE)
        self.text_color_button = HIGButton(_("Text"),
                                           stock=gtk.STOCK_SELECT_COLOR)
        self.highlight_color_button = HIGButton(_("Highlight"),
                                                stock=gtk.STOCK_SELECT_COLOR)

    def __connect_buttons(self):
        self.bold_tg_button.connect("toggled", self.update_example)
        self.italic_tg_button.connect("toggled", self.update_example)
        self.underline_tg_button.connect("toggled", self.update_example)

        self.text_color_button.connect("clicked", self.text_color_dialog)
        self.highlight_color_button.connect("clicked",
                                            self.highlight_color_dialog)

    ####################################
    # Text color dialog

    def text_color_dialog(self, widget):
        color_dialog = gtk.ColorSelectionDialog("%s %s" %
                                                (self.label, _("text color")))
        color_dialog.colorsel.set_current_color(self.text_color)

        color_dialog.ok_button.connect("clicked", self.text_color_dialog_ok,
                                       color_dialog)
        color_dialog.cancel_button.connect("clicked",
                                           self.text_color_dialog_cancel,
                                           color_dialog)
        color_dialog.connect("delete-event", self.text_color_dialog_close,
                             color_dialog)

        color_dialog.run()

    def text_color_dialog_ok(self, widget, color_dialog):
        self.text_color = color_dialog.colorsel.get_current_color()
        color_dialog.destroy()
        self.update_example()

    def text_color_dialog_cancel(self, widget, color_dialog):
        color_dialog.destroy()

    def text_color_dialog_close(self, widget, extra, color_dialog):
        color_dialog.destroy()

    #########################################
    # Highlight color dialog
    def highlight_color_dialog(self, widget):
        color_dialog = gtk.ColorSelectionDialog(
            "%s %s" % (self.property_name, _("highlight color")))
        color_dialog.colorsel.set_current_color(self.highlight_color)

        color_dialog.ok_button.connect("clicked",
                                       self.highlight_color_dialog_ok,
                                       color_dialog)
        color_dialog.cancel_button.connect("clicked",
                                           self.highlight_color_dialog_cancel,
                                           color_dialog)
        color_dialog.connect("delete-event", self.highlight_color_dialog_close,
                             color_dialog)

        color_dialog.run()

    def highlight_color_dialog_ok(self, widget, color_dialog):
        self.highlight_color = color_dialog.colorsel.get_current_color()
        color_dialog.destroy()
        self.update_example()

    def highlight_color_dialog_cancel(self, widget, color_dialog):
        color_dialog.destroy()

    def highlight_color_dialog_close(self, widget, extra, color_dialog):
        color_dialog.destroy()

    def update_example(self, widget=None):
        start = 0
        end = len(self.example)

        attributes = pango.AttrList()

        attributes.insert(
            pango.AttrForeground(self.text_color.red, self.text_color.green,
                                 self.text_color.blue, start, end))
        attributes.insert(
            pango.AttrBackground(self.highlight_color.red,
                                 self.highlight_color.green,
                                 self.highlight_color.blue, start, end))

        # Bold verification
        if self.bold_tg_button.get_active():
            attributes.insert(pango.AttrWeight(pango.WEIGHT_HEAVY, start, end))
        else:
            attributes.insert(pango.AttrWeight(pango.WEIGHT_NORMAL, start,
                                               end))

        # Italic verification
        if self.italic_tg_button.get_active():
            attributes.insert(pango.AttrStyle(pango.STYLE_ITALIC, start, end))
        else:
            attributes.insert(pango.AttrStyle(pango.STYLE_NORMAL, start, end))

        # Underline verification
        if self.underline_tg_button.get_active():
            attributes.insert(
                pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end))
        else:
            attributes.insert(
                pango.AttrUnderline(pango.UNDERLINE_NONE, start, end))

        self.example_label.set_attributes(attributes)

    def show_bold(self, widget):
        self.example_label.set_markup("<>")

    def get_example(self):
        return self.example_label.get_text()

    def set_example(self, example):
        self.example_label.set_text(example)

    def get_bold(self):
        if self.bold_tg_button.get_active():
            return 1
        return 0

    def set_bold(self, bold):
        self.bold_tg_button.set_active(bold)

    def get_italic(self):
        if self.italic_tg_button.get_active():
            return 1
        return 0

    def set_italic(self, italic):
        self.italic_tg_button.set_active(italic)

    def get_underline(self):
        if self.underline_tg_button.get_active():
            return 1
        return 0

    def set_underline(self, underline):
        self.underline_tg_button.set_active(underline)

    def get_label(self):
        return self.property_name_label.get_text()

    def set_label(self, label):
        self.property_name_label.set_text(label)

    label = property(get_label, set_label)
    example = property(get_example, set_example)
    bold = property(get_bold, set_bold)
    italic = property(get_italic, set_italic)
    underline = property(get_underline, set_underline)