changed: the way how buttons on display tab work

suppose we have "123+34+56" in the first edit, then select
         (bo mouse or a keyboard) "123+34" similar as you would like
         to copy them to the clipboard, then press a button let we say
         "sin" - the result is: "sin(123+34)+56"



git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@343 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2011-03-07 20:36:25 +00:00
parent 6c0a4b110e
commit af2f173a82
3 changed files with 187 additions and 177 deletions

View File

@ -1,8 +1,12 @@
Version 0.9.3 prerelease (2011.02.08):
Version 0.9.3 prerelease (2011.03.07):
* added: German translation made by
Moritz Beleites <morbel at gmx.net>
* added: on display tab: an option how many digits should be grouped
* changed: the way how buttons on display tab work
suppose we have "123+34+56" in the first edit, then select
(bo mouse or a keyboard) "123+34" similar as you would like
to copy them to the clipboard, then press a button let we say
"sin" - the result is: "sin(123+34)+56"
Version 0.9.2 (2010.09.24):
* added: Italian translation made by

View File

@ -56,6 +56,153 @@ void PrintErrorCode()
GetPrgRes()->GetLanguages()->ErrorMessage(last_code) );
}
int CalcBracketsPrev(const char * buf, int start)
{
int last;
int brackets;
last = start;
--start;
while( true )
{
for( ; start>=0 && buf[start]!=';' ; --start )
if( buf[start] != ' ' )
last = start;
if( start <= 0 )
return last;
--start; // skipping a semicolon
brackets = 0;
for( ; start>=0 ; --start )
{
if( buf[start] == '(' )
++brackets;
else
if( buf[start] == ')' )
--brackets;
if( brackets > 0 )
{
last = start;
break;
}
}
}
}
int CalcBracketsNext(const char * buf, int size, int start)
{
int last;
int brackets;
if( start >= size )
return size;
last = start;
while( true )
{
for( ; start<size && buf[start]!=';' ; ++start )
if( buf[start] != ' ' )
last = start;
if( start >= size )
{
if( buf[last] == ';' || buf[last] == ' ' )
return last;
else
return last+1;
}
++start; // skipping a semicolon
brackets = 0;
for( ; start<size ; ++start )
{
if( buf[start] == '(' )
++brackets;
else
if( buf[start] == ')' )
--brackets;
if( brackets < 0 )
{
last = start;
break;
}
}
}
}
void CalcBracketsPos(HWND input, int & start, int & end)
{
DWORD sel_start, sel_end;
SendMessage(input, EM_GETSEL, (WPARAM)&sel_start, (LPARAM)&sel_end);
if( sel_start != sel_end )
{
start = sel_start;
end = sel_end;
return;
}
char * buf = GetPrgRes()->GetBufferTemp();
start = 0;
end = SendMessage(input, WM_GETTEXT, GetPrgRes()->GetBufferSize(), (LPARAM)buf);
if( end > 0 )
{
start = CalcBracketsPrev(buf, sel_start);
end = CalcBracketsNext(buf, end, sel_start);
}
}
void InsertText(const char * first, const char * last, int cursor_back = 0)
{
HWND input = GetDlgItem(GetPrgRes()->GetMainWindow(), IDC_INPUT_EDIT);
int start, end;
CalcBracketsPos(input, start, end);
size_t len_first = strlen(first);
SendMessage(input, EM_SETSEL, start, start);
SendMessage(input, EM_REPLACESEL, 1, LPARAM(first));
SendMessage(input, EM_SETSEL, end+len_first, end+len_first);
SendMessage(input, EM_REPLACESEL, 1, LPARAM(last));
if( start == end )
{
// was empty string - we're setting the cursor inside the texts
SendMessage(input, EM_SETSEL, end+len_first, end+len_first);
}
else
{
size_t len_last = strlen(last);
SendMessage(input, EM_SETSEL, end+len_first+len_last-cursor_back, end+len_first+len_last-cursor_back);
}
SetFocus(input);
}
/*
this functions inserts a given string into the first edit control
and then moves the caret
@ -117,7 +264,7 @@ return true;
BOOL WmTabCommand_Press_sin(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("sin()",-1);
InsertText("sin(", ")");
return true;
}
@ -125,7 +272,7 @@ return true;
BOOL WmTabCommand_Press_cos(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("cos()",-1);
InsertText("cos(", ")");
return true;
}
@ -133,7 +280,7 @@ return true;
BOOL WmTabCommand_Press_tan(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("tan()",-1);
InsertText("tan(", ")");
return true;
}
@ -141,7 +288,7 @@ return true;
BOOL WmTabCommand_Press_cot(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("cot()",-1);
InsertText("cot(", ")");
return true;
}
@ -149,7 +296,7 @@ return true;
BOOL WmTabCommand_Press_ln(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("ln()",-1);
InsertText("ln(", ")");
return true;
}
@ -157,7 +304,7 @@ return true;
BOOL WmTabCommand_Press_log(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("log( ; 2)",-5);
InsertText("log(", " ; )", 1);
return true;
}
@ -165,7 +312,7 @@ return true;
BOOL WmTabCommand_Press_abs(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("abs()",-1);
InsertText("abs(", ")");
return true;
}
@ -173,7 +320,7 @@ return true;
BOOL WmTabCommand_Press_factorial(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("factorial()",-1);
InsertText("factorial(", ")");
return true;
}
@ -251,38 +398,42 @@ return true;
BOOL WmTabCommand_Press_asin(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("asin()",-1);
InsertText("asin(", ")");
return true;
}
BOOL WmTabCommand_Press_acos(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("acos()",-1);
InsertText("acos(", ")");
return true;
}
BOOL WmTabCommand_Press_atan(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("atan()",-1);
InsertText("atan(", ")");
return true;
}
BOOL WmTabCommand_Press_acot(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("acot()",-1);
InsertText("acot(", ")");
return true;
}
BOOL WmTabCommand_Press_sgn(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("sgn()",-1);
InsertText("sgn(", ")");
return true;
}
BOOL WmTabCommand_Press_mod(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("mod(;)",-2);
InsertText("mod(", " ; )", 1);
return true;
}
@ -298,7 +449,7 @@ return true;
BOOL WmTabCommand_Press_int(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("int()",-1);
InsertText("int(", ")");
return true;
}
@ -306,7 +457,7 @@ return true;
BOOL WmTabCommand_Press_round(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("round()",-1);
InsertText("round(", ")");
return true;
}
@ -314,7 +465,7 @@ return true;
BOOL WmTabCommand_Press_exp(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("exp()",-1);
InsertText("exp(", ")");
return true;
}
@ -1051,140 +1202,11 @@ return true;
int CalcBracketsPrev(const char * buf, int start)
{
int last;
int brackets;
last = start;
--start;
while( true )
{
for( ; start>=0 && buf[start]!=';' ; --start )
if( buf[start] != ' ' )
last = start;
if( start <= 0 )
return last;
--start; // skipping a semicolon
brackets = 0;
for( ; start>=0 ; --start )
{
if( buf[start] == '(' )
++brackets;
else
if( buf[start] == ')' )
--brackets;
if( brackets > 0 )
{
last = start;
break;
}
}
}
}
int CalcBracketsNext(const char * buf, int size, int start)
{
int last;
int brackets;
if( start >= size )
return size;
last = start;
while( true )
{
for( ; start<size && buf[start]!=';' ; ++start )
if( buf[start] != ' ' )
last = start;
if( start >= size )
{
if( buf[last] == ';' || buf[last] == ' ' )
return last;
else
return last+1;
}
++start; // skipping a semicolon
brackets = 0;
for( ; start<size ; ++start )
{
if( buf[start] == '(' )
++brackets;
else
if( buf[start] == ')' )
--brackets;
if( brackets < 0 )
{
last = start;
break;
}
}
}
}
void CalcBracketsPos(HWND input, int & start, int & end)
{
DWORD sel_start, sel_end;
SendMessage(input, EM_GETSEL, (WPARAM)&sel_start, (LPARAM)&sel_end);
if( sel_start != sel_end )
{
start = sel_start;
end = sel_end;
return;
}
start = 0;
end = SendMessage(input, EM_LINELENGTH, 0, 0);
if( end > 0 )
{
const char * buf = new char[end+sizeof(WORD)];
*(WORD*)(buf) = end;
SendMessage(input, EM_GETLINE, 0, (LPARAM)buf);
start = CalcBracketsPrev(buf, sel_start);
end = CalcBracketsNext(buf, end, sel_start);
delete [] buf;
}
}
BOOL WmTabCommand_Press_bracketsall(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND main = GetPrgRes()->GetMainWindow();
HWND input = GetDlgItem(main, IDC_INPUT_EDIT);
int start, end;
CalcBracketsPos(input, start, end);
SendMessage(input, EM_SETSEL, start, start);
SendMessage(input, EM_REPLACESEL, 1, LPARAM("("));
SendMessage(input, EM_SETSEL, end+1, end+1);
SendMessage(input, EM_REPLACESEL, 1, LPARAM(")"));
if( start == end )
// was empty string - we're setting the cursor inside the brackets
SendMessage(input, EM_SETSEL, end+1, end+1);
SetFocus(input);
InsertText("(", ")");
return true;
}
@ -1192,23 +1214,7 @@ return true;
BOOL WmTabCommand_Press_1div_bracketsall(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND main = GetPrgRes()->GetMainWindow();
HWND input = GetDlgItem(main, IDC_INPUT_EDIT);
int start, end;
CalcBracketsPos(input, start, end);
SendMessage(input, EM_SETSEL, start, start);
SendMessage(input, EM_REPLACESEL, 1, LPARAM("1/("));
SendMessage(input, EM_SETSEL, end+3, end+3);
SendMessage(input, EM_REPLACESEL, 1, LPARAM(")"));
if( start == end )
// was empty string - we're setting the cursor inside the brackets
SendMessage(input, EM_SETSEL, end+3, end+3);
SetFocus(input);
InsertText("1/(", ")");
return true;
}
@ -1232,7 +1238,7 @@ return true;
BOOL WmTabCommand_Press_floor(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("floor()", -1);
InsertText("floor(", ")");
return true;
}
@ -1240,7 +1246,7 @@ return true;
BOOL WmTabCommand_Press_ceil(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("ceil()", -1);
InsertText("ceil(", ")");
return true;
}
@ -1248,7 +1254,7 @@ return true;
BOOL WmTabCommand_Press_min(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("min()", -1);
InsertText("min(", ")");
return true;
}
@ -1256,7 +1262,7 @@ return true;
BOOL WmTabCommand_Press_max(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("max()", -1);
InsertText("max(", ")");
return true;
}
@ -1264,7 +1270,7 @@ return true;
BOOL WmTabCommand_Press_avg(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("avg()", -1);
InsertText("avg(", ")");
return true;
}
@ -1272,7 +1278,7 @@ return true;
BOOL WmTabCommand_Press_sum(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("sum()", -1);
InsertText("sum(", ")");
return true;
}
@ -1280,7 +1286,7 @@ return true;
BOOL WmTabCommand_Press_root(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("root( ; 2)", -5);
InsertText("root(", " ; )", 1);
return true;
}
@ -1288,14 +1294,14 @@ return true;
BOOL WmTabCommand_Press_gamma(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("gamma()", -1);
InsertText("gamma(", ")");
return true;
}
BOOL WmTabCommand_Press_sqrt(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
InsertText("sqrt()", -1);
InsertText("sqrt(", ")");
return true;
}