; ***************************************************************************
; 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_GUI_CHECKBOX_ASM
#define AQH_AVR_GUI_CHECKBOX_ASM


; ***************************************************************************
; This module uses two ressources to show a boolean state:
; - RESSOURCE_IMG_CHECKBOX_UNCHECKED: image showing an unchecked checkbox
; - RESSOURCE_IMG_CHECKBOX_CHECKED:   image showing a checked checkbox
;
; The current state can be set with @ref CheckBox_SetState and retrieved
; with @ref CheckBox_GetState.
; ***************************************************************************




; ***************************************************************************
; defines

.equ CHECKBOX_OFFS_BEGIN       =  IMAGEVIEW_SIZE
.equ CHECKBOX_OFFS_STATE       =  CHECKBOX_OFFS_BEGIN+0
.equ CHECKBOX_SIZE             =  CHECKBOX_OFFS_BEGIN+1


; checkbutton states
.equ CHECKBOX_STATE_UNCHECKED = 0
.equ CHECKBOX_STATE_CHECKED   = 1


; values
.equ CHECKBOX_VALUE_STATE        = IMAGEVIEW_VALUE_NEXTFREE+0
.equ CHECKBOX_VALUE_NEXTFREE     = IMAGEVIEW_VALUE_NEXTFREE+1



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

.cseg



; ---------------------------------------------------------------------------
; @routine CheckBox_new @global
;
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object
; @param X parent widget
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @clobbers any

CheckBox_new:
  ldi r24, LOW(CHECKBOX_SIZE)
  ldi r25, HIGH(CHECKBOX_SIZE)
  bigcall Object_Alloc           ; (!r16, !r17, !X)
  brcc CheckBox_new_ret
  rcall CheckBox_Init               ; (r16, r17, X)
  sec
CheckBox_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CheckBox_Init @global
;
; @param Y address of widget
; @param X parent widget (if any)
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @clobbers r16, r17, X

CheckBox_Init:
  ; call base class
  ldi r20, LOW(RESSOURCE_IMG_CHECKBOX_UNCHECKED)
  ldi r21, HIGH(RESSOURCE_IMG_CHECKBOX_UNCHECKED)
  bigcall ImageView_Init
  
  ; set default signal map
  ldi r16, LOW(CheckBox_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(CheckBox_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

  ret
; @end




; ---------------------------------------------------------------------------
; @routine CheckBox_SetState @global
;
; @param Y address of widget
; @param r16 new state
; @clobbers any, !Y

CheckBox_SetState:
  mov xl, r16
  ldi r17, CHECKBOX_VALUE_STATE
  bigjmp Widget_SetValue
; @end



; ---------------------------------------------------------------------------
; @routine CheckBox_GetState @global
;
; @param Y address of widget
; @return r16 state
; @clobbers any, !Y

CheckBox_GetState:
  ldi r16, WIDGET_SIGNAL_GETVALUE
  ldi r17, CHECKBOX_VALUE_STATE
  bigcall OBJ_HandleSignal
  mov r16, r18
  brcs CheckBox_GetState_ret
  clr r16
CheckBox_GetState_ret:
  ret
; @end





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


; ---------------------------------------------------------------------------
; @routine CheckBox_OnSetValueState @global
;
; @param Y  address of widget
; @param XL new state to set
; @clobbers any, !Y

CheckBox_OnSetValueState:
  mov r16, xl
  rcall checkBoxSetState
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CheckBox_OnGetValueState @global
;
; @param Y    address of widget
; @return r18 current state
; @clobbers any, !Y

CheckBox_OnGetValueState:
  ldd r18, Y+CHECKBOX_OFFS_STATE
  clr r19
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine checkBoxSetState @global
;
; @param Y address of widget
; @clobbers any, !Y

; @param r16 new state to set

checkBoxSetState:
  std Y+CHECKBOX_OFFS_STATE, r16
  ldi xl, LOW(RESSOURCE_IMG_CHECKBOX_UNCHECKED)
  ldi xh, HIGH(RESSOURCE_IMG_CHECKBOX_UNCHECKED)
  ldd r17, Y+OBJECT_OFFS_FLAGS
  cpi r16, CHECKBOX_STATE_UNCHECKED
  breq checkBoxSetState_set
  ldi xl, LOW(RESSOURCE_IMG_CHECKBOX_CHECKED)
  ldi xh, HIGH(RESSOURCE_IMG_CHECKBOX_CHECKED)
checkBoxSetState_set:
  sbr r17, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+OBJECT_OFFS_FLAGS, r17
  bigjmp ImageView_SetRessourceId               ; (r16, r17, r18, r19)
; @end






; ***************************************************************************
; data in FLASH

CheckBox_DefaultSignalmap:
  ; header
  .dw ImageView_DefaultSignalmap*2  ; next table to use
  ; entries
  .db CHECKBOX_VALUE_STATE,     WIDGET_SIGNAL_SETVALUE,  LOW(CheckBox_OnSetValueState), HIGH(CheckBox_OnSetValueState)
  .db CHECKBOX_VALUE_STATE,     WIDGET_SIGNAL_GETVALUE,  LOW(CheckBox_OnGetValueState), HIGH(CheckBox_OnGetValueState)
  .db 0, 0, 0, 0  ; end of table



#endif



