OSDN Git Service

remove unused code.
[kita/kita.git] / kita / src / respopup.cpp
1 /***************************************************************************
2 *   Copyright (C) 2007 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
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 ***************************************************************************/
10
11 #include "respopup.h"
12
13 #include <qapplication.h>
14 #include <qcursor.h>
15
16 #include <dom/html_element.h>
17
18 #include "kitaui/htmlview.h"
19
20 #include "libkita/config_xt.h"
21 #include "libkita/kitaconfig.h"
22
23 namespace Kita
24 {
25     ResPopup::ResPopup( KHTMLView* view, const KURL& url )
26             : QFrame( view, "res_popup",
27                       WStyle_Customize
28                       | WStyle_NoBorder
29                       | WStyle_Tool
30                       | WType_TopLevel
31                       | WX11BypassWM
32                     )
33     {
34         m_url = url;
35         m_htmlPart = NULL;
36
37         m_htmlPart = new KitaHTMLPart( this );
38         m_htmlPart->setup( HTMLPART_MODE_POPUP , url );
39         connect( m_htmlPart, SIGNAL( hideChildPopup() ), SIGNAL( hideChildPopup() ) );
40     }
41
42
43     ResPopup::~ResPopup()
44     {
45         if ( m_htmlPart ) delete m_htmlPart;
46     }
47
48
49
50     /* public */
51     void ResPopup::setText( const QString& str )
52     {
53         const int maxwd = 1600;
54         const int maxht = 1200;
55
56         QString style = QString( "body.pop {"
57                                  " font-size: %1pt; "
58                                  " font-family: %2; "
59                                  " color: %3; "
60                                  " background-color: %4; "
61                                  " border-width: 0;"
62                                  "}" )
63                         .arg( Kita::Config::popupFont().pointSize() )
64                         .arg( Kita::Config::popupFont().family() )
65                         .arg( Kita::Config::popupColor().name() )
66                         .arg( Kita::Config::popupBackground().name() );
67
68         QString text = "<html><head><style>";
69         text += KitaConfig::defaultStyleSheetText();
70         text += style;
71         if ( Kita::Config::useStyleSheet() ) {
72             text += KitaConfig::styleSheetText();
73         }
74         text += "</style></head><body class=\"pop\">";
75         text += str;
76         text += "</body></html>";
77
78         if ( m_htmlPart ) {
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 );
84             m_htmlPart->end();
85             m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOff );
86         }
87     }
88
89
90     /* public */
91     void ResPopup::adjustSize()
92     {
93         if ( !m_htmlPart ) return ;
94
95         int width = 0, xx = 0, leftmrg = 0;
96         int maxwidth = 0, maxheight = 0;
97         DOM::Node curnode = m_htmlPart->htmlDocument().body().firstChild();
98
99         for ( ;; ) {
100
101             QRect qr = curnode.getRect();
102             int tmpwd = qr.right() - qr.left();
103
104             /*----------------------------------*/
105
106             if ( curnode.nodeType() == DOM::Node::TEXT_NODE ) {
107                 if ( xx == 0 ) xx = qr.left();
108                 width += tmpwd;
109             }
110
111             /*----------------------------------*/
112
113             else if ( curnode.nodeName().string() == "div" ) {
114                 if ( leftmrg == 0 ) leftmrg = qr.left();
115                 width = 0;
116                 xx = 0;
117             }
118
119             /*----------------------------------*/
120
121             else if ( curnode.nodeName().string() == "br" ) {
122                 width = 0;
123                 xx = 0;
124             }
125
126
127             /*----------------------------------*/
128
129             if ( leftmrg + xx + width > maxwidth ) maxwidth = leftmrg + xx + width;
130             if ( qr.bottom() > maxheight ) maxheight = qr.bottom();
131
132             /* move to the next node */
133             DOM::Node next = curnode.firstChild();
134
135             if ( next.isNull() ) next = curnode.nextSibling();
136
137             while ( !curnode.isNull() && next.isNull() ) {
138                 curnode = curnode.parentNode();
139                 if ( !curnode.isNull() ) next = curnode.nextSibling();
140             }
141
142             curnode = next;
143
144             if ( curnode.isNull() ) break;
145         }
146
147         const int mrg = 32;
148
149         int wd = maxwidth + mrg;
150         int ht = maxheight + mrg;
151
152         m_htmlPart->view() ->resize( wd, ht );
153         QFrame::adjustSize();
154     }
155
156
157     /* public */
158     void ResPopup::adjustPos( QPoint pos )
159     {
160         enum{
161             POS_LeftUp,
162             POS_RightUp,
163             POS_LeftDown,
164             POS_RightDown
165         };
166
167         /* config */
168
169         const int mrg = 16;
170
171         /*----------------------------*/
172
173         if ( !m_htmlPart ) return ;
174
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();
179         int idx;
180
181         if ( ( x + mrg ) + wd < sw
182                 && ( y - mrg ) - ht >= 0 ) idx = POS_RightUp;
183
184         else if ( ( x - mrg ) - wd >= 0
185                   && y - ( ht + mrg ) >= 0 ) idx = POS_LeftUp;
186
187         else if ( ( x + mrg ) + wd < sw
188                   && ( y + mrg ) + ht < sh ) idx = POS_RightDown;
189
190         else if ( ( x - mrg ) - wd >= 0
191                   && ( y + mrg ) + ht < sh ) idx = POS_LeftDown;
192
193         else {
194             int area[ 4 ];
195             area[ 0 ] = ( sw - x ) * y;
196             area[ 1 ] = x * y;
197             area[ 2 ] = ( sw - x ) * ( sh - y );
198             area[ 3 ] = x * ( sh - y );
199
200             idx = 0;
201             for ( int i = 1; i < 4; ++i ) if ( area[ i ] > area[ idx ] ) idx = i;
202         }
203
204         switch ( idx ) {
205
206         case POS_RightUp:
207             x = x + mrg;
208             y = ( y - mrg ) - ht;
209             break;
210
211         case POS_LeftUp:
212             x = ( x - mrg ) - wd;
213             y = ( y - mrg ) - ht;
214             break;
215
216         case POS_RightDown:
217             x = x + mrg;
218             y = y + mrg;
219             break;
220
221         case POS_LeftDown:
222             x = ( x - mrg ) - wd;
223             y = y + mrg;
224             break;
225         }
226
227         if ( x < 0 ) {
228
229             x = ht % 16;
230         }
231         if ( x + wd >= sw ) {
232
233             x = sw - wd - ( ht % 16 );
234
235             if ( x < 0 ) {
236                 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
237                 x = 0;
238                 wd = sw;
239             }
240         }
241
242         if ( y < 0 ) {
243             if ( x <= pos.x() && pos.x() < x + wd ) {
244                 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
245                 ht += y;
246             }
247             y = 0;
248         }
249         if ( y + ht >= sh ) {
250
251             if ( x <= pos.x() && pos.x() < x + wd ) {
252                 if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
253                 ht = sh - y;
254             } else {
255                 y = sh - ht;
256
257                 if ( y < 0 ) {
258                     if ( m_htmlPart ) m_htmlPart->view() ->setVScrollBarMode( QScrollView::AlwaysOn );
259                     y = 0;
260                     ht = sh;
261                 }
262             }
263         }
264
265         pos.setX( x );
266         pos.setY( y );
267         move( pos );
268
269         if ( m_htmlPart ) m_htmlPart->view() ->resize( wd, ht );
270         resize( wd , ht );
271     }
272
273
274     /* move mouse pointer above the popup frame */  /* public */
275     void ResPopup::moveMouseAbove()
276     {
277         /* config */
278
279         const int mrg = 10;
280
281         /*-------------------------------*/
282
283         QPoint pos = QCursor::pos();
284         int cx = pos.x(), cy = pos.y();
285         int px = x();
286         int py = y();
287         int wd = width();
288         int ht = height();
289
290         if ( cx <= px ) cx = px + mrg;
291         else if ( cx >= px + wd ) cx = px + wd - mrg;
292
293         if ( cy <= py ) cy = py + mrg;
294         else if ( cy >= py + ht ) cy = py + ht - mrg;
295
296         QCursor::setPos( cx, cy );
297     }
298 }