/************************************************************************
    filename:   GTK2CEGuiRendererSelector.cpp
    created:    24/9/2004
    author:     Paul D Turner
*************************************************************************/
/*************************************************************************
    Crazy Eddie's GUI System (http://www.cegui.org.uk)
    Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
 
    This library 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
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*************************************************************************/
#include "GTK2CEGuiRendererSelector.h"

// Renderer name strings
const std::string GTK2CEGuiRendererSelector::OgreRendererName("Ogre3D Engine Renderer");
const std::string GTK2CEGuiRendererSelector::OpenGLRendererName("OpenGL Renderer (using glut)");
const std::string GTK2CEGuiRendererSelector::IrrlichtRendererName("Irrlicht Engine Renderer");



GTK2CEGuiRendererSelector::GTK2CEGuiRendererSelector()
{
    // fake args for gtk_init
    int argc = 1;
    char* argv = "SampleApp";
    char** pargv = &argv;

    gtk_init (&argc, &pargv);

    createDialog();
}

GTK2CEGuiRendererSelector::~GTK2CEGuiRendererSelector()
{
}

bool GTK2CEGuiRendererSelector::inkokeDialog()
{
    CEGuiRendererType renderer_types[3];
    int idx = 0;
    
    // Put items in the combobox for enabled renderers.
    if (d_rendererAvailability[OgreGuiRendererType])
    {
        gtk_combo_box_append_text(GTK_COMBO_BOX(d_combobox1), OgreRendererName.c_str());
        renderer_types[idx++] = OgreGuiRendererType;
    }
    if (d_rendererAvailability[OpenGLGuiRendererType])
    {
        gtk_combo_box_append_text(GTK_COMBO_BOX(d_combobox1), OpenGLRendererName.c_str());
        renderer_types[idx++] = OpenGLGuiRendererType;
    }
    if (d_rendererAvailability[IrrlichtGuiRendererType])
    {
        gtk_combo_box_append_text(GTK_COMBO_BOX(d_combobox1), IrrlichtRendererName.c_str());
        renderer_types[idx++] = IrrlichtGuiRendererType;
    }
    
    gtk_widget_show(d_dialog);
    gtk_main ();

    if (!d_cancelled)
    {
        gint active = gtk_combo_box_get_active(GTK_COMBO_BOX(d_combobox1));

        if (active != -1)
        {
            d_lastSelected = renderer_types[active];
        }
        else
        {
            d_cancelled = true;
        }

    }

    return !d_cancelled;
}

void GTK2CEGuiRendererSelector::createDialog()
{
    d_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request(d_dialog, 320, 100);
    gtk_window_set_title(GTK_WINDOW(d_dialog), "CEGui - Renderer Selection");
    gtk_window_set_position(GTK_WINDOW(d_dialog), GTK_WIN_POS_CENTER);
    gtk_window_set_resizable(GTK_WINDOW(d_dialog), FALSE);
    gtk_window_set_type_hint(GTK_WINDOW(d_dialog), GDK_WINDOW_TYPE_HINT_DIALOG);

    d_vbox1 = gtk_vbox_new(FALSE, 10);
    gtk_widget_show(d_vbox1);
    gtk_container_add(GTK_CONTAINER(d_dialog), d_vbox1);

    d_label1 = gtk_label_new("Select Renderer:");
    gtk_widget_show(d_label1);
    gtk_box_pack_start(GTK_BOX(d_vbox1), d_label1, FALSE, FALSE, 0);
    gtk_misc_set_alignment(GTK_MISC(d_label1), 0, 0.5);

    d_hbox1 = gtk_hbox_new(FALSE, 0);
    gtk_widget_show(d_hbox1);
    gtk_box_pack_end(GTK_BOX(d_vbox1), d_hbox1, FALSE, TRUE, 5);

    d_cancelButton = gtk_button_new_from_stock("gtk-cancel");
    gtk_widget_show(d_cancelButton);
    gtk_box_pack_start(GTK_BOX(d_hbox1), d_cancelButton, TRUE, FALSE, 0);

    d_okButton = gtk_button_new_from_stock("gtk-ok");
    gtk_widget_show(d_okButton);
    gtk_box_pack_start(GTK_BOX(d_hbox1), d_okButton, TRUE, FALSE, 0);

    d_combobox1 = gtk_combo_box_new_text();
    gtk_widget_show(d_combobox1);
    gtk_box_pack_start(GTK_BOX(d_vbox1), d_combobox1, TRUE, FALSE, 0);

    g_signal_connect((gpointer) d_cancelButton, "clicked",
                       G_CALLBACK(GTK2CEGuiRendererSelector::on_cancelButton_clicked),
                       this);
    g_signal_connect((gpointer) d_okButton, "clicked",
                       G_CALLBACK(GTK2CEGuiRendererSelector::on_okButton_clicked),
                       this);
}

void GTK2CEGuiRendererSelector::on_cancelButton_clicked(GtkButton* button, gpointer user_data)
{
    static_cast<GTK2CEGuiRendererSelector*>(user_data)->d_cancelled = true;
    gtk_widget_hide(static_cast<GTK2CEGuiRendererSelector*>(user_data)->d_dialog);
    gtk_main_quit();
}

void GTK2CEGuiRendererSelector::on_okButton_clicked(GtkButton* button, gpointer user_data)
{
    static_cast<GTK2CEGuiRendererSelector*>(user_data)->d_cancelled = false;
    gtk_widget_hide(static_cast<GTK2CEGuiRendererSelector*>(user_data)->d_dialog);
    gtk_main_quit();
}
