#include <gtk/gtk.h>
#include <stdio.h>

void set_model_narrow(GtkComboBoxText *const comboBoxText)
{
	gtk_combo_box_text_remove_all(comboBoxText);
	gtk_widget_set_size_request(GTK_WIDGET(comboBoxText), 1, -1);
	gtk_combo_box_text_append(comboBoxText, NULL, "short");
	gtk_combo_box_text_append(comboBoxText, NULL, "again");
	gtk_combo_box_text_append(comboBoxText, NULL, "more");
	gtk_combo_box_set_active(GTK_COMBO_BOX(comboBoxText), 0);
}

void set_model_wide(GtkComboBoxText *const comboBoxText)
{
	gtk_combo_box_text_remove_all(comboBoxText);
	gtk_widget_set_size_request(GTK_WIDGET(comboBoxText), 1, -1);
	gtk_combo_box_text_append(comboBoxText, NULL, "a long value");
	gtk_combo_box_text_append(comboBoxText, NULL, "another one");
	gtk_combo_box_text_append(comboBoxText, NULL, "an even longer value");
	gtk_combo_box_set_active(GTK_COMBO_BOX(comboBoxText), 0);
}


void signal_clicked(GtkWidget *const toggleButton, void *const user_data)
{
	GtkComboBoxText *const comboBoxText = GTK_COMBO_BOX_TEXT(user_data);

	GtkRequisition minimal, natural;
	gtk_widget_get_preferred_size(GTK_WIDGET(comboBoxText), &minimal, &natural);
	printf("old sizes: %dx%d; %dx%d\n", minimal.width, minimal.height, natural.width, natural.height);

	switch ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(toggleButton) ) ) {
		case FALSE:
			set_model_narrow(comboBoxText);
		break;                    
		case TRUE:                
			set_model_wide(comboBoxText);
		break;
	}

	gtk_widget_get_preferred_size(GTK_WIDGET(comboBoxText), &minimal, &natural);
	printf("new sizes: %dx%d; %dx%d\n\n", minimal.width, minimal.height, natural.width, natural.height);
}

int main(int argc, char **argv)
{
	gtk_init(&argc, &argv);

	GtkWidget *const comboBoxText = gtk_combo_box_text_new();
	set_model_narrow( GTK_COMBO_BOX_TEXT(comboBoxText) );

	GtkWidget *const toggleButton = gtk_toggle_button_new_with_label("Click");
	g_signal_connect(G_OBJECT(toggleButton), "clicked", G_CALLBACK(signal_clicked), comboBoxText);

	GtkWidget *const grid = gtk_grid_new();
	gtk_orientable_set_orientation(GTK_ORIENTABLE(grid), GTK_ORIENTATION_VERTICAL);
	gtk_container_add(GTK_CONTAINER(grid), toggleButton);
	gtk_container_add(GTK_CONTAINER(grid), comboBoxText);

	GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
	gtk_container_add(GTK_CONTAINER(window), grid);
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
	gtk_widget_show_all(window);
	gtk_widget_grab_focus(toggleButton);

	gtk_main();
}
