#!/usr/bin/ruby
#########################
def wait_for_loading
#puts $main_load_thr
    return false if !$main_load_thr
    re=0
    while $main_load_thr.status!=false do sleep(0.1);re+=1;break if re>100; end
    return re<=100
end
#########################
require 'pathname'
require "fileutils"
script=Pathname::new($0)
BIBLICONC_BASE_DIRECTORY=script.realpath.dirname
$LOAD_PATH.unshift(BIBLICONC_BASE_DIRECTORY.to_s+"/")
PIXMAPS_DIRECTORY=BIBLICONC_BASE_DIRECTORY.to_s+"/"
PWD=ENV["PWD"]
require 'gtk2'
require 'print_functions'
require 'bibles'
require 'options'
require 'search'
require 'read'
require 'select_bible'
require 'help'
require 'config'
require 'request'
LANG='en'
#########################
RED_ON_WHITE="[1;31;49m"
GREEN_ON_WHITE="[1;32;49m"
BLUE_ON_WHITE="[1;34;49m"
CYAN_ON_WHITE="[1;36;49m"
BLACK_ON_WHITE="[0;30;49m"
###############
BIBLICONC_NAME="Concordance Bibliquest"
BIBLICONC_VERSION="2.1.0"
#########################
MAX_REQUEST=10

$general_options=Hash::new

$xmw=$ymw=nil
$wmw=800
$hmw=550
Configg::read_config

def cb_quit
    Configg::write_config
    #Gtk::main_quit
    $done=true
end
def cb_about
    about_dialog=Gtk::AboutDialog::new
    about_dialog.version=BIBLICONC_VERSION
    about_dialog.name=BIBLICONC_NAME
    about_dialog.authors=["www.bibliquest.org"]
    #about_dialog.license=""
    about_dialog.website="www.bibliquest.org/concordance.htm"
    about_dialog.comments="Les remarques sont à envoyer à: courier@bibliquest.org"
    if FileTest::exist?(PIXMAPS_DIRECTORY+"bibliconc48.png") then about_dialog.logo=Gdk::Pixbuf::new(PIXMAPS_DIRECTORY+"bibliconc48.png") end
    about_dialog.run
    about_dialog.destroy
end
def cb_save
    if Request::casual
	save_dialog=Gtk::FileChooserDialog::new($lang.menu("10101"),nil,Gtk::FileChooser::Action::SAVE,"",[Gtk::Stock::CANCEL,Gtk::Dialog::RESPONSE_CANCEL],[Gtk::Stock::SAVE,-3])
	if save_dialog.run==-3 then
	    chemin=save_dialog.filename
	    # vérifier s'il existe déjà?
	    Request::casual.save_to_file(chemin)
	    save_dialog.destroy
	else 
	    save_dialog.destroy
	    return 
	end
    end
end
def update_popup_menu
    if Request::casual
	$save_item.sensitive=true
    else
	$save_item.sensitive=false
    end
end
#########################
def create_button(icon,text,size=Gtk::IconSize::LARGE_TOOLBAR)
    button=Gtk::Button::new
    button.label=text
    button.image=Gtk::Image::new(icon,size)
    button
end
#########################
Gtk::init

$request=Array::new
$casual_request=-1
$expr=""

$main_window=Gtk::Window::new(Gtk::Window::TOPLEVEL)
$main_window.signal_connect("destroy"){$done=true}#{Gtk::main_quit}
main_box=Gtk::VBox::new(false,0)
$main_window.add(main_box)
main_buttonbar=Gtk::HButtonBox::new
main_box.pack_start(main_buttonbar,false,true,0)
$quit_button=create_button(Gtk::Stock::QUIT,$lang.menu("1000"))
$quit_button.signal_connect("clicked"){cb_quit}
main_buttonbar.pack_start($quit_button,false,false,0)
$selectconc_button=create_button(Gtk::Stock::SELECT_COLOR,$lang.menu("2000"))
$selectconc_button.signal_connect("clicked"){cb_select_bible}
# décommenté
main_buttonbar.pack_start($selectconc_button,false,false,0)
$rewind_button=create_button(Gtk::Stock::GO_BACK,$lang.menu("5000"))
$rewind_button.signal_connect("clicked"){cb_rewind}
main_buttonbar.pack_start($rewind_button,false,false,0)
$forward_button=create_button(Gtk::Stock::GO_FORWARD,$lang.menu("6000"))
$forward_button.signal_connect("clicked"){cb_forward}
main_buttonbar.pack_start($forward_button,false,false,0)
$find_button=create_button(Gtk::Stock::FIND,$lang.menu("3000"))
$find_button.signal_connect("clicked"){cb_search}
main_buttonbar.pack_start($find_button,false,false,0)
$read_button=create_button(Gtk::Stock::INDEX,$lang.menu("4000"))
$read_button.signal_connect("clicked"){cb_read}
main_buttonbar.pack_start($read_button,false,false,0)
$options_button=create_button(Gtk::Stock::PREFERENCES,$lang.menu("7000"))
$options_button.signal_connect("clicked"){cb_options}
main_buttonbar.pack_start($options_button,false,false,0)
$about_button=create_button(Gtk::Stock::ABOUT,$lang.menu("8000"))
$about_button.signal_connect("clicked"){cb_about}
main_buttonbar.pack_start($about_button,false,false,0)
main_scrolled_window=Gtk::ScrolledWindow::new(nil,nil)
main_scrolled_window.hscrollbar_policy=Gtk::PolicyType::AUTOMATIC
main_scrolled_window.vscrollbar_policy=Gtk::PolicyType::AUTOMATIC
main_box.pack_start(main_scrolled_window,true,true,0)
$main_textview=Gtk::TextView::new
$main_textview.wrap_mode=Gtk::TextTag::WRAP_WORD
$main_textview.buffer.create_tag("bold","weight"=>Pango::FontDescription::WEIGHT_BOLD)
$popup_menu=Gtk::Menu::new
$save_item=Gtk::MenuItem::new($lang.menu("10100").to_s)
$save_item.signal_connect("activate"){cb_save}
$popup_menu.append($save_item)
$popup_menu.show_all
$main_textview.signal_connect('button_press_event'){|widget,event| update_popup_menu;$popup_menu.popup(nil,nil,event.button,event.time) if event.button==3}
main_scrolled_window.add($main_textview)
$status_bar=Gtk::Statusbar::new
main_box.pack_start($status_bar,false,true,0)
$status_bar.push($status_bar.get_context_id("Tip"),$lang.tip("0").to_s)

buffer=$main_textview.buffer
buffer.create_tag('target0','background'=>'blue','foreground'=>'white')
buffer.create_tag('target1','background'=>'green','foreground'=>'black')
buffer.create_tag('target2','background'=>'red','foreground'=>'white')
buffer.create_tag('target3','background'=>'yellow','foreground'=>'black')
buffer.create_tag('target4','background'=>'magenta','foreground'=>'black')
buffer.create_tag('target5','background'=>'cyan','foreground'=>'black')
buffer.create_tag('target6','background'=>'white','foreground'=>'black')
buffer.create_tag('target7','background'=>'darkgray','foreground'=>'white')
buffer.create_tag('target8','background'=>'orange','foreground'=>'white')
buffer.create_tag('target9','background'=>'black','foreground'=>'white')


$main_window.title="#{BIBLICONC_NAME} - version #{BIBLICONC_VERSION}"
if FileTest::exist?(PIXMAPS_DIRECTORY+"bibliconc48.png") then $main_window.icon=Gdk::Pixbuf::new(PIXMAPS_DIRECTORY+"bibliconc48.png") end

$main_window.show_all
$main_window.move($xmw,$ymw) if $xmw&&$ymw
$main_window.resize($wmw,$hmw) if $wmw&&$hmw

update_forrew_buttons

#$gtk_thread=Gtk::main # pb of thread blocking; workaround
$done=false
begin
  while Gtk.events_pending?
    Gtk.main_iteration
  end
  sleep 0.02
end until $done
