; ***************************************************************************
; 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_AQHOME_D_NODEVALUEID_ASM
#define AQH_AVR_GUI2_AQHOME_D_NODEVALUEID_ASM


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


; widget
.equ DLGNODEVALUEID_OFFS_BEGIN            = CDIALOG_SIZE
.equ DLGNODEVALUEID_OFFS_NODEADDR         = DLGNODEVALUEID_OFFS_BEGIN+0
.equ DLGNODEVALUEID_OFFS_VALUEID          = DLGNODEVALUEID_OFFS_BEGIN+1
.equ DLGNODEVALUEID_SIZE                  = DLGNODEVALUEID_OFFS_BEGIN+2


; selectors
.equ DLGNODEVALUEID_SEL_NODEADDR     = DIALOG_SEL_NEXTFREE+0
.equ DLGNODEVALUEID_SEL_VALUEID      = DIALOG_SEL_NEXTFREE+1
.equ DLGNODEVALUEID_SEL_NEXTFREE     = DIALOG_SEL_NEXTFREE+2




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

.cseg



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_new @global
;
; @param  Y       pointer to GUIAPP
; @param  r21:r20 ressource id for title
; @return CFLAG   set of okay, cleared otherwise
; @return Y       address of newly created object

DlgNodeValueId_new:
  bigcall GuiApp_GetRootWindow        ; r19:r18=root window (none)
  brcc DlgNodeValueId_new_ret
  mov xl, r18                         ; use root window as parent for main window
  mov xh, r19
  ldi r24, LOW(DLGNODEVALUEID_SIZE)
  ldi r25, HIGH(DLGNODEVALUEID_SIZE)
  push r20
    push r21
      bigcall Object_Alloc            ; Y=space (!r16, !r17, !X)
    pop r21
  pop r20
  brcc DlgNodeValueId_new_ret
  rcall DlgNodeValueId_Init
DlgNodeValueId_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_Init @global
;
; @param  Y       pointer to dialog
; @param  X       pointer to root window
; @param  r21:r20 ressource id for title
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgNodeValueId_Init:
  ldi r16, (1<<DIALOG_OPTS_STDBTNS_BIT)                                                                ; OPTS
  ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
  ldi r22, LOW(DlgNodeValueId_Config*2)
  ldi r23, HIGH(DlgNodeValueId_Config*2)
  bigcall CDialog_Init               ; Y=main window
  brcc DlgNodeValueId_Init_ret

  ; set default signal map
  ldi r16, LOW(DlgNodeValueId_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(DlgNodeValueId_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

DlgNodeValueId_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_SetNodeAddr @global
;
; @param Y pointer to dialog
; @param X new value (only xl used)
; @clobbers none

DlgNodeValueId_SetNodeAddr:
  std Y+DLGNODEVALUEID_OFFS_NODEADDR, xl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_GetNodeAddr @global
;
; @param  Y       pointer to dialog
; @return r19:r18 value
; @clobbers none

DlgNodeValueId_GetNodeAddr:
  ldd r18, Y+DLGNODEVALUEID_OFFS_NODEADDR
  clr r19
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_SetValueId @global
;
; @param Y pointer to dialog
; @param X new value (only xl used)
; @clobbers none

DlgNodeValueId_SetValueId:
  std Y+DLGNODEVALUEID_OFFS_VALUEID, xl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_GetValueId @global
;
; @param  Y       pointer to dialog
; @return r19:r18 value
; @clobbers none

DlgNodeValueId_GetValueId:
  ldd r18, Y+DLGNODEVALUEID_OFFS_VALUEID
  clr r19
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_toGui @global
;
; @param  Y       pointer to dialog
; @clobbers any, !Y, !Z

DlgNodeValueId_toGui:
  ; save node addr
  ldd xl, Y+DLGNODEVALUEID_OFFS_NODEADDR
  clr xh
  ldi r16, DLGNODEVALUEID_SEL_NODEADDR
  bigcall CDialog_SetChildValue
  ; save value id
  ldd xl, Y+DLGNODEVALUEID_OFFS_VALUEID
  clr xh
  ldi r16, DLGNODEVALUEID_SEL_VALUEID
  bigjmp CDialog_SetChildValue
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_fromGui @global
;
; @param  Y       pointer to dialog
; @clobbers any, !Y, !Z

DlgNodeValueId_fromGui:
  ldi r16, DLGNODEVALUEID_SEL_NODEADDR
  bigcall CDialog_GetChildValue
  std Y+DLGNODEVALUEID_OFFS_NODEADDR, r18

  ldi r16, DLGNODEVALUEID_SEL_VALUEID
  bigcall CDialog_GetChildValue
  std Y+DLGNODEVALUEID_OFFS_VALUEID, r18
  ret
; @end





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


; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_OnToGui @global
;
; @param Y pointer to dialog
; @clobbers any, !Y

DlgNodeValueId_OnToGui:
  rcall DlgNodeValueId_toGui
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgNodeValueId_OnFromGui @global
;
; @param Y pointer to dialog
; @clobbers any, !Y

DlgNodeValueId_OnFromGui:
  rcall DlgNodeValueId_fromGui
  sec
  ret
; @end








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



; dialog setup data
DlgNodeValueId_Config:
  .db CWIDGETCFG_MAINWIDGET_TYPE_MLAYOUT, 0                 ; TYPE, FLAGS
  .dw 0                                                     ; STYLE
  .db 2, 0                                                  ; PARAM1, PARAM2
  
  ; line 1, left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
         (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_NODEADDR                                ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 1, right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db DLGNODEVALUEID_SEL_NODEADDR, 0                        ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 254                                                ; MINVAL, MAXVAL

  ; line 2, left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_VALUEID                                 ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL


  ; line 2, right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db DLGNODEVALUEID_SEL_VALUEID, 0                         ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 255                                                ; MINVAL, MAXVAL

  ; end of configuration
  .dw 0



; signal map
DlgNodeValueId_DefaultSignalmap:
  ; header
  .dw CDialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                     DIALOG_SIGNAL_TOGUI,      LOW(DlgNodeValueId_OnToGui),         HIGH(DlgNodeValueId_OnToGui)
  .db 0,                     DIALOG_SIGNAL_FROMGUI,    LOW(DlgNodeValueId_OnFromGui),       HIGH(DlgNodeValueId_OnFromGui)
  ; end of table
  .db 0, 0, 0, 0



#endif

