#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

GtkWidget *mainWin;

/*============================================================================*/
void btn_click(GtkWidget *btn, gpointer data)
{
    GtkWidget *dlgWin;
    GtkWidget *contentArea;
    GtkWidget *vBox;  
    GtkWidget *combo;
    int i;
    char buf[128];

    dlgWin = gtk_dialog_new_with_buttons(data, GTK_WINDOW(mainWin),
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         "_OK", GTK_RESPONSE_CLOSE,
                                         NULL);
    gtk_window_set_position(GTK_WINDOW(dlgWin), GTK_WIN_POS_CENTER_ON_PARENT);

    contentArea = gtk_dialog_get_content_area(GTK_DIALOG(dlgWin));
    vBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(contentArea), vBox);
    gtk_widget_set_margin_start(vBox, 20);
    gtk_widget_set_margin_end(vBox, 20);
    gtk_widget_set_margin_top(vBox, 20);
    gtk_widget_set_margin_bottom(vBox, 20);

    combo = gtk_combo_box_text_new();
    gtk_box_pack_start(GTK_BOX(vBox), combo, FALSE, FALSE, 20);
    if (g_ascii_strncasecmp(data, "list", 4) == 0)
    {
        const gchar *styleSheet = "#rcrs-combo { -GtkComboBox-appears-as-list: true }";
        gtk_widget_set_name(combo, "rcrs-combo");
        GtkStyleContext *styleCtx = gtk_widget_get_style_context(combo);
        GtkCssProvider *cssPrvdr = gtk_css_provider_new();
        gtk_css_provider_load_from_data(cssPrvdr, styleSheet, -1, NULL);
        gtk_style_context_add_provider(styleCtx, GTK_STYLE_PROVIDER(cssPrvdr),
                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    }
    for (i = 1; i <= 50; i++)
    {
        sprintf(buf, "Entry %d", i);
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), buf);
    }
    gtk_widget_show(combo);
    gtk_widget_show(vBox);

    gtk_dialog_run(GTK_DIALOG(dlgWin));
    gtk_widget_destroy(dlgWin);
}

/*============================================================================*/
/* Event handler for the delete event */
static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    gtk_main_quit();

    /* Let GTK process the destroy and terminate us */
    return FALSE;
}

/*============================================================================*/
int main(int argc, char *argv[])
{
    GtkWidget *hBox, *vBox, *vBox1, *vBox2;  
    GtkWidget *btnBox;  
    GtkWidget *label;
    GtkWidget *button;
    GtkWidget *combo;
    int i, j;
    char buf[128];

    gtk_init(&argc, &argv);

    mainWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(mainWin), "Test Application");
    gtk_window_set_position(GTK_WINDOW(mainWin), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(mainWin), 500, -1);

    vBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(mainWin), vBox);
    hBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_pack_start(GTK_BOX(vBox), hBox, TRUE, FALSE, 20);
    vBox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(hBox), vBox1, TRUE, FALSE, 20);
    vBox2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_end(GTK_BOX(hBox), vBox2, TRUE, FALSE, 20);

    for (i = 0; i < 2; i++)
    {
        GtkWidget *thisVbx = (i == 0) ? vBox1 : vBox2;  
        const gchar *strType =  (i == 0) ? "Menu-style" : "List-style";  

        label = gtk_label_new(strType);
        gtk_box_pack_start(GTK_BOX(thisVbx), label, FALSE, FALSE, 20);
        gtk_widget_show(label);
        combo = gtk_combo_box_text_new();
        gtk_box_pack_start(GTK_BOX(thisVbx), combo, FALSE, FALSE, 20);
        if (i > 0)
        {
            const gchar *styleSheet = "#rcrs-combo { -GtkComboBox-appears-as-list: true }";
            gtk_widget_set_name(combo, "rcrs-combo");
            GtkStyleContext *styleCtx = gtk_widget_get_style_context(combo);
            GtkCssProvider *cssPrvdr = gtk_css_provider_new();
            gtk_css_provider_load_from_data(cssPrvdr, styleSheet, -1, NULL);
            gtk_style_context_add_provider(styleCtx, GTK_STYLE_PROVIDER(cssPrvdr),
                                           GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
        }
        for (j = 1; j <= 50; j++)
        {
            sprintf(buf, "Entry %d", j);
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), buf);
        }
        gtk_widget_show(combo);

        button = gtk_button_new_with_label("Dialog...");
        gtk_box_pack_start(GTK_BOX(thisVbx), button, FALSE, FALSE, 20);
        g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(btn_click), (gpointer)strType);
        gtk_widget_show(button);
    }

    btnBox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_box_pack_end(GTK_BOX(vBox), btnBox, TRUE, FALSE, 0);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(btnBox), GTK_BUTTONBOX_END);
    button = gtk_button_new_with_label("Close");
    gtk_container_add(GTK_CONTAINER(btnBox), button);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(delete_event), NULL);
    gtk_widget_show(button);

    gtk_widget_show(vBox);
    gtk_widget_show(hBox);
    gtk_widget_show(vBox1);
    gtk_widget_show(vBox2);
    gtk_widget_show(btnBox);

    g_signal_connect(G_OBJECT(mainWin), "delete_event", G_CALLBACK(delete_event), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(mainWin), 10);
    gtk_widget_show(mainWin);
    gtk_main();

    return 0;
}

/*============================================================================*/

