Monday, July 21, 2014

C++ - Custom Tooltip

Custom tool tip C++ class implements multiline and multicolor tool tip, which can be used to present different information, in several lines, using various colors:


CPP and H file can be reviewed and downloaded from following locations: source and header. The class is MFC dependent. Use of the class is simple:




void CCustomChartControl::OnMouseMove(UINT nFlags, CPoint point)
{
  COLORREF color1, color2, color3;
  CString strText1(_T("")), strText2(_T("")), strText3(_T(""));

  // initialize colors and texts
  // ...

  if (m_pWndToolTip == NULL)
  {
     m_pWndToolTip = new CCustomToolTip();
   
     if (!m_pWndToolTip->Create(this))
     {
         TRACE(_T("Couldn't create tool tip window!"));    
     }
     else
     {
         m_pWndToolTip->ShowToolTip(TRUE);
     }
  }

  if (m_pWndToolTip != NULL)
  {        
     m_pWndToolTip->UpdateText(strText1, strText2, strText3,
         true, true, true,
         true, true, true,
         color1, color2, color3,
         point);   
  }
}
In method CCustomToolTip::UpdateText you can set texts with three different colors and visibility types (visible/not visible).

Thursday, June 19, 2014

Android App - Zoromatic Flashlight

Zoromatic Flashlight for Android is simple application, which uses device's camera flash, to provide flashlight at any time. It also provides additional strobe feature, with frequencies from 0 to 20 Hz.



Application can be downloaded from following location: https://play.google.com/store/apps/details?id=com.zoromatic.flashlight.

Sunday, October 6, 2013

C++ - Bitap algorithm for Levenshtein distance calculation

In information theory and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertion, deletion, substitution) required to change one word into the other.

The bitap algorithm (also known as the shift-or, shift-and or Baeza-Yates–Gonnet algorithm) is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is "approximately equal" to a given pattern, where approximate equality is defined in terms of Levenshtein distance — if the substring and pattern are within a given distance k of each other, then the algorithm considers them equal.


C++ implementation of bitap algorithm is given below:



#include 
#include 

using namespace std;

#define Diff_Timeout    1.0f
#define Diff_EditCost   4
#define Match_Threshold   0.5f
#define Match_Distance   100
#define Patch_DeleteThreshold 0.5f
#define Patch_Margin    4
#define Match_MaxBits   32

int match_bitap(const wstring &text, const wstring &pattern, int loc) 
{
 if (!(Match_MaxBits == 0 || pattern.length() <= Match_MaxBits)) 
 {
  throw _T("Pattern too long for this application.");
 }

 // Initialize the alphabet.
 map  s = match_alphabet(pattern);

 // Highest score beyond which we give up.
 double score_threshold = Match_Threshold;
 // Is there a nearby exact match? (speedup)
 int best_loc = text.find(pattern, loc);
 
 if (best_loc != -1) 
 {
  score_threshold = min(match_bitapScore(0, best_loc, loc, pattern), score_threshold);
  // What about in the other direction? (speedup)
  best_loc = text.rfind(pattern, loc + pattern.length());
  if (best_loc != -1) {
   score_threshold = min(match_bitapScore(0, best_loc, loc, pattern), score_threshold);
  }
 }

 // Initialize the bit arrays.
 int matchmask = 1 << (pattern.length() - 1);
 best_loc = -1;

 int bin_min, bin_mid;
 int bin_max = pattern.length() + text.length();
 int *rd;
 int *last_rd = NULL;
 
 for (int d = 0; d < pattern.length(); d++) 
 {
  // Scan for the best match; each iteration allows for one more error.
  // Run a binary search to determine how far from 'loc' we can stray at
  // this error level.
  bin_min = 0;
  bin_mid = bin_max;
  
  while (bin_min < bin_mid) 
  {
   if (match_bitapScore(d, loc + bin_mid, loc, pattern) <= score_threshold) 
   {
     bin_min = bin_mid;
   } 
   else 
   {
    bin_max = bin_mid;
   }
   
   bin_mid = (bin_max - bin_min) / 2 + bin_min;
  }
  
  // Use the result from this iteration as the maximum for the next.
  bin_max = bin_mid;
  int start = max(1, loc - bin_mid + 1);
  int finish = min(loc + bin_mid, text.length()) + pattern.length();

  rd = new int[finish + 2];
  rd[finish + 1] = (1 << d) - 1;
  
  for (int j = finish; j >= start; j--) 
  {
   int charMatch;
   
   if (text.length() <= j - 1) 
   {
    // Out of range.
    charMatch = 0;
   } 
   else 
   {
    charMatch = s[text[j - 1]];
   }

   if (d == 0) 
   {
    // First pass: exact match.
    rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
   } 
   else 
   {
    // Subsequent passes: fuzzy match.
    rd[j] = ((rd[j + 1] << 1) | 1) & charMatch
     | (((last_rd[j + 1] | last_rd[j]) << 1) | 1)
     | last_rd[j + 1];
   }
   
   if ((rd[j] & matchmask) != 0) 
   {
    double score = match_bitapScore(d, j - 1, loc, pattern);
    // This match will almost certainly be better than any existing
    // match.  But check anyway.
    if (score <= score_threshold) 
    {
     // Told you so.
     score_threshold = score;
     best_loc = j - 1;
     if (best_loc > loc) 
     {
      // When passing loc, don't exceed our current distance from loc.
      start = max(1, 2 * loc - best_loc);
     } 
     else 
     {
      // Already passed loc, downhill from here on in.
      break;
     }
    }
   }
  }

  if (match_bitapScore(d + 1, loc, loc, pattern) > score_threshold) 
  {
   // No hope for a (better) match at greater error levels.
   break;
  }
  
  delete [] last_rd;
  last_rd = rd;
 }

 delete [] last_rd;
 delete [] rd;
 return best_loc;
}


double match_bitapScore(int e, int x, int loc, const wstring &pattern) 
{
 const float accuracy = static_cast (e) / pattern.length();
 const int proximity = abs(loc - x);
 
 if (Match_Distance == 0) 
 {
  // Dodge divide by zero error.
  return proximity == 0 ? accuracy : 1.0;
 }
 
 return accuracy + (proximity / static_cast (Match_Distance));
}


map  match_alphabet(const wstring &pattern) 
{
 map  s;
 int i;
 
 for (i = 0; i < pattern.length(); i++) 
 {
  TCHAR c = pattern[i];
  s[c] = 0;
 }
 
 for (i = 0; i < pattern.length(); i++) 
 {
  TCHAR c = pattern[i];
  s[c] = (s[c] | (1 << (pattern.length() - i - 1)));
 }

 return s;
}

The call to procedure is as follows:

int res = match_bitap(stringOne, stringTwo, 0);

where stringOne and stringTwo are strings whose match (Levenshtein distance) is to be determined, and 0 is location in stringOne where matching procedure should start. Return value is Levenshtein distance, as described above.

Thursday, June 20, 2013

HTML - Boxes around text within HTML table cell


If you want to have boxes only around some text within larger HTML container (td, div), use following code:


<span style="display:inline-block;border:1px solid #000;">
Your text goes here...
</span>


If you apply code to text within <td> element of a table, you'll get result like this:



Thursday, February 21, 2013

Android App - Zoromatic ScreenLock

Zoromatic ScreenLock is standalone Android application which allows user to lock device's screen, with one single click. This is not a widget, so it does not resides in memory, it turns itself off after lock.



Settings screen allows user to provide administration rights to the application:



Before uninstall, ScreenLock Admin rights should be disabled in Settings.

ScreenLock works on Android 2.2 or newer. The newest version can be downloaded from here.

Friday, February 8, 2013

Android App - Zoromatic Widgets

Zoromatic Widgets are set of multifunctional widgets for Android 2.2 and higher. To add any of available widgets to your home screen, long tap the screen, select Widgets from menu, and then select Zoromatic Widgets.

Available widgets:
  • Weather and Clock widget
  • Battery Status
  • Power Widget (multiple widgets)
  • Toggle WiFi
  • Toggle Mobile Data
  • Toggle Ringer
  • Toggle GPS
  • Toggle Bluetooth
  • Toggle Airplane Mode
  • Toggle NFC
  • Toggle Auto Sync
  • Set Screen Brightness



Weather and Clock widget is configurable, each widget has its own settings. Some of available custom options for clock piece of main widget are:
  • Clock color
  • Clock font
  • Clock skin
  • Widget transparency
  • Date format

Weather settings include:
  • Temperature scale (Celsius, Fahrenheit)
  • Custom weather icons
  • Location settings (Current location, using GPS, and custom location)
  • Refresh interval (30 minutes to 6 hours)
  • Use WiFi only for scheduled refresh

                     
There is weather forecast available for each weather widget, for selected locations



Application also provides battery status notification in status bar, theme and language selection:



Widgets work on Android 2.2 and newer. The newest version can be downloaded here.

Windows App - MiniPad - Syntax Highlighting Text and Source Code Editor

MiniPad is fully equipped text and source code editor, with syntax highlighting feature. It offers following main functionalities:

  • Work with both ASCII and Unicode files;
  • Syntax highlighting support for more than 32 groups of file types, with over 100 types supported in total;
  • Basic editing options (cut, copy, paste, delete, drag and drop blocks);
  • Navigation options (find, replace, bookmarks, go to line);
  • Formatting text (font, text and background colors, lower-upper case transitions);
  • Column selection (experimental);
  • Full screen view;
  • Switch between multiple documents or tabbed interface;
  • Application themes (from Windows 2000 to Office 2007);




Works on Windows XP and higher.

The application is single executable and can be downloaded here.