SOL9 Sample: ToolTips

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2012/06/29
// 2012/07/20 Updated open method to use the format parameter when you call RichText::streamIn method.

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol/FileDialog.h>
#include <sol/ScrolledRichText.h>
#include <sol/ImageButton.h>
#include <sol/ToolTips.h>

#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {

private:
  ScrolledRichText text;

  FileDialog fileDialog;
  
  ImageButton openButton;
  ImageButton saveAsButton;
  
  ToolTips tipsOpen;
  ToolTips tipsSaveAs;

public:
  /**
   * Constructor
   */
  AppView(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name,
    args.set(XmNstyle, (WS_CLIPCHILDREN|WS_CLIPSIBLINGS) )
    .set(XmNexStyle, WS_EX_CONTROLPARENT))
  {
    ClientDC dc(this);

    int th = dc.getTextHeight();

    Args ar;
    //Create scrolledRichText 
    text.create(this, NULL, ar);
    
    //Create openButton
    ar.reset();
    ar.set(XmNx,      2);
    ar.set(XmNy,      2);
    ar.set(XmNwidth,  38);
    ar.set(XmNheight, 38);
    ar.set(XmNiconResourceName, (const TCHAR*)IDI_OPEN);
    ar.set(XmNscaling, ImageButton::StretchImage);
    ar.set(XmNcursor, (ulong)LoadCursor(NULL, IDC_HAND));
    openButton.create(this, NULL, ar);
    
    //Create saveAsButton
    ar.reset();
    ar.set(XmNx,      2+44+2);
    ar.set(XmNy,      2);
    ar.set(XmNwidth,  38);
    ar.set(XmNheight, 38);
    ar.set(XmNiconResourceName, (const TCHAR*)IDI_SAVEAS);
    ar.set(XmNscaling, ImageButton::StretchImage);
    ar.set(XmNcursor, (ulong)LoadCursor(NULL, IDC_HAND));
    saveAsButton.create(this, NULL, ar);

    //AddCallback to openButton
    openButton.addCallback(XmNactivateCallback, this,
      (Callback)&AppView::open, NULL);

    //AddCallback to saveAsButton
    saveAsButton.addCallback(XmNactivateCallback, this,
      (Callback)&AppView::saveAs, NULL);

    //Create tipsOpen 
    ar.reset();
    ar.set(XmNexStyle, WS_EX_TOPMOST);
    ar.set(XmNstyle, TTS_NOPREFIX | TTS_ALWAYSTIP);
    tipsOpen.create(this, NULL, ar);
    tipsOpen.addTool(&openButton, _T("Open a file"), TTF_SUBCLASS);

    //Create tipsSaveAs
    ar.reset();
    ar.set(XmNexStyle, WS_EX_TOPMOST);
    ar.set(XmNstyle, TTS_NOPREFIX | TTS_ALWAYSTIP);
    tipsSaveAs.create(this, NULL, ar);
    tipsSaveAs.addTool(&saveAsButton, _T("Save as a file"), TTF_SUBCLASS);

    //Add eventHandlers.
    addEventHandler(WM_SIZE, this, 
      (Handler)&AppView::size, NULL);

    addEventHandler(WM_CLOSE, this, 
      (Handler)&AppView::close, NULL);

    //Create a fileDialog
    ar.reset();
    ar.set(XmNfilter, 
      _T("Text File(*.txt,*.rtf)\0*.txt;*.rtff\0Text File*.txt)\0*.txt\0RTF File(*.rtf)\0*.rtf\0"));

    fileDialog.create(this, _T(""), ar);
    
    //Add menucallbacks.
    addCallback(XmNmenuCallback, ID_NEW, this,
      (Callback)&AppView::clear, NULL);
    addCallback(XmNmenuCallback, ID_OPEN, this,
      (Callback)&AppView::open, NULL);
    addCallback(XmNmenuCallback, ID_SAVEAS, this,
      (Callback)&AppView::saveAs, NULL);
    addCallback(XmNmenuCallback, ID_EXIT, this,
      (Callback)&AppView::exit, NULL);

    addCallback(XmNmenuCallback, ID_VERSION, this,
      (Callback)&AppView::version, NULL);

    restorePlacement();
  }

public:
  ~AppView()
  {
  }


private:
  long size(Event& event)
  {
    RECT r;
    getClientRect(&r);
    int w = r.right - r.left;
    int h = r.bottom - r.top;

    text.reshape(2, 44, w-4, h-44-2);
    return 0;
  }

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

private:
  void setCaption(const TCHAR* fileName)
  {
    TCHAR title[1024];
    _stprintf_s(title, CountOf(title), _T("%s - AppView"), fileName);
    setText(title);
  }

private:
  void  clear(Action& action) 
  {
    int len = text.getTextLength();
    if (len>0) {
      if (showMessageDialog(_T("Are you sure to clean current text in RichText Area?"),
        MB_OKCANCEL|MB_ICONQUESTION) ==IDOK) {
        text.clear();
      }
    }
  }

private:
  void  open(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.open()){
      TCHAR* filename = fileDialog.getFileName();

      File file;
      if (file.isExistent(filename)) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);
        String fileName = filename;
        UINT format = SF_TEXT; //Default format
        if (fileName.endsWithIgnoreCase(_T(".txt"))) {
          format = SF_TEXT;
        }
        if (fileName.endsWithIgnoreCase(_T(".rtf"))) {
          format = SF_RTF;
        }
        BOOL rc = text.streamIn(filename, format);// 2012/07/20
        if (rc) {
            setCaption(filename);
          } else {
            TCHAR msg[1024];
            _stprintf_s(msg, CountOf(msg), _T("Failed to open a file '%s'"), filename);
            showMessageDialog( msg, MB_OK|MB_ICONERROR);
          }
      }
    }  
  }

private:
  void  saveAs(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      TCHAR textFileName[1024];
      _stprintf_s(textFileName, CountOf(textFileName), _T("_%s_.txt"), _T("sample"));
      ar.set(XmNfileName, textFileName);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.save()){
      TCHAR* filename = fileDialog.getFileName();

      File file;
      if (file.isExistent(filename) == false) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);
          
        TCHAR msg[1024];
        _stprintf_s(msg, CountOf(msg), _T("Failed to save a file '%s'"), filename);
        
        String file = filename;
        UINT format = SF_TEXT; //Default format.
        if (file.endsWithIgnoreCase(_T(".txt")) ) {
          format = SF_TEXT;
        }
        if (file.endsWithIgnoreCase(_T(".rtf")) ) {
          format = SF_RTF;
        }

        BOOL rc = text.streamOut(filename, format);
        if (rc) {
          setCaption(filename);
        } else {
          showMessageDialog( msg, MB_OK|MB_ICONERROR);
        }
      }
    }  
  }

private:
  void  exit(Action& action) 
  {
    if (showMessageDialog(_T("Are you sure to exit this application?"),
        MB_OKCANCEL|MB_ICONQUESTION) ==IDOK) {
        ApplicationView::exit(action);
    }
  }

private:
  void version(Action& action)
  {    
    showMessageDialog(_T("AppView"), 
      _T("Sample for ToolTips and RichText Copyright (c) 2012 Antillia.com"), MB_OK|MB_ICONINFORMATION);

  }
};

}

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

  try {
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNbackground, (COLOR_BTNFACE +1));
    AppView appView(applet, name, args);
    appView.realize();

    applet.run();

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


Last modified: 1 Feb 2017

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