8ea1f601c1fc61215a3dd61cb77907a6a3d1d8d7
[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 HBox 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     private Label label;
16
17     public signal void expanded ();
18     public signal void remove_id ();
19     public signal void details_id ();
20     public signal void send_id ();
21
22     public void collapse ()
23     {
24         this.hbutton_box.set_visible (false);
25
26         set_idcard_color ();
27     }
28
29     private bool button_press_cb ()
30     {
31         this.hbutton_box.set_visible (!hbutton_box.get_visible ());
32
33         set_idcard_color ();
34
35         if (hbutton_box.get_visible () == true)
36           this.expanded ();
37
38         return false;
39     }
40
41     private void delete_button_cb ()
42     {
43        this.remove_id ();
44     }
45
46     private void details_button_cb ()
47     {
48        this.details_id ();
49     }
50
51     private void send_button_cb ()
52     {
53        this.send_id ();
54     }
55
56     private void set_idcard_color ()
57     {
58         var color = Gdk.Color ();
59
60         if (hbutton_box.get_visible () == false)
61         {
62             color.red = 65535;
63             color.green = 65535;
64             color.blue = 65535;
65         }
66         else
67         {
68             color.red = 33333;
69             color.green = 33333;
70             color.blue = 60000;
71         }
72         var state = this.get_state ();
73         this.event_box.modify_bg (state, color);
74     }
75     
76     public void
77     update_id_card_label ()
78     {
79         string services_text = "";
80
81         var display_name = Markup.printf_escaped ("<big>%s</big>", this.id_card.display_name);
82         for (int i=0; i<id_card.services.length; i++)
83         {
84             var service = id_card.services[i];
85             
86             if (i == (id_card.services.length - 1))
87               services_text = services_text + Markup.printf_escaped ("<i>%s</i>", service);
88             else
89               services_text = services_text + Markup.printf_escaped ("<i>%s, </i>", service);
90         }
91         label.set_markup (display_name + "\n" + services_text);
92     }
93
94     public IdCardWidget (IdCard id_card)
95     {
96         this.id_card = id_card;
97
98         var image = new Image.from_pixbuf (get_pixbuf(id_card));
99
100         label = new Label (null);
101         label.set_alignment ((float) 0, (float) 0.5);
102         label.set_ellipsize (Pango.EllipsizeMode.END);
103         update_id_card_label();
104
105         table = new Gtk.HBox (false, 6);
106         table.pack_start (image, false, false, 0);
107         table.pack_start (label, true, true, 0);
108
109         this.delete_button = new Button.with_label (_("Delete"));
110         this.details_button = new Button.with_label (_("View details"));
111         this.send_button = new Button.with_label (_("Send"));
112         set_atk_name_description (delete_button, _("Delete"), _("Delete this ID Card"));
113         set_atk_name_description (details_button, _("Details"), _("View the details of this ID Card"));
114         set_atk_name_description (send_button, _("Send"), _("Send this ID Card"));
115         this.hbutton_box = new HButtonBox ();
116         hbutton_box.pack_end (delete_button);
117         hbutton_box.pack_end (details_button);
118         hbutton_box.pack_end (send_button);
119         send_button.set_sensitive (false);
120
121         delete_button.clicked.connect (delete_button_cb);
122         details_button.clicked.connect (details_button_cb);
123         send_button.clicked.connect (send_button_cb);
124
125         this.main_vbox = new VBox (false, 12);
126         main_vbox.pack_start (table, true, true, 0);
127         main_vbox.pack_start (hbutton_box, false, false, 0);
128         main_vbox.set_border_width (12);
129
130         event_box = new EventBox ();
131         event_box.add (main_vbox);
132         event_box.button_press_event.connect (button_press_cb);
133         this.pack_start (event_box, true, true);
134
135         this.show_all ();
136         this.hbutton_box.hide ();
137
138         set_idcard_color ();
139     }
140
141     private void set_atk_name_description (Widget widget, string name, string description)
142     {
143        var atk_widget = widget.get_accessible ();
144
145        atk_widget.set_name (name);
146        atk_widget.set_description (description);
147     }
148 }