summaryrefslogtreecommitdiff
path: root/src/user/raleigh/d
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/raleigh/d')
-rw-r--r--src/user/raleigh/d/dialog.cpp74
-rw-r--r--src/user/raleigh/d/saving_window.cpp55
2 files changed, 129 insertions, 0 deletions
diff --git a/src/user/raleigh/d/dialog.cpp b/src/user/raleigh/d/dialog.cpp
new file mode 100644
index 0000000..38ca310
--- /dev/null
+++ b/src/user/raleigh/d/dialog.cpp
@@ -0,0 +1,74 @@
+#include <raleigh/d/dialog.h>
+
+#include <raleigh/w/padding.h>
+#include <raleigh/w/button.h>
+#include <raleigh/w/label.h>
+#include <raleigh/w/hbox.h>
+
+using namespace raleigh;
+
+void dialog::show_modal() {
+ show();
+ do {
+ _wait_for_action();
+ _yield_task();
+ consume_actions();
+ } while (!result);
+ to_be_deleted.add_back(*this);
+}
+
+static bool on_diag_close(window_tag_t) {
+ return false;
+}
+
+struct diag_and_result {
+ dialog *d;
+ diag_result_t r;
+ diag_and_result(dialog *d, diag_result_t r)
+ : d(d), r(r) {}
+};
+
+static void set_diag_result(button_tag_t tag) {
+ const diag_and_result *const cast = (const diag_and_result *)tag;
+ cast->d->result = cast->r;
+ //TODO: go through all of raleigh and add destructors
+ //in this case, delete all of these tags when the window is closed
+}
+
+dialog::dialog(widget &top_part, alist<duple<const char *, diag_result_t>> buttons)
+ : window(*new vbox(), RGB(bf, bf, bf), &on_diag_close), result(0), main_box((vbox *)&root) {
+ dllist<widget &> *button_row = new dllist<widget &>();
+ for (duple<const char *, diag_result_t> *i = buttons.buf; i < buttons.buf + buttons.n_entries; ++i) {
+ label *l = new label(i->a);
+ padding *p = new padding(*l, 4);
+ button *b = new button(*p, &set_diag_result, new diag_and_result(this, i->b));
+ padding *pb = new padding(*b, 2);
+ button_row->add_back(*pb);
+ }
+ hbox *button_box = new hbox(*button_row);
+
+ padding *ptop = new padding(top_part, 2);
+ padding *pbot = new padding(*button_box, 2);
+
+ main_box->add_end(*ptop);
+ main_box->add_end(*pbot);
+}
+
+alist<duple<const char *, diag_result_t>> &mk_yes_no_cancel() {
+ alist<duple<const char *, diag_result_t>> *list = new alist<duple<const char *, diag_result_t>>(3, 1);
+ list->add_back(duple<const char *, diag_result_t>("Yes", YES));
+ list->add_back(duple<const char *, diag_result_t>("No", NO));
+ list->add_back(duple<const char *, diag_result_t>("Cancel", CANCEL));
+ return *list;
+}
+
+alist<duple<const char *, diag_result_t>> &mk_yes_no_retry() {
+ alist<duple<const char *, diag_result_t>> *list = new alist<duple<const char *, diag_result_t>>(3, 1);
+ list->add_back(duple<const char *, diag_result_t>("Yes", YES));
+ list->add_back(duple<const char *, diag_result_t>("No", NO));
+ list->add_back(duple<const char *, diag_result_t>("Retry", RETRY));
+ return *list;
+}
+
+alist<duple<const char *, diag_result_t>> &raleigh::yes_no_cancel(mk_yes_no_cancel());
+alist<duple<const char *, diag_result_t>> &raleigh::yes_no_retry(mk_yes_no_retry()); \ No newline at end of file
diff --git a/src/user/raleigh/d/saving_window.cpp b/src/user/raleigh/d/saving_window.cpp
new file mode 100644
index 0000000..a2d57e4
--- /dev/null
+++ b/src/user/raleigh/d/saving_window.cpp
@@ -0,0 +1,55 @@
+#include <raleigh/d/saving_window.h>
+#include <raleigh/d/dialog.h>
+
+#include <raleigh/w/padding.h>
+#include <raleigh/w/button.h>
+#include <raleigh/w/label.h>
+#include <raleigh/w/hbox.h>
+#include <raleigh/w/vbox.h>
+
+#include <structs/dllist.h>
+
+namespace raleigh {
+ bool on_saving_window_close(window_tag_t tag) {
+ saving_window *const sw = (saving_window *)tag;
+ if (sw->is_saved)
+ return true;
+
+ label text("There is unsaved content in this window.");
+ label text2("Would you like to save before closing it?");
+
+ dllist<widget &> rows;
+ rows.add_back(text);
+ rows.add_back(text2);
+ vbox vb(rows);
+ padding vbp(vb, 2);
+
+ dialog diag(vbp, yes_no_cancel);
+ diag.show_modal();
+
+ switch (diag.result) {
+ case YES:
+ save:
+ if (!sw->save_func(sw->save_tag)) {
+ label text3("Failing saved. Still quit?");
+ dialog diag2(text3, yes_no_retry);
+ diag2.show_modal();
+ switch (diag2.result) {
+ case YES:
+ return true;
+ case RETRY:
+ goto save;
+ default:
+ return false;
+ }
+ }
+ case NO:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ saving_window::saving_window(bool (*save_func)(save_tag_t), save_tag_t save_tag, widget &root, _pixel_t bg_color)
+ : window(root, bg_color, &on_saving_window_close, (window_tag_t)this), is_saved(true), save_func(save_func), save_tag(save_tag) {}
+} \ No newline at end of file