#!/usr/bin/perl

# Copyright (C) 2006 Mandriva
#                    Olivier Blin <blino@mandriva.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.



use lib qw(/usr/lib/libDrakX);

use standalone;
use common;
use interactive;
use Xconfig::glx;

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

my $glx = Xconfig::glx::detect_may_install($in) or $in->exit;

require any;
my $running_wm = any::running_window_manager();

sub choose_gtk {
    my ($glx) = @_;
    require mygtk2;
    import mygtk2 qw(gtknew);
    require ugtk2;
    import ugtk2 qw(:create :helpers :wrappers :dialogs);

    $ugtk2::wm_icon = 'drak3d-16';
    my $title = N("3D Desktop effects");
    my $w = ugtk2->new($title, no_Window_Manager => !$running_wm);

    $::main_window = $w->{real_window}; #- so that transient_for is defined for wait messages and dialogs

    add_icon_path('/usr/share/mcc/themes/default/');

    my @compiz_servers = qw(AIGLX Xgl);
    my $compiz_choice = find { $glx->{capabilities}{$_} } uniq($glx->{method}, @compiz_servers);
    my $compiz_group;
    my %compiz_radio = map {
        my $server = $_;
        my $radio = gtknew('RadioButton', text => N("Use %s", $server),
                           $compiz_group ? (group => $compiz_group->get_group) : (),
                           sensitive => $glx->{capabilities}{$server},
                           active => $compiz_choice eq $server,
                           toggled => sub {
                               if ($_[0]->get_active) {
                                   $compiz_choice = $glx->{method} = $server;
                               }
                           });
        $compiz_group ||= $radio;
        $server => $radio;
    } @compiz_servers;

    my $compiz_box = gtknew('HBox', padding => 20,
                            sensitive => member($glx->{method}, @compiz_servers),
                            children_tight => [
       gtknew('VBox', spacing => 5, children_tight => [
           $compiz_radio{AIGLX},
           gtknew('WrappedLabel',
                  alignment => [ 0, 0 ],
                  padding => [ 30, 0 ],
                  text => N("AIGLX is directly integrated into the graphical server. If your system supports it, it is the preferred solution.")),
           #gtkset_sensitive(Gtk2::Expander->new(N("Advanced AIGLX options")), 0),
           $compiz_radio{Xgl},
           gtknew('WrappedLabel',
                  alignment => [ 0, 0 ],
                  padding => [ 30, 0 ],
                  text => N("Xgl is an additional graphical server that adds 3D desktop support.")),
           gtknew('Button', text => N("Run %s configuration tool", 'compiz'),
                  sensitive => $glx->{supported}, clicked => sub {
                      require run_program;
                      run_program::raw({ detach => 1 }, 'gset-compiz');
                  }),
       ]),
   ]);

    my $method_group;
    my %methods_radio = map {
        my ($name, $effects, $text, $sensitive) = @$_;
        my $radio = gtknew('RadioButton', text => $text,
                           $method_group ? (group => $method_group->get_group) : (),
                           sensitive => $sensitive,
                           active => do {
                               my $status = $effects->();
                               defined $status && $glx->{method} eq $status;
                           },
                           toggled => sub {
                               if ($_[0]->get_active) {
                                   $compiz_box->set_sensitive($name eq 'compiz');
                                   $glx->{method} = $effects->();
                               }
                           });
        $method_group ||= $radio;
        $name => $radio;
    } [ 'none', sub { '' }, N("No 3D desktop effects"), 1 ],
      [ 'compiz', sub { $compiz_choice }, N("Full 3D desktop effects (compiz)"), $glx->{capabilities}{direct_rendering},  ],
      [ 'minimal', sub { '3d-desktop' }, N("Minimal desktop effects (3D desktop switching only)"), 0 ];

    gtkadd($w->{window},
           gtknew('VBox', spacing => 5, children => [
               $::isEmbedded ? () : (0, Gtk2::Banner->new('drak3d', $title)),
               0, gtknew('Title2', label => N("This tool allows you to configure 3D desktop effects.")),
               if_(!$glx->{supported}, 0, N("Your system does not support 3D desktop effects.")),
               0, gtknew('VBox', spacing => 5, children_tight => [
                   $methods_radio{none},
                   #$methods_radio{minimal},
                   $methods_radio{compiz},
                   gtknew('VBox', spacing => 5, children_tight => [
                       gtknew('WrappedLabel',
                              alignment => [ 0, 0 ],
                              padding => [ 30, 0 ],
                              text => N("Compiz is a window manager that makes use of accelerated graphical servers. It provides 3D desktop and compositing effects in window management, such as translucent windows and moving effects.")),
                       $compiz_box,
                   ]),
               ]),
               1, gtknew('Label'),
               0, $w->create_okcancel(undef, undef, undef, [ N("Help"), sub { run_program::raw({ detach => 1 }, 'drakhelp', '--id', 'drak3d') } ]),
           ]),
       );

    $w->{ok}->set_sensitive($glx->{supported});
    $w->main;
    $w->{retval};
}

my $res;
if ($in->isa('interactive::gtk')) {
    $res = choose_gtk($glx);
} else {
    $res = Xconfig::glx::choose_interactive($in, $glx);
}
if ($res) {
    Xconfig::glx::write($glx);
    any::ask_for_X_restart($in);
}

$in->exit;
