; ***************************************************************************
; 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_LABEL_ASM
#define AQH_AVR_GUI2_LABEL_ASM


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

; Label in flash
.equ LABEL_OFFS_LABEL      =  WIDGET_SIZE
.equ LABEL_OFFS_TEXT_LO    =  LABEL_OFFS_LABEL+0
.equ LABEL_OFFS_TEXT_HI    =  LABEL_OFFS_LABEL+1
.equ LABEL_SIZE            =  LABEL_OFFS_LABEL+2



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

.cseg


; ---------------------------------------------------------------------------
; @routine Label_OnDraw @global
;
; @param Z byte address of object (for LPM!)
; @param xl param1
; @param xh param2
; @clobbers any, !Z

Label_OnDraw:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  breq Label_OnDraw_ret

  adiw zh:zl, WIDGET_OFFS_OPTS_LO  ; get OPTS low
  lpm r14, Z
  sbiw zh:zl, WIDGET_OFFS_OPTS_LO

  rcall Widget_Clear
  rcall labelWriteText

  mov r16, r14
  andi r16, (1<<WIDGET_OPTSLO_BORDER_BIT)
  breq Label_OnDraw_done
  ldi r16, LOW(STYLE_BUTTON_COL_BORDER)
  mov r2, r16
  ldi r16, HIGH(STYLE_BUTTON_COL_BORDER)
  mov r3, r16
  bigcall Widget_DrawBorder
  
Label_OnDraw_done:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  cbr r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+WIDGET_SD_OFFS_FLAGS, r16

Label_OnDraw_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine labelWriteText
;
; @param  Z       byte address of object (for LPM!)
; @param  R14     opts low
; @clobbers any, !Z

labelWriteText:
  push zl
    push zh
      adiw zh:zl, LABEL_OFFS_TEXT_LO
      lpm r16, Z+
      lpm zh, Z
      mov zl, r16
      or r16, zh
      breq labelWriteText_noText

      ldi r18, STYLE_WIN_FONT_WIDTH
      rcall Widget_CalcStringWidthFLASH       ; r13:r12=string width
    pop zh
  pop zl
  rcall Widget_AlignPosXY                     ; (r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19)
  adiw zh:zl, LABEL_OFFS_TEXT_LO
  lpm r12, Z+
  lpm r13, Z
  sbiw zh:zl, LABEL_OFFS_TEXT_LO+1
  bigcall Widget_DrawTextFlash
  ret
labelWriteText_noText:
    pop zh
  pop zl
  ret
; @end



Label_DefaultSignalmap:
  .db 0, OBJECT_SIGNAL_CREATE, LOW(Widget_OnCreate), HIGH(Widget_OnCreate)
  .db 0, WIDGET_SIGNAL_DRAW,   LOW(Label_OnDraw),    HIGH(Label_OnDraw)
  .db 0, 0, 0, 0  ; end of table



#endif

