# -*- Perl -*-
#
# This is part of X-RUST-1.0 Beta 4.
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah, Department of Computer Scinece
#
#                    Shawn T. Amundson <amundson@cs.umn.edu> and the
#                    University of Minnesota Computer Science Department
#
# Abstract: This file contains definitions and subroutines which deal with
#           the request part of X-Rust.
#
# 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 of the License, or
# 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 the source distribution of Inquire, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.
#

#
# $Id: xrust_request.pl,v 1.1.1.1 2000/05/03 19:54:39 wright Exp $
#

use strict;
use vars qw(%REQUESTS %STATIC_REQUEST $caller $AMW %FONTS);

sub create_request_window {
    #
    # Abstract: Create the "request display" window if it 
    #           isn't already open.
    #

    my $queue_name  = shift;
    my $request_num = shift;

    #
    # Check to see if the window already exists.  If it does, send it to the top.
    #
    if (&request_element_exists($queue_name,$request_num,'_window')) {
	$REQUESTS{$queue_name}->{$request_num}->{'_window'}->raise;
	return;
    }

    #
    # Create the window.
    #
    $REQUESTS{$queue_name}->{$request_num}->{'_window'} = $MW->Toplevel;
    $REQUESTS{$queue_name}->{$request_num}->{'_window'}->bind('<Destroy>', 
		  sub {
		      undef $REQUESTS{$queue_name}->{$request_num}->{'_window'};
		  });
    $REQUESTS{$queue_name}->{$request_num}->{'_window'}->title("Request $queue_name #$request_num"); 


    #
    # Fill the window with the appropriate widgets.
    #
    my $frame = &create_request_view($REQUESTS{$queue_name}->{$request_num}->{'_window'},
				     $queue_name,
				     $request_num);
    $frame->pack;

}

sub create_static_request {
    #
    # Abstract:
    #    For lack of a better term, the piece of the main window
    #    which contains a single request at a time, I call it the
    #    "static request" area.  This subroutine creates this area.
    #

    my $topframe = shift;

    my $frame = $topframe->Frame;

    my $button_width = 16;
    my @button_pack = (
		       -side => 'top'
		       );

    my @menubutton_pack = (
			   -side => 'left',
			   -fill => 'x',
			   -expand => 1
			  );

    my @menu_frame_pack = (
			   -side => 'top', 
			   -fill => 'x',
			  );

    my $label_width = 8;

    my $action = $frame->Frame;
    $action->pack(
		  -side => 'right',
		  -fill => 'y'
		 ); 

    my $notice_text_frame = $action->Frame;
    $notice_text_frame->pack(@menu_frame_pack);

    my $notice_text = $notice_text_frame->Label(
						-text => 'Ticket Properties',
						-font => $FONTS{'BUTTON_FONT'},
						-anchor => 'sw'
						);

    $notice_text->pack(
		       -side => 'top'
		       );

    my $notice_text1 = $notice_text_frame->Label(
						-text => 'For Ticket Displayed',
						-font => $FONTS{'BUTTON_FONT'},
						);

    $notice_text1->pack(
		       -side => 'top'
		       );

    my $notice_text2 = $notice_text_frame->Label(
						-text => 'In Window At Left',
						-font => $FONTS{'BUTTON_FONT'},
						);

    $notice_text2->pack(
		       -side => 'bottom'
		       );



    # **** Queue ****

    my $queue_frame = $action->Frame;
    $queue_frame->pack(@menu_frame_pack);

    my $queue_label = $queue_frame->Label(
					  -text => 'Queue',
					  -font => $FONTS{'BUTTON_FONT'},
					  -width => $label_width,
					  -anchor => 'sw'
					  );
    $queue_label->pack(
			-side => 'left'
		       );
    my $action_queue = $queue_frame->Menubutton(
						-text => '...',
						-relief => 'raised'
						);
    $action_queue->pack(@menubutton_pack);
    my $action_queue_menu = $action_queue->Menu(-tearoff => 0);
    $action_queue->configure(
			     -menu => $action_queue_menu,
			     -state => 'disabled',
			     -font  => $FONTS{'BUTTON_FONT'}
			     );

    my $each;
    foreach $each (split(':',$globals->{"alias_name"})) {
	$action_queue->radiobutton(
				    -label     => "$each",
				    -value     => $each
				   );
    }

    # **** Status ****

    my $status_frame = $action->Frame;
    $status_frame->pack(@menu_frame_pack);

    my $status_label = $status_frame->Label(
					    -text => 'Status',
					    -font => $FONTS{'BUTTON_FONT'},
					    -width => $label_width,
					    -anchor => 'sw'
					   );
    $status_label->pack(
			-side => 'left'
		       );
    my $action_status = $status_frame->Menubutton(
						  -text => '...',
						  -relief => 'raised'
					   );
    $action_status->pack(@menubutton_pack);
    my $action_status_menu = $action_status->Menu(-tearoff => 0);
    $action_status->configure(
			      -menu => $action_status_menu,
			      -state => 'disabled',
			      -font  => $FONTS{'BUTTON_FONT'}
			     );

    my $each;
    foreach $each (split(':',$globals->{"valid_status"})) {
	$action_status->radiobutton(
				    -label     => "$each",
				    -value     => $each
				   );
    }

    # **** Priority ****
    my $priority_frame = $action->Frame;
    $priority_frame->pack(@menu_frame_pack);

    my $priority_label = $priority_frame->Label(
						-text => 'Priority',
						-font => $FONTS{'BUTTON_FONT'},
						-width => $label_width,
						-anchor => 'sw'
						);
    $priority_label->pack(
			-side => 'left'
		       );
    my $action_priority = $priority_frame->Menubutton(
						      -text => '...',
						      -relief => 'raised'
						      );
    $action_priority->pack(@menubutton_pack);
    my $action_priority_menu = $action_priority->Menu(-tearoff => 0);
    $action_priority->configure(
				-menu => $action_priority_menu,
				-state => 'disabled',
				-font => $FONTS{'BUTTON_FONT'}
			     );

    my $radial;
    foreach $each ("high","normal","low") {
	$radial = $action_priority->radiobutton(
						-label => "$each",
						-value => $each
					       );
    }

    # **** Category ****
    my $category_frame = $action->Frame;
    $category_frame->pack(@menu_frame_pack);

    my $category_label = $category_frame->Label(
						-text   => 'Category',
						-font   => $FONTS{'BUTTON_FONT'},
						-width  => $label_width,
						-anchor => 'sw'
						);
    $category_label->pack(
			-side => 'left'
		       );
    my $action_category = $category_frame->Menubutton(
						      -text   => '...',
						      -relief => 'raised'
						      );
    $action_category->pack(@menubutton_pack);
    my $action_category_menu = $action_category->Menu(-tearoff => 0);
    $action_category->configure(
				-menu => $action_category_menu,
				-state => 'disabled',
				-font  => $FONTS{'BUTTON_FONT'}
			     );
    foreach $each ("user","work") {
	$radial = $action_category->radiobutton(
						-label => "$each",
						-value => $each
					       );
    }


    # **** Access ****
    my $access_frame = $action->Frame;
    $access_frame->pack(@menu_frame_pack);

    my $access_label = $access_frame->Label(
						-text   => 'Access',
						-font   => $FONTS{'BUTTON_FONT'},
						-width  => $label_width,
						-anchor => 'sw'
						);
    $access_label->pack(
			-side => 'left'
		       );
    my $action_access = $access_frame->Menubutton(
						      -text   => '...',
						      -relief => 'raised'
						      );
    $action_access->pack(@menubutton_pack);
    my $action_access_menu = $action_access->Menu(-tearoff => 0);
    $action_access->configure(
				-menu => $action_access_menu,
				-state => 'disabled',
				-font  => $FONTS{'BUTTON_FONT'}
			     );
    foreach $each ("public","private") {
	$radial = $action_access->radiobutton(
						-label => "$each",
						-value => $each
					       );
    }

    ### Other buttons ###

    my $action_eta = $action->Button(
				     -text    => "Set ETA",
				     -font    => $FONTS{'BUTTON_FONT'},
				     -width   => $button_width,
				     -state   => 'disabled'
				     );
    $action_eta->pack(@button_pack);

    my $action_user = $action->Button(
				      -text    => "Change User",
				      -font    => $FONTS{'BUTTON_FONT'},
				      -width   => $button_width,
				      -state   => 'disabled'
				      );
    $action_user->pack(@button_pack);

    my $action_subject = $action->Button(
					 -text    => "Change Subject",
					 -font    => $FONTS{'BUTTON_FONT'},
					 -width   => $button_width,
					 -state   => 'disabled'
					);
    $action_subject->pack(@button_pack);

    my $action_comment = $action->Button(
					 -text    => "Add Comments",
					 -font    => $FONTS{'BUTTON_FONT'},
					 -width   => $button_width,
					 -state   => 'disabled'
					);
    $action_comment->pack(@button_pack);

    my $action_top = $topframe->Frame;
    $action_top->pack(
		  -side => 'bottom',
		  -fill => 'y'
		 ); 

    @button_pack = (
		    -side => 'right'
		    );

    my $action_mail = $action_top->Button(
				      -text  => "Mail",
				      -font  => $FONTS{'BUTTON_FONT'},
				      -width => $button_width,
				      -state => 'disabled'
				     );
    $action_mail->pack(@button_pack);

    my $action_resolve = $action_top->Button(
					 -text    => "Resolve",
					 -font    => $FONTS{'BUTTON_FONT'},
					 -width   => $button_width,
					 -state   => 'disabled',
					);
    $action_resolve->pack(@button_pack);

    my $action_take = $action_top->Button(
				      -text  => "Take",
				      -font  => $FONTS{'BUTTON_FONT'},
				      -width => $button_width,
				      -state => 'disabled'
				     );
    $action_take->pack(@button_pack);

    my $action_assign = $action_top->Button(
					-text  => "Assign",
					-font  => $FONTS{'BUTTON_FONT'},
					-width => $button_width,
					-state => 'disabled'
					);

    $action_assign->pack(@button_pack);

    #-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    # Create a frame to display a request log

    my $request_vert_scroll = $frame->Scrollbar(-orient => 'vertical');
    $request_vert_scroll->pack(-side => 'right', -fill => 'y');
    my $request_horiz_scroll = $frame->Scrollbar(-orient => 'horiz');
	
    my $action_text = $frame->Text(
				   -yscrollcommand =>  [$request_vert_scroll => 'set'],
				   -xscrollcommand =>  [$request_horiz_scroll => 'set'],
				   -width          => 80,
				   -height         => 15, 
				   -font           => $FONTS{'TEXT_FONT'},
				   -wrap           => 'none',
				   -setgrid        => 1,
				   -state          => 'disabled'
				  );
	
    $request_vert_scroll->configure(-command => [$action_text => 'yview']);
    $request_horiz_scroll->configure(-command => [$action_text => 'xview']);
    $action_text->pack(-expand => 1, -fill => 'both');
    $request_horiz_scroll->pack(-side => 'top', -fill => 'x');

    # 
    # We want to configure several widgets later in different sections
    # of the program.  Widgets start with a "_" in the request structure
    # used.
    # 
    $STATIC_REQUEST{'_mail'}     = $action_mail;
    $STATIC_REQUEST{'_take'}     = $action_take;
    $STATIC_REQUEST{'_assign'}   = $action_assign;
    $STATIC_REQUEST{'_status'}   = $action_status;
    $STATIC_REQUEST{'_category'} = $action_category;
    $STATIC_REQUEST{'_access'}   = $action_access;
    $STATIC_REQUEST{'_priority'} = $action_priority;
    $STATIC_REQUEST{'_resolve'}  = $action_resolve;
    $STATIC_REQUEST{'_comment'}  = $action_comment;
    $STATIC_REQUEST{'_queue_name'} = $action_queue;
    $STATIC_REQUEST{'_text'}     = $action_text;
    $STATIC_REQUEST{'_eta'}      = $action_eta;

    return $frame;
	

}


sub create_request_view {
    #
    # Abstract:
    #     This creates the widgets used in each request window.
    #

    my $topframe    = shift;
    my $queue_name  = shift;
    my $request_num = shift;

    my $frame = $topframe->Frame;

    my $button_width = 14;
    my @button_pack = (
		       -side => 'top'
		       );

    my $action = $frame->Frame;
    $action->pack(
		  -side => 'right',
		  -fill => 'y'
		 ); 

    my $action_mail = $action->Button(
				      -text => "Mail",
				      -font => $FONTS{'BUTTON_FONT'},
				      -width => $button_width,
				      -state => 'disabled'
				     );
    $action_mail->pack(@button_pack);

    my $action_resolve = $action->Button(
					 -text => "Resolve",
					 -font => $FONTS{'BUTTON_FONT'},
					 -width => $button_width,
					 -command => sub {
					     &do_func($queue_name,$request_num,'status','resolved');
					 }
					);
    $action_resolve->pack(@button_pack);

    my $action_take = $action->Button(
				      -text => "Take",
				      -font => $FONTS{'BUTTON_FONT'},
				      -width => $button_width,
				      -state => 'disabled',
				      -command => sub { 
					  if ($REQUESTS{$queue_name}->{$request_num}->{'owner'} ne $caller) {
					      &do_func($queue_name,$request_num,'take');
					  } else {
					      &do_func($queue_name,$request_num,'untake');
					  }
				      }
				     );
    $action_take->pack(@button_pack);

    my $action_assign = $action->Button(
					-text => "Assign",
					-font => $FONTS{'BUTTON_FONT'},
					-width => $button_width,
					-state => 'disabled',
					-command => sub {&assign_func($queue_name, $request_num);
						     }
					);
    $action_assign->pack(@button_pack);

    my @menubutton_pack = (
			   -side => 'top',
			   -fill => 'x',
			   );

    # **** Status ****

    my $action_status = $action->Menubutton(
					    -text => 'Status...',
					    -relief => 'raised'
					   );
    $action_status->pack(@menubutton_pack);
    my $action_status_menu = $action_status->Menu(-tearoff => 0);
    $action_status->configure(
			      -menu => $action_status_menu,
			      -state => 'disabled',
			      -font  => $FONTS{'BUTTON_FONT'}
			     );

    my $each;
    foreach $each (split(':',$globals->{"valid_status"})) {
	$action_status->radiobutton(
				    -label    => "$each",
				    -variable => \$REQUESTS{$queue_name}->{$request_num}->{'status'},
				    -value    => $each,
				    -command  => sub {
					&do_func($queue_name,$request_num,'status',$each);
				    }
				   );
    }

    # **** Priority ****
    my $action_priority = $action->Menubutton(
					      -text => 'Priority...',
					      -relief => 'raised'
					      );
    $action_priority->pack(@menubutton_pack);
    my $action_priority_menu = $action_priority->Menu(-tearoff => 0);
    $action_priority->configure(
			      -menu => $action_priority_menu,
			      -state => 'disabled',
			      -font  => $FONTS{'BUTTON_FONT'}
			     );

    my $radial;
    foreach $each ("high","normal","low") {
	$radial = $action_priority->radiobutton(
						-label     => "$each",
						-variable => \$REQUESTS{$queue_name}->{$request_num}->{'priority'},
						-value    => $each,
						-command  => sub {
						    &do_func($queue_name,$request_num,'priority',$each);
						}
					       );
    }

    # **** Category ****
    my $action_category = $action->Menubutton(
					      -text => 'Category...',
					      -relief => 'raised'
					      );
    $action_category->pack(@menubutton_pack);
    my $action_category_menu = $action_category->Menu(-tearoff => 0);
    $action_category->configure(
				-menu => $action_category_menu,
				-state => 'disabled',
				-font  => $FONTS{'BUTTON_FONT'}
			     );
    foreach $each ("user","work") {
	$radial = $action_category->radiobutton(
						-label     => "$each",
						-variable => \$REQUESTS{$queue_name}->{$request_num}->{'category'},
						-value    => $each,
						-command  => sub {
						    &do_func($queue_name,$request_num,'category',$each);
						}
					       );
    }

 
    my $action_close = $action->Button(
				       -text => "Close",
				       -font => $FONTS{'BUTTON_FONT'},
				       -width => 16,
				       -command => \sub {$topframe->destroy});
    
    $action_close->pack(@menubutton_pack);

    my @button_pack = (
                       -side => 'right'
                       );
 
    my $action_top = $frame->Frame;
    $action->pack(
                  -side => 'top',
                  -fill => 'y'
		  ); 
 
    my $action_mail = $action_top->Button(
                                      -text  => "Mail",
                                      -font  => $FONTS{'BUTTON_FONT'},
                                      -width => $button_width,
                                      -state => 'disabled'
				      );
    $action_mail->pack(@button_pack);
    my $action_resolve = $action_top->Button(
                                         -text    => "Resolve",
                                         -font    => $FONTS{'BUTTON_FONT'},
                                         -width   => $button_width,
                                         -state   => 'disabled',
				      );
    $action_resolve->pack(@button_pack);
 
    my $action_take = $action_top->Button(
                                      -text  => "Take",
                                      -font  => $FONTS{'BUTTON_FONT'},
                                      -width => $button_width,
                                      -state => 'disabled'
				      );
    $action_take->pack(@button_pack);
 
    my $action_assign = $action_top->Button(
                                        -text  => "Assign",
                                        -font  => $FONTS{'BUTTON_FONT'},
                                        -width => $button_width,
                                        -state => 'disabled'
                                        );
 
    $action_assign->pack(@button_pack);


    #-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    # Create a frame to display a request log

    my $request_vert_scroll = $frame->Scrollbar(-orient => 'vertical');
    $request_vert_scroll->pack(-side => 'right', -fill => 'y');
    my $request_horiz_scroll = $frame->Scrollbar(-orient => 'horiz');
	
    my $action_text = $frame->Text(
				   -yscrollcommand =>  [$request_vert_scroll => 'set'],
				   -xscrollcommand =>  [$request_horiz_scroll => 'set'],
				   -width          => 80,
				   -height         => 15, 
				   -font           => $FONTS{'TEXT_FONT'}
				   -wrap           => 'none',
				   -setgrid        => 1,
				   -state          => 'disabled'
				  );
	
    $request_vert_scroll->configure(-command => [$action_text => 'yview']);
    $request_horiz_scroll->configure(-command => [$action_text => 'xview']);
    $action_text->pack(-expand => 1, -fill => 'both');
    $request_horiz_scroll->pack(-side => 'top', -fill => 'x');

    $REQUESTS{$queue_name}->{$request_num}->{'_take'}     = $action_take;
    $REQUESTS{$queue_name}->{$request_num}->{'_assign'}   = $action_assign;
    $REQUESTS{$queue_name}->{$request_num}->{'_status'}   = $action_status;
    $REQUESTS{$queue_name}->{$request_num}->{'_category'} = $action_category;
    $REQUESTS{$queue_name}->{$request_num}->{'_priority'} = $action_priority;
    $REQUESTS{$queue_name}->{$request_num}->{'_text'}     = $action_text;

    return $frame;
	
}



1;

