OSDN Git Service

Move the directories
[kita/kita.git] / src / respopup.cpp
diff --git a/src/respopup.cpp b/src/respopup.cpp
new file mode 100644 (file)
index 0000000..01eaf44
--- /dev/null
@@ -0,0 +1,299 @@
+/***************************************************************************
+*   Copyright (C) 2007 by Kita Developers                                 *
+*   ikemo@users.sourceforge.jp                                            *
+*                                                                         *
+*   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.                                   *
+***************************************************************************/
+
+#include "respopup.h"
+
+#include <QtGui/QApplication>
+#include <QtGui/QCursor>
+#include <QtGui/QDesktopWidget>
+
+#include <dom/html_document.h>
+#include <dom/html_element.h>
+
+#include "const.h"
+#include "htmlpart.h"
+#include "kitaui/htmlview.h"
+#include "libkita/colorconfig.h"
+#include "libkita/globalconfig.h"
+
+using namespace Kita;
+
+ResPopup::ResPopup(KHTMLView* view, const KUrl& url)
+        : QFrame(view,
+                  Qt::FramelessWindowHint
+                  | Qt::Tool
+                  | Qt::Window
+                  | Qt::X11BypassWindowManagerHint
+               )
+{
+    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
+    m_url = url;
+    m_htmlPart = 0;
+
+    m_htmlPart = new HTMLPart(this);
+    m_htmlPart->view()->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
+    m_htmlPart->setup(HTMLPART_MODE_POPUP , url);
+    connect(m_htmlPart, SIGNAL(hideChildPopup()), SIGNAL(hideChildPopup()));
+}
+
+
+ResPopup::~ResPopup()
+{
+    delete m_htmlPart;
+}
+
+
+
+/* public */
+void ResPopup::setText(const QString& str)
+{
+    const int maxwd = 1600;
+    const int maxht = 1200;
+
+    QString style = QString("body.pop {"
+                             " font-size: %1pt; "
+                             " font-family: %2; "
+                             " color: %3; "
+                             " background-color: %4; "
+                             " border-width: 0;"
+                             "}")
+                    .arg(GlobalConfig::popupFont().pointSize())
+                    .arg(GlobalConfig::popupFont().family())
+                    .arg(ColorConfig::popup().name())
+                    .arg(ColorConfig::popupBackground().name());
+
+    QString text = "<html><head><style>";
+    text += DEFAULT_STYLESHEET;
+    text += style;
+    text += "</style></head><body class=\"pop\">";
+    text += str;
+    text += "</body></html>";
+
+    if (m_htmlPart) {
+        m_htmlPart->view()->setMinimumSize(maxwd, maxht);
+        m_htmlPart->setJScriptEnabled(false);
+        m_htmlPart->setJavaEnabled(false);
+        m_htmlPart->begin(KUrl("file:/dummy.htm"));
+        m_htmlPart->write(text);
+        m_htmlPart->end();
+        //m_htmlPart->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); TODO
+    }
+}
+
+/* public */
+void ResPopup::adjustSize()
+{
+#if 0
+    if (!m_htmlPart) return ;
+
+    int width = 0, xx = 0, leftmrg = 0;
+    int maxwidth = 0, maxheight = 0;
+    DOM::Node curnode = m_htmlPart->htmlDocument().body().firstChild();
+
+    for (;;) {
+
+        QRect qr = curnode.getRect();
+        int tmpwd = qr.right() - qr.left();
+
+        /*----------------------------------*/
+
+        if (curnode.nodeType() == DOM::Node::TEXT_NODE) {
+            if (xx == 0) xx = qr.left();
+            width += tmpwd;
+        }
+
+        /*----------------------------------*/
+
+        else if (curnode.nodeName().string() == "div") {
+            if (leftmrg == 0) leftmrg = qr.left();
+            width = 0;
+            xx = 0;
+        }
+
+        /*----------------------------------*/
+
+        else if (curnode.nodeName().string() == "br") {
+            width = 0;
+            xx = 0;
+        }
+
+
+        /*----------------------------------*/
+
+        if (leftmrg + xx + width > maxwidth) maxwidth = leftmrg + xx + width;
+        if (qr.bottom() > maxheight) maxheight = qr.bottom();
+
+        /* move to the next node */
+        DOM::Node next = curnode.firstChild();
+
+        if (next.isNull()) next = curnode.nextSibling();
+
+        while (!curnode.isNull() && next.isNull()) {
+            curnode = curnode.parentNode();
+            if (!curnode.isNull()) next = curnode.nextSibling();
+        }
+
+        curnode = next;
+
+        if (curnode.isNull()) break;
+    }
+
+    const int mrg = 32;
+
+    int wd = maxwidth + mrg;
+    int ht = maxheight + mrg;
+#endif
+    QFrame::adjustSize();
+    m_htmlPart->view()->setMinimumSize(width(), height());
+}
+
+
+/* public */
+void ResPopup::adjustPos(const QPoint& point)
+{
+    QPoint pos = point;
+    enum{
+        POS_LeftUp,
+        POS_RightUp,
+        POS_LeftDown,
+        POS_RightDown
+    };
+
+    /* config */
+
+    const int mrg = 16;
+
+    /*----------------------------*/
+
+    if (!m_htmlPart) return ;
+
+    QRect qr = QApplication::desktop() ->rect();
+    int sw = qr.width(), sh = qr.height();
+    int wd = width(), ht = height();
+    int x = pos.x(), y = pos.y();
+    int idx;
+
+    if ((x + mrg) + wd < sw
+            && (y - mrg) - ht >= 0) idx = POS_RightUp;
+
+    else if ((x - mrg) - wd >= 0
+              && y - (ht + mrg) >= 0) idx = POS_LeftUp;
+
+    else if ((x + mrg) + wd < sw
+              && (y + mrg) + ht < sh) idx = POS_RightDown;
+
+    else if ((x - mrg) - wd >= 0
+              && (y + mrg) + ht < sh) idx = POS_LeftDown;
+
+    else {
+        int area[ 4 ];
+        area[ 0 ] = (sw - x) * y;
+        area[ 1 ] = x * y;
+        area[ 2 ] = (sw - x) * (sh - y);
+        area[ 3 ] = x * (sh - y);
+
+        idx = 0;
+        for (int i = 1; i < 4; ++i) if (area[ i ] > area[ idx ]) idx = i;
+    }
+
+    switch (idx) {
+
+    case POS_RightUp:
+        x = x + mrg;
+        y = (y - mrg) - ht;
+        break;
+
+    case POS_LeftUp:
+        x = (x - mrg) - wd;
+        y = (y - mrg) - ht;
+        break;
+
+    case POS_RightDown:
+        x = x + mrg;
+        y = y + mrg;
+        break;
+
+    case POS_LeftDown:
+        x = (x - mrg) - wd;
+        y = y + mrg;
+        break;
+    }
+
+    if (x < 0) {
+
+        x = ht % 16;
+    }
+    if (x + wd >= sw) {
+
+        x = sw - wd - (ht % 16);
+
+        if (x < 0) {
+            if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+            x = 0;
+            wd = sw;
+        }
+    }
+
+    if (y < 0) {
+        if (x <= pos.x() && pos.x() < x + wd) {
+            if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+            ht += y;
+        }
+        y = 0;
+    }
+    if (y + ht >= sh) {
+
+        if (x <= pos.x() && pos.x() < x + wd) {
+            if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+            ht = sh - y;
+        } else {
+            y = sh - ht;
+
+            if (y < 0) {
+                if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+                y = 0;
+                ht = sh;
+            }
+        }
+    }
+
+    pos.setX(x);
+    pos.setY(y);
+    move(pos);
+
+    if (m_htmlPart) m_htmlPart->view() ->resize(wd, ht);
+    resize(wd , ht);
+}
+
+
+/* move mouse pointer above the popup frame */  /* public */
+void ResPopup::moveMouseAbove()
+{
+    /* config */
+
+    const int mrg = 10;
+
+    /*-------------------------------*/
+
+    QPoint pos = QCursor::pos();
+    int cx = pos.x(), cy = pos.y();
+    int px = x();
+    int py = y();
+    int wd = width();
+    int ht = height();
+
+    if (cx <= px) cx = px + mrg;
+    else if (cx >= px + wd) cx = px + wd - mrg;
+
+    if (cy <= py) cy = py + mrg;
+    else if (cy >= py + ht) cy = py + ht - mrg;
+
+    QCursor::setPos(cx, cy);
+}