view_history.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  1. #include "main.h"
  2. #include "..\..\General\gen_ml/config.h"
  3. #include "resource.h"
  4. #include "../nu/DialogSkinner.h"
  5. #include "../nu/listview.h"
  6. #include <time.h>
  7. #include "..\..\General\gen_ml/gaystring.h"
  8. #include "..\..\General\gen_ml/ml_ipc.h"
  9. #include <malloc.h>
  10. #include <string>
  11. #include "../nu/AutoWide.h"
  12. #include "../nu/AutoCharFn.h"
  13. #include "..\..\General\gen_ml/ml.h"
  14. #include "..\..\General\gen_ml/ml_ipc_0313.h"
  15. #include "../nu/sort.h"
  16. #include "../nu/menushortcuts.h"
  17. #include "../Winamp/strutil.h"
  18. #include "api__ml_history.h"
  19. #include <strsafe.h>
  20. static INT_PTR IPC_LIBRARY_SENDTOMENU;
  21. static void history_cleanupifnecessary();
  22. static GayStringW g_q;
  23. static W_ListView resultlist;
  24. static int resultSkin, customAllowed;
  25. static HWND m_hwnd, m_headerhwnd;
  26. static historyRecordList itemCache;
  27. static int history_bgThread_Kill;
  28. static HANDLE history_bgThread_Handle;
  29. static void fileInfoDialogs(HWND hwndParent);
  30. static int m_lv_last_topidx;
  31. int groupBtn = 1, enqueuedef = 0;
  32. HMENU g_context_menus2 = NULL;
  33. static viewButtons view;
  34. static void MakeDateStringW(__time64_t convertTime, wchar_t *dest, size_t destlen)
  35. {
  36. SYSTEMTIME sysTime = {0};
  37. tm *newtime = _localtime64(&convertTime);
  38. sysTime.wYear = newtime->tm_year + 1900;
  39. sysTime.wMonth = newtime->tm_mon + 1;
  40. sysTime.wDayOfWeek = newtime->tm_wday;
  41. sysTime.wDay = newtime->tm_mday;
  42. sysTime.wHour = newtime->tm_hour;
  43. sysTime.wMinute = newtime->tm_min;
  44. sysTime.wSecond = newtime->tm_sec;
  45. GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &sysTime, NULL, dest, (int)destlen);
  46. size_t dateSize = lstrlenW(dest);
  47. dest += dateSize;
  48. destlen -= dateSize;
  49. if (destlen)
  50. {
  51. *dest++ = ' ';
  52. destlen--;
  53. }
  54. GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, dest, (int)destlen);
  55. }
  56. void makeFilename2(const wchar_t *filename, wchar_t *filename2, int filename2_len)
  57. {
  58. filename2[0]=0;
  59. if (wcsstr(filename,L"~"))
  60. {
  61. WIN32_FIND_DATAW d = {0};
  62. HANDLE h = FindFirstFileW(filename,&d);
  63. if (h != INVALID_HANDLE_VALUE)
  64. {
  65. FindClose(h);
  66. lstrcpynW(filename2,filename,filename2_len);
  67. wchar_t *p=scanstr_backW(filename2,L"\\",filename2-1)+1;
  68. int offs=(int)(p-filename2);
  69. lstrcpynW(filename2+offs,d.cFileName,filename2_len - offs);
  70. if (!_wcsicmp(filename,filename2)) filename2[0]=0;
  71. }
  72. }
  73. }
  74. void db_setFieldString(nde_scanner_t s, unsigned char id, const wchar_t *data)
  75. {
  76. nde_field_t f = NDE_Scanner_GetFieldByID(s, id);
  77. if (!f) f = NDE_Scanner_NewFieldByID(s, id);
  78. NDE_StringField_SetString(f, data);
  79. }
  80. void db_setFieldInt(nde_scanner_t s, unsigned char id, int data)
  81. {
  82. nde_field_t f = NDE_Scanner_GetFieldByID(s, id);
  83. if (!f) f = NDE_Scanner_NewFieldByID(s, id);
  84. NDE_IntegerField_SetValue(f, data);
  85. }
  86. void queryStrEscape(const wchar_t *p, GayStringW &str)
  87. {
  88. if (!p || !*p) return;
  89. size_t l = wcslen(p);
  90. wchar_t *escaped = (wchar_t *)calloc((l*3+1), sizeof(wchar_t));
  91. if (escaped)
  92. {
  93. wchar_t *d = escaped;
  94. while (p && *p)
  95. {
  96. if (*p == L'%') { *d++ = L'%'; *d++ = L'%'; }
  97. else if (*p == L'\"') { *d++ = L'%'; *d++ = L'2'; *d++ = L'2'; }
  98. else if (*p == L'\'') { *d++ = L'%'; *d++ = L'2'; *d++ = L'7'; }
  99. else if (*p == L'[') { *d++ = L'%'; *d++ = L'5'; *d++ = L'B'; }
  100. else if (*p == L']') { *d++ = L'%'; *d++ = L'5'; *d++ = L'D'; }
  101. else if (*p == L'(') { *d++ = L'%'; *d++ = L'2'; *d++ = L'8'; }
  102. else if (*p == L')') { *d++ = L'%'; *d++ = L'2'; *d++ = L'9'; }
  103. else if (*p == L'#') { *d++ = L'%'; *d++ = L'2'; *d++ = L'3'; }
  104. else *d++ = *p;
  105. p++;
  106. }
  107. *d = 0;
  108. str.Set(escaped);
  109. free(escaped);
  110. }
  111. }
  112. int STRCMP_NULLOK(const wchar_t *pa, const wchar_t *pb)
  113. {
  114. if (!pa) pa = L"";
  115. else SKIP_THE_AND_WHITESPACE(pa)
  116. if (!pb) pb = L"";
  117. else SKIP_THE_AND_WHITESPACE(pb)
  118. return _wcsicmp(pa, pb);
  119. }
  120. struct SortRules
  121. {
  122. int by;
  123. int dir;
  124. };
  125. static int __fastcall sortFunc(const void *elem1, const void *elem2, const void *context)
  126. {
  127. historyRecord *a=(historyRecord*)elem1;
  128. historyRecord *b=(historyRecord*)elem2;
  129. const SortRules *rules = (SortRules *)context;
  130. int use_by = rules->by;
  131. int use_dir = !!rules->dir;
  132. #define RETIFNZ(v) if ((v)<0) return use_dir?1:-1; if ((v)>0) return use_dir?-1:1;
  133. // this might be too slow, but it'd be nice
  134. int x;
  135. for (x = 0; x < 4; x ++)
  136. {
  137. if (use_by == HISTORY_SORT_FILENAME)
  138. {
  139. int v=STRCMP_NULLOK(a->filename,b->filename);
  140. RETIFNZ(v)
  141. return 0;
  142. }
  143. else if (use_by == HISTORY_SORT_TITLE)
  144. {
  145. int v=STRCMP_NULLOK(a->title,b->title);
  146. RETIFNZ(v)
  147. return 0;
  148. }
  149. else if (use_by == HISTORY_SORT_LASTPLAYED)
  150. {
  151. __time64_t v1=a->lastplayed;
  152. __time64_t v2=b->lastplayed;
  153. RETIFNZ(v2-v1)
  154. return 0;
  155. }
  156. else if (use_by == HISTORY_SORT_PLAYCOUNT)
  157. {
  158. int v1=a->playcnt;
  159. int v2=b->playcnt;
  160. RETIFNZ(v2-v1)
  161. use_by=HISTORYVIEW_COL_LASTPLAYED;
  162. }
  163. else if (use_by == HISTORY_SORT_LENGTH) // length -> artist -> album -> track
  164. {
  165. int v1=a->length;
  166. int v2=b->length;
  167. if (v1<0)v1=0;
  168. if (v2<0)v2=0;
  169. RETIFNZ(v2-v1)
  170. return 0;
  171. }
  172. else if (use_by == HISTORY_SORT_OFFSET)
  173. {
  174. int v1=a->offset;
  175. int v2=b->offset;
  176. if (v1<0)v1=0;
  177. if (v2<0)v2=0;
  178. RETIFNZ(v2-v1)
  179. return 0;
  180. }
  181. else break; // no sort order?
  182. }
  183. #undef RETIFNZ
  184. return 0;
  185. }
  186. void sortResults(historyRecordList *obj, int column, int dir) // sorts the results based on the current sort mode
  187. {
  188. if (obj->Size > 1)
  189. {
  190. SortRules rules = {column, dir};
  191. nu::qsort(obj->Items,obj->Size,sizeof(historyRecord),&rules, sortFunc);
  192. }
  193. }
  194. // does not copy filename
  195. void recentScannerRefToObjCacheNFN(nde_scanner_t s, historyRecordList *obj)
  196. {
  197. nde_field_t f=NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_TITLE);
  198. if (f)
  199. {
  200. wchar_t *strval = NDE_StringField_GetString(f);
  201. ndestring_retain(strval);
  202. obj->Items[obj->Size].title = strval;
  203. }
  204. else
  205. obj->Items[obj->Size].title = 0;
  206. f=NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_LENGTH);
  207. obj->Items[obj->Size].length = f?NDE_IntegerField_GetValue(f):-1;
  208. f=NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_OFFSET);
  209. obj->Items[obj->Size].offset = f?NDE_IntegerField_GetValue(f):-1;
  210. f=NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_PLAYCOUNT);
  211. obj->Items[obj->Size].playcnt = f?NDE_IntegerField_GetValue(f):0;
  212. f=NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_LASTPLAYED);
  213. obj->Items[obj->Size].lastplayed = f?NDE_IntegerField_GetValue(f):0;
  214. obj->Size++;
  215. }
  216. static void freeRecentRecord(historyRecord *p)
  217. {
  218. ndestring_release(p->title);
  219. ndestring_release(p->filename);
  220. }
  221. void emptyRecentRecordList(historyRecordList *obj)
  222. {
  223. historyRecord *p=obj->Items;
  224. while (obj->Size-->0)
  225. {
  226. freeRecentRecord(p);
  227. p++;
  228. }
  229. obj->Size=0;
  230. }
  231. void freeRecentRecordList(historyRecordList *obj)
  232. {
  233. emptyRecentRecordList(obj);
  234. free(obj->Items);
  235. obj->Items=0;
  236. obj->Alloc=obj->Size=0;
  237. }
  238. void allocRecentRecordList(historyRecordList *obj, int newsize, int granularity)
  239. {
  240. if (newsize < obj->Alloc || newsize < obj->Size) return;
  241. size_t old_Alloc = obj->Alloc;
  242. obj->Alloc=newsize+granularity;
  243. historyRecord *data = (historyRecord*)realloc(obj->Items,sizeof(historyRecord)*obj->Alloc);
  244. if (data)
  245. {
  246. obj->Items=data;
  247. }
  248. else
  249. {
  250. data=(historyRecord*)malloc(sizeof(historyRecord)*obj->Alloc);
  251. if (data)
  252. {
  253. memcpy(data, obj->Items, sizeof(historyRecord)*old_Alloc);
  254. free(obj->Items);
  255. obj->Items=data;
  256. }
  257. else obj->Alloc = (int)old_Alloc;
  258. }
  259. if (!obj->Items) obj->Alloc=0;
  260. }
  261. static void playFiles(int enqueue, int all)
  262. {
  263. if (history_bgThread_Handle) return;
  264. int cnt=0;
  265. int l=itemCache.Size;
  266. for(int i=0;i<l;i++)
  267. {
  268. if ( all || resultlist.GetSelected( i ) )
  269. {
  270. if ( !cnt )
  271. {
  272. if ( !enqueue ) SendMessage( plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_DELETE );
  273. cnt++;
  274. }
  275. enqueueFileWithMetaStructW s = { 0 };
  276. s.filename = itemCache.Items[ i ].filename;
  277. s.title = itemCache.Items[ i ].title;
  278. s.ext = NULL;
  279. s.length = itemCache.Items[ i ].length;
  280. SendMessage( plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILEW );
  281. }
  282. }
  283. if (cnt)
  284. {
  285. if(!enqueue) SendMessage(plugin.hwndWinampParent, WM_WA_IPC,0,IPC_STARTPLAY);
  286. }
  287. }
  288. static int history_saveQueryToList(nde_scanner_t s, historyRecordList *obj, int user32, int *killswitch) {
  289. emptyRecentRecordList(obj);
  290. NDE_Scanner_First(s, killswitch);
  291. if (killswitch && *killswitch)
  292. {
  293. return 0;
  294. }
  295. int r;
  296. unsigned int total_length_s=0;
  297. do
  298. {
  299. nde_field_t f = NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_FILENAME);
  300. if (!f) break;
  301. allocRecentRecordList(obj,obj->Size+1);
  302. if (!obj->Alloc) break;
  303. wchar_t *strval = NDE_StringField_GetString(f);
  304. ndestring_retain(strval);
  305. obj->Items[obj->Size].filename = strval;
  306. recentScannerRefToObjCacheNFN(s,obj);
  307. int thisl=obj->Items[obj->Size-1].length;
  308. if (thisl > 0) total_length_s+=thisl * obj->Items[obj->Size-1].playcnt;
  309. else total_length_s|=(1<<31);
  310. r=NDE_Scanner_Next(s, killswitch);
  311. if (killswitch && *killswitch)
  312. {
  313. return 0;
  314. }
  315. } while(r);
  316. if (obj->Size && obj->Size < obj->Alloc - 1024)
  317. {
  318. size_t old_Alloc = obj->Alloc;
  319. obj->Alloc = obj->Size;
  320. historyRecord *data=(historyRecord*)realloc(obj->Items,sizeof(historyRecord)*obj->Alloc);
  321. if (data)
  322. {
  323. obj->Items=data;
  324. }
  325. else
  326. {
  327. data=(historyRecord*)malloc(sizeof(historyRecord)*obj->Alloc);
  328. if (data)
  329. {
  330. memcpy(data, obj->Items, sizeof(historyRecord)*old_Alloc);
  331. free(obj->Items);
  332. obj->Items=data;
  333. }
  334. else obj->Alloc = (int)old_Alloc;
  335. }
  336. }
  337. if (killswitch && *killswitch) return 0;
  338. sortResults(obj,
  339. g_config->ReadInt(L"recent_sort_by",HISTORY_SORT_LASTPLAYED),
  340. g_config->ReadInt(L"recent_sort_dir",0));
  341. if (killswitch && *killswitch) return 0;
  342. return total_length_s;
  343. }
  344. typedef struct
  345. {
  346. int user32;
  347. } history_bgThreadParms;
  348. // out can never be bigger than in+1
  349. static void parsequicksearch(wchar_t *out, const wchar_t *in) // parses a list into a list of terms that we are searching for
  350. {
  351. int inquotes = 0, neednull = 0;
  352. while (in && *in)
  353. {
  354. wchar_t c = *in++;
  355. if (c != ' ' && c != '\t' && c != '\"')
  356. {
  357. neednull = 1;
  358. *out++ = c;
  359. }
  360. else if (c == '\"')
  361. {
  362. inquotes = !inquotes;
  363. if (!inquotes)
  364. {
  365. *out++ = 0;
  366. neednull = 0;
  367. }
  368. }
  369. else
  370. {
  371. if (inquotes) *out++ = c;
  372. else if (neednull)
  373. {
  374. *out++ = 0;
  375. neednull = 0;
  376. }
  377. }
  378. }
  379. *out++ = 0;
  380. *out++ = 0;
  381. }
  382. void makeQueryStringFromText(GayStringW *query, const wchar_t *text, int nf)
  383. {
  384. int ispar = 0;
  385. if (query->Get()[0])
  386. {
  387. ispar = 1;
  388. query->Append(L"&(");
  389. }
  390. if (!_wcsnicmp(text, L"query:", 6)) query->Append(text + 6); // copy the query as is
  391. else if (text[0] == L'?') query->Append(text + 1);
  392. else // this is ubergay. no wait it isn't anymore. it rocks now due to the GayString
  393. {
  394. int isAny = 0;
  395. if (text && (*text == L'*' && text[1] == L' '))
  396. {
  397. isAny = 1;
  398. text += 2;
  399. }
  400. int cchText = lstrlenW(text);
  401. wchar_t *tmpbuf = (wchar_t*)calloc((cchText + 2), sizeof(wchar_t));
  402. parsequicksearch(tmpbuf, text);
  403. int x;
  404. const wchar_t *fields[5] =
  405. {
  406. L"filename",
  407. L"title",
  408. L"artist",
  409. L"album",
  410. L"genre",
  411. };
  412. wchar_t *p = tmpbuf;
  413. while (p && *p)
  414. {
  415. size_t lenp = wcslen(p);
  416. if (p == tmpbuf) query->Append(L"(");
  417. else if (isAny) query->Append(L")|(");
  418. else query->Append(L")&(");
  419. if (p[0] == L'<' && p[wcslen(p) - 1] == L'>' && wcslen(p) > 2)
  420. {
  421. wchar_t *op = p;
  422. while (op && *op)
  423. {
  424. if (op && *op == L'\'') *op = L'\"';
  425. if (op) op++;
  426. }
  427. p[lenp - 1] = 0; // remove >
  428. query->Append(p + 1);
  429. }
  430. else
  431. {
  432. for (x = 0; x < (int)min(sizeof(fields) / sizeof(fields[0]), nf); x ++)
  433. {
  434. const wchar_t *field = fields[x];
  435. if (x) query->Append(L"|");
  436. query->Append(field);
  437. query->Append(L" HAS \"");
  438. GayStringW escaped;
  439. queryStrEscape(p, escaped);
  440. query->Append(escaped.Get());
  441. query->Append(L"\"");
  442. }
  443. }
  444. p += lenp + 1;
  445. }
  446. query->Append(L")");
  447. free(tmpbuf);
  448. }
  449. if (ispar) query->Append(L")");
  450. }
  451. static DWORD WINAPI history_bgThreadQueryProc(void *tmp)
  452. {
  453. history_bgThreadParms *p=(history_bgThreadParms*)tmp;
  454. EnterCriticalSection(&g_db_cs);
  455. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  456. NDE_Scanner_Query(s, g_q.Get());
  457. int total_length_s=history_saveQueryToList(s,&itemCache, p->user32, &history_bgThread_Kill);
  458. NDE_Table_DestroyScanner(g_table, s);
  459. LeaveCriticalSection(&g_db_cs);
  460. if (!history_bgThread_Kill) PostMessage(m_hwnd,WM_APP+3,0x69,total_length_s);
  461. return 0;
  462. }
  463. void history_bgQuery_Stop() // exported for other people to call since it is useful (eventually
  464. { // we should have bgQuery pass the new query info along but I'll do that soon)
  465. if (history_bgThread_Handle)
  466. {
  467. history_bgThread_Kill=1;
  468. WaitForSingleObject(history_bgThread_Handle,INFINITE);
  469. CloseHandle(history_bgThread_Handle);
  470. history_bgThread_Handle=0;
  471. }
  472. KillTimer(m_hwnd,123);
  473. }
  474. static void history_bgQuery(int user32=0) // only internal used
  475. {
  476. history_bgQuery_Stop();
  477. SetDlgItemTextW(m_hwnd,IDC_MEDIASTATUS,WASABI_API_LNGSTRINGW(IDS_SCANNING_ELLIPSE));
  478. SetTimer(m_hwnd,123,200,NULL);
  479. DWORD id;
  480. static history_bgThreadParms parms;
  481. parms.user32=user32;
  482. history_bgThread_Kill=0;
  483. history_bgThread_Handle=CreateThread(NULL,0,history_bgThreadQueryProc,(LPVOID)&parms,0,&id);
  484. }
  485. static void doQuery(HWND hwndDlg, wchar_t *text, int dobg=1) {
  486. history_bgQuery_Stop();
  487. GayStringW query;
  488. if (text[0]) makeQueryStringFromText(&query,text,2);
  489. wchar_t *parent_query=NULL;
  490. SendMessage(GetParent(hwndDlg),WM_APP+2,0,(LONG_PTR)&parent_query);
  491. g_q.Set(L"");
  492. if(parent_query && parent_query[0])
  493. {
  494. g_q.Set(L"(");
  495. g_q.Append(parent_query);
  496. g_q.Append(L")");
  497. }
  498. if (query.Get() && query.Get()[0])
  499. {
  500. if(g_q.Get()[0])
  501. {
  502. g_q.Append(L" & (");
  503. g_q.Append(query.Get());
  504. g_q.Append(L")");
  505. }
  506. else g_q.Set(query.Get());
  507. }
  508. if (dobg) history_bgQuery();
  509. }
  510. static WNDPROC search_oldWndProc;
  511. static DWORD WINAPI search_newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  512. {
  513. if(uMsg == WM_KEYDOWN && wParam == VK_DOWN)
  514. {
  515. HWND hwndList = resultlist.getwnd();
  516. if (hwndList)
  517. {
  518. PostMessage(GetParent(hwndDlg), WM_NEXTDLGCTL, (WPARAM)hwndList, TRUE);
  519. ListView_SetItemState(hwndList,0,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);
  520. }
  521. }
  522. return (DWORD)CallWindowProcW(search_oldWndProc,hwndDlg,uMsg,wParam,lParam);
  523. }
  524. static void exploreItemFolder(HWND hwndDlg)
  525. {
  526. if (resultlist.GetSelectionMark() >= 0)
  527. {
  528. int l=resultlist.GetCount();
  529. for(int i=0;i<l;i++)
  530. {
  531. if (resultlist.GetSelected(i))
  532. {
  533. WASABI_API_EXPLORERFINDFILE->AddFile(itemCache.Items[i].filename);
  534. }
  535. }
  536. WASABI_API_EXPLORERFINDFILE->ShowFiles();
  537. }
  538. }
  539. static void removeSelectedItems(int isAll=0)
  540. {
  541. int hasdel=0;
  542. history_bgQuery_Stop();
  543. EnterCriticalSection(&g_db_cs);
  544. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  545. for(int i=0;i<itemCache.Size;i++)
  546. {
  547. if(resultlist.GetSelected(i) || isAll)
  548. {
  549. if(NDE_Scanner_LocateNDEFilename(s, HISTORYVIEW_COL_FILENAME,FIRST_RECORD,itemCache.Items[i].filename))
  550. {
  551. hasdel=1;
  552. NDE_Scanner_Edit(s);
  553. NDE_Scanner_Delete(s);
  554. NDE_Scanner_Post(s);
  555. }
  556. }
  557. }
  558. NDE_Table_DestroyScanner(g_table, s);
  559. if (!hasdel)
  560. {
  561. LeaveCriticalSection(&g_db_cs);
  562. return;
  563. }
  564. NDE_Table_Sync(g_table);
  565. NDE_Table_Compact(g_table);
  566. g_table_dirty=0;
  567. LeaveCriticalSection(&g_db_cs);
  568. resultlist.Clear();
  569. emptyRecentRecordList(&itemCache);
  570. SendMessage(m_hwnd,WM_APP+1,0,0); //refresh current view
  571. }
  572. static void removeSelectedItemOffsets(int isAll=0)
  573. {
  574. int hasdel=0;
  575. history_bgQuery_Stop();
  576. EnterCriticalSection(&g_db_cs);
  577. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  578. for(int i=0;i<itemCache.Size;i++)
  579. {
  580. if(resultlist.GetSelected(i) || isAll)
  581. {
  582. if(NDE_Scanner_LocateNDEFilename(s, HISTORYVIEW_COL_FILENAME,FIRST_RECORD,itemCache.Items[i].filename))
  583. {
  584. db_setFieldInt(s, HISTORYVIEW_COL_OFFSET, -1);
  585. NDE_Scanner_Post(s);
  586. hasdel=1;
  587. itemCache.Items[i].offset = -1;
  588. }
  589. }
  590. }
  591. NDE_Table_DestroyScanner(g_table, s);
  592. if (!hasdel)
  593. {
  594. LeaveCriticalSection(&g_db_cs);
  595. return;
  596. }
  597. NDE_Table_Sync(g_table);
  598. NDE_Table_Compact(g_table);
  599. g_table_dirty=0;
  600. LeaveCriticalSection(&g_db_cs);
  601. resultlist.RefreshAll();
  602. }
  603. static void History_SaveLastQuery(HWND hwnd)
  604. {
  605. LPSTR pszQuery = NULL;
  606. HWND hEditbox = GetDlgItem(hwnd, IDC_QUICKSEARCH);
  607. if (NULL != hEditbox)
  608. {
  609. UINT cchTextMax = GetWindowTextLength(hEditbox);
  610. if (0 != cchTextMax)
  611. {
  612. cchTextMax++;
  613. LPWSTR pszText = (LPWSTR)calloc(cchTextMax, sizeof(WCHAR));
  614. if (NULL != pszText)
  615. {
  616. UINT cchText = GetWindowTextW(hEditbox, pszText, cchTextMax);
  617. if (0 != cchText)
  618. {
  619. UINT cchQuery = WideCharToMultiByte(CP_UTF8, 0, pszText, cchText, NULL, 0, NULL, NULL);
  620. if (0 != cchQuery)
  621. {
  622. cchQuery++;
  623. pszQuery = (LPSTR)calloc(cchQuery, sizeof(CHAR));
  624. if (NULL != pszQuery)
  625. {
  626. cchQuery = WideCharToMultiByte(CP_UTF8, 0, pszText, cchText, pszQuery, cchQuery, NULL, NULL);
  627. pszQuery[cchQuery] = '\0';
  628. }
  629. }
  630. }
  631. free(pszText);
  632. }
  633. }
  634. }
  635. g_config->WriteString("recent_lastquery", pszQuery);
  636. if (NULL != pszQuery)
  637. free(pszQuery);
  638. }
  639. void SwapPlayEnqueueInMenu(HMENU listMenu)
  640. {
  641. int playPos=-1, enqueuePos=-1;
  642. MENUITEMINFOW playItem={sizeof(MENUITEMINFOW), 0,}, enqueueItem={sizeof(MENUITEMINFOW), 0,};
  643. int numItems = GetMenuItemCount(listMenu);
  644. for (int i=0;i<numItems;i++)
  645. {
  646. UINT id = GetMenuItemID(listMenu, i);
  647. if (id == ID_MEDIAWND_PLAYSELECTEDFILES)
  648. {
  649. playItem.fMask = MIIM_ID;
  650. playPos = i;
  651. GetMenuItemInfoW(listMenu, i, TRUE, &playItem);
  652. }
  653. else if (id == ID_MEDIAWND_ENQUEUESELECTEDFILES)
  654. {
  655. enqueueItem.fMask = MIIM_ID;
  656. enqueuePos= i;
  657. GetMenuItemInfoW(listMenu, i, TRUE, &enqueueItem);
  658. }
  659. }
  660. playItem.wID = ID_MEDIAWND_ENQUEUESELECTEDFILES;
  661. enqueueItem.wID = ID_MEDIAWND_PLAYSELECTEDFILES;
  662. SetMenuItemInfoW(listMenu, playPos, TRUE, &playItem);
  663. SetMenuItemInfoW(listMenu, enqueuePos, TRUE, &enqueueItem);
  664. }
  665. void SyncMenuWithAccelerators(HWND hwndDlg, HMENU menu)
  666. {
  667. HACCEL szAccel[24] = {0};
  668. INT c = WASABI_API_APP->app_getAccelerators(hwndDlg, szAccel, sizeof(szAccel)/sizeof(szAccel[0]), FALSE);
  669. AppendMenuShortcuts(menu, szAccel, c, MSF_REPLACE);
  670. }
  671. static HRGN g_rgnUpdate = NULL;
  672. static int offsetX = 0, offsetY = 0;
  673. typedef struct _LAYOUT
  674. {
  675. INT id;
  676. HWND hwnd;
  677. INT x;
  678. INT y;
  679. INT cx;
  680. INT cy;
  681. DWORD flags;
  682. HRGN rgn;
  683. }
  684. LAYOUT, PLAYOUT;
  685. #define SETLAYOUTPOS(_layout, _x, _y, _cx, _cy) { _layout->x=_x; _layout->y=_y;_layout->cx=_cx;_layout->cy=_cy;_layout->rgn=NULL; }
  686. #define SETLAYOUTFLAGS(_layout, _r) \
  687. { \
  688. BOOL fVis; \
  689. fVis = (WS_VISIBLE & (LONG)GetWindowLongPtr(_layout->hwnd, GWL_STYLE)); \
  690. if (_layout->x == _r.left && _layout->y == _r.top) _layout->flags |= SWP_NOMOVE; \
  691. if (_layout->cx == (_r.right - _r.left) && _layout->cy == (_r.bottom - _r.top)) _layout->flags |= SWP_NOSIZE; \
  692. if ((SWP_HIDEWINDOW & _layout->flags) && !fVis) _layout->flags &= ~SWP_HIDEWINDOW; \
  693. if ((SWP_SHOWWINDOW & _layout->flags) && fVis) _layout->flags &= ~SWP_SHOWWINDOW; \
  694. }
  695. #define LAYOUTNEEEDUPDATE(_layout) ((SWP_NOMOVE | SWP_NOSIZE) != ((SWP_NOMOVE | SWP_NOSIZE | SWP_HIDEWINDOW | SWP_SHOWWINDOW) & _layout->flags))
  696. #define GROUP_MIN 0x1
  697. #define GROUP_MAX 0x3
  698. #define GROUP_SEARCH 0x1
  699. #define GROUP_STATUSBAR 0x2
  700. #define GROUP_MAIN 0x3
  701. static void LayoutWindows(HWND hwnd, BOOL fRedraw, BOOL fUpdateAll = FALSE)
  702. {
  703. static INT controls[] =
  704. {
  705. GROUP_SEARCH, IDC_SEARCHCAPTION, IDC_CLEAR, IDC_QUICKSEARCH,
  706. GROUP_STATUSBAR, IDC_BUTTON_PLAY, IDC_BUTTON_ENQUEUE, IDC_BUTTON_CUSTOM, IDC_REMOVEBOOK, IDC_MEDIASTATUS,
  707. GROUP_MAIN, IDC_LIST2
  708. };
  709. INT index;
  710. RECT rc, rg, ri;
  711. LAYOUT layout[sizeof(controls)/sizeof(controls[0])], *pl;
  712. BOOL skipgroup;
  713. HRGN rgn = NULL;
  714. GetClientRect(hwnd, &rc);
  715. if (rc.bottom == rc.top || rc.right == rc.left) return;
  716. SetRect(&rg, rc.left, rc.top, rc.right, rc.bottom);
  717. pl = layout;
  718. skipgroup = FALSE;
  719. InvalidateRect(hwnd, NULL, TRUE);
  720. for (index = 0; index < sizeof(controls) / sizeof(*controls); index++)
  721. {
  722. if (controls[index] >= GROUP_MIN && controls[index] <= GROUP_MAX) // group id
  723. {
  724. skipgroup = FALSE;
  725. switch (controls[index])
  726. {
  727. case GROUP_SEARCH:
  728. {
  729. wchar_t buffer[128] = {0};
  730. GetDlgItemTextW(hwnd, IDC_BUTTON_PLAY, buffer, ARRAYSIZE(buffer));
  731. LRESULT idealSize = MLSkinnedButton_GetIdealSize(GetDlgItem(hwnd, IDC_BUTTON_PLAY), buffer);
  732. SetRect(&rg, rc.left, rc.top + WASABI_API_APP->getScaleY(2),
  733. rc.right - WASABI_API_APP->getScaleX(2),
  734. rc.top + WASABI_API_APP->getScaleY(HIWORD(idealSize)+1));
  735. rc.top = rg.bottom + WASABI_API_APP->getScaleY(3);
  736. break;
  737. }
  738. case GROUP_STATUSBAR:
  739. {
  740. wchar_t buffer[128] = {0};
  741. HWND ctrl = GetDlgItem(hwnd, IDC_BUTTON_PLAY);
  742. GetWindowTextW(ctrl, buffer, ARRAYSIZE(buffer));
  743. LRESULT idealSize = MLSkinnedButton_GetIdealSize(ctrl, buffer);
  744. SetRect(&rg, rc.left + WASABI_API_APP->getScaleX(1),
  745. rc.bottom - WASABI_API_APP->getScaleY(HIWORD(idealSize)),
  746. rc.right, rc.bottom);
  747. rc.bottom = rg.top - WASABI_API_APP->getScaleY(3);
  748. break;
  749. }
  750. case GROUP_MAIN:
  751. SetRect(&rg, rc.left + WASABI_API_APP->getScaleX(1), rc.top, rc.right, rc.bottom);
  752. break;
  753. }
  754. continue;
  755. }
  756. if (skipgroup) continue;
  757. pl->id = controls[index];
  758. pl->hwnd = GetDlgItem(hwnd, pl->id);
  759. if (!pl->hwnd) continue;
  760. GetWindowRect(pl->hwnd, &ri);
  761. MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&ri, 2);
  762. pl->flags = SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW | SWP_NOCOPYBITS;
  763. switch (pl->id)
  764. {
  765. case IDC_SEARCHCAPTION:
  766. {
  767. wchar_t buffer[128] = {0};
  768. GetWindowTextW(pl->hwnd, buffer, ARRAYSIZE(buffer));
  769. LRESULT idealSize = MLSkinnedStatic_GetIdealSize(pl->hwnd, buffer);
  770. SETLAYOUTPOS(pl, rg.left + WASABI_API_APP->getScaleX(2),
  771. rg.top + WASABI_API_APP->getScaleY(1),
  772. WASABI_API_APP->getScaleX(LOWORD(idealSize)),
  773. (rg.bottom - rg.top));
  774. rg.left += (pl->cx + WASABI_API_APP->getScaleX(4));
  775. break;
  776. }
  777. case IDC_CLEAR:
  778. {
  779. wchar_t buffer[128] = {0};
  780. GetWindowTextW(pl->hwnd, buffer, ARRAYSIZE(buffer));
  781. LRESULT idealSize = MLSkinnedButton_GetIdealSize(pl->hwnd, buffer);
  782. LONG width = LOWORD(idealSize) + WASABI_API_APP->getScaleX(6);
  783. pl->flags |= (((rg.right - rg.left) - width) > WASABI_API_APP->getScaleX(40)) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW ;
  784. SETLAYOUTPOS(pl, rg.right - width, rg.top, width, (rg.bottom - rg.top));
  785. if (SWP_SHOWWINDOW & pl->flags) rg.right -= (pl->cx + WASABI_API_APP->getScaleX(4));
  786. break;
  787. }
  788. case IDC_QUICKSEARCH:
  789. pl->flags |= (rg.right > rg.left) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW;
  790. SETLAYOUTPOS(pl, rg.left, rg.top, rg.right - rg.left - WASABI_API_APP->getScaleX(1),
  791. (rg.bottom - rg.top) - WASABI_API_APP->getScaleY(1));
  792. break;
  793. case IDC_BUTTON_PLAY:
  794. case IDC_BUTTON_ENQUEUE:
  795. case IDC_BUTTON_CUSTOM:
  796. case IDC_REMOVEBOOK:
  797. if (IDC_BUTTON_CUSTOM != pl->id || customAllowed)
  798. {
  799. if (groupBtn && pl->id == IDC_BUTTON_PLAY && enqueuedef == 1)
  800. {
  801. pl->flags |= SWP_HIDEWINDOW;
  802. break;
  803. }
  804. if (groupBtn && pl->id == IDC_BUTTON_ENQUEUE && enqueuedef != 1)
  805. {
  806. pl->flags |= SWP_HIDEWINDOW;
  807. break;
  808. }
  809. if (groupBtn && (pl->id == IDC_BUTTON_PLAY || pl->id == IDC_BUTTON_ENQUEUE) && customAllowed)
  810. {
  811. pl->flags |= SWP_HIDEWINDOW;
  812. break;
  813. }
  814. wchar_t buffer[128] = {0};
  815. GetWindowTextW(pl->hwnd, buffer, ARRAYSIZE(buffer));
  816. LRESULT idealSize = MLSkinnedButton_GetIdealSize(pl->hwnd, buffer);
  817. LONG width = LOWORD(idealSize) + WASABI_API_APP->getScaleX(6);
  818. SETLAYOUTPOS(pl, rg.left, rg.bottom - WASABI_API_APP->getScaleY(HIWORD(idealSize)),
  819. width, WASABI_API_APP->getScaleY(HIWORD(idealSize)));
  820. pl->flags |= ((rg.right - rg.left) > width) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW;
  821. if (SWP_SHOWWINDOW & pl->flags) rg.left += (pl->cx + WASABI_API_APP->getScaleX(4));
  822. }
  823. else
  824. pl->flags |= SWP_HIDEWINDOW;
  825. break;
  826. case IDC_MEDIASTATUS:
  827. SETLAYOUTPOS(pl, rg.left, rg.top, rg.right - rg.left, (rg.bottom - rg.top));
  828. pl->flags |= (pl->cx > WASABI_API_APP->getScaleX(16)) ? SWP_SHOWWINDOW : SWP_HIDEWINDOW;
  829. break;
  830. case IDC_LIST2:
  831. SETLAYOUTPOS(pl, rg.left, rg.top + WASABI_API_APP->getScaleY(1),
  832. (rg.right - rg.left) - WASABI_API_APP->getScaleX(3),
  833. (rg.bottom - rg.top) - WASABI_API_APP->getScaleY(2));
  834. break;
  835. }
  836. SETLAYOUTFLAGS(pl, ri);
  837. if (LAYOUTNEEEDUPDATE(pl))
  838. {
  839. if (SWP_NOSIZE == ((SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_NOSIZE) & pl->flags) &&
  840. ri.left == (pl->x + offsetX) && ri.top == (pl->y + offsetY) && !fUpdateAll && IsWindowVisible(pl->hwnd))
  841. {
  842. SetRect(&ri, pl->x, pl->y, pl->cx + pl->x, pl->y + pl->cy);
  843. ValidateRect(hwnd, &ri);
  844. }
  845. pl++;
  846. }
  847. else if (!fUpdateAll && (fRedraw || (!offsetX && !offsetY)) && IsWindowVisible(pl->hwnd))
  848. {
  849. ValidateRect(hwnd, &ri);
  850. if (GetUpdateRect(pl->hwnd, NULL, FALSE))
  851. {
  852. if (!rgn) rgn = CreateRectRgn(0,0,0,0);
  853. GetUpdateRgn(pl->hwnd, rgn, FALSE);
  854. OffsetRgn(rgn, pl->x, pl->y);
  855. InvalidateRgn(hwnd, rgn, FALSE);
  856. }
  857. }
  858. }
  859. if (pl != layout)
  860. {
  861. LAYOUT *pc;
  862. HDWP hdwp = BeginDeferWindowPos((INT)(pl - layout));
  863. for (pc = layout; pc < pl && hdwp; pc++)
  864. {
  865. hdwp = DeferWindowPos(hdwp, pc->hwnd, NULL, pc->x, pc->y, pc->cx, pc->cy, pc->flags);
  866. }
  867. if (hdwp) EndDeferWindowPos(hdwp);
  868. if (!rgn) rgn = CreateRectRgn(0, 0, 0, 0);
  869. if (fRedraw)
  870. {
  871. GetUpdateRgn(hwnd, rgn, FALSE);
  872. for (pc = layout; pc < pl && hdwp; pc++)
  873. {
  874. if (pc->rgn)
  875. {
  876. OffsetRgn(pc->rgn, pc->x, pc->y);
  877. CombineRgn(rgn, rgn, pc->rgn, RGN_OR);
  878. }
  879. }
  880. RedrawWindow(hwnd, NULL, rgn, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASENOW | RDW_ALLCHILDREN);
  881. }
  882. if (g_rgnUpdate)
  883. {
  884. GetUpdateRgn(hwnd, g_rgnUpdate, FALSE);
  885. for (pc = layout; pc < pl && hdwp; pc++)
  886. {
  887. if (pc->rgn)
  888. {
  889. OffsetRgn(pc->rgn, pc->x, pc->y);
  890. CombineRgn(g_rgnUpdate, g_rgnUpdate, pc->rgn, RGN_OR);
  891. }
  892. }
  893. }
  894. for (pc = layout; pc < pl && hdwp; pc++)
  895. if (pc->rgn) DeleteObject(pc->rgn);
  896. }
  897. if (rgn) DeleteObject(rgn);
  898. ValidateRgn(hwnd, NULL);
  899. }
  900. static void LayoutWindows2(HWND hwnd, BOOL fRedraw)
  901. {
  902. RECT rc, rg;
  903. HRGN rgn = NULL;
  904. GetClientRect(hwnd, &rc);
  905. SetRect(&rg, 0, 0, 0, 0);
  906. rc.top += WASABI_API_APP->getScaleY(2);
  907. rc.right -= WASABI_API_APP->getScaleY(2);
  908. if (rc.bottom <= rc.top || rc.right <= rc.left) return;
  909. HWND temp = GetDlgItem(hwnd, IDC_DB_ERROR);
  910. GetWindowRect(temp, &rg);
  911. SetWindowPos(temp, NULL, WASABI_API_APP->getScaleX(20), WASABI_API_APP->getScaleY(20),
  912. rc.right - rc.left - WASABI_API_APP->getScaleX(40),
  913. rc.bottom - rc.top - WASABI_API_APP->getScaleY(45),
  914. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOREDRAW);
  915. temp = GetDlgItem(hwnd, IDC_RESET_DB_ON_ERROR);
  916. GetWindowRect(temp, &rg);
  917. SetWindowPos(temp, NULL, ((rc.right - rc.left) - (rg.right - rg.left)) / WASABI_API_APP->getScaleX(2),
  918. rc.bottom - (rg.bottom - rg.top),
  919. rg.right - rg.left, rg.bottom - rg.top,
  920. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOREDRAW);
  921. InvalidateRect(hwnd, NULL, TRUE);
  922. if (fRedraw)
  923. {
  924. UpdateWindow(hwnd);
  925. }
  926. if (g_rgnUpdate)
  927. {
  928. GetUpdateRgn(hwnd, g_rgnUpdate, FALSE);
  929. if (rgn)
  930. {
  931. OffsetRgn(rgn, rc.left, rc.top);
  932. CombineRgn(g_rgnUpdate, g_rgnUpdate, rgn, RGN_OR);
  933. }
  934. }
  935. ValidateRgn(hwnd, NULL);
  936. if (rgn) DeleteObject(rgn);
  937. }
  938. static void history_ManageButtons(HWND hwndDlg)
  939. {
  940. int has_selection = resultlist.GetSelectedCount();
  941. const int buttonids[] = { IDC_BUTTON_PLAY, IDC_BUTTON_ENQUEUE, IDC_BUTTON_CUSTOM, IDC_REMOVEBOOK};
  942. for (size_t i = 0; i != sizeof(buttonids)/sizeof(buttonids[0]); i++)
  943. {
  944. HWND controlHWND = GetDlgItem(hwndDlg, buttonids[i]);
  945. EnableWindow(controlHWND, has_selection);
  946. }
  947. }
  948. void history_UpdateButtonText(HWND hwndDlg, int _enqueuedef)
  949. {
  950. if (groupBtn)
  951. {
  952. switch(_enqueuedef)
  953. {
  954. case 1:
  955. SetDlgItemTextW(hwndDlg, IDC_BUTTON_PLAY, view.enqueue);
  956. customAllowed = FALSE;
  957. break;
  958. default:
  959. // v5.66+ - re-use the old predixis parts so the button can be used functionally via ml_enqplay
  960. // pass the hwnd, button id and plug-in id so the ml plug-in can check things as needed
  961. pluginMessage p = {ML_MSG_VIEW_BUTTON_HOOK_IN_USE, (INT_PTR)_enqueuedef, 0, 0};
  962. wchar_t *pszTextW = (wchar_t *)SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_SEND_PLUGIN_MESSAGE, (WPARAM)&p);
  963. if (pszTextW && pszTextW[0] != 0)
  964. {
  965. // set this to be a bit different so we can just use one button and not the
  966. // mixable one as well (leaving that to prevent messing with the resources)
  967. SetDlgItemTextW(hwndDlg, IDC_BUTTON_PLAY, pszTextW);
  968. customAllowed = TRUE;
  969. }
  970. else
  971. {
  972. SetDlgItemTextW(hwndDlg, IDC_BUTTON_PLAY, view.play);
  973. customAllowed = FALSE;
  974. }
  975. break;
  976. }
  977. }
  978. }
  979. void UpdateMenuItems(HWND hwndDlg, HMENU menu)
  980. {
  981. bool swapPlayEnqueue=false;
  982. if (g_config->ReadInt(L"enqueuedef", 0) == 1)
  983. {
  984. SwapPlayEnqueueInMenu(menu);
  985. swapPlayEnqueue=true;
  986. }
  987. SyncMenuWithAccelerators(hwndDlg, menu);
  988. if (swapPlayEnqueue) SwapPlayEnqueueInMenu(menu);
  989. }
  990. enum
  991. {
  992. BPM_ECHO_WM_COMMAND=0x1, // send WM_COMMAND and return value
  993. BPM_WM_COMMAND = 0x2, // just send WM_COMMAND
  994. };
  995. BOOL history_ButtonPopupMenu(HWND hwndDlg, int buttonId, HMENU menu, int flags=0)
  996. {
  997. RECT r;
  998. HWND buttonHWND = GetDlgItem(hwndDlg, buttonId);
  999. GetWindowRect(buttonHWND, &r);
  1000. UpdateMenuItems(hwndDlg, menu);
  1001. MLSkinnedButton_SetDropDownState(buttonHWND, TRUE);
  1002. UINT tpmFlags = TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_BOTTOMALIGN | TPM_LEFTALIGN;
  1003. if (!(flags & BPM_WM_COMMAND))
  1004. tpmFlags |= TPM_RETURNCMD;
  1005. int x = Menu_TrackPopup(plugin.hwndLibraryParent, menu, tpmFlags, r.left, r.top, hwndDlg, NULL);
  1006. if ((flags & BPM_ECHO_WM_COMMAND) && x)
  1007. SendMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(x, 0), 0);
  1008. MLSkinnedButton_SetDropDownState(buttonHWND, FALSE);
  1009. return x;
  1010. }
  1011. static void history_Play(HWND hwndDlg, HWND from, UINT idFrom)
  1012. {
  1013. HMENU listMenu = GetSubMenu(g_context_menus2, 0);
  1014. int count = GetMenuItemCount(listMenu);
  1015. if (count > 2)
  1016. {
  1017. for (int i = 2; i < count; i++)
  1018. {
  1019. DeleteMenu(listMenu, 2, MF_BYPOSITION);
  1020. }
  1021. }
  1022. history_ButtonPopupMenu(hwndDlg, idFrom, listMenu, BPM_WM_COMMAND);
  1023. }
  1024. BOOL CALLBACK view_historyDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  1025. {
  1026. BOOL a = (BOOL)dialogSkinner.Handle(hwndDlg, uMsg, wParam, lParam); if (a) return a;
  1027. static HMENU sendto_hmenu;
  1028. static librarySendToMenuStruct s;
  1029. switch(uMsg)
  1030. {
  1031. case WM_INITMENUPOPUP:
  1032. if (wParam && (HMENU)wParam == s.build_hMenu && s.mode==1)
  1033. {
  1034. if (SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_LIBRARY_SENDTOMENU)==0xffffffff)
  1035. s.mode=2;
  1036. }
  1037. return 0;
  1038. case WM_DISPLAYCHANGE:
  1039. {
  1040. ListView_SetTextColor(resultlist.getwnd(), dialogSkinner.Color(WADLG_ITEMFG));
  1041. ListView_SetBkColor(resultlist.getwnd(), dialogSkinner.Color(WADLG_ITEMBG));
  1042. ListView_SetTextBkColor(resultlist.getwnd(), dialogSkinner.Color(WADLG_ITEMBG));
  1043. resultlist.SetFont(dialogSkinner.GetFont());
  1044. UpdateWindow(hwndDlg);
  1045. LayoutWindows(hwndDlg, TRUE);
  1046. return 0;
  1047. }
  1048. case WM_CONTEXTMENU:
  1049. {
  1050. HWND hwndFrom = (HWND)wParam;
  1051. if (hwndFrom != resultlist.getwnd())
  1052. return 0;
  1053. int x = GET_X_LPARAM(lParam), y = GET_Y_LPARAM(lParam);
  1054. POINT pt = {x, y};
  1055. if (x == -1 || y == -1) // x and y are -1 if the user invoked a shift-f10 popup menu
  1056. {
  1057. RECT itemRect = {0};
  1058. int selected = resultlist.GetNextSelected();
  1059. if (selected != -1) // if something is selected we'll drop the menu from there
  1060. {
  1061. resultlist.GetItemRect(selected, &itemRect);
  1062. ClientToScreen(resultlist.getwnd(), (POINT *)&itemRect);
  1063. }
  1064. else // otherwise we'll drop it from the top-left corner of the listview, adjusting for the header location
  1065. {
  1066. GetWindowRect(resultlist.getwnd(), &itemRect);
  1067. HWND hHeader = (HWND)SNDMSG(hwndFrom, LVM_GETHEADER, 0, 0L);
  1068. RECT headerRect;
  1069. if ((WS_VISIBLE & GetWindowLongPtr(hHeader, GWL_STYLE)) && GetWindowRect(hHeader, &headerRect))
  1070. {
  1071. itemRect.top += (headerRect.bottom - headerRect.top);
  1072. }
  1073. }
  1074. x = itemRect.left;
  1075. y = itemRect.top;
  1076. }
  1077. HWND hHeader = (HWND)SNDMSG(hwndFrom, LVM_GETHEADER, 0, 0L);
  1078. RECT headerRect;
  1079. if (0 == (WS_VISIBLE & GetWindowLongPtr(hHeader, GWL_STYLE)) || FALSE == GetWindowRect(hHeader, &headerRect))
  1080. {
  1081. SetRectEmpty(&headerRect);
  1082. }
  1083. if (FALSE != PtInRect(&headerRect, pt))
  1084. {
  1085. return 0;
  1086. }
  1087. HMENU g_context_menus=WASABI_API_LOADMENU(IDR_CONTEXTMENUS);
  1088. HMENU menu=GetSubMenu(g_context_menus,0);
  1089. sendto_hmenu=GetSubMenu(menu,2);
  1090. UpdateMenuItems(hwndDlg, menu);
  1091. s.mode = 0;
  1092. s.hwnd = 0;
  1093. s.build_hMenu = 0;
  1094. IPC_LIBRARY_SENDTOMENU = (INT_PTR)SendMessage(plugin.hwndWinampParent, WM_WA_IPC,(WPARAM)&"LibrarySendToMenu",IPC_REGISTER_WINAMP_IPCMESSAGE);
  1095. if (IPC_LIBRARY_SENDTOMENU > 65536 && SendMessage(plugin.hwndWinampParent, WM_WA_IPC,(WPARAM)0,IPC_LIBRARY_SENDTOMENU)==0xffffffff)
  1096. {
  1097. s.mode = 1;
  1098. s.hwnd = hwndDlg;
  1099. s.data_type = ML_TYPE_FILENAMESW;
  1100. s.ctx[1] = 1;
  1101. s.build_hMenu = sendto_hmenu;
  1102. }
  1103. UINT menustate = 0;
  1104. int n=resultlist.GetSelectedCount();
  1105. if(n == 0)
  1106. {
  1107. menustate = MF_BYCOMMAND|MF_GRAYED;
  1108. }
  1109. else
  1110. {
  1111. menustate = MF_BYCOMMAND|MF_ENABLED;
  1112. }
  1113. EnableMenuItem(menu,ID_PE_ID3,menustate);
  1114. EnableMenuItem(menu,ID_MEDIAWND_PLAYSELECTEDFILES,menustate);
  1115. EnableMenuItem(menu,ID_MEDIAWND_ENQUEUESELECTEDFILES,menustate);
  1116. EnableMenuItem(menu,ID_MEDIAWND_EXPLOREFOLDER,menustate);
  1117. EnableMenuItem(menu,ID_MEDIAWND_REMOVEFROMLIBRARY,menustate);
  1118. EnableMenuItem(menu,ID_MEDIAWND_REMOVEOFFSETFROMLIBRARY,menustate);
  1119. EnableMenuItem(menu, 2, MF_BYPOSITION|(n==0?MF_GRAYED:MF_ENABLED));
  1120. int r = Menu_TrackPopup(plugin.hwndLibraryParent, menu, TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTBUTTON, x, y, hwndDlg, NULL);
  1121. if(!SendMessage(hwndDlg,WM_COMMAND,r,0))
  1122. {
  1123. if (s.mode == 2)
  1124. {
  1125. s.menu_id = r;
  1126. if (SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_LIBRARY_SENDTOMENU) == 0xffffffff)
  1127. {
  1128. // build my data.
  1129. s.mode=3;
  1130. s.data_type=ML_TYPE_FILENAMESW;
  1131. //std::vector<wchar_t> sendStr;
  1132. std::wstring sendStr;
  1133. int l=resultlist.GetCount();
  1134. for(int i=0;i<l;i++)
  1135. {
  1136. if (resultlist.GetSelected(i))
  1137. {
  1138. // HAKAN: why (len + 1) ?
  1139. //sendStr.append(itemCache.Items[i].filename, wcslen(itemCache.Items[i].filename)+1);
  1140. sendStr.append(itemCache.Items[i].filename, wcslen(itemCache.Items[i].filename));
  1141. }
  1142. }
  1143. // HAKAN: No need to add trailing zero
  1144. //sendStr.push_back(0);
  1145. s.data = (void*)sendStr.c_str();
  1146. if(SendMessage(plugin.hwndWinampParent, WM_WA_IPC,(WPARAM)&s,IPC_LIBRARY_SENDTOMENU)!=1)
  1147. {
  1148. s.mode=3;
  1149. s.data_type=ML_TYPE_FILENAMES;
  1150. //std::vector<char> sendStrA;
  1151. std::string sendStrA;
  1152. int l=resultlist.GetCount();
  1153. for(int i=0;i<l;i++)
  1154. {
  1155. if (resultlist.GetSelected(i))
  1156. {
  1157. // HAKAN: why (len + 1) ?
  1158. //sendStrA.append(AutoCharFn(itemCache.Items[i].filename), strlen(AutoCharFn(itemCache.Items[i].filename))+1);
  1159. sendStrA.append(AutoCharFn(itemCache.Items[i].filename), strlen(AutoCharFn(itemCache.Items[i].filename)));
  1160. }
  1161. }
  1162. // HAKAN: No need to add trailing zero
  1163. //sendStrA.push_back(0);
  1164. s.data = (void*)sendStrA.c_str();
  1165. SendMessage(plugin.hwndWinampParent, WM_WA_IPC,(WPARAM)&s,IPC_LIBRARY_SENDTOMENU);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. if (s.mode)
  1171. {
  1172. s.mode=4;
  1173. SendMessage(plugin.hwndWinampParent, WM_WA_IPC,(WPARAM)&s,IPC_LIBRARY_SENDTOMENU); // cleanup
  1174. }
  1175. sendto_hmenu=0;
  1176. DestroyMenu(g_context_menus);
  1177. Sleep(100);
  1178. MSG msg = {0};
  1179. while(PeekMessage(&msg,NULL,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE)); //eat return
  1180. }
  1181. return 0;
  1182. case WM_INITDIALOG:
  1183. {
  1184. m_hwnd=hwndDlg;
  1185. g_q.Set(L"");
  1186. HACCEL accel = WASABI_API_LOADACCELERATORSW(IDR_VIEW_ACCELERATORS);
  1187. if (accel)
  1188. WASABI_API_APP->app_addAccelerators(hwndDlg, &accel, 1, TRANSLATE_MODE_CHILD);
  1189. if (!view.play)
  1190. {
  1191. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_GET_VIEW_BUTTON_TEXT, (WPARAM)&view);
  1192. }
  1193. history_cleanupifnecessary();
  1194. itemCache.Items=0;
  1195. itemCache.Alloc=0;
  1196. itemCache.Size=0;
  1197. resultlist.setwnd(GetDlgItem(hwndDlg,IDC_LIST2));
  1198. resultlist.ForceUnicode();
  1199. resultSkin = (int)(INT_PTR)resultlist.getwnd(); //Might be unsafe
  1200. groupBtn = g_config->ReadInt(L"groupbtn", 1);
  1201. enqueuedef = (g_config->ReadInt(L"enqueuedef", 0) == 1);
  1202. // v5.66+ - re-use the old predixis parts so the button can be used functionally via ml_enqplay
  1203. // pass the hwnd, button id and plug-in id so the ml plug-in can check things as needed
  1204. pluginMessage p = {ML_MSG_VIEW_BUTTON_HOOK, (INT_PTR)hwndDlg, (INT_PTR)MAKELONG(IDC_BUTTON_CUSTOM, IDC_BUTTON_ENQUEUE), (INT_PTR)L"ml_history"};
  1205. wchar_t *pszTextW = (wchar_t *)SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_SEND_PLUGIN_MESSAGE, (WPARAM)&p);
  1206. if (pszTextW && pszTextW[0] != 0)
  1207. {
  1208. // set this to be a bit different so we can just use one button and not the
  1209. // mixable one as well (leaving that to prevent messing with the resources)
  1210. customAllowed = TRUE;
  1211. SetDlgItemTextW(hwndDlg, IDC_BUTTON_CUSTOM, pszTextW);
  1212. }
  1213. else
  1214. customAllowed = FALSE;
  1215. MLSKINWINDOW m = {0};
  1216. m.skinType = SKINNEDWND_TYPE_LISTVIEW;
  1217. m.style = SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS | SWLVS_FULLROWSELECT | SWLVS_DOUBLEBUFFER | SWLVS_ALTERNATEITEMS;
  1218. m.hwndToSkin = resultlist.getwnd();
  1219. MLSkinWindow(mediaLibrary.library, &m);
  1220. m.skinType = SKINNEDWND_TYPE_BUTTON;
  1221. m.style = SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS | (groupBtn ? SWBS_SPLITBUTTON : 0);
  1222. FLICKERFIX ff = {0, FFM_ERASEINPAINT};
  1223. const int buttonids[] = {IDC_BUTTON_PLAY, IDC_BUTTON_ENQUEUE, IDC_BUTTON_CUSTOM};
  1224. for (size_t i=0;i!=sizeof(buttonids)/sizeof(buttonids[0]);i++)
  1225. {
  1226. m.hwndToSkin = ff.hwnd = GetDlgItem(hwndDlg, buttonids[i]);
  1227. if (IsWindow(m.hwndToSkin))
  1228. {
  1229. MLSkinWindow(plugin.hwndLibraryParent, &m);
  1230. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_FLICKERFIX, (WPARAM)&ff);
  1231. }
  1232. }
  1233. INT ffcl[] = {IDC_REMOVEBOOK, IDC_CLEAR, IDC_MEDIASTATUS, IDC_QUICKSEARCH, IDC_SEARCHCAPTION};
  1234. m.skinType = SKINNEDWND_TYPE_AUTO;
  1235. m.style = SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS;
  1236. for (INT index = 0; index < sizeof(ffcl) / sizeof(INT); index++)
  1237. {
  1238. ff.hwnd = GetDlgItem(hwndDlg, ffcl[index]);
  1239. if (IsWindow(ff.hwnd))
  1240. {
  1241. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_FLICKERFIX, (WPARAM)&ff);
  1242. m.hwndToSkin = ff.hwnd;
  1243. MLSkinWindow(plugin.hwndLibraryParent, &m);
  1244. }
  1245. }
  1246. ListView_SetTextColor(resultlist.getwnd(),dialogSkinner.Color(WADLG_ITEMFG));
  1247. ListView_SetBkColor(resultlist.getwnd(), dialogSkinner.Color(WADLG_ITEMBG));
  1248. ListView_SetTextBkColor(resultlist.getwnd(),dialogSkinner.Color(WADLG_ITEMBG));
  1249. resultlist.SetFont(dialogSkinner.GetFont());
  1250. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_LAST_PLAYED), g_config->ReadInt(L"recent_col_lp", 111));
  1251. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_PLAY_COUNT), g_config->ReadInt(L"recent_col_count", 70));
  1252. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_TITLE), g_config->ReadInt(L"recent_col_title", 238));
  1253. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_LENGTH), g_config->ReadInt(L"recent_col_len", 50));
  1254. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_FILENAME), g_config->ReadInt(L"recent_col_filename", 285));
  1255. resultlist.AddCol(WASABI_API_LNGSTRINGW(IDS_COL_OFFSET), g_config->ReadInt(L"recent_col_offset", 80));
  1256. m_headerhwnd=ListView_GetHeader(resultlist.getwnd());
  1257. {
  1258. char *query="";
  1259. if (g_config->ReadInt(L"remembersearch",0)) query = g_config->ReadString("recent_lastquery", "");
  1260. AutoWide queryUnicode(query, CP_UTF8);
  1261. SetDlgItemTextW(hwndDlg,IDC_QUICKSEARCH,queryUnicode);
  1262. KillTimer(hwndDlg,UPDATE_QUERY_TIMER_ID);
  1263. doQuery(hwndDlg,queryUnicode,0);
  1264. }
  1265. {
  1266. int l_sc=g_config->ReadInt(L"recent_sort_by", HISTORY_SORT_LASTPLAYED);
  1267. int l_sd=g_config->ReadInt(L"recent_sort_dir", 0);
  1268. mediaLibrary.ListViewSort(resultSkin, l_sc, l_sd);
  1269. mediaLibrary.ListViewShowSort(resultSkin, TRUE);
  1270. }
  1271. history_ManageButtons(hwndDlg);
  1272. history_UpdateButtonText(hwndDlg, enqueuedef == 1);
  1273. search_oldWndProc = (WNDPROC)(LONG_PTR)SetWindowLongPtrW(GetDlgItem(hwndDlg,IDC_QUICKSEARCH),GWLP_WNDPROC,(LONG_PTR)search_newWndProc);
  1274. break;
  1275. }
  1276. case WM_WINDOWPOSCHANGED:
  1277. if ((SWP_NOSIZE | SWP_NOMOVE) != ((SWP_NOSIZE | SWP_NOMOVE) & ((WINDOWPOS*)lParam)->flags) ||
  1278. (SWP_FRAMECHANGED & ((WINDOWPOS*)lParam)->flags))
  1279. {
  1280. LayoutWindows(hwndDlg, !(SWP_NOREDRAW & ((WINDOWPOS*)lParam)->flags));
  1281. }
  1282. return 0;
  1283. case WM_USER + 0x201:
  1284. offsetX = (short)LOWORD(wParam);
  1285. offsetY = (short)HIWORD(wParam);
  1286. g_rgnUpdate = (HRGN)lParam;
  1287. return TRUE;
  1288. case WM_MOUSEMOVE:
  1289. if (GetCapture()==hwndDlg)
  1290. {
  1291. POINT p;
  1292. p.x=GET_X_LPARAM(lParam);
  1293. p.y=GET_Y_LPARAM(lParam);
  1294. ClientToScreen(hwndDlg,&p);
  1295. mlDropItemStruct m={0};
  1296. m.type=ML_TYPE_FILENAMESW;
  1297. m.p=p;
  1298. pluginHandleIpcMessage(ML_IPC_HANDLEDRAG,(WPARAM)&m);
  1299. break;
  1300. }
  1301. break;
  1302. case WM_LBUTTONUP:
  1303. if (GetCapture()==hwndDlg)
  1304. {
  1305. ReleaseCapture();
  1306. POINT p;
  1307. p.x=GET_X_LPARAM(lParam);
  1308. p.y=GET_Y_LPARAM(lParam);
  1309. ClientToScreen(hwndDlg,&p);
  1310. mlDropItemStruct m={0};
  1311. m.type=ML_TYPE_FILENAMESW;
  1312. m.p=p;
  1313. m.flags=ML_HANDLEDRAG_FLAG_NOCURSOR;
  1314. pluginHandleIpcMessage(ML_IPC_HANDLEDRAG,(WPARAM)&m);
  1315. if (m.result>0)
  1316. {
  1317. size_t buf_size=4096;
  1318. wchar_t *buf=(wchar_t*)calloc(buf_size, sizeof(wchar_t));
  1319. int buf_pos=0;
  1320. int l=resultlist.GetCount();
  1321. for(int i=0;i<l;i++)
  1322. {
  1323. if (resultlist.GetSelected(i))
  1324. {
  1325. const wchar_t *tbuf = itemCache.Items[i].filename;
  1326. size_t cchFilename = wcslen(tbuf) + 1;
  1327. size_t newsize=buf_pos + cchFilename;
  1328. if (newsize < buf_size)
  1329. {
  1330. size_t old_buf_size = buf_size;
  1331. buf_size=newsize+4096;
  1332. wchar_t *reallocated_buf = (wchar_t*)realloc(buf,buf_size*sizeof(wchar_t));
  1333. if (reallocated_buf)
  1334. {
  1335. buf = reallocated_buf;
  1336. }
  1337. else
  1338. {
  1339. wchar_t *newbuf = (wchar_t*)malloc(buf_size*sizeof(wchar_t));
  1340. if (!newbuf) // out of memory? well we can at least send what we've got
  1341. break;
  1342. memcpy(newbuf, buf, old_buf_size);
  1343. free(buf);
  1344. buf = newbuf;
  1345. }
  1346. }
  1347. StringCchCopyNW(buf+buf_pos,buf_size-buf_pos,tbuf,cchFilename);
  1348. buf_pos=(int)newsize;
  1349. }
  1350. }
  1351. if (buf_pos)
  1352. {
  1353. buf[buf_pos]=0;
  1354. m.flags=0;
  1355. m.result=0;
  1356. m.data=(void*)buf;
  1357. pluginHandleIpcMessage(ML_IPC_HANDLEDROP,(WPARAM)&m);
  1358. }
  1359. free(buf);
  1360. }
  1361. }
  1362. break;
  1363. case WM_COMMAND:
  1364. if (GetFocus() != GetDlgItem(hwndDlg, IDC_QUICKSEARCH))
  1365. {
  1366. switch(LOWORD(wParam))
  1367. {
  1368. case IDC_REMOVEBOOK:
  1369. removeSelectedItems(0);
  1370. break;
  1371. case IDC_CLEAR:
  1372. SetDlgItemText(hwndDlg,IDC_QUICKSEARCH,TEXT(""));
  1373. break;
  1374. case IDC_QUICKSEARCH:
  1375. if (HIWORD(wParam) == EN_CHANGE)
  1376. {
  1377. KillTimer(hwndDlg,UPDATE_QUERY_TIMER_ID);
  1378. SetTimer(hwndDlg,UPDATE_QUERY_TIMER_ID,100,NULL);
  1379. }
  1380. break;
  1381. case IDC_BUTTON_PLAY:
  1382. case ID_MEDIAWND_PLAYSELECTEDFILES:
  1383. case IDC_BUTTON_ENQUEUE:
  1384. case ID_MEDIAWND_ENQUEUESELECTEDFILES:
  1385. case IDC_BUTTON_CUSTOM:
  1386. {
  1387. if (HIWORD(wParam) == MLBN_DROPDOWN)
  1388. {
  1389. history_Play(hwndDlg, (HWND)lParam, LOWORD(wParam));
  1390. }
  1391. else
  1392. {
  1393. int action;
  1394. if (LOWORD(wParam) == IDC_BUTTON_PLAY || LOWORD(wParam) == ID_MEDIAWND_PLAYSELECTEDFILES)
  1395. {
  1396. action = (HIWORD(wParam) == 1) ? g_config->ReadInt(L"enqueuedef", 0) == 1 : 0;
  1397. }
  1398. else if (LOWORD(wParam) == IDC_BUTTON_ENQUEUE || LOWORD(wParam) == ID_MEDIAWND_ENQUEUESELECTEDFILES)
  1399. {
  1400. action = (HIWORD(wParam) == 1) ? g_config->ReadInt(L"enqueuedef", 0) != 1 : 1;
  1401. }
  1402. else
  1403. break;
  1404. playFiles(action, 0);
  1405. }
  1406. break;
  1407. }
  1408. case ID_MEDIAWND_SELECTALL:
  1409. ListView_SetItemState(resultlist.getwnd(), -1, LVIS_SELECTED, LVIS_SELECTED);
  1410. break;
  1411. case ID_MEDIAWND_REMOVEFROMLIBRARY:
  1412. removeSelectedItems(0);
  1413. break;
  1414. case ID_MEDIAWND_REMOVEOFFSETFROMLIBRARY:
  1415. removeSelectedItemOffsets(0);
  1416. break;
  1417. case ID_MEDIAWND_EXPLOREFOLDER:
  1418. exploreItemFolder(hwndDlg);
  1419. break;
  1420. case ID_PE_ID3:
  1421. fileInfoDialogs(hwndDlg);
  1422. break;
  1423. }
  1424. }
  1425. else
  1426. {
  1427. switch(LOWORD(wParam))
  1428. {
  1429. case IDC_QUICKSEARCH:
  1430. if (HIWORD(wParam) == EN_CHANGE)
  1431. {
  1432. KillTimer(hwndDlg,UPDATE_QUERY_TIMER_ID);
  1433. SetTimer(hwndDlg,UPDATE_QUERY_TIMER_ID,100,NULL);
  1434. }
  1435. break;
  1436. case ID_MEDIAWND_SELECTALL:
  1437. SendDlgItemMessageW(hwndDlg, IDC_QUICKSEARCH, EM_SETSEL, 0, -1);
  1438. break;
  1439. case ID_MEDIAWND_REMOVEFROMLIBRARY:
  1440. {
  1441. DWORD start = -1, end = -1;
  1442. SendDlgItemMessageW(hwndDlg, IDC_QUICKSEARCH, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  1443. if (start != -1)
  1444. {
  1445. if (start == end)
  1446. {
  1447. SendDlgItemMessageW(hwndDlg, IDC_QUICKSEARCH, EM_SETSEL, start, end + 1);
  1448. }
  1449. SendDlgItemMessageW(hwndDlg, IDC_QUICKSEARCH, EM_REPLACESEL, TRUE, (LPARAM)L"");
  1450. SendDlgItemMessageW(hwndDlg, IDC_QUICKSEARCH, EM_SETSEL, start, start);
  1451. }
  1452. }
  1453. break;
  1454. }
  1455. }
  1456. break;
  1457. case WM_TIMER:
  1458. if (wParam == 123)
  1459. {
  1460. if (history_bgThread_Handle)
  1461. {
  1462. ListView_SetItemCount(resultlist.getwnd(),1);
  1463. ListView_RedrawItems(resultlist.getwnd(),0,0);
  1464. }
  1465. }
  1466. else if (wParam == UPDATE_QUERY_TIMER_ID)
  1467. {
  1468. KillTimer(hwndDlg,UPDATE_QUERY_TIMER_ID);
  1469. wchar_t text[512] = {0};
  1470. GetWindowTextW(GetDlgItem(hwndDlg,IDC_QUICKSEARCH),text,511-1);
  1471. text[511]=0;
  1472. doQuery(hwndDlg,text);
  1473. }
  1474. return 0;
  1475. case WM_APP+3: // sent by bgthread
  1476. if (wParam == 0x69)
  1477. {
  1478. history_bgQuery_Stop();
  1479. ListView_SetItemCount(resultlist.getwnd(),0);
  1480. ListView_SetItemCount(resultlist.getwnd(),itemCache.Size);
  1481. if (itemCache.Size>0) ListView_RedrawItems(resultlist.getwnd(),0,itemCache.Size-1);
  1482. if (m_lv_last_topidx)
  1483. {
  1484. ListView_EnsureVisible(resultlist.getwnd(),m_lv_last_topidx,FALSE);
  1485. m_lv_last_topidx=0;
  1486. }
  1487. unsigned int total_plays=0;
  1488. int x;
  1489. for (x = 0; x < itemCache.Size; x ++)
  1490. {
  1491. total_plays += itemCache.Items[x].playcnt;
  1492. }
  1493. int total_length_s = (int)lParam & 0x7FFFFFFF;
  1494. int uncert=(int)(lParam>>31);
  1495. wchar_t buf[1024] = {0}, itemStr[16] = {0}, playStr[16] = {0};
  1496. StringCchPrintfW(buf, 1024,
  1497. L"%d %s, %u %s ",
  1498. itemCache.Size,
  1499. WASABI_API_LNGSTRINGW_BUF(itemCache.Size==1?IDS_ITEM:IDS_ITEMS,itemStr,16),
  1500. total_plays,
  1501. WASABI_API_LNGSTRINGW_BUF(total_plays==1?IDS_PLAY:IDS_PLAYS,playStr,16));
  1502. if (total_length_s < 60*60)
  1503. {
  1504. StringCchPrintfW(buf+wcslen(buf), 64, L"[%s%u:%02u]",
  1505. uncert ? L"~" : L"", total_length_s / 60,
  1506. total_length_s % 60);
  1507. }
  1508. else if (total_length_s < 60*60*24)
  1509. {
  1510. StringCchPrintfW(buf+wcslen(buf), 64, L"[%s%u:%02u:%02u]",
  1511. uncert ? L"~" : L"", total_length_s / 60 / 60,
  1512. (total_length_s / 60) % 60, total_length_s % 60);
  1513. }
  1514. else
  1515. {
  1516. wchar_t days[16] = {0};
  1517. int total_days = total_length_s / (60 * 60 * 24);
  1518. total_length_s -= total_days * 60 * 60 * 24;
  1519. StringCchPrintfW(buf+wcslen(buf), 64,
  1520. L"[%s%u %s+%u:%02u:%02u]",
  1521. uncert ? L"~" : L"", total_days,
  1522. WASABI_API_LNGSTRINGW_BUF(total_days == 1 ? IDS_DAY : IDS_DAYS, days, 16),
  1523. total_length_s / 60 / 60, (total_length_s / 60) % 60, total_length_s % 60);
  1524. }
  1525. SetDlgItemTextW(hwndDlg,IDC_MEDIASTATUS,buf);
  1526. }
  1527. break;
  1528. case WM_DESTROY:
  1529. if (resultlist.getwnd())
  1530. {
  1531. g_config->WriteInt(L"recent_col_lp", resultlist.GetColumnWidth(0));
  1532. g_config->WriteInt(L"recent_col_count", resultlist.GetColumnWidth(1));
  1533. g_config->WriteInt(L"recent_col_title", resultlist.GetColumnWidth(2));
  1534. g_config->WriteInt(L"recent_col_len", resultlist.GetColumnWidth(3));
  1535. g_config->WriteInt(L"recent_col_filename", resultlist.GetColumnWidth(4));
  1536. g_config->WriteInt(L"recent_col_offset", resultlist.GetColumnWidth(5));
  1537. }
  1538. History_SaveLastQuery(hwndDlg);
  1539. if (g_table_dirty && g_table)
  1540. {
  1541. EnterCriticalSection(&g_db_cs);
  1542. NDE_Table_Sync(g_table);
  1543. g_table_dirty=0;
  1544. LeaveCriticalSection(&g_db_cs);
  1545. }
  1546. emptyRecentRecordList(&itemCache);
  1547. free(itemCache.Items);
  1548. itemCache.Items=0;
  1549. itemCache.Alloc=0;
  1550. itemCache.Size=0;
  1551. m_hwnd=0;
  1552. g_q.Set(L"");
  1553. WASABI_API_APP->app_removeAccelerators(hwndDlg);
  1554. break;
  1555. case WM_PAINT:
  1556. {
  1557. int tab[] = {IDC_QUICKSEARCH|DCW_SUNKENBORDER, IDC_LIST2|DCW_SUNKENBORDER};
  1558. dialogSkinner.Draw(hwndDlg, tab, 1 + !!IsWindowVisible(GetDlgItem(hwndDlg, IDC_QUICKSEARCH)));
  1559. }
  1560. return 0;
  1561. case WM_APP+1:
  1562. history_bgQuery((int)lParam);
  1563. break;
  1564. case WM_ML_CHILDIPC:
  1565. if(lParam == ML_CHILDIPC_GO_TO_SEARCHBAR)
  1566. {
  1567. SendDlgItemMessage(hwndDlg, IDC_QUICKSEARCH, EM_SETSEL, 0, -1);
  1568. SetFocus(GetDlgItem(hwndDlg,IDC_QUICKSEARCH));
  1569. }
  1570. else if (lParam == ML_CHILDIPC_REFRESH_SEARCH)
  1571. {
  1572. PostMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDC_QUICKSEARCH, EN_CHANGE), (LPARAM)GetDlgItem(hwndDlg, IDC_QUICKSEARCH));
  1573. }
  1574. break;
  1575. case WM_ERASEBKGND:
  1576. return 1; //handled by WADlg_DrawChildWindowBorders in WM_PAINT
  1577. case WM_NOTIFY:
  1578. {
  1579. LPNMHDR l=(LPNMHDR)lParam;
  1580. if (l->idFrom==IDC_LIST2) // media view
  1581. {
  1582. if (l->code == NM_DBLCLK)
  1583. {
  1584. playFiles((!!g_config->ReadInt(L"enqueuedef", 0)) ^ (!!(GetAsyncKeyState(VK_SHIFT)&0x8000)),0);
  1585. }
  1586. else if (l->code == LVN_ODFINDITEMW) // yay we find an item (for kb shortcuts)
  1587. {
  1588. if (history_bgThread_Handle) return 0;
  1589. NMLVFINDITEMW *t = (NMLVFINDITEMW *)lParam;
  1590. int i=t->iStart;
  1591. if (i >= itemCache.Size) i=0;
  1592. int cnt=itemCache.Size-i;
  1593. if (t->lvfi.flags & LVFI_WRAP) cnt+=i;
  1594. int by = g_config->ReadInt(L"recent_sort_by", HISTORY_SORT_LASTPLAYED);
  1595. while (cnt-->0)
  1596. {
  1597. historyRecord *thisitem=itemCache.Items + i;
  1598. wchar_t tmp[128] = {0};
  1599. const wchar_t *name=0;
  1600. switch (by)
  1601. {
  1602. case HISTORYVIEW_COL_FILENAME:
  1603. name=thisitem->filename;
  1604. if (!wcsstr(name,L"://"))
  1605. {
  1606. while (name && *name) name++;
  1607. while (name && name >= thisitem->filename && *name != '/' && *name != '\\') name--;
  1608. if (name) name++;
  1609. }
  1610. break;
  1611. case HISTORYVIEW_COL_TITLE: name=thisitem->title; break;
  1612. case HISTORYVIEW_COL_LASTPLAYED:
  1613. tmp[0]=0;
  1614. if (thisitem->lastplayed > 0)
  1615. {
  1616. __time64_t timev = thisitem->lastplayed;
  1617. MakeDateStringW(timev, tmp, ARRAYSIZE(tmp));
  1618. }
  1619. name=tmp;
  1620. break;
  1621. case HISTORYVIEW_COL_PLAYCOUNT:
  1622. StringCchPrintfW(tmp,128,L"%u",thisitem->playcnt);
  1623. name=tmp;
  1624. break;
  1625. case HISTORYVIEW_COL_LENGTH:
  1626. tmp[0]=0;
  1627. if (thisitem->length >= 0) StringCchPrintfW(tmp,128,L"%d:%02d",thisitem->length/60,thisitem->length%60);
  1628. name=tmp;
  1629. break;
  1630. case HISTORYVIEW_COL_OFFSET:
  1631. tmp[0]=0;
  1632. if (thisitem->offset > 0) StringCchPrintfW(tmp,128,L"%d:%02d",(thisitem->offset/1000)/60,(thisitem->offset/1000)%60);
  1633. name=tmp;
  1634. break;
  1635. }
  1636. if (!name) name=L"";
  1637. else SKIP_THE_AND_WHITESPACE(name)
  1638. if (t->lvfi.flags & (4|LVFI_PARTIAL))
  1639. {
  1640. if (!_wcsnicmp(name,t->lvfi.psz,lstrlenW(t->lvfi.psz)))
  1641. {
  1642. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,i);
  1643. return 1;
  1644. }
  1645. }
  1646. else if (t->lvfi.flags & LVFI_STRING)
  1647. {
  1648. if (!_wcsicmp(name,t->lvfi.psz))
  1649. {
  1650. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,i);
  1651. return 1;
  1652. }
  1653. }
  1654. else
  1655. {
  1656. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,-1);
  1657. return 1;
  1658. }
  1659. if (++i == itemCache.Size) i=0;
  1660. }
  1661. SetWindowLongPtr(hwndDlg,DWLP_MSGRESULT,-1);
  1662. return 1;
  1663. }
  1664. else if (l->code == LVN_GETDISPINFOW)
  1665. {
  1666. NMLVDISPINFOW *lpdi = (NMLVDISPINFOW*) lParam;
  1667. int item=lpdi->item.iItem;
  1668. if (history_bgThread_Handle)
  1669. {
  1670. if (!item && lpdi->item.iSubItem == 0 && lpdi->item.mask & LVIF_TEXT)
  1671. {
  1672. static char bufpos;
  1673. static char chars[4]={'/','-','\\','|'};
  1674. StringCchPrintfW(lpdi->item.pszText,lpdi->item.cchTextMax,
  1675. L"%s %c",WASABI_API_LNGSTRINGW(IDS_SCANNING),
  1676. chars[bufpos++&3]);
  1677. return 0;
  1678. }
  1679. }
  1680. if (item < 0 || item >= itemCache.Size) return 0;
  1681. historyRecord *thisitem = itemCache.Items + item;
  1682. if (lpdi->item.mask & (LVIF_TEXT|/*LVIF_IMAGE*/0)) // we can always do images too :)
  1683. {
  1684. if (lpdi->item.mask & LVIF_TEXT)
  1685. {
  1686. wchar_t tmpbuf[128] = {0};
  1687. const wchar_t *nameptr=0;
  1688. switch (lpdi->item.iSubItem)
  1689. {
  1690. case HISTORYVIEW_COL_FILENAME:
  1691. nameptr=thisitem->filename;
  1692. if (!wcsstr(nameptr,L"://"))
  1693. {
  1694. while (nameptr && *nameptr) nameptr++;
  1695. while (nameptr && nameptr >= thisitem->filename && *nameptr != L'/' && *nameptr != L'\\') nameptr--;
  1696. if (nameptr) nameptr++;
  1697. }
  1698. break;
  1699. case HISTORYVIEW_COL_TITLE: nameptr=thisitem->title; break;
  1700. case HISTORYVIEW_COL_LASTPLAYED:
  1701. if (thisitem->lastplayed > 0)
  1702. {
  1703. __time64_t timev = thisitem->lastplayed;
  1704. MakeDateStringW(timev, tmpbuf, ARRAYSIZE(tmpbuf));
  1705. }
  1706. nameptr=tmpbuf;
  1707. break;
  1708. case HISTORYVIEW_COL_PLAYCOUNT:
  1709. StringCchPrintfW(tmpbuf,128,L"%u",thisitem->playcnt);
  1710. nameptr=tmpbuf;
  1711. break;
  1712. case HISTORYVIEW_COL_LENGTH:
  1713. if (thisitem->length >= 0)
  1714. StringCchPrintfW(tmpbuf,128,L"%d:%02d",thisitem->length/60,thisitem->length%60);
  1715. nameptr=tmpbuf;
  1716. break;
  1717. case HISTORYVIEW_COL_OFFSET:
  1718. if (thisitem->offset > 0)
  1719. StringCchPrintfW(tmpbuf,128,L"%d:%02d",(thisitem->offset/1000)/60,(thisitem->offset/1000)%60);
  1720. nameptr=tmpbuf;
  1721. break;
  1722. }
  1723. if (nameptr) lstrcpynW(lpdi->item.pszText,nameptr,lpdi->item.cchTextMax);
  1724. else lpdi->item.pszText[0]=0;
  1725. }
  1726. // if(lpdi->item.mask & LVIF_IMAGE)
  1727. } // bother
  1728. return 0;
  1729. } // LVN_GETDISPINFO
  1730. else if (l->code == LVN_COLUMNCLICK)
  1731. {
  1732. NMLISTVIEW *p=(NMLISTVIEW*)lParam;
  1733. int l_sc=g_config->ReadInt(L"recent_sort_by",HISTORY_SORT_LASTPLAYED);
  1734. int l_sd=g_config->ReadInt(L"recent_sort_dir",0);
  1735. if (p->iSubItem == l_sc) l_sd=!l_sd;
  1736. else { l_sd=0; l_sc=p->iSubItem; }
  1737. g_config->WriteInt(L"recent_sort_by",l_sc);
  1738. g_config->WriteInt(L"recent_sort_dir",l_sd);
  1739. mediaLibrary.ListViewSort(resultSkin, l_sc, l_sd);
  1740. sortResults(&itemCache,
  1741. g_config->ReadInt(L"recent_sort_by",HISTORY_SORT_LASTPLAYED),
  1742. g_config->ReadInt(L"recent_sort_dir",0));
  1743. ListView_SetItemCount(resultlist.getwnd(),0);
  1744. ListView_SetItemCount(resultlist.getwnd(),itemCache.Size);
  1745. ListView_RedrawItems(resultlist.getwnd(),0,itemCache.Size-1);
  1746. }
  1747. else if (l->code == LVN_BEGINDRAG)
  1748. {
  1749. SetCapture(hwndDlg);
  1750. }
  1751. else if (l->code == LVN_ITEMCHANGED)
  1752. {
  1753. history_ManageButtons(hwndDlg);
  1754. }
  1755. }
  1756. }
  1757. break;
  1758. case WM_APP + 104:
  1759. {
  1760. history_UpdateButtonText(hwndDlg, (int)wParam);
  1761. LayoutWindows(hwndDlg, TRUE);
  1762. return 0;
  1763. }
  1764. }
  1765. return FALSE;
  1766. }
  1767. void nukeHistory(HWND hwndDlg)
  1768. {
  1769. wchar_t titleStr[32] = {0};
  1770. if (MessageBoxW(hwndDlg, WASABI_API_LNGSTRINGW(IDS_REMOVE_ALL_HISTORY),
  1771. WASABI_API_LNGSTRINGW_BUF(IDS_CONFIRMATION,titleStr,32),
  1772. MB_YESNO | MB_ICONQUESTION) == IDYES)
  1773. {
  1774. closeDb();
  1775. wchar_t tmp[MAX_PATH] = {0};
  1776. StringCchPrintfW(tmp, MAX_PATH, L"%s\\recent.dat", g_tableDir);
  1777. DeleteFileW(tmp);
  1778. StringCchPrintfW(tmp, MAX_PATH, L"%s\\recent.idx", g_tableDir);
  1779. DeleteFileW(tmp);
  1780. openDb();
  1781. // trigger a refresh of the current view
  1782. PostMessage(plugin.hwndLibraryParent, WM_USER + 30, 0, 0);
  1783. }
  1784. }
  1785. BOOL CALLBACK view_errorinfoDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  1786. {
  1787. BOOL a= (BOOL)dialogSkinner.Handle(hwndDlg,uMsg,wParam,lParam); if (a) return a;
  1788. switch(uMsg)
  1789. {
  1790. case WM_INITDIALOG:
  1791. {
  1792. SetWindowText(GetDlgItem(hwndDlg, IDC_DB_ERROR),
  1793. (wchar_t*)WASABI_API_LOADRESFROMFILEW(TEXT("TEXT"), MAKEINTRESOURCE((nde_error ? IDR_NDE_ERROR : IDR_DB_ERROR)), 0));
  1794. if (nde_error)
  1795. DestroyWindow(GetDlgItem(hwndDlg, IDC_RESET_DB_ON_ERROR));
  1796. FLICKERFIX ff;
  1797. INT index;
  1798. INT ffcl[] = { IDC_DB_ERROR,
  1799. IDC_RESET_DB_ON_ERROR,
  1800. };
  1801. ff.mode = FFM_ERASEINPAINT;
  1802. for (index = 0; index < (sizeof(ffcl) / sizeof(INT)); index++)
  1803. {
  1804. ff.hwnd = GetDlgItem(hwndDlg, ffcl[index]);
  1805. if (IsWindow(ff.hwnd))
  1806. {
  1807. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_FLICKERFIX, (WPARAM)&ff);
  1808. }
  1809. }
  1810. MLSKINWINDOW m = {0};
  1811. m.skinType = SKINNEDWND_TYPE_DIALOG;
  1812. m.hwndToSkin = hwndDlg;
  1813. m.style = SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS;
  1814. MLSkinWindow(plugin.hwndLibraryParent, &m);
  1815. }
  1816. return TRUE;
  1817. case WM_COMMAND:
  1818. if(LOWORD(wParam) == IDC_RESET_DB_ON_ERROR)
  1819. {
  1820. nukeHistory(hwndDlg);
  1821. }
  1822. break;
  1823. case WM_WINDOWPOSCHANGED:
  1824. if ((SWP_NOSIZE | SWP_NOMOVE) != ((SWP_NOSIZE | SWP_NOMOVE) & ((WINDOWPOS*)lParam)->flags) ||
  1825. (SWP_FRAMECHANGED & ((WINDOWPOS*)lParam)->flags))
  1826. {
  1827. LayoutWindows2(hwndDlg, !(SWP_NOREDRAW & ((WINDOWPOS*)lParam)->flags));
  1828. }
  1829. return 0;
  1830. case WM_USER+66:
  1831. if (wParam == -1)
  1832. {
  1833. LayoutWindows2(hwndDlg, TRUE);
  1834. }
  1835. return TRUE;
  1836. case WM_USER + 0x201:
  1837. offsetX = (short)LOWORD(wParam);
  1838. offsetY = (short)HIWORD(wParam);
  1839. g_rgnUpdate = (HRGN)lParam;
  1840. return TRUE;
  1841. case WM_PAINT:
  1842. {
  1843. dialogSkinner.Draw(hwndDlg, 0, 0);
  1844. }
  1845. return 0;
  1846. case WM_ERASEBKGND:
  1847. return 1; //handled by WADlg_DrawChildWindowBorders in WM_PAINT
  1848. }
  1849. return FALSE;
  1850. }
  1851. void history_cleanupifnecessary()
  1852. {
  1853. if (!g_table) return;
  1854. time_t now=time(NULL);
  1855. // if we've done it in the last 8 hours, don't do it again!
  1856. if (now < g_config->ReadInt(L"recent_limitlt",0) + 8*60*60) return;
  1857. // time to cleanup
  1858. int limit_d=g_config->ReadInt(L"recent_limitd",1);
  1859. int limit_dn=g_config->ReadInt(L"recent_limitnd",30);
  1860. if (!limit_d || limit_dn < 1) return;
  1861. g_config->WriteInt(L"recent_limitlt",(int)now);
  1862. EnterCriticalSection(&g_db_cs);
  1863. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  1864. wchar_t str[512] = {0};
  1865. StringCchPrintfW(str,512,L"lastplay < [%d days ago]",limit_dn);
  1866. NDE_Scanner_Query(s, str);
  1867. NDE_Scanner_First(s);
  1868. for (;;)
  1869. {
  1870. if (!NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_LASTPLAYED)) break;
  1871. NDE_Scanner_Edit(s);
  1872. NDE_Scanner_Delete(s);
  1873. NDE_Scanner_Post(s);
  1874. g_table_dirty++;
  1875. }
  1876. NDE_Table_DestroyScanner(g_table, s);
  1877. if (g_table_dirty)
  1878. {
  1879. NDE_Table_Sync(g_table);
  1880. g_table_dirty=0;
  1881. NDE_Table_Compact(g_table);
  1882. }
  1883. LeaveCriticalSection(&g_db_cs);
  1884. }
  1885. void history_onFile(const wchar_t *fn, int offset)
  1886. {
  1887. if (!fn || fn && !*fn) return;
  1888. int isstream=!!wcsstr(fn,L"://");
  1889. if (isstream)
  1890. {
  1891. if (g_config->ReadInt(L"recent_track",1)&2) return;
  1892. }
  1893. else
  1894. {
  1895. if (!(g_config->ReadInt(L"recent_track",1)&1)) return;
  1896. }
  1897. if (!g_table && !openDb()) return;
  1898. const wchar_t *filename = fn;
  1899. int was_querying=0;
  1900. if (history_bgThread_Handle)
  1901. {
  1902. history_bgQuery_Stop();
  1903. was_querying=1;
  1904. }
  1905. KillTimer(m_hwnd,123);
  1906. EnterCriticalSection(&g_db_cs);
  1907. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  1908. wchar_t filename2[2048] = {0}; // full lfn path if set
  1909. makeFilename2(filename,filename2,ARRAYSIZE(filename2));
  1910. int found=0;
  1911. if (filename2[0])
  1912. {
  1913. if (NDE_Scanner_LocateFilename(s, HISTORYVIEW_COL_FILENAME, FIRST_RECORD, filename2)) found = 2;
  1914. }
  1915. if (!found)
  1916. {
  1917. if (NDE_Scanner_LocateFilename(s, HISTORYVIEW_COL_FILENAME, FIRST_RECORD, filename)) found = 1;
  1918. }
  1919. int cnt=0;
  1920. if (found)
  1921. {
  1922. NDE_Scanner_Edit(s);
  1923. if (found == 1 && filename2[0]) db_setFieldString(s,HISTORYVIEW_COL_FILENAME,filename2); // if we have a better filename, update it
  1924. nde_field_t f = NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_PLAYCOUNT);
  1925. cnt = f?NDE_IntegerField_GetValue(f):0;
  1926. }
  1927. else
  1928. {
  1929. NDE_Scanner_New(s);
  1930. db_setFieldString(s, HISTORYVIEW_COL_FILENAME, filename2[0] ? filename2 : filename);
  1931. int plidx= (int)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_GETLISTPOS);
  1932. const wchar_t *ft=(const wchar_t*)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, plidx, IPC_GETPLAYLISTTITLEW);
  1933. if (!ft || (INT_PTR)ft == 1) ft=fn;
  1934. const wchar_t *ftp=ft;
  1935. int length= (int)SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 1, IPC_GETOUTPUTTIME);
  1936. if (*ftp == '[' && (ftp=wcsstr(ftp,L"]")))
  1937. {
  1938. ftp++;
  1939. while (ftp && *ftp == ' ') ftp++;
  1940. if (ftp && !*ftp) ftp=ft;
  1941. }
  1942. else ftp=ft;
  1943. db_setFieldInt(s, HISTORYVIEW_COL_LENGTH, length);
  1944. db_setFieldString(s, HISTORYVIEW_COL_TITLE, ftp);
  1945. }
  1946. if (offset >= 0)
  1947. {
  1948. db_setFieldInt(s, HISTORYVIEW_COL_OFFSET, (offset > 0 ? offset : -1));
  1949. }
  1950. else
  1951. {
  1952. db_setFieldInt(s, HISTORYVIEW_COL_LASTPLAYED, (int)time(NULL));
  1953. db_setFieldInt(s, HISTORYVIEW_COL_PLAYCOUNT, cnt+1);
  1954. }
  1955. NDE_Scanner_Post(s);
  1956. NDE_Table_DestroyScanner(g_table, s);
  1957. NDE_Table_Sync(g_table);
  1958. g_table_dirty++;
  1959. // changed to save the history when updated to prevent it being
  1960. // lost but retains the 8hr cleanup, etc which is otherwise run
  1961. if (g_table_dirty > 100)
  1962. {
  1963. history_cleanupifnecessary();
  1964. }
  1965. if (g_table_dirty)
  1966. {
  1967. // and to keep existing behaviour for the dirty count, we
  1968. // ensure that even on saving we maintain the dirty count
  1969. closeDb(false);
  1970. openDb();
  1971. }
  1972. LeaveCriticalSection(&g_db_cs);
  1973. if (was_querying)
  1974. history_bgQuery();
  1975. if (IsWindow(m_hwnd))
  1976. {
  1977. m_lv_last_topidx=ListView_GetTopIndex(resultlist.getwnd());
  1978. SendMessage(m_hwnd,WM_APP+1,0,0);
  1979. }
  1980. }
  1981. int retrieve_offset(const wchar_t *fn)
  1982. {
  1983. int offset = -1;
  1984. int isstream=!!wcsstr(fn,L"://");
  1985. if (isstream)
  1986. {
  1987. if (g_config->ReadInt(L"recent_track",1)&2) return offset;
  1988. }
  1989. else
  1990. {
  1991. if (!(g_config->ReadInt(L"recent_track",1)&1)) return offset;
  1992. }
  1993. if (!g_table && !openDb()) return offset;
  1994. const wchar_t *filename=fn;
  1995. EnterCriticalSection(&g_db_cs);
  1996. nde_scanner_t s = NDE_Table_CreateScanner(g_table);
  1997. wchar_t filename2[2048] = {0}; // full lfn path if set
  1998. makeFilename2(filename,filename2,ARRAYSIZE(filename2));
  1999. int found=0;
  2000. if (filename2[0])
  2001. {
  2002. if (NDE_Scanner_LocateFilename(s, HISTORYVIEW_COL_FILENAME,FIRST_RECORD,filename2)) found=2;
  2003. }
  2004. if (!found)
  2005. {
  2006. if (NDE_Scanner_LocateFilename(s, HISTORYVIEW_COL_FILENAME,FIRST_RECORD,filename)) found=1;
  2007. }
  2008. if (found)
  2009. {
  2010. nde_field_t f = NDE_Scanner_GetFieldByID(s, HISTORYVIEW_COL_OFFSET);
  2011. offset = f?NDE_IntegerField_GetValue(f):-1;
  2012. }
  2013. NDE_Table_DestroyScanner(g_table, s);
  2014. LeaveCriticalSection(&g_db_cs);
  2015. return offset;
  2016. }
  2017. void fileInfoDialogs(HWND hwndParent)
  2018. {
  2019. history_bgQuery_Stop();
  2020. int l=resultlist.GetCount(),i;
  2021. int needref=0;
  2022. for(i=0;i<l;i++)
  2023. {
  2024. if(!resultlist.GetSelected(i)) continue;
  2025. historyRecord *song=(historyRecord *)itemCache.Items + i;
  2026. if (!song->filename || !song->filename[0]) continue;
  2027. infoBoxParamW p;
  2028. p.filename=song->filename;
  2029. p.parent=hwndParent;
  2030. if (SendMessage(plugin.hwndWinampParent,WM_WA_IPC,(WPARAM)&p,IPC_INFOBOXW)) break;
  2031. needref=1;
  2032. EnterCriticalSection(&g_db_cs);
  2033. nde_scanner_t s= NDE_Table_CreateScanner(g_table);
  2034. if (NDE_Scanner_LocateNDEFilename(s, HISTORYVIEW_COL_FILENAME,FIRST_RECORD,song->filename))
  2035. {
  2036. wchar_t ft[1024]={0};
  2037. basicFileInfoStructW bi = {0};
  2038. bi.filename=p.filename;
  2039. bi.length=-1;
  2040. bi.title=ft;
  2041. bi.titlelen=ARRAYSIZE(ft);
  2042. SendMessage(plugin.hwndWinampParent,WM_WA_IPC,(WPARAM)&bi,IPC_GET_BASIC_FILE_INFOW);
  2043. db_setFieldInt(s,HISTORYVIEW_COL_LENGTH,bi.length);
  2044. db_setFieldString(s,HISTORYVIEW_COL_TITLE,ft);
  2045. NDE_Scanner_Post(s);
  2046. }
  2047. NDE_Table_DestroyScanner(g_table, s);
  2048. g_table_dirty++;
  2049. LeaveCriticalSection(&g_db_cs);
  2050. }
  2051. if (g_table_dirty && g_table)
  2052. {
  2053. EnterCriticalSection(&g_db_cs);
  2054. NDE_Table_Sync(g_table);
  2055. g_table_dirty=0;
  2056. LeaveCriticalSection(&g_db_cs);
  2057. }
  2058. if (needref)
  2059. {
  2060. SendMessage(hwndParent,WM_TIMER,UPDATE_QUERY_TIMER_ID,0);
  2061. }
  2062. MSG msg;
  2063. while(PeekMessage(&msg,NULL,WM_KEYFIRST,WM_KEYLAST,PM_REMOVE)); //eat return
  2064. }