/*
 * Copyright (c) 2003, 2004 Jean-Yves Lefort
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Jean-Yves Lefort nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "sgtk-hig.h"
#include "st-search-dialog.h"
#include "st-stock.h"

/*** type definitions ********************************************************/

struct _STSearchDialogPrivate
{
  GtkWidget	*entry;
};
  
/*** function declarations ***************************************************/

static void st_search_dialog_class_init (STSearchDialogClass *class);
static void st_search_dialog_init (STSearchDialog *dialog);

static void st_search_dialog_update_sensitivity (STSearchDialog *dialog);
static void st_search_dialog_entry_changed_h (GtkEditable *editable,
					      gpointer user_data);

/*** implementation **********************************************************/

GType
st_search_dialog_get_type (void)
{
  static GType search_dialog_type = 0;
  
  if (! search_dialog_type)
    {
      static const GTypeInfo search_dialog_info = {
	sizeof(STSearchDialogClass),
	NULL,
	NULL,
	(GClassInitFunc) st_search_dialog_class_init,
	NULL,
	NULL,
	sizeof(STSearchDialog),
	0,
	(GInstanceInitFunc) st_search_dialog_init,
      };
      
      search_dialog_type = g_type_register_static(SGTK_TYPE_ALERT_DIALOG,
						  "STSearchDialog",
						  &search_dialog_info,
						  0);
    }

  return search_dialog_type;
}

static void
st_search_dialog_class_init (STSearchDialogClass *class)
{
  g_type_class_add_private(class, sizeof(STSearchDialogPrivate));
}

static void
st_search_dialog_init (STSearchDialog *dialog)
{
  GtkWidget *label;
  GtkWidget *table;

  dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE(dialog, ST_TYPE_SEARCH_DIALOG, STSearchDialogPrivate);

  gtk_dialog_add_buttons(GTK_DIALOG(dialog),
			 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			 GTK_STOCK_FIND, GTK_RESPONSE_OK,
			 NULL);
  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
  
  gtk_image_set_from_stock(GTK_IMAGE(SGTK_ALERT_DIALOG(dialog)->image),
			   ST_STOCK_SEARCH,
			   GTK_ICON_SIZE_DIALOG);

  label = gtk_label_new_with_mnemonic(_("_Search for:"));
  gtk_label_set_selectable(GTK_LABEL(label), TRUE);

  dialog->priv->entry = gtk_entry_new();
  gtk_entry_set_activates_default(GTK_ENTRY(dialog->priv->entry), TRUE);

  gtk_label_set_mnemonic_widget(GTK_LABEL(label), dialog->priv->entry);

  g_signal_connect(dialog->priv->entry, "changed", G_CALLBACK(st_search_dialog_entry_changed_h), dialog);

  table = gtk_table_new(1, 2, FALSE);

  gtk_table_set_row_spacings(GTK_TABLE(table), SGTK_HIG_CONTROL_SPACING);
  gtk_table_set_col_spacings(GTK_TABLE(table), SGTK_HIG_CONTROL_LABEL_SPACING);

  gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
		   GTK_FILL,
		   0,
		   0,
		   0);
  gtk_table_attach(GTK_TABLE(table), dialog->priv->entry, 1, 2, 0, 1,
		   GTK_EXPAND | GTK_FILL,
		   0,
		   0,
		   0);

  gtk_box_pack_start(GTK_BOX(SGTK_ALERT_DIALOG(dialog)->hbox), table, TRUE, TRUE, 0);
  
  gtk_widget_show_all(table);

  st_search_dialog_update_sensitivity(dialog);
}

static void
st_search_dialog_update_sensitivity (STSearchDialog *dialog)
{
  const char *text;

  g_return_if_fail(ST_IS_SEARCH_DIALOG(dialog));

  text = gtk_entry_get_text(GTK_ENTRY(dialog->priv->entry));

  gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), GTK_RESPONSE_OK, *text != 0);
}

static void
st_search_dialog_entry_changed_h (GtkEditable *editable, gpointer user_data)
{
  STSearchDialog *dialog = user_data;

  st_search_dialog_update_sensitivity(dialog);
}

GtkWidget *
st_search_dialog_new (GtkWindow *parent)
{
  STSearchDialog *dialog;

  dialog = g_object_new(ST_TYPE_SEARCH_DIALOG, NULL);

  if (parent)
    gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);

  return GTK_WIDGET(dialog);
}

const char *
st_search_dialog_get_text (STSearchDialog *dialog)
{
  g_return_val_if_fail(ST_IS_SEARCH_DIALOG(dialog), NULL);

  return gtk_entry_get_text(GTK_ENTRY(dialog->priv->entry));
}
