// ============================================================
// vqt6/_bridge_gui.cpp — QtGui C 桥接层实现
// ============================================================

#include "_bridge.h"
#include "_bridge_gui.h"
#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QIcon>
#include <QCursor>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QCloseEvent>
#include <QPainterPath>
#include <QPolygon>
#include <QPoint>
#include <QPointF>
#include <QRect>
#include <QRectF>
#include <QGuiApplication>
#include <QScreen>
#include <QImageReader>
#include <QBuffer>
#include <QStyle>
#include <QStringList>

// ============================================================
// QColor
// ============================================================
extern "C" VQT6_API void* vqt6_qcolor_create_rgb(int r, int g, int b) {
    return new QColor(r, g, b);
}

extern "C" VQT6_API void* vqt6_qcolor_create_rgba(int r, int g, int b, int a) {
    return new QColor(r, g, b, a);
}

extern "C" VQT6_API void* vqt6_qcolor_create_name(void* name) {
    if (name == nullptr) return new QColor();
    return new QColor(*static_cast<QString*>(name));
}

extern "C" VQT6_API void* vqt6_qcolor_create_hex(void* hex_str) {
    if (hex_str == nullptr) return new QColor();
    return new QColor(*static_cast<QString*>(hex_str));
}

extern "C" VQT6_API int vqt6_qcolor_red(void* c) {
    if (c == nullptr) return 0;
    return static_cast<QColor*>(c)->red();
}

extern "C" VQT6_API int vqt6_qcolor_green(void* c) {
    if (c == nullptr) return 0;
    return static_cast<QColor*>(c)->green();
}

extern "C" VQT6_API int vqt6_qcolor_blue(void* c) {
    if (c == nullptr) return 0;
    return static_cast<QColor*>(c)->blue();
}

extern "C" VQT6_API int vqt6_qcolor_alpha(void* c) {
    if (c == nullptr) return 255;
    return static_cast<QColor*>(c)->alpha();
}

extern "C" VQT6_API void vqt6_qcolor_set_red(void* c, int r) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setRed(r);
}

extern "C" VQT6_API void vqt6_qcolor_set_green(void* c, int g) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setGreen(g);
}

extern "C" VQT6_API void vqt6_qcolor_set_blue(void* c, int b) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setBlue(b);
}

extern "C" VQT6_API void vqt6_qcolor_set_alpha(void* c, int a) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setAlpha(a);
}

extern "C" VQT6_API void vqt6_qcolor_set_rgb(void* c, int r, int g, int b) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setRgb(r, g, b);
}

extern "C" VQT6_API void vqt6_qcolor_set_rgba(void* c, int r, int g, int b, int a) {
    if (c == nullptr) return;
    static_cast<QColor*>(c)->setRgba(r, g, b, a);
}

extern "C" VQT6_API void* vqt6_qcolor_name(void* c) {
    if (c == nullptr) return nullptr;
    return new QString(static_cast<QColor*>(c)->name());
}

extern "C" VQT6_API int vqt6_qcolor_is_valid(void* c) {
    if (c == nullptr) return 0;
    return static_cast<QColor*>(c)->isValid() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qcolor_free(void* c) {
    delete static_cast<QColor*>(c);
}

// ============================================================
// QFont
// ============================================================
extern "C" VQT6_API void* vqt6_qfont_create(void) {
    return new QFont();
}

extern "C" VQT6_API void* vqt6_qfont_create_with_family(void* family) {
    if (family == nullptr) return new QFont();
    return new QFont(*static_cast<QString*>(family));
}

extern "C" VQT6_API void* vqt6_qfont_create_with_family_size(void* family, int point_size) {
    if (family == nullptr) return new QFont();
    return new QFont(*static_cast<QString*>(family), point_size);
}

extern "C" VQT6_API void vqt6_qfont_set_family(void* font, void* family) {
    if (font == nullptr || family == nullptr) return;
    static_cast<QFont*>(font)->setFamily(*static_cast<QString*>(family));
}

extern "C" VQT6_API void* vqt6_qfont_family(void* font) {
    if (font == nullptr) return nullptr;
    return new QString(static_cast<QFont*>(font)->family());
}

extern "C" VQT6_API void vqt6_qfont_set_point_size(void* font, int point_size) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setPointSize(point_size);
}

extern "C" VQT6_API int vqt6_qfont_point_size(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->pointSize();
}

extern "C" VQT6_API void vqt6_qfont_set_pixel_size(void* font, int pixel_size) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setPixelSize(pixel_size);
}

extern "C" VQT6_API int vqt6_qfont_pixel_size(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->pixelSize();
}

extern "C" VQT6_API void vqt6_qfont_set_bold(void* font, int bold) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setBold(bold != 0);
}

extern "C" VQT6_API int vqt6_qfont_bold(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->bold() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qfont_set_italic(void* font, int italic) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setItalic(italic != 0);
}

extern "C" VQT6_API int vqt6_qfont_italic(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->italic() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qfont_set_underline(void* font, int underline) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setUnderline(underline != 0);
}

extern "C" VQT6_API int vqt6_qfont_underline(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->underline() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qfont_set_strike_out(void* font, int strike_out) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setStrikeOut(strike_out != 0);
}

extern "C" VQT6_API int vqt6_qfont_strike_out(void* font) {
    if (font == nullptr) return 0;
    return static_cast<QFont*>(font)->strikeOut() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qfont_set_weight(void* font, int weight) {
    if (font == nullptr) return;
    static_cast<QFont*>(font)->setWeight(static_cast<QFont::Weight>(weight));
}

extern "C" VQT6_API int vqt6_qfont_weight(void* font) {
    if (font == nullptr) return 0;
    return static_cast<int>(static_cast<QFont*>(font)->weight());
}

extern "C" VQT6_API void vqt6_qfont_free(void* font) {
    delete static_cast<QFont*>(font);
}

// ============================================================
// QFontMetrics
// ============================================================
extern "C" VQT6_API void* vqt6_qfontmetrics_create(void* font) {
    if (font == nullptr) return nullptr;
    return new QFontMetrics(*static_cast<QFont*>(font));
}

extern "C" VQT6_API int vqt6_qfontmetrics_width(void* fm, void* text) {
    if (fm == nullptr || text == nullptr) return 0;
    return static_cast<QFontMetrics*>(fm)->horizontalAdvance(*static_cast<QString*>(text));
}

extern "C" VQT6_API int vqt6_qfontmetrics_height(void* fm) {
    if (fm == nullptr) return 0;
    return static_cast<QFontMetrics*>(fm)->height();
}

extern "C" VQT6_API int vqt6_qfontmetrics_ascent(void* fm) {
    if (fm == nullptr) return 0;
    return static_cast<QFontMetrics*>(fm)->ascent();
}

extern "C" VQT6_API int vqt6_qfontmetrics_descent(void* fm) {
    if (fm == nullptr) return 0;
    return static_cast<QFontMetrics*>(fm)->descent();
}

extern "C" VQT6_API void vqt6_qfontmetrics_free(void* fm) {
    delete static_cast<QFontMetrics*>(fm);
}

// ============================================================
// QPixmap
// ============================================================
extern "C" VQT6_API void* vqt6_qpixmap_create(void) {
    return new QPixmap();
}

extern "C" VQT6_API void* vqt6_qpixmap_create_with_size(int w, int h) {
    return new QPixmap(w, h);
}

extern "C" VQT6_API int vqt6_qpixmap_load(void* pm, void* file_name) {
    if (pm == nullptr || file_name == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->load(*static_cast<QString*>(file_name)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qpixmap_load_from_data(void* pm, const void* data, size_t size) {
    if (pm == nullptr || data == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->loadFromData(static_cast<const uchar*>(data), static_cast<uint>(size)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qpixmap_save(void* pm, void* file_name) {
    if (pm == nullptr || file_name == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->save(*static_cast<QString*>(file_name)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qpixmap_width(void* pm) {
    if (pm == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->width();
}

extern "C" VQT6_API int vqt6_qpixmap_height(void* pm) {
    if (pm == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->height();
}

extern "C" VQT6_API int vqt6_qpixmap_is_null(void* pm) {
    if (pm == nullptr) return 0;
    return static_cast<QPixmap*>(pm)->isNull() ? 1 : 0;
}

extern "C" VQT6_API void* vqt6_qpixmap_scaled(void* pm, int w, int h) {
    if (pm == nullptr) return nullptr;
    return new QPixmap(static_cast<QPixmap*>(pm)->scaled(w, h));
}

extern "C" VQT6_API void vqt6_qpixmap_fill(void* pm, void* color) {
    if (pm == nullptr) return;
    if (color != nullptr) {
        static_cast<QPixmap*>(pm)->fill(*static_cast<QColor*>(color));
    } else {
        static_cast<QPixmap*>(pm)->fill();
    }
}

extern "C" VQT6_API void vqt6_qpixmap_free(void* pm) {
    delete static_cast<QPixmap*>(pm);
}

// ============================================================
// QImage
// ============================================================
extern "C" VQT6_API void* vqt6_qimage_create(void) {
    return new QImage();
}

extern "C" VQT6_API void* vqt6_qimage_create_with_size(int w, int h, int format) {
    return new QImage(w, h, static_cast<QImage::Format>(format));
}

extern "C" VQT6_API int vqt6_qimage_load(void* img, void* file_name) {
    if (img == nullptr || file_name == nullptr) return 0;
    return static_cast<QImage*>(img)->load(*static_cast<QString*>(file_name)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qimage_save(void* img, void* file_name) {
    if (img == nullptr || file_name == nullptr) return 0;
    return static_cast<QImage*>(img)->save(*static_cast<QString*>(file_name)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qimage_width(void* img) {
    if (img == nullptr) return 0;
    return static_cast<QImage*>(img)->width();
}

extern "C" VQT6_API int vqt6_qimage_height(void* img) {
    if (img == nullptr) return 0;
    return static_cast<QImage*>(img)->height();
}

extern "C" VQT6_API int vqt6_qimage_format(void* img) {
    if (img == nullptr) return 0;
    return static_cast<int>(static_cast<QImage*>(img)->format());
}

extern "C" VQT6_API int vqt6_qimage_is_null(void* img) {
    if (img == nullptr) return 0;
    return static_cast<QImage*>(img)->isNull() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qimage_fill(void* img, void* color) {
    if (img == nullptr) return;
    if (color != nullptr) {
        static_cast<QImage*>(img)->fill(*static_cast<QColor*>(color));
    }
}

extern "C" VQT6_API void* vqt6_qimage_scaled(void* img, int w, int h) {
    if (img == nullptr) return nullptr;
    return new QImage(static_cast<QImage*>(img)->scaled(w, h));
}

extern "C" VQT6_API void vqt6_qimage_free(void* img) {
    delete static_cast<QImage*>(img);
}

// ============================================================
// QPainter
// ============================================================
extern "C" VQT6_API void* vqt6_qpainter_create(void) {
    return new QPainter();
}

extern "C" VQT6_API int vqt6_qpainter_begin(void* p, void* device) {
    if (p == nullptr || device == nullptr) return 0;
    return static_cast<QPainter*>(p)->begin(static_cast<QPixmap*>(device)) ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qpainter_end(void* p) {
    if (p == nullptr) return 0;
    return static_cast<QPainter*>(p)->end() ? 1 : 0;
}

extern "C" VQT6_API int vqt6_qpainter_is_active(void* p) {
    if (p == nullptr) return 0;
    return static_cast<QPainter*>(p)->isActive() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qpainter_set_pen(void* p, void* pen) {
    if (p == nullptr || pen == nullptr) return;
    static_cast<QPainter*>(p)->setPen(*static_cast<QPen*>(pen));
}

extern "C" VQT6_API void vqt6_qpainter_set_brush(void* p, void* brush) {
    if (p == nullptr || brush == nullptr) return;
    static_cast<QPainter*>(p)->setBrush(*static_cast<QBrush*>(brush));
}

extern "C" VQT6_API void vqt6_qpainter_set_font(void* p, void* font) {
    if (p == nullptr || font == nullptr) return;
    static_cast<QPainter*>(p)->setFont(*static_cast<QFont*>(font));
}

extern "C" VQT6_API void* vqt6_qpainter_pen(void* p) {
    if (p == nullptr) return nullptr;
    return new QPen(static_cast<QPainter*>(p)->pen());
}

extern "C" VQT6_API void* vqt6_qpainter_brush(void* p) {
    if (p == nullptr) return nullptr;
    return new QBrush(static_cast<QPainter*>(p)->brush());
}

extern "C" VQT6_API void* vqt6_qpainter_font(void* p) {
    if (p == nullptr) return nullptr;
    return new QFont(static_cast<QPainter*>(p)->font());
}

extern "C" VQT6_API void vqt6_qpainter_set_opacity(void* p, double opacity) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->setOpacity(opacity);
}

extern "C" VQT6_API double vqt6_qpainter_opacity(void* p) {
    if (p == nullptr) return 1.0;
    return static_cast<QPainter*>(p)->opacity();
}

extern "C" VQT6_API void vqt6_qpainter_draw_line(void* p, int x1, int y1, int x2, int y2) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawLine(x1, y1, x2, y2);
}

extern "C" VQT6_API void vqt6_qpainter_draw_rect(void* p, int x, int y, int w, int h) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawRect(x, y, w, h);
}

extern "C" VQT6_API void vqt6_qpainter_fill_rect(void* p, int x, int y, int w, int h, void* color) {
    if (p == nullptr || color == nullptr) return;
    static_cast<QPainter*>(p)->fillRect(x, y, w, h, *static_cast<QColor*>(color));
}

extern "C" VQT6_API void vqt6_qpainter_draw_ellipse(void* p, int x, int y, int w, int h) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawEllipse(x, y, w, h);
}

extern "C" VQT6_API void vqt6_qpainter_draw_arc(void* p, int x, int y, int w, int h, int start_angle, int span_angle) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawArc(x, y, w, h, start_angle, span_angle);
}

extern "C" VQT6_API void vqt6_qpainter_draw_chord(void* p, int x, int y, int w, int h, int start_angle, int span_angle) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawChord(x, y, w, h, start_angle, span_angle);
}

extern "C" VQT6_API void vqt6_qpainter_draw_pie(void* p, int x, int y, int w, int h, int start_angle, int span_angle) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawPie(x, y, w, h, start_angle, span_angle);
}

extern "C" VQT6_API void vqt6_qpainter_draw_polygon(void* p, int* points, int point_count) {
    if (p == nullptr || points == nullptr || point_count < 1) return;
    QPolygon polygon;
    for (int i = 0; i < point_count; ++i) {
        polygon << QPoint(points[i*2], points[i*2+1]);
    }
    static_cast<QPainter*>(p)->drawPolygon(polygon);
}

extern "C" VQT6_API void vqt6_qpainter_draw_polyline(void* p, int* points, int point_count) {
    if (p == nullptr || points == nullptr || point_count < 1) return;
    QPolygon polyline;
    for (int i = 0; i < point_count; ++i) {
        polyline << QPoint(points[i*2], points[i*2+1]);
    }
    static_cast<QPainter*>(p)->drawPolyline(polyline);
}

extern "C" VQT6_API void vqt6_qpainter_draw_point(void* p, int x, int y) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->drawPoint(x, y);
}

extern "C" VQT6_API void vqt6_qpainter_draw_points(void* p, int* points, int point_count) {
    if (p == nullptr || points == nullptr || point_count < 1) return;
    QPolygon pts;
    for (int i = 0; i < point_count; ++i) {
        pts << QPoint(points[i*2], points[i*2+1]);
    }
    static_cast<QPainter*>(p)->drawPoints(pts);
}

extern "C" VQT6_API void vqt6_qpainter_draw_text(void* p, int x, int y, void* text) {
    if (p == nullptr || text == nullptr) return;
    static_cast<QPainter*>(p)->drawText(x, y, *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qpainter_draw_text_in_rect(void* p, int x, int y, int w, int h, int flags, void* text) {
    if (p == nullptr || text == nullptr) return;
    static_cast<QPainter*>(p)->drawText(x, y, w, h, flags, *static_cast<QString*>(text));
}

extern "C" VQT6_API void vqt6_qpainter_draw_pixmap(void* p, int x, int y, void* pixmap) {
    if (p == nullptr || pixmap == nullptr) return;
    static_cast<QPainter*>(p)->drawPixmap(x, y, *static_cast<QPixmap*>(pixmap));
}

extern "C" VQT6_API void vqt6_qpainter_draw_image(void* p, int x, int y, void* image) {
    if (p == nullptr || image == nullptr) return;
    static_cast<QPainter*>(p)->drawImage(x, y, *static_cast<QImage*>(image));
}

extern "C" VQT6_API void vqt6_qpainter_draw_tiled_pixmap(void* p, int x, int y, int w, int h, void* pixmap) {
    if (p == nullptr || pixmap == nullptr) return;
    static_cast<QPainter*>(p)->drawTiledPixmap(x, y, w, h, *static_cast<QPixmap*>(pixmap));
}

extern "C" VQT6_API void vqt6_qpainter_translate(void* p, int dx, int dy) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->translate(dx, dy);
}

extern "C" VQT6_API void vqt6_qpainter_rotate(void* p, double angle) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->rotate(angle);
}

extern "C" VQT6_API void vqt6_qpainter_scale(void* p, double sx, double sy) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->scale(sx, sy);
}

extern "C" VQT6_API void vqt6_qpainter_reset_transform(void* p) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->resetTransform();
}

extern "C" VQT6_API void vqt6_qpainter_save(void* p) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->save();
}

extern "C" VQT6_API void vqt6_qpainter_restore(void* p) {
    if (p == nullptr) return;
    static_cast<QPainter*>(p)->restore();
}

extern "C" VQT6_API void vqt6_qpainter_free(void* p) {
    delete static_cast<QPainter*>(p);
}

// ============================================================
// QPen
// ============================================================
extern "C" VQT6_API void* vqt6_qpen_create(void) {
    return new QPen();
}

extern "C" VQT6_API void* vqt6_qpen_create_with_color(void* color) {
    if (color == nullptr) return new QPen();
    return new QPen(*static_cast<QColor*>(color));
}

extern "C" VQT6_API void* vqt6_qpen_create_with_color_width(void* color, int width) {
    if (color == nullptr) return new QPen();
    return new QPen(*static_cast<QColor*>(color), width);
}

extern "C" VQT6_API void vqt6_qpen_set_color(void* pen, void* color) {
    if (pen == nullptr || color == nullptr) return;
    static_cast<QPen*>(pen)->setColor(*static_cast<QColor*>(color));
}

extern "C" VQT6_API void* vqt6_qpen_color(void* pen) {
    if (pen == nullptr) return nullptr;
    return new QColor(static_cast<QPen*>(pen)->color());
}

extern "C" VQT6_API void vqt6_qpen_set_width(void* pen, int width) {
    if (pen == nullptr) return;
    static_cast<QPen*>(pen)->setWidth(width);
}

extern "C" VQT6_API int vqt6_qpen_width(void* pen) {
    if (pen == nullptr) return 0;
    return static_cast<QPen*>(pen)->width();
}

extern "C" VQT6_API void vqt6_qpen_set_style(void* pen, int style) {
    if (pen == nullptr) return;
    static_cast<QPen*>(pen)->setStyle(static_cast<Qt::PenStyle>(style));
}

extern "C" VQT6_API int vqt6_qpen_style(void* pen) {
    if (pen == nullptr) return 0;
    return static_cast<int>(static_cast<QPen*>(pen)->style());
}

extern "C" VQT6_API void vqt6_qpen_free(void* pen) {
    delete static_cast<QPen*>(pen);
}

// ============================================================
// QBrush
// ============================================================
extern "C" VQT6_API void* vqt6_qbrush_create(void) {
    return new QBrush();
}

extern "C" VQT6_API void* vqt6_qbrush_create_with_color(void* color) {
    if (color == nullptr) return new QBrush();
    return new QBrush(*static_cast<QColor*>(color));
}

extern "C" VQT6_API void* vqt6_qbrush_create_with_style(int style) {
    return new QBrush(static_cast<Qt::BrushStyle>(style));
}

extern "C" VQT6_API void vqt6_qbrush_set_color(void* brush, void* color) {
    if (brush == nullptr || color == nullptr) return;
    static_cast<QBrush*>(brush)->setColor(*static_cast<QColor*>(color));
}

extern "C" VQT6_API void* vqt6_qbrush_color(void* brush) {
    if (brush == nullptr) return nullptr;
    return new QColor(static_cast<QBrush*>(brush)->color());
}

extern "C" VQT6_API void vqt6_qbrush_set_style(void* brush, int style) {
    if (brush == nullptr) return;
    static_cast<QBrush*>(brush)->setStyle(static_cast<Qt::BrushStyle>(style));
}

extern "C" VQT6_API int vqt6_qbrush_style(void* brush) {
    if (brush == nullptr) return 0;
    return static_cast<int>(static_cast<QBrush*>(brush)->style());
}

extern "C" VQT6_API void vqt6_qbrush_free(void* brush) {
    delete static_cast<QBrush*>(brush);
}

// ============================================================
// QIcon
// ============================================================
extern "C" VQT6_API void* vqt6_qicon_create(void) {
    return new QIcon();
}

extern "C" VQT6_API void* vqt6_qicon_create_with_file(void* file_name) {
    if (file_name == nullptr) return new QIcon();
    return new QIcon(*static_cast<QString*>(file_name));
}

extern "C" VQT6_API void vqt6_qicon_add_file(void* icon, void* file_name) {
    if (icon == nullptr || file_name == nullptr) return;
    static_cast<QIcon*>(icon)->addFile(*static_cast<QString*>(file_name));
}

extern "C" VQT6_API void vqt6_qicon_add_pixmap(void* icon, void* pixmap) {
    if (icon == nullptr || pixmap == nullptr) return;
    static_cast<QIcon*>(icon)->addPixmap(*static_cast<QPixmap*>(pixmap));
}

extern "C" VQT6_API int vqt6_qicon_is_null(void* icon) {
    if (icon == nullptr) return 0;
    return static_cast<QIcon*>(icon)->isNull() ? 1 : 0;
}

extern "C" VQT6_API void* vqt6_qicon_pixmap(void* icon, int w, int h) {
    if (icon == nullptr) return nullptr;
    return new QPixmap(static_cast<QIcon*>(icon)->pixmap(w, h));
}

extern "C" VQT6_API void vqt6_qicon_free(void* icon) {
    delete static_cast<QIcon*>(icon);
}

// ============================================================
// QCursor
// ============================================================
extern "C" VQT6_API void* vqt6_qcursor_create(void) {
    return new QCursor();
}

extern "C" VQT6_API void* vqt6_qcursor_create_with_shape(int shape) {
    return new QCursor(static_cast<Qt::CursorShape>(shape));
}

extern "C" VQT6_API void vqt6_qcursor_pos(int* x, int* y) {
    QPoint p = QCursor::pos();
    if (x != nullptr) *x = p.x();
    if (y != nullptr) *y = p.y();
}

extern "C" VQT6_API void vqt6_qcursor_set_pos(int x, int y) {
    QCursor::setPos(x, y);
}

extern "C" VQT6_API void vqt6_qcursor_set_shape(void* cursor, int shape) {
    if (cursor == nullptr) return;
    static_cast<QCursor*>(cursor)->setShape(static_cast<Qt::CursorShape>(shape));
}

extern "C" VQT6_API int vqt6_qcursor_shape(void* cursor) {
    if (cursor == nullptr) return 0;
    return static_cast<int>(static_cast<QCursor*>(cursor)->shape());
}

extern "C" VQT6_API void vqt6_qcursor_free(void* cursor) {
    delete static_cast<QCursor*>(cursor);
}

// ============================================================
// QKeyEvent
// ============================================================
extern "C" VQT6_API void* vqt6_qkeyevent_create(int type, int key, int modifiers, void* text) {
    QString* t = static_cast<QString*>(text);
    return new QKeyEvent(static_cast<QEvent::Type>(type), key, static_cast<Qt::KeyboardModifiers>(modifiers),
                         t != nullptr ? *t : QString());
}

extern "C" VQT6_API int vqt6_qkeyevent_key(void* event) {
    if (event == nullptr) return 0;
    return static_cast<QKeyEvent*>(event)->key();
}

extern "C" VQT6_API void* vqt6_qkeyevent_text(void* event) {
    if (event == nullptr) return nullptr;
    return new QString(static_cast<QKeyEvent*>(event)->text());
}

extern "C" VQT6_API int vqt6_qkeyevent_modifiers(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QKeyEvent*>(event)->modifiers());
}

extern "C" VQT6_API int vqt6_qkeyevent_is_auto_repeat(void* event) {
    if (event == nullptr) return 0;
    return static_cast<QKeyEvent*>(event)->isAutoRepeat() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qkeyevent_free(void* event) {
    delete static_cast<QKeyEvent*>(event);
}

// ============================================================
// QMouseEvent
// ============================================================
extern "C" VQT6_API void* vqt6_qmouseevent_create(int type, void* local_pos, int button, int buttons, int modifiers) {
    QPointF* lp = static_cast<QPointF*>(local_pos);
    return new QMouseEvent(
        static_cast<QEvent::Type>(type),
        lp != nullptr ? *lp : QPointF(0, 0),
        Qt::NoButton,
        static_cast<Qt::MouseButton>(button),
        static_cast<Qt::KeyboardModifiers>(modifiers)
    );
}

extern "C" VQT6_API int vqt6_qmouseevent_button(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->button());
}

extern "C" VQT6_API int vqt6_qmouseevent_buttons(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->buttons());
}

extern "C" VQT6_API int vqt6_qmouseevent_x(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->position().x());
}

extern "C" VQT6_API int vqt6_qmouseevent_y(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->position().y());
}

extern "C" VQT6_API int vqt6_qmouseevent_global_x(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->globalPosition().x());
}

extern "C" VQT6_API int vqt6_qmouseevent_global_y(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->globalPosition().y());
}

extern "C" VQT6_API int vqt6_qmouseevent_modifiers(void* event) {
    if (event == nullptr) return 0;
    return static_cast<int>(static_cast<QMouseEvent*>(event)->modifiers());
}

extern "C" VQT6_API void vqt6_qmouseevent_free(void* event) {
    delete static_cast<QMouseEvent*>(event);
}

// ============================================================
// QResizeEvent / QPaintEvent / QCloseEvent
// ============================================================
extern "C" VQT6_API void* vqt6_qresizeevent_create(int width, int height, int old_width, int old_height) {
    return new QResizeEvent(QSize(width, height), QSize(old_width, old_height));
}

extern "C" VQT6_API void vqt6_qresizeevent_size(void* event, int* w, int* h) {
    if (event == nullptr) return;
    QSize s = static_cast<QResizeEvent*>(event)->size();
    if (w != nullptr) *w = s.width();
    if (h != nullptr) *h = s.height();
}

extern "C" VQT6_API void vqt6_qresizeevent_old_size(void* event, int* w, int* h) {
    if (event == nullptr) return;
    QSize s = static_cast<QResizeEvent*>(event)->oldSize();
    if (w != nullptr) *w = s.width();
    if (h != nullptr) *h = s.height();
}

extern "C" VQT6_API void vqt6_qresizeevent_free(void* event) {
    delete static_cast<QResizeEvent*>(event);
}

extern "C" VQT6_API void* vqt6_qpaintevent_create(int rect_x, int rect_y, int rect_w, int rect_h) {
    return new QPaintEvent(QRect(rect_x, rect_y, rect_w, rect_h));
}

extern "C" VQT6_API void vqt6_qpaintevent_rect(void* event, int* x, int* y, int* w, int* h) {
    if (event == nullptr) return;
    QRect r = static_cast<QPaintEvent*>(event)->rect();
    if (x != nullptr) *x = r.x();
    if (y != nullptr) *y = r.y();
    if (w != nullptr) *w = r.width();
    if (h != nullptr) *h = r.height();
}

extern "C" VQT6_API void vqt6_qpaintevent_free(void* event) {
    delete static_cast<QPaintEvent*>(event);
}

extern "C" VQT6_API void* vqt6_qcloseevent_create(void) {
    return new QCloseEvent();
}

extern "C" VQT6_API int vqt6_qcloseevent_is_accepted(void* event) {
    if (event == nullptr) return 0;
    return static_cast<QCloseEvent*>(event)->isAccepted() ? 1 : 0;
}

extern "C" VQT6_API void vqt6_qcloseevent_accept(void* event) {
    if (event == nullptr) return;
    static_cast<QCloseEvent*>(event)->accept();
}

extern "C" VQT6_API void vqt6_qcloseevent_ignore(void* event) {
    if (event == nullptr) return;
    static_cast<QCloseEvent*>(event)->ignore();
}

extern "C" VQT6_API void vqt6_qcloseevent_free(void* event) {
    delete static_cast<QCloseEvent*>(event);
}
