/
mx-dialog.c
952 lines (794 loc) · 26.5 KB
/
mx-dialog.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*
* mx-dialog.c
*
* Copyright 2010 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.
*
* Written by: Chris Lord <chris@linux.intel.com>
* Iain Holmes <iain@linux.intel.com>
*
*/
/**
* SECTION:mx-dialog
* @short_description: a modal, single-widget container
*
* #MxDialog is a single-widget container. When presented, it performs a
* suitable transition animation and blocks input to the actors beneath it
* until it is hidden again.
*
* It also allows actions to be added to it, which will be represented as
* buttons, using #MxButton.
*
* Since: 1.2
*/
#include "mx-dialog.h"
#include "mx-button-group.h"
#include "mx-private.h"
#include "mx-stylable.h"
#include "mx-utils.h"
static void mx_stylable_iface_init (MxStylableIface *iface);
static void mx_focusable_iface_init (MxFocusableIface *iface);
G_DEFINE_TYPE_WITH_CODE (MxDialog, mx_dialog, MX_TYPE_BIN,
G_IMPLEMENT_INTERFACE (MX_TYPE_STYLABLE,
mx_stylable_iface_init)
G_IMPLEMENT_INTERFACE (MX_TYPE_FOCUSABLE,
mx_focusable_iface_init))
#define DIALOG_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), MX_TYPE_DIALOG, MxDialogPrivate))
typedef struct {
MxAction *action;
ClutterActor *button;
} MxDialogAction;
struct _MxDialogPrivate
{
guint visible : 1;
guint child_has_focus : 1;
guint transition_time;
gfloat angle;
ClutterTimeline *timeline;
ClutterAlpha *alpha;
gfloat zoom;
/* Dialog-specific variables */
ClutterActor *background;
ClutterActor *button_box;
guint spacing;
GList *actions;
};
static void mx_dialog_show (ClutterActor *self);
static void mx_dialog_hide (ClutterActor *self);
static MxFocusable *
mx_dialog_move_focus (MxFocusable *focusable,
MxFocusDirection direction,
MxFocusable *from)
{
ClutterActor *child;
MxFocusHint hint = MX_FOCUS_HINT_PRIOR;
MxDialog *self = MX_DIALOG (focusable);
MxDialogPrivate *priv = self->priv;
child = mx_bin_get_child (MX_BIN (focusable));
if (child && !MX_IS_FOCUSABLE (child))
child = NULL;
focusable = NULL;
switch (direction)
{
case MX_FOCUS_DIRECTION_PREVIOUS:
hint = MX_FOCUS_HINT_LAST;
case MX_FOCUS_DIRECTION_UP:
if (!priv->child_has_focus && child)
{
priv->child_has_focus = TRUE;
focusable = mx_focusable_accept_focus (MX_FOCUSABLE (child), hint);
}
break;
case MX_FOCUS_DIRECTION_NEXT:
hint = MX_FOCUS_HINT_FIRST;
case MX_FOCUS_DIRECTION_DOWN:
if (priv->child_has_focus && priv->actions)
{
priv->child_has_focus = FALSE;
focusable =
mx_focusable_accept_focus (MX_FOCUSABLE (priv->button_box), hint);
}
default:
break;
}
if (focusable || MX_FOCUS_DIRECTION_OUT)
return focusable;
else
return mx_focusable_accept_focus (MX_FOCUSABLE (self), hint);
}
static MxFocusable *
mx_dialog_accept_focus (MxFocusable *focusable, MxFocusHint hint)
{
ClutterActor *child;
MxDialog *self = MX_DIALOG (focusable);
MxDialogPrivate *priv = self->priv;
child = mx_bin_get_child (MX_BIN (focusable));
if (child && !MX_IS_FOCUSABLE (child))
{
hint = MX_FOCUS_HINT_PRIOR;
child = NULL;
}
focusable = NULL;
switch (hint)
{
default:
case MX_FOCUS_HINT_PRIOR:
if (child && priv->child_has_focus)
focusable = mx_focusable_accept_focus (MX_FOCUSABLE (child), hint);
if (focusable)
break;
case MX_FOCUS_HINT_LAST:
priv->child_has_focus = FALSE;
if (priv->actions)
focusable = mx_focusable_accept_focus (MX_FOCUSABLE (priv->button_box),
hint);
if (focusable)
break;
case MX_FOCUS_HINT_FIRST:
priv->child_has_focus = TRUE;
if (child)
focusable = mx_focusable_accept_focus (MX_FOCUSABLE (child), hint);
break;
}
/* If we don't have a focusable child, we still return ourselves so
* that a dialog can't lose focus.
*/
return focusable ? focusable : MX_FOCUSABLE (self);
}
static void
mx_focusable_iface_init (MxFocusableIface *iface)
{
iface->move_focus = mx_dialog_move_focus;
iface->accept_focus = mx_dialog_accept_focus;
}
static void
mx_stylable_iface_init (MxStylableIface *iface)
{
static gboolean is_initialized = FALSE;
if (G_UNLIKELY (!is_initialized))
{
GParamSpec *pspec;
is_initialized = TRUE;
pspec = g_param_spec_uint ("x-mx-spacing",
"Spacing",
"The size of the spacing",
0, G_MAXUINT, 12,
MX_PARAM_READWRITE);
mx_stylable_iface_install_property (iface, MX_TYPE_DIALOG, pspec);
}
}
static void
mx_dialog_dispose (GObject *object)
{
MxDialog *self = MX_DIALOG (object);
MxDialogPrivate *priv = self->priv;
if (priv->background)
{
clutter_actor_remove_child (CLUTTER_ACTOR (object), priv->background);
priv->background = NULL;
}
if (priv->button_box)
{
clutter_actor_remove_child (CLUTTER_ACTOR (object), priv->button_box);
priv->button_box = NULL;
}
G_OBJECT_CLASS (mx_dialog_parent_class)->dispose (object);
}
static void
mx_dialog_finalize (GObject *object)
{
MxDialog *self = (MxDialog *) object;
MxDialogPrivate *priv = self->priv;
if (priv->actions)
{
GList *a;
for (a = priv->actions; a; a = a->next)
g_slice_free (MxDialogAction, a->data);
g_list_free (priv->actions);
}
G_OBJECT_CLASS (mx_dialog_parent_class)->finalize (object);
}
static void
mx_dialog_get_preferred_width (ClutterActor *actor,
gfloat for_height,
gfloat *min_width_p,
gfloat *nat_width_p)
{
ClutterActor *child;
MxPadding padding, ipadding;
gfloat child_width[2], button_width[2];
MxDialog *self = MX_DIALOG (actor);
MxDialogPrivate *priv = self->priv;
mx_widget_get_padding (MX_WIDGET (actor), &padding);
mx_widget_get_padding (MX_WIDGET (priv->background), &ipadding);
/* If we're getting the preferred-width with a height, make
* sure to take off the padding from the height, and the space
* required for the button-box.
*/
if (for_height >= 0)
{
gfloat button_height;
if (priv->actions)
clutter_actor_get_preferred_height (priv->button_box, -1,
NULL, &button_height);
else
button_height = 0;
for_height = MAX (0, for_height - padding.top - padding.bottom -
ipadding.top - ipadding.bottom -
button_height - priv->spacing);
}
/* Get the minimum/preferred width of the child */
child = mx_bin_get_child (MX_BIN (actor));
if (child)
clutter_actor_get_preferred_width (child, for_height,
&child_width[0], &child_width[1]);
else
child_width[0] = child_width[1] = 0;
/* Get the minimum/preferred width of the button-box */
if (priv->actions)
clutter_actor_get_preferred_width (priv->button_box, -1,
&button_width[0], &button_width[1]);
else
button_width[0] = button_width[1] = 0;
/* Our preferred width is the maximum of the button-box width and the
* child width, plus space for padding.
*/
if (min_width_p)
*min_width_p = MAX (child_width[0], button_width[0]) +
padding.left + padding.right +
ipadding.left + ipadding.right;
if (nat_width_p)
*nat_width_p = MAX (child_width[1], button_width[1]) +
padding.left + padding.right +
ipadding.left + ipadding.right;
}
static void
mx_dialog_get_preferred_height (ClutterActor *actor,
gfloat for_width,
gfloat *min_height_p,
gfloat *nat_height_p)
{
ClutterActor *child;
MxPadding padding, ipadding;
gfloat min_height, nat_height;
MxDialog *self = MX_DIALOG (actor);
MxDialogPrivate *priv = self->priv;
mx_widget_get_padding (MX_WIDGET (actor), &padding);
mx_widget_get_padding (MX_WIDGET (priv->background), &ipadding);
/* Adjust for-width for padding */
if (for_width >= 0)
for_width = MAX (0, for_width - padding.left - padding.right -
ipadding.left - ipadding.right);
/* Calculate the height of the button-box + padding */
if (priv->actions)
clutter_actor_get_preferred_height (priv->button_box, for_width,
&min_height, &nat_height);
else
min_height = nat_height = 0;
min_height += padding.top + padding.bottom + ipadding.top + ipadding.bottom;
nat_height += padding.top + padding.bottom + ipadding.top + ipadding.bottom;
/* If the child exists, add its height + spacing to the result */
child = mx_bin_get_child (MX_BIN (actor));
if (child)
{
clutter_actor_get_preferred_height (child, for_width,
min_height_p, nat_height_p);
if (min_height_p)
*min_height_p += min_height + priv->spacing;
if (nat_height_p)
*nat_height_p += nat_height + priv->spacing;
}
else
{
if (min_height_p)
*min_height_p = min_height;
if (nat_height_p)
*nat_height_p = nat_height;
}
}
static void
mx_dialog_allocate (ClutterActor *actor,
const ClutterActorBox *box,
ClutterAllocationFlags flags)
{
MxPadding padding;
ClutterActor *child;
gfloat button_width, button_height;
ClutterActorBox inner_box, child_box;
MxDialogPrivate *priv = MX_DIALOG (actor)->priv;
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->
allocate (actor, box, flags);
/* Get the available space */
mx_widget_get_padding (MX_WIDGET (actor), &padding);
inner_box.x1 = padding.left;
inner_box.y1 = padding.top;
inner_box.x2 = box->x2 - box->x1 - padding.right;
inner_box.y2 = box->y2 - box->y1 - padding.bottom;
/* Get the padding of the background box */
mx_widget_get_padding (MX_WIDGET (priv->background), &padding);
/* Get the size of the button-box */
if (priv->actions)
{
clutter_actor_get_preferred_size (priv->button_box,
NULL, NULL,
&button_width, &button_height);
button_height += priv->spacing;
}
else
button_width = button_height = 0;
/* Allocate the child */
child = mx_bin_get_child (MX_BIN (actor));
/* Calculate the available space for the child */
child_box = inner_box;
child_box.x1 += padding.left;
child_box.y1 += padding.top;
child_box.x2 -= padding.right;
child_box.y2 -= padding.bottom + button_height;
/* Allocate the child and work out the allocation box for the
* button box.
*/
if (child)
{
gboolean x_fill, y_fill, width;
MxAlign x_align, y_align;
mx_bin_get_fill (MX_BIN (actor), &x_fill, &y_fill);
mx_bin_get_alignment (MX_BIN (actor), &x_align, &y_align);
mx_allocate_align_fill (child, &child_box,
x_align, y_align,
x_fill, y_fill);
width = child_box.x2 - child_box.x1;
/* Adjust the allocation if the button-box is wider than
* the dialog contents.
*/
if (button_width > width)
{
child_box.x1 += (gint)((button_width - width)/2.f);
child_box.x2 += (gint)((button_width - width)/2.f);
}
inner_box = child_box;
inner_box.y2 += button_height;
if (button_width > width)
{
inner_box.x1 = inner_box.x2 - button_width;
child_box.x1 = inner_box.x1;
child_box.x2 = child_box.x1 + width;
}
clutter_actor_allocate (child, &child_box, flags);
child_box.y1 = child_box.y2 + priv->spacing;
if (button_width > width)
child_box.x2 = child_box.x1 + button_width;
else
child_box.x1 = child_box.x2 - button_width;
child_box.y2 = child_box.y1 + button_height - priv->spacing;
}
else
{
child_box.x1 += (child_box.x2 - child_box.x1 - button_width) / 2.f;
child_box.y1 += (child_box.y2 - child_box.y1 - button_height) / 2.f;
child_box.x2 = child_box.x1 + button_width;
child_box.y2 = child_box.y2 + button_height;
inner_box = child_box;
}
/* Allocate the button-box if necessary */
if (priv->actions)
clutter_actor_allocate (priv->button_box, &child_box, flags);
/* Allocate the background */
inner_box.x1 -= padding.left;
inner_box.y1 -= padding.top;
inner_box.x2 += padding.right;
inner_box.y2 += padding.bottom;
clutter_actor_allocate (priv->background, &inner_box, flags);
}
static void
mx_dialog_paint (ClutterActor *actor)
{
ClutterActor *child;
gfloat width, height;
MxDialogPrivate *priv = MX_DIALOG (actor)->priv;
clutter_actor_get_size (actor, &width, &height);
cogl_set_source_color4ub (0, 0, 0, 0x7b);
cogl_rectangle (0, 0, width, height);
cogl_translate (width/2, height/2, 0);
cogl_scale (priv->zoom, priv->zoom, 1.f);
cogl_rotate (priv->angle, 0, 0, 1);
cogl_translate (-width/2, -height/2, 0);
clutter_actor_paint (priv->background);
child = mx_bin_get_child (MX_BIN (actor));
if (child)
clutter_actor_paint (child);
if (priv->actions)
clutter_actor_paint (priv->button_box);
}
static void
mx_dialog_pick (ClutterActor *actor,
const ClutterColor *color)
{
gfloat width, height;
ClutterGeometry geom;
MxDialogPrivate *priv = MX_DIALOG (actor)->priv;
/* Paint a rectangle over our allocation to block input to
* other actors.
*/
clutter_actor_get_allocation_geometry (actor, &geom);
cogl_set_source_color4ub (color->red,
color->green,
color->blue,
color->alpha);
cogl_rectangle (0, 0, geom.width, geom.height);
clutter_actor_get_size (actor, &width, &height);
cogl_translate (width/2, height/2, 0);
cogl_scale (priv->zoom, priv->zoom, 1.f);
cogl_rotate (priv->angle, 0, 0, 1);
cogl_translate (-width/2, -height/2, 0);
/* Chain up */
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->pick (actor, color);
if (priv->actions)
clutter_actor_paint (priv->button_box);
}
static void
mx_dialog_angle_cb (GObject *window,
GParamSpec *pspec,
MxDialog *dialog)
{
g_object_get (window, "window-rotation-angle", &dialog->priv->angle, NULL);
clutter_actor_queue_redraw (CLUTTER_ACTOR (dialog));
}
static void
mx_dialog_map (ClutterActor *actor)
{
MxWindow *window;
ClutterActor *stage;
MxDialog *self = MX_DIALOG (actor);
MxDialogPrivate *priv = self->priv;
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->map (actor);
clutter_actor_map (priv->background);
clutter_actor_map (priv->button_box);
stage = clutter_actor_get_parent (actor);
if (CLUTTER_IS_STAGE (stage))
{
window = mx_window_get_for_stage (CLUTTER_STAGE (stage));
if (window)
{
g_signal_connect (window, "notify::window-rotation-angle",
G_CALLBACK (mx_dialog_angle_cb), actor);
mx_dialog_angle_cb (G_OBJECT (window), NULL, self);
}
}
}
static void
mx_dialog_unmap (ClutterActor *actor)
{
MxWindow *window;
ClutterActor *stage;
MxDialogPrivate *priv = MX_DIALOG (actor)->priv;
stage = clutter_actor_get_parent (actor);
if (CLUTTER_IS_STAGE (stage))
{
window = mx_window_get_for_stage (CLUTTER_STAGE (stage));
if (window)
g_signal_handlers_disconnect_by_func (window, mx_dialog_angle_cb,
actor);
}
clutter_actor_unmap (priv->button_box);
clutter_actor_unmap (priv->background);
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->unmap (actor);
}
static void
mx_dialog_class_init (MxDialogClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
g_type_class_add_private (klass, sizeof (MxDialogPrivate));
object_class->dispose = mx_dialog_dispose;
object_class->finalize = mx_dialog_finalize;
actor_class->get_preferred_width = mx_dialog_get_preferred_width;
actor_class->get_preferred_height = mx_dialog_get_preferred_height;
actor_class->allocate = mx_dialog_allocate;
actor_class->paint = mx_dialog_paint;
actor_class->pick = mx_dialog_pick;
actor_class->map = mx_dialog_map;
actor_class->unmap = mx_dialog_unmap;
actor_class->show = mx_dialog_show;
actor_class->hide = mx_dialog_hide;
}
static void
mx_dialog_completed_cb (ClutterTimeline *timeline,
ClutterActor *self)
{
ClutterTimelineDirection direction;
MxDialogPrivate *priv = MX_DIALOG (self)->priv;
priv->zoom = 1.f;
/* Reverse the direction and rewind the timeline. This means that when
* a timeline finishes, its progress stays at 1.0, or 0.0 and it is
* ready to start again.
*/
direction = clutter_timeline_get_direction (timeline);
clutter_timeline_set_direction (timeline,
(direction == CLUTTER_TIMELINE_FORWARD) ?
CLUTTER_TIMELINE_BACKWARD :
CLUTTER_TIMELINE_FORWARD);
if (direction == CLUTTER_TIMELINE_FORWARD)
return;
/* Finish hiding */
CLUTTER_ACTOR_SET_FLAGS (self, CLUTTER_ACTOR_VISIBLE);
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->hide (self);
}
static void
mx_dialog_new_frame_cb (ClutterTimeline *timeline,
gint msecs,
ClutterActor *self)
{
MxDialog *frame = MX_DIALOG (self);
MxDialogPrivate *priv = frame->priv;
ClutterActor *parent = clutter_actor_get_parent (self);
gfloat opacity = clutter_alpha_get_alpha (priv->alpha);
priv->zoom = 1.0f + (1.f - opacity) / 2.f;
clutter_actor_set_opacity (self, (guint8)(opacity * 255.f));
/* Queue a redraw on the parent, as having our hidden flag set will
* short-circuit the redraw queued on ourselves via set_opacity.
*/
if (parent)
clutter_actor_queue_redraw (parent);
}
static void
mx_dialog_style_changed_cb (MxDialog *self)
{
guint spacing;
MxDialogPrivate *priv = self->priv;
mx_stylable_get (MX_STYLABLE (self),
"x-mx-spacing", &spacing,
NULL);
if (priv->spacing != spacing)
{
priv->spacing = spacing;
clutter_actor_queue_relayout (CLUTTER_ACTOR (self));
}
}
static void
mx_dialog_init (MxDialog *self)
{
ClutterActor *actor = CLUTTER_ACTOR (self);
MxDialogPrivate *priv = self->priv = DIALOG_PRIVATE (self);
priv->transition_time = 250;
priv->timeline = clutter_timeline_new (priv->transition_time);
priv->alpha = clutter_alpha_new_full (priv->timeline,
CLUTTER_EASE_OUT_QUAD);
priv->background = mx_frame_new ();
mx_stylable_set_style_class (MX_STYLABLE (priv->background),
"MxDialogBackground");
priv->button_box = mx_box_layout_new ();
mx_stylable_set_style_class (MX_STYLABLE (priv->button_box),
"MxDialogButtonBox");
priv->child_has_focus = TRUE;
clutter_actor_add_child (actor, priv->background);
clutter_actor_add_child (actor, priv->button_box);
g_signal_connect (priv->timeline, "completed",
G_CALLBACK (mx_dialog_completed_cb), self);
g_signal_connect (priv->timeline, "new-frame",
G_CALLBACK (mx_dialog_new_frame_cb), self);
g_signal_connect (self, "style-changed",
G_CALLBACK (mx_dialog_style_changed_cb), self);
g_object_set (G_OBJECT (self), "show-on-set-parent", FALSE, NULL);
clutter_actor_set_reactive (CLUTTER_ACTOR (self), TRUE);
}
/**
* mx_dialog_new:
*
* Creates a new #MxDialog.
*
* Returns: A newly allocated #MxDialog
*
* Since: 1.2
*/
ClutterActor *
mx_dialog_new (void)
{
return g_object_new (MX_TYPE_DIALOG, NULL);
}
/**
* mx_dialog_set_transient_parent:
* @dialog: A #MxDialog
* @actor: A #ClutterActor
*
* Sets the parent of the #MxDialog. This is the actor over which the
* modal frame will appear when clutter_actor_show() is called.
*
* Since: 1.2
*/
void
mx_dialog_set_transient_parent (MxDialog *dialog,
ClutterActor *actor)
{
g_return_if_fail (MX_IS_DIALOG (dialog));
g_return_if_fail (CLUTTER_IS_ACTOR (actor));
clutter_actor_add_child (actor, CLUTTER_ACTOR (dialog));
clutter_actor_add_constraint (CLUTTER_ACTOR (dialog),
clutter_bind_constraint_new (actor,
CLUTTER_BIND_SIZE,
0));
}
static void
mx_dialog_steal_focus (MxDialog *self)
{
ClutterActor *stage = clutter_actor_get_stage (CLUTTER_ACTOR (self));
if (stage)
{
MxFocusManager *manager =
mx_focus_manager_get_for_stage (CLUTTER_STAGE (stage));
mx_focus_manager_push_focus (manager, MX_FOCUSABLE (self));
}
}
static void
mx_dialog_show (ClutterActor *self)
{
MxDialog *dialog = MX_DIALOG (self);
MxDialogPrivate *priv = dialog->priv;
if (!priv->visible)
{
ClutterActor *parent = clutter_actor_get_parent (self);
if (!parent)
return;
priv->visible = TRUE;
if (clutter_timeline_is_playing (priv->timeline))
{
ClutterTimelineDirection direction =
clutter_timeline_get_direction (priv->timeline);
direction = (direction == CLUTTER_TIMELINE_FORWARD) ?
CLUTTER_TIMELINE_BACKWARD : CLUTTER_TIMELINE_FORWARD;
clutter_timeline_set_direction (priv->timeline, direction);
CLUTTER_ACTOR_SET_FLAGS (self, CLUTTER_ACTOR_VISIBLE);
mx_dialog_steal_focus (dialog);
return;
}
clutter_actor_set_opacity (self, 0x00);
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->show (self);
clutter_alpha_set_mode (priv->alpha, CLUTTER_EASE_OUT_QUAD);
clutter_timeline_start (priv->timeline);
mx_dialog_steal_focus (dialog);
}
}
static void
mx_dialog_hide (ClutterActor *self)
{
MxDialog *dialog = MX_DIALOG (self);
MxDialogPrivate *priv = dialog->priv;
if (priv->visible)
{
ClutterActor *stage;
ClutterActor *parent = clutter_actor_get_parent (self);
priv->visible = FALSE;
if (!parent)
{
CLUTTER_ACTOR_CLASS (mx_dialog_parent_class)->hide (self);
return;
}
CLUTTER_ACTOR_UNSET_FLAGS (self, CLUTTER_ACTOR_VISIBLE);
stage = clutter_actor_get_stage (self);
if (stage)
{
MxFocusManager *manager =
mx_focus_manager_get_for_stage (CLUTTER_STAGE (stage));
mx_focus_manager_move_focus (manager, MX_FOCUS_DIRECTION_OUT);
}
if (clutter_timeline_is_playing (priv->timeline))
{
ClutterTimelineDirection direction =
clutter_timeline_get_direction (priv->timeline);
direction = (direction == CLUTTER_TIMELINE_FORWARD) ?
CLUTTER_TIMELINE_BACKWARD : CLUTTER_TIMELINE_FORWARD;
clutter_timeline_set_direction (priv->timeline, direction);
return;
}
/* The timeline is running in reverse, so use ease-in quad */
clutter_alpha_set_mode (priv->alpha, CLUTTER_EASE_IN_QUAD);
clutter_timeline_start (priv->timeline);
}
}
/**
* mx_dialog_add_action:
* @dialog: A #MxDialog
* @action: A #MxAction
*
* Adds an #MxButton that represents @action to the button area of @dialog
*
* Since: 1.2
*/
void
mx_dialog_add_action (MxDialog *dialog,
MxAction *action)
{
MxDialogPrivate *priv;
ClutterActor *button;
MxDialogAction *da;
g_return_if_fail (MX_IS_DIALOG (dialog));
g_return_if_fail (MX_IS_ACTION (action));
priv = dialog->priv;
button = mx_button_new ();
mx_button_set_action (MX_BUTTON (button), action);
clutter_actor_add_child (priv->button_box, button);
/* So we can maintain the two way relationship between action and button */
da = g_slice_new (MxDialogAction);
da->action = action;
da->button = button;
priv->actions = g_list_append (priv->actions, da);
}
/**
* mx_dialog_remove_action:
* @dialog: A #MxDialog
* @action: A #MxAction
*
* Removes the button associated with @action from the button area of @dialog
*
* Since: 1.2
*/
void
mx_dialog_remove_action (MxDialog *dialog,
MxAction *action)
{
MxDialogPrivate *priv;
MxDialogAction *da;
GList *a;
g_return_if_fail (MX_IS_DIALOG (dialog));
g_return_if_fail (MX_IS_ACTION (action));
priv = dialog->priv;
da = NULL;
for (a = priv->actions; a; a = a->next)
{
MxDialogAction *data = a->data;
if (data->action == action)
{
priv->actions = g_list_delete_link (priv->actions, a);
da = data;
break;
}
}
if (da == NULL)
{
g_warning ("Action '%s' was not found in dialog",
mx_action_get_name (action));
return;
}
clutter_actor_remove_child (priv->button_box, da->button);
g_slice_free (MxDialogAction, da);
}
/**
* mx_dialog_get_actions:
* @dialog: A #MxDialog
*
* Retrieves a list of actions added to @dialog.
*
* Returns: (transfer container) (element-type Mx.Action): A newly allocated
* #GList of #MxAction objects. The actions in the list are owned by the
* dialog.
*
* Since: 1.2
*/
GList *
mx_dialog_get_actions (MxDialog *dialog)
{
GList *a, *list;
MxDialogPrivate *priv;
g_return_val_if_fail (MX_IS_DIALOG (dialog), NULL);
priv = dialog->priv;
list = NULL;
for (a = priv->actions; a; a = a->next)
{
MxDialogAction *da = a->data;
list = g_list_prepend (list, da->action);
}
return g_list_reverse (list);
}