AskGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2005  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include "pent_include.h"
00021 #include "AskGump.h"
00022 #include "ButtonWidget.h"
00023 #include "UCList.h"
00024 #include "UCMachine.h"
00025 #include "IDataSource.h"
00026 #include "ODataSource.h"
00027 
00028 
00029 // p_dynamic_class stuff 
00030 DEFINE_RUNTIME_CLASSTYPE_CODE(AskGump,ItemRelativeGump);
00031 
00032 AskGump::AskGump()
00033         : ItemRelativeGump(), answers(0)
00034 {
00035 
00036 }
00037 
00038 AskGump::AskGump(uint16 owner, UCList *answers_) : 
00039         ItemRelativeGump(0, 0, 0, 0, owner, FLAG_KEEP_VISIBLE, LAYER_ABOVE_NORMAL),
00040         answers(new UCList(2))
00041 {
00042         answers->copyStringList(*answers_);
00043 }
00044 
00045 AskGump::~AskGump()
00046 {
00047         answers->freeStrings();
00048         delete answers;
00049 }
00050 
00051 // Init the gump, call after construction
00052 void AskGump::InitGump(Gump* newparent, bool take_focus)
00053 {
00054         // OK, this is a bit of a hack, but it's how it has to be
00055         int     fontnum;
00056         if (owner == 1) fontnum = 6;
00057         else if (owner > 256) fontnum = 8;
00058         else switch (owner%3) {
00059                 case 1:
00060                         fontnum = 5;
00061                         break;
00062                         
00063                 case 2:
00064                         fontnum = 7;
00065                         break;
00066 
00067                 default:
00068                         fontnum = 0;
00069                         break;
00070         }
00071 
00072         int px = 0, py = 0;
00073 
00074         // This is a hack. We init the gump twice...
00075         ItemRelativeGump::InitGump(newparent, take_focus);
00076 
00077         for (unsigned int i = 0; i < answers->getSize(); ++i) {
00078                 std::string str_answer = "@ ";
00079                 str_answer += UCMachine::get_instance()->getString(answers->getStringIndex(i));
00080 
00081                 ButtonWidget *child = new ButtonWidget(px, py, str_answer,
00082                                                                                            true, fontnum);
00083                 child->InitGump(this);
00084                 child->SetIndex(i);
00085 
00086                 Pentagram::Rect cd;
00087                 child->GetDims(cd);
00088                 if (i+1 < answers->getSize())
00089                         cd.h += child->getVlead();
00090 
00091                 if (px+cd.w > 160 && px != 0) 
00092                 {
00093                         py = dims.h;
00094                         px = 0;
00095                         child->Move(px,py);
00096                 }
00097 
00098                 if (cd.w+px > dims.w) dims.w = cd.w+px;
00099                 if (cd.h+py > dims.h) dims.h = cd.h+py;
00100 
00101                 px += cd.w+4;
00102         }
00103 
00104         // Wait with ItemRelativeGump initialization until we calculated our size.
00105         ItemRelativeGump::InitGump(newparent, take_focus);
00106 }
00107 
00108 void AskGump::ChildNotify(Gump *child, uint32 message)
00109 {
00110         if (message == ButtonWidget::BUTTON_CLICK) 
00111         {
00112                 uint16 s = answers->getStringIndex(child->GetIndex());
00113                 process_result = s;
00114 
00115                 // answers' strings are going to be deleted, so make sure
00116                 // the response string won't be deleted
00117                 answers->removeString(s, true); 
00118 
00119                 Close();
00120         }
00121 }
00122 
00123 void AskGump::saveData(ODataSource* ods)
00124 {
00125         ItemRelativeGump::saveData(ods);
00126 
00127         answers->save(ods);
00128 }
00129 
00130 bool AskGump::loadData(IDataSource* ids, uint32 version)
00131 {
00132         if (!ItemRelativeGump::loadData(ids, version)) return false;
00133 
00134         answers = new UCList(2);
00135         answers->load(ids, version);
00136 
00137         // HACK ALERT
00138         int px = 0, py = 0;
00139 
00140         dims.w = 0;
00141         dims.h = 0;
00142 
00143 
00144         for (unsigned int i = 0; i < answers->getSize(); ++i) {
00145 
00146                 ButtonWidget *child = 0;
00147 
00148                 std::list<Gump*>::iterator it;
00149                 for (it = children.begin(); it != children.end(); ++it) {
00150                         if ((*it)->GetIndex() != (int)i) continue;
00151                         child = p_dynamic_cast<ButtonWidget*>(*it);
00152                         if (!child) continue;
00153                 }
00154 
00155                 if (!child) return false;
00156 
00157                 Pentagram::Rect cd;
00158                 child->GetDims(cd);
00159 
00160                 if (px+cd.w > 160 && px != 0) 
00161                 {
00162                         py = dims.h;
00163                         px = 0;
00164                 }
00165                 child->Move(px,py);
00166 
00167                 if (cd.w+px > dims.w) dims.w = cd.w+px;
00168                 if (cd.h+py > dims.h) dims.h = cd.h+py;
00169 
00170                 px += cd.w+4;
00171         }
00172 
00173         return true;
00174 }
00175 
00176 // You should always use Protection

Generated on Fri Jul 27 22:27:09 2007 for pentagram by  doxygen 1.4.7