e945fdd2f366fe83ace7a094760110d59d4bb41f
[moonshot-ui.git] / src / moonshot-idcard-widget.vala
1 using Gtk;
2
3 class IdCardWidget : Box
4 {
5     public IdCard id_card { get; set; default = null; }
6
7     private VBox main_vbox;
8     private Table table;
9     public Button delete_button { get; private set; default = null; }
10     public Button details_button { get; private set; default = null; }
11     public Button send_button { get; private set; default = null; }
12     private HButtonBox hbutton_box;
13     private EventBox event_box;
14
15     public signal void expanded ();
16     public signal void remove_id ();
17     public signal void details_id ();
18
19     public void collapse ()
20     {
21         this.hbutton_box.set_visible (false);
22
23         set_idcard_color ();
24     }
25
26     private bool button_press_cb ()
27     {
28         this.hbutton_box.set_visible (!hbutton_box.get_visible ());
29
30         set_idcard_color ();
31
32         if (hbutton_box.get_visible () == true)
33           this.expanded ();
34
35         return false;
36     }
37
38     private void delete_button_cb ()
39     {
40        this.remove_id ();
41     }
42
43     private void details_button_cb ()
44     {
45        this.details_id ();
46     }
47
48     private void set_idcard_color ()
49     {
50         var color = Gdk.Color ();
51
52         if (hbutton_box.get_visible () == false)
53         {
54             color.red = 65535;
55             color.green = 65535;
56             color.blue = 65535;
57         }
58         else
59         {
60             color.red = 33333;
61             color.green = 33333;
62             color.blue = 60000;
63         }
64         var state = this.get_state ();
65         this.event_box.modify_bg (state, color);
66     }
67
68     public IdCardWidget (IdCard id_card)
69     {
70         string services_text = "";
71         this.id_card = id_card;
72
73         var image = new Image.from_pixbuf (id_card.pixbuf);
74
75         var issuer = Markup.printf_escaped ("<b>%s</b>", this.id_card.issuer);
76         foreach (string service in id_card.services)
77         {
78             services_text = services_text + Markup.printf_escaped ("<i>%s, </i>", service);
79         }
80         var text = issuer + "\n" + services_text;
81
82         var id_data_label = new Label (null);
83         id_data_label.set_markup (text);
84         id_data_label.set_alignment ((float) 0, (float) 0.5);
85
86         this.table = new Table (1, 2, false);
87         table.attach_defaults (image, 0, 1, 0, 1);
88         table.attach_defaults (id_data_label, 1, 2, 0, 1);
89
90         this.delete_button = new Button.with_label (_("Delete"));
91         this.details_button = new Button.with_label (_("View details"));
92         this.send_button = new Button.with_label (_("Send"));
93         set_atk_name_description (delete_button, _("Delete"), _("Delete this ID Card"));
94         set_atk_name_description (details_button, _("Details"), _("View the details of this ID Card"));
95         set_atk_name_description (send_button, _("Send"), _("Send this ID Card"));
96         this.hbutton_box = new HButtonBox ();
97         hbutton_box.pack_end (delete_button);
98         hbutton_box.pack_end (details_button);
99         hbutton_box.pack_end (send_button);
100
101         delete_button.clicked.connect (delete_button_cb);
102         details_button.clicked.connect (details_button_cb);
103
104         this.main_vbox = new VBox (false, 12);
105         main_vbox.pack_start (table, true, true, 0);
106         main_vbox.pack_start (hbutton_box, false, false, 0);
107         main_vbox.set_border_width (12);
108
109         event_box = new EventBox ();
110         event_box.add (main_vbox);
111         event_box.button_press_event.connect (button_press_cb);
112         this.pack_start (event_box, true, true);
113
114         this.show_all ();
115         this.hbutton_box.hide ();
116
117         set_idcard_color ();
118     }
119
120     private void set_atk_name_description (Widget widget, string name, string description)
121     {
122        var atk_widget = widget.get_accessible ();
123
124        atk_widget.set_name (name);
125        atk_widget.set_description (description);
126     }
127 }