summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/harddrake2
blob: 9210f5ca7e47f4beeac1073220728d149d52c920 (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
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
#!/usr/bin/perl
# -*- coding: utf-8 -*-

use utf8;
use strict;
use diagnostics;
use lib qw(/usr/lib/libDrakX);
use standalone;
use MDK::Common; # help perl_checker
use common;

# i18n: IMPORTANT: to get correct namespace (drakconf instead of only libDrakX)
BEGIN { unshift @::textdomains, 'drakconf' }

use mygtk3 qw(gtknew); #- do not import gtkadd which conflicts with ugtk3 version
use ugtk3 qw(:create :helpers :wrappers);
use interactive;
use modules::parameters;
use harddrake::data;  #- needs to stay after use-ugtk3 as long as this module defines globals containing some N()
use fsedit;
use pkgs;
use POSIX qw(:sys_wait_h);


# { field => [ short_translation, full_description] }
my %fields = 
    (
     generic =>
     {
      "alternative_drivers" => [ N("Alternative drivers"),
                                 N("the list of alternative drivers for this sound card") ],
      "bus" => 
      [ N("Bus"), 
        N("this is the physical bus on which the device is plugged (eg: PCI, USB, ...)") ],
      "bus_id" => 
      [ N("Bus identification"), 
        N("- PCI and USB devices: this lists the vendor, device, subvendor and subdevice PCI/USB ids") ],
      "bus_location" => 
      [ N("Location on the bus"), 
        N("- pci devices: this gives the PCI slot, device and function of this card
- eide devices: the device is either a slave or a master device
- scsi devices: the scsi bus and the scsi device ids") ],
      "capacity" => [ N("Drive capacity"), N("special capacities of the driver (burning ability and or DVD support)") ],
      "description" => [ N("Description"), N("this field describes the device") ],
      "device" => [ N("Old device file"),
                    N("old static device name used in dev package") ],
      "driver" => [
                   #-PO: here "module" is the "jargon term" for a kernel driver
                   N("Module"), N("the module of the GNU/Linux kernel that handles the device") ],
      "extended_partitions" => [ N("Extended partitions"), N("the number of extended partitions") ],
      "geometry" => [ N("Geometry"), N("Cylinder/head/sectors geometry of the disk") ],
      "host" => [ N("Disk controller"), N("the disk controller on the host side") ],
      "info" => [ N("Identifier"), N("usually the device serial number") ],
      "media_type" => [ N("Media class"), N("class of hardware device") ],
      "Model" => [ N("Model"), N("hard disk model") ],
      "port" => [ N("Port"), N("network printer port") ],
      "primary_partitions" => [ N("Primary partitions"), N("the number of the primary partitions") ],
      "Vendor" => [ N("Vendor"), N("the vendor name of the device") ],
      "pci_domain" => [ N("PCI domain"), N("the PCI domain of the device") ],
      "pci_revision" => [ N("PCI revision"), N("the PCI domain of the device") ],
      "pci_bus" => [ N("Bus PCI #"), N("the PCI bus on which the device is plugged") ],
      "pci_device" => [ N("PCI device #"), N("PCI device number") ],
      "pci_function" => [ N("PCI function #"), N("PCI function number") ],
      "vendor" => [ N("Vendor ID"), N("this is the standard numerical identifier of the vendor") ],
      "id" => [ N("Device ID"), N("this is the numerical identifier of the device") ],
      "subvendor" => [ N("Sub vendor ID"), N("this is the minor numerical identifier of the vendor") ],
      "subid" => [ N("Sub device ID"), N("this is the minor numerical identifier of the device") ],
      "usb_pci_device" =>, [ N("Device USB ID"), N("..") ],
     },
     CPU =>
     {
      "system type" => [ "Sytem Type", "Name of the system" ],
      "BogoMIPS" => [ N("Bogomips"), N("the GNU/Linux kernel needs to run a calculation loop at boot time to initialize a timer counter.  Its result is stored as bogomips as a way to \"benchmark\" the cpu.") ],
      "bogomips" => [ N("Bogomips"), N("the GNU/Linux kernel needs to run a calculation loop at boot time to initialize a timer counter.  Its result is stored as bogomips as a way to \"benchmark\" the cpu.") ],
      "cache size" => [ N("Cache size"), N("size of the (second level) cpu cache") ],
      "cpu family" => [ N("Cpuid family"), N("family of the cpu (eg: 6 for i686 class)") ],
      "cpuid level" => [ N("Cpuid level"), N("information level that can be obtained through the cpuid instruction") ],
      "cpu MHz" => [ N("Frequency (MHz)"), N("the CPU frequency in MHz (Megahertz which in first approximation may be coarsely assimilated to number of instructions the cpu is able to execute per second)") ],
      "flags" => [ N("Flags"), N("CPU flags reported by the kernel") ],
      "cpu cores" => [ N("Cores"), N("CPU cores") ],
      "core id" => [ N("Core ID"), N("Core ID") ],
      "physical id" => [ N("Physical ID"), N("Physical ID") ],
      "apicid" => [ N("ACPI ID"), N("ACPI ID") ],
      "siblings" => [ N("Siblings"), N("Siblings") ],
      "level" => [ N("Level"), N("sub generation of the cpu") ],
      "model" => [ N("Model"), N("generation of the cpu (eg: 8 for Pentium III, ...)") ],
      "model name" => [ N("Model name"), N("official vendor name of the cpu") ],
      "cpu model" => [ N("Model name"), N("official vendor name of the cpu") ],
      "name" => [ N("Name"), N("the name of the CPU") ],
      "processor" => [ N("Processor ID"), N("the number of the processor") ],
      "stepping" => [ N("Model stepping"), N("stepping of the cpu (sub model (generation) number)") ],
      "vendor_id" => [ N("Vendor"), N("the vendor name of the processor") ],
      "wp" => [ N("Write protection"), N("the WP flag in the CR0 register of the cpu enforce write protection at the memory page level, thus enabling the processor to prevent unchecked kernel accesses to user memory (aka this is a bug guard)") ],
     },
     FLOPPY =>
     {
      info => [ N("Floppy format"), N("format of floppies supported by the drive") ],
     },
     HARDDISK =>
     {
      channel => [ N("Channel"), N("EIDE/SCSI channel") ],
      info => [ N("Disk identifier"), N("usually the disk serial number") ],
      id => [ N("Target id number"), N("the SCSI target identifier") ],
      lun => [ N("Logical unit number"), N("the SCSI Logical Unit Number (LUN). SCSI devices connected to a host are uniquely identified by a
channel number, a target id and a logical unit number") ],
     },
     MEMORY =>
     {
      'Installed Size' => [ 
          #-PO: here, "size" is the size of the ram chip (eg: 128Mo, 256Mo, ...)
          N("Installed size"), N("Installed size of the memory bank") ],
      'Enabled Size' => [ N("Enabled Size"), N("Enabled size of the memory bank") ],
      'name' => [ N("Type"), N("type of the memory device") ],
      'Current Speed' => [ N("Speed"), N("Speed of the memory bank") ],
      'Bank Connections' => [ N("Bank connections"), '' ],
      'Socket Designation' => [ N("Name"), N("Socket designation of the memory bank") ],
      # for Memory Device:
      'Locator' => [ N("Location"), ],
      'Size' => [ N("Size"), N("Size of the memory device") ],
      'Speed' => [ N("Speed"), N("Speed of the memory bank") ],
     },
     MOUSE =>
     {
      "device" => [ N("Device file"), N("the device file used to communicate with the kernel driver for the mouse") ],
      EmulateWheel => [ N("Emulated wheel"), N("whether the wheel is emulated or not") ],
      MOUSETYPE => [ N("Type"), N("the type of the mouse") ],
      name => [ N("Name"), N("the name of the mouse") ],
      nbuttons => [ N("Number of buttons"), N("the number of buttons the mouse has") ],
      type => [ N("Bus"), N("the type of bus on which the mouse is connected") ],
      Protocol => [ N("Mouse protocol used by X11"), N("the protocol that the graphical desktop use with the mouse") ],
     }
    );

my $identification = N("Identification");
my %groups = (
              generic =>
              {
               $identification => [ qw(Vendor model description info media_type) ],
               N("Connection") => [ qw(bus pci_domain pci_bus pci_device pci_function pci_revision vendor id subvendor subid) ],
              },
              AUDIO =>
              {
               N("Driver") => [ qw(driver alternative_drivers) ],
              },
              CPU =>
              {
               $identification => [ qw(processor vendor_id), "model name", "cpu family", qw(model level stepping), "cpuid level" ],
               N("Cores") => [ 'cpu cores', 'core id', 'physical id', 'apicid', 'siblings' ],
               N("Performances") => [ "cpu MHz", "cache size", "bogomips" ],
              },
              HARDDISK =>
              {
               $identification => [ qw(Vendor Model description info media_type) ],
               N("Connection") => [ qw(bus channel lun id) ],
               N("Bus identification") => [ qw(vendor id subvendor subid) ],
               N("Device") => [ qw(device) ],
               N("Partitions") => [ qw(primary_partitions extended_partitions) ],
              },
              MOUSE =>
              {
               $identification => [ qw(name type MOUSETYPE Protocol) ],
               N("Features") => [ qw(EmulateWheel nbuttons) ],
              },
             );

foreach my $class (qw(BURNER CDROM DVDROM)) {
    $groups{$class} = $groups{HARDDISK};
    $fields{$class} = $fields{HARDDISK};
}


my ($in, $pid, $w);

my (%options, %check_boxes);
my $conffile = "/etc/sysconfig/harddrake2/ui.conf";

my ($current_device, $current_class, $current_configurator);

my %sysh = distrib();
my $distro_name = $sysh{system};

my %menu_options = (
                    'MODEMS_DETECTION' => N("Autodetect _modems"),
                    'PARALLEL_ZIP_DETECTION' => N("Autodetect parallel _zip drives"),
                    );

$ugtk3::wm_icon = "harddrake";
$w = ugtk3->new(N("Hardware Configuration"));
# fake diagnostics pragma:
local $::main_window = $w->{real_window};

my $has_help = -x "/usr/sbin/drakhelp_inst";
my $ui = gtknew('UIManager', actions => [
		    # [name, stock_id, value, label, accelerator, tooltip, callback]
		    [ 'FileMenu',        undef, N("_File") ],
		    [ 'Quit', undef, N("_Quit"), N("<control>Q"), undef, \&quit_global ],
		    [ 'OptionsMenu',        undef, N("_Options") ],
		    [ 'HelpMenu',        undef, N("_Help") ],
		    if_($has_help, [ 'Help', undef, N("_Help"), N("<control>H"), undef, \&run_help ]),
		    [ 'Fields description', undef, N("_Fields description"), undef, undef, \&fields_help ],
		    [ 'Report Bug', undef, N("_Report Bug"), undef, undef, \&run_drakbug ],
		    [ 'About', undef, N("_About..."), '', undef, \&about ],
		],
		toggle_actions => [
		    [ 'MODEMS_DETECTION', undef, $menu_options{MODEMS_DETECTION}, undef, undef, \&handle_modem_option ],
		    [ 'PARALLEL_ZIP_DETECTION', undef, $menu_options{PARALLEL_ZIP_DETECTION}, undef, undef, \&handle_zip_option ],
		],
		string =>
		join("\n",
		     qq(<ui>
  <menubar name='MenuBar'>
    <menu action='FileMenu'>
      <menuitem action='Quit'/>
    </menu>
    <menu action='OptionsMenu'>
      <menuitem action='MODEMS_DETECTION'/>
      <menuitem action='PARALLEL_ZIP_DETECTION'/>
    </menu>
    <menu action='HelpMenu'>),
		     if_($has_help, "<menuitem action='Help'/>"),
		     qq(
      <menuitem action='Fields description'/>
      <menuitem action='Report Bug'/>
      <menuitem action='About'/>
    </menu>
  </menubar>
</ui>)));

$in = 'interactive'->vnew('su'); #require_root_capability();

%options = getVarsFromSh($conffile);
$options{MDV_ONLINE} ||= [];

# Build the gui
add_icon_path('/usr/share/pixmaps/harddrake2/');
$::noborderWhenEmbedded = 1;


my $menubar = $ui->get_widget('/MenuBar');
$w->{window}->set_size_request(805, 550) if !$::isEmbedded;

my $tree_model = Gtk3::TreeStore->new("Gtk3::Gdk::Pixbuf", "Glib::String", "Glib::Int");
$w->{window}->add(gtkpack_(0, Gtk3::VBox->new(0, 0),
                           0, $menubar,
                           0, Gtk3::Banner->new("/usr/share/mcc/themes/default/harddrake-mdk.png", translate("Hardware")),
                           1, create_hpaned(my $f = gtkadd(Gtk3::Frame->new(N("Detected hardware")), 
                                                   create_scrolled_window(gtkset_size_request(my $tree = Gtk3::TreeView->new_with_model($tree_model), 350, -1), ['automatic', 'automatic'])),
                                            gtkpack_(0, Gtk3::VBox->new(0, 0),
                                                     1, gtkadd(my $frame = Gtk3::Frame->new(N("Information")),
                                                               create_scrolled_window(my $text = Gtk3::TextView->new)),
                                                     0, my $module_cfg_button = gtksignal_connect(Gtk3::Button->new(N("Set current driver options")),
                                                                                                  clicked => sub {
                                                                                                      local $SIG{CHLD} = undef;
                                                                                                      require modules::interactive;
                                                                                                      modules::interactive::config_window($in, $current_device);
                                                                                                  }),
                                                     0, my $config_button = gtksignal_connect(Gtk3::Button->new(N("Run config tool")),
                                                                                              # we've a configurator, let's add a button for it and show it
                                                                                              clicked => sub {
                                                                                                  return 1 if defined $pid;
                                                                                                  run_program::raw({ detach => 1 }, $current_configurator);
                                                                                              })
                                                     ),
                                            ),
                          )
                 );

$f->set_size_request(350, -1);
$text->set_wrap_mode('word');
$frame->set_size_request(300, 450) unless $::isEmbedded;
#    $tree->set_column_auto_resize(0, 1);
my (@data, @configurators);
$tree->append_column(my $textcolumn = Gtk3::TreeViewColumn->new);
$textcolumn->pack_start(my $img_renderer = Gtk3::CellRendererPixbuf->new, 0);
$textcolumn->set_attributes($img_renderer, pixbuf => 0);

$textcolumn->pack_start(my $text_renderer = Gtk3::CellRendererText->new, 1);
$textcolumn->set_attributes($text_renderer, text => 1);

$tree->set_headers_visible(0);

sub fill_default_text {
    my ($text) = @_;
    $text->get_buffer->set_text(N("Click on a device in the tree on the left in order to display its information here."));
}

my %has_parameter;
$tree->get_selection->signal_connect('changed' => sub {
    my ($select) = @_;
    my ($model, $iter) = $select->get_selected;
    if ($model) {
        my $idx = $model->get($iter, 2);
        ($current_device, $current_class) = @{$data[$idx]};
        
        if ($idx ne -1) {
            my %device_fields = map {
                # The U+200E character is to force LTR display, as what what follows the colon is always in LTR (device names, paths, etc),
                # this ensures proper displaying of names like /dev/fd0 (otherwise it gets 'dev/fd0/').
                # it must come *after* the space, as the space must follow the colon following the direction of writting.
                my $field = lookup_field($_);
                if_($_ && $field->[0], $_ =>
                   [
                    [ $field->[0] . ": \x{200e}", { 'foreground' => 'royalblue3', 'weight' => 'bold' } ],
                    [ ($current_device->{$_} =~ /^(unknown)/ ? N("unknown") :
                       $current_device->{$_} =~ /^(Unknown)/ ? N("Unknown") : 
                       $current_device->{$_} eq 'yes' ? N("Yes") : 
                       $current_device->{$_} eq 'no'  ? N("No") : 
                       $current_device->{$_}) . "\n\n", if_($_ eq 'driver' && $current_device->{$_} =~ /^unknown|^Bad:/, { foreground => 'indian red' }) ]
                   ]);
            } sort keys %$current_device;
            my %groups = map { if_(ref $groups{$_}, %{$groups{$_}}) } 'generic', $current_class;
            my ($grouped, $ungrouped) = partition {
                my $field = $_;
                member($field, map { @$_ } values %groups);
            } sort keys %device_fields;
            my @formated;
            foreach my $group ($identification, grep { $_ ne $identification } keys %groups) {
                my @fields = @{$groups{$group}};
                # have we at least a member in that group?
                next unless any { member($_, @fields) } @$grouped;
                
                push @formated, titleFormat($group);
                push @formated, map { if_(ref $_, @$_) } @device_fields{@fields};
            }
            push @formated, if_(@formated && @$ungrouped, titleFormat(N("Misc"))), map { @{$device_fields{$_}} } @$ungrouped;
            gtktext_insert($text, \@formated);

            foreach (keys %$current_device) {
                print qq(Warning: skip "$_" field => "$current_device->{$_}"\n\n) unless (lookup_field($_))[0];
            }
            
            # if we've valid driver, let's offer to configure it, else hide buttons 
            show_hide(defined($current_device->{driver}) && $current_device->{driver} !~ /^unknown|^Bad|^Card|^Hsf|^kbd|^Mouse:USB|^mouse\d|^Removable:|\|/, $module_cfg_button);
            
            $current_configurator = $configurators[$idx];
            show_hide($current_configurator && -x first(split /\s+/, $current_configurator), $config_button);  # strip arguments for -x test

            # hide module configuration button if needed:
            my $driver = $current_device->{driver};
            if (!defined $has_parameter{$driver}) {
                local $SIG{CHLD} = undef;
                $has_parameter{$driver} ||= to_bool(scalar modules::parameters::parameters($driver));
            }
            show_hide($has_parameter{$driver}, $module_cfg_button);

            return 1;
        }
    }
    # No device is selected:
    fill_default_text($text);
    undef $current_device;
    $config_button->hide;
    $module_cfg_button->hide;
});

$w->{rwindow}->add_accel_group($ui->get_accel_group);
# show the main window earlier (so that sub dialogs can use main
# window's icon and so that no Gtk+ critical message got displayed):
$w->{rwindow}->show_all;
my $flush_guard = Gtk3::GUI_Update_Guard->new;
$_->hide foreach $module_cfg_button, $config_button; # hide buttons while no device
my $wait = $in->wait_message(N("Please wait"), N("Detection in progress"));
gtkflush() if !$::isEmbedded;


my $index = 0;

my @classes;

# Fill the graphic devices tree with a "tree branch" widget per device category
foreach my $hw_class (@harddrake::data::tree) {
    my ($Ident, $title, $icon, $configurator, $detector) = @$hw_class{qw(class string icon configurator detector)};
    next if ref($detector) ne "CODE"; #skip class witouth detector
    next if $Ident =~ /MODEM/ && $::testing;
    $detect_devices::detect_serial_modem = 0 if $Ident =~ /MODEM/ && !$options{MODEMS_DETECTION};

    my @devices = $detector->(\%options);
    next unless @devices; # Skip empty class (no devices)
    push @classes, [ $Ident, $title, $icon, $configurator, @devices ];
}

# Fill the graphic devices tree with a "tree branch" widget per device category
foreach (@classes) {
    my ($Ident, $title, $icon, $configurator, @devices) = @$_;

    my $parent_iter = $tree_model->append_set(undef, [ 0 => gtkcreate_pixbuf($icon), 1 => $title, 2 => -1 ]);

    my $all_hds;
    $all_hds = fsedit::get_hds() if $Ident eq "HARDDISK";

    # Fill the graphic tree with a "tree leaf" widget per device
    foreach (@devices) {
        # we really should test for $title there:
        if ($_->{bus} && $_->{bus} eq "PCI") {
            # do not display unknown driver for system bridges that're managed by kernel core:
            delete $_->{driver} if $_->{driver} eq "unknown" && ($Ident =~ /^ATA_STORAGE|BRIDGE|SMB_CONTROLLER|MEMORY_OTHER $/ || $_->{description} =~ /3Com.*5610/);
        }
        rename_field($_, 'usb_description', 'description');
        # split description into manufacturer/description
        ($_->{Vendor}, $_->{description}) = split(/\|/, $_->{description}) if $_->{description};

        if ($_->{val}) { # Scanner ?
            my $val = $_->{val};
            ($_->{Vendor}, $_->{description}) = split(/\|/, $val->{DESCRIPTION});
        }
        # EIDE detection incoherency:
        if ($_->{bus} && $_->{bus} eq 'ide') {
            $_->{channel} = $_->{channel} ? N("secondary") : N("primary");
            delete $_->{info} if $_->{Vendor};
        }
        if (defined $_->{capacity}) {
            my ($burner, $dvd) = (N("burner"), N("DVD"));
            $_->{capacity} =~ s/burner/$burner/;
            $_->{capacity} =~ s/DVD/$dvd/;
        }
        $configurator .= harddrake::data::set_removable_configurator($Ident, $_);
        if ($Ident eq "HARDDISK") {
            my $hd = $_;
            my $info = find { $_->{device} eq $hd->{device} } @{$all_hds->{hds}};
            $hd->{geometry} = join('/', map { $info->{geom}{$_} } qw(cylinders heads sectors)) . " (CHS)";
            $hd->{primary_partitions} = @{$info->{primary}{normal}} if $info->{primary}{normal};
            $hd->{extended_partitions} = @{$info->{extended}} if $info->{extended};
            $hd->{primary_partitions} .= " (" . join(', ', map { $_->{device} }@{$info->{primary}{normal}}) . ")" if $hd->{primary_partitions};
            if ($hd->{extended_partitions}) {
                $hd->{extended_partitions} .= " (" . join(', ', map { $_->{normal}{device} }@{$info->{extended}}) . ")";
            } else {
                delete $hd->{extended_partitions} if $hd->{extended_partitions} eq '0';
            }
        }
        $_->{EmulateWheel} = bool2yesno($_->{EmulateWheel}) if $Ident eq "MOUSE";
        rename_field($_, 'usb_bus', 'bus');
        rename_field($_, 'usb_driver', 'driver');
        rename_field($_, 'usb_id', 'id');
        rename_field($_, 'usb_media_type', 'media_type');
        force_rename_field($_, 'nice_media_type', 'media_type');
        rename_field($_, 'usb_pci_bus', 'bus');
        force_rename_field($_, 'nice_bus', 'bus');
        rename_field($_, 'usb_vendor', 'vendor');
        rename_field($_, 'vendor_name', 'Vendor');

        foreach my $i (qw(synaptics unsafe val wacom)) { delete $_->{$i} }

        my $custom_id = harddrake::data::custom_id($_, $title);
        foreach my $field (qw(device)) {
            $_->{$field} = "/dev/$_->{$field}" if $_->{$field};
        }
        format_bus_ids($_);
        if ($_->{pci_revision}) {
            $_->{pci_revision} = sprintf("0x%02x", $_->{pci_revision});
        } else {
            delete $_->{pci_revision};# if $_->{pci_revision} eq "0x0000";
        }

        $tree_model->append_set($parent_iter, [ 1 => $custom_id, 2 => $index++ ]);
        push @data, [ $_, $Ident ];
        push @configurators, $configurator;
    }
}

undef $flush_guard;

sub format_bus_ids {
    my ($device) = @_;
    # do not vivify ids:
    return if !($device->{vendor} && $device->{id});
    foreach my $field (qw(vendor id subvendor subid)) {
        next if !$device->{$field};
        $device->{$field} = sprintf("0x%04x", $device->{$field});
        delete $device->{$field} if $device->{$field} eq "0xffff";  # 0xffff equals to '*'
    }
}

sub reap_children() {
    # reap zombies
    my $child_pid;
    do { $child_pid = waitpid(-1, POSIX::WNOHANG); undef $pid if $pid == $child_pid } until $child_pid > 0;
}

$SIG{CHLD} = \&reap_children;

$w->{rwindow}->signal_connect(delete_event => \&quit_global);
$w->{rwindow}->set_position('center') unless $::isEmbedded;

foreach (keys %menu_options) {
    $options{$_} = 0 unless defined($options{$_}); # force detection by default
    $check_boxes{$_} = $ui->get_widget('/MenuBar/OptionsMenu/' . $_);
    $check_boxes{$_}->set_active($options{$_});    # restore saved values
}

$textcolumn->set_min_width(350);
#$textcolumn->set_minmax_width(400);
$textcolumn->set_sizing('GTK_TREE_VIEW_COLUMN_AUTOSIZE');#GROW_ONLY
#$tree->columns_autosize();
$tree->signal_connect(realize => sub { $tree->get_selection->select_path(Gtk3::TreePath->new_first) });

{
    $SIG{CHLD} = undef;
    #local $SIG{CHLD} = sub {};

    # do no try to install too generic hw packages:
    my @blacklist = [ qw(dmraid mdadm) ];
    if (my @packages = difference2([ pkgs::detect_hardware_packages($in->do_pkgs) ], @blacklist)) {
        @packages = difference2(\@packages, [ $in->do_pkgs->are_installed(@packages) ]);
        # we use uniq() because on biarch machines, we got packages twice:
        my @packages2install = uniq($in->do_pkgs->are_available(@packages));

        undef $wait;
        
        if (@packages2install && $in->ask_yesorno(N("Warning"), N("The following packages need to be installed:\n") . join(', ', @packages2install))) {
            $in->do_pkgs->install(@packages2install);
        }
    }
}
$SIG{CHLD} = \&reap_children;
undef $wait;

# fill in default right text since no device is selected on startup:
fill_default_text($text);

$w->main;

sub about() {
    my $license = formatAlaTeX(translate($::license));
    $license =~ s/\n/\n\n/sg; # nicer formatting
    my $w = gtknew('AboutDialog', name => N("Harddrake"),
                   version => mageia_release_info()->{version},
                   logo => '/usr/share/icons/harddrake.png',
                   copyright => N("Copyright (C) %s by %s", '2001-2008', 'Mandriva') . "\n" . N("Copyright (C) %s by %s", '2011', N("Mageia")),
                   license => $license, wrap_license => 1,
                   comments => N("This is HardDrake, a %s hardware configuration tool.", $distro_name),
                   website => 'http://www.mageia.org',
                   website_label => N("Mageia"),
                   authors => [ 'Thierry Vignaud <thierry.vignaud.com>' ],
                   translator_credits =>
                     #-PO: put here name(s) and email(s) of translator(s) (eg: "John Smith <jsmith@nowhere.com>")
                     N("_: Translator(s) name(s) & email(s)\n"),
                   transient_for => $::main_window, modal => 1, position_policy => 'center-on-parent',
               );
    $w->show_all;
    $w->run;
}

sub fields_help() {
    if ($current_device) {
        create_dialog(N("Harddrake help"),
                      '<big><b>' . N("Description of the fields:\n\n") . '</b></big>'
                      . join("\n\n", map {
                          my $info = lookup_field($_);
                          if_($info->[0], formatAlaTeX(qq(<span foreground="royalblue3">$info->[0]:</span> $info->[1])));
                      } sort keys %$current_device),
               { use_markup => 1, transient => $w->{real_window}, height => 400, scroll => 1 });
    } else {
        create_dialog(N("Select a device!"), N("Once you've selected a device, you'll be able to see the device information in fields displayed on the right frame (\"Information\")"), { transient => $w->{real_window} });
    }
}

sub quit_global() {
    kill(15, $pid) if $pid;
    setVarsInSh($conffile, \%options) if !$::testing;
    ugtk3->exit(0);
}

sub run_drakbug() {
    run_program::raw({ detach => 1 }, 'drakbug', '--report', 'harddrake');
}

sub run_help() {
    run_program::raw({ detach => 1 }, 'drakhelp', '--id', 'harddrake');
}

sub handle_modem_option() {
    $options{MODEMS_DETECTION} = $check_boxes{MODEMS_DETECTION}->get_active;
}

sub handle_zip_option() {
    $options{PARALLEL_ZIP_DETECTION} = $check_boxes{PARALLEL_ZIP_DETECTION}->get_active;
}

sub show_hide {
    my ($bool, $button) = @_;
    if ($bool) { $button->show } else { $button->hide }
}

sub lookup_field {
    my ($field) = @_;
    my $class = find { defined $fields{$_} && defined $fields{$_}{$field} } ($current_class, 'generic');
    $fields{$class}{$field};
}

sub titleFormat {
    my ($title) = @_;
    [ $title . "\n", { 'weight' => 'bold', scale => 1.4399999999999 } ]; # Pango->PANGO_SCALE_LARGE
}

sub force_rename_field {
    my ($dev, $new_field, $field) = @_;
    if ($dev->{$new_field}) {
        delete $dev->{$field};
        rename_field($dev, $new_field, $field);
    }
}


sub rename_field {
    my ($dev, $field, $new_field) = @_;
    if ($dev->{$field}) {
        if ($dev->{$new_field}) {
            $dev->{$new_field} .= " ($dev->{$field})";
        } else {
            $dev->{$new_field} = $dev->{$field};
        }
        delete $dev->{$field};
    }
}

sub popup_menu {
    my ($menu) = @_;
    sub { my (undef, $event) = @_;
          if ($event->type eq 'button-press') {
              $menu->popup(undef, undef, undef, undef, $event->button, $event->time);
              # Tell calling code that we have handled this event; the buck stops here.
              return 1;
          }
          # Tell calling code that we have not handled this event; pass it on.
          return 0;
      };
}
hl opt">; use strict; use feature 'state'; our @ISA = qw(Exporter); our @EXPORT = qw(gtknew gtkset gtkadd gtkval_register gtkval_modify); use c; use log; use common; use Gtk2; sub 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; Locale::gettext::bind_textdomain_codeset($_, 'UTF8') foreach 'libDrakX', if_(!$::isInstall, 'libDrakX-standalone'), if_($::isRestore, 'draksnapshot'), if_($::isInstall, 'urpmi'), 'drakx-net', 'drakx-kbd-mouse-x11', # shared translation @::textdomains; Glib->enable_exceptions2; } init() unless $::no_ugtk_init; Glib->enable_exceptions2 if $::isInstall; 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}; $w->set_padding(@{delete $opts->{padding}}) if exists $opts->{padding}; $w->set_sensitive(delete $opts->{sensitive}) if exists $opts->{sensitive}; $w->signal_connect(expose_event => delete $opts->{expose_event}) if exists $opts->{expose_event}; $w->signal_connect(realize => delete $opts->{realize}) if exists $opts->{realize}; (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 (my $sensitive_ref = delete $opts->{sensitive_ref}) { my $set = sub { $w->set_sensitive($$sensitive_ref) }; gtkval_register($w, $sensitive_ref, $set); $set->(); } if (%$opts && !$opts->{allow_unknown_options}) { internal_error("$action $class: unknown option(s) " . join(', ', keys %$opts)); } $w; } sub _gtk__Install_Button { my ($w, $opts, $_class) = @_; $opts->{child} = gtknew('HBox', spacing => 5, children_tight => [ # FIXME: not RTL compliant (lang::text_direction_rtl() ? ...) gtknew('Image', file => 'advanced_expander'), gtknew('Label', text => delete $opts->{text}), ], ); $opts->{relief} = 'none'; _gtk__Button($w, $opts, 'Button'); } sub _gtk__Button { &_gtk_any_Button } sub _gtk__ToggleButton { &_gtk_any_Button } sub _gtk__CheckButton { &_gtk_any_Button } sub _gtk__RadioButton { &_gtk_any_Button } sub _gtk_any_Button { my ($w, $opts, $class) = @_; if (!$w) { my @radio_options; if ($class eq 'RadioButton') { @radio_options = delete $opts->{group}; } $w = $opts->{child} ? "Gtk2::$class"->new(@radio_options) : delete $opts->{mnemonic} ? "Gtk2::$class"->new_with_mnemonic(@radio_options, delete $opts->{text} || '') : $opts->{text} ? "Gtk2::$class"->new_with_label(@radio_options, delete $opts->{text} || '') : "Gtk2::$class"->new(@radio_options); $w->{format} = delete $opts->{format} if exists $opts->{format}; } 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}); } elsif (exists $opts->{stock}) { $w->set_label(delete $opts->{stock}); $w->set_use_stock(1); } 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 }); add2hash_($opts, { value => $opts->{lower} }) if !exists $opts->{value}; 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->set_digits(delete $opts->{digits}) if exists $opts->{digits}; if (my $value_ref = delete $opts->{value_ref}) { my $set = sub { $w->set_value($$value_ref) }; gtkval_register($w, $value_ref, $set); $set->(); $w->signal_connect(value_changed => sub { gtkval_modify($value_ref, $w->get_value, $set); }); } $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; } 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("cannot 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 = $w->flip(1) if delete $opts->{flip}; } $w; } # Image_using_pixmap is rendered using DITHER_MAX which is much better on 16bpp displays sub _gtk__Image_using_pixmap { &_gtk__Image } # Image_using_pixbuf is rendered using DITHER_MAX & transparency which is much better on 16bpp displays sub _gtk__Image_using_pixbuf { &_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_stock(delete $opts->{stock}, 'button') if exists $opts->{stock}; $w->{options} = { flip => delete $opts->{flip} }; $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); } : $class =~ /using_pixbuf/ ? sub { my ($w, $file) = @_; my $pixbuf = _pixbuf_render_alpha(gtknew('Pixbuf', file => $file, %{$w->{options}}), 255); my ($width, $height) = ($pixbuf->get_width, $pixbuf->get_height); $w->set_size_request($width, $height); $w->{pixbuf} = $pixbuf; $w->signal_connect(expose_event => sub { my (undef, $event) = @_; if (!$w->{x}) { my $alloc = $w->allocation; $w->{x} = $alloc->x; $w->{y} = $alloc->y; } # workaround Gtk+ bug: in installer, first event is not complete and rectables are bogus: if ($::isInstall) { $pixbuf->render_to_drawable($w->window, $w->style->fg_gc('normal'), 0, 0, $w->{x}, $w->{y}, $width, $height, 'max', 0, 0); return; } foreach my $rect ($event->region->get_rectangles) { my @values = $rect->values; $pixbuf->render_to_drawable($w->window, $w->style->fg_gc('normal'), @values[0..1], $w->{x}+$values[0], $w->{y}+$values[1], @values[2..3], 'max', 0, 0); } }); } : sub { my ($w, $file, $o_size) = @_; my $pixbuf = gtknew('Pixbuf', file => $file, if_($o_size, size => $o_size), %{$w->{options}}); $w->set_from_pixbuf($pixbuf); }; } if (my $name = delete $opts->{file}) { my $file = _find_imgfile(may_apply($w->{format}, $name)) or internal_error("cannot 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("cannot 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 if !defined $opts->{line_wrap}; _gtk__Label($w, $opts); } our $left_padding = 20; sub _gtk__Label_Left { my ($w, $opts) = @_; $opts->{alignment} ||= [ 0, 0 ]; $opts->{padding} ||= [ $left_padding, 0 ]; _gtk__WrappedLabel($w, $opts); } sub _gtk__Label_Right { my ($w, $opts) = @_; $opts->{alignment} ||= [ 1, 0.5 ]; _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_selectable(delete $opts->{selectable}) if exists $opts->{selectable}; $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 _gtk__Alignment { my ($w, $_opts) = @_; if (!$w) { $w = Gtk2::Alignment->new(0, 0, 0, 0); } $w; } sub title1_to_markup { my ($label) = @_; if ($::isInstall) { my $font = lang::l2pango_font($::o->{locale}{lang}); if (my ($font_size) = $font =~ /(\d+)/) { $font_size++; $font =~ s/\d+/$font_size/; } qq(<span foreground="#5A8AD6" font="$font">$label</span>); } else { qq(<b><big>$label</big></b>); } } sub _gtk__Install_Title { my ($w, $opts) = @_; local $opts->{widget_name} = 'Banner'; $opts->{text} = uc($opts->{text}) if $::isInstall; gtknew('HBox', widget_name => 'Banner', children => [ 0, gtknew('Label', padding => [ 6, 0 ]), 1, gtknew('VBox', widget_name => 'Banner', children_tight => [ _gtk__Title2($w, $opts), if_($::isInstall, Gtk2::HSeparator->new), ]), 0, gtknew('Label', padding => [ 6, 0 ]), ]); } sub _gtk__Title1 { my ($w, $opts) = @_; $opts ||= {}; $opts->{text_markup} = title1_to_markup(delete($opts->{label})) if $opts->{label}; _gtk__WrappedLabel($w, $opts); } sub _gtk__Title2 { my ($w, $opts) = @_; $opts ||= {}; $opts->{alignment} = [ 0, 0 ]; _gtk__Title1($w, $opts); } sub _gtk__Sexy_IconEntry { my ($w, $opts) = @_; require Gtk2::Sexy; if (!$w) { $w = Gtk2::Sexy::IconEntry->new; $w->set_editable(delete $opts->{editable}) if exists $opts->{editable}; } $w->add_clear_button if delete $opts->{clear_button}; if (my $icon = delete $opts->{primary_icon}) { $w->set_icon('primary', $icon); $w->set_icon_highlight('primary', $icon); } if (my $icon = delete $opts->{secondary_icon}) { $w->set_icon('secondary', $icon); $w->set_icon_highlight('secondary', $icon); } $w->signal_connect('icon-released' => delete $opts->{'icon-released'}) if exists $opts->{'icon-released'}; $w->signal_connect('icon-pressed' => delete $opts->{'icon-pressed'}) if exists $opts->{'icon-pressed'}; _gtk__Entry($w, $opts); } sub _gtk__Entry { my ($w, $opts) = @_; if (!$w) { $w = Gtk2::Entry->new; $w->set_editable(delete $opts->{editable}) if exists $opts->{editable}; } if (my $icon = delete $opts->{primary_icon}) { $w->set_icon_from_stock('primary', $icon); #$w->set_icon_highlight('primary', $icon); } if (my $icon = delete $opts->{secondary_icon}) { $w->set_icon_from_stock('secondary', $icon); #$w->set_icon_highlight('secondary', $icon); } $w->signal_connect('icon-release' => delete $opts->{'icon-release'}) if exists $opts->{'icon-release'}; $w->signal_connect('icon-press' => delete $opts->{'icon-press'}) if exists $opts->{'icon-press'}; $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->signal_connect(changed => sub { gtkval_modify($text_ref, $w->get_text, $set); }); } $w; } sub _gtk__WeaknessCheckEntry { my ($w, $opts) = @_; if (!$w) { $w = _gtk__Entry($w, $opts); } $w->signal_connect('changed' => sub { require authentication; my $password_weakness = authentication::compute_password_weakness($w->get_text); $w->set_icon_from_pixbuf('GTK_ENTRY_ICON_SECONDARY', _get_weakness_icon($password_weakness)); $w->set_icon_tooltip_text('GTK_ENTRY_ICON_SECONDARY', _get_weakness_tooltip($password_weakness)); }); $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__WebKit_View { my ($w, $opts, $_class, $_action) = @_; if (!$w) { $w = Gtk2::WebKit::WebView->new; } # disable contextual menu: if (delete $opts->{no_popup_menu}) { $w->signal_connect('populate-popup' => sub { my (undef, $menu) = @_; $menu->destroy if $menu; 1; }); } $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 Gtk2::WebKit::WebView))) { $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->get_left_margin <= 6; $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 (!delete $opts->{no_shadow} && $action eq 'gtknew' && ref($child) =~ /Gtk2::(Html2|SimpleList|TextView|TreeView|WebKit::WebView)/) { $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__MDV_Notebook { my ($w, $opts, $_class, $_action) = @_; if (!$w) { import_style_ressources(); my ($layout, $selection_arrow, $selection_bar); my $parent_window = delete $opts->{parent_window} || root_window(); my $root_height = first($parent_window->get_size); my $suffix = $root_height == 800 && !$::isStandalone ? '_600' : '_768'; # the white square is a little bit above the actual left sidepanel: my $offset = 20; my $is_flip_needed = text_direction_rtl(); my $filler = gtknew('Image', file => 'left-background-filler.png'); my $filler_height = $filler->get_pixbuf->get_height; my $left_background = gtknew('Image_using_pixbuf', file => 'left-background.png'); my $lf_height = $left_background->{pixbuf}->get_height; my @right_background = $::isInstall ? gtknew('Image', file => "right-white-background_left_part$suffix", flip => $is_flip_needed) : map { gtknew('Image', file => "right-white-background_left_part-$_", flip => $is_flip_needed); } 1, 2, 2, 3; my $width1 = $left_background->{pixbuf}->get_width; my $total_width = $width1 + $right_background[0]->get_pixbuf->get_width; my $arrow_x = text_direction_rtl() ? $offset/2 - 4 : $width1 - $offset - 3; $w = gtknew('HBox', spacing => 0, children => [ 0, $layout = gtknew('Layout', width => $total_width - $offset, children => [ #Layout Fixed # stacking order is important for "Z-buffer": [ $left_background, 0, 0 ], if_($suffix ne '_600', [ $filler, 0, $lf_height ], [ gtknew('Image', file => 'left-background-filler.png'), 0, $lf_height + $filler_height ], [ gtknew('Image', file => 'left-background-filler.png'), 0, $lf_height + $filler_height*2 ], ), [ $selection_bar = gtknew('Image', file => 'rollover.png'), 0, 0 ], # arbitrary vertical position ($opts->{children} ? @{ delete $opts->{children} } : ()), [ my $box = gtknew('VBox', spacing => 0, height => -1, children => [ 0, $right_background[0], if_(!$::isInstall, 1, $right_background[1], 1, $right_background[2], # enought up to to XYZx1280 resolution 0, $right_background[3], ), ]), (text_direction_rtl() ? 0 : $width1 - $offset), 0 ], # stack on top (vertical position is arbitrary): [ $selection_arrow = gtknew('Image', file => 'steps_on', flip => $is_flip_needed), $arrow_x, 0, ], ]), 1, delete $opts->{right_child} || gtknew('Image_using_pixbuf', file => "right-white-background_right_part$suffix", flip => $is_flip_needed), ]); $w->signal_connect('size-allocate' => sub { my (undef, $requisition) = @_; state $width ||= $right_background[0]->get_pixbuf->get_width; $box->set_size_request($width, $requisition->height); }); $_->set_property('no-show-all', 1) foreach $selection_bar, $selection_arrow; bless($w, 'Gtk2::MDV_Notebook'); add2hash($w, { arrow_x => $arrow_x, layout => $layout, selection_arrow => $selection_arrow, selection_bar =>$selection_bar, }); } $w; } sub _gtk__Fixed { my ($w, $opts, $_class, $_action) = @_; if (!$w) { $w = Gtk2::Fixed->new; $w->set_has_window(delete $opts->{has_window}) if exists $opts->{has_window}; _gtknew_handle_layout_children($w, $opts); } $w; } sub _gtk__Layout { my ($w, $opts, $_class, $_action) = @_; if (!$w) { $w = Gtk2::Layout->new; _gtknew_handle_layout_children($w, $opts); } $w; } sub _gtknew_handle_layout_children { my ($w, $opts) = @_; $opts->{children} ||= []; push @{$opts->{children}}, [ delete $opts->{child}, delete $opts->{x}, delete $opts->{y} ] if exists $opts->{child}; foreach (@{$opts->{children}}) { $w->put(@$_); } delete $opts->{children}; if ($opts->{pixbuf_file}) { my $pixbuf = if_($opts->{pixbuf_file}, gtknew('Pixbuf', file => delete $opts->{pixbuf_file})); $w->signal_connect( realize => sub { ugtk2::set_back_pixbuf($w, $pixbuf); }); } } 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("cannot create a Plug without a socket_id"); $w = "Gtk2::$class"->new(delete $opts->{socket_id}); } elsif ($class eq 'FileChooserDialog') { my $action = delete $opts->{action} || internal_error("missing action for FileChooser"); $w = Gtk2::FileChooserDialog->new(delete $opts->{title}, delete $opts->{transient_for} || $::main_window, $action, N("Cancel") => 'cancel', delete $opts->{button1} || N("Ok") => 'ok', ); } else { $w = "Gtk2::$class"->new; } if ($::isInstall || $::set_dialog_hint) { $w->set_type_hint('dialog'); # for matchbox window manager } $w->set_modal(delete $opts->{modal}) if exists $opts->{modal}; $opts->{transient_for} ||= $::main_window if $::main_window; $w->set_modal(1) if exists $opts->{transient_for}; $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("cannot find $name"); } } } $w->set_title(delete $opts->{title}) if exists $opts->{title}; if (my $child = delete $opts->{child}) { $w->add($child); $child->show; } $w; } my $previous_popped_and_reuse_window; sub destroy_previous_popped_and_reuse_window() { $previous_popped_and_reuse_window or return; $previous_popped_and_reuse_window->destroy; $previous_popped_and_reuse_window = undef; } 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 # FIXME!!! any { !$_->isa('Gtk2::DrawingArea') && $_->visible } $::WizardTable->get_children; }; my $pop_and_reuse = delete $opts->{pop_and_reuse} && $pop_it; 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_and_reuse) { destroy_previous_popped_and_reuse_window(); } if ($previous_popped_and_reuse_window && $pop_and_reuse) { $w = $previous_popped_and_reuse_window; $w->remove($w->child); gtkadd($w, child => $sub_child); %$opts = (); } elsif ($pop_it) { $opts->{child} = $sub_child; $w = _create_Window($opts, ''); $previous_popped_and_reuse_window = $w if $pop_and_reuse; } 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 => $::WizardTable, }); $::WizardWindow = _create_Window($opts, 'special_center'); } } 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, pop_and_reuse => $pop_and_reuse, 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__FileChooserDialog { &_gtk_any_Window } 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}; $w->set_position(delete $opts->{position}) if exists $opts->{position}; } 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; $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_border_width(delete $opts->{border_width}) if exists $opts->{border_width}; $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($_); $w->attach($_, $j, $j + 1, $i, $i + 1, $j != $#$l && !$w->{mcc} ? ('fill', 'fill', $w->{xpadding}, $w->{ypadding}) : (['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) = @$_; member($fill, qw(0 1 fill 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') : member($meth, qw(destroy 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); } my $enable_quit_popup; sub enable_quit_popup { my ($bool) = @_; $enable_quit_popup = $bool; } state $in_callback; sub quit_popup() { return if !$enable_quit_popup; if (!$in_callback) { $in_callback = 1; my $_guard = before_leaving { undef $in_callback }; require ugtk2; my $w = ugtk2->new(N("Confirmation"), grab => 1); ugtk2::_ask_okcancel($w, N("Are you sure you want to quit?"), N("Quit"), N("Cancel")); my $ret = ugtk2::main($w); return 1 if !$ret; } } sub quit_callback { my ($w) = @_; return 1 if quit_popup(); if ($::isWizard) { $w->destroy; die 'wizcancel'; } else { if (Gtk2->main_level) { Gtk2->main_quit; } else { # block window deletion if not in main loop (eg: while starting the GUI) return 1; } } } sub _create_Window { my ($opts, $special_center) = @_; 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 => $special_center ? 'none' : $no_Window_Manager ? 'center-always' : 'center-on-parent', if_($::isInstall, position => [ $::stepswidth + ($::o->{windowwidth} - $::real_windowwidth) / 2, ($::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 => \&quit_callback); if ($::isInstall && !$::isStandalone) { 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-* really are window manager hints for centering, whereas we want #- to center the main window in the right part of the screen my ($wi, $he); $w->signal_connect(size_allocate => sub { my (undef, $event) = @_; my @w_size = $event->values; # ignore bogus sizing events: return if $w_size[2] < 5; 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, ($::o->{windowheight} - $he) / 2)); }) if $special_center; } $w->present if $no_Window_Manager; $w; } sub _find_imgfile { my ($name) = @_; if ($name =~ m|/| && -f $name) { $name; } else { foreach my $path (_icon_paths()) { foreach ('', '.png', '.xpm', '.jpg') { 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') { if (!$opts{append}) { $buffer->set_text(''); $textview->{anchors} = []; } foreach my $token (@$t) { my ($item, $tag) = @$token; my $iter1 = $buffer->get_end_iter; if (ref($item) =~ /^Gtk2::Gdk::Pixbuf/) { $buffer->insert_pixbuf($iter1, $item); next; } if (ref($item) =~ /^Gtk2::/) { my $anchor = $buffer->create_child_anchor($iter1); $textview->add_child_at_anchor($item, $anchor); $textview->{anchors} ||= []; push @{$textview->{anchors}}, $anchor; 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 { if ($opts{append}) { $buffer->insert($buffer->get_end_iter, $t); } else { $textview->{anchors} = []; $buffer->set_text($t); } } $textview->{to_bottom}->() if $textview->{to_bottom}; #- the following line is needed to move the cursor to the beginning, so that if the #- textview has a scrollbar, it will not 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 _allow_scroll_TextView_to_bottom { my ($scrolledWindow, $textView) = @_; $textView->get_buffer->create_mark('end', $textView->get_buffer->get_end_iter, 0); sub { my ($o_force) = @_; my $adjustment = $scrolledWindow->get_vadjustment; if ($o_force || $adjustment->page_size + $adjustment->value == $adjustment->upper) { flush(); #- one must flush before scrolling to end, otherwise the text just added *may* not be taken into account correctly, and so it doesn't really scroll to end $textView->scroll_to_mark($textView->get_buffer->get_mark('end'), 0, 1, 0, 1); } }; } sub asteriskize { my ($label) = @_; "\x{2022} " . $label; } sub get_main_window_size() { $::real_windowwidth ? ($::real_windowwidth, $::real_windowheight) : $::isWizard ? (540, 360) : (600, 400); } # in order to workaround infamous 6 years old gnome bug #101968: sub get_label_width() { first(mygtk2::get_main_window_size()) - 55 - $left_padding; } sub set_main_window_size { my ($window) = @_; my ($width, $height) = get_main_window_size(); $window->set_size_request($width, $height); } 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", 'data/icons', 'data/pixmaps', 'standalone/icons', '/usr/share/rpmdrake/icons'); } sub main { my ($window, $o_verif) = @_; my $destroyed; $window->signal_connect(destroy => sub { $destroyed = 1 }); $window->show; do { Gtk2->main } while (!$destroyed && $o_verif && !$o_verif->()); may_destroy($window); flush(); } sub sync { my ($window) = @_; $window->show; flush(); } sub flush() { Gtk2->main_iteration while Gtk2->events_pending; } sub enable_sync_flush { my ($w) = @_; $w->signal_connect(expose_event => sub { $w->{displayed} = 1; 0 }); } sub sync_flush { my ($w) = @_; # hackish :-( mygtk2::sync($w) while !$w->{displayed}; } sub register_main_window { my ($w) = @_; push @::main_windows, $::main_window = $w; } sub may_destroy { my ($w) = @_; return if !$w; @::main_windows = difference2(\@::main_windows, [ $w->{real_window} ]); if ($::main_window eq $w->{real_window}) { undef $::main_window; $::main_window = $::main_windows[-1]; } $w->destroy; } sub root_window() { state $root; $root ||= Gtk2::Gdk->get_default_root_window; } sub rgb2color { my ($r, $g, $b) = @_; my $color = Gtk2::Gdk::Color->new($r, $g, $b); root_window()->get_colormap->rgb_find_color($color); $color; } sub set_root_window_background {