util.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. #include <commctrl.h>
  26. #include "r_defs.h"
  27. #include "resource.h"
  28. #include "../Agave/Language/api_language.h"
  29. #if 0//syntax highlighting
  30. #include "compiler.h"
  31. #include "richedit.h"
  32. #endif
  33. char* GetTextResource(UINT id){
  34. void* data = 0;
  35. HRSRC rsrc = FindResource(WASABI_API_LNG_HINST,MAKEINTRESOURCE(id),"TEXT");
  36. if(rsrc){
  37. HGLOBAL resourceHandle = LoadResource(WASABI_API_LNG_HINST,rsrc);
  38. data = LockResource(resourceHandle);
  39. }
  40. return (char*)data;
  41. }
  42. void loadComboBox(HWND dlg, char *ext, char *selectedName)
  43. {
  44. char path[MAX_PATH];
  45. int a;
  46. HANDLE ff;
  47. WIN32_FIND_DATA fd;
  48. wsprintf(path,"%s\\%s",g_path,ext);
  49. ff=FindFirstFile(path, &fd);
  50. if (ff == INVALID_HANDLE_VALUE) return;
  51. do
  52. {
  53. SendMessage(dlg, CB_ADDSTRING, 0, (LPARAM)(fd.cFileName));
  54. } while (FindNextFile(ff, &fd));
  55. FindClose(ff);
  56. a = SendMessage(dlg, CB_FINDSTRINGEXACT, 0, (LPARAM)(selectedName));
  57. if (a != CB_ERR) SendMessage(dlg, CB_SETCURSEL, (WPARAM) a, 0);
  58. }
  59. void GR_SelectColor(HWND hwnd, int *a)
  60. {
  61. static COLORREF custcolors[16];
  62. CHOOSECOLOR cs;
  63. cs.lStructSize = sizeof(cs);
  64. cs.hwndOwner = hwnd;
  65. cs.hInstance = 0;
  66. cs.rgbResult=((*a>>16)&0xff)|(*a&0xff00)|((*a<<16)&0xff0000);
  67. cs.lpCustColors = custcolors;
  68. cs.Flags = CC_RGBINIT|CC_FULLOPEN;
  69. if (ChooseColor(&cs))
  70. {
  71. *a = ((cs.rgbResult>>16)&0xff)|(cs.rgbResult&0xff00)|((cs.rgbResult<<16)&0xff0000);
  72. }
  73. }
  74. void GR_DrawColoredButton(DRAWITEMSTRUCT *di, COLORREF color)
  75. {
  76. color = ((color>>16)&0xff)|(color&0xff00)|((color<<16)&0xff0000);
  77. char wt[123];
  78. HPEN hPen,hOldPen;
  79. HBRUSH hBrush,hOldBrush;
  80. hPen = (HPEN)GetStockObject(BLACK_PEN);
  81. LOGBRUSH lb={BS_SOLID,color,0};
  82. hBrush = CreateBrushIndirect(&lb);
  83. hOldPen=(HPEN)SelectObject(di->hDC,hPen);
  84. hOldBrush=(HBRUSH)SelectObject(di->hDC,hBrush);
  85. Rectangle(di->hDC,di->rcItem.left,di->rcItem.top,di->rcItem.right,di->rcItem.bottom);
  86. GetWindowText(di->hwndItem,wt,sizeof(wt));
  87. SetBkColor(di->hDC,color);
  88. SetTextColor(di->hDC,~color & 0xffffff);
  89. DrawText(di->hDC,wt,-1,&di->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
  90. DeleteObject(hBrush);
  91. SelectObject(di->hDC,hOldPen);
  92. SelectObject(di->hDC,hOldBrush);
  93. }
  94. static int m_help_lastpage=4;
  95. static char *m_localtext;
  96. static void _dosetsel(HWND hwndDlg)
  97. {
  98. HWND tabwnd=GetDlgItem(hwndDlg,IDC_TAB1);
  99. int sel=TabCtrl_GetCurSel(tabwnd);
  100. char *text="";
  101. m_help_lastpage=sel;
  102. if (sel == 0)
  103. /*
  104. text="Many AVS effects allow you to write simple expressions to control\r\n"
  105. "visualization. Here is a brief summary of how to write AVS code.\r\n"
  106. "\r\n"
  107. "Many aspects of AVS code are similar to C (including comments). "
  108. "\r\n"
  109. "You can create new variables just by using them, and you can read\r\n"
  110. "and write predefined variables (of which each effect has its own)\r\n"
  111. "to interact with the effect. Note that variables are all floating\r\n"
  112. "point numbers (no strings), and the maximum length of a variable's\r\n"
  113. "name is 8 characters (anything longer will be ignored.\r\n"
  114. "\r\n"
  115. "So, to create a variable, you can simply use it, for example:\r\n"
  116. " x = 5;\r\n"
  117. "\r\n"
  118. "You can also use a variety of operators and math functions to\r\n"
  119. "modify variables, see the Operators and Functions tabs above.\r\n"
  120. "\r\n"
  121. "Code can include C and C++ style comments:\r\n"
  122. "\r\n"
  123. "You can combine operators and functions into expressions, such\r\n"
  124. "as:\r\n"
  125. " x = 5 * cos(y) / 32.0; // this does some leetness right here\r\n"
  126. "\r\n"
  127. "You can use multiple expressions by seperating them with one or\r\n"
  128. "more semicolons, for example:\r\n"
  129. " x = x * 17.0; x = x / 5; y = pow(x,3.0);\r\n"
  130. "\r\n"
  131. "It is worth noting that extra whitespace (spaces, newlines) is\r\n"
  132. "ignored, so if you need to space things out for clarity, you can.\r\n"
  133. ;
  134. else if (sel == 1)
  135. text="The following operators are available:\r\n"
  136. "=\r\n"
  137. " assigns a value to a variable. \r\n"
  138. " example: var=5;\r\n"
  139. "\r\n"
  140. "+\r\n"
  141. " adds two values, returns the sum. \r\n"
  142. " example: var=5+var2;\r\n"
  143. "\r\n"
  144. "-\r\n"
  145. " subtracts two values, returns the difference. \r\n"
  146. " example: var=5-var2;\r\n"
  147. "\r\n"
  148. "*\r\n"
  149. " multiplies two values, returns the product. \r\n"
  150. " example: var=5*var2;\r\n"
  151. "\r\n"
  152. "/\r\n"
  153. " divides two values, returns the quotient. \r\n"
  154. " example: var=5/var2;\r\n"
  155. "\r\n"
  156. "%\r\n"
  157. " converts two values to integer, performs division, returns remainder\r\n"
  158. " example: var=var2%5;\r\n"
  159. "\r\n"
  160. "|\r\n"
  161. " converts two values to integer, returns bitwise OR of both values\r\n"
  162. " example: var=var2|31;\r\n"
  163. "\r\n"
  164. "&\r\n"
  165. " converts two values to integer, returns bitwise AND of both values\r\n"
  166. " example: var=var2&31;\r\n"
  167. "\r\n"
  168. ;
  169. else if (sel == 2)
  170. text="Functions available from code:\r\n"
  171. "abs(value)\r\n"
  172. " = returns the absolute value of 'value'\r\n"
  173. "\r\n"
  174. "sin(value)\r\n"
  175. " = returns the sine of the radian angle 'value'\r\n"
  176. "\r\n"
  177. "cos(value)\r\n"
  178. " = returns the cosine of the radian angle 'value'\r\n"
  179. "\r\n"
  180. "tan(value)\r\n"
  181. " = returns the tangent of the radian angle 'value'\r\n"
  182. "\r\n"
  183. "asin(value)\r\n"
  184. " = returns the arcsine (in radians) of 'value'\r\n"
  185. "\r\n"
  186. "acos(value)\r\n"
  187. " = returns the arccosine (in radians) of 'value'\r\n"
  188. "\r\n"
  189. "atan(value)\r\n"
  190. " = returns the arctangent (in radians) of 'value'\r\n"
  191. "\r\n"
  192. "atan2(value,value2)\r\n"
  193. " = returns the arctangent (in radians) of 'value'/'value2'\r\n"
  194. "\r\n"
  195. "sqr(value)\r\n"
  196. " = returns the square of 'value'\r\n"
  197. "\r\n"
  198. "sqrt(value)\r\n"
  199. " = returns the square root of 'value'\r\n"
  200. "\r\n"
  201. "invsqrt(value)\r\n"
  202. " = returns the reciprocal of the square root of 'value' (1/sqrt(value))\r\n"
  203. " (uses a fast approximation, may not always = 1/sqrt(value) :)\r\n"
  204. "\r\n"
  205. "pow(value,value2)\r\n"
  206. " = returns 'value' to the power of 'value2'\r\n"
  207. "\r\n"
  208. "exp(value)\r\n"
  209. " = returns e to the power of 'value'\r\n"
  210. "\r\n"
  211. "log(value)\r\n"
  212. " = returns the log in base e of 'value'\r\n"
  213. "\r\n"
  214. "log10(value)\r\n"
  215. " = returns the log in base 10 of 'value'\r\n"
  216. "\r\n"
  217. "floor(value)\r\n"
  218. " = returns the largest integer less than or equal to 'value'\r\n"
  219. "\r\n"
  220. "ceil(value)\r\n"
  221. " = returns the smallest integer greater than or equal to 'value'\r\n"
  222. "\r\n"
  223. "sign(value)\r\n"
  224. " = returns the sign of 'value' (-1.0 or 1.0, or 0.0 or -0.0 for 0.0 or -0.0)\r\n"
  225. "\r\n"
  226. "min(value,value2)\r\n"
  227. " = returns the smallest of 'value' and 'value2'\r\n"
  228. "\r\n"
  229. "max(var,var2)\r\n"
  230. " = returns the greatest of 'value' and 'value2'\r\n"
  231. "\r\n"
  232. "sigmoid(value,value2)\r\n"
  233. " = returns sigmoid function value of x='value' ('value2'=constraint)\r\n"
  234. "\r\n"
  235. "rand(value)\r\n"
  236. " = returns a random integer between 0 and 'value'\r\n"
  237. "\r\n"
  238. "band(value,value2)\r\n"
  239. " = returns a boolean AND of 'value' and 'value2'\r\n"
  240. "\r\n"
  241. "bor(value,value2)\r\n"
  242. " = returns a boolean OR of 'value' and 'value2'\r\n"
  243. "\r\n"
  244. "bnot(value)\r\n"
  245. " = returns a boolean NOT of 'value'\r\n"
  246. "\r\n"
  247. "if(condition,valtrue,valfalse)\r\n"
  248. " = returns 'valtrue' if 'condition' is nonzero, returns 'valfalse' otherwise.\r\n"
  249. " new in AVS 2.8+: only one of valtrue/valfalse is evaluated, depending on condition\r\n"
  250. "\r\n"
  251. "assign(dest, source)\r\n"
  252. " = if 'dest' is a variable, assigns the value of 'source' to it. returns the value of 'source'.\r\n"
  253. " a little trick: assign(if(v,a,b),1.0); is like if V is true, a=1.0, otherwise b=1.0. :)\r\n"
  254. "\r\n"
  255. "exec2(parm1, parm2)\r\n"
  256. " = evaluates parm1, then parm2, and returns the value of parm2.\r\n"
  257. "\r\n"
  258. "equal(value,value2)\r\n"
  259. " = returns 1.0 if 'value' is equal to 'value2', otherwise returns 0.0\r\n"
  260. "\r\n"
  261. "above(value,value2)\r\n"
  262. " = returns 1.0 if 'value' is greater than 'value2', otherwise returns 0.0\r\n"
  263. "\r\n"
  264. "below(value,value2)\r\n"
  265. " = returns 1.0 if 'value' is less than 'value2', otherwise returns 0.0\r\n"
  266. "\r\n"
  267. "getosc(band,width,channel)\r\n"
  268. " = returns waveform data centered at 'band', (0..1), sampled 'width' (0..1) wide.\r\n"
  269. " 'channel' can be: 0=center, 1=left, 2=right. return value is (-1..1)\r\n"
  270. "\r\n"
  271. "getspec(band,width,channel)\r\n"
  272. " = returns spectrum data centered at 'band', (0..1), sampled 'width' (0..1) wide.\r\n"
  273. " 'channel' can be: 0=center, 1=left, 2=right. return value is (0..1)\r\n"
  274. "\r\n"
  275. "gettime(start_time)\r\n"
  276. " = returns time in seconds since start_time (start_time can be 0 for time since boot)\r\n"
  277. " (start_time can be -1.0 for current play time in seconds\r\n"
  278. " (start_time can be -2.0 for current play length in seconds\r\n"
  279. "\r\n"
  280. "getkbmouse(which_parm)\r\n"
  281. " = returns information about the location and state of the keyboard or mouse\r\n"
  282. " which_parm = 1: mouse X position (-1..1 is onscreen)\r\n"
  283. " which_parm = 2: mouse Y position (-1..1 is onscreen)\r\n"
  284. " which_parm = 3: mouse left button state (0 up, 1 down)\r\n"
  285. " which_parm = 4: mouse right button state (0 up, 1 down)\r\n"
  286. " which_parm = 5: mouse middle button state (0 up, 1 down)\r\n"
  287. " which_parm > 5: (GetAsyncKeyState(which_parm)&0x8000)?1:0\r\n"
  288. "\r\n"
  289. "megabuf(index)\r\n"
  290. " = can be used to get or set an item from the 1 million item temp buffer\r\n"
  291. " to get, use: val=megabuf(index);\r\n"
  292. " to set, use: assign(megabuf(index),val);\r\n"
  293. "gmegabuf(index)\r\n"
  294. " = can be used to get or set an item from the global 1 million item buffer\r\n"
  295. " to get, use: val=gmegabuf(index);\r\n"
  296. " to set, use: assign(gmegabuf(index),val);\r\n"
  297. "\r\n"
  298. "loop(count, statement)\r\n"
  299. " = executes <statement> <count> times. count is evaluated once and clamped\r\n"
  300. " to 0..4096. best used with exec2() and exec3() and assign(). Note that\r\n"
  301. " the return value of loop() is undefined and should not be used.\r\n"
  302. "\r\n"
  303. ;
  304. else if (sel == 3)
  305. text="Constants\r\n"
  306. " '$PI' can be used in place of '3.14159'\r\n"
  307. " '$E' can be used in place of '2.71828'\r\n"
  308. " '$PHI' can be used in place of '1.618033'\r\n"
  309. " Numbers can be specified as integers or as floating point\r\n"
  310. " (i.e. '5' or '5.0' or '5.00001')\r\n"
  311. ;
  312. */
  313. text=GetTextResource(IDR_HELP_1);
  314. else if (sel == 1)
  315. text=GetTextResource(IDR_HELP_2);
  316. else if (sel == 2)
  317. text=GetTextResource(IDR_HELP_3);
  318. else if (sel == 3)
  319. text=GetTextResource(IDR_HELP_4);
  320. else if (sel == 4 && m_localtext)
  321. text=m_localtext;
  322. SetDlgItemText(hwndDlg,IDC_EDIT1,text);
  323. }
  324. static BOOL CALLBACK evalHelpDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  325. {
  326. switch (uMsg)
  327. {
  328. case WM_INITDIALOG:
  329. {
  330. TCITEM item;
  331. HWND tabwnd=GetDlgItem(hwndDlg,IDC_TAB1);
  332. item.mask=TCIF_TEXT;
  333. item.pszText=WASABI_API_LNGSTRING(IDS_GENERAL);
  334. TabCtrl_InsertItem(tabwnd,0,&item);
  335. item.pszText=WASABI_API_LNGSTRING(IDS_OPERATORS);
  336. TabCtrl_InsertItem(tabwnd,1,&item);
  337. item.pszText=WASABI_API_LNGSTRING(IDS_FUNCTIONS);
  338. TabCtrl_InsertItem(tabwnd,2,&item);
  339. item.pszText=WASABI_API_LNGSTRING(IDS_CONSTANTS);
  340. TabCtrl_InsertItem(tabwnd,3,&item);
  341. // fucko: context specific stuff
  342. m_localtext=0;
  343. if (lParam)
  344. {
  345. item.pszText=(char *)lParam;
  346. m_localtext=item.pszText + strlen(item.pszText)+1;
  347. TabCtrl_InsertItem(tabwnd,4,&item);
  348. }
  349. else if (m_help_lastpage > 3) m_help_lastpage=0;
  350. TabCtrl_SetCurSel(tabwnd,m_help_lastpage);
  351. _dosetsel(hwndDlg);
  352. }
  353. return 0;
  354. case WM_COMMAND:
  355. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  356. EndDialog(hwndDlg,1);
  357. return 0;
  358. case WM_NOTIFY:
  359. {
  360. LPNMHDR p=(LPNMHDR) lParam;
  361. if (p->idFrom == IDC_TAB1 && p->code == TCN_SELCHANGE) _dosetsel(hwndDlg);
  362. }
  363. return 0;
  364. }
  365. return 0;
  366. }
  367. void compilerfunctionlist(HWND hwndDlg, char *localinfo)
  368. {
  369. WASABI_API_DIALOGBOXPARAM(IDD_EVAL_HELP,hwndDlg,evalHelpDlgProc, (LONG)localinfo);
  370. }
  371. #if 0//syntax highlighting
  372. // If you include richedit boxes, you need to load the richlib at the beginning:
  373. // HANDLE hRichLib = LoadLibrary("RICHED32.DLL");
  374. // When quitting:
  375. // FreeLibrary(hRichLib);
  376. #define M_WORD 1
  377. #define M_NUM 2
  378. #define M_COMM 3
  379. #define is_alpha(a) ((((a) >= 'A') && ((a) <= 'Z')) || (((a) >= 'a') && ((a) <= 'z')))
  380. #define is_num(a) (((a) >= '0') && ((a) <= '9'))
  381. #define is_op(a) (((a) == '=') || ((a) == '+') || ((a) == '-') || ((a) == '*') || ((a) == '/') || ((a) == '%'))
  382. // Colors for bracket pairs (loops around, ugly colors for now), can be any number of colors
  383. static int bcol[] = { RGB(192, 0, 0), RGB(64, 128, 128), RGB(128, 0, 255), RGB(128, 128, 255) };
  384. #define COLOR_COMMENT RGB(0, 128, 0)
  385. #define COLOR_FUNC RGB(0, 0, 192)
  386. #define COLOR_VAR RGB(96, 96, 96)
  387. #define COLOR_OP RGB(0, 0, 0)
  388. #define COLOR_NUMBER RGB(0, 0, 128)
  389. // Actual syntax highlighting
  390. // 'hwnd' is a richedit box, 'data' is the code of 'size' characters
  391. void doAVSEvalHighLight(HWND hwndDlg, UINT sub, char *data) {
  392. int size=strlen(data);
  393. HWND hwnd=GetDlgItem(hwndDlg,sub);
  394. CHARRANGE cr, cro;
  395. CHARFORMAT cf;
  396. cf.cbSize = sizeof(CHARFORMAT);
  397. cf.dwMask = CFM_COLOR;
  398. cf.dwEffects = 0;
  399. SendMessage(hwnd, WM_SETREDRAW, false, 0);
  400. SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&cro);
  401. int mode = 0;
  402. int pos = 0;
  403. int brackets = 0;
  404. for (int i = 0; i < size; ++i) {
  405. if (mode == M_COMM) {
  406. // We're inside a comment, check for its end
  407. if ((data[i] == ';') || ((data[i] == '*') && ((i+1) < size) && (data[++i] == '/'))) {
  408. mode = 0;
  409. cf.crTextColor = COLOR_COMMENT;
  410. cr.cpMin = pos;
  411. cr.cpMax = i+1;
  412. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  413. SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  414. }
  415. } else if (is_alpha(data[i]) || ((mode == M_WORD) && is_num(data[i]))) {
  416. // Letter tokens (func calls, vars)
  417. if (mode != M_WORD) {
  418. // Enter word-mode if we haven't yet
  419. mode = M_WORD;
  420. pos = i;
  421. }
  422. // Stop condition
  423. bool valid = (i != (size-1)); // Check if this isn't the last character
  424. if (valid) {
  425. valid = is_num(data[i+1]) || is_alpha(data[i+1]);
  426. }
  427. if (!valid) {
  428. // We have reached the end of this word
  429. cr.cpMin = pos;
  430. cr.cpMax = i+1;
  431. // Check if its a function
  432. bool func = false;
  433. for (int j = 0; j < (sizeof(fnTable) / sizeof(fnTable[0])); ++j) {
  434. if ((i - pos + 1) == (signed)strlen(fnTable[j].name)) {
  435. if (strnicmp(fnTable[j].name, &data[pos], strlen(fnTable[j].name)) == 0) {
  436. func = true;
  437. break;
  438. }
  439. }
  440. }
  441. cf.crTextColor = func ? COLOR_FUNC : COLOR_VAR;
  442. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  443. SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  444. }
  445. } else if (is_op(data[i])) {
  446. // This is an operator
  447. mode = 0;
  448. cf.crTextColor = COLOR_OP;
  449. cr.cpMin = i;
  450. cr.cpMax = i+1;
  451. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  452. SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  453. } else if ((data[i] & 0x80) || ((data[i] == '/') && ((i+1) < size) && (data[++i] == '*'))) {
  454. // This is a comment marker, enter comment mode
  455. mode = M_COMM;
  456. pos = i;
  457. } else if ((data[i] == '(') || (data[i] == ')') || (data[i] == ',')) {
  458. // Reached brackets: we count them and color them in pairs
  459. mode = 0;
  460. if (data[i] == '(') ++brackets;
  461. cf.crTextColor = bcol[brackets % (sizeof(bcol) / sizeof(bcol[0]))];
  462. if (data[i] == ')') --brackets;
  463. cr.cpMin = i;
  464. cr.cpMax = i+1;
  465. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  466. SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  467. } else if (is_num(data[i]) || (data[i] == '.')) {
  468. // constants
  469. if (mode != M_NUM) {
  470. pos = i;
  471. mode = M_NUM;
  472. }
  473. // Stop condition
  474. bool valid = (i != (size-1));
  475. if (valid) {
  476. valid = is_num(data[i+1]) || (data[i+1] == '.');
  477. }
  478. if (!valid) {
  479. cf.crTextColor = COLOR_NUMBER;
  480. cr.cpMin = pos;
  481. cr.cpMax = i+1;
  482. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cr);
  483. SendMessage(hwnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
  484. }
  485. } else if (data[i] == ';') {
  486. // Reset bracket count and mode for every statement.
  487. mode = 0;
  488. brackets = 0;
  489. }
  490. }
  491. SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&cro);
  492. SendMessage(hwnd, WM_SETREDRAW, true, 0);
  493. InvalidateRect(hwnd, 0, true);
  494. }
  495. #endif