📄 mghtmlview.cpp
字号:
// mghtmlview: the main part of displaying data.//// -----------------------------------------------------------------------------------#include <pthread.h>#include <semaphore.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdlib.h>#include <qvariant.h>#include <qlist.h>#include "render_interface.h"#include "render_replaced.h"#include "render_object.h"#include "render_root.h"#include "render_style.h"#include "mgcolor.h"#include "mgpen.h"#include "mgbrush.h"#include "mgfont.h"#include "mgpainter.h"#include "html_elementimpl.h"#include "html_documentimpl.h"#include "html_inlineimpl.h"#include "mghtml_settings.h"#include "mghtml_part.h"#include "../misc/htmltags.h"#include "mghtmlview.h"#include "../include/typedef.h"#include "../net/network.h"#include "mgmessage.h"#if 0#include "mgjs_main.h"#endif#include "mghtmlview.h"#include "kdebug.h"#include <assert.h>#include "render_interface.h"#ifdef JAVASCRIPT_ENABLE#include "../mgjs/mgjs_EventConst.h"#endif#define DEBUG_BY_XHTANG 0using namespace DOM;using namespace khtml;extern void SetAddressForBar(char * strAddress);extern void SetStatusForBar(char * strStatus);extern QList<char> addresslist;class MGHTMLViewPrivate { // this class is for restoring the data in the viewpublic: MGHTMLViewPrivate() { reset(); } ~MGHTMLViewPrivate() { } void reset() { underMouse = 0; linkPressed = false; useSlowRepaints = false; currentNode = 0; originalNode = 0; } NodeImpl *underMouse; DOM::HTMLElementImpl *currentNode; DOM::HTMLElementImpl *originalNode; bool linkPressed; bool useSlowRepaints; // i do not know what is the meaning it is. ?????};MGHTMLView::MGHTMLView(){ m_part = 0; m_painter = 0; m_doc = 0; m_xOffset = 0; m_yOffset = 0; #ifdef JAVASCRIPT_ENABLE m_JSRuntime = NULL; m_JSContext = NULL; m_JSObject = NULL;#endif // !!!!! get the cursor from setting. m_handCursor = GetSystemCursor (IDC_HAND_POINT); d = new MGHTMLViewPrivate;}MGHTMLView::~MGHTMLView(){#ifdef JAVASCRIPT_ENABLE FreeJavaScript(this,m_JSRuntime,m_JSContext); m_JSRuntime = NULL; m_JSContext = NULL; #endif //DestroyAllControls(m_hWnd); //DestroyWindow(m_hWnd); if (m_part) { //DOM::HTMLDocumentImpl *doc = m_part->docImpl(); //if (doc && doc->body()) // doc->detach(); delete m_part; m_part = NULL; } if(m_painter) { delete m_painter; m_painter = NULL; } if(d) { delete d; d = NULL; }}void MGHTMLView::init() // invoke it from create message.{ _marginWidth = -1; _marginHeight = -1; _width = 0; _height = 0; m_xOffset = 0; m_yOffset = 0; resizeContents(visibleWidth(), visibleHeight());}int MGHTMLView::visibleWidth() const{ RECT rect; GetClientRect( m_hWnd, &rect ); return rect.right-rect.left+1;}int MGHTMLView::visibleHeight() const{ RECT rect; GetClientRect( m_hWnd, &rect ); return rect.bottom-rect.top+1;}void MGHTMLView::clear() // repaint the screen{ // ????? repaint the scroll bar? resizeContents(visibleWidth(), visibleHeight()); if(d->useSlowRepaints) { // why do we need to delete the paintBuffer here ? // delete d->tp; // tp = 0; // delete paintBuffer; // paintBuffer = 0; // setStaticBackground(false); } d->reset(); // emit cleared(); // !!!!! what to do ?}void MGHTMLView::drawContents(HDC hdc, int ex, int ey, int ew, int eh){ m_painter->hdc = hdc; NodeImpl *body = 0; if( m_part->docImpl() ) body = m_part->docImpl()->body(); if(!body) //FillBox(hdc, ex, ey, ew, eh); return; m_doc->renderer()->print( m_painter, 0, 0, ew, eh, ex, ey );}void MGHTMLView::layoutIFrame(DOM::HTMLElementImpl *child){ DOM::HTMLElementImpl *nchild; if(child==NULL)return; if(child->id()==ID_IFRAME&&child->renderer()!=NULL){ child->renderer()->layout(); } nchild=(DOM::HTMLElementImpl *)(child->firstChild()); while(nchild!=NULL){ layoutIFrame(nchild); nchild = (HTMLElementImpl *)(nchild->nextSibling()); }}void MGHTMLView::setLayouted(DOM::HTMLElementImpl *child,bool f){ HTMLElementImpl *nchild; if(!child)return; if(child->renderer())child->renderer()->setLayouted(f); nchild=(HTMLElementImpl *)(child->firstChild()); while(nchild!=0){ setLayouted(nchild,f); nchild = (HTMLElementImpl *)(nchild->nextSibling()); }}void MGHTMLView::layout(){ if( m_part->docImpl() ) { DOM::HTMLDocumentImpl *document = m_part->docImpl(); khtml::RenderRoot* root = static_cast<khtml::RenderRoot *>(document->renderer()); NodeImpl *body = document->body(); if(body && body->id() == ID_FRAMESET) { _width = visibleWidth(); body->renderer()->setLayouted(false); body->renderer()->layout(); root->layout(); return; } _height = visibleHeight(); _width = visibleWidth(); root->setLayouted(false); setLayouted((DOM::HTMLElementImpl*)body,false); root->layout(); layoutIFrame((DOM::HTMLElementImpl*)body); //m_doc->renderer()->layout(); // ????? if( visibleWidth() < m_clntSize.width() ) { EnableScrollBar( getHwnd(), SB_HORZ, true ); SetScrollRange( getHwnd(), SB_HORZ, 0, m_clntSize.width()-visibleWidth() ); SetScrollPos( getHwnd(), SB_HORZ, 0 ); ShowScrollBar( getHwnd(), SB_HORZ, true ); } else { EnableScrollBar( getHwnd(), SB_HORZ, false ); ShowScrollBar( getHwnd(), SB_HORZ, false ); } if( visibleHeight() < m_clntSize.height() ) { EnableScrollBar( getHwnd(), SB_VERT, true ); SetScrollRange( getHwnd(), SB_VERT, 0, m_clntSize.height()-visibleHeight() ); SetScrollPos( getHwnd(), SB_VERT, 0 ); ShowScrollBar( getHwnd(), SB_VERT, true ); } else { EnableScrollBar( getHwnd(), SB_VERT, false ); ShowScrollBar( getHwnd(), SB_VERT, false ); } /////////////////////////////////////////////////////////////////////////////// } else { _width = visibleWidth(); }}int MGHTMLView::contentsX() const{ return m_xOffset;}int MGHTMLView::contentsY() const{ return m_yOffset;}void MGHTMLView::paintElement( khtml::RenderObject *o, int xPos, int yPos ) // where invoke it?????{ int yOff = contentsY(); if(yOff > yPos + o->height() || yOff + visibleHeight() < yPos) return; int xOff = contentsX(); o->printObject( m_painter , xOff, yOff, visibleWidth(), visibleHeight(), xPos , yPos );}bool MGHTMLView::createView( int x, int y, int w, int h, bool visible, HWND hParent ){ // must have a parent window static HCURSOR hCur = GetSystemCursor( 0 ); static WNDCLASS wndClass = { 0, 0, 0, 0, hCur, COLOR_lightwhite, HTMLViewProc, 0 }; if( wndClass.spClassName == 0 ) // unregistered { wndClass.spClassName = "miniguibrowser"; if( !RegisterWindowClass( &wndClass ) ) { wndClass.spClassName = 0; return false; } } m_hWnd = CreateWindowEx( wndClass.spClassName, "MiniGUIBrowser", (visible ? WS_VISIBLE : 0) | WS_HSCROLL | WS_VSCROLL, 0, MG::g_nChildWndID++, x, y, w, h, hParent, (DWORD)this ); if (m_hWnd == (HWND)HWND_INVALID ) return false; ShowScrollBar( m_hWnd, SB_HORZ, false ); ShowScrollBar( m_hWnd, SB_VERT, false ); m_hWndParent = hParent; // FIXME: do not construct m_part in view. m_part = new MGHTMLPart (this); m_painter = new MGPainter (m_part); //m_doc = new HTMLDocumentImpl (this); init(); // when init it????? //m_doc->begin (); m_part->begin(); m_doc = m_part->docImpl();// m_doc->attach (this);// m_doc->write (MGString ("<P>Monqueror -- The browser based on MiniGUI.</P>"));// m_doc->write (MGString ("<P>Please choose File->Open to open a HTML file. </P>")); //m_doc->end(); m_part->end(); #ifdef JAVASCRIPT_ENABLE //added for javascript m_JsTimerId = 1; m_JsTimer.setAutoDelete(0); m_JsEventTypeToCapture = 0; m_JsEventType = 0; m_JsKeyEventValue = 0; m_JSRuntime = JS_Init(1000000L); if (!m_JSRuntime) {// fprintf (stderr, "can't create JavaScript runtime"); return FALSE; } m_JSContext = JS_NewContext(m_JSRuntime,8192); if (!m_JSContext) {// fprintf (stderr, "can't create JavaScript context"); return FALSE; } InitialJavaScript(this,&m_JSRuntime,&(m_JSContext),&(m_JSObject));#endif return true;}bool MGHTMLView::ScriptDataProcessed(){ m_doc->myClose(); return 1;} ////////////////////////////// Event Handling ////////////////////////////////////////void MGHTMLView::OnMousePressEvent(int x, int y, DWORD flags){ if(!m_part->docImpl()) return; int xm, ym; xm = x + m_xOffset; ym = y + m_yOffset; DOMString url; NodeImpl *innerNode=0; long offset=0; m_part->docImpl()->mouseEvent( xm, ym, flags, DOM::NodeImpl::MousePress, 0, 0, url, innerNode, offset); d->underMouse = innerNode; #ifdef JAVASCRIPT_ENABLEif(innerNode) { if((m_JsEventTypeToCapture & mgjsDOMEvent::MOUSEDOWN)) { m_JsEventType = mgjsDOMEvent::MOUSEDOWN; m_JsEventPageX = x; m_JsEventPageY = y; EventEvaluateJavaScript(&m_JSContext,&m_JSObject,this); } }#endif// if (innerNode && url.length()) // { // // !!!!! go to link or go to archor// gotoLink (innerNode);// } m_part->khtmlMousePressEvent(xm, ym, innerNode, url); // !!!!! modify it to give paramater.}void MGHTMLView::OnMouseDoubleClickEvent(int x, int y, DWORD flags){ if(!m_part->docImpl()) return; int xm, ym; xm = x + m_xOffset; ym = y + m_yOffset; DOMString url; NodeImpl *innerNode=0; long offset=0; m_part->docImpl()->mouseEvent( xm, ym, flags, DOM::NodeImpl::MousePress, 0, 0, url, innerNode, offset); //m_part->khtmlMouseDoubleClickEvent(xm, ym, innerNode, url); // !!!!! modify it to give paramater. // now when double click, do nothing.}void MGHTMLView::OnMouseMoveEvent(int x, int y, DWORD flags){ if(!m_part->docImpl()) return; int xm, ym; xm = x + m_xOffset; ym = y + m_yOffset; DOMString url; NodeImpl *innerNode=0; long offset=0; m_part->docImpl()->mouseEvent( xm, ym, flags, DOM::NodeImpl::MousePress, 0, 0, url, innerNode, offset);#ifdef JAVASCRIPT_ENABLE // execute the scheduled script. This is to make sure the mouseover events come after the mouseout events // when implement JS, do it. m_part->executeScheduledScript();#endif d->underMouse = innerNode; HCURSOR c = GetSystemCursor (IDC_ARROW); if ( innerNode ) { switch( innerNode->style()->cursor() ) { case CURSOR_AUTO: if ( url.length() && const_cast<MGHTMLSettings *>(m_part->settings())->changeCursor() ) c = m_handCursor; break; case CURSOR_CROSS: c = GetSystemCursor (IDC_CROSS); break; case CURSOR_POINTER: c = m_handCursor; break; case CURSOR_MOVE: c = GetSystemCursor (IDC_MOVE); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -