1 /***************************************************************************
2 * Copyright (C) 2007 by Kita Developers *
3 * ikemo@users.sourceforge.jp *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
13 #include <QtGui/QApplication>
14 #include <QtGui/QCursor>
15 #include <QtGui/QDesktopWidget>
17 #include <dom/html_document.h>
18 #include <dom/html_element.h>
22 #include "kitaui/htmlview.h"
23 #include "libkita/colorconfig.h"
24 #include "libkita/globalconfig.h"
28 ResPopup::ResPopup(KHTMLView* view, const KUrl& url)
30 Qt::FramelessWindowHint
33 | Qt::X11BypassWindowManagerHint
36 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
40 m_htmlPart = new HTMLPart(this);
41 m_htmlPart->view()->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
42 m_htmlPart->setup(HTMLPART_MODE_POPUP , url);
43 connect(m_htmlPart, SIGNAL(hideChildPopup()), SIGNAL(hideChildPopup()));
55 void ResPopup::setText(const QString& str)
57 const int maxwd = 1600;
58 const int maxht = 1200;
60 QString style = QString("body.pop {"
64 " background-color: %4; "
67 .arg(GlobalConfig::popupFont().pointSize())
68 .arg(GlobalConfig::popupFont().family())
69 .arg(ColorConfig::popup().name())
70 .arg(ColorConfig::popupBackground().name());
72 QString text = "<html><head><style>";
73 text += DEFAULT_STYLESHEET;
75 text += "</style></head><body class=\"pop\">";
77 text += "</body></html>";
80 m_htmlPart->view()->setMinimumSize(maxwd, maxht);
81 m_htmlPart->setJScriptEnabled(false);
82 m_htmlPart->setJavaEnabled(false);
83 m_htmlPart->begin(KUrl("file:/dummy.htm"));
84 m_htmlPart->write(text);
86 //m_htmlPart->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); TODO
91 void ResPopup::adjustSize()
94 if (!m_htmlPart) return ;
96 int width = 0, xx = 0, leftmrg = 0;
97 int maxwidth = 0, maxheight = 0;
98 DOM::Node curnode = m_htmlPart->htmlDocument().body().firstChild();
102 QRect qr = curnode.getRect();
103 int tmpwd = qr.right() - qr.left();
105 /*----------------------------------*/
107 if (curnode.nodeType() == DOM::Node::TEXT_NODE) {
108 if (xx == 0) xx = qr.left();
112 /*----------------------------------*/
114 else if (curnode.nodeName().string() == "div") {
115 if (leftmrg == 0) leftmrg = qr.left();
120 /*----------------------------------*/
122 else if (curnode.nodeName().string() == "br") {
128 /*----------------------------------*/
130 if (leftmrg + xx + width > maxwidth) maxwidth = leftmrg + xx + width;
131 if (qr.bottom() > maxheight) maxheight = qr.bottom();
133 /* move to the next node */
134 DOM::Node next = curnode.firstChild();
136 if (next.isNull()) next = curnode.nextSibling();
138 while (!curnode.isNull() && next.isNull()) {
139 curnode = curnode.parentNode();
140 if (!curnode.isNull()) next = curnode.nextSibling();
145 if (curnode.isNull()) break;
150 int wd = maxwidth + mrg;
151 int ht = maxheight + mrg;
153 QFrame::adjustSize();
154 m_htmlPart->view()->setMinimumSize(width(), height());
159 void ResPopup::adjustPos(const QPoint& point)
173 /*----------------------------*/
175 if (!m_htmlPart) return ;
177 QRect qr = QApplication::desktop() ->rect();
178 int sw = qr.width(), sh = qr.height();
179 int wd = width(), ht = height();
180 int x = pos.x(), y = pos.y();
183 if ((x + mrg) + wd < sw
184 && (y - mrg) - ht >= 0) idx = POS_RightUp;
186 else if ((x - mrg) - wd >= 0
187 && y - (ht + mrg) >= 0) idx = POS_LeftUp;
189 else if ((x + mrg) + wd < sw
190 && (y + mrg) + ht < sh) idx = POS_RightDown;
192 else if ((x - mrg) - wd >= 0
193 && (y + mrg) + ht < sh) idx = POS_LeftDown;
197 area[ 0 ] = (sw - x) * y;
199 area[ 2 ] = (sw - x) * (sh - y);
200 area[ 3 ] = x * (sh - y);
203 for (int i = 1; i < 4; ++i) if (area[ i ] > area[ idx ]) idx = i;
235 x = sw - wd - (ht % 16);
238 if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
245 if (x <= pos.x() && pos.x() < x + wd) {
246 if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
253 if (x <= pos.x() && pos.x() < x + wd) {
254 if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
260 if (m_htmlPart) m_htmlPart->view() ->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
271 if (m_htmlPart) m_htmlPart->view() ->resize(wd, ht);
276 /* move mouse pointer above the popup frame */ /* public */
277 void ResPopup::moveMouseAbove()
283 /*-------------------------------*/
285 QPoint pos = QCursor::pos();
286 int cx = pos.x(), cy = pos.y();
292 if (cx <= px) cx = px + mrg;
293 else if (cx >= px + wd) cx = px + wd - mrg;
295 if (cy <= py) cy = py + mrg;
296 else if (cy >= py + ht) cy = py + ht - mrg;
298 QCursor::setPos(cx, cy);