/*

  Copyright (c) 2003,2004 uim Project http://uim.freedesktop.org/

  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 authors 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 REGENTS 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 REGENTS 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.

*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <uim/uim.h>
#include <uim/uim-helper.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>

#define NR_CANDIDATES 10 /* This should be configurable */

typedef struct {
  GtkWidget *window;
  GtkWidget* view;
  GtkWidget *numlabel;
  GtkListStore* store;
  int nr_candidates;
  int candidate_index;
  int is_active;
}candidate_window;

static candidate_window cwin;
static int read_tag;

void
init_candidate_win(void)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column; 
  
  GtkWidget* scrw;  
  GtkWidget *vbox;
  GtkWidget *numlabel;

  vbox = gtk_vbox_new(FALSE, 0);

  cwin.store = gtk_list_store_new(2, G_TYPE_UINT, G_TYPE_STRING);
  
  cwin.window = gtk_window_new(GTK_WINDOW_POPUP);
  gtk_container_set_border_width(GTK_CONTAINER(cwin.window), 1);
  gtk_window_set_default_size(GTK_WINDOW(cwin.window), 80, 150);
  
  scrw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrw),
				 GTK_POLICY_NEVER,
				 GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start(GTK_BOX(vbox), scrw, TRUE, TRUE, 0);
  
  cwin.view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(cwin.store));
  gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(cwin.view), TRUE);
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(cwin.view), FALSE);
  gtk_container_add(GTK_CONTAINER(scrw), cwin.view);

  gtk_container_add(GTK_CONTAINER(cwin.window), vbox);
    
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("No",
						    renderer,
						    "text",
						    0,
						    NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(cwin.view), column);
  
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("Text",
						    renderer,
						    "text",
						    1,
						    NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(cwin.view), column);
  
  numlabel = gtk_label_new("");

  gtk_box_pack_start(GTK_BOX(vbox), numlabel, FALSE, FALSE, 0);

  cwin.numlabel = numlabel;

  cwin.is_active = 0;

  gtk_widget_show(numlabel);
  gtk_widget_show_all(vbox);
}

void
candwin_activate(char **str)
{
  gsize rbytes, wbytes;
  GtkTreeIter ti;
  int i,j = 1;
  char *utf8_str;

  gtk_list_store_clear(cwin.store);
  
  for(i = 2; str[i];i++) {

    if(strcmp(str[i], "") == 0) {
      break;
    }
    utf8_str = g_convert(str[i],
			 -1, 
			 "UTF-8",
			 "EUC-JP",
			 &rbytes, &wbytes, NULL);
    
    gtk_list_store_append(cwin.store, &ti);
    gtk_list_store_set(cwin.store, &ti,
		       0, j,
		       1, utf8_str,
		       -1);
    j++;
    g_free(utf8_str);
  }
  
  gtk_widget_show_all(cwin.window);
  cwin.nr_candidates = i - 2 ;
  cwin.is_active = 1;
}

void
candwin_update(char **str)
{
  int index;
  char idx[20];
  GtkTreePath* path;

  sscanf(str[1],"%d",&index);
  cwin.candidate_index = index;

  path = gtk_tree_path_new_from_indices(index, -1);
  gtk_tree_view_set_cursor(GTK_TREE_VIEW(cwin.view),
			   path, NULL, FALSE);
  gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(cwin.view),
			       path, NULL, FALSE, 0.0, 0.0);

  sprintf(idx, "%d / %d", cwin.candidate_index + 1 , cwin.nr_candidates); 
  
  gtk_label_set_text( GTK_LABEL(cwin.numlabel), idx);

}

void
candwin_move(char **str)
{
  int x,y;
  sscanf(str[1],"%d",&x);
  sscanf(str[2],"%d",&y);
  gtk_window_move(GTK_WINDOW(cwin.window), x, y );
}


void
candwin_show(void)
{
  if(cwin.is_active)
    gtk_widget_show_all(cwin.window);
}

void
candwin_deactivate(void)
{
  gtk_widget_hide(cwin.window);
  cwin.is_active = 0;
}

void str_parse(char *str)
{
  gchar **tmp;
  int i = 0;
  
  tmp = g_strsplit(str, "\n", 0);
  
  while(tmp[i]){
    
    if(strcmp("activate", tmp[i]) == 0){
      candwin_activate(tmp);
    } else if(strcmp("select", tmp[i]) == 0) {
      candwin_update(tmp);
    } else if(strcmp("show", tmp[i]) == 0) {
      candwin_show();
    } else if(strcmp("hide", tmp[i]) == 0) {
      gtk_widget_hide_all(cwin.window);
    } else if(strcmp("move", tmp[i]) == 0) {
      candwin_move(tmp);
    } else if(strcmp("deactivate", tmp[i]) == 0) {
      candwin_deactivate();
    }
    i++;
  }
  g_strfreev(tmp);
}

static void
read_cb(gpointer p, int fd, GdkInputCondition c)
{
  char *tmp = strdup("");
  char buf[1024];

  while (fgets (buf, sizeof(buf), stdin ) != NULL ) {
    if(strcmp( buf, "\n" ) == 0) {
       str_parse(tmp);
       free(tmp);
       return;
    }
    tmp = realloc(tmp, strlen(tmp) + strlen(buf) + 1);
    strcat(tmp, buf);
  }
  exit(-1);
}

int
main(int argc, char *argv[])
{
  gtk_set_locale();
  gtk_init( &argc, &argv );

  init_candidate_win();

  read_tag = gdk_input_add(0, (GdkInputCondition)GDK_INPUT_READ,
			   read_cb, 0);
  gtk_main ();
  return 0;
}
