1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "WOInput.h"
@class WOAssociation;
@interface WOPopUpButton : WOInput
{
// WODynamicElement: extraAttributes
// WODynamicElement: otherTagString
// WOInput: name
// WOInput: value
// WOInput: disabled
@protected
WOAssociation *list;
WOAssociation *item;
WOAssociation *selection;
WOAssociation *string; // WO4
WOAssociation *noSelectionString; // WO4
WOAssociation *selectedValue; // WO4.5
WOAssociation *escapeHTML; // WO4.5
WOAssociation *itemGroup; // SOPE
WOElement *template; // SOPE?
}
@end
@class WOResponse, WOContext;
@interface WOPopUpButton(PrivateMethods)
- (void)appendOptionsToResponse:(WOResponse *)_response
inContext:(WOContext *)_ctx;
@end
#include "decommon.h"
#ifdef DEBUG
static int profElements = -1;
static Class NSDateClass = Nil;
@interface WOContext(ComponentStackCount)
- (unsigned)componentStackCount;
@end
#endif
@implementation WOPopUpButton
static NSNumber *yesNum = nil;
static BOOL debugPopUp = NO;
+ (void)initialize {
if (yesNum == nil) yesNum = [[NSNumber numberWithBool:YES] retain];
}
- (void)_handleDeprecatedBindings:(NSDictionary *)_config {
id tmp;
if ((tmp = OWGetProperty(_config, @"singleSelection"))) {
if ([tmp isValueConstant]) {
if ([tmp boolValueInComponent:nil]) {
[self debugWithFormat:
@"Note: template uses deprecated 'singleSelection' binding!"];
}
else {
[self errorWithFormat:
@"'singleSelection' binding is set to NO, which is "
@"unsupported now!"];
}
}
else {
[self errorWithFormat:
@"will ignore deprecated 'singleSelection' binding: %@", tmp];
}
[tmp release];
}
}
- (id)initWithName:(NSString *)_name
associations:(NSDictionary *)_config
template:(WOElement *)_t
{
#if DEBUG
if (profElements == -1) {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
profElements = [[ud objectForKey:@"WOProfileElements"] boolValue] ? 1 : 0;
}
if (NSDateClass == Nil)
NSDateClass = [NSDate class];
#endif
if ((self = [super initWithName:_name associations:_config template:_t])) {
self->list = OWGetProperty(_config, @"list");
self->item = OWGetProperty(_config, @"item");
self->selection = OWGetProperty(_config, @"selection");
self->string = OWGetProperty(_config, @"string");
if (self->string == nil )
self->string = OWGetProperty(_config, @"displayString");
self->noSelectionString = OWGetProperty(_config, @"noSelectionString");
self->selectedValue = OWGetProperty(_config, @"selectedValue");
self->escapeHTML = OWGetProperty(_config, @"escapeHTML");
self->itemGroup = OWGetProperty(_config, @"itemGroup");
self->template = [_t retain];
if (self->selection != nil && self->selectedValue != nil)
[self logWithFormat:
@"cannot have both 'selection' and 'selectedValue' bindings!"];
/* compatibility */
if (self->noSelectionString == nil)
self->noSelectionString = OWGetProperty(_config, @"nilString");
if (self->escapeHTML == nil)
self->escapeHTML = [[WOAssociation associationWithValue:yesNum] retain];
[self _handleDeprecatedBindings:_config];
}
return self;
}
- (void)dealloc {
[self->noSelectionString release];
[self->escapeHTML release];
[self->list release];
[self->item release];
[self->selection release];
[self->string release];
[self->selectedValue release];
[self->itemGroup release];
[super dealloc];
}
/* handling the request */
- (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
WOComponent *sComponent;
NSString *formValue;
NSArray *objects;
id object;
sComponent = [_ctx component];
if ([self->disabled boolValueInComponent:[_ctx component]])
return;
formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)];
if (debugPopUp) {
[self logWithFormat:@"%@ / %@: value=%@ ..", OWFormElementName(self, _ctx),
[_ctx elementID], formValue];
}
if (formValue == nil) {
/* nothing changed, or not in submitted form */
if (debugPopUp) [self logWithFormat:@"found no form value!"];
return;
}
objects = [self->list valueInComponent:sComponent];
object = nil;
if (self->value != nil) {
/* has a value binding, walk list to find object */
unsigned i, toGo;
if (debugPopUp) [self logWithFormat:@"scan value: %@", self->value];
for (i = 0, toGo = [objects count]; i < toGo; i++) {
NSString *cv;
object = [objects objectAtIndex:i];
if ([self->item isValueSettable])
[self->item setValue:object inComponent:sComponent];
cv = [self->value stringValueInComponent:sComponent];
if ([cv isEqualToString:formValue])
break;
// important, reset object otherwise the last item will be preselected!
object = nil;
}
}
else if (![formValue isEqualToString:WONoSelectionString]) {
/* an index binding */
int idx;
idx = [formValue intValue];
if (idx >= (int)[objects count]) {
[[_ctx page] logWithFormat:@"popup-index %i out of range 0-%i",
idx, [objects count] - 1];
object = nil;
}
else
object = [objects objectAtIndex:idx];
}
if ([self->selectedValue isValueSettable])
[self->selectedValue setValue:formValue inComponent:sComponent];
/* process selection */
if ([self->selection isValueSettable]) {
NSArray *sel;
if (object != nil) {
sel = [object retain];
}
else /* nil item selected */
sel = nil;
[self->selection setValue:sel inComponent:sComponent];
[sel release]; sel = nil;
}
if ([self->item isValueSettable])
[self->item setValue:nil inComponent:sComponent]; // Reset 'item'
}
/* generate response */
- (void)appendOptionsToResponse:(WOResponse *)_response
inContext:(WOContext *)_ctx
{
WOComponent *sComponent = [_ctx component];
NSString *nilStr = nil;
NSArray *array = nil;
id sel = nil;
int i, toGo;
BOOL escapesHTML;
BOOL byVal;
id previousGroup = nil;
#if DEBUG
NSTimeInterval st = 0.0;
if (profElements)
st = [[NSDateClass date] timeIntervalSince1970];
#endif
escapesHTML = [self->escapeHTML boolValueInComponent:sComponent];
nilStr = [self->noSelectionString stringValueInComponent:sComponent];
array = [self->list valueInComponent:sComponent];
if (self->selection == nil){
if (self->selectedValue != nil){
byVal = YES;
sel = [self->selectedValue valueInComponent:sComponent];
}
else{
byVal = NO;
sel = nil;
}
}
else{
if (self->selectedValue != nil){
byVal = YES;
sel = [self->selectedValue valueInComponent:sComponent];
NSLog(@"WARNING(%@): "
@"using both 'selection' and 'selectedValue' bindings!",
self);
}
else{
byVal = NO;
sel = [self->selection valueInComponent:sComponent];
}
}
toGo = [array count];
#if DEBUG
if (profElements) {
NSTimeInterval diff;
int j;
diff = [[NSDateClass date] timeIntervalSince1970] - st;
if (diff > 0.001) {
for (j = [_ctx componentStackCount]; j >= 0; j--)
printf(" ");
printf("PopUpOption[setup] %s: %0.3fs\n",
[[_ctx elementID] cString], diff);
}
}
#endif
if (nilStr != nil) {
if (self->itemGroup != nil) {
id group;
if ([self->item isValueSettable])
[self->item setValue:nil inComponent:sComponent];
group = [self->itemGroup stringValueInComponent:sComponent];
if (group != nil) {
WOResponse_AddCString(_response, "<optgroup label=\"");
if (escapesHTML) {
WOResponse_AddHtmlString(_response, group);
}
else {
WOResponse_AddString(_response, group);
}
WOResponse_AddCString(_response, "\">");
previousGroup = [group retain];
}
}
WOResponse_AddCString(_response, "<option value=\"");
WOResponse_AddString(_response, WONoSelectionString);
WOResponse_AddCString(_response, "\">");
WOResponse_AddHtmlString(_response, nilStr);
WOResponse_AddCString(_response, "</option>");
// FIXME (stephane) Shouldn't we set the 'selected' if selArray/selValueArray is empty?
}
for (i = 0; i < toGo; i++) {
NSString *v = nil;
NSString *displayV = nil;
id object;
BOOL isSelected;
id group;
#if DEBUG
NSTimeInterval st = 0.0;
if (profElements)
st = [[NSDateClass date] timeIntervalSince1970];
#endif
object = [array objectAtIndex:i];
if ([self->item isValueSettable])
[self->item setValue:object inComponent:sComponent];
isSelected = sel ? [sel isEqual:object] : NO;
v = (self->value != nil)
? [self->value stringValueInComponent:sComponent]
: (NSString *)[NSString stringWithFormat:@"%i", i];
if (byVal){
isSelected = sel ? [sel isEqual:v] : NO;
}
else
isSelected = sel ? [sel isEqual:object] : NO;
displayV = self->string
? [self->string stringValueInComponent:sComponent]
: [object stringValue];
if (displayV == nil) displayV = (escapesHTML ? @"<nil>" : @"<nil>");
group = self->itemGroup != nil
? [self->itemGroup stringValueInComponent:sComponent]
: (NSString *)nil;
if (group != nil) {
BOOL groupChanged = NO;
if (previousGroup == nil)
groupChanged = YES;
else {
if (![group isEqualToString:previousGroup]) {
WOResponse_AddCString(_response, "</optgroup>");
groupChanged = YES;
}
}
if (groupChanged) {
WOResponse_AddCString(_response, "<optgroup label=\"");
if (escapesHTML) {
WOResponse_AddHtmlString(_response, group);
}
else {
WOResponse_AddString(_response, group);
}
WOResponse_AddCString(_response, "\">");
ASSIGN(previousGroup, group);
}
}
else {
if (previousGroup != nil) {
WOResponse_AddCString(_response, "</optgroup>");
ASSIGN(previousGroup, nil);
}
}
WOResponse_AddCString(_response, "<option value=\"");
WOResponse_AddHtmlString(_response, v); // WO escapes it, always
if (isSelected) {
WOResponse_AddString(_response,
_ctx->wcFlags.allowEmptyAttributes
? @"\" selected>" : @"\" selected=\"selected\">");
}
else {
WOResponse_AddString(_response, @"\">");
}
if (escapesHTML){
WOResponse_AddHtmlString(_response, displayV);
}
else{
WOResponse_AddString(_response, displayV);
}
WOResponse_AddCString(_response, "</option>");
#if DEBUG
if (profElements) {
NSTimeInterval diff;
int j;
diff = [[NSDateClass date] timeIntervalSince1970] - st;
if (diff > 0.001) {
#if 1
for (j = [_ctx componentStackCount]; j >= 0; j--)
printf(" ");
#endif
printf("PopUpOption[%i] %s: %0.3fs\n", i,
[[_ctx elementID] cString], diff);
}
}
#endif
}
if (previousGroup != nil) {
WOResponse_AddCString(_response, "</optgroup>");
[previousGroup release];
}
if ([self->item isValueSettable])
[self->item setValue:nil inComponent:sComponent]; // Reset 'item'
}
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
#if DEBUG
NSTimeInterval st = 0.0;
if (profElements)
st = [[NSDateClass date] timeIntervalSince1970];
#endif
if ([_ctx isRenderingDisabled]) {
[self->template appendToResponse:_response inContext:_ctx];
}
else {
WOResponse_AddCString(_response, "<select name=\"");
[_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
WOResponse_AddChar(_response, '"');
[self appendExtraAttributesToResponse:_response inContext:_ctx];
if (self->otherTagString != nil) {
WOResponse_AddChar(_response, ' ');
WOResponse_AddString(_response,
[self->otherTagString stringValueInComponent:
[_ctx component]]);
}
if ([self->disabled boolValueInComponent:[_ctx component]])
WOResponse_AddCString(_response, " disabled=\"disabled\"");
WOResponse_AddChar(_response, '>');
[self appendOptionsToResponse:_response inContext:_ctx];
[self->template appendToResponse:_response inContext:_ctx];
WOResponse_AddCString(_response, "</select>");
}
#if DEBUG
if (profElements) {
NSTimeInterval diff;
int i;
diff = [[NSDateClass date] timeIntervalSince1970] - st;
if (diff > 0.001) {
#if 1
for (i = [_ctx componentStackCount]; i >= 0; i--)
printf(" ");
#endif
printf("PopUpButton %s: %0.3fs\n",
[[_ctx elementID] cString], diff);
}
}
#endif
}
/* description */
- (NSString *)associationDescription {
NSMutableString *str;
str = [NSMutableString stringWithCapacity:256];
[str appendString:[super associationDescription]];
if (self->list) [str appendFormat:@" list=%@", self->list];
if (self->item) [str appendFormat:@" item=%@", self->item];
if (self->selection) [str appendFormat:@" selection=%@", self->selection];
if (self->string) [str appendFormat:@" displayString=%@", self->string];
if (self->noSelectionString)
[str appendFormat:@" noselection=%@", self->noSelectionString];
if (self->escapeHTML)
[str appendFormat:@" escapeHTML=%@", self->escapeHTML];
if (self->selectedValue)
[str appendFormat:@" selectedValue=%@", self->selectedValue];
if (self->itemGroup)
[str appendFormat:@" itemGroup=%@", self->itemGroup];
return str;
}
@end /* WOPopUpButton */
|