2009-10-13 11:14:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Intel Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU Lesser General Public License,
|
|
|
|
* version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <gmodule.h>
|
|
|
|
|
|
|
|
#include <clutter/clutter.h>
|
2009-11-10 12:21:26 +00:00
|
|
|
#include <cogl/cogl.h>
|
2011-05-16 15:04:27 +00:00
|
|
|
#include <cogl-pango/cogl-pango.h>
|
2009-10-13 11:14:05 +00:00
|
|
|
|
2009-11-13 15:27:18 +00:00
|
|
|
#define INSTRUCTIONS \
|
|
|
|
"Press v\t\342\236\236\tSwitch horizontal/vertical\n" \
|
2010-03-17 15:16:09 +00:00
|
|
|
"Press h\t\342\236\236\tSwitch homogeneous\n" \
|
2009-11-13 15:27:18 +00:00
|
|
|
"Press p\t\342\236\236\tSwitch pack start/end\n" \
|
|
|
|
"Press s\t\342\236\236\tIncrement spacing (up to 12px)\n" \
|
2009-12-13 01:07:44 +00:00
|
|
|
"Press a\t\342\236\236\tSwitch animations on/off\n" \
|
2009-11-13 15:27:18 +00:00
|
|
|
"Press q\t\342\236\236\tQuit"
|
|
|
|
|
2009-10-13 11:14:05 +00:00
|
|
|
static ClutterActor *hover_actor = NULL;
|
2009-12-04 17:39:04 +00:00
|
|
|
static ClutterActor *box = NULL;
|
|
|
|
static ClutterActor *label = NULL;
|
2009-11-10 12:21:26 +00:00
|
|
|
static guint last_index = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_paint (ClutterActor *actor,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
guint index_ = GPOINTER_TO_UINT (user_data);
|
|
|
|
gchar *text = g_strdup_printf ("%u", index_);
|
|
|
|
ClutterActorBox alloc = { 0, };
|
|
|
|
CoglColor color;
|
|
|
|
gint layout_width, layout_height;
|
|
|
|
gfloat width, height;
|
|
|
|
PangoLayout *layout;
|
|
|
|
|
|
|
|
clutter_actor_get_allocation_box (actor, &alloc);
|
|
|
|
clutter_actor_box_get_size (&alloc, &width, &height);
|
|
|
|
|
|
|
|
layout = clutter_actor_create_pango_layout (actor, text);
|
|
|
|
pango_layout_get_size (layout, &layout_width, &layout_height);
|
|
|
|
|
2010-09-03 15:55:12 +00:00
|
|
|
cogl_color_init_from_4ub (&color, 0, 0, 0, 255);
|
2009-11-10 12:21:26 +00:00
|
|
|
cogl_pango_render_layout (layout,
|
|
|
|
(width - (layout_width / 1024)) / 2,
|
|
|
|
(height - (layout_height / 1024)) / 2,
|
|
|
|
&color, 0);
|
|
|
|
|
|
|
|
g_object_unref (layout);
|
|
|
|
g_free (text);
|
|
|
|
}
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
enter_event (ClutterActor *actor,
|
|
|
|
ClutterEvent *event,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
clutter_rectangle_set_border_width (CLUTTER_RECTANGLE (actor), 2);
|
2010-10-11 16:45:30 +00:00
|
|
|
clutter_rectangle_set_border_color (CLUTTER_RECTANGLE (actor),
|
|
|
|
CLUTTER_COLOR_Black);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
hover_actor = actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
leave_event (ClutterActor *actor,
|
|
|
|
ClutterEvent *event,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
clutter_rectangle_set_border_width (CLUTTER_RECTANGLE (actor), 0);
|
|
|
|
|
|
|
|
hover_actor = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
button_release_event (ClutterActor *actor,
|
|
|
|
ClutterEvent *event,
|
2009-12-04 17:39:04 +00:00
|
|
|
ClutterBoxLayout *layout)
|
2009-10-13 11:14:05 +00:00
|
|
|
{
|
|
|
|
gboolean xfill, yfill;
|
|
|
|
ClutterBoxAlignment xalign, yalign;
|
|
|
|
gint button;
|
|
|
|
|
|
|
|
button = clutter_event_get_button (event);
|
|
|
|
|
2012-01-27 09:47:25 +00:00
|
|
|
if (button == CLUTTER_BUTTON_PRIMARY)
|
2009-10-13 11:14:05 +00:00
|
|
|
{
|
2009-12-04 17:39:04 +00:00
|
|
|
clutter_box_layout_get_fill (layout, actor, &xfill, &yfill);
|
|
|
|
clutter_box_layout_set_fill (layout, actor,
|
2009-10-13 11:14:05 +00:00
|
|
|
xfill ? FALSE : TRUE,
|
|
|
|
yfill ? FALSE : TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-04 17:39:04 +00:00
|
|
|
clutter_box_layout_get_alignment (layout, actor, &xalign, &yalign);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
if (xalign < 2)
|
|
|
|
xalign += 1;
|
|
|
|
else
|
|
|
|
xalign = 0;
|
|
|
|
|
|
|
|
if (yalign < 2)
|
|
|
|
yalign += 1;
|
|
|
|
else
|
|
|
|
yalign = 0;
|
|
|
|
|
2009-12-04 17:39:04 +00:00
|
|
|
clutter_box_layout_set_alignment (layout, actor, xalign, yalign);
|
2009-10-13 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-04 17:39:04 +00:00
|
|
|
add_actor (ClutterBoxLayout *layout,
|
2009-11-10 12:21:26 +00:00
|
|
|
guint index_)
|
2009-10-13 11:14:05 +00:00
|
|
|
{
|
|
|
|
ClutterActor *rect;
|
|
|
|
static gboolean expand = TRUE;
|
2010-10-11 16:45:30 +00:00
|
|
|
ClutterColor color;
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
clutter_color_from_hls (&color,
|
|
|
|
g_random_double_range (0.0, 360.0),
|
|
|
|
0.5,
|
|
|
|
0.5);
|
2010-10-11 16:45:30 +00:00
|
|
|
color.alpha = 255;
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
rect = clutter_rectangle_new_with_color (&color);
|
|
|
|
clutter_actor_set_size (rect, 32, 64);
|
2009-12-04 17:39:04 +00:00
|
|
|
clutter_box_layout_pack (layout, rect, expand,
|
2009-10-13 11:14:05 +00:00
|
|
|
FALSE, /* x-fill */
|
|
|
|
FALSE, /* y-fill */
|
|
|
|
CLUTTER_BOX_ALIGNMENT_CENTER,
|
|
|
|
CLUTTER_BOX_ALIGNMENT_CENTER);
|
|
|
|
|
|
|
|
clutter_actor_set_reactive (rect, TRUE);
|
2009-11-10 12:21:26 +00:00
|
|
|
g_signal_connect_after (rect, "paint",
|
|
|
|
G_CALLBACK (on_paint),
|
|
|
|
GUINT_TO_POINTER (index_));
|
2009-10-13 11:14:05 +00:00
|
|
|
g_signal_connect (rect, "enter-event", G_CALLBACK (enter_event), NULL);
|
|
|
|
g_signal_connect (rect, "leave-event", G_CALLBACK (leave_event), NULL);
|
|
|
|
g_signal_connect (rect, "button-release-event",
|
|
|
|
G_CALLBACK (button_release_event),
|
2009-12-04 17:39:04 +00:00
|
|
|
layout);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
expand = !expand;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
key_release_cb (ClutterActor *actor,
|
|
|
|
ClutterEvent *event,
|
|
|
|
ClutterBoxLayout *layout)
|
|
|
|
{
|
|
|
|
gboolean toggle;
|
|
|
|
guint spacing;
|
|
|
|
|
|
|
|
switch (clutter_event_get_key_symbol (event))
|
|
|
|
{
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_a:
|
2009-12-13 01:07:44 +00:00
|
|
|
toggle = clutter_box_layout_get_use_animations (layout);
|
|
|
|
clutter_box_layout_set_use_animations (layout, !toggle);
|
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_v:
|
2012-03-27 16:08:59 +00:00
|
|
|
{
|
|
|
|
ClutterOrientation orientation;
|
|
|
|
|
|
|
|
orientation = clutter_box_layout_get_orientation (layout);
|
|
|
|
|
|
|
|
if (orientation == CLUTTER_ORIENTATION_HORIZONTAL)
|
|
|
|
orientation = CLUTTER_ORIENTATION_VERTICAL;
|
|
|
|
else
|
|
|
|
orientation = CLUTTER_ORIENTATION_HORIZONTAL;
|
|
|
|
|
|
|
|
clutter_box_layout_set_orientation (layout, orientation);
|
|
|
|
}
|
2009-10-13 11:14:05 +00:00
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_h:
|
2010-03-17 15:16:09 +00:00
|
|
|
toggle = clutter_box_layout_get_homogeneous (layout);
|
|
|
|
clutter_box_layout_set_homogeneous (layout, !toggle);
|
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_p:
|
2009-10-13 11:14:05 +00:00
|
|
|
toggle = clutter_box_layout_get_pack_start (layout);
|
|
|
|
clutter_box_layout_set_pack_start (layout, !toggle);
|
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_s:
|
2009-10-13 11:14:05 +00:00
|
|
|
spacing = clutter_box_layout_get_spacing (layout);
|
|
|
|
|
|
|
|
if (spacing > 12)
|
|
|
|
spacing = 0;
|
|
|
|
else
|
|
|
|
spacing++;
|
|
|
|
|
|
|
|
clutter_box_layout_set_spacing (layout, spacing);
|
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_plus:
|
2009-11-10 12:21:26 +00:00
|
|
|
add_actor (layout, last_index++);
|
2009-10-13 11:14:05 +00:00
|
|
|
break;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_q:
|
2009-11-13 15:27:18 +00:00
|
|
|
clutter_main_quit ();
|
|
|
|
break;
|
|
|
|
|
2009-10-13 11:14:05 +00:00
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stage_size_changed_cb (ClutterActor *stage,
|
|
|
|
const ClutterActorBox *allocation,
|
|
|
|
ClutterAllocationFlags flags,
|
2009-12-04 17:39:04 +00:00
|
|
|
gpointer dummy G_GNUC_UNUSED)
|
2009-10-13 11:14:05 +00:00
|
|
|
{
|
|
|
|
gfloat width, height;
|
|
|
|
|
|
|
|
clutter_actor_box_get_size (allocation, &width, &height);
|
|
|
|
clutter_actor_set_size (box, width - 100, height - 100);
|
2009-12-04 17:39:04 +00:00
|
|
|
|
|
|
|
clutter_actor_set_y (label,
|
|
|
|
height - clutter_actor_get_height (label) - 8);
|
2009-10-13 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT int
|
|
|
|
test_box_layout_main (int argc, char *argv[])
|
|
|
|
{
|
2009-12-04 17:39:04 +00:00
|
|
|
ClutterActor *stage;
|
2009-10-13 11:14:05 +00:00
|
|
|
ClutterLayoutManager *layout;
|
|
|
|
gint i;
|
|
|
|
|
2011-02-22 00:19:35 +00:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
|
|
return 1;
|
2009-10-13 11:14:05 +00:00
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
stage = clutter_stage_new ();
|
2009-10-13 11:14:05 +00:00
|
|
|
clutter_stage_set_title (CLUTTER_STAGE (stage), "Box Layout");
|
|
|
|
clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
|
|
|
|
clutter_actor_set_size (stage, 640, 480);
|
2011-11-09 13:41:15 +00:00
|
|
|
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
layout = clutter_box_layout_new ();
|
|
|
|
|
|
|
|
box = clutter_box_new (layout);
|
|
|
|
clutter_container_add_actor (CLUTTER_CONTAINER (stage), box);
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
2009-11-10 12:21:26 +00:00
|
|
|
add_actor (CLUTTER_BOX_LAYOUT (layout), last_index++);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
|
|
|
g_signal_connect (stage, "key-release-event",
|
|
|
|
G_CALLBACK (key_release_cb),
|
|
|
|
layout);
|
|
|
|
g_signal_connect (stage, "allocation-changed",
|
|
|
|
G_CALLBACK (stage_size_changed_cb),
|
2009-12-04 17:39:04 +00:00
|
|
|
NULL);
|
2009-10-13 11:14:05 +00:00
|
|
|
|
2009-11-13 15:27:18 +00:00
|
|
|
label = clutter_text_new_with_text ("Sans 12px", INSTRUCTIONS);
|
|
|
|
clutter_container_add_actor (CLUTTER_CONTAINER (stage), label);
|
|
|
|
clutter_actor_set_y (label,
|
|
|
|
480 - clutter_actor_get_height (label) - 8);
|
|
|
|
|
2009-10-13 11:14:05 +00:00
|
|
|
clutter_actor_show (stage);
|
|
|
|
|
|
|
|
clutter_main ();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2011-11-09 13:41:15 +00:00
|
|
|
|
|
|
|
G_MODULE_EXPORT const char *
|
|
|
|
test_box_layout_describe (void)
|
|
|
|
{
|
|
|
|
return "BoxLayout layout manager example";
|
|
|
|
}
|