package partition_table::raw; # $Id$ use diagnostics; use strict; use common; use devices; use detect_devices; use log; use c; my @MBR_signatures = ( if_(arch() =~ /ppc/, map { [ 'yaboot', 0, "PM", 0x200 * $_ + 0x10, "bootstrap\0" ] } 0 .. 61 ), [ 'empty', 0, "\0\0\0\0" ], [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x17e, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x18a, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x181, "GRUB \0" ], [ 'lilo', 0x2, "LILO" ], [ 'lilo', 0x6, "LILO" ], [ 'grub', 0x6, "GRUB" ], [ 'osbs', 0x2, "OSBS" ], #- http://www.prz.tu-berlin.de/~wolf/os-bs.html [ 'pqmagic', 0xef, "PQV" ], [ 'BootStar', 0x130, "BootStar:" ], [ 'DocsBoot', 0x148, 'DocsBoot' ], [ 'system_commander', 0x1ad, "SYSCMNDRSYS" ], [ 'Be Os', 0x24, 'Boot Manager' ], [ 'os2', 0, "\xFA\xB8\x30\x00", 0xfA, "OS/2" ], [ 'TimO', 0, 'IBM Thinkpad hibernation partition' ], [ 'dos', 0xa0, "\x25\x03\x4E\x02\xCD\x13" ], [ 'dos', 0xa0, "\x00\xB4\x08\xCD\x13\x72" ], #- nt2k's [ 'dos', 0x60, "\xBB\x00\x7C\xB8\x01\x02\x57\xCD\x13\x5F\x73\x0C\x33\xC0\xCD\x13" ], #- nt's [ 'dos', 0x70, "\x0C\x33\xC0\xCD\x13\x4F\x75\xED\xBE\xA3" ], [ 'freebsd', 0xC0, "\x00\x30\xE4\xCD\x16\xCD\x19\xBB\x07\x00\xB4" ], [ 'freebsd', 0x160, "\x6A\x10\x89\xE6\x48\x80\xCC\x40\xCD\x13" ], [ 'dummy', 0xAC, "\x0E\xB3\x07\x56\xCD\x10\x5E\xEB" ], #- caldera? [ 'ranish', 0x100, "\x6A\x10\xB4\x42\x8B\xF4\xCD\x13\x8B\xE5\x73" ], [ 'os2', 0x1c2, "\x0A" ], [ 'Acronis', 0, "\xE8\x12\x01" ], ); sub typeOfMBR($) { typeFromMagic(devices::make($_[0]), @MBR_signatures) } sub typeOfMBR_($) { typeFromMagic($_[0], @MBR_signatures) } sub hasExtended { 0 } sub cylinder_size($) { my ($hd) = @_; $hd->{geom}{sectors} * $hd->{geom}{heads}; } sub first_usable_sector { 1 } sub last_usable_sector { my ($hd) = @_; $hd->{totalsectors}; } #- default method for starting a partition, only head size or twice #- is allowed for starting a partition after a cylinder boundarie. sub adjustStart($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; $part->{start} = round_up($part->{start}, $part->{start} % cylinder_size($hd) < 2 * $hd->{geom}{sectors} ? $hd->{geom}{sectors} : cylinder_size($hd)); $part->{size} = $end - $part->{start}; $part->{size} > 0 or die "adjustStart get a too small partition to handle correctly"; } #- adjusting end to match a cylinder boundary, two methods are used and must #- match at the end, else something is wrong and nothing will be done on #- partition table. #- $end2 is computed by removing 2 (or only 1 if only 2 heads on drive) groups #- of sectors, this is necessary to handle extended partition where logical #- partition start after 1 (or 2 accepted) groups of sectors (typically 63). #- $end is floating (is not on cylinder boudary) so we have to choice a good #- candidate, $end1 or $end2 should always be good except $end1 for small #- partition size. sub adjustEnd($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; $end > $hd->{geom}{cylinders} * cylinder_size($hd) && $end <= $hd->{totalsectors} and return; my $end1 = round_down($end, cylinder_size($hd)); my $end2 = round_up($end - ($hd->{geom}{heads} > 2 ? 2 : 1) * $hd->{geom}{sectors}, cylinder_size($hd)); $end2 <= $hd->{geom}{cylinders} * cylinder_size($hd) or die "adjustEnd go beyond end of device geometry ($end2 > $hd->{totalsectors})"; $part->{size} = ($end1 - $part->{start} > cylinder_size($hd) ? $end1 : $end2) - $part->{start}; $part->{size} > 0 or internal_error("adjustEnd get a too small partition to handle correctly"); } sub get_geometry($) { my ($dev) = @_; my $g = ""; sysopen(my $F, $dev, 0) or return; ioctl($F, c::HDIO_GETGEO(), $g) or return; my %geom; @geom{qw(heads sectors cylinders start)} = unpack "CCSL", $g; $geom{totalcylinders} = $geom{cylinders}; my $total; #- $geom{cylinders} is no good (only a ushort, that means less than 2^16 => at best 512MB) if ($total = c::total_sectors(fileno $F)) { $geom{cylinders} = int $total / $geom{heads} / $geom{sectors}; } else { $total = $geom{heads} * $geom{sectors} * $geom{cylinders} } { geom => \%geom, totalsectors => $total }; } sub openit { my ($hd, $o_mode) = @_; my $F; sysopen($F, $hd->{file}, $o_mode || 0) and $F; } # cause kernel to re-read partition table sub kernel_read($) { my ($hd) = @_; common::sync(); my $F = openit($hd) or return 0; common::sync(); sleep(1); $hd->{rebootNeeded} = !ioctl($F, c::BLKRRPART(), 0); common::sync(); close $F; common::sync(); sleep(1); } sub raw_removed { my ($_hd, $_raw) = @_; } sub can_raw_add { my ($hd) = @_; $_->{size} || $_->{type} or return 1 foreach @{$hd->{primary}{raw}}; 0; } sub raw_add { my ($_hd, $raw, $part) = @_; foreach (@$raw) { $_->{size} || $_->{type} and next; $_ = $part; return; } die "raw_add: partition table already full"; } sub zero_MBR { my ($hd) = @_; #- force the standard partition type for the architecture my $type = arch() =~ /ia64/ ? 'gpt' : arch() eq "alpha" ? "bsd" : arch() =~ /^sparc/ ? "sun" : arch() eq "ppc" ? "mac" : "dos"; #- override standard mac type on PPC for IBM machines to dos $type = "dos" if arch() =~ /ppc/ && detect_devices::get_mac_model() =~ /^IBM/; require "partition_table/$type.pm"; bless $hd, "partition_table::$type"; $hd->{primary} = $hd->clear_raw; delete $hd->{extended}; } sub zero_MBR_and_dirty { my ($hd) = @_; zero_MBR($hd); $hd->{isDirty} = $hd->{needKernelReread} = 1; } #- ugly stuff needed mainly for Western Digital IDE drives #- try writing what we've just read, yells if it fails #- testing on last sector of head #0 (unused in 99% cases) #- #- return false if the device can't be written to (especially for Smartmedia) sub test_for_bad_drives { my ($hd) = @_; log::l("test_for_bad_drives($hd->{file})"); my $sector = $hd->{geom}{sectors} - 1; sub error { die "$_[0] error: $_[1]" } my $F = openit($hd, $::testing ? 0 : 2) or error(openit($hd) ? 'write' : 'read', "can't open device"); my $seek = sub { c::lseek_sector(fileno($F), $sector, 0) or error('read', "seeking to sector $sector failed"); }; my $tmp; &$seek; sysread $F, $tmp, $SECTORSIZE or error('read', "can't even read ($!)"); return if $hd->{readonly} || $::testing; &$seek; syswrite $F, $tmp or error('write', "can't even write ($!)"); my $tmp2; &$seek; sysread $F, $tmp2, $SECTORSIZE or die "test_for_bad_drives: can't even read again ($!)"; $tmp eq $tmp2 or die \N("Something bad is happening on your drive. A test to check the integrity of data has failed. It means writing anything on the disk will end up with random, corrupted data."); } 1; /a> 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
package mygtk2;
use diagnostics;
use strict;
our @ISA = qw(Exporter);
our @EXPORT = qw(gtknew gtkset gtkadd gtkval_register gtkval_modify);
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 @_");
}
my %opts = @_;
_gtk(undef, $class, 'gtknew', \%opts);
}
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 @_");
}
my %opts = @_;
$class =~ s/^(Gtk2|Gtk2::Gdk|mygtk2)::// or internal_error("gtkset unknown class $class");
_gtk($w, $class, 'gtkset', \%opts);
}
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 @_");
}
my %opts = @_;
$class =~ s/^(Gtk2|Gtk2::Gdk|mygtk2)::// or internal_error("gtkadd unknown class $class");
_gtk($w, $class, 'gtkadd', \%opts);
}
my %refs;
sub gtkval_register {
my ($w, $ref, $sub) = @_;
push @{$w->{_ref}}, $ref;
$w->signal_connect(destroy => sub {
@{$refs{$ref}} = grep { $_->[1] != $w } @{$refs{$ref}};
delete $refs{$ref} if !@{$refs{$ref}};
});
push @{$refs{$ref}}, [ $sub, $w ];
}
sub gtkval_modify {
my ($ref, $val, @to_skip) = @_;
my $prev = '' . $ref;
$$ref = $val;
if ($prev ne '' . $ref) {
internal_error();
}
foreach (@{$refs{$ref} || []}) {
my ($f, @para) = @$_;
$f->(@para) if !member($f, @to_skip);
}
}
my $global_tooltips;
sub _gtk {
my ($w, $class, $action, $opts) = @_;
if (my $f = $mygtk2::{"_gtk__$class"}) {
$w = $f->($w, $opts, $class, $action);
} else {
internal_error("$action $class: unknown class");
}
$w->set_size_request(delete $opts->{width} || -1, delete $opts->{height} || -1) if exists $opts->{width} || exists $opts->{height};
if (my $position = delete $opts->{position}) {
$w->move($position->[0], $position->[1]);
}
$w->set_name(delete $opts->{widget_name}) if exists $opts->{widget_name};
$w->can_focus(delete $opts->{can_focus}) if exists $opts->{can_focus};
$w->can_default(delete $opts->{can_default}) if exists $opts->{can_default};
$w->grab_focus if delete $opts->{grab_focus};
(delete $opts->{size_group})->add_widget($w) if $opts->{size_group};
if (my $tip = delete $opts->{tip}) {
$global_tooltips ||= Gtk2::Tooltips->new;
$global_tooltips->set_tip($w, $tip);
}
#- WARNING: hide_ref and show_ref are not effective until you gtkval_modify the ref
if (my $hide_ref = delete $opts->{hide_ref}) {
gtkval_register($w, $hide_ref, sub { $$hide_ref ? $w->hide : $w->show });
} elsif (my $show_ref = delete $opts->{show_ref}) {
gtkval_register($w, $show_ref, sub { $$show_ref ? $w->show : $w->hide });
}
if (%$opts && !$opts->{allow_unknown_options}) {
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 (!$w) {
if (!$opts->{image}) {
add2hash_($opts, { mnemonic => 1 });
}
$w = $opts->{image} ? "Gtk2::$class"->new :
delete $opts->{mnemonic} ? "Gtk2::$class"->new_with_mnemonic(delete $opts->{text} || '') :
"Gtk2::$class"->new_with_label(delete $opts->{text} || '');
$w->{format} = delete $opts->{format} if exists $opts->{format};
}
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 (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");
$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 not find image $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 not find image $$file_ref");
$w->set_from_file($file);
};
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) = @_;
gtknew('HBox', children_tight => [ _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};
}
if (my $text_ref = delete $opts->{text_ref}) {
my $set = sub { $w->set_text($$text_ref) };
gtkval_register($w, $text_ref, $set);
$set->();
}
$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, $_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->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::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::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__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(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} or internal_error("missing child");
my $provided_banner = delete $opts->{banner};
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,
});
$::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';
}
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) {
my $meth = $action =~ /save|create/ ? 'set_current_name' : 'set_filename';
$w->$meth($file);
}
}
$w;
}
sub _gtk__VBox { &_gtk_any_Box }
sub _gtk__HBox { &_gtk_any_Box }