/*
  Cloned Xunzip
  Copyright (C) 1999 Tero Koskinen

  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
  (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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
*/

#include <stdio.h>
#include <config.h>

#include <gnome.h>

#include <string>

#include "app.hh"

#include "callbacks.hh"
#include "prepareapp.hh"

#include "menu.hh"
#include "toolbar.hh"

void prepare_app(const char *argv0,App *app)
{
  // string title;
  gchar *title;

  if (!app)
    {
      printf("Creating a new application, because the last one was null\n");
      app=new App();
    }

  if (app->readConfig)
    app->read_config();

  app->app=gnome_app_new((char*)argv0,PACKAGE);

  gtk_signal_connect(GTK_OBJECT(app->app),"delete_event",
		     GTK_SIGNAL_FUNC(de_app_quit),app);

  title=g_strdup_printf(_("Cloned Xunzip %s"),VERSION);

  gtk_window_set_title(GTK_WINDOW(app->app),title);

  g_free(title);

  gtk_widget_set_usize(app->app,app->width,app->height);
  gtk_window_set_policy(GTK_WINDOW(app->app), TRUE, TRUE, FALSE);

  app->list=create_clist();

  gtk_signal_connect(
	  GTK_OBJECT(app->list),"select_row",
	  GTK_SIGNAL_FUNC(select_list_row_callback),app);
  
  gtk_signal_connect(
	  GTK_OBJECT(app->list),"unselect_row",
	  GTK_SIGNAL_FUNC(unselect_list_row_callback),app);
  
  connect_dnd(app->list,app);
  
  GtkWidget *sw;
  sw = gtk_scrolled_window_new(NULL,NULL);
  gtk_container_add (GTK_CONTAINER (sw), app->list);
  gtk_scrolled_window_set_policy (
	  GTK_SCROLLED_WINDOW (sw),
	  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  gnome_app_set_contents(GNOME_APP(app->app),sw);

  filemenu[0].user_data=(gpointer)app;
  filemenu[2].user_data=(gpointer)app;
  filemenu[4].user_data=(gpointer)app;
  toolbar[0].user_data=(gpointer)app;
  toolbar[2].user_data=(gpointer)app;
  toolbar[3].user_data=(gpointer)app;
  toolbar[4].user_data=(gpointer)app;
  toolbar[6].user_data=(gpointer)app;
  actionsmenu[0].user_data=(gpointer)app;
  actionsmenu[1].user_data=(gpointer)app;
  actionsmenu[2].user_data=(gpointer)app;

  gnome_app_create_menus(GNOME_APP(app->app),mainmenu);
  gnome_app_create_toolbar(GNOME_APP(app->app), toolbar);
  gtk_menu_item_right_justify(GTK_MENU_ITEM(mainmenu[HELP_MENU].widget));

  app->bar=gnome_appbar_new(TRUE, TRUE,GNOME_PREFERENCES_USER);
  gnome_app_set_statusbar(GNOME_APP(app->app),app->bar);

  gtk_widget_show_all(app->app);
}

void connect_dnd(GtkWidget *w,App *app)
{
  static GtkTargetEntry drop_types[] = {
	  { "text/plain",0,TARGET_TEXT_PLAIN },
	  { "text/uri-list",0,TARGET_URI_LIST },
  };
  static GtkTargetEntry drag_types[] = {
	  { "text/plain",0,TARGET_TEXT_PLAIN },
	  { "_NETSCAPE_URL",0,TARGET_URI_LIST },
  };

  gtk_signal_connect (GTK_OBJECT (w), "drag_data_received",
                      GTK_SIGNAL_FUNC(dnd_drop_callback), app);

  gtk_signal_connect (GTK_OBJECT(w),"drag_data_get",
		      GTK_SIGNAL_FUNC(dnd_drag_callback),app);

  gtk_drag_dest_set (w,(GtkDestDefaults)(
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_DROP),
                     drop_types, sizeof(drop_types) / sizeof(drop_types[0]),
                     GDK_ACTION_COPY);

  gtk_drag_source_set(w,GDK_BUTTON1_MASK,drag_types,
		      sizeof(drag_types) / sizeof(drag_types[0]),
		      GDK_ACTION_COPY);
}

GtkWidget *create_clist(void)
{
  GtkWidget *w;
  char *titles[5]=
  {
    N_("Name"),
    N_("Date"),
    N_("Ratio"),
    N_("Size"),
    N_("Packed")
  };

  w=gtk_clist_new_with_titles(5,titles);
  if (!w)
    {
      fprintf(stderr,"Cannot create list!\n");
      return NULL;
    }
	
  gtk_clist_set_selection_mode(GTK_CLIST(w), GTK_SELECTION_SINGLE);

  gtk_clist_set_column_width(GTK_CLIST(w),0,90);
  gtk_clist_set_column_width(GTK_CLIST(w),1,70);
  gtk_clist_set_column_width(GTK_CLIST(w),2,30);
  gtk_clist_set_column_width(GTK_CLIST(w),3,70);
  gtk_clist_set_column_width(GTK_CLIST(w),4,70);
  gtk_clist_column_titles_passive(GTK_CLIST(w));

  return w;
}
