(bind-func Widget_radio_c
(lambda (label:i8* cb:[void,i32]* nstates:i32)
(let ((lab (String label))
(state:i32 0)
(old_state:i32 -1)
(click_p:i1 0)
(prev_click_p:i1 0))
(lambda (widget:Widget* bounds:Rect*)
(let ((vg (GUI_context EXTEMPORE_GUI))
(parent (Widget_parent widget))
(vertical_p (or (null? parent)
(Widget_split_vertical_p parent)))
(pos (if vertical_p
(/ (- (gui_mouse_x) (tref bounds 0))
(tref bounds 2))
(/ (- (gui_mouse_y) (tref bounds 1))
(tref bounds 3))))
(button_bounds:Rect* (salloc)))
;; toggle state
(set! click_p (gui_left_click_p bounds))
(if (and click_p (not prev_click_p))
(begin
(set! state (convert (* (convert nstates float) pos)))
;; fire callback if state has changed
(if (<> state old_state)
(cb state))
(set! old_state state)))
(set! prev_click_p click_p)
;; draw widgets
(doloop (i nstates)
(if vertical_p
(tfill! button_bounds
(+ (tref bounds 0)
(* (/ (tref bounds 2) (convert nstates)) (convert i)))
(tref bounds 1)
(/ (tref bounds 2) (convert nstates))
(tref bounds 3))
(tfill! button_bounds
(tref bounds 0)
(+ (tref bounds 1)
(* (/ (tref bounds 3) (convert nstates)) (convert i)))
(tref bounds 2)
(/ (tref bounds 3) (convert nstates))))
(nvgBeginPath vg)
(nvgStrokeColor vg GUI_COLOR_BG)
(if (= i state)
(nvgFillColor vg GUI_COLOR_MAGENTA_L)
(nvgFillColor vg GUI_COLOR_MAGENTA_D))
(gui_draw_rect button_bounds)
(nvgFill vg)
(nvgStroke vg))
;; (nvgFillColor vg GUI_COLOR_BG)
;; (nvgBeginPath vg)
;; (gui_draw_text_in_bounds bounds lab 30.)
;; (nvgFill vg)
void)))))