/*
 *	gtk_common.c
 *	
 *	(C) 2001 Robert Kling
 *	robkli-8@student.luth.se
 *	http://boombox.campus.luth.se
 *	
 *	Based on code (C) 2000 Daniel Sundberg
 *	
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <gtk/gtk.h>
#include <stdlib.h>
#include "gtk_common.h"

gint close_popup_dialog(GtkWidget *widget, gpointer data) {

	gtk_widget_destroy(data);
    return TRUE;
}

void popup_dialog(int height, gboolean wrap, char *title, char *text, char *button_text) {

	GtkWidget *window, *textlabel, *button;

	window = gtk_dialog_new();
	gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(close_popup_dialog), window);
	gtk_widget_set_usize( GTK_WIDGET(window), 300, height);
	gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
	gtk_window_set_title(GTK_WINDOW (window), title);
	gtk_container_set_border_width(GTK_CONTAINER (window), 3);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);

  	textlabel = gtk_label_new(text);
  	gtk_label_set_line_wrap(GTK_LABEL(textlabel), wrap);
  	//gtk_label_set_justify(...);
  	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), textlabel, TRUE, TRUE, 0);
	gtk_widget_show(textlabel);

	button = gtk_button_new_with_label (button_text);
  	gtk_signal_connect(GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (close_popup_dialog), window);
  	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area), button, FALSE, TRUE, 0);
  	gtk_widget_show(button);

  	gtk_widget_show(window);
}


/* Create a new hbox with an image and a label packed into it
 * and return the box. */
GtkWidget *xpm_label_box(GtkWidget *parent, char *xpm_data[], gchar *label_text )
{
  GtkWidget *box, *label, *pixmapwid;
  GdkPixmap *pixmap;
  GdkBitmap *mask;
  GtkStyle *style;

  box = gtk_hbox_new (FALSE, 0);
  gtk_container_set_border_width(GTK_CONTAINER (box), 0);
  
  style = gtk_widget_get_style(parent);
  pixmap = gdk_pixmap_colormap_create_from_xpm_d(parent->window, gtk_widget_get_colormap(parent), &mask, &style->bg[GTK_STATE_NORMAL], xpm_data);
  pixmapwid = gtk_pixmap_new (pixmap, mask);

  label = gtk_label_new (label_text);
  gtk_box_pack_start (GTK_BOX (box), pixmapwid, FALSE, FALSE, 3);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 3);
  
  gtk_widget_show(pixmapwid);
  gtk_widget_show(label);
  
  return(box);
}

/* Get the text of the label we packed into a box 
   in the function above */
gchar *xpm_label_box_get_text(GtkWidget *box) {
  gchar *text;
  GList *glist = GTK_BOX(box)->children;
  GtkBoxChild *boxchild = (GtkBoxChild *)glist->next->data;
  GtkWidget *widget = boxchild->widget;
  gtk_label_get(GTK_LABEL(widget), &text);

  return text;
}

GtkWidget *gtk_button_new_with_label_with_pixmap(gchar *label, char *xpm_data[]) {
  GtkWidget *button, *box;

  button = gtk_button_new();
  box = xpm_label_box(button, xpm_data, label);
  gtk_widget_show(box);
  gtk_container_add(GTK_CONTAINER(button), box);
  return button;
}

gchar *gtk_button_get_text(GtkWidget *button) {
  if (GTK_IS_LABEL(GTK_BIN(button)->child)) {
    gchar *text;
    GtkWidget *label = GTK_BIN(button)->child;
    gtk_label_get(GTK_LABEL(label), &text);
    return text;
  } else {
    return xpm_label_box_get_text(GTK_BIN(button)->child);  
  }
}

GtkWidget *gtk_menu_item_new_with_label_with_pixmap(gchar *label, char *xpm_data[]) {
  GtkWidget *menu_item, *box;

  menu_item = gtk_menu_item_new();
  box = xpm_label_box(menu_item, xpm_data, label);
  gtk_widget_show(box);
  gtk_container_add(GTK_CONTAINER(menu_item), box);
  return menu_item;
}

gchar *gtk_menu_item_get_text(GtkWidget *menu_item) {
  if (GTK_IS_LABEL(GTK_BIN(menu_item)->child)) {
    gchar *text;
    GtkWidget *label = GTK_BIN(menu_item)->child;
    gtk_label_get(GTK_LABEL(label), &text);
    return text;
  } else {
    return xpm_label_box_get_text(GTK_BIN(menu_item)->child);  
  }
}

syntax highlighted by Code2HTML, v. 0.9.1