; ***************************************************************************
; 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_BASE_CDIALOG_ASM
#define AQH_AVR_GUI_BASE_CDIALOG_ASM


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

; CDIALOG data
.equ CDIALOG_OFFS_BEGIN            = DIALOG_SIZE
.equ CDIALOG_OFFS_CONFIG_LO        = CDIALOG_OFFS_BEGIN+0
.equ CDIALOG_OFFS_CONFIG_HI        = CDIALOG_OFFS_BEGIN+1
.equ CDIALOG_SIZE                  = CDIALOG_OFFS_BEGIN+2



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

.cseg



; ---------------------------------------------------------------------------
; @routine CDialog_Init @global
;
; @param Y pointer to MainWindow
; @param X pointer to root window
; @param r16 value for OBJECT_OFFS_OPTS
; @param R21:R20 title ressource
; @param r23:r22 pointer to settings (byte addr for LPM!)
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

CDialog_Init:
  push r22
    push r23
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      bigcall Dialog_Init
    pop r23
  pop r22
  brcc CDialog_Init_ret

  ; store config
  std Y+CDIALOG_OFFS_CONFIG_LO, r22
  std Y+CDIALOG_OFFS_CONFIG_HI, r23

  ; set default signal map
  ldi r16, LOW(CDialog_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(CDialog_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16
  
CDialog_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CDialog_CreateContent @global
;
; @param Y pointer to MainWindow
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

CDialog_CreateContent:
  ; create content
  bigcall MainWindow_GetContentWidget  ; r19:r18=content window
  brcc CDialog_CreateContent_ret
  mov xl, r18                          ; use content window as parent
  mov xh, r19
  push zl
    push zh
      push xl                              ; content window
        push xh
          ldd zl, Y+CDIALOG_OFFS_CONFIG_LO
          ldd zh, Y+CDIALOG_OFFS_CONFIG_HI
          push yl                          ; main window
            push yh
              ldi r16, 0
              ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT)
              bigcall CWidget_Create
            pop yh
          pop yl
        pop xh
      pop xl
    pop zh
  pop zl
CDialog_CreateContent_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CDialog_GetChildBySelector
;
; @param  Y       pointer to MainWindow
; @param  r16     selector to search for
; @return CFLAG   set if widget found, cleared otherwise
; @return r19:r18 resulting widget
; @clobbers R16-R19

CDialog_GetChildBySelector:
  push r16
    bigcall MainWindow_GetFirstChildOfContentWidget   ; r19:r18=result (r16)
  pop r16
  brcc CDialog_GetChildBySelector_ret
  push yl
    push yh
      mov yl, r18
      mov yh, r19
      bigcall OBJ_GetChildBySelector                      ; (R17, R18, R19)
    pop yh
  pop yl
CDialog_GetChildBySelector_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CDialog_SetChildValue
;
; @param Y pointer to dialog
; @param X   new value
; @param R16 selector of child to set value to
; @clobbers any, !Y, !Z

CDialog_SetChildValue:
  rcall CDialog_GetChildBySelector   ; r19:r18=result (R16-R19)
  brcc CDialog_SetChildValue_ret
  push yl
    push yh
      mov yl, r18
      mov yh, r19
      bigcall Widget_SetCurrentValue
    pop yh
  pop yl
CDialog_SetChildValue_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine CDialog_GetChildValue
;
; @param Y pointer to dialog
; @param R16 selector of child to set value to
; @clobbers any, !Y, !Z

CDialog_GetChildValue:
  rcall CDialog_GetChildBySelector       ; r19:r18=result (R16-R19)
  brcc CDialog_GetChildValue_retZero
  push yl
    push yh
      mov yl, r18
      mov yh, r19
      bigcall Widget_GetCurrentValue
    pop yh
  pop yl
  brcs CDialog_GetChildValue_ret
CDialog_GetChildValue_retZero:
  clr r18
  clr r19
CDialog_GetChildValue_ret:
  ret
; @end



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

CDialog_OnMkContent:
  rcall CDialog_CreateContent
  sec
  ret
; @end






CDialog_DefaultSignalmap:
  ; header
  .dw Dialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,            DIALOG_SIGNAL_MKCONTENT,    LOW(CDialog_OnMkContent),   HIGH(CDialog_OnMkContent)
  ; end of table
  .db 0, 0, 0, 0



#endif

