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 #include <algorithm>
00030
00031 #include "pgdropdown.h"
00032 #include "pglog.h"
00033 #include "pglistbox.h"
00034 #include "pglistboxitem.h"
00035 #include "pgapplication.h"
00036
00037 PG_DropDown::PG_DropDown(PG_Widget* parent, const PG_Rect& r, int id, const std::string& style) : PG_Widget(parent, r),
00038
00039 my_EditBox(NULL), my_DropButton(NULL), my_DropList(NULL) {
00040 PG_Rect rect(0, 0, r.my_width - r.my_height, r.my_height);
00041
00042 SetID(id);
00043
00044 my_EditBox = new PG_LineEdit(this, rect, style);
00045 my_EditBox->sigEditBegin.connect(sigEditBegin.slot());
00046 my_EditBox->sigEditEnd.connect(sigEditEnd.slot());
00047 my_EditBox->sigEditReturn.connect(sigEditReturn.slot());
00048
00049 PG_Rect rbutton(abs(r.my_width - r.my_height), 0, r.my_height, r.my_height);
00050 my_DropButton = new PG_Button(this, rbutton, PG_NULLSTR, -1, style);
00051 my_DropButton->SetID(IDDROPDOWN_BOX);
00052 my_DropButton->sigClick.connect(slot(*this, &PG_DropDown::handleButtonClick));
00053
00054 PG_Rect rlist(r.my_xpos, r.my_ypos + r.my_height +1, r.my_width, r.my_height );
00055 my_DropList = new PG_ListBox(NULL, rlist, style);
00056 my_DropList->SetAutoResize(true, true);
00057 my_DropList->SetShiftOnRemove(false, true);
00058
00059
00060 my_DropList->sigSelectItem.connect(slot(*this, &PG_DropDown::select_handler));
00061 my_DropList->sigDelete.connect(slot(*this, &PG_DropDown::onDropListDeletion));
00062
00063 LoadThemeStyle(style);
00064 }
00065
00066
00067 bool PG_DropDown::onDropListDeletion( const PG_MessageObject* dropList ) {
00068 if ( dropList == my_DropList )
00069 my_DropList = NULL;
00070 return true;
00071 }
00072
00073
00074 PG_DropDown::~PG_DropDown() {
00075 delete my_DropList;
00076 }
00077
00078 void PG_DropDown::LoadThemeStyle(const std::string& style) {
00079 my_EditBox->LoadThemeStyle(style);
00080 my_DropButton->LoadThemeStyle(style);
00081 my_DropList->LoadThemeStyle(style);
00082 }
00083
00084 void PG_DropDown::AddChild(PG_Widget* child) {
00085 if (my_EditBox == NULL || my_DropButton == NULL || my_DropList == NULL) {
00086 PG_Widget::AddChild(child);
00087 } else {
00088 my_DropList->AddChild(child);
00089
00090 }
00091 }
00092
00093
00094 void PG_DropDown::AddItem(const std::string& text, void* userdata, Uint16 height) {
00095 Uint16 h = height;
00096 static PG_String ytext = "A";
00097
00098 if(height == 0) {
00099 PG_FontEngine::GetTextSize(ytext, GetFont(), NULL, NULL, NULL, NULL, &h);
00100 h += 2;
00101 }
00102
00103 new PG_ListBoxItem(this, h, text, NULL, userdata);
00104
00105 }
00106
00107
00108 void PG_DropDown::RemoveAll() {
00109 if ( my_DropList )
00110 my_DropList->RemoveAll();
00111 }
00112
00113 void PG_DropDown::DeleteAll() {
00114 if ( my_DropList )
00115 my_DropList->DeleteAll();
00116 }
00117
00118 void PG_DropDown::eventShow() {
00119 if ( my_DropList )
00120 my_DropList->SetVisible(false);
00121 }
00122
00123 void PG_DropDown::eventHide() {
00124 if ( my_DropList )
00125 my_DropList->Hide();
00126 }
00127
00128 bool PG_DropDown::handleButtonClick(PG_Button* button) {
00129 if(button->GetID() != IDDROPDOWN_BOX) {
00130 return false;
00131 }
00132
00133 if(my_DropList->IsVisible()) {
00134 my_DropList->Hide();
00135 } else {
00136 PG_Rect pos = *my_DropList;
00137 pos.x = my_xpos;
00138 pos.y = my_ypos+my_height;
00139
00140 pos.w = PG_MIN( my_DropList->my_width, PG_Application::GetScreenWidth() - pos.x );
00141 pos.h = PG_MIN( my_DropList->my_height, PG_Application::GetScreenHeight() - pos.y);
00142
00143 my_DropList->MoveWidget( pos );
00144
00145 my_DropList->Show();
00146 }
00147
00148 return true;
00149 }
00150
00151 void PG_DropDown::SetIndent(Uint16 indent) {
00152 my_DropList->SetIndent(indent);
00153 }
00154
00155 void PG_DropDown::SetEditable(bool edit) {
00156 my_EditBox->SetEditable(edit);
00157 }
00158
00159 bool PG_DropDown::GetEditable() {
00160 return my_EditBox->GetEditable();
00161 }
00162
00163 const PG_String& PG_DropDown::GetText() {
00164 return my_EditBox->GetText();
00165 }
00166
00167 void PG_DropDown::SetText(const std::string& new_text) {
00168 my_EditBox->SetText(new_text);
00169 }
00170
00171 bool PG_DropDown::eventSelectItem(PG_ListBoxBaseItem* item) {
00172 return false;
00173 }
00174
00175 void PG_DropDown::eventSizeWidget(Uint16 w, Uint16 h) {}
00176
00177 void PG_DropDown::eventMoveWidget(int x, int y) {
00178 if(my_DropList && my_DropList->IsVisible()) {
00179 my_DropList->Hide();
00180 }
00181 }
00182
00183 bool PG_DropDown::select_handler(PG_ListBoxBaseItem* item) {
00184 my_EditBox->SetText(item->GetText());
00185 item->Select(false);
00186 my_DropList->SelectItem(NULL);
00187 my_DropList->Hide();
00188
00189 eventSelectItem(item);
00190 sigSelectItem(item);
00191
00192 return true;
00193 }
00194
00195 bool PG_DropDown::ProcessEvent(const SDL_Event * event, bool bModal) {
00196
00197 if(bModal && my_DropList->IsVisible()) {
00198 if(my_DropList->ProcessEvent(event, true)) {
00199 return true;
00200 }
00201 }
00202
00203 return PG_Widget::ProcessEvent(event, bModal);
00204 }
00205
00206 void PG_DropDown::SelectFirstItem() {
00207 my_DropList->SelectFirstItem();
00208 }
00209
00210 void PG_DropDown::SelectNextItem() {
00211 my_DropList->SelectNextItem();
00212 }
00213
00214 void PG_DropDown::SelectPrevItem() {
00215 my_DropList->SelectPrevItem();
00216 }
00217
00218 void PG_DropDown::SelectItem(const int n) {
00219 int i;
00220
00221 my_DropList->SelectFirstItem(false);
00222
00223 for (i=0; i < n; i++)
00224 my_DropList->SelectNextItem( i == (n-1));
00225 }
00226
00227 int PG_DropDown::GetSelectedItemIndex() {
00228 return my_DropList->GetSelectedIndex();
00229 }
00230
00231
00232 Uint16 PG_DropDown::GetIndent() {
00233 return my_DropList->GetIndent();
00234 }
00235
00236 void PG_DropDown::SetAlignment(PG_Label::TextAlign style) {
00237 my_DropList->SetAlignment(style);
00238 }
00239
00240 PG_Label::TextAlign PG_DropDown::GetAlignment() {
00241 return my_DropList->GetAlignment();
00242 }
00243
00244 PG_Widget* PG_DropDown::GetFirstInList() {
00245 return my_DropList->GetFirstInList();
00246 }