-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.cpp
100 lines (74 loc) · 2.58 KB
/
dialogs.cpp
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
#include "dialogs.h"
// A message is like cout, simply displaying information to the user
void Dialogs::message(std::string msg, std::string title) {
Gtk::MessageDialog *dialog = new Gtk::MessageDialog(title);
dialog->set_secondary_text(msg, true);
dialog->run();
dialog->close();
while (Gtk::Main::events_pending()) Gtk::Main::iteration();
delete dialog;
}
// A question is a message that allows the user to respond with a button
int Dialogs::question(std::string msg, std::string title,
std::vector<std::string> buttons) {
Gtk::Dialog *dialog = new Gtk::Dialog();
dialog->set_title(title);
Gtk::Label *label = new Gtk::Label(msg);
dialog->get_content_area()->pack_start(*label);
label->show();
for(int i=0; i<buttons.size(); ++i) dialog->add_button(buttons[i], i);
int result = dialog->run();
dialog->close();
while (Gtk::Main::events_pending()) Gtk::Main::iteration();
delete label;
delete dialog;
return result;
}
// A request for a line of text input
std::string Dialogs::input(std::string msg, std::string title, std::string default_text,
std::string cancel_text) {
Gtk::Dialog *dialog = new Gtk::Dialog();
dialog->set_title(title);
Gtk::Label *label = new Gtk::Label(msg);
dialog->get_content_area()->pack_start(*label);
label->show();
dialog->add_button("Cancel", 0);
dialog->add_button("OK", 1);
dialog->set_default_response(1);
Gtk::Entry *entry = new Gtk::Entry{};
entry->set_text(default_text);
entry->set_max_length(50);
entry->show();
dialog->get_vbox()->pack_start(*entry);
int result = dialog->run();
std::string text = entry->get_text();
dialog->close();
while (Gtk::Main::events_pending()) Gtk::Main::iteration();
delete entry;
delete label;
delete dialog;
if (result == 1)
return text;
else
return cancel_text;
}
// Display an image from a disk file
void Dialogs::image(std::string filename, std::string title, std::string msg) {
Gtk::Dialog *dialog = new Gtk::Dialog();
dialog->set_title(title);
Gtk::Label *label = new Gtk::Label(msg);
dialog->get_content_area()->pack_start(*label);
label->show();
dialog->add_button("Close", 0);
dialog->set_default_response(0);
Gtk::Image *image = new Gtk::Image{filename};
image->show();
dialog->get_vbox()->pack_start(*image);
int result = dialog->run();
dialog->close();
while (Gtk::Main::events_pending()) Gtk::Main::iteration();
delete image;
delete label;
delete dialog;
return;
}