; ***************************************************************************
; copyright   : (C) 2026 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef AQH_AVR_GUI2_BUTTONGROUP_ASM
#define AQH_AVR_GUI2_BUTTONGROUP_ASM


; ***************************************************************************
; This modules assumes all children to be CheckButton widgets.
; Of those children only one is active at any time.
; When a child gets activated a COMMAND signal will be emitted with the selector
; of the child emitting the signal.
; ***************************************************************************



; ***************************************************************************
; code

.cseg



; ***************************************************************************
; signal handlers



ButtonGroup_OnCommand:
  tst xl
  brne ButtonGroup_OnCommand_done
  push r17
    rcall buttonGroupDeselectOthers
  pop r17                              ; re-use selector from original signal
  ldi r16, WIDGET_SIGNAL_COMMAND
  bigcall OBJ_EmitSignalWithSelector
ButtonGroup_OnCommand_done:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine hbuttonGroupDeselectOthers
;
; @param Y address of widget
; @param r17 selector of the selected child

buttonGroupDeselectOthers:
  push yl
    push yh
      bigcall OBJ_GetFirstChild               ; (none)
buttonGroupDeselectOthers_loop:
        brcc buttonGroupDeselectOthers_done
        mov yl, r18
        mov yh, r19
        ldd r16, Y+OBJECT_OFFS_SELECTOR
        cp r16, r17
        breq buttonGroupDeselectOthers_next
        push r17
          clr r16
          bigcall CheckButton_SetState
        pop r17
buttonGroupDeselectOthers_next:
        bigcall OBJ_GetNext
      rjmp buttonGroupDeselectOthers_loop
buttonGroupDeselectOthers_done:
    pop yh
  pop yl
  ret
; @end






#endif

