SOL9 Sample: FindDialog

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL++2000 
// 1999.09.24 Modified
// 2000.02.18

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\ScrolledText.h>
#include <sol\FindDialog.h>
#include <sol\FileDialog.h>
#include "Resource.h"

namespace SOL {

class TextEditor :public ApplicationView {
private:

  UINT    findMsg;
  ScrolledText  text;
  FileDialog  fileDialog;
  FindDialog  findDialog;


public:
  TextEditor(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, args) 
  {
    Args ar;
    ar.set(XmNnoHideSel, True);
    text.create(this, _T(""), ar);
  
    // 1999.09.24
    add(text);

    findMsg = RegisterWindowMessage(FINDMSGSTRING);

    ar.reset();
    ar.set(XmNflags, FR_DOWN);
    findDialog.create(this, NULL, ar);

    ar.reset();
    fileDialog.create(this, NULL, ar);

    addCallback(XmNmenuCallback, ID_OPEN, this,
      (Callback)&TextEditor::open, NULL);
    addCallback(XmNmenuCallback, ID_FIND, this,
      (Callback)&TextEditor::find, NULL);

    addCallback(XmNmenuCallback, ID_EXIT, this,
      (Callback)&TextEditor::exit, NULL);

    addEventHandler(findMsg, this,
      (Handler)&TextEditor::search, NULL);
  }


private:
  long search(Event& event) 
  {
    DWORD flag = findDialog.getFlag();
    TCHAR* find = findDialog.getFindString();
    //MessageBox(NULL, find, _T("debug"), MB_OK);

    if(strlen(find) > 0 && (flag & FR_DOWN)) {
      searchDown();
    }
    else {
      searchUp();
      // FR_UP case is not supported.
      MessageBeep(-1);
    }
    return NULL;
  }

private:
  void searchDown()
  {
    int  lines = text.getLineCount();
    TCHAR* find = findDialog.getFindString();

    int  start, end;
    text.getSel(&start, &end);
    int line = text.lineFromChar(end);
    int pos  = text.lineIndex(line);

    Boolean flag = FALSE;
    int  n = strlen(find);
    for(int i = line; i<lines; i++) {
      int lineLength = text.lineLength(i);
      /*if (lineLength ==0) {
        continue;
      }
      */
      TCHAR* buffer = new TCHAR[lineLength+1];
      int len = text.getLine(i, buffer, lineLength);
  
      buffer[len] = ZERO;
      //MessageBox(NULL, buffer, _T("Debug"), MB_OK);
      int indx = 0;
      if(line == i) {
        indx = end - pos;
      }
      if(len >= n) {
        
        for(int j = indx; j<len; j++) {
          if(strncmp(&buffer[j], find, n) == 0) {
            text.setFocus();
            text.setSel(pos+j, pos+j+n);
            text.scrollCaret();
            flag = TRUE;
            break;
          }
        }
      
      }
      delete [] buffer;
      pos += (len+2);  
      if(flag) {
        break;
      }
     }
  }

private:
  void searchUp()
  {
    int  lines = text.getLineCount();
    //TCHAR buffer[512];
    TCHAR* find = findDialog.getFindString();

    int  start = 0;
    int end   = 0;
    text.getSel(&start, &end);
    int line = text.lineFromChar(start);
    int pos  = text.lineIndex(line);

    Boolean flag = FALSE;
    int  n = strlen(find);
    for(int i = line; i>=0; i--) {
      int lineLength = text.lineLength(i);
      if (lineLength ==0) {
  
        continue;
      }

      TCHAR* buffer = new TCHAR[lineLength+1];
      int len = text.getLine(i, buffer, lineLength);

      //int len = text.getLine(i, buffer, sizeof(buffer));
      buffer[len] = ZERO;

      int indx = len-1;
      if(line == i) {
        indx = start - pos;
      }
      if(len >= n) {
        
        for(int j = indx-1; j>=0; j--) {
          if(strncmp(&buffer[j], find, n) == 0) {
          setText(&buffer[j]);
          text.setFocus();
          text.setSel(pos+j, pos+j+n);
          text.scrollCaret();

          flag = TRUE;
          break;
           }
        }
      
      }
      int lenx = 0;
      if(i>0) {
        lenx = text.getLine(i-1, buffer, lineLength);
        pos -= (lenx+2);
      }

      delete [] buffer;

      if(flag) {
        break;
      }
     }
  }

private:
  void find(Action& action)
  {
    if(!findDialog.isWindow()) {
      findDialog.find(_T(""));
    }
  }

private:

  void open(Action& action)
  {
    if(fileDialog.open()) {
      TCHAR* name = fileDialog.getFileName();
      text.load(name);
    }
  }
};

}


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

    Args args;
    TextEditor editor(applet, name, args);
    editor.realize();
    applet.run();

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


Last modified: 1 Feb 2017

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