package GCSplash;

###################################################
#
#  Copyright 2005 Tian
#
#  This file is part of GCfilms.
#
#  GCfilms 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 of the License, or
#  (at your option) any later version.
#
#  GCfilms 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 GCfilms; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###################################################

use strict;
use Gtk2;

{
    package GCSplashWindow;
    use base "Gtk2::Window";
    
    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new('popup');
        bless ($self, $class);

        $self->{parent} = shift;
        $self->{version} = shift;

        $self->{splashImage} = $self->{parent}->{logosDir}.'splash.png';

        $self->set_decorated(0);
        $self->set_resizable(0);
        $self->set_destroy_with_parent(1);
        $self->set_position('center-always');
        
        $self->{label} = new Gtk2::Label('test');

        $self->{vbox} = new Gtk2::VBox(0,0);        
		
		$self->{progress} = new Gtk2::ProgressBar;
		$self->{progress}->set_size_request(240,-1);

        my $color_fg = Gtk2::Gdk::Color->parse('#1c86ee');
        my $color_progress_bg = Gtk2::Gdk::Color->parse('#ffffff');
        my $color_progress_fg = Gtk2::Gdk::Color->parse('#1c86ee');

        $self->{label}->modify_fg('normal', $color_fg);
        $self->{progress}->modify_bg('normal', $color_progress_bg);
        $self->{progress}->modify_bg('active', $color_progress_fg);
        $self->{progress}->modify_bg('prelight', $color_progress_fg);

        my $eventbox = Gtk2::EventBox->new();
        my $img = Gtk2::Image->new_from_pixmap (undef, undef);
        my $inbox = new Gtk2::Fixed;
        $eventbox->add($inbox);
        $inbox->put($img,0,0);
        $inbox->put($self->{label},0,120);
        $inbox->put($self->{progress},30,150);
	    $self->{vbox}->pack_start($eventbox,0,0,0);
	    
        $self->add($self->{vbox});
	    my $drawing_area = Gtk2::DrawingArea->new;
        $self->{vbox}->pack_start($drawing_area,0,0,0);
	    $drawing_area->realize;
		
	    my ($pango_w, $pango_h) = (300,200);
        my $pixmap = Gtk2::Gdk::Pixmap->new ($drawing_area->window,
                $pango_w,
                $pango_h,
                -1);

        my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($self->{splashImage});
		my ($pm, $m) = $pixbuf->render_pixmap_and_mask(255);
    	$img->set_from_pixmap($pm, $m);
    	$self->shape_combine_mask ($m, 0, 0) if $m;
        $self->set_position('center-always');
        $self->{vbox}->show_all;
        $self->show;
        $self->{label}->set_size_request(300,-1);
        
        return $self;
    }

    sub init
    {
        my $self = shift;

        if ($self->{phase} == 0)
        {
            $self->{label}->set_markup('<b>'.$self->{parent}->{lang}->{SplashInit}.'</b>');
            $self->{phase} = 1;
        }
        elsif ($self->{phase} == 1)
        {
            $self->setProgress(0.0);
            $self->{parent}->init($self);
            $self->setProgress(0.1);
            $self->{phase} = 2;
            $self->{label}->set_markup('<b>'.$self->{parent}->{lang}->{SplashLoad}.'</b>');
        }
        elsif ($self->{phase} == 2)
        {
            $self->{parent}->loadPrevious($self);
            $self->setProgress(1.0);
            $self->{parent}->{initializing} = 0;
            $self->{parent}->set_sensitive(1);
            $self->{phase} = 3;
            $self->{label}->set_markup('<b>'.$self->{parent}->{lang}->{SplashDone}.'</b>');
        }
        else
        {
            $self->{parent}->initEnd;
            Glib::Timeout->add(700 ,\&destroyMe, $self);
            return 0;
        }
        
        Glib::Timeout->add(100 ,\&init, $self);           

        return 0;
    }

    sub setMoviesTotal
    {
        my ($self, $total) = @_;
        $self->{step} = GCUtils::round($total / 7);
        $self->{step} = 1 if $self->{step} < 1;
        $self->{total} = $total;
    }

    sub setProgressForMoviesLoad
    {
        my ($self, $current) = @_;
        if (! $self->{total})
        {
            $self->{progress}->set_fraction(0.2);
        }
        else
        {
            return if ($current % $self->{step});
            $self->{progress}->set_fraction(0.1 + (($current / $self->{total}) * 0.3));
        }
        Gtk2->main_iteration while (Gtk2->events_pending);
    }

    sub setProgressForMoviesDisplay
    {
        my ($self, $current) = @_;
        if (! $self->{total})
        {
            $self->{progress}->set_fraction(0.6);
        }
        else
        {
            return if ($current % $self->{step});
            $self->{progress}->set_fraction(0.4 + (($current / $self->{total}) * 0.5));
        }
        Gtk2->main_iteration while (Gtk2->events_pending);
    }

    sub setProgress
    {
        my ($self, $current) = @_;
        $self->{progress}->set_fraction($current);
        Gtk2->main_iteration while (Gtk2->events_pending);
    }

    sub destroyMe
    {
        my $self = shift;
        
        $self->destroy;
        return 0;
    }

    sub show
    {
        my $self = shift;

        $self->SUPER::show_all;
        $self->{phase} = 0;
        Glib::Timeout->add(10 ,\&init, $self);
    }
}

1;
