diff options
author | Pascal Rigaux <pixel@mandriva.com> | 2004-12-02 12:15:10 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 2004-12-02 12:15:10 +0000 |
commit | f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e (patch) | |
tree | bfb02d11ee11ef8cfa3ee61b0ed4800ca71deee6 | |
parent | f62e71fa54930821f209b187b9526e6992fa75b4 (diff) | |
download | drakx-backup-do-not-use-f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e.tar drakx-backup-do-not-use-f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e.tar.gz drakx-backup-do-not-use-f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e.tar.bz2 drakx-backup-do-not-use-f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e.tar.xz drakx-backup-do-not-use-f6f78109c1b145d2fc0d4c8d9f7fe31b00c9730e.zip |
future is in mygtk2, ugtk2 must die (and it will die, but not that soon (to keep compatibility))
-rw-r--r-- | perl-install/mygtk2.pm | 635 | ||||
-rw-r--r-- | perl-install/ugtk2.pm | 269 |
2 files changed, 727 insertions, 177 deletions
diff --git a/perl-install/mygtk2.pm b/perl-install/mygtk2.pm new file mode 100644 index 000000000..cb93ef680 --- /dev/null +++ b/perl-install/mygtk2.pm @@ -0,0 +1,635 @@ +package mygtk2; + +use diagnostics; +use strict; +use lang; + +our @ISA = qw(Exporter); +our @EXPORT = qw(gtknew gtkset gtkadd); + +use c; +use log; +use common; + +use Gtk2; +use Gtk2::Gdk::Keysyms; + +unless ($::no_ugtk_init) { + !check_for_xserver() and print("Cannot be run in console mode.\n"), c::_exit(0); + $::one_message_has_been_translated and warn("N() was called from $::one_message_has_been_translated BEFORE gtk2 initialisation, replace it with a N_() AND a translate() later.\n"), c::_exit(1); + + Gtk2->init; + c::bind_textdomain_codeset($_, 'UTF8') foreach 'libDrakX', @::textdomains; + $::need_utf8_i18n = 1; +} +Gtk2->croak_execeptions if (!$::no_ugtk_init || $::isInstall) && 0.95 < $Gtk2::VERSION; + + + +sub gtknew { + my $class = shift; + if (@_ % 2 != 0) { + internal_error("gtknew $class: bad options @_"); + } + if (my $r = find { ref $_->[0] } group_by2(@_)) { + internal_error("gtknew $class: $r should be a string in @_"); + } + _gtk(undef, $class, 'gtknew', @_); +} + +sub gtkset { + my $w = shift; + my $class = ref($w); + if (@_ % 2 != 0) { + internal_error("gtkset $class: bad options @_"); + } + if (my $r = find { ref $_->[0] } group_by2(@_)) { + internal_error("gtkset $class: $r should be a string in @_"); + } + $class =~ s/^Gtk2::(Gdk::)?// or internal_error("gtkset unknown class $class"); + + _gtk($w, $class, 'gtkset', @_); +} + +sub gtkadd { + my $w = shift; + my $class = ref($w); + if (@_ % 2 != 0) { + internal_error("gtkadd $class: bad options @_"); + } + if (my $r = find { ref $_->[0] } group_by2(@_)) { + internal_error("gtkadd $class: $r should be a string in @_"); + } + $class =~ s/^Gtk2::(Gdk::)?// or internal_error("gtkadd unknown class $class"); + + _gtk('gtkadd', $w, $class, @_); +} + + +my %refs; + +sub gtkval_register { + my ($w, $ref, $sub) = @_; + $w->{_ref} = $ref; + $w->signal_connect(destroy => sub { + delete $refs{$ref}{$w}; + delete $refs{$ref} if !%{$refs{$ref}}; + }); + $refs{$ref}{$w} = [ $sub, $w ]; +} +sub gtkval_modify { + my ($ref, $val, @to_skip) = @_; + $$ref = $val; + foreach (values %{$refs{$ref} || {}}) { + my ($f, @para) = @$_; + $f->(@para) if !member($f, @to_skip); + } +} + +my $global_tooltips; + +sub _gtk { + my ($w, $class, $action, %opts) = @_; + + my @known_opts = ('width', 'height', 'position', 'can_focus', 'widget_name', 'grab_focus', 'tip', 'size_group'); + + if (my $f = $mygtk2::{"_gtk__$class"}) { + $w = $f->($w, \%opts, $class, $action); + } else { + internal_error("$action $class: unknown class"); + } + + $w->set_size_request($opts{width} || -1, $opts{height} || -1) if $opts{width} || $opts{height}; + $w->set_uposition($opts{position}[0], $opts{position}[1]) if $opts{position}; + $w->set_name($opts{widget_name}) if $opts{widget_name}; + $w->can_focus($opts{can_focus}) if defined $opts{can_focus}; + $w->grab_focus if $opts{grab_focus}; + $opts{size_group}->add_widget($w) if $opts{size_group}; + if ($opts{tip}) { + $global_tooltips ||= Gtk2::Tooltips->new; + $global_tooltips->set_tip($w, $opts{tip}); + } + + delete $opts{$_} foreach @known_opts; + if (%opts) { + internal_error("$action $class: unknown option(s) " . join(', ', keys %opts)); + } + $w; +} + + +sub _gtk__Button { &_gtk_any_Button } +sub _gtk__ToggleButton { &_gtk_any_Button } +sub _gtk__CheckButton { &_gtk_any_Button } +sub _gtk_any_Button { + my ($w, $opts, $class) = @_; + + if (!$opts->{image}) { + add2hash_($opts, { mnemonic => 1 }); + } + + if (!$w) { + $w = $opts->{image} || !exists $opts->{text} ? "Gtk2::$class"->new : + delete $opts->{mnemonic} ? "Gtk2::$class"->new_with_label(delete $opts->{text}) : + "Gtk2::$class"->new_with_mnemonic(delete $opts->{text}); + } + + if (my $image = delete $opts->{image}) { + $w->add($image); + $image->show; + } + $w->set_sensitive(delete $opts->{sensitive}) if exists $opts->{sensitive}; + $w->set_relief(delete $opts->{relief}) if exists $opts->{relief}; + + if ($class eq 'Button') { + $w->signal_connect(clicked => delete $opts->{clicked}) if exists $opts->{clicked}; + } else { + if (my $active_ref = delete $opts->{active_ref}) { + my $set = sub { $w->set_active($$active_ref) }; + $w->signal_connect(toggled => sub { + gtkval_modify($active_ref, $w->get_active, $set); + }); + gtkval_register($w, $active_ref, $set); + gtkval_register($w, $active_ref, delete $opts->{toggled}) if exists $opts->{toggled}; + $set->(); + } else { + $w->set_active(delete $opts->{active}) if exists $opts->{active}; + $w->signal_connect(toggled => delete $opts->{toggled}) if exists $opts->{toggled}; + } + } + $w; +} + +sub _gtk__CheckMenuItem { + my ($w, $opts, $class) = @_; + + if (!$w) { + add2hash_($opts, { mnemonic => 1 }); + + $w = $opts->{image} || !exists $opts->{text} ? "Gtk2::$class"->new : + delete $opts->{mnemonic} ? "Gtk2::$class"->new_with_label(delete $opts->{text}) : + "Gtk2::$class"->new_with_mnemonic(delete $opts->{text}); + } + + $w->set_active(delete $opts->{active}) if exists $opts->{active}; + $w->signal_connect(toggled => delete $opts->{toggled}) if exists $opts->{toggled}; + $w; +} + +sub _gtk___SpinButton { + my ($w, $opts) = @_; + + if (!$w) { + $opts->{adjustment} ||= do { + add2hash_($opts, { step_increment => 1, page_increment => 5, page_size => 1, value => delete $opts->{lower} }); + Gtk2::Adjustment->new(delete $opts->{value}, delete $opts->{lower}, delete $opts->{upper}, delete $opts->{step_increment}, delete $opts->{page_increment}, delete $opts->{page_size}); + }; + $w = Gtk2::SpinButton->new(delete $opts->{adjustment}, delete $opts->{climb_rate} || 0, delete $opts->{digits} || 0); + } + + $w->signal_connect(value_changed => delete $opts->{value_changed}) if exists $opts->{value_changed}; + $w; +} + +sub _gtk__HScale { + my ($w, $opts) = @_; + + if (!$w) { + $opts->{adjustment} ||= do { + add2hash_($opts, { step_increment => 1, page_increment => 5, page_size => 1, value => delete $opts->{lower} }); + Gtk2::Adjustment->new(delete $opts->{value}, delete $opts->{lower}, (delete $opts->{upper}) + 1, delete $opts->{step_increment}, delete $opts->{page_increment}, delete $opts->{page_size}); + }; + $w = Gtk2::HScale->new(delete $opts->{adjustment}); + } + + $w->signal_connect(value_changed => delete $opts->{value_changed}) if exists $opts->{value_changed}; + $w; +} + +sub _gtk__VSeparator { &_gtk_any_simple } +sub _gtk__HSeparator { &_gtk_any_simple } +sub _gtk__Calendar { &_gtk_any_simple } + +sub _gtk__Pixbuf { + my ($w, $opts) = @_; + + if (!$w) { + my $name = delete $opts->{file} or internal_error("missing file"); + my $file = _find_imgfile($name) or internal_error("can't find $name"); + $w = Gtk2::Gdk::Pixbuf->new_from_file($file); + } + $w; +} + +sub _gtk__Image { + my ($w, $opts, $class) = @_; + + if (!$w) { + $w = "Gtk2::$class"->new; + $w->{format} = delete $opts->{format} if exists $opts->{format}; + } + + if (my $name = delete $opts->{file}) { + my $file = _find_imgfile(may_apply($w->{format}, $name)) or internal_error("can't find $name"); + $w->set_from_file($file); + } elsif (my $file_ref = delete $opts->{file_ref}) { + my $set = sub { + my $file = _find_imgfile(may_apply($w->{format}, $$file_ref)) or internal_error("can't find $$file_ref"); + $w->set_from_file($file); + }; + gtkval_register($w, $file_ref, $set); + $set->(); + } + $w; +} + +sub _gtk__WrappedLabel { + my ($w, $opts) = @_; + + $opts->{line_wrap} = 1; + _gtk__Label($w, $opts); +} + +sub _gtk__Label { + my ($w, $opts) = @_; + + if ($w) { + $w->set_text(delete $opts->{text}) if exists $opts->{text}; + } else { + $w = exists $opts->{text} ? Gtk2::Label->new(delete $opts->{text}) : Gtk2::Label->new; + $w->set_justify(delete $opts->{justify}) if exists $opts->{justify}; + $w->set_line_wrap(delete $opts->{line_wrap}) if exists $opts->{line_wrap}; + $w->set_alignment(@{delete $opts->{alignment}}) if exists $opts->{alignment}; + $w->modify_font(Gtk2::Pango::FontDescription->from_string(delete $opts->{font})) if exists $opts->{font}; + } + + $w->set_markup(delete $opts->{text_markup}) if exists $opts->{text_markup}; + $w; +} + +sub _gtk__Entry { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::Entry->new; + $w->set_editable(delete $opts->{editable}) if exists $opts->{editable}; + } + + $w->set_text(delete $opts->{text}) if exists $opts->{text}; + $w->signal_connect(key_press_event => delete $opts->{key_press_event}) if exists $opts->{key_press_event}; + $w; +} + +sub _gtk__TextView { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::TextView->new; + $w->set_editable(delete $opts->{editable}) if exists $opts->{editable}; + $w->set_wrap_mode(delete $opts->{wrap_mode}) if exists $opts->{wrap_mode}; + $w->set_cursor_visible(delete $opts->{cursor_visible}) if exists $opts->{cursor_visible}; + } + + _text_insert($w, delete $opts->{text}) if exists $opts->{text}; + $w; +} + +sub _gtk__ComboBox { + my ($w, $opts, $_class, $action) = @_; + + if (!$w) { + $w = Gtk2::ComboBox->new_text; + $w->{format} = delete $opts->{format} if exists $opts->{format}; + + } + if (exists $opts->{list}) { + $w->{list} = delete $opts->{list}; + $w->{formatted_list} = $w->{format} ? [ map { $w->{format}($_) } @{$w->{list}} ] : $w->{list}; + $w->append_text($_) foreach @{$w->{formatted_list}}; + } + + if ($action eq 'gtknew') { + if (my $text_ref = delete $opts->{text_ref}) { + my $set = sub { + my $val = may_apply($w->{format}, $$text_ref); + warn "setting $val\n"; + eval { $w->set_active(find_index { $_ eq $val } @{$w->{formatted_list}}) }; + }; + $w->signal_connect(changed => sub { + gtkval_modify($text_ref, $w->{list}[$w->get_active], $set); + }); + gtkval_register($w, $text_ref, $set); + gtkval_register($w, $text_ref, delete $opts->{changed}) if exists $opts->{changed}; + $set->(); + } else { + my $val = delete $opts->{text}; + eval { $w->set_active(find_index { $_ eq $val } @{$w->{formatted_list}}) } if defined $val; + $w->signal_connect(changed => delete $opts->{changed}) if exists $opts->{changed}; + } + } + $w; +} + +sub _gtk__ScrolledWindow { + my ($w, $opts, $_class, $action) = @_; + + if (!$w) { + $w = Gtk2::ScrolledWindow->new(undef, undef); + $w->set_policy(@{delete $opts->{policy} || [ 'automatic', 'automatic' ]}); + } + + if (my $child = delete $opts->{child}) { + if (member(ref($child), qw(Gtk2::Layout Gtk2::Text Gtk2::TextView Gtk2::TreeView))) { + $w->add($child); + } else { + $w->add_with_viewport($child); + } + $child->set_focus_vadjustment($w->get_vadjustment) if $child->can('set_focus_vadjustment'); + $child->set_left_margin(6) if ref($child) =~ /Gtk2::TextView/; + $child->show; + + $w->child->set_shadow_type(delete $opts->{shadow_type}) if exists $opts->{shadow_type}; + + if ($action eq 'gtknew' && ref($child) =~ /Gtk2::TextView|Gtk2::TreeView/) { + $w = gtknew('Frame', shadow_type => 'in', child => $w); + } + } + $w; +} + +sub _gtk__Frame { + my ($w, $opts) = @_; + + if ($w) { + $w->set_label(delete $opts->{text}) if exists $opts->{text}; + } else { + $w = Gtk2::Frame->new(delete $opts->{text}); + $w->set_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; + $w->set_shadow_type(delete $opts->{shadow_type}) if exists $opts->{shadow_type}; + } + + if (my $child = delete $opts->{child}) { + $w->add($child); + $child->show; + } + $w; +} + +sub _gtk__Window { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::Window->new(delete $opts->{type} || 'toplevel'); + $w->set_modal(delete $opts->{modal}) if exists $opts->{modal}; + $w->set_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; + $w->set_shadow_type(delete $opts->{shadow_type}) if exists $opts->{shadow_type}; + } + $w->set_title(delete $opts->{title}) if exists $opts->{title}; + + if (my $child = delete $opts->{child}) { + $w->add($child); + $child->show; + } + $w; +} + +sub _gtk__Dialog { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::Dialog->new; + + $w->set_modal(delete $opts->{modal}) if exists $opts->{modal}; + $w->set_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; + $w->set_transient_for(delete $opts->{transient_for}) if exists $opts->{transient_for}; + $w->set_position(delete $opts->{position_policy}) if exists $opts->{position_policy}; + } + $w; +} + +sub _gtk__FileSelection { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::FileSelection->new(delete $opts->{title} || ''); + gtkset($w->ok_button, %{delete $opts->{ok_button}}) if exists $opts->{ok_button}; + gtkset($w->cancel_button, %{delete $opts->{cancel_button}}) if exists $opts->{cancel_button}; + } + $w; +} + +sub _gtk__VBox { &_gtk_any_Box } +sub _gtk__HBox { &_gtk_any_Box } +sub _gtk_any_Box { + my ($w, $opts, $class, $action) = @_; + + if (!$w) { + $w = "Gtk2::$class"->new(0,0); + $w->set_homogeneous(delete $opts->{homogenous}) if exists $opts->{homogenous}; + $w->set_spacing(delete $opts->{spacing}) if exists $opts->{spacing}; + $w->set_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; + } elsif ($action eq 'gtkset') { + $_->destroy foreach $w->get_children; + } + + _gtknew_handle_children($w, $opts); + $w; +} + +sub _gtk__VButtonBox { &_gtk_any_ButtonBox } +sub _gtk__HButtonBox { &_gtk_any_ButtonBox } +sub _gtk_any_ButtonBox { + my ($w, $opts, $class, $action) = @_; + + if (!$w) { + $w = "Gtk2::$class"->new; + $w->set_layout(delete $opts->{layout} || 'spread'); + } elsif ($action eq 'gtkset') { + $_->destroy foreach $w->get_children; + } + + _gtknew_handle_children($w, $opts); + $w; +} + +sub _gtk__Notebook { + my ($w, $opts) = @_; + + if (!$w) { + $w = Gtk2::Notebook->new; + $w->set_property('show-tabs', delete $opts->{show_tabs}) if exists $opts->{show_tabs}; + $w->set_property('show-border', delete $opts->{show_border}) if exists $opts->{show_border}; + } + + if (exists $opts->{children}) { + foreach (group_by2(@{delete $opts->{children}})) { + my ($title, $page) = @$_; + $w->append_page($page, $title); + $page->show; + $title->show; + } + } + $w; +} + +sub _gtk__Table { + my ($w, $opts) = @_; + + if (!$w) { + add2hash_($opts, { xpadding => 5, ypadding => 0, border_width => $::isInstall ? 3 : 10 }); + + $w = Gtk2::Table->new(0, 0, delete $opts->{homogeneous} || 0); + $w->set_col_spacings(delete $opts->{col_spacings} || 0); + $w->set_row_spacings(delete $opts->{row_spacings} || 0); + $w->set_border_width(delete $opts->{border_width}); + $w->{$_} = delete $opts->{$_} foreach 'xpadding', 'ypadding', 'mcc'; + } + + each_index { + my ($i, $l) = ($::i, $_); + each_index { + my $j = $::i; + if ($_) { + ref $_ or $_ = Gtk2::WrappedLabel->new($_); + $j != $#$l && !$w->{mcc} ? + $w->attach($_, $j, $j + 1, $i, $i + 1, + 'fill', 'fill', $w->{xpadding}, $w->{ypadding}) : + $w->attach($_, $j, $j + 1, $i, $i + 1, + ['expand', 'fill'], ref($_) eq 'Gtk2::ScrolledWindow' || $_->get_data('must_grow') ? ['expand', 'fill'] : [], 0, 0); + $_->show; + } + } @$l; + } @{delete $opts->{children} || []}; + + $w; +} + +sub _gtk_any_simple { + my ($w, $_opts, $class) = @_; + + $w ||= "Gtk2::$class"->new; +} + +sub _gtknew_handle_children { + my ($w, $opts) = @_; + + my @child = exists $opts->{children_tight} ? map { [ 0, $_ ] } @{delete $opts->{children_tight}} : + exists $opts->{children_loose} ? map { [ 1, $_ ] } @{delete $opts->{children_loose}} : + exists $opts->{children} ? group_by2(@{delete $opts->{children}}) : (); + + foreach (@child) { + my ($fill, $child) = @$_; + $fill eq '0' || $fill eq '1' or internal_error("odd {children} parameter must be 0 or 1 (got $fill)"); + ref $child or $child = Gtk2::WrappedLabel->new($child); + $w->pack_start($child, $fill, $fill, 0); + $child->show; + } +} + +sub _find_imgfile { + my ($name) = @_; + + if ($name =~ m|/| && -f $name) { + $name; + } else { + foreach my $path (_icon_paths()) { + foreach ('', '.png', '.xpm') { + my $file = "$path/$name$_"; + -f $file and return $file; + } + } + } +} + +# _text_insert() can be used with any of choose one of theses styles: +# - no tags: +# _text_insert($textview, "My text.."); +# - anonymous tags: +# _text_insert($textview, [ [ 'first text', { 'foreground' => 'blue', 'background' => 'green', ... } ], +# [ 'second text' ], +# [ 'third', { 'font' => 'Serif 15', ... } ], +# ... ]); +# - named tags: +# $textview->{tags} = { +# 'blue_green' => { 'foreground' => 'blue', 'background' => 'green', ... }, +# 'big_font' => { 'font' => 'Serif 35', ... }, +# } +# _text_insert($textview, [ [ 'first text', 'blue_green' ], +# [ 'second', 'big_font' ], +# ... ]); +# - mixed anonymous and named tags: +# $textview->{tags} = { +# 'blue_green' => { 'foreground' => 'blue', 'background' => 'green', ... }, +# 'big_font' => { 'font' => 'Serif 35', ... }, +# } +# _text_insert($textview, [ [ 'first text', 'blue_green' ], +# [ 'second text' ], +# [ 'third', 'big_font' ], +# [ 'fourth', { 'font' => 'Serif 15', ... } ], +# ... ]); +sub _text_insert { + my ($textview, $t, %opts) = @_; + my $buffer = $textview->get_buffer; + $buffer->{tags} ||= {}; + $buffer->{gtk_tags} ||= {}; + my $gtk_tags = $buffer->{gtk_tags}; + my $tags = $buffer->{tags}; + if (ref($t) eq 'ARRAY') { + $opts{append} or $buffer->set_text(''); + foreach my $token (@$t) { + my ($item, $tag) = @$token; + my $iter1 = $buffer->get_end_iter; + if ($item =~ /^Gtk2::Gdk::Pixbuf/) { + $buffer->insert_pixbuf($iter1, $item); + next; + } + if ($tag) { + if (ref($tag)) { + # use anonymous tags + $buffer->insert_with_tags($iter1, $item, $buffer->create_tag(undef, %$tag)); + } else { + # fast text insertion: + # since in some contexts (eg: localedrake, rpmdrake), we use quite a lot of identical tags, + # it's much more efficient and less memory pressure to use named tags + $gtk_tags->{$tag} ||= $buffer->create_tag($tag, %{$tags->{$token->[1]}}); + $buffer->insert_with_tags($iter1, $item, $gtk_tags->{$tag}); + } + } else { + $buffer->insert($iter1, $item); + } + } + } else { + $buffer->set_text($t); + } + #- the following line is needed to move the cursor to the beginning, so that if the + #- textview has a scrollbar, it won't scroll to the bottom when focusing (#3633) + $buffer->place_cursor($buffer->get_start_iter); + $textview->set_wrap_mode($opts{wrap_mode} || 'word'); + $textview->set_editable($opts{editable} || 0); + $textview->set_cursor_visible($opts{visible} || 0); + $textview; +} + + +my @icon_paths; +sub add_icon_path { push @icon_paths, @_ } +sub _icon_paths() { + (@icon_paths, (exists $ENV{SHARE_PATH} ? ($ENV{SHARE_PATH}, "$ENV{SHARE_PATH}/icons", "$ENV{SHARE_PATH}/libDrakX/pixmaps") : ()), + "/usr/lib/libDrakX/icons", "pixmaps", 'standalone/icons', '/usr/share/rpmdrake/icons'); +} +add_icon_path(@icon_paths, + exists $ENV{SHARE_PATH} ? "$ENV{SHARE_PATH}/libDrakX/pixmaps" : (), + '/usr/lib/libDrakX/icons', 'standalone/icons'); + +sub sync { + my ($window) = @_; + $window->show; + flush(); +} + +sub flush() { + Gtk2->main_iteration while Gtk2->events_pending; +} + +1; + diff --git a/perl-install/ugtk2.pm b/perl-install/ugtk2.pm index f16003445..63c484b33 100644 --- a/perl-install/ugtk2.pm +++ b/perl-install/ugtk2.pm @@ -34,20 +34,11 @@ $EXPORT_TAGS{all} = [ map { @$_ } values %EXPORT_TAGS ]; use c; use log; use common; +use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version use Gtk2; use Gtk2::Gdk::Keysyms; -unless ($::no_ugtk_init) { - !check_for_xserver() and print("Cannot be run in console mode.\n"), c::_exit(0); - $::one_message_has_been_translated and warn("N() was called from $::one_message_has_been_translated BEFORE gtk2 initialisation, replace it with a N_() AND a translate() later.\n"), c::_exit(1); - - Gtk2->init; - c::bind_textdomain_codeset($_, 'UTF8') foreach 'libDrakX', @::textdomains; - $::need_utf8_i18n = 1; -} -Gtk2->croak_execeptions if (!$::no_ugtk_init || $::isInstall) && 0.95 < $Gtk2::VERSION; - $border = 5; @@ -61,7 +52,7 @@ $border = 5; # you're building. sub gtkdestroy { $_[0] and $_[0]->destroy } -sub gtkflush() { Gtk2->main_iteration while Gtk2->events_pending } +sub gtkflush() { mygtk2::flush() } sub gtkhide { $_[0]->hide; $_[0] } sub gtkmove { $_[0]->window->move($_[1], $_[2]); $_[0] } sub gtkpack { gtkpowerpack(1, 1, @_) } @@ -288,7 +279,7 @@ sub create_scrolled_window { $W->set_left_margin(6) if ref($W) =~ /Gtk2::TextView/; $W->show; if (ref($W) =~ /Gtk2::TextView|Gtk2::TreeView/) { - gtkadd(gtkset_shadow_type(Gtk2::Frame->new, 'in'), $w); + gtknew('Frame', shadow_type => 'in', child => $w); } else { $w; } @@ -305,7 +296,7 @@ sub create_box_with_title { my ($o, @l) = @_; my $nbline = sum(map { round(length($_) / 60 + 1/2) } map { split "\n" } @l); - my $box = Gtk2::VBox->new(0,0); + my $box = gtknew('VBox'); if ($nbline == 0) { $o->{box_size} = 0; return $box; @@ -313,26 +304,26 @@ sub create_box_with_title { $o->{box_size} = n_line_size($nbline, 'text', $box); if (@l <= 2 && $nbline > 4) { $o->{icon} && !$::isWizard and - eval { gtkpack__($box, gtkset_border_width(gtkpack_(Gtk2::HBox->new(0,0), 1, gtkcreate_img($o->{icon})),5)) }; + eval { gtkpack__($box, gtknew('HBox', border_width => 5, children_loose => [ gtkcreate_img($o->{icon}) ])) }; my $wanted = $o->{box_size}; $o->{box_size} = min(200, $o->{box_size}); my $has_scroll = $o->{box_size} < $wanted; - my $wtext = Gtk2::TextView->new; + chomp(my $text = join("\n", @l)); + my $wtext = gtknew('TextView', text => $text); $wtext->set_left_margin(3); $wtext->can_focus($has_scroll); $wtext->signal_connect(button_press_event => sub { 1 }); #- disable selecting text and popping the contextual menu (GUI team says it's *horrible* to be able to do select text!) - chomp(my $text = join("\n", @l)); - my $scroll = create_scrolled_window(gtktext_insert($wtext, $text)); - my $width = 400; - $scroll->signal_connect(realize => sub { + my $scroll = create_scrolled_window($wtext); + my $width = 400; + $scroll->signal_connect(realize => sub { my $layout = $wtext->create_pango_layout($text); $layout->set_width(($width - 10) * Gtk2::Pango->scale); $wtext->set_size_request($width, min(200, ($layout->get_pixel_size)[1] + 10)); $scroll->set_size_request($width, min(200, ($layout->get_pixel_size)[1] + 10)); $o->{rwindow}->queue_resize; }); - $scroll->set_size_request($width, 200); + $scroll->set_size_request($width, 200); gtkpack_($box, $o->{box_allow_grow} || 0, $scroll); } else { my $a = !$::no_separator; @@ -344,32 +335,32 @@ sub create_box_with_title { }; if ($o->{icon} && (!$::isWizard || $::isInstall)) { gtkpack__($box, - gtkpack_(Gtk2::HBox->new(0,0), - 0, gtkset_size_request(Gtk2::VBox->new(0,0), 15, 0), + gtknew('HBox', children => [ + 0, gtknew('VBox', width => 15), 0, eval { gtkcreate_img($o->{icon}) }, - 0, gtkset_size_request(Gtk2::VBox->new(0,0), 15, 0), - 1, gtkpack_($o->{box_title} = Gtk2::VBox->new(0,0), - 1, Gtk2::HBox->new(0,0), + 0, gtknew('VBox', width => 15), + 1, $o->{box_title} = gtknew('VBox', children_loose => [ + gtknew('HBox', children => [ (map { my $w = $new_label->($_); $::isWizard and $w->set_justify("left"); (0, $w); } @l), - 1, Gtk2::HBox->new(0,0), - ) - ), - if_($a, Gtk2::HSeparator->new) + 1, gtknew('HBox'), + ]) ]) + ]), + if_($a, gtknew('HSeparator')) ); } else { gtkpack__($box, - if_($::isWizard, gtkset_size_request(Gtk2::Label->new, 0, 10)), + if_($::isWizard, gtknew('Label', height => 10)), (map { my $w = $new_label->($_); - $::isWizard ? gtkpack__(Gtk2::HBox->new(0,0), gtkset_size_request(Gtk2::Label->new, 20, 0), $w) + $::isWizard ? gtknew('HBox', children_tight => [ gtknew('Label', width => 20), $w ]) : $w; } @l), - if_($::isWizard, gtkset_size_request(Gtk2::Label->new, 0, 15)), - if_($a, Gtk2::HSeparator->new) + if_($::isWizard, gtknew('Label', height => 15)), + if_($a, gtknew('HSeparator')), ); } } @@ -377,13 +368,17 @@ sub create_box_with_title { sub _create_dialog { my ($title, $o_options) = @_; - my $dialog = Gtk2::Dialog->new; - $dialog->set_title($title); - $dialog->set_position('center-on-parent'); # center-on-parent doesn't work + my $options = $o_options || {}; + + #- keep compatibility with "transient" now called "transient_for" + $options->{transient_for} = delete $options->{transient} if $options->{transient}; + + my $dialog = gtknew('Dialog', title => $title, + position_policy => 'center-on-parent', # center-on-parent doesn't work + modal => 1, + %$options, + ); may_set_icon($dialog, $wm_icon || $::Wizard_pix_up || "wiz_default_up.png"); - $dialog->set_size_request($o_options->{width} || -1, $o_options->{height} || -1); - $dialog->set_modal(1); - $dialog->set_transient_for($o_options->{transient}) if $o_options->{transient}; $dialog; } @@ -396,25 +391,25 @@ sub create_dialog { $dialog->set_border_width(10); my $text = ref($label) ? $label : $o_options->{use_markup} ? gtkset_markup(Gtk2::WrappedLabel->new, $label) : Gtk2::WrappedLabel->new($label); gtkpack($dialog->vbox, - gtkpack_(Gtk2::HBox->new, + gtknew('HBox', children => [ if_($o_options->{stock}, 0, Gtk2::Image->new_from_stock($o_options->{stock}, 'dialog'), - 0, Gtk2::Label->new(" "), + 0, gtknew('Label', text => " "), ), 1, $o_options->{scroll} ? create_scrolled_window($text, [ 'never', 'automatic' ]) : $text, - ), + ]), ); if ($o_options->{cancel}) { - my $button2 = Gtk2::Button->new(N("Cancel")); - $button2->signal_connect(clicked => sub { $ret = 0; $dialog->destroy; Gtk2->main_quit }); - $button2->can_default(1); - $dialog->action_area->pack_start($button2, 1, 1, 0); + $dialog->action_area->pack_start( + gtknew('Button', text => N("Cancel"), + clicked => sub { $ret = 0; $dialog->destroy; Gtk2->main_quit }, + can_default => 1), + 1, 1, 0); } - my $button = Gtk2::Button->new(N("Ok")); - $button->can_default(1); - $button->signal_connect(clicked => sub { $ret = 1; $dialog->destroy; Gtk2->main_quit }); + my $button = gtknew('Button', text => N("Ok"), can_default => 1, + clicked => sub { $ret = 1; $dialog->destroy; Gtk2->main_quit }); $dialog->action_area->pack_start($button, 1, 1, 0); $button->grab_default; @@ -511,21 +506,21 @@ sub create_okcancel { $cancel = $::isWizard ? N("Previous") : N("Cancel"); } my $ok = defined $o_ok ? $o_ok : $::isWizard ? ($::Wizard_finished ? N("Finish") : N("Next")) : N("Ok"); - my $bok = $ok && gtksignal_connect($w->{ok} = Gtk2::Button->new($ok), clicked => $w->{ok_clicked} || sub { $w->{retval} = 1; Gtk2->main_quit }); + my $bok = $ok && ($w->{ok} = gtknew('Button', text => $ok, clicked => $w->{ok_clicked} || sub { $w->{retval} = 1; Gtk2->main_quit })); my $bprev; if ($cancel) { - $bprev = gtksignal_connect($w->{cancel} = Gtk2::Button->new($cancel), clicked => $w->{cancel_clicked} || + $bprev = $w->{cancel} = gtknew('Button', text => $cancel, clicked => $w->{cancel_clicked} || sub { log::l("default cancel_clicked"); undef $w->{retval}; Gtk2->main_quit }); } - $w->{wizcancel} = gtksignal_connect(Gtk2::Button->new(N("Cancel")), clicked => sub { die 'wizcancel' }) if $::isWizard && !$::isInstall; - my $f = sub { $w->{buttons}{$_[0][0]} = gtksignal_connect(Gtk2::Button->new($_[0][0]), clicked => $_[0][1]) }; + $w->{wizcancel} = gtknew('Button', text => N("Cancel"), clicked => sub { die 'wizcancel' }) if $::isWizard && !$::isInstall; + my $f = sub { $w->{buttons}{$_[0][0]} = gtknew('Button', text => $_[0][0], clicked => $_[0][1]) }; my @left = ((map { $f->($_) } grep { $_->[2] && !$_->[3] } @other), map { $f->($_) } grep { !$_->[2] && !$_->[3] } @other); my @right = ((map { $f->($_) } grep { $_->[2] && $_->[3] } @other), map { $f->($_) } grep { !$_->[2] && $_->[3] } @other); # we put space to group buttons in two packs (but if there's only one when not in wizard mode) # but in the installer where all windows run in wizard mode because of design even when not in a wizard step - $bprev = Gtk2::Label->new if !$cancel && $::Wizard_no_previous && !@left && !@right; + $bprev = gtknew('Label') if !$cancel && $::Wizard_no_previous && !@left && !@right; if ($::isWizard) { # wizard mode: order is cancel/left_extras/white/right_extras/prev/next unshift @left, $w->{wizcancel} if !$::isInstall; @@ -533,27 +528,26 @@ sub create_okcancel { } else { # normal mode: cancel/ok button follow GNOME's HIG unshift @left, $bprev; - push @left, Gtk2::Label->new if $ok && $cancel; # space buttons but if there's only one button + push @left, gtknew('Label') if $ok && $cancel; # space buttons but if there's only one button push @right, $bok; } - gtkpack(Gtk2::VBox->new, - Gtk2::HSeparator->new, - gtkpack(Gtk2::HBox->new, - (map { - gtkpack( - create_hbox($_->[1]), - (map { - $_->can_default($::isWizard); - $_; - } grep { $_ } @{$_->[0]}) - ); + gtknew('VBox', children_loose => [ + gtknew('HSeparator'), + gtknew('HBox', children_loose => [ + map { + gtknew('HButtonBox', layout => $_->[1], + children_loose => [ + map { + $_->can_default($::isWizard); + $_; + } grep { $_ } @{$_->[0]} + ]); } ([ \@left, 'start' ], [ \@right, 'end' ], ) - ) - ), - ); + ]), + ]); } sub _setup_paned { @@ -620,74 +614,9 @@ sub may_set_icon { } } -# gtktext_insert() can be used with any of choose one of theses styles: -# - no tags: -# gtktext_insert($textview, "My text.."); -# - anonymous tags: -# gtktext_insert($textview, [ [ 'first text', { 'foreground' => 'blue', 'background' => 'green', ... } ], -# [ 'second text' ], -# [ 'third', { 'font' => 'Serif 15', ... } ], -# ... ]); -# - named tags: -# $textview->{tags} = { -# 'blue_green' => { 'foreground' => 'blue', 'background' => 'green', ... }, -# 'big_font' => { 'font' => 'Serif 35', ... }, -# } -# gtktext_insert($textview, [ [ 'first text', 'blue_green' ], -# [ 'second', 'big_font' ], -# ... ]); -# - mixed anonymous and named tags: -# $textview->{tags} = { -# 'blue_green' => { 'foreground' => 'blue', 'background' => 'green', ... }, -# 'big_font' => { 'font' => 'Serif 35', ... }, -# } -# gtktext_insert($textview, [ [ 'first text', 'blue_green' ], -# [ 'second text' ], -# [ 'third', 'big_font' ], -# [ 'fourth', { 'font' => 'Serif 15', ... } ], -# ... ]); -sub gtktext_insert { - my ($textview, $t, %opts) = @_; - my $buffer = $textview->get_buffer; - $buffer->{tags} ||= {}; - $buffer->{gtk_tags} ||= {}; - my $gtk_tags = $buffer->{gtk_tags}; - my $tags = $buffer->{tags}; - if (ref($t) eq 'ARRAY') { - $opts{append} or $buffer->set_text(''); - foreach my $token (@$t) { - my ($item, $tag) = @$token; - my $iter1 = $buffer->get_end_iter; - if ($item =~ /^Gtk2::Gdk::Pixbuf/) { - $buffer->insert_pixbuf($iter1, $item); - next; - } - if ($tag) { - if (ref($tag)) { - # use anonymous tags - $buffer->insert_with_tags($iter1, $item, $buffer->create_tag(undef, %$tag)); - } else { - # fast text insertion: - # since in some contexts (eg: localedrake, rpmdrake), we use quite a lot of identical tags, - # it's much more efficient and less memory pressure to use named tags - $gtk_tags->{$tag} ||= $buffer->create_tag($tag, %{$tags->{$token->[1]}}); - $buffer->insert_with_tags($iter1, $item, $gtk_tags->{$tag}); - } - } else { - $buffer->insert($iter1, $item); - } - } - } else { - $buffer->set_text($t); - } - #- the following line is needed to move the cursor to the beginning, so that if the - #- textview has a scrollbar, it won't scroll to the bottom when focusing (#3633) - $buffer->place_cursor($buffer->get_start_iter); - $textview->set_wrap_mode($opts{wrap_mode} || 'word'); - $textview->set_editable($opts{editable} || 0); - $textview->set_cursor_visible($opts{visible} || 0); - $textview; -} +sub gtktext_insert { &mygtk2::_text_insert } +sub icon_paths { &mygtk2::_icon_paths } +sub add_icon_path { &mygtk2::add_icon_path } # extracts interesting font metrics for a given widget sub gtkfontinfo { @@ -760,7 +689,7 @@ sub fill_tiled { sub add2notebook { my ($n, $title, $book) = @_; - $n->append_page($book, gtkshow(Gtk2::Label->new($title))); + $n->append_page($book, gtkshow(gtknew('Label', text => $title))); $book->show; } @@ -903,16 +832,6 @@ sub gtkset_background { $root->draw_rectangle($gc, 1, 0, 0, $w, $h); } -sub add_icon_path { push @icon_paths, @_ } -sub icon_paths() { - (@icon_paths, (exists $ENV{SHARE_PATH} ? ($ENV{SHARE_PATH}, "$ENV{SHARE_PATH}/icons", "$ENV{SHARE_PATH}/libDrakX/pixmaps") : ()), - "/usr/lib/libDrakX/icons", "pixmaps", 'standalone/icons', '/usr/share/rpmdrake/icons'); -} -add_icon_path(@icon_paths, - exists $ENV{SHARE_PATH} ? "$ENV{SHARE_PATH}/libDrakX/pixmaps" : (), - '/usr/lib/libDrakX/icons', 'standalone/icons'); - - # -=-=---=-=---=-=---=-=---=-=---=-=---=-=---=-=---=-=---=-=---=-=---=-=--- # toplevel window creation helper @@ -946,8 +865,8 @@ sub new { if ($::isInstall) { gtkadd($o->{rwindow}, - gtkadd(gtkset_shadow_type(Gtk2::Frame->new(undef), 'out'), - $o->{window} = gtkset_border_width(gtkset_shadow_type(Gtk2::Frame->new(undef), 'none'), 3) + gtknew('Frame', shadow_type => 'out', child => + $o->{window} = gtknew('Frame', shadow_type => 'none', border_width => 3) )); } else { $o->{window} = $o->{rwindow}; @@ -957,11 +876,10 @@ sub new { $o->{rwindow}->set_transient_for($o->{transient}) if $o->{transient} && $o->{transient} =~ /Gtk2::Window/; } else { - $o->{rwindow} = $o->{window} = Gtk2::VBox->new(0,0); - $o->{window}->set_border_width($::Wizard_splash ? 0 : 10); + $o->{rwindow} = $o->{window} = gtknew('VBox', border_width => $::Wizard_splash ? 0 : 10); set_main_window_size($o); - $::WizardTable ||= Gtk2::VBox->new(0, 0); + $::WizardTable ||= gtknew('VBox'); if (!$::Plug && $o->{isEmbedded}) { $::Plug = $::WizardWindow = gtkshow(Gtk2::Plug->new($::XID)); @@ -970,7 +888,7 @@ sub new { gtkadd($::Plug, $::WizardTable); } elsif (!$::WizardWindow) { $::WizardWindow = _create_window($title); - gtkadd($::WizardWindow, gtkadd(gtkset_shadow_type(Gtk2::Frame->new, 'out'), $::WizardTable)); + gtkadd($::WizardWindow, gtknew('Frame', shadow_type => 'out', child => $::WizardTable)); if ($::isInstall) { $::WizardWindow->signal_connect(key_press_event => sub { @@ -1140,7 +1058,7 @@ sub _ask_warn($@) { my ($o, @msgs) = @_; gtkadd($o->{window}, gtkpack($o->create_box_with_title(@msgs), - gtksignal_connect(my $w = Gtk2::Button->new(N("Ok")), "clicked" => sub { Gtk2->main_quit }), + my $w = gtknew('Button', text => N("Ok"), clicked => sub { Gtk2->main_quit }), ), ); $w->grab_focus; @@ -1205,16 +1123,16 @@ sub ask_browse_tree_info { $textcolumn->set_max_width(200); gtkadd($w->{window}, - gtkpack_(Gtk2::VBox->new(0,5), + gtknew('VBox', spacing => 5, children => [ 0, $common->{message}, - 1, gtkpack(Gtk2::HBox->new(0,0), + 1, gtknew('HBox', children_loose => [ create_scrolled_window($tree), - gtkadd(Gtk2::Frame->new(N("Info")), - create_scrolled_window(my $info = Gtk2::TextView->new), - )), - 0, my $box1 = Gtk2::HBox->new(0,15), - 0, my $box2 = Gtk2::HBox->new(0,10), - )); + gtknew('Frame', text => N("Info"), child => + create_scrolled_window(my $info = gtknew('TextView')), + ) ]), + 0, my $box1 = gtknew('HBox', spacing => 15), + 0, my $box2 = gtknew('HBox', spacing => 10), + ])); #gtkpack__($box2, my $toolbar = Gtk2::Toolbar->new('horizontal', 'icons')); gtkpack__($box2, my $toolbar = Gtk2::Toolbar->new); @@ -1222,23 +1140,22 @@ sub ask_browse_tree_info { @l = reverse @l if !$::isInstall; my @buttons = map { my ($t, $val) = @$_; - $box2->pack_end(my $w = gtksignal_connect(Gtk2::Button->new($t), clicked => sub { - $w->{retval} = $val; - Gtk2->main_quit; - }), 0, 1, 20); + $box2->pack_end(my $w = gtknew('Button', text => $t, clicked => sub { + $w->{retval} = $val; + Gtk2->main_quit; + }), 0, 1, 20); $w; } @l; @buttons = reverse @buttons if !$::isInstall; - gtkpack__($box2, gtksignal_connect(Gtk2::Button->new(N("Help")), clicked => sub { + gtkpack__($box2, gtknew('Button', text => N("Help"), clicked => sub { ask_warn(N("Help"), $common->{interactive_help}->()); })) if $common->{interactive_help}; if ($common->{auto_deps}) { - gtkpack__($box1, gtksignal_connect(gtkset_active(Gtk2::CheckButton->new($common->{auto_deps}), $common->{state}{auto_deps}), - clicked => sub { invbool \$common->{state}{auto_deps} })); + gtkpack__($box1, gtknew('CheckButton', text => $common->{auto_deps}, val => \$common->{state}{auto_deps})); } - $box1->pack_end(my $status = Gtk2::Label->new, 0, 1, 20); + $box1->pack_end(my $status = gtknew('Label'), 0, 1, 20); $w->{window}->set_size_request(map { $_ - 2 * $border - 4 } $::windowwidth, $::windowheight) if !$::isInstall; $buttons[0]->grab_focus; @@ -1523,7 +1440,7 @@ sub gtk_TextView_get_log { sub gtk_new_TextView_get_log { my ($command, $filter_output, $when_command_is_over) = @_; - my $log_w = gtkset_editable(Gtk2::TextView->new, 0); + my $log_w = gtknew('TextView', editable => 0); my $log_scroll = create_scrolled_window($log_w); #- $log_scroll is a frame, not a ScrolledWindow, so giving $log_scroll->child my $pid = gtk_TextView_get_log($log_w, $log_scroll->child, $command, $filter_output, $when_command_is_over) or return; $log_scroll, $pid; @@ -1697,16 +1614,14 @@ sub set { package Gtk2::WrappedLabel; sub new { my ($_type, $o_text, $o_align) = @_; - ugtk2::gtkset_alignment(ugtk2::gtkset_line_wrap(Gtk2::Label->new($o_text || ''), 1), $o_align || 0, 0.5); + mygtk2::gtknew('WrappedLabel', text => $o_text || '', alignment => [ $o_align || 0, 0.5 ]); } package Gtk2::Entry; sub new_with_text { - my ($_class, @text) = @_; - my $entry = Gtk2::Entry->new; - @text and $entry->set_text(@text); - return $entry; + my ($_class, $o_text) = @_; + mygtk2::gtknew('Entry', text => $o_text); } |