; ***************************************************************************
; 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_D_NUMINPUT_ASM
#define AQH_AVR_GUI_BASE_D_NUMINPUT_ASM


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

.equ DLGNUMINPUT_OFFS_BEGIN   = MAINWINDOW_SIZE
.equ DLGNUMINPUT_SIZE         = DLGNUMINPUT_OFFS_BEGIN+0



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

.cseg



; ---------------------------------------------------------------------------
; @routine DlgNumInput_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

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



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

DlgNumInput_Init:
  ldi r16, 0                                                                                           ; OPTS
  ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
  bigcall MainWindow_new               ; Y=main window
  brcc DlgNumInput_Init_ret

  bigcall MainWindow_GetContentWidget  ; r19:r18=content window
  brcc DlgNumInput_Init_ret
  mov xl, r18                          ; use content window as parent
  mov xh, r19

  ; Y=MainWindow
  push yl                              ; main window
    push yh
      push xl                          ; content window
        push xh
          rcall dlgNumInputCreateContent
        pop xh                         ; content window
      pop xl
      ; TODO: create button line (OK, ABORT)
      bigcall C03App_CreateBackButton
    pop yh                             ; main window
  pop yl  
DlgNumInput_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine dlgNumInputCreateContent
;
; @param Y pointer to this main window
; @param X pointer to content window of this MainWindow (is a VLayout, becomes parent)
; @return CFLAG set of okay, cleared otherwise

dlgNumInputCreateContent:
  ; create input field
  ldi r16, (1<<WIDGET_OPTS_BORDER_BIT)
  ldi r17, (WIDGET_PACK_CENTER<<WIDGET_PACK_HSELF0_BIT)  | \
           (WIDGET_PACK_BEGIN <<WIDGET_PACK_VSELF0_BIT)   | \
           (WIDGET_PACK_END   <<WIDGET_PACK_HCONTENT0_BIT) | \
           (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)   ; PACK
  clr r20     ; number of postkomma digits
  ldi r21, 10 ; max number of digits
  push xl
    push xh
      bigcall ValueLabel_new
    pop xh
  pop xl
  brcc dlgNumInputCreateContent_ret
  
  ; create numeric keypad
  ldi r16, (1<<WIDGET_OPTS_BORDER_BIT)
  ldi r17, (WIDGET_PACK_CENTER<<WIDGET_PACK_HSELF0_BIT)     | (WIDGET_PACK_BEGIN<<WIDGET_PACK_VSELF0_BIT) | \
           (WIDGET_PACK_CENTER <<WIDGET_PACK_HCONTENT0_BIT) | (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)
  push xl
    push xh
      bigcall NumKeyPad_new
    pop xh
  pop xl
  brcc dlgNumInputCreateContent_ret

  sec
dlgNumInputCreateContent_ret:
  ret
; @end





#endif

