Gtk2-Ex-WYSIWYG

 view release on metacpan or  search on metacpan

lib/Gtk2/Ex/WYSIWYG.pm  view on Meta::CPAN


This module is a subclass of L<Gtk2::Table> containing both a text view
and a 'toolbar' to allow a user to edit and format text. It can serialise
to a plain text block and a tag stack, or to incomplete HTML (the output is
not a complete HTML document, but can be included inside one). It can also
'deserialise' from this same data to easily allow content from one WYSIWYG to
be transfered to another - the more efficient of these is the text/tag stack,
however the HTML form can be more easily stored.

An undo/redo stack is also included, as well as a modification to the text
view's popup menu to allow the user to set the wrap mode with ease.

It should be noted that WYSIWYG emulates paragraphs by using \n\s*\n as a
paragraph separator. The leading newline in the sequence will belong to the
leading paragraph, and the rest to 'interparagraph space'. This has some
implications - interparagraph space honours vertical space (ie, extra newlines
will be rendered when exporting to HTML) but not horizontal space - any spaces
you put inside interparagraph space will be ignored, as will any font
formatting you apply.

It also means that should two paragraphs be joined by a user edit (either by

lib/Gtk2/Ex/WYSIWYG.pm  view on Meta::CPAN

BEGIN {
  my ($TT, $TT_L); # Fake tooltips and label for same
  my $TOOLTIPS;    # 'Real' tooltips widget for buttons

  sub _init_tooltips {
    my $self = shift;
    return if defined $TOOLTIPS;
    $TOOLTIPS = Gtk2::Tooltips->new; # Class wide. Would be nice if there was a
                                     # way of determining if a tooltip widget
                                     # was already created and use that
    $TT = Gtk2::Window->new('popup'); # The 'fake' link tooltip window
    $TT_L = Gtk2::Label->new;
    $TT_L->set_padding(4, 4);
    $TT->set_resizable(0);
    $TT->set_decorated(0);
    $TT->set_position('mouse'); # We modify this on popup
    # Would be good to get the current theme colour - on ubuntu this works, but
    # using blackbox on freebsd results in a colour that is 'too yellow'
    $TT->modify_bg('normal',
                   Gtk2::Gdk::Color->new(245 << 8, 245 << 8, 181 << 8));
    my $frame = Gtk2::Frame->new;
    $frame->set_shadow_type('etched-in');
    $frame->add($TT_L);
    $TT->add($frame);
  }

lib/Gtk2/Ex/WYSIWYG.pm  view on Meta::CPAN

  $buf->signal_connect_after('delete-range' => sub {$self->_after_delete(@_)});
  $buf->signal_connect('apply-tag' => sub {$self->_on_apply_tag(@_)});
  $buf->signal_connect('remove-tag' => sub {$self->_on_remove_tag(@_)});
  $self->{Cursor}{Current} = 'Text';
  $self->{Cursor}{Text} = Gtk2::Gdk::Cursor->new('xterm');
  $self->{Cursor}{Link} = Gtk2::Gdk::Cursor->new('hand2');
  $self->{Text}->signal_connect(motion_notify_event =>
                                sub {$self->_on_motion_notify(@_)});
  $self->{Text}->signal_connect('focus-out-event' =>
                                sub {$self->_on_unfocus_text});
  $self->{Text}->signal_connect('populate-popup',
                                sub {$self->_on_popup(@_)});
}

##########
# _init_font_list - examines the pango context and sets available fonts,
#                   the default font and the default size
##########
sub _init_font_list {
  my $self = shift;
  my $c = $self->get_pango_context;
  $BUTTONS{Font}{Default} = $c->get_font_description->get_family;

lib/Gtk2/Ex/WYSIWYG.pm  view on Meta::CPAN

##########
sub _on_remove_tag {
  my $self = shift;
  my ($buf, $tag, $s, $e) = @_;
  $self->_record_undo(UNDO_REMOVE_TAG, $s->get_offset, $e->get_offset, $tag)
    if $self->_is_my_tag($tag);
  return 0;
}

##########
# _on_popup - modify the default popup window to include a Wrap menu
##########
sub _on_popup {
  my $self = shift;
  my ($txt, $menu) = @_;
  my $currmode = $txt->get_wrap_mode;
  my $mt = Gtk2::MenuItem->new('Wrap');
  my $sub = Gtk2::Menu->new;
  $mt->set_submenu($sub);
  my $grp = undef;
  for my $it (['None', 'none'], ['Character', 'char'],
              ['Word', 'word'], ['Word, then character', 'word-char']) {
    my $mi = Gtk2::RadioMenuItem->new($grp, $it->[0]);

lib/Gtk2/Ex/WYSIWYG.pm  view on Meta::CPAN

      }
      $item->signal_connect(activate => sub {
                              $self->_item_selected($label, $dat);
                            });
      $item->show;
      $menu->append($item);
    }
    $sel = 0 if not defined $sel;
    $menu->set_active($sel);
    # Popup the menu
    $menu->popup(undef, undef, undef, undef, $self, undef);
    $menu->popup(undef, undef, '_menu_pos', $self, $self, undef);
    my ($mx, $my) = $menu->get_size_request;
    my ($bx, $by) = $self->get_size_request;
    $menu->set_size_request($bx, -1) if $mx < $bx;
    my $active = $menu->get_active;
    ($active) = $menu->get_children if not defined $active;
    $menu->select_item($active);
    return 0;
  }

  # !!! _menu_pos assumes that the menu _HAS ALREADY BEEN POPPED UP!_



( run in 2.693 seconds using v1.01-cache-2.11-cpan-364913b4093 )