Skip to content
API Documentation
BastionGuard™
CustomMessageDialog.vala
Go to the documentation of this file.
1/*
2 * CustomMessageDialog.vala
3 *
4 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24
25using Gtk;
26using Gee;
27
28using TeeJee.Logging;
33using TeeJee.System;
34using TeeJee.Misc;
35
36public class CustomMessageDialog : Gtk.Dialog {
37
38 private Gtk.Box vbox_main;
39 private Gtk.Label lbl_msg;
40 private Gtk.ScrolledWindow sw_msg;
41 private Gtk.Button btn_ok;
42 private Gtk.Button btn_cancel;
43 private Gtk.Button btn_yes;
44 private Gtk.Button btn_no;
45
46 private string msg_title;
47 private string msg_body;
48 private Gtk.MessageType msg_type;
49 private Gtk.ButtonsType buttons_type;
50
51 public CustomMessageDialog(string _msg_title, string _msg_body, Gtk.MessageType _msg_type, Window? parent, Gtk.ButtonsType _buttons_type) {
52
53 set_transient_for(parent);
54 set_modal(true);
55
56 msg_title = _msg_title;
57 msg_body = _msg_body;
58 msg_type = _msg_type;
59 buttons_type = _buttons_type;
60
62
63 lbl_msg.hexpand = true;
64
65 lbl_msg.vexpand = true;
66 sw_msg.hexpand = true;
67 sw_msg.vexpand = true;
68 sw_msg.vscrollbar_policy = PolicyType.NEVER;
69
70 sw_msg.set_size_request(500, 150); // sets minimum size
71
72 show();
73
74 if (lbl_msg.get_allocated_height() > 400){
75 sw_msg.vscrollbar_policy = PolicyType.AUTOMATIC;
76 sw_msg.set_size_request(500, 400);
77 }
78 }
79
80 public void init_window () {
81
82 this.title = "";
83 this.set_icon_name("bastionguard-backup");
84 this.resizable = false;
85 this.deletable = false;
86
87 //vbox_main
88 vbox_main = get_content_area () as Gtk.Box;
89 vbox_main.margin_start = 6;
90 vbox_main.margin_end = 6;
91 vbox_main.margin_top = 6;
92 vbox_main.margin_bottom = 6;
93
94 //hbox_contents
95 var hbox_contents = new Gtk.Box(Orientation.HORIZONTAL, 6);
96 hbox_contents.margin_start = 6;
97 hbox_contents.margin_end = 6;
98 hbox_contents.margin_top = 6;
99 hbox_contents.margin_bottom = 6;
100 vbox_main.append(hbox_contents);
101
102 string icon_name = "dialog-info";
103
104 switch(msg_type){
105 case Gtk.MessageType.INFO:
106 icon_name = "dialog-info";
107 break;
108 case Gtk.MessageType.WARNING:
109 icon_name = "dialog-warning";
110 break;
111 case Gtk.MessageType.QUESTION:
112 icon_name = "dialog-question";
113 break;
114 case Gtk.MessageType.ERROR:
115 icon_name = "dialog-error";
116 break;
117 case Gtk.MessageType.OTHER:
118 icon_name = "dialog-info";
119 break;
120 }
121
122 // image ----------------
123
124 var img = IconManager.lookup_image(icon_name, 16);
125 hbox_contents.append(img);
126
127 // label -------------------
128
129 var text = "<span weight=\"bold\" size=\"x-large\">%s</span>\n\n%s".printf(
131 msg_body);
132 lbl_msg = new Gtk.Label(text);
133 lbl_msg.xalign = 0.0f;
134 lbl_msg.yalign = 0.0f;
135 lbl_msg.max_width_chars = 70;
136 lbl_msg.wrap = true;
137 lbl_msg.wrap_mode = Pango.WrapMode.WORD_CHAR;
138 lbl_msg.use_markup = true;
139
140 //sw_msg
141 sw_msg = new Gtk.ScrolledWindow();
142 //sw_msg.set_shadow_type (ShadowType.ETCHED_IN);
143 sw_msg.set_child(lbl_msg);
144 sw_msg.hscrollbar_policy = PolicyType.NEVER;
145 sw_msg.vscrollbar_policy = PolicyType.NEVER;
146 //sw_msg.set_size_request(500, 400);
147 hbox_contents.append(sw_msg);
148
149 // actions -------------------------
150
151 switch(buttons_type){
152 case Gtk.ButtonsType.NONE: break;
153 case Gtk.ButtonsType.OK:
154 btn_ok = (Gtk.Button) add_button (_("OK"), Gtk.ResponseType.OK);
155 btn_ok.grab_focus();
156 break;
157 case Gtk.ButtonsType.CLOSE:
158 btn_cancel = (Gtk.Button) add_button (_("Close"), Gtk.ResponseType.CLOSE);
159 btn_cancel.grab_focus();
160 break;
161 case Gtk.ButtonsType.CANCEL:
162 btn_cancel = (Gtk.Button) add_button (_("Cancel"), Gtk.ResponseType.CANCEL);
163 btn_cancel.grab_focus();
164 break;
165 case Gtk.ButtonsType.OK_CANCEL:
166 btn_ok = (Gtk.Button) add_button (_("OK"), Gtk.ResponseType.OK);
167 btn_cancel = (Gtk.Button) add_button (_("Cancel"), Gtk.ResponseType.CANCEL);
168 btn_ok.grab_focus();
169 break;
170 case Gtk.ButtonsType.YES_NO:
171 btn_yes = (Gtk.Button) add_button (_("Yes"), Gtk.ResponseType.YES);
172 btn_no = (Gtk.Button) add_button (_("No"), Gtk.ResponseType.NO);
173 btn_yes.grab_focus();
174 break;
175 }
176 }
177
178 public int run() {
179 int result = Gtk.ResponseType.CLOSE;
180 var loop = new GLib.MainLoop(null, false);
181 this.response.connect((response_id) => { result = response_id; loop.quit(); });
182 this.close_request.connect(() => { result = Gtk.ResponseType.CLOSE; if (loop.is_running()) loop.quit(); return false; });
183 this.present();
184 loop.run();
185 return result;
186 }
187}
#define _(s)
CustomMessageDialog(string _msg_title, string _msg_body, Gtk.MessageType _msg_type, Window? parent, Gtk.ButtonsType _buttons_type)
Gtk.ScrolledWindow sw_msg
static Gtk.Image lookup_image(string icon_name, int icon_size, bool symbolic=false, bool use_hardcoded=false)
string escape_html(string html)