SOL9 Sample: StreamBrowser

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9  
// 2012/02/10
// Sample program to stream-in a string stream in a richTextEditor to a webBrowser
// by using a loadString method of SOL::HTMLView class.

#define COMMONCONTROLS_V6

#include <sol/ApplicationView.h>
#include <sol/SplitPane.h>

#include <sol/ole/HTMLView.h>
#include <sol/ScrolledRichText.h>

#include <sol/PushButton.h>

#include <sol/FileVersionDialog.h>
#include <sol/FileDialog.h>
#include <sol/Stdio.h>
#include <sol/ScrolledText.h>

#include "resource.h"

#define WM_STREAMIN  (WM_USER+2012)

namespace SOL {

/**
 * class StreamBrowser
 */
class StreamBrowser :public ApplicationView {
private:
  PushButton       pushButton;
  SplitPane    splitPane;
  ScrolledRichText richText;
  HTMLView htmlView;

  FileVersionDialog fileVersion;
  FileDialog        fileDialog;

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

    Args ar;
    ar.reset();
    ar.set(XmNwidth, 100);
    ar.set(XmNheight, 24);
    pushButton.create(this, _T("StreamIn"), ar);
    pushButton.addCallback(XmNactivateCallback, this, (Callback)&StreamBrowser::streamIn, NULL);
    
    ar.reset();

    ar.set(XmNdirection, SplitPane::HORIZONTAL);
    splitPane.create(this, NULL, ar);
    add(splitPane);

    ar.reset();
    ar.set(XmNstyle, WS_CHILD|WS_VISIBLE);
    ar.set(XmNexStyle, (ulong)WS_EX_ACCEPTFILES);
    richText.create(&splitPane, _T(""), ar);
  
    richText.setEventMask(ENM_DROPFILES);
    richText.addCallback(XmNdropFilesCallback, this,
      (Callback)&StreamBrowser::dropped, NULL);

    //Add the richText to the splitPane
    splitPane.add(&richText);

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

    //Add the htmlView to the splitPane
    splitPane.add(&htmlView);

    splitPane.setSashPosition(260);

    //Create a fileVersionDialog
    fileVersion.create(this);

    addCallback(XmNmenuCallback, IDM_NEW, this, (Callback)&StreamBrowser::clear, NULL);
    addCallback(XmNmenuCallback, IDM_FILEOPEN, this, (Callback)&StreamBrowser::openText, NULL);
    addCallback(XmNmenuCallback, IDM_FILESAVE, this, (Callback)&StreamBrowser::saveText, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, (Callback)&StreamBrowser::exit, NULL);
    addCallback(XmNmenuCallback, IDM_VERSION, this, (Callback)&StreamBrowser::version, NULL);

    addEventHandler(WM_SIZE, this, (Handler)&StreamBrowser::size, NULL);
    addEventHandler(WM_CLOSE, this, (Handler)&StreamBrowser::close, NULL);

    //Create a fileSelectionDialog
    ar.reset();
    fileDialog.create(this, NULL, ar);

    restorePlacement();
  }

private:
  void setCaption(const TCHAR* filename)
  {
    if(filename) {
      TCHAR text[MAX_PATH];
      _stprintf_s(text, CountOf(text), _T("%s - StreamBrowser"), filename);
      setText(text);
    }    
  }


public:
  void dropped(Action& action)
  {
    Event& event = action.getEvent();
    ENDROPFILES* endrop = (ENDROPFILES*)event.getLParam();
    HDROP hdrop = (HDROP)endrop->hDrop;
    if (hdrop) {
      TCHAR filename[_MAX_PATH];
      DragQueryFile(hdrop, 0, filename, CountOf(filename));
      File file;
      if (file.isUnicodeFile(filename)) {
        richText.streamIn(filename, SF_TEXT|SF_UNICODE);
      } else {
        richText.streamIn(filename, SF_TEXT);
      }

      TCHAR* html= richText.getText();
      htmlView.loadString(html);
      setCaption(filename);
    }
  }

public:
  void clear(Action& action)
  {
    richText.setText("");

    htmlView.navigate(_T("about:blank"));
  }

public:
  void streamIn(Action& action)
  {
    TCHAR* html= richText.getText();
    htmlView.loadString(html);
  }

public:
  void openText(Action& action)
  {
    TCHAR dir[_MAX_PATH];
    if (restoreFileFolder(dir, CountOf(dir))) {
      Args ar;
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }

    fileDialog.setCaption(_T("Open as Text"));
    if(fileDialog.open()) {
      TCHAR* filename  = fileDialog.getFileName();
      File file;
      if (file.isUnicodeFile(filename)) {
        richText.streamIn(filename, SF_TEXT|SF_UNICODE);
      } else {
        richText.streamIn(filename, SF_TEXT);
      }
      setCaption(filename);
      saveFileFolder(filename);
      
      TCHAR* html= richText.getText();
      htmlView.loadString(html);
    }
  }

public:
  void saveText(Action& action)
  {
    TCHAR dir[_MAX_PATH];
    if (restoreFileFolder(dir, CountOf(dir))) {
      Args ar;
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }

    fileDialog.setCaption(_T("Sava as Text"));
    if(fileDialog.save()) {
      TCHAR* filename  = fileDialog.getFileName();
      richText.streamOut(filename, SF_TEXT);
      setCaption(filename);
      saveFileFolder(filename);
    }
  }

public:
  void version(Action& action)
  {
    fileVersion.popupAt(action);
  }

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

    int h = 30;
    pushButton.reshape(3, 3,100, h-6);
    splitPane.reshape(0, h,  LOWORD(l), HIWORD(l)-h);
    return 0;
  }

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

}

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

  try {

    Application applet(name, argc, argv);

    Args args;
    args.set(XmNstyle, WS_CLIPCHILDREN);
    args.set(XmNclassStyle, CS_DBLCLKS);
    StreamBrowser browser(applet, name, args);
    browser.realize();

    applet.run();

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


Last modified: 1 Feb 2017

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