// 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
// widgetToolbar.c  -- Toolbar widget
//
// A horizontal container with a raised 1px bevel background, used to
// hold buttons, separators, spacers, and other control widgets in a
// strip at the top of a window. Like StatusBar, the toolbar is a
// standard HBox layout with custom paint -- child widgets position
// themselves via the generic horizontal box algorithm.
//
// The 1px raised bevel (vs the 2px used for window chrome) keeps the
// toolbar visually distinct but lightweight. The bevel's bottom shadow
// line serves as the separator between the toolbar and the content
// area below it.
//
// Tight 2px padding and spacing match the StatusBar for visual
// consistency between top and bottom chrome bars. Children (typically
// ImageButtons or small Buttons) handle their own mouse/key events.

#include "dvxWgtP.h"

#define TOOLBAR_PAD          2
#define TOOLBAR_GAP          2

static int32_t sTypeId = -1;


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

void widgetToolbarGetLayoutMetrics(const WidgetT *w, const BitmapFontT *font, int32_t *pad, int32_t *gap, int32_t *extraTop, int32_t *borderW);
void widgetToolbarPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors);
WidgetT *wgtToolbar(WidgetT *parent);


void widgetToolbarGetLayoutMetrics(const WidgetT *w, const BitmapFontT *font, int32_t *pad, int32_t *gap, int32_t *extraTop, int32_t *borderW) {
    (void)w;
    (void)font;
    *pad      = TOOLBAR_PAD;
    *gap      = TOOLBAR_GAP;
    *extraTop = 0;
    *borderW  = 0;
}


// Raised bevel with windowFace fill. The 1px bevel width is
// intentionally thinner than window chrome (2px) to keep the
// toolbar from looking too heavy.
void widgetToolbarPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
    (void)font;

    // Draw raised background and bottom separator
    BevelStyleT bevel;
    bevel.highlight = colors->windowHighlight;
    bevel.shadow    = colors->windowShadow;
    bevel.face      = colors->windowFace;
    bevel.width     = 1;
    drawBevel(d, ops, w->x, w->y, w->w, w->h, &bevel);
}


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

    if (w) {
        w->padding = wgtPixels(2);
        w->spacing = wgtPixels(2);
    }

    return w;
}


static const WidgetClassT sClassToolbar = {
    .version = WGT_CLASS_VERSION,
    .flags   = WCLASS_HORIZ_CONTAINER,
    .handlers = {
        [WGT_METHOD_PAINT]              = (void *)widgetToolbarPaint,
        [WGT_METHOD_GET_LAYOUT_METRICS] = (void *)widgetToolbarGetLayoutMetrics,
        [WGT_METHOD_CALC_MIN_SIZE]      = (void *)widgetCalcMinSizeBox,
        [WGT_METHOD_LAYOUT]             = (void *)widgetLayoutBox,
    }
};


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

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

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