SOL9 Sample: SolWebBrowser

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolWebBrowser.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



// SOL++2000 
// 2008/08/10
// 2008/09/16 Added an event handler for WM_BROWSER_DOCUMENTCOMPLETE from SOL::BrowserEvent class.
// 2009/10/04 Modified a value of DEFAULT_WIN_CLASS_STYLE of class Application in Main function 
//in the follwing way to avoid a window redrawing.
//
//   Application::DEFAULT_WIN_CLASS_STYLE = CS_DBLCLKS;

//2011/02/23 Updated to enable TAB key in HTMLView.
//2011/09/04 Fixed a bug in selChanged method. 
//2012/02/20 Update to comment out the following line for SolWebBrowser in Main.
//  args.set(XmNexStyle, WS_EX_CONTROLPARENT|WS_EX_TRANSPARENT);
//
#include <sol\ApplicationView.h>
#include <sol\ole\HTMLView.h>
#include <sol\ExtendedCombobox.h>
#include <sol/Stdio.h>
#include <sol/Form.h>

#include "resource.h"

namespace SOL {

/**
 * class SolWebBrowser
 */
class SolWebBrowser :public ApplicationView {
private:
  Form form;
  HTMLView htmlView;
  ExtendedComboBox comboBox;

public:
  /**
   * Constructor
   */
  SolWebBrowser(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1))  )
  {

    Args ar;
    ar.set(XmNstyle, (ulong)CBS_DROPDOWN);
    ar.set(XmNheight, 100);
    comboBox.create(this, _T(""), ar);
    comboBox.addCallback(XmNselChangeCallback, this, (Callback)&SolWebBrowser::selChanged, NULL);

    ar.reset();
    
    ar.set(XmNstyle, WS_CHILD|WS_VISIBLE);
    htmlView.create(this, _T(""), ar);

    //2011/02/23
    addEventHandler(WM_KEYDOWN, this, (Handler)&SolWebBrowser::keyDown, NULL);

    addCallback(XmNmenuCallback, IDM_EXIT, this, (Callback)&SolWebBrowser::exit, NULL);

    addEventHandler(WM_SIZE, this, (Handler)&SolWebBrowser::size, NULL);
    addEventHandler(WM_CLOSE, this, (Handler)&SolWebBrowser::close, NULL);
    //208/09/16
    addEventHandler(WM_BROWSER_DOCUMENTCOMPLETE, this, (Handler)&SolWebBrowser::complete, NULL);

    addEventHandler(WM_NOTIFY, this,(Handler)&SolWebBrowser::notify, NULL);

    restorePlacement();
  }

private:
  /**
   * CBN_SELCHANGE callback.
   * Get a url (current selection) of comboBox and navigate to the url.
   */
  //2008/09/16
  void selChanged(Action& action) {
    COMBOBOXEXITEM ci;
    memset(&ci, 0, sizeof(ci));
    TCHAR url[1024];
    memset(url, (TCHAR)0, CountOf(url)); // 2011/09/04 Modifed to use CountOf instead of sizeof;

    ci.mask = CBEIF_TEXT;
    ci.pszText = url;
    ci.cchTextMax = CountOf(url);    // 2011/09/04 Modified to use CountOf instead of sizeof;
    ci.iItem = -1;
    
    comboBox.send(CBEM_GETITEM, 0, (LPARAM)&ci);
    htmlView.navigate(url);
    TCHAR caption[1024];
    _stprintf_s(caption, CountOf(caption), _T("%s - SolWebBrowser"), (const TCHAR*)url);
    setText(caption);
  }

public:
  /**
   * WM_KEYDOWN event handler.
   */
  //2011/02/23 Added
  long keyDown(Event& event)
  {
    MSG msg;
    msg.message = event.getMessage();
    msg.lParam = event.getLParam();
    msg.wParam = event.getWParam();

    //Call HTMLView::transateAccelator
    htmlView.translateAccelator(&msg);
    return 0;
  }

public:
  /**
   * WM_SIZE event handler.
   */
  long size(Event& event)
  {
    LPARAM l = event.getLParam();

    int w=0;
    int h=30;
    comboBox.getSize(w, h);

    comboBox.reshape(0, 0, LOWORD(l), h);
    htmlView.reshape(0, h, LOWORD(l), HIWORD(l)-h);

    return 0;
  }

public:
  /**
   * Event handler for a user-defined WM_BROWSER_DOCUMENTCOMPLETE from SOL::BrowserEvent class.
   *
   */
  //2008/09/16
  long complete(Event& event)
  {
    //Implement your code to handle DISPID_COMPLETE
    String url = "";
    htmlView.getLocationURL(url);
    const TCHAR* curl = (const TCHAR*)url;
    
    if (curl && !(_stricmp(curl, _T("about:blank")) ==0 ||
      _stricmp(curl, _T("res://ieframe.dll/navcancl.htm")) ==0) ) {    
      comboBox.setText(curl);

      int e = comboBox.send(CB_FINDSTRINGEXACT, -1, (LPARAM)curl);
      if (e == CB_ERR) {
        //If comboBox has no entry for the curl, add it to the comboBox.
        COMBOBOXEXITEM cbei;
        memset(&cbei, 0, sizeof(cbei));
        cbei.mask = CBEIF_TEXT;

        cbei.iItem          = 0;
        cbei.pszText        = (TCHAR*)curl;
        cbei.cchTextMax     = strlen(curl);

        comboBox.send(CBEM_INSERTITEM, 0,(LPARAM)&cbei);
      }
    }
    return 0;
  }

public:
  long notify(Event& event) 
  {
    LPARAM lParam = event.getLParam();
  
    NMCOMBOBOXEX* nm = (NMCOMBOBOXEX*)lParam;
    if (nm->hdr.hwndFrom == comboBox.getWindow() &&
      nm->hdr.code==CBEN_ENDEDIT) {

      NMCBEENDEDIT* nme = (NMCBEENDEDIT*)nm;
      if (nme->iWhy == CBENF_RETURN) {
    
        TCHAR url[1024];
        memset(url, (TCHAR)0, CountOf(url));
        comboBox.getText(url, CountOf(url));
        if (strlen(url)>0) {
          navigate(url);
        }
      }
    }
    return 0;
  }


public:
  long close(Event& event)
  {
    savePlacement();
    return defaultProc(event);
  }

public:
  void navigate(const TCHAR* url) {
    //If you change font size, call zoom method with some level(0,1,2,3,4)
    //htmlView.zoom(2);

    htmlView.navigate(url);
    TCHAR caption[1024];
    
    _stprintf_s(caption, CountOf(caption), _T("%s - SolWebBrowser"), (const TCHAR*)url);
    setText(caption);
    comboBox.setText(url);
  }
};

}

//////////////////////////////////////
// SolWebBrowser.exe url
//
//  Program entry point.
void  Main(int argc, TCHAR** argv)
{
  OleInitialize(NULL);
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();

  try {
    const TCHAR* url= _T("https://www.microsoft.com/"); //http -> https
    if (argc ==2) {
      //2009/10/19
      // If specified a url on command line, use it.
      url = argv[1];
    }

    Application applet(name, argc, argv);

    Args args;
    //2009/10/07
    args.set(XmNstyle, WS_CLIPCHILDREN);
    //2012/02/20
    //args.set(XmNexStyle, WS_EX_CONTROLPARENT|WS_EX_TRANSPARENT);
    args.set(XmNclassStyle, CS_DBLCLKS);

    SolWebBrowser browser(applet, name, args);
    browser.realize();
    browser.navigate(url);

  
    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
  OleUninitialize();
}


Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.