Add a test for model-based popovers

https://bugzilla.gnome.org/show_bug.cgi?id=723014
This commit is contained in:
Matthias Clasen 2014-01-26 08:02:28 -05:00
parent 2fea2d4dbd
commit 59099cd6e7
3 changed files with 192 additions and 1 deletions

View File

@ -143,7 +143,9 @@ noinst_PROGRAMS = $(TEST_PROGS) \
testtitlebar \
testsplitheaders \
testactionbar \
testwindowsize
testwindowsize \
testpopover \
$(NULL)
if USE_X11
noinst_PROGRAMS += testerrors

118
tests/popover.ui Normal file
View File

@ -0,0 +1,118 @@
<interface>
<menu id="menu">
<section>
<item>
<attribute name="label">No action</attribute>
<attribute name="action">action1</attribute>
</item>
<item>
<attribute name="label">Toggle</attribute>
<attribute name="action">top.action2</attribute>
<attribute name="hidden-when">action-missing</attribute>
</item>
<item>
<attribute name="label">Another Toggle</attribute>
<attribute name="action">top.action2a</attribute>
</item>
</section>
<section>
<attribute name="label">Middle Section</attribute>
<item>
<attribute name="label">Radio 1</attribute>
<attribute name="action">top.action3</attribute>
<attribute name="target">three</attribute>
</item>
<item>
<attribute name="label">Radio 2</attribute>
<attribute name="action">top.action3</attribute>
<attribute name="target">four</attribute>
</item>
</section>
<submenu>
<attribute name="label">Submenu 1</attribute>
<section>
<attribute name="label">5555</attribute>
<item>
<attribute name="label">Item 5</attribute>
<attribute name="action">top.action5</attribute>
</item>
<item>
<attribute name="label">Item 5a</attribute>
<attribute name="action">top.action5</attribute>
</item>
<item>
<attribute name="label">Item 5b</attribute>
<attribute name="action">top.action5</attribute>
</item>
<item>
<attribute name="label">Item 5c</attribute>
<attribute name="action">top.action5</attribute>
</item>
<item>
<attribute name="label">Item 5d</attribute>
<attribute name="action">top.action5</attribute>
</item>
</section>
<section>
<attribute name="label">6666</attribute>
<item>
<attribute name="label">Item 6</attribute>
<attribute name="action">top.action6</attribute>
</item>
<item>
<attribute name="label">Item 6a</attribute>
<attribute name="action">top.action6</attribute>
</item>
<item>
<attribute name="label">Item 6b</attribute>
<attribute name="action">top.action6</attribute>
</item>
<item>
<attribute name="label">Item 6c</attribute>
<attribute name="action">top.action6</attribute>
</item>
<item>
<attribute name="label">Item 6d</attribute>
<attribute name="action">top.action6</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">Submenu 2</attribute>
<item>
<attribute name="label">Item 7</attribute>
<attribute name="action">top.action7</attribute>
</item>
<submenu>
<attribute name="label">Subsubmenu</attribute>
<attribute name="icon">preferences-desktop-font</attribute>
<item>
<attribute name="label">Item 8</attribute>
<attribute name="action">action8</attribute>
</item>
<section>
<item>
<attribute name="label">Item 9</attribute>
<attribute name="action">top.action9</attribute>
</item>
<item>
<attribute name="label">Item 10</attribute>
<attribute name="action">top.action10</attribute>
</item>
</section>
</submenu>
</submenu>
<section>
<attribute name="label">End Section</attribute>
<item>
<attribute name="label">Another Item 9</attribute>
<attribute name="action">top.action9</attribute>
<attribute name="icon">preferences-desktop-font</attribute>
</item>
<item>
<attribute name="label">Another Item 10</attribute>
<attribute name="action">top.action10</attribute>
</item>
</section>
</menu>
</interface>

71
tests/testpopover.c Normal file
View File

@ -0,0 +1,71 @@
#include <gtk/gtk.h>
static void
activate (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_print ("%s activated\n", g_action_get_name (G_ACTION (action)));
}
static GActionEntry entries[] = {
{ "action1", activate, NULL, NULL, NULL },
{ "action2", NULL, NULL, "true", NULL },
{ "action2a", NULL, NULL, "false", NULL },
{ "action3", NULL, "s", "'three'", NULL },
{ "action4", activate, NULL, NULL, NULL },
{ "action5", activate, NULL, NULL, NULL },
{ "action6", activate, NULL, NULL, NULL },
{ "action7", activate, NULL, NULL, NULL },
{ "action8", activate, NULL, NULL, NULL },
{ "action9", activate, NULL, NULL, NULL },
{ "action10", activate, NULL, NULL, NULL }
};
int main (int argc, char *argv[])
{
GtkWidget *win;
GtkWidget *button;
GtkBuilder *builder;
GMenuModel *model;
GtkWidget *popover;
GSimpleActionGroup *actions;
GtkWidget *box;
gtk_init (&argc, &argv);
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (win), 400, 600);
actions = g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), NULL);
gtk_widget_insert_action_group (win, "top", G_ACTION_GROUP (actions));
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_container_add (GTK_CONTAINER (win), box);
button = gtk_button_new_with_label ("Pop");
g_object_set (button, "margin", 10, NULL);
gtk_widget_set_halign (button, GTK_ALIGN_END);
gtk_widget_set_valign (button, GTK_ALIGN_START);
gtk_container_add (GTK_CONTAINER (box), button);
builder = gtk_builder_new_from_file ("popover.ui");
model = (GMenuModel *)gtk_builder_get_object (builder, "menu");
popover = gtk_popover_new_from_model (button, model);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_show), popover);
button = gtk_menu_button_new ();
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), model);
g_object_set (button, "margin", 10, NULL);
gtk_widget_set_halign (button, GTK_ALIGN_END);
gtk_widget_set_valign (button, GTK_ALIGN_START);
gtk_container_add (GTK_CONTAINER (box), button);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}