]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WORadioButtonList.m
fixed bundle resource lookup on MacOSX, changed resource lookup in
[sope] / sope-appserver / NGObjWeb / DynamicElements / WORadioButtonList.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "WOInput.h"
23
24 @interface WORadioButtonList : WOInput
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28   // WOInput:    name
29   // WOInput:    value
30   // WOInput:    disabled
31 @protected
32   WOAssociation *list;
33   WOAssociation *item;
34   WOAssociation *index;
35   WOAssociation *selection;
36   WOAssociation *prefix;
37   WOAssociation *suffix;
38 }
39
40 @end /* WORadioButtonList */
41
42 #include "common.h"
43
44 // TODO: add support for template? (does WO provide this?)
45
46 @implementation WORadioButtonList
47
48 - (id)initWithName:(NSString *)_name
49   associations:(NSDictionary *)_config
50   template:(WOElement *)_c 
51 {
52   if ((self = [super initWithName:_name associations:_config template:_c])) {
53     self->list      = OWGetProperty(_config, @"list");
54     self->item      = OWGetProperty(_config, @"item");
55     self->index     = OWGetProperty(_config, @"index");
56     self->selection = OWGetProperty(_config, @"selection");
57     self->prefix    = OWGetProperty(_config, @"prefix");
58     self->suffix    = OWGetProperty(_config, @"suffix");
59   }
60   return self;
61 }
62
63 - (void)dealloc {
64   [self->list      release];
65   [self->item      release];
66   [self->index     release];
67   [self->selection release];
68   [self->prefix    release];
69   [self->suffix    release];
70   [super dealloc];
71 }
72
73 /* processing requests */
74
75 - (void)takeValuesFromRequest:(WORequest *)_request
76   inContext:(WOContext *)_ctx
77 {
78   WOComponent *sComponent = [_ctx component];
79   unsigned idx;
80   NSArray  *array;
81   id       formValue;
82   
83   if ([self->disabled boolValueInComponent:sComponent])
84     return;
85   
86   formValue = [_request formValueForKey:OWFormElementName(self, _ctx)];
87   if (formValue == nil)
88     return;
89   
90   idx   = [formValue unsignedIntValue];
91   array = [self->list valueInComponent:sComponent];
92
93   if ([self->index isValueSettable])
94     [self->index setUnsignedIntValue:idx inComponent:sComponent];
95
96   if ([self->item isValueSettable])
97     [self->item setValue:[array objectAtIndex:idx] inComponent:sComponent];
98       
99   if ([self->selection isValueSettable]) {
100     [self->selection setValue:[array objectAtIndex:idx]
101                      inComponent:sComponent];
102   }
103 }
104
105 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
106   return nil;
107 }
108
109 /* generating response */
110
111 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
112   WOComponent *sComponent;
113   NSArray     *array;
114   unsigned    goCount;
115   unsigned    cnt;
116   NSString    *n;
117   id          sel;
118
119   sComponent = [_ctx component];
120   array      = [self->list valueInComponent:sComponent];
121   goCount    = [array count];
122
123   if (goCount <= 0)
124     return;
125
126   n   = OWFormElementName(self, _ctx);
127   sel = [self->selection valueInComponent:sComponent];
128
129   for (cnt = 0; cnt < goCount; cnt++) {
130     id object = [array objectAtIndex:cnt];
131
132     if ([self->index isValueSettable])
133       [self->index setUnsignedIntValue:cnt inComponent:sComponent];
134
135     if ([self->item isValueSettable])
136       [self->item setValue:object inComponent:sComponent];
137
138     if (self->prefix) {
139       WOResponse_AddString(_response,
140                            [self->prefix stringValueInComponent:sComponent]);
141     }
142
143     /* add radio button */
144     {
145       WOResponse_AddCString(_response, "<input type=\"radio\" name=\"");
146       [_response appendContentHTMLAttributeValue:n];
147       WOResponse_AddCString(_response, "\" value=\"");
148       WOResponse_AddInt(_response, cnt);
149       WOResponse_AddCString(_response, "\"");
150
151       if ([sel isEqual:object])
152         WOResponse_AddCString(_response, " checked=\"checked\"");
153       [self appendExtraAttributesToResponse:_response inContext:_ctx];
154       if (self->otherTagString) {
155         WOResponse_AddChar(_response, ' ');
156         WOResponse_AddString(_response,
157                              [self->otherTagString stringValueInComponent:
158                                     [_ctx component]]);
159       }
160       WOResponse_AddCString(_response, " />");
161
162       // the value in a radio list is the string besides the button
163       if (self->value) {
164         NSString *s;
165         
166         s = [self->value stringValueInComponent:sComponent];
167         WOResponse_AddHtmlString(_response, s);
168       }
169     }
170         
171     if (self->suffix) {
172       WOResponse_AddString(_response,
173                            [self->suffix stringValueInComponent:sComponent]);
174     }
175   }
176 }
177
178 /* description */
179
180 - (NSString *)associationDescription {
181   NSMutableString *str;
182
183   str = [NSMutableString stringWithCapacity:128];
184   [str appendString:[super associationDescription]];
185   if (self->list)      [str appendFormat:@" list=%@",      self->list];
186   if (self->item)      [str appendFormat:@" item=%@",      self->item];
187   if (self->index)     [str appendFormat:@" index=%@",     self->index];
188   if (self->prefix)    [str appendFormat:@" prefix=%@",    self->prefix];
189   if (self->suffix)    [str appendFormat:@" suffix=%@",    self->suffix];
190   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
191
192   return str;
193 }
194
195 @end /* WORadioButtonList */