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 "pgradiobutton.h"
00030 #include "pgapplication.h"
00031 #include "pgtheme.h"
00032 #include "pgbutton.h"
00033
00034 #include "propstrings_priv.h"
00035
00036 PG_RadioButton::PG_RadioButton(PG_Widget* parent, const PG_Rect& r, const std::string& text, PG_RadioButton* firstOfGroup, int id, const std::string& style) : PG_ThemeWidget(parent, r) {
00037 PG_Rect rectButton;
00038 PG_Rect rectLabel;
00039
00040 SetID(id);
00041
00042 my_groupFirst = (firstOfGroup == NULL) ? this : firstOfGroup;
00043 my_groupNext = NULL;
00044
00045 my_isPressed = false;
00046 my_storeMarker = false;
00047 my_hoverTransparency = 128;
00048
00049 if(text.empty()) {
00050 rectButton.SetRect(0, 0, r.my_width, r.my_height);
00051 } else {
00052 rectButton.SetRect(0, 0, r.my_height, r.my_height);
00053 }
00054
00055 my_widgetButton = new PG_Button(this, rectButton);
00056 my_widgetButton->SetToggle(true);
00057 my_widgetButton->EnableReceiver(false);
00058 my_widgetButton->sigClick.connect(slot(*this, &PG_RadioButton::handleButtonClick));
00059
00060 rectLabel.SetRect(rectButton.my_width, 0, r.my_width - rectButton.my_width, r.my_height);
00061 my_widgetLabel = new PG_Label(this, rectLabel, text, style);
00062 my_widgetLabel->EnableReceiver(false);
00063
00064
00065 if(style != PG_PropStr::RadioButton) {
00066 LoadThemeStyle(PG_PropStr::RadioButton);
00067 }
00068
00069
00070 LoadThemeStyle(style);
00071
00072 SetTransparency(255);
00073
00074 AddToGroup(this);
00075
00076 if(firstOfGroup == NULL) {
00077 SetPressed();
00078 }
00079 }
00080
00081 void PG_RadioButton::LoadThemeStyle(const std::string& widgettype) {
00082 PG_Theme* t = PG_Application::GetTheme();
00083
00084 t->GetProperty(widgettype, PG_PropStr::RadioButton, PG_PropStr::transparency, my_hoverTransparency);
00085
00086 PG_ThemeWidget::LoadThemeStyle(widgettype, PG_PropStr::RadioButton);
00087
00088 my_widgetButton->LoadThemeStyle(widgettype, "CheckButton");
00089 my_widgetLabel->LoadThemeStyle(widgettype);
00090 }
00091
00092 PG_RadioButton::~PG_RadioButton() {
00093 delete my_widgetLabel;
00094 delete my_widgetButton;
00095 }
00096
00097 void PG_RadioButton::eventMouseEnter() {
00098 SetTransparency(my_hoverTransparency);
00099 Update();
00100 PG_ThemeWidget::eventMouseEnter();
00101 }
00102
00103 void PG_RadioButton::eventMouseLeave() {
00104 SetTransparency(255);
00105 Update();
00106 PG_ThemeWidget::eventMouseLeave();
00107 }
00108
00109 bool PG_RadioButton::eventMouseButtonUp(const SDL_MouseButtonEvent* my_widgetButton) {
00110 PG_RadioButton* list = my_groupFirst;
00111 Uint16 mx;
00112 Uint16 my;
00113 if(my_groupFirst != NULL) {
00114
00115 if(my_widgetButton->button == 4) {
00116 while(list->my_groupNext != NULL) {
00117 if(list->my_groupNext == this) {
00118 mx = (Uint16)(list->my_xpos + 0.5 + ((my_widgetButton->x * 1.0 - list->my_xpos) / list->my_groupNext->my_width) * list->my_width);
00119 my = list->my_ypos + list->my_height / 2;
00120 SDL_WarpMouse(mx, my);
00121 break;
00122 }
00123 list = list->my_groupNext;
00124 }
00125 return true;
00126 }
00127
00128 if(my_widgetButton->button == 5) {
00129 do {
00130 if(list == this && list->my_groupNext != NULL) {
00131 mx = (Uint16)(list->my_groupNext->my_xpos + 0.5 + ((my_widgetButton->x * 1.0 - list->my_groupNext->my_xpos) / list->my_width) * list->my_groupNext->my_width);
00132 my = list->my_groupNext->my_ypos + list->my_groupNext->my_height / 2;
00133 SDL_WarpMouse(mx, my);
00134 break;
00135 }
00136 list = list->my_groupNext;
00137 } while(list != NULL);
00138 return true;
00139 }
00140 }
00141
00142 SetPressed();
00143 return true;
00144 }
00145
00146 bool PG_RadioButton::handleButtonClick(PG_Button* button) {
00147 SetPressed();
00148 return true;
00149 }
00150
00151 void PG_RadioButton::SetPressed() {
00152
00153 PG_RadioButton* list = my_groupFirst;
00154
00155 while(list != NULL) {
00156 if (list->my_isPressed) {
00157 list->ReleaseButton();
00158 }
00159 list = list->my_groupNext;
00160 }
00161
00162 my_widgetButton->SetPressed(true);
00163
00164 if(my_isPressed)
00165 return;
00166
00167 my_isPressed = true;
00168
00169 Update();
00170
00171
00172 sigClick(this, true);
00173 }
00174
00175 bool PG_RadioButton::GetPressed() {
00176 return my_isPressed;
00177 }
00178
00179 void PG_RadioButton::ReleaseButton() {
00180 my_widgetButton->SetPressed(false);
00181 my_isPressed = false;
00182 }
00183
00184 void PG_RadioButton::AddToGroup(PG_RadioButton* w) {
00185 PG_RadioButton* list = my_groupFirst;
00186
00187 while(list->my_groupNext != NULL) {
00188 list = list->my_groupNext;
00189 }
00190
00191 list->my_groupNext = w;
00192 w->my_groupFirst = my_groupFirst;
00193 w->my_groupNext = NULL;
00194 }
00195
00196 void PG_RadioButton::SetText(const std::string& text) {
00197 my_widgetLabel->SetText(text);
00198 }
00199
00200 const PG_String& PG_RadioButton::GetText() {
00201 return my_widgetLabel->GetText();
00202 }
00203
00204 void PG_RadioButton::SetAlignment(PG_Label::TextAlign a) {
00205 my_widgetLabel->SetAlignment(a);
00206 }
00207
00208 void PG_RadioButton::SetFontColor(const PG_Color& Color) {
00209 my_widgetLabel->SetFontColor(Color);
00210 }
00211
00212 void PG_RadioButton::SetSizeByText(int Width, int Height, const std::string& Text) {
00213 my_widgetButton->SetSizeByText();
00214 my_widgetLabel->SetSizeByText(0, 0, Text);
00215
00216 my_widgetLabel->MoveWidget(my_widgetButton->my_width, 0);
00217 SizeWidget(my_widgetButton->my_width + my_widgetLabel->my_width + Width, PG_MAX(my_widgetButton->my_height + Height, my_widgetLabel->my_height));
00218 my_widgetButton->MoveWidget(0, (my_height - my_widgetButton->my_height) >> 1);
00219 }