/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "interface.h"
#include "support.h"
#include "../include/loaddic.h"

//辞書の一覧
//
GList *dicList;
//
//インデックスリスト
//
GList *indList;
//
//辞書読み込み終了フラグ
//
gboolean dicloadflag=0;

//
//ウィンドウウィジェット
//
GtkWidget *window1,*dicinfo;

int
main (int argc, char *argv[])
{
	gint i=0,active=0,contextid;
	gchar *envtext;
	gchar *statustext;
	//statusbar用
	GtkStatusbar *statusbar;
	//treelist用
	GtkTreeView *tview;
	GtkListStore *model;
	GtkTreeViewColumn *column1,*column2;
	GtkCellRenderer *rend;
	//combobox用
	GtkComboBoxEntry *combo;
	GList *listp;
	diclistitem *dicitem;
	
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");

  /*
   * The following code was added by Glade to create one of each component
   * (except popup menus), just so that you see something after building
   * the project. Delete any components that you don't want shown initially.
   */
  //辞書の読み込み
  if (g_file_test(PACKAGE_DATA_DIR "/endicrc",G_FILE_TEST_IS_REGULAR )){
		dicList=(GList*)LoadIniFile(PACKAGE_DATA_DIR "/endicrc",dicList);
	}
	else{
		envtext=g_strdup_printf("%s/.endicrc",g_getenv("HOME"));
		dicList=(GList*)LoadIniFile(envtext,dicList);
		g_free(envtext);
	}

	//辞書が存在しないとき
	if(dicList==NULL){
		printf("no dictionary list\n");
		return -1;
	}

	window1 = create_endic ();
	dicinfo = create_dicinfodialog ();

	/*
	 * TreeViewの設定
	 */
	tview=(GtkTreeView*)lookup_widget(GTK_WIDGET(window1),"dicview");
	model=gtk_list_store_new(2,G_TYPE_STRING,G_TYPE_STRING);
	gtk_tree_view_set_model(GTK_TREE_VIEW(tview),GTK_TREE_MODEL(model));

	//レンダラーの作成
	rend=gtk_cell_renderer_text_new();
	//第1カラムの作成
	column1=gtk_tree_view_column_new_with_attributes(
									"words",rend,"text",0,NULL);
	//自動調整に設定
	gtk_tree_view_column_set_sizing(column1,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
	//レンダラーの作成
	rend=gtk_cell_renderer_text_new();
	//第2カラムの作成
	column2=gtk_tree_view_column_new_with_attributes(
									"about",rend,"text",1,NULL);
	//自動調整に設定
	gtk_tree_view_column_set_sizing(column2,GTK_TREE_VIEW_COLUMN_AUTOSIZE);

	gtk_tree_view_append_column(tview,column1);
	gtk_tree_view_append_column(tview,column2);

	/*
	 * cmbdiclist の設定
	 */
	combo=(GtkComboBoxEntry*)lookup_widget(GTK_WIDGET(window1),"cmbdiclist");
	//エントリーを作成
	dicList=g_list_first(dicList);
	listp=dicList;
	while(listp){
		dicitem=(diclistitem*)listp->data;
		//h!=NULLは選択中の辞書と判断
		if(dicitem->h)
			active=i;
		gtk_combo_box_append_text(GTK_COMBO_BOX(combo),dicitem->dicname);
		listp=g_list_next(listp);
		i++;
	}
	gtk_combo_box_set_active(GTK_COMBO_BOX(combo),active);

	dicloadflag=TRUE;
	/*
	 * ステータスバーの設定
	 */
	statusbar=GTK_STATUSBAR(
		lookup_widget(GTK_WIDGET(window1),"statusbar1"));
	contextid=gtk_statusbar_get_context_id(statusbar,"contextid");

	statustext=g_strdup_printf("dic count: %d", g_list_length(dicList));

	gtk_statusbar_push(statusbar,contextid,statustext);
	
  gtk_widget_show (window1);

	//
  gtk_main ();

	g_free(statustext);
  return 0;
}
