ui/win32/menu.c

changeset 1037
fbe4bb4eba8c
parent 1036
24677835f298
equal deleted inserted replaced
1036:24677835f298 1037:fbe4bb4eba8c
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "menu.h" 29 #include "menu.h"
30
31 #include <cx/array_list.h>
30 32
31 static ui_menu_add_f createMenuItem[] = { 33 static ui_menu_add_f createMenuItem[] = {
32 /* UI_MENU */ ui_add_menu, 34 /* UI_MENU */ ui_add_menu,
33 /* UI_MENU_ITEM */ ui_add_menu_item, 35 /* UI_MENU_ITEM */ ui_add_menu_item,
34 /* UI_MENU_CHECK_ITEM */ ui_add_menu_checkitem, 36 /* UI_MENU_CHECK_ITEM */ ui_add_menu_checkitem,
162 void ui_checkitem_set(UiInteger *i, int64_t value) { 164 void ui_checkitem_set(UiInteger *i, int64_t value) {
163 i->value = value; 165 i->value = value;
164 menu_stateitem_update(i->obj); 166 menu_stateitem_update(i->obj);
165 } 167 }
166 168
169 static void menu_radioitem_clicked(UiObject *obj, uint64_t id, UiStateMenuItem *item) {
170 UiInteger *i = item->var->value; // UiVar is always not NULL for radio items
171 ui_set(i, item->index+1);
172
173 UiEvent event;
174 event.obj = obj;
175 event.window = obj->window;
176 event.document = obj->ctx->document;
177 event.eventdata = i;
178 event.eventdatatype = UI_EVENT_DATA_INTEGER_VALUE;
179 event.intval = item->state;
180 event.set = ui_get_setop();
181
182 if (item->onchange) {
183 item->onchange(&event, item->userdata);
184 }
185
186 event.intval = (int)ui_get(i);
187 ui_notify_evt(i->observers, &event);
188 }
189
167 void ui_add_menu_radioitem(HMENU parent, int pos, UiMenuItemI *item, UiObject *obj) { 190 void ui_add_menu_radioitem(HMENU parent, int pos, UiMenuItemI *item, UiObject *obj) {
168 191 uint64_t id = ++obj->ctx->command_id_counter;
192
193 UiMenuRadioItem *i = (UiMenuRadioItem*)item;
194 AppendMenu(parent, MF_STRING, id, i->label);
195
196 UiVar *var = uic_widget_var(obj->ctx, obj->ctx, NULL, i->varname, UI_VAR_INTEGER);
197 if (!var) {
198 return; // radio item without var is useless
199 }
200
201 UiInteger *v = var->value;
202 CxList *group = v->obj;
203 if (!group) {
204 // first radio button in this group
205 group = cxArrayListCreate(obj->ctx->allocator, sizeof(UiStateMenuItem), 4);
206 v->obj = group;
207 v->get = ui_radioitem_get;
208 v->set = ui_radioitem_set;
209 }
210
211 UiStateMenuItem sitem = { 0 };
212 sitem.obj = obj;
213 sitem.menu = parent;
214 sitem.id = id;
215 sitem.onchange = i->callback;
216 sitem.userdata = i->userdata;
217 sitem.var = var;
218 sitem.index = (int)cxListSize(group);
219 cxListAdd(group, &sitem);
220
221 if (v->value == sitem.index+1) {
222 sitem.state = 1;
223 menu_stateitem_update(&sitem);
224 }
225
226
227 UiStateMenuItem *sitem_ptr = cxListAt(group, sitem.index);
228 // register command id
229 UiCommand cmd;
230 cmd.callback = (ui_command_func)menu_radioitem_clicked;
231 cmd.userdata = sitem_ptr;
232 cxMapPut(obj->ctx->command_map, id, &cmd);
233 }
234
235 int64_t ui_radioitem_get(UiInteger *i) {
236 return i->value;
237 }
238
239 void ui_radioitem_set(UiInteger *i, int64_t value) {
240 CxList *group = i->obj;
241 // de-select all items
242 CxIterator it = cxListIterator(group);
243 cx_foreach(UiStateMenuItem *, item, it) {
244 if (item->state) {
245 item->state = FALSE;
246 menu_stateitem_update(item);
247 }
248 }
249
250 if (value > 0) {
251 UiStateMenuItem *item = cxListAt(group, value-1);
252 if (item) {
253 item->state = TRUE;
254 menu_stateitem_update(item);
255 }
256 }
169 } 257 }
170 258
171 void ui_add_menu_list(HMENU parent, int pos, UiMenuItemI *item, UiObject *obj) { 259 void ui_add_menu_list(HMENU parent, int pos, UiMenuItemI *item, UiObject *obj) {
172 260
173 } 261 }

mercurial