+ wxCOMPtr<IHTMLDocument2> document(GetDocument());
+
+ if(document)
+ {
+ document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
+ }
+}
+
+wxCOMPtr<IHTMLDocument2> wxWebViewIE::GetDocument() const
+{
+ wxCOMPtr<IDispatch> dispatch;
+ wxCOMPtr<IHTMLDocument2> document;
+ HRESULT result = m_webBrowser->get_Document(&dispatch);
+ if(dispatch && SUCCEEDED(result))
+ {
+ //document is set to null automatically if the interface isn't supported
+ dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&document);
+ }
+ return document;
+}
+
+bool wxWebViewIE::IsElementVisible(IHTMLElement* elm)
+{
+ wxIHTMLCurrentStyle* style;
+ IHTMLElement *elm1 = elm;
+ wxIHTMLElement2 *elm2;
+ BSTR tmp_bstr;
+ bool is_visible = true;
+ //This method is not perfect but it does discover most of the hidden elements.
+ //so if a better solution is found, then please do improve.
+ while(elm1)
+ {
+ if(SUCCEEDED(elm1->QueryInterface(wxIID_IHTMLElement2, (void**) &elm2)))
+ {
+ if(SUCCEEDED(elm2->get_currentStyle(&style)))
+ {
+ //Check if the object has the style display:none.
+ if((style->get_display(&tmp_bstr) != S_OK) ||
+ (tmp_bstr != NULL && (_wcsicmp(tmp_bstr, L"none") == 0)))
+ {
+ is_visible = false;
+ }
+ //Check if the object has the style visibility:hidden.
+ if((is_visible && (style->get_visibility(&tmp_bstr) != S_OK)) ||
+ (tmp_bstr != NULL && _wcsicmp(tmp_bstr, L"hidden") == 0))
+ {
+ is_visible = false;
+ }
+ style->Release();
+ }
+ elm2->Release();
+ }
+
+ //Lets check the object's parent element.
+ IHTMLElement* parent;
+ if(is_visible && SUCCEEDED(elm1->get_parentElement(&parent)))
+ {
+ elm1->Release();
+ elm1 = parent;
+ }
+ else
+ {
+ elm1->Release();
+ break;
+ }
+ }
+ return is_visible;
+}
+
+void wxWebViewIE::FindInternal(const wxString& text, int flags, int internal_flag)
+{
+ wxIMarkupServices *pIMS;
+ wxIMarkupContainer *pIMC;
+ wxIMarkupPointer *ptrBegin, *ptrEnd;
+ IHTMLElement* elm;
+ long find_flag = 0;
+ IHTMLDocument2 *document = GetDocument();
+ //This function does the acutal work.
+ if(SUCCEEDED(document->QueryInterface(wxIID_IMarkupServices, (void **)&pIMS)))
+ {
+ if(SUCCEEDED(document->QueryInterface(wxIID_IMarkupContainer, (void **)&pIMC)))
+ {
+ BSTR attr_bstr = SysAllocString(L"style=\"background-color:#ffff00\"");
+ BSTR text_bstr = SysAllocString(text.wc_str());
+ pIMS->CreateMarkupPointer(&ptrBegin);
+ pIMS->CreateMarkupPointer(&ptrEnd);
+
+ ptrBegin->SetGravity(wxPOINTER_GRAVITY_Right);
+ ptrBegin->MoveToContainer(pIMC, TRUE);
+ //Create the find flag from the wx one.
+ if(flags & wxWEBVIEW_FIND_ENTIRE_WORD)
+ {
+ find_flag |= wxFINDTEXT_WHOLEWORD;
+ }
+ if(flags & wxWEBVIEW_FIND_MATCH_CASE)
+ {
+ find_flag |= wxFINDTEXT_MATCHCASE;
+ }
+
+ //A little speed-up to avoid to re-alloc in the positions vector.
+ if(text.Len() < 3 && m_findPointers.capacity() < 500)
+ {
+ m_findPointers.reserve(text.Len() == 1 ? 1000 : 500);
+ }
+
+ while(ptrBegin->FindText(text_bstr, find_flag, ptrEnd, NULL) == S_OK)
+ {
+ if(ptrBegin->CurrentScope(&elm) == S_OK)
+ {
+ if(IsElementVisible(elm))
+ {
+ //Highlight the word if the flag was set.
+ if(flags & wxWEBVIEW_FIND_HIGHLIGHT_RESULT)
+ {
+ IHTMLElement* pFontEl;
+ pIMS->CreateElement(wxTAGID_FONT, attr_bstr, &pFontEl);
+ pIMS->InsertElement(pFontEl, ptrBegin, ptrEnd);
+ }
+ if(internal_flag & wxWEBVIEW_FIND_REMOVE_HIGHLIGHT)
+ {
+ IHTMLElement* pFontEl;
+ ptrBegin->CurrentScope(&pFontEl);
+ pIMS->RemoveElement(pFontEl);
+ pFontEl->Release();
+ }
+ if(internal_flag & wxWEBVIEW_FIND_ADD_POINTERS)
+ {
+ wxIMarkupPointer *cptrBegin, *cptrEnd;
+ pIMS->CreateMarkupPointer(&cptrBegin);
+ pIMS->CreateMarkupPointer(&cptrEnd);
+ cptrBegin->MoveToPointer(ptrBegin);
+ cptrEnd->MoveToPointer(ptrEnd);
+ m_findPointers.push_back(wxFindPointers(cptrBegin,cptrEnd));
+ }
+ }
+ elm->Release();
+ }
+ ptrBegin->MoveToPointer(ptrEnd);
+ }
+ //Clean up.
+ SysFreeString(text_bstr);
+ SysFreeString(attr_bstr);
+ pIMC->Release();
+ ptrBegin->Release();
+ ptrEnd->Release();
+ }
+ pIMS->Release();
+ }
+ document->Release();
+}
+
+long wxWebViewIE::FindNext(int direction)
+{
+ //Don't bother if we have no pointers set.
+ if(m_findPointers.empty())
+ {
+ return wxNOT_FOUND;
+ }
+ //Manage the find position and do some checks.
+ if(direction > 0)
+ {
+ m_findPosition++;
+ }
+ else
+ {
+ m_findPosition--;
+ }
+
+ if(m_findPosition >= (signed)m_findPointers.size())
+ {
+ if(m_findFlags & wxWEBVIEW_FIND_WRAP)
+ {
+ m_findPosition = 0;
+ }
+ else
+ {
+ m_findPosition--;
+ return wxNOT_FOUND;
+ }
+ }
+ else if(m_findPosition < 0)
+ {
+ if(m_findFlags & wxWEBVIEW_FIND_WRAP)
+ {
+ m_findPosition = m_findPointers.size()-1;
+ }
+ else
+ {
+ m_findPosition++;
+ return wxNOT_FOUND;
+ }
+ }
+ //some variables to use later on.
+ IHTMLElement *body_element;
+ IHTMLBodyElement *body;
+ wxIHTMLTxtRange *range = NULL;
+ wxIMarkupServices *pIMS;
+ IHTMLDocument2 *document = GetDocument();
+ long ret = -1;
+ //Now try to create a range from the body.
+ if(SUCCEEDED(document->get_body(&body_element)))
+ {
+ if(SUCCEEDED(body_element->QueryInterface(IID_IHTMLBodyElement,(void**)&body)))
+ {
+ if(SUCCEEDED(body->createTextRange((IHTMLTxtRange**)(&range))))
+ {
+ //So far so good, now we try to position our find pointers.
+ if(SUCCEEDED(document->QueryInterface(wxIID_IMarkupServices,(void **)&pIMS)))
+ {
+ wxIMarkupPointer *begin = m_findPointers[m_findPosition].begin, *end = m_findPointers[m_findPosition].end;
+ if(pIMS->MoveRangeToPointers(begin,end,range) == S_OK && range->select() == S_OK)
+ {
+ ret = m_findPosition;
+ }
+ pIMS->Release();
+ }
+ range->Release();
+ }
+ body->Release();
+ }
+ body_element->Release();
+ }