/*************************************************************************************************************
* This file contains main function
* 2012/10 by Dlxko
* Version 4.0
**************************************************************************************************************/
#include "Main.h"

// Init GTK+2
using namespace std;
int main( int argc, char **argv );

// Win main function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
	return main (__argc, __argv);
}

// Main function of program
int main(int argc, char *argv[])
{
	// Initialize extern variables
	current_buddy = 0;
	activated_events = 0;

	// Start threads support
	if( ! g_thread_supported() )
		g_thread_init( NULL );

	// GTK init
	gtk_init (&argc, &argv);

	// Create dialog window
	create_dialog_window();

	// Get connection or close
	s = dxconnect();
	if (s == 0 || s == -1)
		function_show_error(LANG_CANT_CREATE_CONNECT);

	// New loggin window
	log_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_resize (GTK_WINDOW (log_window), 300, 80);
	gtk_window_set_title (GTK_WINDOW (log_window), LANG_LOGIN);
	g_signal_connect_swapped (log_window, "delete-event", G_CALLBACK (signal_go_offline), NULL);
	gtk_window_set_icon(GTK_WINDOW(log_window), default_icon);

	// New conversation window
	conversation_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (conversation_window), 470, 325);
	gtk_window_set_title (GTK_WINDOW (conversation_window), "DxSock");
	g_signal_connect (conversation_window, "delete-event", G_CALLBACK (signal_close_conversation), NULL);
	gtk_widget_hide_on_delete(conversation_window);
	gtk_window_set_icon(GTK_WINDOW(conversation_window), default_icon);

	// Create new box and container
	conversation_notebox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (conversation_window), conversation_notebox);

	// Create notebook
	conversation_notebook = gtk_notebook_new();
	gtk_box_pack_start(GTK_BOX(conversation_notebox), conversation_notebook, TRUE, TRUE, 0);
	gtk_notebook_set_scrollable(GTK_NOTEBOOK(conversation_notebook), TRUE);
	gtk_notebook_popup_enable(GTK_NOTEBOOK(conversation_notebook));

	// Show loggin
	singlebody_control_loggin_shown();

	// Load pixmap
	if(!create_pixmaps())
		function_show_error(LANG_CANT_CREATE_IMAGES);

	// Create channel
	if(!create_channel())
		function_show_error(LANG_CANT_CREATE_CHANNEL);

	// Create events and adder
	crate_requests();

	// Create loop
	gtk_main();

	// Exit
	return 0;
}