<commit_before>/*
  QtCurve KWin window decoration
  Copyright (C) 2007 - 2009 <NAME> <<EMAIL>>

  This class is taken, and modified from the Bespin KWin window decoration.
  Copyright (c) 2008 <NAME> <<EMAIL>>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
 */

#include <QPainter>
#include <QPolygon>
#include <QTimer>
#include <QEvent>
#include <QMouseEvent>
#include <QStyleOptionSizeGrip>

#ifdef Q_WS_X11
#include <QX11Info>
#include <X11/Xlib.h>
#include "fixx11h.h"
#endif

#include "qtcurveclient.h"
#include "qtcurvehandler.h"
#include "resizecorner.h"

#define CORNER_SIZE 12

namespace KWinQtCurve
{

ResizeCorner::ResizeCorner(QtCurveClient * parent)
            : QWidget(parent->widget()),
              col(KDecoration::options()->color(KDecoration::ColorTitleBar, true))
{
    hide();
    if (!(parent->widget() && parent->windowId())) {
        deleteLater();
        return;
    }
    client = parent;
    setCursor(QCursor(Qt::SizeFDiagCursor));
    setFixedSize(CORNER_SIZE, CORNER_SIZE);
    QPolygon triangle(3);
    triangle.putPoints(0, 3, CORNER_SIZE,0, CORNER_SIZE,CORNER_SIZE, 0,CORNER_SIZE);
    setMask(triangle);
    raise();
    installEventFilter(this);
    show();
}

void ResizeCorner::raise()
{
    WId  root,
         daddy = 0,
         *kids = 0L;
    uint numKids = 0;

    XQueryTree(QX11Info::display(), client->windowId(), &root, &daddy, &kids, &numKids);
    if (daddy)
        XReparentWindow(QX11Info::display(), winId(), daddy, 0, 0);
    move(client->width() - (CORNER_SIZE+2), client->height() - (CORNER_SIZE+2));
    client->widget()->removeEventFilter(this);
    client->widget()->installEventFilter(this);
}

void ResizeCorner::move(int x, int y)
{
   int l, r, t, b;
   client->borders(l, r, t, b);
   QWidget::move(x-(l+r), y-(t+b));
}

void ResizeCorner::setColor(const QColor &c)
{
    col=c;
    if(col==palette().color(QPalette::Window))
        col = col.value() > 100 ? col.dark(130) : col.light(120);
    col.setAlphaF(0.5);
}
 
bool ResizeCorner::eventFilter(QObject *obj, QEvent *ev)
{
    if (obj == this && QEvent::ZOrderChange==ev->type())
    {
        removeEventFilter(this);
        raise();
        installEventFilter(this);
        return false;
    }
    
    if (obj == parent() && QEvent::Resize==ev->type())
        move(client->width() - (CORNER_SIZE+2), client->height() - (CORNER_SIZE+2));

    return false;
}

static Atom netMoveResize = XInternAtom(QX11Info::display(), "_NET_WM_MOVERESIZE", False);

void ResizeCorner::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton==ev->button())
    {
        // complex way to say: client->performWindowOperation(KDecoration::ResizeOp);
        // stolen... errr "adapted!" from QSizeGrip
        QX11Info info;<commit_msg>Place size grip on edge<commit_after>/*
  QtCurve KWin window decoration
  Copyright (C) 2007 - 2009 <NAME> <<EMAIL>>

  This class is taken, and modified from the Bespin KWin window decoration.
  Copyright (c) 2008 <NAME> <<EMAIL>>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
 */

#include <QPainter>
#include <QPolygon>
#include <QTimer>
#include <QEvent>
#include <QMouseEvent>
#include <QStyleOptionSizeGrip>

#ifdef Q_WS_X11
#include <QX11Info>
#include <X11/Xlib.h>
#include "fixx11h.h"
#endif

#include "qtcurveclient.h"
#include "qtcurvehandler.h"
#include "resizecorner.h"

#define CORNER_SIZE 12

namespace KWinQtCurve
{

ResizeCorner::ResizeCorner(QtCurveClient * parent)
            : QWidget(parent->widget()),
              col(KDecoration::options()->color(KDecoration::ColorTitleBar, true))
{
    hide();
    if (!(parent->widget() && parent->windowId())) {
        deleteLater();
        return;
    }
    client = parent;
    setCursor(QCursor(Qt::SizeFDiagCursor));
    setFixedSize(CORNER_SIZE, CORNER_SIZE);
    QPolygon triangle(3);
    triangle.putPoints(0, 3, CORNER_SIZE,0, CORNER_SIZE,CORNER_SIZE, 0,CORNER_SIZE);
    setMask(triangle);
    raise();
    installEventFilter(this);
    show();
}

void ResizeCorner::raise()
{
    WId  root,
         daddy = 0,
         *kids = 0L;
    uint numKids = 0;

    XQueryTree(QX11Info::display(), client->windowId(), &root, &daddy, &kids, &numKids);
    if (daddy)
        XReparentWindow(QX11Info::display(), winId(), daddy, 0, 0);
    move(client->width() - CORNER_SIZE, client->height() - CORNER_SIZE);
    client->widget()->removeEventFilter(this);
    client->widget()->installEventFilter(this);
}

void ResizeCorner::move(int x, int y)
{
   int l, r, t, b;
   client->borders(l, r, t, b);
   QWidget::move(x-(l+r), y-(t+b));
}

void ResizeCorner::setColor(const QColor &c)
{
    col=c;
    if(col==palette().color(QPalette::Window))
        col = col.value() > 100 ? col.dark(130) : col.light(120);
    col.setAlphaF(0.5);
}
 
bool ResizeCorner::eventFilter(QObject *obj, QEvent *ev)
{
    if (obj == this && QEvent::ZOrderChange==ev->type())
    {
        removeEventFilter(this);
        raise();
        installEventFilter(this);
        return false;
    }
    
    if (obj == parent() && QEvent::Resize==ev->type())
        move(client->width() - CORNER_SIZE, client->height() - CORNER_SIZE);

    return false;
}

static Atom netMoveResize = XInternAtom(QX11Info::display(), "_NET_WM_MOVERESIZE", False);

void ResizeCorner::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton==ev->button())
    {
        // complex way to say: client->performWindowOperation(KDecoration::ResizeOp);
        // stolen... errr "adapted!" from QSizeGrip
        QX11Info info;
{'fraction_non_alphanumeric': 0.13378023899737687, 'fraction_numerical': 0.018944914019236373, 'mean_word_length': 3.6910457963089542, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7352284', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2253, 'n_words': 619}