Beispiel #1
0
    def get_toolbar(self):
        toolbar = gtk.Toolbar()
        toolbar.set_style({
                'default': False,
                'both': gtk.TOOLBAR_BOTH,
                'text': gtk.TOOLBAR_TEXT,
                'icons': gtk.TOOLBAR_ICONS}[CONFIG['client.toolbar']])

        self.widget.pack_start(toolbar, expand=False, fill=True)

        for icon in ['bold', 'italic', 'underline']:
            button = gtk.ToggleToolButton('gtk-%s' % icon)
            button.connect('toggled', self.toggle_props, icon)
            toolbar.insert(button, -1)
            self.tag_widgets[icon] = button

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        for name, options, active in [
                ('family', FAMILIES, FAMILIES.index('normal')),
                ('size', SIZES, SIZES.index('4')),
                ]:
            try:
                combobox = gtk.ComboBoxText()
            except AttributeError:
                combobox = gtk.combo_box_new_text()
            for option in options:
                combobox.append_text(option)
            combobox.set_active(active)
            combobox.set_focus_on_click(False)
            combobox.connect('changed', self.change_props, name)
            tool = gtk.ToolItem()
            tool.add(combobox)
            toolbar.insert(tool, -1)
            self.tag_widgets[name] = combobox

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        button = None
        for icon in ['left', 'center', 'right', 'fill']:
            name = icon
            if icon == 'fill':
                name = 'justify'
            stock_id = 'gtk-justify-%s' % icon
            if hasattr(gtk.RadioToolButton, 'new_with_stock_from_widget'):
                button = gtk.RadioToolButton.new_with_stock_from_widget(
                    button, stock_id)
            else:
                button = gtk.RadioToolButton(button, stock_id)
            button.set_active(icon == 'left')
            button.connect('toggled', self.toggle_justification, name)
            toolbar.insert(button, -1)
            self.tag_widgets[name] = button

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        for icon, label in [
                ('foreground', _('Foreground')),
                # TODO ('background', _('Background')),
                ]:
            button = gtk.ToolButton('tryton-text-%s' % icon)
            button.set_label(label)
            button.connect('clicked', self.toggle_color, icon)
            toolbar.insert(button, -1)
            self.tag_widgets[icon] = button

        return toolbar
Beispiel #2
0
    def get_toolbar(self, textview):
        toolbar = Gtk.Toolbar()
        toolbar.set_style({
            'default': False,
            'both': Gtk.ToolbarStyle.BOTH,
            'text': Gtk.ToolbarStyle.TEXT,
            'icons': Gtk.ToolbarStyle.ICONS,
        }[CONFIG['client.toolbar']])
        tag_widgets = self.tag_widgets[textview] = {}

        for icon, label in [
            ('bold', _("Bold")),
            ('italic', _("Italic")),
            ('underline', _("Underline")),
        ]:
            button = Gtk.ToggleToolButton()
            button.set_icon_widget(
                IconFactory.get_image('tryton-format-%s' % icon,
                                      Gtk.IconSize.SMALL_TOOLBAR))
            button.set_label(label)
            button.connect('toggled', self.toggle_props, icon, textview)
            toolbar.insert(button, -1)
            tag_widgets[icon] = button

        toolbar.insert(Gtk.SeparatorToolItem(), -1)

        for name, options, active in [
            ('family', FAMILIES, FAMILIES.index('normal')),
            ('size', SIZES, SIZES.index('4')),
        ]:
            combobox = Gtk.ComboBoxText()
            for option in options:
                combobox.append_text(option)
            combobox.set_active(active)
            combobox.set_focus_on_click(False)
            combobox.connect('changed', self.change_props, name, textview)
            tool = Gtk.ToolItem()
            tool.add(combobox)
            toolbar.insert(tool, -1)
            tag_widgets[name] = combobox

        toolbar.insert(Gtk.SeparatorToolItem(), -1)

        button = None
        for name, label in [
            ('left', _("Align Left")),
            ('center', _("Align Center")),
            ('right', _("Align Right")),
            ('justify', _("Justify")),
        ]:
            icon = 'tryton-format-align-%s' % name
            button = Gtk.RadioToolButton.new_from_widget(button)
            button.set_icon_widget(
                IconFactory.get_image(icon, Gtk.IconSize.SMALL_TOOLBAR))
            button.set_active(icon == 'left')
            button.set_label(label)
            button.connect('toggled', self.toggle_justification, name,
                           textview)
            toolbar.insert(button, -1)
            tag_widgets[name] = button

        toolbar.insert(Gtk.SeparatorToolItem(), -1)

        for icon, label in [
            ('foreground', _("Foreground Color")),
                # TODO ('background', _('Background')),
        ]:
            button = Gtk.ToolButton()
            if icon == 'foreground':
                button.set_icon_widget(
                    IconFactory.get_image('tryton-format-color-text',
                                          Gtk.IconSize.SMALL_TOOLBAR))
            button.set_label(label)
            button.connect('clicked', self.toggle_color, icon, textview)
            toolbar.insert(button, -1)
            tag_widgets[icon] = button

        return toolbar
Beispiel #3
0
    def detect_style(self, *args):
        try:
            start, end = self.text_buffer.get_selection_bounds()
        except ValueError:
            start = end = self.text_buffer.get_iter_at_mark(
                self.text_buffer.get_insert())

        def toggle_button(name, values):
            try:
                value, = values
            except ValueError:
                value = False
            button = self.tag_widgets[name]
            button.handler_block_by_func(self.toggle_props)
            button.set_active(value)
            button.handler_unblock_by_func(self.toggle_props)

        def set_combobox(name, indexes):
            try:
                index, = indexes
            except ValueError:
                index = -1
            combobox = self.tag_widgets[name]
            combobox.handler_block_by_func(self.change_props)
            combobox.set_active(index)
            combobox.handler_unblock_by_func(self.change_props)

        def toggle_justification(names, value):
            if len(names) != 1:
                value = False
            for name in names:
                button = self.tag_widgets[name]
                button.handler_block_by_func(self.toggle_justification)
                button.set_active(value)
                button.handler_unblock_by_func(self.toggle_justification)

        bolds, italics, underlines = set(), set(), set()
        families, sizes, justifications = set(), set(), set()

        iter_ = start.copy()
        while True:
            bold, italic, underline = False, False, False
            family = FAMILIES.index('normal')
            size = SIZES.index('4')
            justification = 'left'

            for tag in iter_.get_tags():
                if not tag.props.name:
                    continue
                elif tag.props.name == 'bold':
                    bold = True
                elif tag.props.name == 'italic':
                    italic = True
                elif tag.props.name == 'underline':
                    underline = True
                elif tag.props.name.startswith('family'):
                    _, family = tag.props.name.split()
                    family = FAMILIES.index(family)
                elif tag.props.name.startswith('size'):
                    _, size = tag.props.name.split()
                    size = SIZES.index(size)
                elif tag.props.name.startswith('justification'):
                    _, justification = tag.props.name.split()
            bolds.add(bold)
            italics.add(italic)
            underlines.add(underline)
            families.add(family)
            sizes.add(size)
            justifications.add(justification)

            iter_.forward_char()
            if iter_.compare(end) > 0:
                iter_ = end
            if iter_.compare(end) == 0:
                break

        for name, values in [
                ('bold', bolds),
                ('italic', italics),
                ('underline', underlines)]:
            toggle_button(name, values)
        set_combobox('family', families)
        set_combobox('size', sizes)
        toggle_justification(justifications, True)
Beispiel #4
0
    def __init__(self, view, attrs):
        super(RichTextBox, self).__init__(view, attrs)
        self.text_buffer = gtk.TextBuffer()
        setup_tags(self.text_buffer)
        self.text_buffer.register_serialize_format(MIME, serialize, None)
        self.text_buffer.register_deserialize_format(MIME, deserialize, None)
        self.text_buffer.connect_after('insert-text', self.insert_text_style)
        self.textview.set_buffer(self.text_buffer)
        self.textview.connect_after('move-cursor', self.detect_style)
        self.textview.connect('button-release-event', self.detect_style)

        self.toolbar = gtk.Toolbar()
        self.toolbar.set_style({
            'default': False,
            'both': gtk.TOOLBAR_BOTH,
            'text': gtk.TOOLBAR_TEXT,
            'icons': gtk.TOOLBAR_ICONS
        }[CONFIG['client.toolbar']])

        self.widget.pack_start(self.toolbar, expand=False, fill=True)
        self.tag_widgets = {}
        self.tags = {}

        for icon in ['bold', 'italic', 'underline']:
            button = gtk.ToggleToolButton('gtk-%s' % icon)
            button.connect('toggled', self.toggle_props, icon)
            self.toolbar.insert(button, -1)
            self.tag_widgets[icon] = button

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        for name, options, active in [
            ('family', FAMILIES, FAMILIES.index('normal')),
            ('size', SIZES, SIZES.index('4')),
        ]:
            try:
                combobox = gtk.ComboBoxText()
            except AttributeError:
                combobox = gtk.combo_box_new_text()
            for option in options:
                combobox.append_text(option)
            combobox.set_active(active)
            combobox.set_focus_on_click(False)
            combobox.connect('changed', self.change_props, name)
            tool = gtk.ToolItem()
            tool.add(combobox)
            self.toolbar.insert(tool, -1)
            self.tag_widgets[name] = combobox

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        button = None
        for icon in ['left', 'center', 'right', 'fill']:
            name = icon
            if icon == 'fill':
                name = 'justify'
            button = gtk.RadioToolButton(button, 'gtk-justify-%s' % icon)
            button.set_active(icon == 'left')
            button.connect('toggled', self.toggle_justification, name)
            self.toolbar.insert(button, -1)
            self.tag_widgets[name] = button

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        self.colors = {}
        for icon, label in [
            ('foreground', _('Foreground')),
                # TODO ('background', _('Background')),
        ]:
            button = gtk.ToolButton('tryton-text-%s' % icon)
            button.set_label(label)
            button.connect('clicked', self.toggle_color, icon)
            self.toolbar.insert(button, -1)
            self.tag_widgets[icon] = button
Beispiel #5
0
def add_toolbar(textview):
    toolbar = Gtk.Toolbar()

    tag_widgets = {}
    colors = {}

    for icon, label in [
            ('bold', _("Bold")),
            ('italic', _("Italic")),
            ('underline', _("Underline")),
            ]:
        button = Gtk.ToggleToolButton()
        button.set_icon_widget(IconFactory.get_image(
                'tryton-format-%s' % icon,
                Gtk.IconSize.SMALL_TOOLBAR))
        button.set_label(label)
        button.connect('toggled', _toggle_props, icon, textview)
        toolbar.insert(button, -1)
        tag_widgets[icon] = button

    toolbar.insert(Gtk.SeparatorToolItem(), -1)

    for name, options, active in [
            ('family', FAMILIES, FAMILIES.index('normal')),
            ('size', SIZES, SIZES.index('4')),
            ]:
        combobox = Gtk.ComboBoxText()
        for option in options:
            combobox.append_text(option)
        combobox.set_active(active)
        combobox.set_focus_on_click(False)
        combobox.connect('changed', _change_props, name, textview)
        tool = Gtk.ToolItem()
        tool.add(combobox)
        toolbar.insert(tool, -1)
        tag_widgets[name] = combobox

    toolbar.insert(Gtk.SeparatorToolItem(), -1)

    button = None
    for name, label in [
            ('left', _("Align Left")),
            ('center', _("Align Center")),
            ('right', _("Align Right")),
            ('justify', _("Justify")),
            ]:
        icon = 'tryton-format-align-%s' % name
        button = Gtk.RadioToolButton.new_from_widget(button)
        button.set_icon_widget(IconFactory.get_image(
                icon, Gtk.IconSize.SMALL_TOOLBAR))
        button.set_active(icon == 'left')
        button.set_label(label)
        button.connect(
            'toggled', _toggle_justification, name, textview)
        toolbar.insert(button, -1)
        tag_widgets[name] = button

    toolbar.insert(Gtk.SeparatorToolItem(), -1)

    for icon, label in [
            ('foreground', _("Foreground Color")),
            # TODO ('background', _('Background')),
            ]:
        button = Gtk.ToolButton()
        if icon == 'foreground':
            button.set_icon_widget(IconFactory.get_image(
                    'tryton-format-color-text',
                    Gtk.IconSize.SMALL_TOOLBAR))
        button.set_label(label)
        button.connect('clicked', _toggle_color, icon, textview, colors)
        toolbar.insert(button, -1)
        tag_widgets[icon] = button

    buffer_ = textview.get_buffer()
    buffer_.connect_after(
        'insert-text', _insert_text_style, tag_widgets)
    textview.connect_after(
        'move-cursor',
        lambda *a: _detect_style(textview, tag_widgets, colors))
    textview.connect_after(
        'button-release-event',
        lambda *a: _detect_style(textview, tag_widgets, colors))

    return toolbar
Beispiel #6
0
    def __init__(self, view, attrs):
        super(RichTextBox, self).__init__(view, attrs)
        self.text_buffer = gtk.TextBuffer()
        setup_tags(self.text_buffer)
        self.text_buffer.register_serialize_format(
            MIME, serialize, None)
        self.text_buffer.register_deserialize_format(
            MIME, deserialize, None)
        self.text_buffer.connect_after('insert-text', self.insert_text_style)
        self.textview.set_buffer(self.text_buffer)
        self.textview.connect_after('move-cursor', self.detect_style)
        self.textview.connect('button-release-event', self.detect_style)

        self.toolbar = gtk.Toolbar()
        self.toolbar.set_style({
                'default': False,
                'both': gtk.TOOLBAR_BOTH,
                'text': gtk.TOOLBAR_TEXT,
                'icons': gtk.TOOLBAR_ICONS}[CONFIG['client.toolbar']])

        self.widget.pack_start(self.toolbar, expand=False, fill=True)
        self.tag_widgets = {}
        self.tags = {}

        for icon in ['bold', 'italic', 'underline']:
            button = gtk.ToggleToolButton('gtk-%s' % icon)
            button.connect('toggled', self.toggle_props, icon)
            self.toolbar.insert(button, -1)
            self.tag_widgets[icon] = button

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        for name, options, active in [
                ('family', FAMILIES, FAMILIES.index('normal')),
                ('size', SIZES, SIZES.index('4')),
                ]:
            try:
                combobox = gtk.ComboBoxText()
            except AttributeError:
                combobox = gtk.combo_box_new_text()
            for option in options:
                combobox.append_text(option)
            combobox.set_active(active)
            combobox.set_focus_on_click(False)
            combobox.connect('changed', self.change_props, name)
            tool = gtk.ToolItem()
            tool.add(combobox)
            self.toolbar.insert(tool, -1)
            self.tag_widgets[name] = combobox

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        button = None
        for icon in ['left', 'center', 'right', 'fill']:
            name = icon
            if icon == 'fill':
                name = 'justify'
            button = gtk.RadioToolButton(button, 'gtk-justify-%s' % icon)
            button.set_active(icon == 'left')
            button.connect('toggled', self.toggle_justification, name)
            self.toolbar.insert(button, -1)
            self.tag_widgets[name] = button

        self.toolbar.insert(gtk.SeparatorToolItem(), -1)

        self.colors = {}
        for icon, label in [
                ('foreground', _('Foreground')),
                # TODO ('background', _('Background')),
                ]:
            button = gtk.ToolButton('tryton-text-%s' % icon)
            button.set_label(label)
            button.connect('clicked', self.toggle_color, icon)
            self.toolbar.insert(button, -1)
            self.tag_widgets[icon] = button
Beispiel #7
0
    def detect_style(self, *args):
        try:
            start, end = self.text_buffer.get_selection_bounds()
        except ValueError:
            start = end = self.text_buffer.get_iter_at_mark(
                self.text_buffer.get_insert())

        def toggle_button(name, values):
            try:
                value, = values
            except ValueError:
                value = False
            button = self.tag_widgets[name]
            button.handler_block_by_func(self.toggle_props)
            button.set_active(value)
            button.handler_unblock_by_func(self.toggle_props)

        def set_combobox(name, indexes):
            try:
                index, = indexes
            except ValueError:
                index = -1
            combobox = self.tag_widgets[name]
            combobox.handler_block_by_func(self.change_props)
            combobox.set_active(index)
            combobox.handler_unblock_by_func(self.change_props)

        def toggle_justification(names, value):
            if len(names) != 1:
                value = False
            for name in names:
                button = self.tag_widgets[name]
                button.handler_block_by_func(self.toggle_justification)
                button.set_active(value)
                button.handler_unblock_by_func(self.toggle_justification)

        bolds, italics, underlines = set(), set(), set()
        families, sizes, justifications = set(), set(), set()

        iter_ = start.copy()
        while True:
            bold, italic, underline = False, False, False
            family = FAMILIES.index('normal')
            size = SIZES.index('4')
            justification = 'left'

            for tag in iter_.get_tags():
                if not tag.props.name:
                    continue
                elif tag.props.name == 'bold':
                    bold = True
                elif tag.props.name == 'italic':
                    italic = True
                elif tag.props.name == 'underline':
                    underline = True
                elif tag.props.name.startswith('family'):
                    _, family = tag.props.name.split()
                    family = FAMILIES.index(family)
                elif tag.props.name.startswith('size'):
                    _, size = tag.props.name.split()
                    size = SIZES.index(size)
                elif tag.props.name.startswith('justification'):
                    _, justification = tag.props.name.split()
            bolds.add(bold)
            italics.add(italic)
            underlines.add(underline)
            families.add(family)
            sizes.add(size)
            justifications.add(justification)

            iter_.forward_char()
            if iter_.compare(end) > 0:
                iter_ = end
            if iter_.compare(end) == 0:
                break

        for name, values in [
                ('bold', bolds),
                ('italic', italics),
                ('underline', underlines)]:
            toggle_button(name, values)
        set_combobox('family', families)
        set_combobox('size', sizes)
        toggle_justification(justifications, True)
Beispiel #8
0
    def get_toolbar(self, textview):
        toolbar = gtk.Toolbar()
        toolbar.set_style({
            'default': False,
            'both': gtk.TOOLBAR_BOTH,
            'text': gtk.TOOLBAR_TEXT,
            'icons': gtk.TOOLBAR_ICONS
        }[CONFIG['client.toolbar']])
        tag_widgets = self.tag_widgets[textview] = {}

        for icon in ['bold', 'italic', 'underline']:
            button = gtk.ToggleToolButton()
            button.set_icon_widget(
                IconFactory.get_image('tryton-format-%s' % icon,
                                      gtk.ICON_SIZE_SMALL_TOOLBAR))
            button.connect('toggled', self.toggle_props, icon, textview)
            toolbar.insert(button, -1)
            tag_widgets[icon] = button

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        for name, options, active in [
            ('family', FAMILIES, FAMILIES.index('normal')),
            ('size', SIZES, SIZES.index('4')),
        ]:
            try:
                combobox = gtk.ComboBoxText()
            except AttributeError:
                combobox = gtk.combo_box_new_text()
            for option in options:
                combobox.append_text(option)
            combobox.set_active(active)
            combobox.set_focus_on_click(False)
            combobox.connect('changed', self.change_props, name, textview)
            tool = gtk.ToolItem()
            tool.add(combobox)
            toolbar.insert(tool, -1)
            tag_widgets[name] = combobox

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        button = None
        for name in ['left', 'center', 'right', 'justify']:
            icon = 'tryton-format-align-%s' % name
            button = gtk.RadioToolButton.new_from_widget(button)
            button.set_icon_widget(
                IconFactory.get_image(icon, gtk.ICON_SIZE_SMALL_TOOLBAR))
            button.set_active(icon == 'left')
            button.connect('toggled', self.toggle_justification, name,
                           textview)
            toolbar.insert(button, -1)
            tag_widgets[name] = button

        toolbar.insert(gtk.SeparatorToolItem(), -1)

        for icon, label in [
            ('foreground', _('Foreground')),
                # TODO ('background', _('Background')),
        ]:
            button = gtk.ToolButton()
            if icon == 'foreground':
                button.set_icon_widget(
                    IconFactory.get_image('tryton-format-color-text',
                                          gtk.ICON_SIZE_SMALL_TOOLBAR))
            button.set_label(label)
            button.connect('clicked', self.toggle_color, icon, textview)
            toolbar.insert(button, -1)
            tag_widgets[icon] = button

        return toolbar