File indexing completed on 2023-05-30 09:09:59
0001 /* This file is part of the KDE project 0002 0003 Copyright (C) 1997 Martin Jones (mjones@kde.org) 0004 (C) 1998 Waldo Bastian (bastian@kde.org) 0005 (C) 1998, 1999 Torben Weis (weis@kde.org) 0006 (C) 1999 Lars Knoll (knoll@kde.org) 0007 (C) 1999 Antti Koivisto (koivisto@kde.org) 0008 (C) 2006 Germain Garand (germain@ebooksfrance.org) 0009 0010 This library is free software; you can redistribute it and/or 0011 modify it under the terms of the GNU Library General Public 0012 License as published by the Free Software Foundation; either 0013 version 2 of the License, or (at your option) any later version. 0014 0015 This library is distributed in the hope that it will be useful, 0016 but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0018 Library General Public License for more details. 0019 0020 You should have received a copy of the GNU Library General Public License 0021 along with this library; see the file COPYING.LIB. If not, write to 0022 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0023 Boston, MA 02110-1301, USA. 0024 */ 0025 0026 #ifndef KHTMLVIEW_H 0027 #define KHTMLVIEW_H 0028 0029 #include <khtml_export.h> 0030 0031 // qt includes and classes 0032 #include <QScrollArea> 0033 0034 class QPainter; 0035 class QPrinter; 0036 class QRect; 0037 template< typename T > class QVector; 0038 template <class T> class QStack; 0039 0040 namespace DOM 0041 { 0042 class HTMLDocumentImpl; 0043 class DocumentImpl; 0044 class ElementImpl; 0045 class HTMLTitleElementImpl; 0046 class HTMLGenericFormElementImpl; 0047 class HTMLFormElementImpl; 0048 class HTMLAnchorElementImpl; 0049 class HTMLInputElementImpl; 0050 class NodeImpl; 0051 class CSSProperty; 0052 } 0053 0054 namespace KJS 0055 { 0056 class WindowFunc; 0057 class ExternalFunc; 0058 } 0059 0060 namespace khtml 0061 { 0062 class RenderObject; 0063 class RenderCanvas; 0064 class RenderLineEdit; 0065 class RenderPartObject; 0066 class RenderWidget; 0067 class RenderLayer; 0068 class RenderBox; 0069 class CSSStyleSelector; 0070 class LineEditWidget; 0071 class CaretBox; 0072 class HTMLTokenizer; 0073 class KHTMLWidgetPrivate; 0074 class KHTMLWidget 0075 { 0076 public: 0077 KHTMLWidget(); 0078 ~KHTMLWidget(); 0079 KHTMLWidgetPrivate *m_kwp; 0080 }; 0081 void applyRule(DOM::CSSProperty *prop); 0082 } 0083 0084 class KHTMLPart; 0085 class KHTMLViewPrivate; 0086 0087 namespace khtml 0088 { 0089 0090 } 0091 0092 /** 0093 * Renders and displays HTML in a QScrollArea. 0094 * 0095 * Suitable for use as an application's main view. 0096 **/ 0097 class KHTML_EXPORT KHTMLView : public QScrollArea, public khtml::KHTMLWidget 0098 { 0099 Q_OBJECT 0100 0101 friend class DOM::HTMLDocumentImpl; 0102 friend class DOM::HTMLTitleElementImpl; 0103 friend class DOM::HTMLGenericFormElementImpl; 0104 friend class DOM::HTMLFormElementImpl; 0105 friend class DOM::HTMLAnchorElementImpl; 0106 friend class DOM::HTMLInputElementImpl; 0107 friend class DOM::NodeImpl; 0108 friend class DOM::ElementImpl; 0109 friend class DOM::DocumentImpl; 0110 friend class KHTMLPart; 0111 friend class KHTMLFind; 0112 friend class StorePass; 0113 friend class khtml::RenderCanvas; 0114 friend class khtml::RenderObject; 0115 friend class khtml::RenderLineEdit; 0116 friend class khtml::RenderPartObject; 0117 friend class khtml::RenderWidget; 0118 friend class khtml::KHTMLWidgetPrivate; 0119 friend class khtml::RenderLayer; 0120 friend class khtml::RenderBox; 0121 friend class khtml::CSSStyleSelector; 0122 friend class khtml::LineEditWidget; 0123 friend class khtml::HTMLTokenizer; 0124 friend class KJS::WindowFunc; 0125 friend class KJS::ExternalFunc; 0126 friend void khtml::applyRule(DOM::CSSProperty *prop); 0127 0128 public: 0129 /** 0130 * Constructs a KHTMLView. 0131 */ 0132 KHTMLView(KHTMLPart *part, QWidget *parent); 0133 virtual ~KHTMLView(); 0134 0135 /** 0136 * Returns a pointer to the KHTMLPart that is 0137 * rendering the page. 0138 **/ 0139 KHTMLPart *part() const 0140 { 0141 return m_part; 0142 } 0143 0144 int frameWidth() const 0145 { 0146 return _width; 0147 } 0148 0149 /** 0150 * Sets a margin in x direction. 0151 */ 0152 void setMarginWidth(int x); 0153 0154 /** 0155 * Returns the margin width. 0156 * 0157 * A return value of -1 means the default value will be used. 0158 */ 0159 int marginWidth() const 0160 { 0161 return _marginWidth; 0162 } 0163 0164 /* 0165 * Sets a margin in y direction. 0166 */ 0167 void setMarginHeight(int y); 0168 0169 /** 0170 * Returns the margin height. 0171 * 0172 * A return value of -1 means the default value will be used. 0173 */ 0174 int marginHeight() 0175 { 0176 return _marginHeight; 0177 } 0178 0179 /** 0180 * Sets vertical scrollbar mode. 0181 * 0182 * WARNING: do not call this method on a base class pointer unless you 0183 * specifically want QAbstractScrollArea's variant (not recommended). 0184 * QAbstractScrollArea::setVerticalScrollBarPolicy is *not* virtual. 0185 */ 0186 virtual void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy); 0187 0188 /** 0189 * Sets horizontal scrollbar mode. 0190 * 0191 * WARNING: do not call this method on a base class pointer unless you 0192 * specifically want QAbstractScrollArea's variant (not recommended). 0193 * QAbstractScrollArea::setHorizontalScrollBarPolicy is *not* virtual. 0194 */ 0195 virtual void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy); 0196 0197 /** 0198 * Prints the HTML document. 0199 * @param quick if true, fully automated printing, without print dialog 0200 */ 0201 void print(bool quick = false); 0202 0203 /** 0204 * Prints the HTML document. 0205 * @param printer shared printer instance 0206 * @param quick if true, fully automated printing, without print dialog 0207 * 0208 * @since 5.64 0209 */ 0210 void print(QPrinter *printer, bool quick = false); 0211 0212 /** 0213 * Display all accesskeys in small tooltips 0214 */ 0215 void displayAccessKeys(); 0216 0217 /** 0218 * Returns the contents area's width 0219 */ 0220 int contentsWidth() const; 0221 0222 /** 0223 * Returns the contents area's height 0224 */ 0225 int contentsHeight() const; 0226 0227 /** 0228 * Returns the x coordinate of the contents area point 0229 * that is currently located at the top left in the viewport 0230 */ 0231 int contentsX() const; 0232 0233 /** 0234 * Returns the y coordinate of the contents area point 0235 * that is currently located at the top left in the viewport 0236 */ 0237 int contentsY() const; 0238 0239 /** 0240 * Returns the width of the viewport 0241 */ 0242 int visibleWidth() const; 0243 0244 /** 0245 * Returns the height of the viewport 0246 */ 0247 int visibleHeight() const; 0248 0249 /** 0250 * Place the contents area point x/y 0251 * at the top left of the viewport 0252 */ 0253 void setContentsPos(int x, int y); 0254 0255 /** 0256 * Returns a point translated to viewport coordinates 0257 * @param p the contents area point to translate 0258 * 0259 */ 0260 QPoint contentsToViewport(const QPoint &p) const; 0261 0262 /** 0263 * Returns a point translated to contents area coordinates 0264 * @param p the viewport point to translate 0265 * 0266 */ 0267 QPoint viewportToContents(const QPoint &p) const; 0268 0269 /** 0270 * Returns a point translated to contents area coordinates 0271 * @param x x coordinate of viewport point to translate 0272 * @param y y coordinate of viewport point to translate 0273 * @param cx resulting x coordinate 0274 * @param cy resulting y coordinate 0275 * 0276 */ 0277 void viewportToContents(int x, int y, int &cx, int &cy) const; 0278 0279 /** 0280 * Returns a point translated to viewport coordinates 0281 * @param x x coordinate of contents area point to translate 0282 * @param y y coordinate of contents area point to translate 0283 * @param cx resulting x coordinate 0284 * @param cy resulting y coordinate 0285 * 0286 */ 0287 void contentsToViewport(int x, int y, int &cx, int &cy) const; 0288 0289 /** 0290 * Scrolls the content area by a given amount 0291 * @param x x offset 0292 * @param y y offset 0293 */ 0294 void scrollBy(int x, int y); 0295 0296 /** 0297 * Requests an update of the content area 0298 * @param r the content area rectangle to update 0299 */ 0300 void updateContents(const QRect &r); 0301 void updateContents(int x, int y, int w, int h); 0302 0303 void addChild(QWidget *child, int dx, int dy); 0304 0305 /** 0306 * Requests an immediate repaint of the content area 0307 * @param r the content area rectangle to repaint 0308 */ 0309 void repaintContents(const QRect &r); 0310 void repaintContents(int x, int y, int w, int h); 0311 0312 /** 0313 * Apply a zoom level to the content area 0314 * @param percent a zoom level expressed as a percentage 0315 */ 0316 void setZoomLevel(int percent); 0317 0318 /** 0319 * Retrieve the current zoom level 0320 * 0321 */ 0322 int zoomLevel() const; 0323 0324 /** 0325 * Smooth Scrolling Mode enumeration 0326 * @li SSMDisabled smooth scrolling is disabled 0327 * @li SSMWhenEfficient only use smooth scrolling on pages that do not require a full repaint of the content area when scrolling 0328 * @li SSMAlways smooth scrolling is performed unconditionally 0329 */ 0330 enum SmoothScrollingMode { SSMDisabled = 0, SSMWhenEfficient, SSMEnabled }; 0331 0332 /** 0333 * Set the smooth scrolling mode. 0334 * 0335 * Smooth scrolling mode is normally controlled by the configuration file's SmoothScrolling key. 0336 * Using this setter will override the configuration file's settings. 0337 * 0338 * @since 4.1 0339 */ 0340 void setSmoothScrollingMode(SmoothScrollingMode m); 0341 0342 /** 0343 * Retrieve the current smooth scrolling mode 0344 * 0345 * @since 4.1 0346 */ 0347 SmoothScrollingMode smoothScrollingMode() const; 0348 0349 public Q_SLOTS: 0350 /** 0351 * Resize the contents area 0352 * @param w the new width 0353 * @param h the new height 0354 */ 0355 virtual void resizeContents(int w, int h); 0356 0357 /** 0358 * ensure the display is up to date 0359 */ 0360 void layout(); 0361 0362 Q_SIGNALS: 0363 /** 0364 * This signal is used for internal layouting. Don't use it to check if rendering finished. 0365 * Use @ref KHTMLPart completed() signal instead. 0366 */ 0367 void finishedLayout(); 0368 void cleared(); 0369 void zoomView(int); 0370 void hideAccessKeys(); 0371 void repaintAccessKeys(); 0372 void findAheadActive(bool); 0373 0374 protected: 0375 void clear(); 0376 0377 bool event(QEvent *event) override; 0378 void paintEvent(QPaintEvent *) override; 0379 void resizeEvent(QResizeEvent *event) override; 0380 void showEvent(QShowEvent *) override; 0381 void hideEvent(QHideEvent *) override; 0382 bool focusNextPrevChild(bool next) override; 0383 void mousePressEvent(QMouseEvent *) override; 0384 void focusInEvent(QFocusEvent *) override; 0385 void focusOutEvent(QFocusEvent *) override; 0386 void mouseDoubleClickEvent(QMouseEvent *) override; 0387 void mouseMoveEvent(QMouseEvent *) override; 0388 void mouseReleaseEvent(QMouseEvent *) override; 0389 #ifndef QT_NO_WHEELEVENT 0390 void wheelEvent(QWheelEvent *) override; 0391 #endif 0392 void dragEnterEvent(QDragEnterEvent *) override; 0393 void dropEvent(QDropEvent *) override; 0394 void closeEvent(QCloseEvent *) override; 0395 virtual bool widgetEvent(QEvent *); 0396 bool viewportEvent(QEvent *e) override; 0397 bool eventFilter(QObject *, QEvent *) override; 0398 void scrollContentsBy(int dx, int dy) override; 0399 0400 void keyPressEvent(QKeyEvent *_ke) override; 0401 void keyReleaseEvent(QKeyEvent *_ke) override; 0402 void doAutoScroll(); 0403 void timerEvent(QTimerEvent *) override; 0404 0405 void setSmoothScrollingModeDefault(SmoothScrollingMode m); 0406 0407 protected Q_SLOTS: 0408 void slotPaletteChanged(); 0409 0410 private Q_SLOTS: 0411 void tripleClickTimeout(); 0412 void accessKeysTimeout(); 0413 void scrollTick(); 0414 0415 /** 0416 * @internal 0417 * used for autoscrolling with MMB 0418 */ 0419 void slotMouseScrollTimer(); 0420 0421 private: 0422 void resizeContentsToViewport(); 0423 0424 void scheduleRelayout(khtml::RenderObject *clippedObj = nullptr); 0425 void unscheduleRelayout(); 0426 0427 bool hasLayoutPending(); 0428 0429 void scheduleRepaint(int x, int y, int w, int h, bool asap = false); 0430 void unscheduleRepaint(); 0431 0432 bool needsFullRepaint() const; 0433 0434 void closeChildDialogs(); 0435 bool dialogsAllowed(); 0436 0437 void setMouseEventsTarget(QWidget *w); 0438 QWidget *mouseEventsTarget() const; 0439 0440 QStack<QRegion> *clipHolder() const; 0441 void setClipHolder(QStack<QRegion> *ch); 0442 0443 void setPart(KHTMLPart *part); 0444 0445 /** 0446 * Paints the HTML document to a QPainter. 0447 * The document will be scaled to match the width of 0448 * rc and clipped to fit in the height. 0449 * yOff determines the vertical offset in the document to start with. 0450 * more, if nonzero will be set to true if the documents extends 0451 * beyond the rc or false if everything below yOff was painted. 0452 **/ 0453 void paint(QPainter *p, const QRect &rc, int yOff = 0, bool *more = nullptr); 0454 0455 void render(QPainter *p, const QRect &r, const QPoint &off); 0456 0457 /** 0458 * Get/set the CSS Media Type. 0459 * 0460 * Media type is set to "screen" for on-screen rendering and "print" 0461 * during printing. Other media types lack the proper support in the 0462 * renderer and are not activated. The DOM tree and the parser itself, 0463 * however, properly handle other media types. To make them actually work 0464 * you only need to enable the media type in the view and if necessary 0465 * add the media type dependent changes to the renderer. 0466 */ 0467 void setMediaType(const QString &medium); 0468 QString mediaType() const; 0469 0470 bool pagedMode() const; 0471 0472 bool scrollTo(const QRect &); 0473 0474 bool focusNextPrevNode(bool next); 0475 bool handleAccessKey(const QKeyEvent *ev); 0476 bool focusNodeWithAccessKey(QChar c, KHTMLView *caller = nullptr); 0477 QMap< DOM::ElementImpl *, QChar > buildFallbackAccessKeys() const; 0478 void displayAccessKeys(KHTMLView *caller, KHTMLView *origview, QVector< QChar > &taken, bool use_fallbacks); 0479 bool isScrollingFromMouseWheel() const; 0480 void setHasStaticBackground(bool partial = false); 0481 void setHasNormalBackground(); 0482 void addStaticObject(bool fixed); 0483 void removeStaticObject(bool fixed); 0484 void applyTransforms(int &x, int &y, int &w, int &h) const; 0485 void revertTransforms(int &x, int &y, int &w, int &h) const; 0486 void revertTransforms(int &x, int &y) const; 0487 void checkExternalWidgetsPosition(); 0488 0489 void setIgnoreWheelEvents(bool e); 0490 0491 void initWidget(); 0492 0493 DOM::NodeImpl *nodeUnderMouse() const; 0494 DOM::NodeImpl *nonSharedNodeUnderMouse() const; 0495 0496 void restoreScrollBar(); 0497 0498 QStringList formCompletionItems(const QString &name) const; 0499 void clearCompletionHistory(const QString &name); 0500 void addFormCompletionItem(const QString &name, const QString &value); 0501 0502 void addNonPasswordStorableSite(const QString &host); 0503 void delNonPasswordStorableSite(const QString &host); 0504 bool nonPasswordStorableSite(const QString &host) const; 0505 0506 bool dispatchMouseEvent(int eventId, DOM::NodeImpl *targetNode, 0507 DOM::NodeImpl *targetNodeNonShared, bool cancelable, 0508 int detail, QMouseEvent *_mouse, bool setUnder, 0509 int mouseEventType, int orientation = 0); 0510 bool dispatchKeyEvent(QKeyEvent *_ke); 0511 bool dispatchKeyEventHelper(QKeyEvent *_ke, bool generate_keypress); 0512 0513 void complete(bool pendingAction); 0514 0515 void updateScrollBars(); 0516 void setupSmoothScrolling(int dx, int dy); 0517 0518 /** 0519 * Returns the current caret policy when the view is not focused. 0520 * @return a KHTMLPart::CaretDisplay value 0521 */ 0522 int caretDisplayPolicyNonFocused() const; 0523 0524 /** 0525 * Sets the caret display policy when the view is not focused. 0526 * @param policy new display policy as 0527 * defined by KHTMLPart::CaretDisplayPolicy 0528 */ 0529 void setCaretDisplayPolicyNonFocused(int policy); 0530 0531 // -- caret event handler 0532 0533 /** 0534 * Evaluates key presses for caret navigation on editable nodes. 0535 * @return true if event has been handled 0536 */ 0537 bool caretKeyPressEvent(QKeyEvent *); 0538 0539 // ------------------------------------- member variables ------------------------------------ 0540 private: 0541 friend class KHTMLViewPrivate; 0542 enum LinkCursor { LINK_NORMAL, LINK_MAILTO, LINK_NEWWINDOW }; 0543 0544 void setWidgetVisible(::khtml::RenderWidget *, bool visible); 0545 0546 int _width; 0547 int _height; 0548 0549 int _marginWidth; 0550 int _marginHeight; 0551 0552 KHTMLPart *m_part; 0553 KHTMLViewPrivate *const d; 0554 0555 QString m_medium; // media type 0556 }; 0557 0558 #endif 0559