00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "datetime_box.hpp"
00035 #include "main_window.hpp"
00036 #include "data/config.hpp"
00037 #include "math/math.hpp"
00038 #include "math/time.hpp"
00039
00040 using namespace gsgl;
00041 using namespace gsgl::data;
00042 using namespace gsgl::math;
00043 using namespace gsgl::platform;
00044 using namespace gsgl::framework;
00045
00046
00047 namespace periapsis
00048 {
00049
00050 static config_variable<int> LABEL_WIDTH(L"ui/datetime_box/label_width", 128);
00051 static config_variable<int> JULIAN_WIDTH(L"ui/datetime_box/julian_width", 256);
00052 static config_variable<int> SPACING(L"ui/datetime_box/spacing", 8);
00053
00054
00055 datetime_box::datetime_box(widget *parent, int x, int y, int w, int h, const color & fg, const color & bg)
00056 : widget(parent, x, y, w, h, fg, bg), label(0), julian_input(0), text_input(0), jdn(0), mode(RECALC_NONE)
00057 {
00058 label = new textbox(this, 0, 0, LABEL_WIDTH, h, fg, bg, main_window::FONT_FACE, main_window::FONT_SIZE);
00059 label->get_background()[color::COMPONENT_ALPHA] = 0;
00060 label->get_text() = L"Date & Time:";
00061
00062 julian_input = new textbox(this, LABEL_WIDTH, 0, JULIAN_WIDTH + SPACING, h, fg, bg, main_window::FONT_FACE, main_window::FONT_SIZE);
00063
00064 text_input = new textbox(this, LABEL_WIDTH + JULIAN_WIDTH + SPACING*2, 0, w - (LABEL_WIDTH+JULIAN_WIDTH+SPACING*2), h, fg, bg, main_window::FONT_FACE, main_window::FONT_SIZE);
00065 text_input->get_text() = L"<no date & time specified>";
00066 }
00067
00068
00069 datetime_box::~datetime_box()
00070 {
00071
00072 }
00073
00074
00075 const double & datetime_box::get_jdn() const
00076 {
00077 return jdn;
00078 }
00079
00080
00081 void datetime_box::set_jdn(const double & n)
00082 {
00083 mode = RECALC_BOTH;
00084 jdn = n;
00085 }
00086
00087
00088 const string & datetime_box::get_text() const
00089 {
00090 return text_input->get_text();
00091 }
00092
00093
00094 void datetime_box::set_text(const string & str)
00095 {
00096 mode = RECALC_JDN;
00097 text_input->get_text() = str;
00098 }
00099
00100
00101 void datetime_box::draw()
00102 {
00103 switch (mode)
00104 {
00105 case RECALC_TEXT:
00106 recalc_text();
00107 mode = RECALC_NONE;
00108 break;
00109 case RECALC_JDN:
00110 recalc_jdn();
00111 mode = RECALC_NONE;
00112 break;
00113 case RECALC_BOTH:
00114 julian_input->get_text() = string::format(L"%f", jdn);
00115 recalc_text();
00116 mode = RECALC_NONE;
00117 break;
00118 default:
00119 break;
00120 }
00121
00122 if (jdn == 0.0)
00123 julian_input->get_text() = L"<invalid>";
00124 }
00125
00126
00127 void datetime_box::recalc_text()
00128 {
00129 try
00130 {
00131 jdn = julian_input->get_text().to_double();
00132 julian_day jd(jdn);
00133 text_input->get_text() = jd.to_gregorian_string();
00134 }
00135 catch (...)
00136 {
00137 jdn = 0;
00138 text_input->get_text() = L"<invalid>";
00139 }
00140 }
00141
00142
00143 void datetime_box::recalc_jdn()
00144 {
00145 try
00146 {
00147 julian_day jd;
00148
00149 jd.from_gregorian_string(text_input->get_text());
00150 jdn = jd.get_jdn();
00151 julian_input->get_text() = string::format(L"%f", jdn);
00152 }
00153 catch (...)
00154 {
00155 jdn = 0;
00156 }
00157 }
00158
00159
00160 }