/*
 * Copyright © 2010 Andrea Costa <nukemup@hotmail.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 51 Franklin St
 * Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "block_notes.h"

block_notes_s *block_notes;


/*
 Saves textview data
*/
void
block_notes_save_text (GtkWidget *widget, block_notes_s *block_notes)
{
	GtkTextIter start;
	GtkTextIter end;
	gchar *string;
	FILE *file;
	gtk_text_buffer_get_start_iter (block_notes->buffer, &start);
	gtk_text_buffer_get_end_iter (block_notes->buffer, &end);
	string = gtk_text_buffer_get_text (block_notes->buffer, &start, &end, FALSE);

	file = fopen (block_notes->work_dir, "w");
	if (file != NULL){
		fprintf (file, "%s", string);
		fclose (file);
	}
}


/*
 Main sidegadget's function
*/
GtkWidget*
sidegadget_new (void)
{
	block_notes = g_new0 (block_notes_s, 1);
	block_notes->work_dir = g_strdup_printf ("sidegadget/block-notes/%s", BLOCK_NOTES_DATA_FILE);

	/* Get initial text */
	block_notes_conf_get_initial_text (block_notes, block_notes->work_dir);
	
	block_notes->view = gtk_text_view_new ();
	
	gtk_widget_set_size_request (block_notes->view, -1, 200 - 25);

	/* Set textview's color*/
//	gtk_widget_modify_base (block_notes->view, GTK_STATE_NORMAL, &sidegadget->color_gadget);
	gtk_widget_grab_focus (block_notes->view);

	gtk_text_view_set_editable (GTK_TEXT_VIEW (block_notes->view), TRUE);
	gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (block_notes->view), TRUE);

	gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (block_notes->view), GTK_WRAP_WORD_CHAR);
	gtk_text_view_set_justification (GTK_TEXT_VIEW (block_notes->view), GTK_JUSTIFY_LEFT);

	/* Set space between two rows */
	gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (block_notes->view), 1);
	gtk_text_view_set_pixels_below_lines (GTK_TEXT_VIEW (block_notes->view), 1);

	/* Set borders */
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (block_notes->view), 2);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (block_notes->view), 2);

	/* Set font color */
	gtk_widget_modify_text (block_notes->view, GTK_STATE_NORMAL, &block_notes->color_text);
	
	/* Set font */
	gtk_widget_modify_font (block_notes->view, block_notes->font_pango);

	gtk_widget_show (block_notes->view);

	/* Create a buffer*/
	block_notes->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(block_notes->view));

	/* Insert init_text in the buffer */
	gtk_text_buffer_set_text (block_notes->buffer, block_notes->string_init, -1);

	/* Create scrolled window */
	block_notes->scrolled_win = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (block_notes->scrolled_win), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	/* Insert the textview in the scrolled win */
	gtk_container_add (GTK_CONTAINER(block_notes->scrolled_win), block_notes->view);

	g_signal_connect (G_OBJECT (block_notes->buffer), "changed", G_CALLBACK (block_notes_save_text), block_notes);
	
	return block_notes->scrolled_win;
}