; ***************************************************************************
; 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_ROOTWINDOW_ASM
#define AQH_AVR_GUI2_ROOTWINDOW_ASM


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

.equ ROOTWINDOW_OFFS_BEGIN     =  WIDGET_SIZE
.equ ROOTWINDOW_OFFS_GUIAPP_LO =  ROOTWINDOW_OFFS_BEGIN+0
.equ ROOTWINDOW_OFFS_GUIAPP_HI =  ROOTWINDOW_OFFS_BEGIN+1
.equ ROOTWINDOW_SIZE           =  ROOTWINDOW_OFFS_BEGIN+2



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

.cseg



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

RootWindow_new:
  ldi r24, LOW(ROOTWINDOW_SIZE)
  ldi r25, HIGH(ROOTWINDOW_SIZE)
  bigcall Object_Alloc                ; (!r16, !r17, !X)
  brcc RootWindow_new_ret
  rcall RootWindow_Init               ; (r16, r17, X)
  sec
RootWindow_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine RootWindow_Init @global
;
; @return CFLAG set if okay, cleared otherwise
; @param Y address of widget
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param X   pointer to GUIAPP
; @clobbers r16, r17, X

RootWindow_Init:
  push xl
    push xh
      clr xl
      clr xh
      ; call base class
      bigcall Widget_Init                              ; (r16, r17, X)
    pop xh
  pop xl

  std Y+ROOTWINDOW_OFFS_GUIAPP_LO, xl
  std Y+ROOTWINDOW_OFFS_GUIAPP_HI, xh

  bigcall Widget_SetFullScreen                         ; (R16)
  
  ; set default signal map
  ldi r16, LOW(RootWindow_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(RootWindow_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

  ; set default style
  ldi r16, LOW(RootWindow_DefaultStyle*2)
  std Y+WIDGET_OFFS_STYLE_LO, r16
  ldi r16, HIGH(RootWindow_DefaultStyle*2)
  std Y+WIDGET_OFFS_STYLE_HI, r16

  ret
; @end



; ---------------------------------------------------------------------------
; @routine RootWindow_GetApp @global
;
; @param Y        address of widget
; @return R19:R18 address pointer to GuiApp
; @clobbers none

RootWindow_GetApp:
  ldd r18, Y+ROOTWINDOW_OFFS_GUIAPP_LO
  ldd r19, Y+ROOTWINDOW_OFFS_GUIAPP_HI
  ret
; @end





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


; ---------------------------------------------------------------------------
; @routine RootWindow_OnLayout
;
; @param Y pointer to widget
; @clobbers any, !Y

RootWindow_OnLayout:
  push yl
    push yh
      bigcall OBJ_GetFirstChild
      brcc RootWindow_OnLayout_done
RootWindow_OnLayout_loop:
        mov yl, r18
        mov yh, r19
        ; all children are MainWindow, set to fullscreen
        bigcall Widget_SetFullScreen    ; (R16)
        bigcall OBJ_GetNext
      brcs RootWindow_OnLayout_loop
RootWindow_OnLayout_done:
    pop yh
  pop yl

  ldd r17, Y+OBJECT_OFFS_FLAGS
  cbr r17, (1<<WIDGET_FLAGS_LAYOUT_BIT)
  std Y+OBJECT_OFFS_FLAGS, r17

  sec
  ret
; @end





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

RootWindow_DefaultSignalmap:
  ; header
  .dw Widget_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                           WIDGET_SIGNAL_DRAW,     LOW(Widget_OnDrawNop),    HIGH(Widget_OnDrawNop)
  .db 0,                           WIDGET_SIGNAL_LAYOUT,   LOW(RootWindow_OnLayout), HIGH(RootWindow_OnLayout)
  .db 0, 0, 0, 0  ; end of table


RootWindow_DefaultStyle:
  .dw DISPLAY_COLOR_BLACK      ; frontCol_norm
  .dw DISPLAY_COLOR_LIGHTGREY  ; backCol_norm
  .dw DISPLAY_COLOR_BLACK      ; borderCol_norm
  .dw DISPLAY_COLOR_WHITE      ; shadowCol_norm
  
  .dw DISPLAY_COLOR_WHITE      ; frontCol_activated
  .dw DISPLAY_COLOR_NAVY       ; backCol_activated
  .dw DISPLAY_COLOR_BLACK      ; borderCol_activated
  .dw DISPLAY_COLOR_WHITE      ; shadowCol_activated
  
  .db 0, 0                     ; outerBorderSize, innerBorderSize
  .dw ili9341Font12x16_1*2     ; font
  .db 12, 16                   ; charWidth, charHeight



#endif

