summaryrefslogtreecommitdiffstats
path: root/perl-install/keyboard.pm
blob: 18cfca8379363960662400897b98a04afac3ea6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

package keyboard;

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use common qw(:common :system :file);
use run_program;
use commands;
use log;
use c;


#-######################################################################################
#- Globals
#-######################################################################################
my $KMAP_MAGIC = 0x8B39C07F;

#- a best guess of the keyboard layout, based on the choosen locale
my %lang2keyboard =
(
  'af' => 'us_intl',
  'be' => 'by',
  'bg' => 'bg',
  'br' => 'fr',
  'ca' => 'es',
  'cs' => 'cz',
  'cy' => 'uk',
  'da' => 'dk',
  'de' => 'de',
'de_AT'=> 'de',
'de_CH'=> 'ch_de',
'de_DE'=> 'de', 
  'el' => 'gr',
  'en' => 'us',
'en_US'=> 'us',
'en_GB'=> 'uk',
  'eo' => 'us_intl',
  'es' => 'es',
  'es@tradicional' => 'es',
'es_AR'=> 'la',
'es_ES'=> 'es',
'es_MX'=> 'la',
  'et' => 'ee',
  'eu' => 'es',
  'fi' => 'fi',
  'fr' => 'fr',
'fr_BE'=> 'be',
'fr_CA'=> 'qc',
'fr_CH'=> 'ch_fr',
'fr_FR'=> 'fr',
  'ga' => 'uk',
  'gl' => 'es',
  'he' => 'il',
  'hr' => 'hr',
  'hu' => 'hu',
  'hy' => 'am',
  'is' => 'is',
  'it' => 'it',
  'ka' => 'ge_la',
  'lt' => 'lt',
  'nl' => 'nl',
'nl_BE'=> 'be',
'nl_NL'=> 'nl',
  'no' => 'no',
  'no@nynorsk' => 'no',
  'oc' => 'fr',
  'pl' => 'pl',
  'pt' => 'pt',
'pt_BR'=> 'br',
'pt_PT'=> 'pt',
  'ru' => 'ru',
  'sk' => 'sk',
  'sl' => 'si',
  'sr' => 'yu',
  'sv' => 'se',
  'th' => 'th',
  'tr' => 'tr_q',
  'uk' => 'ua',
  'wa' => 'be',
);

#- key = extension for Xmodmap file, [0] = description of the keyboard,
#- [1] = name for loadkeys, [2] = name for XKB
my %keyboards = (
arch() =~ /^sparc/ ? (
 "cs" => [ __("Czech"),          "sunt5-cz-us", "czsk(cz_us_qwertz)" ],
 "de" => [ __("German"),         "sunt5-de-latin1", "de" ],
 "dvorak" => [ __("Dvorak"),     "sundvorak",   "dvorak" ],
 "es" => [ __("Spanish"),        "sunt5-es",    "es" ],
 "fi" => [ __("Finnish"),        "sunt5-fi-latin1", "fi" ],
 "fr" => [ __("French"),         "sunt5-fr-latin1", "fr" ],
 "no" => [ __("Norwegian"),      "sunt4-no-latin1", "no" ],
 "pl" => [ __("Polish"),         "sun-pl-altgraph", "pl" ],
 "ru" => [ __("Russian"),        "sunt5-ru",    "ru" ],
 "uk" => [ __("UK keyboard"),    "sunt5-uk",    "gb" ],
 "us" => [ __("US keyboard"),    "sunkeymap",   "us" ],
) : (
arch() =~ /^ppc/ ? (
 "us" => [ __("US keyboard"),    "mac-us-ext",  "us" ],
 "de_nodeadkeys" => [ __("German"), "mac-de-latin1-nodeadkeys", "de(nodeadkeys)" ],
 "fr" => [ __("French"),         "mac-fr3",   "fr" ],
) : (
 "am_old" => [ __("Armenian (old)"),	"am_old",	"am(old)" ],
 "am" => [ __("Armenian (typewriter)"),	"am-armscii8",	"am" ],
 "am_phonetic" => [ __("Armenian (phonetic)"), "am_phonetic", "am(phonetic)" ],
#- only xmodmap is currently available
#-"ar" => [ __("Arabic"),        "ar-8859_6",   "ar" ],
 "be" => [ __("Belgian"),        "be-latin1",   "be" ],
 "bg" => [ __("Bulgarian"),      "bg",          "bg" ],
 "br" => [ __("Brazilian (ABNT-2)"),      "br-abnt2",    "br" ],
 "by" => [ __("Belarusian"),      "by-cp1251",  "byru" ],
 "ch_de" => [ __("Swiss (German layout)"), "sg-latin1", "de_CH" ],
 "ch_fr" => [ __("Swiss (French layout)"), "fr_CH-latin1", "fr_CH" ],
 "cz" => [ __("Czech"),          "cz-latin2",   "czsk(cz_us_qwertz)" ],
 "de" => [ __("German"),         "de-latin1",   "de" ],
 "de_nodeadkeys" => [ __("German (no dead keys)"), "de-latin1-nodeadkeys", "de(nodeadkeys)" ],
 "dk" => [ __("Danish"),         "dk-latin1",   "dk" ],
 "dvorak" => [ __("Dvorak"),     "pc-dvorak-latin1", "dvorak" ],
 "ee" => [ __("Estonian"),       "ee-latin9",   "ee" ],
 "es" => [ __("Spanish"),        "es-latin1",   "es" ],
 "fi" => [ __("Finnish"),        "fi-latin1",   "fi" ],
 "fr" => [ __("French"),         "fr-latin1",   "fr" ],
 "ge_ru"=>[__("Georgian (\"Russian\" layout)"),"ge_ru-georgian_academy","ge_ru"],
 "ge_la"=>[__("Georgian (\"Latin\" layout)"),"ge_la-georgian_academy","ge_la"],
 "gr" => [ __("Greek"),          "gr-8859_7",   "gr" ],
 "hu" => [ __("Hungarian"),      "hu-latin2",   "hu" ],
 "hr" => [ __("Croatian"),	 "croat",	"yu" ],
 "il" => [ __("Israeli"),        "il-8859_8",   "il" ],
 "il_phonetic" => [ __("Israeli (Phonetic)"),"hebrew",   "il_phonetic" ],
#- only xmodmap is currently available
#"ir" => [ __("Iranian"),      "ir-isiri3342",   "ir" ],
 "is" => [ __("Icelandic"),      "is-latin1",   "is" ],
 "it" => [ __("Italian"),        "it-latin1",   "it" ],
 "la" => [ __("Latin American"), "la-latin1",   "la" ],
 "nl" => [ __("Dutch"),          "nl-latin1",   "nl" ],
 "lt" => [ __("Lithuanian AZERTY (old)"), "lt-latin7","lt" ],
#- TODO: write a console kbd map for lt_new
 "lt_new" => [ __("Lithuanian AZERTY (new)"), "lt-latin7","lt_new" ],
 "lt_b" => [ __("Lithuanian \"number row\" QWERTY"), "ltb-latin7", "lt_b" ],
 "lt_p" => [ __("Lithuanian \"phonetic\" QWERTY"), "ltp-latin7", "lt_p" ],
 "no" => [ __("Norwegian"),      "no-latin1",   "no" ],
 "pl" => [ __("Polish (qwerty layout)"),        "pl", "pl" ],
 "pl2" => [ __("Polish (qwertz layout)"),       "pl-latin2", "pl" ],
 "pt" => [ __("Portuguese"),     "pt-latin1",   "pt" ],
 "qc" => [ __("Canadian (Quebec)"), "qc-latin1","ca_enhanced" ],
 "ru" => [ __("Russian"),        "ru4",         "ru(winkeys)" ],
 "ru_yawerty" => [ __("Russian (Yawerty)"),"ru-yawerty","ru_yawerty" ],
 "se" => [ __("Swedish"),        "se-latin1",   "se" ],
 "si" => [ __("Slovenian"),      "slovene",     "si" ],
 "sk" => [ __("Slovakian"),      "sk-qwertz",   "czsk(sk_us_qwertz)" ],
 "th" => [ __("Thai keyboard"),  "th",          "th" ],
 "tr_f" => [ __("Turkish (traditional \"F\" model)"), "trf", "tr_f" ],
 "tr_q" => [ __("Turkish (modern \"Q\" model)"), "tr_q-latin5", "tr_q" ],
 "ua" => [ __("Ukrainian"),      "ua",           "ua" ],
 "uk" => [ __("UK keyboard"),    "uk",           "gb" ],
 "us" => [ __("US keyboard"),    "us",           "us" ],
 "us_intl" => [ __("US keyboard (international)"), "us-latin1", "us_intl" ],
 "yu" => [ __("Yugoslavian (latin layout)"), "sr", "yu" ],
)),
);


#-######################################################################################
#- Functions
#-######################################################################################
sub list { map { $_->[0] } values %keyboards }
sub xmodmaps { keys %keyboards }
sub keyboard2text { $keyboards{$_[0]} && $keyboards{$_[0]}[0] }
sub keyboard2kmap { $keyboards{$_[0]} && $keyboards{$_[0]}[1] }
sub keyboard2xkb  { $keyboards{$_[0]} && $keyboards{$_[0]}[2] }
sub text2keyboard {
    my ($t) = @_;
    foreach (keys %keyboards) {
	lc($keyboards{$_}[0]) eq lc($t) and return $_;
    }
    die "unknown keyboard $t";
}

sub loadkeys_files {
    my $archkbd = arch() =~ /^sparc/ ? "sun" : arch() =~ /^i\d/ ? "i386" : arch();
    my $p = "/usr/lib/kbd/keymaps/$archkbd";
    my $post = ".kmap.gz";
    my %trans = ("cz-latin2" => "cz-lat2");
    my (@l, %l);
    foreach (values %keyboards) {
	local $_ = $trans{$_->[1]} || $_->[1];
	my ($l) = grep { -e $_ } ("$p/$_$post");
	$l or /(..)/ and ($l) = grep { -e $_ } ("$p/$1$post");
	print STDERR "unknown $_\n" if $_[0] && !$l; $l or next;
	push @l, $l;
	foreach (`zgrep include $l | grep "^include"`) {
	    /include\s+"(.*)"/ or die "bad line $_";
	    @l{grep { -e $_ } ("$p/$1.inc.gz")} = ();
	}
    }        
    @l, keys %l, grep { -e $_ } map { "$p/$_.inc.gz" } qw(compose euro windowkeys linux-keys-bare);
}

sub lang2keyboard($) {
    local ($_) = @_;
    my $kb = $lang2keyboard{$_} || $keyboards{$_} && $_ || "us";
    $keyboards{$kb} ? $kb : "us"; #- handle incorrect keyboad mapping to us.
}

sub load($) {
    my ($keymap) = @_;
    return if $::testing;

    my ($magic, @keymaps) = unpack "I i" . c::MAX_NR_KEYMAPS() . "a*", $keymap;
    $keymap = pop @keymaps;

    $magic != $KMAP_MAGIC and die "failed to read kmap magic";

    local *F;
    sysopen F, "/dev/console", 2 or die "failed to open /dev/console: $!";

    my $count = 0;
    foreach (0 .. c::MAX_NR_KEYMAPS() - 1) {
	$keymaps[$_] or next;

	my @keymap = unpack "s" . c::NR_KEYS() . "a*", $keymap;
	$keymap = pop @keymap;

	my $key = -1;
	foreach my $value (@keymap) {
	    $key++;
	    c::KTYP($value) != c::KT_SPEC() or next;
	    ioctl(F, c::KDSKBENT(), pack("CCS", $_, $key, $value)) or die "keymap ioctl failed ($_ $key $value): $!";
	 }
	$count++;
    }
    #- log::l("loaded $count keymap tables");
}

sub xmodmap_file {
    my ($keyboard) = @_;
    my $f = "$ENV{SHARE_PATH}/xmodmap/xmodmap.$keyboard";
    if (! -e $f) {
	run_program::run("extract_archive", "$ENV{SHARE_PATH}/xmodmap.cz2", '/tmp', "xmodmap.$keyboard");
	$f = "/tmp/xmodmap.$keyboard";
    }
    -e $f && $f;
}

sub setup($) {
    return if arch() =~ /^sparc/;
    my ($keyboard) = @_;
    my $o = $keyboards{$keyboard} or return;

    log::l("loading keymap $o->[1]");
    if (-e (my $f = "$ENV{SHARE_PATH}/keymaps/$o->[1].kmap")) {
	load(cat_($f));
    } else {
	local *F;
	open F, "extract_archive $ENV{SHARE_PATH}/keymaps.cz2 '' $o->[1].kmap |";
	local $/ = undef;
	eval { load(<F>) };
    }
    my $f = xmodmap_file($keyboard);
    eval { run_program::run('xmodmap', $f) } unless $::testing || !$f;
}

sub write($$;$) {
    my ($prefix, $keyboard, $isNotDelete) = @_;

    setVarsInSh("$prefix/etc/sysconfig/keyboard", { KEYTABLE => keyboard2kmap($keyboard), $isNotDelete ? () : (BACKSPACE => "Delete") });
    run_program::rooted($prefix, "dumpkeys > /etc/sysconfig/console/default.kmap") or log::l("dumpkeys failed");
}

sub read($) {
    my ($prefix) = @_;

    my %keyf = getVarsFromSh("$prefix/etc/sysconfig/keyboard");
    map { keyboard2kmap($_) eq $keyf{KEYTABLE} || $_ eq $keyf{KEYTABLE} ? $_ : (); } keys %keyboards;
}

#-######################################################################################
#- Wonderful perl :(
#-######################################################################################
1;
rmat}; } if (my $widget = delete $opts->{child}) { $w->add($widget); $widget->show; } $w->set_image(delete $opts->{image}) if exists $opts->{image}; $w->set_relief(delete $opts->{relief}) if exists $opts->{relief}; if (my $text_ref = delete $opts->{text_ref}) { my $set = sub { eval { $w->set_label(may_apply($w->{format}, $$text_ref)) }; }; gtkval_register($w, $text_ref, $set); $set->(); } elsif (exists $opts->{text}) { $w->set_label(delete $opts->{text}); } 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__ProgressBar { my ($w, $opts) = @_; if (!$w) { $w = Gtk2::ProgressBar->new; } if (my $fraction_ref = delete $opts->{fraction_ref}) { my $set = sub { $w->set_fraction($$fraction_ref) }; gtkval_register($w, $fraction_ref, $set); $set->(); } elsif (exists $opts->{fraction}) { $w->set_fraction(delete $opts->{fraction}); } $w; } sub _gtk__VSeparator { &_gtk_any_simple } sub _gtk__HSeparator { &_gtk_any_simple } sub _gtk__Calendar { &_gtk_any_simple } sub _gtk__DrawingArea { my ($w, $opts) = @_; if (!$w) { $w = Gtk2::DrawingArea->new; } $w->signal_connect(expose_event => delete $opts->{expose_event}) if exists $opts->{expose_event}; $w; } 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 not find image $name"); if (my $size = delete $opts->{size}) { $w = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file, $size, $size, 1); } else { $w = Gtk2::Gdk::Pixbuf->new_from_file($file); } } $w; } # Image_using_pixmap is rendered using DITHER_MAX which is much better on 16bpp displays sub _gtk__Image_using_pixmap { &_gtk__Image } sub _gtk__Image { my ($w, $opts, $class) = @_; if (!$w) { $w = Gtk2::Image->new; $w->{format} = delete $opts->{format} if exists $opts->{format}; $w->{set_from_file} = $class =~ /using_pixmap/ ? sub { my ($w, $file) = @_; my $pixmap = mygtk2::pixmap_from_pixbuf($w, gtknew('Pixbuf', file => $file)); $w->set_from_pixmap($pixmap, undef); } : sub { my ($w, $file, $o_size) = @_; if ($o_size) { my $pixbuf = gtknew('Pixbuf', file => $file, size => $o_size); $w->set_from_pixbuf($pixbuf); } else { $w->set_from_file($file); } }; } if (my $name = delete $opts->{file}) { my $file = _find_imgfile(may_apply($w->{format}, $name)) or internal_error("can not find image $name"); $w->{set_from_file}->($w, $file, delete $opts->{size}); } 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 not find image $$file_ref"); $w->{set_from_file}->($w, $file, delete $opts->{size}); }; gtkval_register($w, $file_ref, $set); $set->() if $$file_ref; } $w; } sub _gtk__WrappedLabel { my ($w, $opts) = @_; $opts->{line_wrap} = 1; _gtk__Label($w, $opts); } sub _gtk__Label_Left { my ($w, $opts) = @_; $opts->{alignment} ||= [ 0, 0 ]; $opts->{padding} = [ 20, 0 ]; _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_ellipsize(delete $opts->{ellipsize}) if exists $opts->{ellipsize}; $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}; } if (my $text_ref = delete $opts->{text_ref}) { my $set = sub { $w->set_text($$text_ref) }; gtkval_register($w, $text_ref, $set); $set->(); } if (my $t = delete $opts->{text_markup}) { $w->set_markup($t); if ($w->get_text eq '') { log::l("invalid markup in $t. not using the markup"); $w->set_text($t); } } $w; } sub title1_to_markup { my ($label) = @_; '<b><big>' . $label . '</big></b>'; } sub _gtk__Title1 { my ($w, $opts) = @_; $opts ||= {}; $opts->{text_markup} = '<b><big>' . delete($opts->{label}) . '</big></b>'; _gtk__Label($w, $opts); } sub _gtk__Title2 { my ($w, $opts) = @_; $opts ||= {}; $opts->{alignment} = [ 0, 0 ]; _gtk__Title1($w, $opts); } 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}; if (my $text_ref = delete $opts->{text_ref}) { my $set = sub { $w->set_text($$text_ref) }; gtkval_register($w, $text_ref, $set); $set->(); } $w; } sub _gtk__TextView { my ($w, $opts, $_class, $action) = @_; 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}, append => $action eq 'gtkadd') 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}; } my $set_list = sub { $w->{formatted_list} = $w->{format} ? [ map { $w->{format}($_) } @{$w->{list}} ] : $w->{list}; $w->get_model->clear; $w->{strings} = $w->{formatted_list}; # used by Gtk2::ComboBox wrappers such as get_text() in ugtk2 $w->append_text($_) foreach @{$w->{formatted_list}}; }; if (my $list_ref = delete $opts->{list_ref}) { !$opts->{list} or internal_error("both list and list_ref"); my $set = sub { $w->{list} = $$list_ref; $set_list->(); }; gtkval_register($w, $list_ref, $set); $set->(); } elsif (exists $opts->{list}) { $w->{list} = delete $opts->{list}; $set_list->(); } if ($action eq 'gtknew') { if (my $text_ref = delete $opts->{text_ref}) { my $set = sub { my $val = may_apply($w->{format}, $$text_ref); 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->{h_policy} || 'automatic', delete $opts->{v_policy} || 'automatic'); } my $faked_w = $w; if (my $child = delete $opts->{child}) { if (member(ref($child), qw(Gtk2::Layout Gtk2::Html2::View Gtk2::SimpleList Gtk2::SourceView::View 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 (ref($child) eq 'Gtk2::TextView' && delete $opts->{to_bottom}) { $child->{to_bottom} = _allow_scroll_TextView_to_bottom($w, $child); } if ($action eq 'gtknew' && ref($child) =~ /Gtk2::SimpleList|Gtk2::TextView|Gtk2::TreeView/) { $faked_w = gtknew('Frame', shadow_type => 'in', child => $w); } } $faked_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__Expander { my ($w, $opts) = @_; if ($w) { $w->set_label(delete $opts->{text}) if exists $opts->{text}; } else { $w = Gtk2::Expander->new(delete $opts->{text}); } $w->signal_connect(activate => delete $opts->{activate}) if exists $opts->{activate}; if (my $child = delete $opts->{child}) { $w->add($child); $child->show; } $w; } sub _gtk__Window { &_gtk_any_Window } sub _gtk__Dialog { &_gtk_any_Window } sub _gtk__Plug { &_gtk_any_Window } sub _gtk_any_Window { my ($w, $opts, $class) = @_; if (!$w) { if ($class eq 'Window') { $w = "Gtk2::$class"->new(delete $opts->{type} || 'toplevel'); } elsif ($class eq 'Plug') { $opts->{socket_id} or internal_error("can not create a Plug without a socket_id"); $w = "Gtk2::$class"->new(delete $opts->{socket_id}); } else { $w = "Gtk2::$class"->new; } $w->set_modal(1) if exists $opts->{transient_for}; $w->set_modal(delete $opts->{modal}) if exists $opts->{modal}; $w->set_transient_for(delete $opts->{transient_for}) if exists $opts->{transient_for}; $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_position(delete $opts->{position_policy}) if exists $opts->{position_policy}; $w->set_default_size(delete $opts->{default_width} || -1, delete $opts->{default_height} || -1) if exists $opts->{default_width} || exists $opts->{default_height}; my $icon_no_error = $opts->{icon_no_error}; if (my $name = delete $opts->{icon} || delete $opts->{icon_no_error}) { if (my $f = _find_imgfile($name)) { $w->set_icon(gtknew('Pixbuf', file => $f)); } elsif (!$icon_no_error) { internal_error("can not find $name"); } } } $w->set_title(delete $opts->{title}) if exists $opts->{title}; if (my $child = delete $opts->{child}) { $w->add($child); $child->show; } $w; } sub _gtk__MagicWindow { my ($w, $opts) = @_; my $pop_it = delete $opts->{pop_it} || !$::isWizard && !$::isEmbedded || $::WizardTable && do { #- do not take into account the wizard banner any { !$_->isa('Gtk2::DrawingArea') && $_->visible } $::WizardTable->get_children; }; my $sub_child = delete $opts->{child}; my $provided_banner = delete $opts->{banner}; if ($pop_it && $provided_banner) { $sub_child = gtknew('VBox', children => [ 0, $provided_banner, if_($sub_child, 1, $sub_child) ]); } else { $sub_child ||= gtknew('VBox'); } if ($pop_it) { $opts->{child} = $::isInstall ? gtknew('Frame', shadow_type => 'out', child => gtknew('Frame', shadow_type => 'none', border_width => 3, child => $sub_child)) : $sub_child; $w = _create_Window($opts); } else { if (!$::WizardWindow) { my $banner; if (!$::isEmbedded && !$::isInstall && $::Wizard_title) { if (_find_imgfile($opts->{icon_no_error})) { $banner = Gtk2::Banner->new($opts->{icon_no_error}, $::Wizard_title); } else { log::l("ERROR: missing wizard banner $opts->{icon_no_error}"); } } $::WizardTable = gtknew('VBox', if_($banner, children_tight => [ $banner ])); if ($::isEmbedded) { add2hash($opts, { socket_id => $::XID, child => $::WizardTable, }); delete $opts->{no_Window_Manager}; $::Plug = $::WizardWindow = _gtk(undef, 'Plug', 'gtknew', $opts); sync($::WizardWindow); } else { add2hash($opts, { child => gtknew('Frame', shadow_type => 'out', child => $::WizardTable), }); $::WizardWindow = _create_Window($opts); } } else { %$opts = (); } set_main_window_size($::WizardWindow); $w = $::WizardWindow; gtkadd($::WizardTable, children_tight => [ $provided_banner ]) if $provided_banner; gtkadd($::WizardTable, children_loose => [ $sub_child ]); } bless { real_window => $w, child => $sub_child, pop_it => $pop_it, if_($provided_banner, banner => $provided_banner), }, 'mygtk2::MagicWindow'; } # A standard About dialog. Used with: # my $w = gtknew('AboutDialog', ...); # $w->show_all; # $w->run; sub _gtk__AboutDialog { my ($w, $opts) = @_; if (!$w) { $w = Gtk2::AboutDialog->new; $w->signal_connect(response => sub { $_[0]->destroy }); $w->set_program_name(delete $opts->{name}) if exists $opts->{name}; $w->set_version(delete $opts->{version}) if exists $opts->{version}; $w->set_icon(gtknew('Pixbuf', file => delete $opts->{icon})) if exists $opts->{icon}; $w->set_logo(gtknew('Pixbuf', file => delete $opts->{logo})) if exists $opts->{logo}; $w->set_copyright(delete $opts->{copyright}) if exists $opts->{copyright}; $w->set_url_hook(sub { my (undef, $url) = @_; run_program::raw({ detach => 1 }, 'www-browser', $url); }); $w->set_email_hook(sub { my (undef, $url) = @_; run_program::raw({ detach => 1 }, 'www-browser', $url); }); if (my $url = delete $opts->{website}) { $url =~ s/^https:/http:/; # Gtk2::About doesn't like "https://..." like URLs $w->set_website($url); } $w->set_license(delete $opts->{license}) if exists $opts->{license}; $w->set_wrap_license(delete $opts->{wrap_license}) if exists $opts->{wrap_license}; $w->set_comments(delete $opts->{comments}) if exists $opts->{comments}; $w->set_website_label(delete $opts->{website_label}) if exists $opts->{website_label}; $w->set_authors(delete $opts->{authors}) if exists $opts->{authors}; $w->set_documenters(delete $opts->{documenters}) if exists $opts->{documenters}; $w->set_translator_credits(delete $opts->{translator_credits}) if exists $opts->{translator_credits}; $w->set_artists(delete $opts->{artists}) if exists $opts->{artists}; $w->set_modal(delete $opts->{modal}) if exists $opts->{modal}; $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__FileChooser { my ($w, $opts) = @_; #- no nice way to have a {file_ref} on a FileChooser since selection_changed only works for browsing, not file/folder creation if (!$w) { my $action = delete $opts->{action} || internal_error("missing action for FileChooser"); $w = Gtk2::FileChooserWidget->new($action); my $file = $opts->{file} && delete $opts->{file}; if (my $dir = delete $opts->{directory} || $file && dirname($file)) { $w->set_current_folder($dir); } if ($file) { if ($action =~ /save|create/) { $w->set_current_name(basename($file)); } else { $w->set_filename($file); } } } $w; } sub _gtk__VPaned { &_gtk_any_Paned } sub _gtk__HPaned { &_gtk_any_Paned } sub _gtk_any_Paned { my ($w, $opts, $class, $action) = @_; if (!$w) { $w = "Gtk2::$class"->new; $w->set_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; } elsif ($action eq 'gtkset') { $_->destroy foreach $w->get_children; } foreach my $opt (qw(resize1 shrink1 resize2 shrink2)) { $opts->{$opt} = 1 if !defined $opts->{$opt}; } $w->pack1(delete $opts->{child1}, delete $opts->{resize1}, delete $opts->{shrink1}); $w->pack2(delete $opts->{child2}, delete $opts->{resize2}, delete $opts->{shrink2}); $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_homogeneous(delete $opts->{homogenous}) if exists $opts->{homogenous}; $w->set_spacing(delete $opts->{spacing}) if exists $opts->{spacing}; $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}}) : exists $opts->{children_centered} ? ([ 1, gtknew('VBox') ], (map { [ 0, $_ ] } @{delete $opts->{children_centered}}), [ 1, gtknew('VBox') ]) : (); my $padding = delete $opts->{padding}; foreach (@child) { my ($fill, $child) = @$_; $fill eq '0' || $fill eq '1' || $fill eq 'fill' || $fill eq 'expand' or internal_error("odd {children} parameter must be 0 or 1 (got $fill)"); ref $child or $child = Gtk2::WrappedLabel->new($child); my $expand = $fill && $fill ne 'fill' ? 1 : 0; $w->pack_start($child, $expand, $fill, $padding || 0); $child->show; } } #- this magic function redirects method calls: #- * default is to redirect them to the {child} #- * if the {child} doesn't handle the method, we try with the {real_window} #- (eg : add_accel_group set_position set_default_size #- * a few methods are handled specially my %for_real_window = map { $_ => 1 } qw(show_all size_request); sub mygtk2::MagicWindow::AUTOLOAD { my ($w, @args) = @_; my ($meth) = $mygtk2::MagicWindow::AUTOLOAD =~ /mygtk2::MagicWindow::(.*)/; my ($s1, @s2) = $meth eq 'show' ? ('real_window', 'banner', 'child') : $meth eq 'destroy' || $meth eq 'hide' ? ($w->{pop_it} ? 'real_window' : ('child', 'banner')) : $meth eq 'get' && $args[0] eq 'window-position' || $for_real_window{$meth} || !$w->{child}->can($meth) ? 'real_window' : 'child'; #- warn "mygtk2::MagicWindow::$meth", first($w =~ /HASH(.*)/), " on $s1 @s2 (@args)\n"; $w->{$_} && $w->{$_}->$meth(@args) foreach @s2; $w->{$s1}->$meth(@args); } sub _create_Window { my ($opts) = @_; my $no_Window_Manager = exists $opts->{no_Window_Manager} ? delete $opts->{no_Window_Manager} : !$::isStandalone; add2hash($opts, { if_(!$::isInstall && !$::isWizard, border_width => 5), #- policy: during install, we need a special code to handle the weird centering, see below position_policy => $::isInstall ? 'none' : $no_Window_Manager ? 'center-always' : 'center-on-parent', if_($::isInstall, position => [ $::rootwidth - ($::o->{windowwidth} + $::real_windowwidth) / 2, $::logoheight + ($::o->{windowheight} - $::real_windowheight) / 2, ]), }); my $w = _gtk(undef, 'Window', 'gtknew', $opts); #- when the window is closed using the window manager "X" button (or alt-f4) $w->signal_connect(delete_event => sub { if ($::isWizard) { $w->destroy; die 'wizcancel'; } else { Gtk2->main_quit; } }); if ($no_Window_Manager) { _force_keyboard_focus($w); } if ($::isInstall) { require install::gtk; #- for perl_checker install::gtk::handle_unsafe_mouse($::o, $w); $w->signal_connect(key_press_event => \&install::gtk::special_shortcuts); #- force center at a weird position, this can't be handled by position_policy #- because center-on-parent is a window manager hint, and we don't have a WM my ($wi, $he); $w->signal_connect(size_allocate => sub { my (undef, $event) = @_; my @w_size = $event->values; return if $w_size[2] == $wi && $w_size[3] == $he; #BUG (undef, undef, $wi, $he) = @w_size; $w->move(max(0, $::rootwidth - ($::o->{windowwidth} + $wi) / 2), max(0, $::logoheight + ($::o->{windowheight} - $he) / 2)); }); #- without this, the focus is broken during install, though this is not needed during X test, why?? $w->show; } $w; } my $current_window; sub _force_keyboard_focus { my ($w) = @_; sub _XSetInputFocus { my ($w) = @_; if ($current_window == $w) { $w->window->XSetInputFocus; } 0; } #- force keyboard focus instead of mouse focus my $previous_current_window = $current_window; $current_window = $w; $w->signal_connect(expose_event => \&_XSetInputFocus);