cgv
Loading...
Searching...
No Matches
dialog.cxx
1#include "gui_driver.h"
2#include "dialog.h"
3#include <cgv/utils/tokenizer.h>
4#include <cgv/utils/scan.h>
5#include <cgv/gui/application.h>
6#include <cgv/signal/rebind.h>
7
8using namespace cgv::utils;
9
10namespace cgv {
11 namespace gui {
12
14void message(const std::string& _message)
15{
17 if (d.empty())
18 return;
19
20 d->message(_message);
21}
22
24int question(const std::string& _question, const std::vector<std::string>& answers, int default_answer)
25{
27 if (d.empty())
28 return -1;
29
30 return d->question(_question,answers,default_answer);
31}
32
34int question(const std::string& _question, const std::string& answers, int default_answer)
35{
37 if (d.empty())
38 return -1;
39
40 std::vector<std::string> answer_strings;
41 std::vector<int> answer_values;
42
43 std::vector<token> tokens;
44 tokenizer(answers).set_ws(",").bite_all(tokens);
45 int last = -1;
46 for (unsigned i=0; i<tokens.size(); ++i) {
47 std::vector<token> toks;
48 tokenizer(tokens[i]).set_ws("=").bite_all(toks);
49 int idx = ++last;
50 if (toks.size() > 0) {
51 answer_strings.push_back(to_string(toks[0]));
52 if (toks.size() > 1)
53 is_integer(toks[1].begin, toks[1].end, idx);
54 }
55 answer_values.push_back(idx);
56 }
57
58 return answer_values[d->question(_question,answer_strings,default_answer)];
59}
60
61bool query(const std::string& question, std::string& text, bool password)
62{
64 if (d.empty())
65 return false;
66
67 return d->query(question,text,password);
68}
69
70void dialog::set_true_and_hide(bool* result, cgv::gui::window* w)
71{
72 *result = true;
73 w->hide();
74}
76dialog::dialog(const std::string& title, const std::string& group_type)
77{
78 result = false;
79 adjust_size = true;
80 D = cgv::gui::application::create_window(400,10,title,"generic");
81 D->set("group", group_type);
82}
83
85dialog::dialog(int w, int h, const std::string& title, const std::string& group_type)
86{
87 result = false;
88 adjust_size = h <= 0;
89 D = cgv::gui::application::create_window(w,adjust_size ? 10 : h,title,"generic");
90 D->set("group", group_type);
91}
92
95{
96 return D->get_inner_group();
97}
98
100void dialog::add_std_buttons(const std::string& ok_label, const std::string& cancel_label)
101{
102 if (!ok_label.empty())
103 cgv::signal::connect_copy(group()->add_button(ok_label, "w=75", " ")->click, cgv::signal::rebind(set_true_and_hide, cgv::signal::_c(&result), cgv::signal::_c(&(*D))));
104 if (!cancel_label.empty())
105 cgv::signal::connect_copy(group()->add_button(cancel_label, "w=75", "\n")->click, cgv::signal::rebind(&(*D), &cgv::gui::window::hide));
106}
107
110{
111 result = false;
112 if (adjust_size) {
113 D->set("H", group()->get<int>("H"));
114 }
115 D->set("hotspot", true);
116 D->show(true);
117 return result;
118}
119
120
121
122 }
123}
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
static window_ptr create_window(int w, int h, const std::string &title, const std::string &window_type="viewer")
create a window of the given type, where all gui implementations must support the type "viewer"
cgv::gui::gui_group_ptr group()
return the gui group to which new elements are to be add
Definition dialog.cxx:94
void add_std_buttons(const std::string &ok_label="ok", const std::string &cancel_label="")
add buttons for ok and or cancel
Definition dialog.cxx:100
dialog(const std::string &title, const std::string &group_type="align_group")
create from title and adjust size according to content
Definition dialog.cxx:76
bool exec()
execute modal dialog and freeze all other windows
Definition dialog.cxx:109
gui independent window class
Definition window.h:14
virtual void hide()=0
hide the window
the tokenizer allows to split text into tokens in a convenient way.
Definition tokenizer.h:68
tokenizer & set_ws(const std::string &ws)
set the list of white spaces, that separate tokens and are skipped
Definition tokenizer.cxx:38
gui_driver_ptr get_gui_driver()
return the currently registered gui driver or an empty pointer if non has been registered
void message(const std::string &_message)
tell the user something with a message box
Definition dialog.cxx:14
int question(const std::string &_question, const std::vector< std::string > &answers, int default_answer)
ask the user with question to select one of the answers, where default_answer specifies index of defa...
Definition dialog.cxx:24
bool query(const std::string &question, std::string &text, bool password)
query the user for a text, where the second parameter is the default text as well as the returned tex...
Definition dialog.cxx:61
namespace that holds tools that dont fit any other namespace
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
Definition scan.cxx:367
the cgv namespace
Definition print.h:11
Helper functions to process strings.