// A scroll on the icon
void cd_xkbd_set_prev_next_group (int iDelta)
{
	XklState *state = xkl_engine_get_current_state (myData.pEngine);

	cd_debug ("keyboard current state : %d;%d +%d", state->group, state->indicators, iDelta);
	
	int i = 0, n = xkl_engine_get_num_groups (myData.pEngine);
	g_return_if_fail (n > 0);
	int iCurrentGroup = MAX (0, MIN (n-1, state->group));  // on blinde car libxklavier peut bugger en 64bits.
	const gchar **pGroupNames = xkl_engine_get_groups_names (myData.pEngine);
	do  // on passe au groupe suivant/precedent en sautant les faux (-).
	{
		i ++;
		iCurrentGroup += iDelta;  // xkl_engine_get_next_group ne marche pas.
		if (iCurrentGroup == n)
			iCurrentGroup = 0;
		else if (iCurrentGroup < 0)
			iCurrentGroup = n - 1;
	} while (i < n && (pGroupNames[iCurrentGroup] == NULL || *pGroupNames[iCurrentGroup] == '-'));
	
	state->group = iCurrentGroup;
	cd_debug ("keyboard new state : %d", state->group);
	xkl_engine_allow_one_switch_to_secondary_group (myData.pEngine);  // sert a quoi ??
	
	Window Xid = xkl_engine_get_current_window (myData.pEngine);
	xkl_engine_save_state (myData.pEngine, Xid, state);
	xkl_engine_lock_group (myData.pEngine, state->group);  // sert a quoi ??
}
// If the group changes
static void _state_changed (XklEngine *pEngine, XklEngineStateChange type,
                            gint iGroup, gboolean bRestore)
{
	XklState *state = xkl_engine_get_current_state (myData.pEngine);
	cd_debug ("State Changed: %d -> %d (%d) ; %d", myData.iCurrentGroup, state->group, iGroup, state->indicators);

	if (type == GROUP_CHANGED && myData.iCurrentGroup != state->group) // new keyboard layout
	{
		gchar *cShortGroupName = NULL;
		const gchar *cCurrentGroup = NULL;

		// Get the current num group
		guint n = xkl_engine_get_num_groups (myData.pEngine);
		g_return_if_fail (n > 0);

		int iNewGroup = MAX (0, MIN (n-1, state->group));  // workaround for 64bits to avoid strange numbers in 'state'
		const gchar **pGroupNames = xkl_engine_get_groups_names (myData.pEngine);
		g_return_if_fail (pGroupNames != NULL);

		cCurrentGroup = pGroupNames[iNewGroup];
		g_return_if_fail (cCurrentGroup != NULL);

		cd_debug (" group name : %s (%d groups)", cCurrentGroup, n);

		// build the displayed group name
		cShortGroupName = g_strndup (cCurrentGroup, myConfig.iNLetters);
		int index = 0;
		int i;
		for (i = 0; i < state->group; i ++)  // look for groups before us having the same name.
		{
			if (strncmp (cCurrentGroup, pGroupNames[i], myConfig.iNLetters) == 0)
				index ++;
		}
		if (index != 0)  // add a number if several groups have the same name.
		{
			gchar *tmp = cShortGroupName;
			cShortGroupName = g_strdup_printf ("%s%d", cShortGroupName, index+1);
			g_free (tmp);
		}

		myData.iCurrentGroup = state->group;
		cd_xkbd_update_icon (cCurrentGroup, cShortGroupName, TRUE);
		g_free (cShortGroupName);
	}
	else if (type == INDICATORS_CHANGED)
		cd_debug ("Indicators changed"); // according to libxklavier/tests/test_monitor.c, it should work... but I have to miss something
}
//\___________ Define here the action to be taken when the user left-clicks on your icon or on its subdock or your desklet. The icon and the container that were clicked are available through the macros CD_APPLET_CLICKED_ICON and CD_APPLET_CLICKED_CONTAINER. CD_APPLET_CLICKED_ICON may be NULL if the user clicked in the container but out of icons.
CD_APPLET_ON_CLICK_BEGIN
	cd_xkbd_set_prev_next_group (+1);
CD_APPLET_ON_CLICK_END


//\___________ Define here the entries you want to add to the menu when the user right-clicks on your icon or on its subdock or your desklet. The icon and the container that were clicked are available through the macros CD_APPLET_CLICKED_ICON and CD_APPLET_CLICKED_CONTAINER. CD_APPLET_CLICKED_ICON may be NULL if the user clicked in the container but out of icons. The menu where you can add your entries is available throught the macro CD_APPLET_MY_MENU; you can add sub-menu to it if you want.
static void _select_group (GtkMenuItem *menu_item, gpointer *data)
{
	int iNumGroup = GPOINTER_TO_INT (data);
	cd_xkbd_set_group (iNumGroup);
}
CD_APPLET_ON_BUILD_MENU_BEGIN
	GtkWidget *pSubMenu = CD_APPLET_CREATE_MY_SUB_MENU ();
		CD_APPLET_ADD_ABOUT_IN_MENU (pSubMenu);
		
		const XklEngine *pEngine = xkl_engine_get_instance (cairo_dock_get_Xdisplay ());
		const gchar **pGroupNames = xkl_engine_get_groups_names (pEngine);
		int i;
		for (i = 0; pGroupNames[i] != NULL && *pGroupNames[i] != '-'; i ++)
		{
			CD_APPLET_ADD_IN_MENU_WITH_DATA (pGroupNames[i], _select_group, pSubMenu, GINT_TO_POINTER (i));
		}
CD_APPLET_ON_BUILD_MENU_END


CD_APPLET_ON_SCROLL_BEGIN
	cd_xkbd_set_prev_next_group (CD_APPLET_SCROLL_UP ? +1 : -1);
CD_APPLET_ON_SCROLL_END