; ***************************************************************************
; 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_MAINWINDOW_ASM
#define AQH_AVR_GUI2_MAINWINDOW_ASM


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

.equ MAINWINDOW_OFFS_BEGIN      =  VLAYOUT_SIZE
.equ MAINWINDOW_OFFS_PREVWIN_LO =  MAINWINDOW_OFFS_BEGIN+0
.equ MAINWINDOW_OFFS_PREVWIN_HI =  MAINWINDOW_OFFS_BEGIN+1
.equ MAINWINDOW_SIZE            =  MAINWINDOW_OFFS_BEGIN+2


; index of sub-windows
.equ MAINWINDOW_CHILDIDX_TITLE   = 0
.equ MAINWINDOW_CHILDIDX_CONTENT = 1



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

.cseg



; ---------------------------------------------------------------------------
; @routine MainWindow_new @global
;
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object
; @param X parent widget
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param r21:r20 ressource id for title
; @clobbers any

MainWindow_new:
  push r20
    push r21
      ldi r24, LOW(MAINWINDOW_SIZE)
      ldi r25, HIGH(MAINWINDOW_SIZE)
      bigcall Object_Alloc                ; (!r16, !r17, !X)
    pop r21
  pop r20
  brcc MainWindow_new_ret
  rcall MainWindow_Init               ; (r16, r17, X)
  sec
MainWindow_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MainWindow_Init @global
;
; @param Y address of widget
; @param X parent widget (if any)
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param r21:r20 ressource id for title
; @clobbers r16, r17, X

MainWindow_Init:
  push r20
    push r21
      ; call base class
      ldi r20, VLAYOUT_MODE_EXPAND
      bigcall VLayout_Init             ; (r16, r17, X)
    pop r21
  pop r20

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

  ; set style which has no spacing and no borders so needs no drawing because
  ; the children fill the screen completely
  ldi r16, LOW(MainWindow_DefaultStyle*2)
  std Y+WIDGET_OFFS_STYLE_LO, r16
  ldi r16, HIGH(MainWindow_DefaultStyle*2)
  std Y+WIDGET_OFFS_STYLE_HI, r16

  bigcall Widget_SetFullScreen  ; (R16)

  ; create sub widgets
  rcall mainWindowCreateTitleWidget

  ret
; @end



; ---------------------------------------------------------------------------
; @routine MainWindow_GetContentWidget @global
;
; @param Y address of widget
; @param X parent widget (if any)
; @return CFLAG set, if found, cleared otherwise
; @return r19:r18 resulting object
; @clobbers r16

MainWindow_GetContentWidget:
  ldi r16, MAINWINDOW_CHILDIDX_CONTENT
  bigcall OBJ_GetChildAt
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MainWindow_GetContentWidget @global
;
; @param Y address of widget
; @param X parent widget (if any)
; @return CFLAG set, if found, cleared otherwise
; @return r19:r18 resulting object
; @clobbers r16

MainWindow_GetFirstChildOfContentWidget:
  push yl
    push yh
      bigcall MainWindow_GetContentWidget  ; r19:r18=content window (R16)
      brcc MainWindow_GetFirstChildOfContentWidget_popRet
      mov yl, r18
      mov yh, r19
      bigcall OBJ_GetFirstChild            ; get first child
MainWindow_GetFirstChildOfContentWidget_popRet:
    pop yh
  pop yl  
  ret
; @end





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

; ---------------------------------------------------------------------------
; @routine MainWindow_OnGetDefaultWidth @global
;
; @param Y address of widget
; @param r17 value requested
; @param xl param1
; @param xh param2
; @return CFLAG set if signal handled
; @return r19:r18 value
; @clobbers any, !Y

MainWindow_OnGetDefaultWidth:
  ldi r18, LOW(DISPLAY_WIDTH)
  ldi r19, HIGH(DISPLAY_WIDTH)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MainWindow_OnGetDefaultHeight @global
;
; @param Y address of widget
; @param r17 value requested
; @param xl param1
; @param xh param2
; @return CFLAG set if signal handled
; @return r19:r18 value
; @clobbers any, !Y

MainWindow_OnGetDefaultHeight:
  ldi r18, LOW(DISPLAY_HEIGHT)
  ldi r19, HIGH(DISPLAY_HEIGHT)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine mainWindowCreateTitleWidget
;
; @param Y address of main window widget
; @param r21:r20 ressource id for title
; @return CFLAG set of okay, cleared otherwise

mainWindowCreateTitleWidget:
  ; create title widget
  push yl
    push yh
      mov xl, yl
      mov xh, yh
      ldi r16, 0
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT)    | (WIDGET_PACK_BEGIN<<WIDGET_PACK_VSELF0_BIT) | \
               (WIDGET_PACK_BEGIN <<WIDGET_PACK_HCONTENT0_BIT) | (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)
      bigcall Label_new
      brcc mainWindowCreateTitleWidget_done
      ; set style for title widget
      ldi r16, LOW(MainWindow_TitleStyle*2)
      std Y+WIDGET_OFFS_STYLE_LO, r16
      ldi r16, HIGH(MainWindow_TitleStyle*2)
      std Y+WIDGET_OFFS_STYLE_HI, r16
      sec
mainWindowCreateTitleWidget_done:
    pop yh
  pop yl
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MainWindow_CreateContentWidget
;
; @param Y address of main window widget
; @return CFLAG set of okay, cleared otherwise
; @return r19:r18 pointer to created widget

MainWindow_CreateContentWidget:
  ; create content widget
  push yl
    push yh
      mov xl, yl
      mov xh, yh
      ldi r16, 0                                                                                           ; OPTS
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      ldi r20, VLAYOUT_MODE_EXPAND
      bigcall VLayout_new
      brcc MainWindow_CreateContentWidget_done
      ; set style for content widget
      ldi r16, LOW(DefaultStyle_Window*2)
      std Y+WIDGET_OFFS_STYLE_LO, r16
      ldi r16, HIGH(DefaultStyle_Window*2)
      std Y+WIDGET_OFFS_STYLE_HI, r16
      mov r18, yl
      mov r19, yh
MainWindow_CreateContentWidget_done:
    pop yh
  pop yl
  ret
; @end





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

MainWindow_DefaultSignalmap:
  ; header
  .dw VLayout_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                           WIDGET_SIGNAL_DRAW,     LOW(Widget_OnDrawNop),              HIGH(Widget_OnDrawNop)
  .db WIDGET_VALUE_DEFAULT_WIDTH,  WIDGET_SIGNAL_GETVALUE, LOW(MainWindow_OnGetDefaultWidth),  HIGH(MainWindow_OnGetDefaultWidth)
  .db WIDGET_VALUE_DEFAULT_HEIGHT, WIDGET_SIGNAL_GETVALUE, LOW(MainWindow_OnGetDefaultHeight), HIGH(MainWindow_OnGetDefaultHeight)

  .db 0, 0, 0, 0  ; end of table



MainWindow_DefaultStyle:
  .dw STYLE_WIN_FGCOL_NORM           ; frontCol_norm
  .dw STYLE_WIN_BGCOL_NORM           ; backCol_norm
  .dw STYLE_WIN_BORDERCOL_NORM       ; borderCol_norm
  .dw STYLE_WIN_SHADOWCOL_NORM       ; shadowCol_norm
  
  .dw STYLE_WIN_FGCOL_ACTIVATED      ; frontCol_activated
  .dw STYLE_WIN_BGCOL_ACTIVATED      ; backCol_activated
  .dw STYLE_WIN_BORDERCOL_ACTIVATED  ; borderCol_activated
  .dw STYLE_WIN_SHADOWCOL_ACTIVATED  ; shadowCol_activated

  .db 0, 0                           ; outerBorderSize, innerBorderSize
  .dw (STYLE_WIN_FONT*2)             ; font
  .db STYLE_WIN_FONT_WIDTH, \
      STYLE_WIN_FONT_HEIGHT          ; charWidth, charHeight



MainWindow_TitleStyle:
  .dw STYLE_TITLE_FGCOL          ; frontCol_norm
  .dw STYLE_TITLE_BGCOL          ; backCol_norm
  .dw STYLE_TITLE_BORDERCOL      ; borderCol_norm
  .dw STYLE_TITLE_SHADOWCOL      ; shadowCol_norm
  
  .dw STYLE_TITLE_FGCOL          ; frontCol_activated
  .dw STYLE_TITLE_BGCOL          ; backCol_activated
  .dw STYLE_TITLE_BORDERCOL      ; borderCol_activated
  .dw STYLE_TITLE_SHADOWCOL      ; shadowCol_activated
  
  .db 2, 1                       ; outerBorderSize, innerBorderSize
  .dw (STYLE_TITLE_FONT*2)       ; font
  .db STYLE_TITLE_FONT_WIDTH, \
      STYLE_TITLE_FONT_HEIGHT    ; charWidth, charHeight



#endif

