
/* Copyright (C) 2001-2002 Philip Van Hoof
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "gtk/gtksignal.h"
#include "gtk/gtktable.h"
#include "gtk/gtktogglebutton.h"
#include "GtkCodeText.h"



enum {
  GTKCODETEXT_SIGNAL,
  LAST_SIGNAL
};


static void		gtkcodetext_class_init			(GtkCodeTextClass	*klass);
static void		gtkcodetext_init				(GtkCodeText		*gtkcodee);

gboolean		gtkcodetext_keypress_event		(GtkWidget			*text,
												 GdkEventKey		*event,
												 GtkCodeText		*codetext);

gboolean		gtkcodetext_combochanged_event	(GtkWidget			*combo,
												 GtkCodeText		*codetext);

gboolean		gtkcodetext_comboactivate_event	(GtkWidget			*combo,
												 GtkCodeText		*codetext);


static gint gtkcodetext_signals[LAST_SIGNAL] = { 0 };

guint
gtkcodetext_get_type ()
{
static guint gtkcodee_type = 0;
  if (!gtkcodee_type)	  
    {	
	static const GtkTypeInfo gtkcodee_info =
      {
        "GtkCodeText",
        sizeof (GtkCodeText),
        sizeof (GtkCodeTextClass),
        (GtkClassInitFunc) gtkcodetext_class_init,
        (GtkObjectInitFunc) gtkcodetext_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      gtkcodee_type = gtk_type_unique (gtk_vbox_get_type (), &gtkcodee_info);
	  
    }
  return gtkcodee_type;
}


static void
gtkcodetext_class_init (GtkCodeTextClass *class)
{

  GtkObjectClass *object_class;
  object_class = (GtkObjectClass*) class;
  gtkcodetext_signals[GTKCODETEXT_SIGNAL] = gtk_signal_new ("gtkcodetext",
					 GTK_RUN_FIRST,
					 object_class->type,
					 GTK_SIGNAL_OFFSET (GtkCodeTextClass, gtkcodetext),
					 gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);

  gtk_object_class_add_signals (object_class, gtkcodetext_signals, LAST_SIGNAL);
  class->gtkcodetext = NULL;

}





static void
gtkcodetext_init (GtkCodeText *codetext)
{
	GtkWidget *table;

		
	table = gtk_table_new (1, 1, TRUE);
	gtk_container_add (GTK_CONTAINER(codetext), table);
	gtk_widget_show (table);

	codetext->scrolledwindow = gtk_scrolled_window_new (NULL, NULL);	
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (codetext->scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	gtk_table_attach_defaults (GTK_TABLE(table), codetext->scrolledwindow, 0, 1, 1, 2);	
	gtk_widget_show (codetext->scrolledwindow);
	
    codetext->text = gtk_text_new (NULL,NULL);
	gtk_text_set_editable(GTK_TEXT(codetext->text), TRUE);

	gtk_container_add (GTK_CONTAINER (codetext->scrolledwindow), codetext->text);
    gtk_signal_connect (GTK_OBJECT (codetext->text), "key_press_event",
                      GTK_SIGNAL_FUNC (gtkcodetext_keypress_event),
                      codetext);					  
	gtk_widget_show (codetext->text);

	codetext->window = gtk_window_new (GTK_WINDOW_POPUP);
    codetext->combo = gtk_combo_new ();
	
    gtk_signal_connect (GTK_OBJECT (codetext->combo), "changed",
                      GTK_SIGNAL_FUNC (gtkcodetext_combochanged_event),
                      codetext);					  

	gtk_signal_connect (GTK_OBJECT (codetext->combo), "activate",
                      GTK_SIGNAL_FUNC (gtkcodetext_comboactivate_event),
                      codetext);					  

	gtk_container_add (GTK_CONTAINER(codetext->window), codetext->combo);
	gtk_widget_show (codetext->combo);


}


GtkWidget*	gtkcodetext_new			(void)
{
  return GTK_WIDGET ( gtk_type_new (gtkcodetext_get_type ()));
}


gboolean	gtkcodetext_setlanguage	(GtkCodeText *codetext,
									gchar *language) 
{
	g_print("Setting language to %s\n", language);

}



gboolean		gtkcodetext_combochanged_event	(GtkWidget			*combo,
												 GtkCodeText		*codetext)
{
	g_print("Combobox changed\n");
}

gboolean		gtkcodetext_comboactivate_event	(GtkWidget			*combo,
												 GtkCodeText		*codetext)
{
	g_print("Combobox activated\n");
}


gboolean		gtkcodetext_keypress_event		(GtkWidget			*text,
												 GdkEventKey		*event,
												 GtkCodeText		*codetext)
{

/*struct GdkEventKey
{
  GdkEventType type;
  GdkWindow *window;
  gint8 send_event;
  guint32 time;
  guint state;
  guint keyval;
  gint length;
  gchar *string;
};
*/
	
	g_print("Keypress event (%s)\n", event->string);

	if (!(strcmp(event->string,"."))) {
		GList *mylist = NULL;
		gtk_text_insert (GTK_TEXT(codetext->text), NULL, NULL, NULL, "ikke", 4);

		mylist = g_list_append (mylist, "first");
		mylist = g_list_append (mylist, "second");
		
		gtk_combo_set_popdown_strings (GTK_COMBO(codetext->combo), mylist);

		gtk_window_set_position (GTK_WINDOW(codetext->window),GTK_WIN_POS_MOUSE);
		gtk_widget_show (codetext->window);
		gtk_window_activate_focus (GTK_WINDOW(codetext->window));
		gtk_widget_grab_focus (codetext->combo);	


	} else {
		gtk_widget_hide (codetext->window);

	}

  return TRUE;
}
