// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#define DVX_WIDGET_IMPL
// widgetSpacer.c  -- Spacer widget (invisible stretching element)
//
// A zero-sized invisible widget with weight=100, used purely for
// layout control. It absorbs leftover space in box layouts, pushing
// sibling widgets apart. Common use: place a spacer between buttons
// in a toolbar to right-align some buttons, or between a label and
// a control to create elastic spacing.
//
// Because calcMinSize returns 0x0, the spacer takes no space when
// there is none to spare, but greedily absorbs extra space via its
// weight. This is the simplest possible layout primitive -- no paint,
// no mouse, no keyboard, no state. The entire widget is effectively
// just a weight value attached to a position in the sibling list.

#include "dvxWgtP.h"

static int32_t sTypeId = -1;


// ============================================================
// Prototypes
// ============================================================

void     wgtRegister(void);
WidgetT *wgtSpacer(WidgetT *parent);
void     widgetSpacerCalcMinSize(WidgetT *w, const BitmapFontT *font);


// ============================================================
// DXE registration
// ============================================================


static const WidgetClassT sClassSpacer = {
    .version = WGT_CLASS_VERSION,
    .flags   = 0,
    .handlers = {
        [WGT_METHOD_CALC_MIN_SIZE] = (void *)widgetSpacerCalcMinSize,
    }
};

static const struct {
    WidgetT *(*create)(WidgetT *parent);
} sApi = {
    .create = wgtSpacer
};

static const WgtIfaceT sIface = {
    .basName      = "Spacer",
    .props        = NULL,
    .propCount    = 0,
    .methods      = NULL,
    .methodCount  = 0,
    .events       = NULL,
    .eventCount   = 0,
    .createSig    = WGT_CREATE_PARENT
};

void wgtRegister(void) {
    sTypeId = wgtRegisterClass(&sClassSpacer);
    wgtRegisterApi("spacer", &sApi);
    wgtRegisterIface("spacer", &sIface);
}


WidgetT *wgtSpacer(WidgetT *parent) {
    WidgetT *w = widgetAlloc(parent, sTypeId);

    if (w) {
        w->weight = WGT_WEIGHT_FILL;
    }

    return w;
}


void widgetSpacerCalcMinSize(WidgetT *w, const BitmapFontT *font) {
    (void)font;
    w->calcMinW = 0;
    w->calcMinH = 0;
}
