00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "removestock.h"
00019
00020 #include "stockmarketvector.h"
00021 #include "stockmarket.h"
00022 #include "stockcontainer.h"
00023 #include "stock.h"
00024
00025 #include "config.h"
00026
00027 #include <gtkmm/stock.h>
00028
00032 RemoveStock::RemoveStock(const StockMarketVector * const stockMarketVector) {
00033
00034 RemoveStock::stockMarketVector = stockMarketVector;
00035
00036 set_title("removing stock");
00037 set_border_width(5);
00038 set_resizable(false);
00039 set_modal(true);
00040
00041 vbox = manage(new Gtk::VBox(false, 0));
00042 vbox->set_spacing(5);
00043 add(*vbox);
00044
00045 if (stockMarketVector->getNumberOfStockMarkets() > 0) {
00046
00047 label = manage (new Gtk::Label("Select a market:", 0));
00048 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00049 hSep = manage(new Gtk::HSeparator());
00050 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00051 vboxChoice = manage(new Gtk::VBox(false, 0));
00052
00053 radioButtons.clear();
00054 Gtk::RadioButton::Group group;
00055 Gtk::RadioButton *rb;
00056 for (int i = 0; i < stockMarketVector->getNumberOfStockMarkets(); i++) {
00057 StockMarket *stockMarket = stockMarketVector->getStockMarket(i);
00058 rb = manage( new Gtk::RadioButton(group, stockMarket->getMarketName()));
00059 radioButtons.push_back(rb);
00060 vboxChoice->pack_start(*rb, Gtk::PACK_SHRINK, 0);
00061 }
00062
00063 vbox->pack_start(*vboxChoice, Gtk::PACK_SHRINK, 0);
00064 hSep = manage(new Gtk::HSeparator());
00065 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00066
00067 hButtonBox = manage( new Gtk::HButtonBox());
00068 hButtonBox->set_spacing(5);
00069 vbox->pack_start(*hButtonBox, Gtk::PACK_SHRINK, 0);
00070
00071 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00072 buttonCancel->signal_clicked().connect(slot(*this,&RemoveStock::quit));
00073 hButtonBox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00074 buttonNext = manage( new Gtk::Button(Gtk::Stock::GO_FORWARD));
00075 buttonNext->signal_clicked().connect(slot(*this,&RemoveStock::removeStock));
00076 hButtonBox->pack_start(*buttonNext, Gtk::PACK_SHRINK, 0);
00077 } else {
00078 set_title("Message");
00079 label = manage (new Gtk::Label("There are no markets!"));
00080 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00081
00082 buttonCancel = manage(new Gtk::Button(Gtk::Stock::OK));
00083 buttonCancel->signal_clicked().connect(slot(*this,&RemoveStock::quit));
00084 vbox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00085 }
00086 show_all();
00087 }
00088
00092 RemoveStock::~RemoveStock() {
00093
00094 }
00095
00099 void RemoveStock::quit() {
00100 delete this;
00101 }
00102
00106 void RemoveStock::removeStock() {
00107
00108 Gtk::RadioButton *rb;
00109 for (int i = 0; i < radioButtons.size(); i++) {
00110 rb = radioButtons[i];
00111 if (rb->get_active()) {
00112 stockMarket = stockMarketVector->getStockMarket(i);
00113 #ifdef DEBUG
00114 cout << "selected " << stockMarket->getMarketName() << endl;
00115 #endif
00116 }
00117 }
00118 for (int i = 0; i < radioButtons.size(); i++) {
00119 rb = radioButtons[i];
00120 delete rb;
00121 }
00122 radioButtons.clear();
00123
00124 delete vbox;
00125 vbox = manage(new Gtk::VBox(false, 0));
00126 vbox->set_spacing(5);
00127 add(*vbox);
00128 if (stockMarket->getNumberOfStockContainer() > 0) {
00129 tableEditStockValue = manage(new Gtk::Table(2, 1, false));
00130 tableEditStockValue->set_spacings(5);
00131 vbox->pack_start(*tableEditStockValue, Gtk::PACK_SHRINK, 0);
00132
00133 label = manage (new Gtk::Label("Choose a stock:"));
00134 tableEditStockValue->attach(*label, 0, 1, 0, 1);
00135
00136
00137 comboStockName = manage (new Gtk::Combo());
00138 Gtk::Entry *e;
00139 e = comboStockName->get_entry();
00140 e->set_editable(false);
00141 e->set_max_length(80);
00142 e->set_width_chars(80);
00143 Stock *stock;
00144 char c[80];
00145 for (int i = 0; i < stockMarket->getNumberOfStockContainer(); i++) {
00146 stock = stockMarket->getStockContainer(i)->getStock();
00147 sprintf(c,"%d: %s - %s - %d pieces - each %0.2f",i+1,(stock->getName()).c_str(), (stock->getBuyingDate()).c_str(), stock->getAmount(), stock->getBuyingRate());
00148 vector_char.push_back(c);
00149 }
00150 comboStockName->set_popdown_strings(vector_char);
00151 comboStockName->set_use_arrows(true);
00152 comboStockName->set_use_arrows_always(true);
00153 tableEditStockValue->attach(*comboStockName, 0, 1, 1, 2);
00154
00155 hButtonBox = manage( new Gtk::HButtonBox());
00156 hButtonBox->set_spacing(5);
00157 vbox->pack_start(*hButtonBox, Gtk::PACK_SHRINK, 0);
00158
00159 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00160 buttonCancel->signal_clicked().connect(slot(*this,&RemoveStock::quit));
00161 hButtonBox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00162 buttonFinish = manage( new Gtk::Button("Remove Stock"));
00163 buttonFinish->signal_clicked().connect(slot(*this,&RemoveStock::finishRemoveStock));
00164 hButtonBox->pack_start(*buttonFinish, Gtk::PACK_SHRINK, 0);
00165
00166 } else {
00167 set_title("Message");
00168 label = manage (new Gtk::Label("Market is empty!"));
00169 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00170
00171 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00172 buttonCancel->signal_clicked().connect(slot(*this,&RemoveStock::quit));
00173 vbox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00174 }
00175 show_all();
00176 }
00177
00181 void RemoveStock::finishRemoveStock() {
00182 Gtk::Entry *e;
00183 Stock *stock = NULL;
00184 e = comboStockName->get_entry();
00185 string stringVar = e->get_text();
00186 int i = 0;
00187 while ((!(vector_char.empty())) && stock == NULL) {
00188 string s = vector_char.front();
00189 vector_char.pop_front();
00190 if (s == stringVar) {
00191 stock = stockMarket->getStockContainer(i)->getStock();
00192 }
00193 i++;
00194 }
00195 if (stock != NULL) {
00196 bool okay = stockMarket->removeStock(stock);
00197 #ifdef DEBUG
00198 if (okay == true) cout << "Removing the stock returned true." << endl;
00199 else cout << "Removing the stock returned false." << endl;
00200 #endif
00201 } else {
00202 #ifdef DEBUG
00203 cout << "Stock wasn't found in this market!" << endl;
00204 #endif
00205 }
00206 quit();
00207 }
00208
00213 bool RemoveStock::on_delete_event(GdkEventAny *event) {
00214 quit();
00215 return true;
00216 }
00217