summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/demo2layout.c
blob: ca5475d1a599f4278e48ec2232d1c4d575a89fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "demo2layout.h"
#include "four_point_transform.h"

struct _Demo2Layout
{
  GtkLayoutManager parent_instance;

  float position;
  float offset;
};

struct _Demo2LayoutClass
{
  GtkLayoutManagerClass parent_class;
};

G_DEFINE_TYPE (Demo2Layout, demo2_layout, GTK_TYPE_LAYOUT_MANAGER)

static void
demo2_layout_measure (GtkLayoutManager *layout_manager,
                      GtkWidget        *widget,
                      GtkOrientation    orientation,
                      int               for_size,
                      int              *minimum,
                      int              *natural,
                      int              *minimum_baseline,
                      int              *natural_baseline)
{
  GtkWidget *child;
  int minimum_size = 0;
  int natural_size = 0;

  for (child = gtk_widget_get_first_child (widget);
       child != NULL;
       child = gtk_widget_get_next_sibling (child))
    {
      int child_min = 0, child_nat = 0;

      if (!gtk_widget_should_layout (child))
        continue;

      gtk_widget_measure (child, orientation, -1,
                          &child_min, &child_nat,
                          NULL, NULL);
      minimum_size = MAX (minimum_size, child_min);
      natural_size = MAX (natural_size, child_nat);
    }

  *minimum = minimum_size;
  *natural = 3 * natural_size;
}


#define RADIANS(angle) ((angle)*M_PI/180.0);

/* Spherical coordinates */
#define SX(r,t,p) ((r) * sin (t) * cos (p))
#define SZ(r,t,p) ((r) * sin (t) * sin (p))
#define SY(r,t,p) ((r) * cos (t))

static double
map_offset (double x)
{
  x = fmod (x, 180.0);
  if (x < 0.0)
    x += 180.0;

  return x;
}

static void
demo2_layout_allocate (GtkLayoutManager *layout_manager,
                       GtkWidget        *widget,
                       int               width,
                       int               height,
                       int               baseline)
{
  GtkWidget *child;
  GtkRequisition child_req;
  int i, j, k;
  float x0, y0;
  float w, h;
  graphene_point3d_t p1, p2, p3, p4;
  graphene_point3d_t q1, q2, q3, q4;
  double t_1, t_2, p_1, p_2;
  double r;
  graphene_matrix_t m;
  GskTransform *transform;
  double position = DEMO2_LAYOUT (layout_manager)->position;
  double offset = DEMO2_LAYOUT (layout_manager)->offset;

  /* for simplicity, assume all children are the same size */
  gtk_widget_get_preferred_size (gtk_widget_get_first_child (widget), &child_req, NULL);
  w = child_req.width;
  h = child_req.height;

  r = 300;
  x0 = y0 = 300;

  for (child = gtk_widget_get_first_child (widget), i = 0;
       child != NULL;
       child = gtk_widget_get_next_sibling (child), i++)
    {
      j = i / 36;
      k = i % 36;

      gtk_widget_set_child_visible (child, FALSE);

      graphene_point3d_init (&p1, w, h, 1.);
      graphene_point3d_init (&p2, w, 0., 1.);
      graphene_point3d_init (&p3, 0., 0., 1.);
      graphene_point3d_init (&p4, 0., h, 1.);

      t_1 = RADIANS (map_offset (offset + 10 * j));
      t_2 = RADIANS (map_offset (offset + 10 * (j + 1)));
      p_1 = RADIANS (position + 10 * k);
      p_2 = RADIANS (position + 10 * (k + 1));

      if (t_2 < t_1)
        continue;

      if (SZ (r, t_1, p_1) > 0 ||
          SZ (r, t_2, p_1) > 0 ||
          SZ (r, t_1, p_2) > 0 ||
          SZ (r, t_2, p_2) > 0)
        continue;

      gtk_widget_set_child_visible (child, TRUE);

      graphene_point3d_init (&q1, x0 + SX (r, t_1, p_1), y0 + SY (r, t_1, p_1), SZ (r, t_1, p_1));
      graphene_point3d_init (&q2, x0 + SX (r, t_2, p_1), y0 + SY (r, t_2, p_1), SZ (r, t_2, p_1));
      graphene_point3d_init (&q3, x0 + SX (r, t_2, p_2), y0 + SY (r, t_2, p_2), SZ (r, t_2, p_2));
      graphene_point3d_init (&q4, x0 + SX (r, t_1, p_2), y0 + SY (r, t_1, p_2), SZ (r, t_1, p_2));

      /* Get a matrix that moves p1 -> q1, p2 -> q2, ... */
      perspective_3d (&p1, &p2, &p3, &p4,
                      &q1, &q2, &q3, &q4,
                      &m);

      transform = gsk_transform_matrix (NULL, &m);

      /* Since our matrix was built for transforming points with z = 1,
       * prepend a translation to the z = 1 plane.
       */
      transform = gsk_transform_translate_3d (transform,
                                              &GRAPHENE_POINT3D_INIT (0, 0, 1));

      gtk_widget_allocate (child, w, h, -1, transform);
    }
}

static GtkSizeRequestMode
demo2_layout_get_request_mode (GtkLayoutManager *layout_manager,
                               GtkWidget        *widget)
{
  return GTK_SIZE_REQUEST_CONSTANT_SIZE;
}

static void
demo2_layout_class_init (Demo2LayoutClass *klass)
{
  GtkLayoutManagerClass *layout_class = GTK_LAYOUT_MANAGER_CLASS (klass);

  layout_class->get_request_mode = demo2_layout_get_request_mode;
  layout_class->measure = demo2_layout_measure;
  layout_class->allocate = demo2_layout_allocate;
}

static void
demo2_layout_init (Demo2Layout *self)
{
}

GtkLayoutManager *
demo2_layout_new (void)
{
  return g_object_new (DEMO2_TYPE_LAYOUT, NULL);
}

void
demo2_layout_set_position (Demo2Layout *layout,
                           float        position)
{
  layout->position = position;
}

float
demo2_layout_get_position (Demo2Layout *layout)
{
  return layout->position;
}

void
demo2_layout_set_offset (Demo2Layout *layout,
                         float        offset)
{
  layout->offset = offset;
}

float
demo2_layout_get_offset (Demo2Layout *layout)
{
  return layout->offset;
}