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 <qapplication.h>
16 #include <dom/html_element.h>
18 #include "kitaui/htmlview.h"
20 #include "libkita/config_xt.h"
21 #include "libkita/kitaconfig.h"
25 ResPopup::ResPopup( KHTMLView* view, const KURL& url )
26 : QFrame( view, "res_popup",
37 m_htmlPart = new KitaHTMLPart( this );
38 m_htmlPart->setup( HTMLPART_MODE_POPUP , url );
39 connect( m_htmlPart, SIGNAL( hideChildPopup() ), SIGNAL( hideChildPopup() ) );
45 if ( m_htmlPart ) delete m_htmlPart;
51 void ResPopup::setText( const QString& str )
53 const int maxwd = 1600;
54 const int maxht = 1200;
56 QString style = QString( "body.pop {"
60 " background-color: %4; "
63 .arg( Kita::Config::popupFont().pointSize() )
64 .arg( Kita::Config::popupFont().family() )
65 .arg( Kita::Config::popupColor().name() )
66 .arg( Kita::Config::popupBackground().name() );
68 QString text = "<html><head><style>";
69 text += KitaConfig::defaultStyleSheetText();
71 if ( Kita::Config::useStyleSheet() ) {
72 text += KitaConfig::styleSheetText();
74 text += "</style></head><body class=\"pop\">";
76 text += "</body></html>";
79 m_htmlPart->view() ->resize( maxwd, maxht );
80 m_htmlPart->setJScriptEnabled( FALSE );
81 m_htmlPart->setJavaEnabled( FALSE );
82 m_htmlPart->begin( "file:/dummy.htm" );
83 m_htmlPart->write( text );
85 m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOff );
91 void ResPopup::adjustSize()
93 if ( !m_htmlPart ) return ;
95 int width = 0, xx = 0, leftmrg = 0;
96 int maxwidth = 0, maxheight = 0;
97 DOM::Node curnode = m_htmlPart->htmlDocument().body().firstChild();
101 QRect qr = curnode.getRect();
102 int tmpwd = qr.right() - qr.left();
104 /*----------------------------------*/
106 if ( curnode.nodeType() == DOM::Node::TEXT_NODE ) {
107 if ( xx == 0 ) xx = qr.left();
111 /*----------------------------------*/
113 else if ( curnode.nodeName().string() == "div" ) {
114 if ( leftmrg == 0 ) leftmrg = qr.left();
119 /*----------------------------------*/
121 else if ( curnode.nodeName().string() == "br" ) {
127 /*----------------------------------*/
129 if ( leftmrg + xx + width > maxwidth ) maxwidth = leftmrg + xx + width;
130 if ( qr.bottom() > maxheight ) maxheight = qr.bottom();
132 /* move to the next node */
133 DOM::Node next = curnode.firstChild();
135 if ( next.isNull() ) next = curnode.nextSibling();
137 while ( !curnode.isNull() && next.isNull() ) {
138 curnode = curnode.parentNode();
139 if ( !curnode.isNull() ) next = curnode.nextSibling();
144 if ( curnode.isNull() ) break;
149 int wd = maxwidth + mrg;
150 int ht = maxheight + mrg;
152 m_htmlPart->view() ->resize( wd, ht );
153 QFrame::adjustSize();
158 void ResPopup::adjustPos( QPoint pos )
171 /*----------------------------*/
173 if ( !m_htmlPart ) return ;
175 QRect qr = QApplication::desktop() ->rect();
176 int sw = qr.width(), sh = qr.height();
177 int wd = width(), ht = height();
178 int x = pos.x(), y = pos.y();
181 if ( ( x + mrg ) + wd < sw
182 && ( y - mrg ) - ht >= 0 ) idx = POS_RightUp;
184 else if ( ( x - mrg ) - wd >= 0
185 && y - ( ht + mrg ) >= 0 ) idx = POS_LeftUp;
187 else if ( ( x + mrg ) + wd < sw
188 && ( y + mrg ) + ht < sh ) idx = POS_RightDown;
190 else if ( ( x - mrg ) - wd >= 0
191 && ( y + mrg ) + ht < sh ) idx = POS_LeftDown;
195 area[ 0 ] = ( sw - x ) * y;
197 area[ 2 ] = ( sw - x ) * ( sh - y );
198 area[ 3 ] = x * ( sh - y );
201 for ( int i = 1; i < 4; ++i ) if ( area[ i ] > area[ idx ] ) idx = i;
208 y = ( y - mrg ) - ht;
212 x = ( x - mrg ) - wd;
213 y = ( y - mrg ) - ht;
222 x = ( x - mrg ) - wd;
231 if ( x + wd >= sw ) {
233 x = sw - wd - ( ht % 16 );
236 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
243 if ( x <= pos.x() && pos.x() < x + wd ) {
244 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
249 if ( y + ht >= sh ) {
251 if ( x <= pos.x() && pos.x() < x + wd ) {
252 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
258 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
269 if ( m_htmlPart ) m_htmlPart->view() ->resize( wd, ht );
274 /* move mouse pointer above the popup frame */ /* public */
275 void ResPopup::moveMouseAbove()
281 /*-------------------------------*/
283 QPoint pos = QCursor::pos();
284 int cx = pos.x(), cy = pos.y();
290 if ( cx <= px ) cx = px + mrg;
291 else if ( cx >= px + wd ) cx = px + wd - mrg;
293 if ( cy <= py ) cy = py + mrg;
294 else if ( cy >= py + ht ) cy = py + ht - mrg;
296 QCursor::setPos( cx, cy );