playlistpro.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. PlaylistPro build 005
  3. Originally done by pjn123 (www.skinconsortium.com) for the Classic Pro plugin
  4. */
  5. #include "lib/std.mi"
  6. #include "lib/pldir.mi"
  7. #include "init_Playlist.m"
  8. Function resizeResults(int items);
  9. Function doSearch(String input);
  10. Function setSearchBox(boolean onOff);
  11. Function clearSearchBox();
  12. Function int getPlEntry(int search_item);
  13. Function String replaceString(string baseString, string toreplace, string replacedby);
  14. Global Group frameGroup, topbar;
  15. Global Edit searchBox;
  16. Global Text helpSearch, searchNews;// fakeText;
  17. Global GuiObject fakeSB, searchXUI, searchButtonXui;
  18. Global Button searchButton, clearButton, clearButtonText;
  19. Global GuiList searchResults;
  20. Global Boolean foundsomething;
  21. Global int tn, h_tune;
  22. Global String temptoken;
  23. Global Windowholder plwh;
  24. GLobal Timer refreshActiveCheck;
  25. Global PopupMenu search_rc;
  26. Global Container results_container;
  27. Global Layout results_layout, main_layout;
  28. System.onScriptLoaded() {
  29. frameGroup = getScriptGroup();
  30. topbar = frameGroup.findObject("PlaylistPro.topbar");
  31. plwh = frameGroup.findObject("wdh.playlist");
  32. searchBox = frameGroup.findObject("wasabi.edit.box");
  33. searchXUI = frameGroup.findObject("pl.search.edit");
  34. searchButton = frameGroup.findObject("pl.search.go");
  35. searchButtonXui = frameGroup.findObject("pl.search.go");
  36. fakeSB = frameGroup.findObject("pl.search.edit.rect");
  37. clearButton = frameGroup.findObject("pl.search.edit.clear");
  38. clearButtonText = frameGroup.findObject("pl.search.edit.clear.text");
  39. helpSearch = frameGroup.findObject("pl.search.edit.searchhelp");
  40. //fakeText = frameGroup.findObject("pl.search.go.text.fake");
  41. results_container = newDynamicContainer("searchresults");
  42. results_layout = results_container.getLayout("normal");
  43. searchResults = results_layout.findObject("PlaylistPro.list");
  44. searchResults.setFontSize(16);
  45. searchNews = results_layout.findObject("PlaylistPro.list.news");
  46. main_layout = getContainer("Pledit").getLayout("normal");
  47. initAttribs_Playlist();
  48. //translation workaround
  49. /*searchButtonXui.setXmlParam("text", System.translate("Search"));
  50. searchButtonXui.setXmlParam("x", integerToString(-1*fakeText.getAutoWidth()-4));
  51. searchButtonXui.setXmlParam("w", integerToString(fakeText.getAutoWidth()+2));
  52. searchXUI.setXmlParam("w", integerToString(-1*fakeText.getAutoWidth()-9));*/
  53. refreshActiveCheck = new Timer;
  54. refreshActiveCheck.setDelay(100);
  55. }
  56. System.onScriptUnLoading() {
  57. delete refreshActiveCheck;
  58. }
  59. refreshActiveCheck.onTimer(){
  60. if((!System.isAppActive() || System.isMinimized()) && results_layout.isVisible() ) results_layout.hide();
  61. }
  62. results_layout.onSetVisible(boolean onOff){
  63. if(onOff){
  64. refreshActiveCheck.start();
  65. }
  66. else{
  67. refreshActiveCheck.stop();
  68. }
  69. }
  70. System.onShowLayout(Layout _layout){
  71. if(main_layout.isVisible()) results_layout.setXmlParam("ontop", "1");
  72. }
  73. frameGroup.onSetVisible(boolean onOff){
  74. if(!onOff) clearSearchBox();
  75. }
  76. searchButton.onLeftClick(){
  77. doSearch(searchBox.getText());
  78. }
  79. resizeResults(int items){
  80. /*results_layout.setTargetX(results_layout.getLeft());
  81. results_layout.setTargetY(results_layout.getTop());
  82. results_layout.setTargetW(results_layout.getWidth());
  83. results_layout.setTargetH(500);
  84. results_layout.setTargetSpeed(1);
  85. results_layout.gotoTarget();*/
  86. //items++; //temp add one extra for info... xx items found
  87. if(items>20) items=20;
  88. if(items>=1) h_tune=29;
  89. else h_tune=25;
  90. //results_layout.setXmlParam("h", integerToString(20+items*18));
  91. /*results_layout.setTargetX(results_layout.getLeft());
  92. results_layout.setTargetY(results_layout.getTop());
  93. results_layout.setTargetW(results_layout.getWidth());
  94. results_layout.setTargetH(h_tune+items*16);
  95. results_layout.setTargetSpeed(0.3);
  96. results_layout.gotoTarget();*/
  97. results_layout.resize(results_layout.getLeft(), results_layout.getTop(), results_layout.getWidth(), h_tune+items*16);
  98. }
  99. searchBox.onEnter(){
  100. doSearch(searchBox.getText());
  101. }
  102. doSearch(String input){
  103. if(input==""){
  104. clearSearchBox();
  105. return;
  106. }
  107. //search history stuff
  108. String history = getPublicString("cPro.PlaylistPro.history", "");
  109. history = replaceString(history, input, "");
  110. history = replaceString(history, ";;", ";");
  111. if(strleft(history, 1)==";") history = strright(history, strlen(history)-1);
  112. if(strright(history, 1)==";") history = strleft(history, strlen(history)-1);
  113. if(history=="") history= input;
  114. else history= input+";"+history;
  115. String output = getToken(history, ";", 0);
  116. for(int i = 1; i<15; i++){
  117. if(getToken(history, ";", i)=="") break;
  118. output+=";"+getToken(history, ";", i);
  119. }
  120. setPublicString("cPro.PlaylistPro.history", history);
  121. int itemsfound = 0;
  122. input = strlower(input);
  123. results_layout.setXmlParam("x", integerToString(fakeSB.clientToScreenX(fakeSB.getLeft()-2)));
  124. results_layout.setXmlParam("y", integerToString(fakeSB.clientToScreenY(fakeSB.getTop() + fakeSB.getHeight())));
  125. results_layout.setXmlParam("w", integerToString(frameGroup.getWidth()-19));
  126. if(!results_layout.isVisible()) results_layout.setXmlParam("h", "1");
  127. searchResults.deleteAllItems();
  128. searchResults.scrollToItem(0);
  129. for(int i = 0; i<PlEdit.getNumTracks(); i++){
  130. foundsomething=false;
  131. for(tn = 0; tn<10; tn++){
  132. if(getToken(input, " ", tn)==""){
  133. break;
  134. }
  135. temptoken = getToken(input, " ", tn);
  136. if(strsearch(strlower(PlEdit.getTitle(i) + " " + PlEdit.getFileName(i)), temptoken)>=0){
  137. foundsomething=true;
  138. }
  139. else{
  140. foundsomething=false;
  141. }
  142. if(!foundsomething){
  143. break;
  144. }
  145. }
  146. if(foundsomething){
  147. itemsfound++;
  148. searchResults.addItem(integerToString(i+1)+". " + PlEdit.getTitle(i));
  149. if(itemsfound>500){
  150. searchNews.setText("Search result limited to 500 items");
  151. break;
  152. }
  153. }
  154. }
  155. if(itemsfound==0){
  156. searchNews.setText("Nothing was found");
  157. }
  158. else if(itemsfound<=500){
  159. searchNews.setText(System.translate("Items found: ") +integerToString(itemsfound));
  160. }
  161. if(!results_layout.isVisible()) results_layout.show();
  162. // Fix if always on top is enabled.. it just refresh the ontop ;)
  163. results_layout.setXmlParam("ontop", "1");
  164. resizeResults(itemsfound);
  165. }
  166. searchResults.onDoubleClick(Int itemnum){
  167. PlEdit.playTrack (getPlEntry(itemnum));
  168. setSearchBox(false);
  169. }
  170. searchResults.onRightClick(Int itemnum){
  171. search_rc = new PopupMenu;
  172. search_rc.addCommand("Move selected to top", 1, 0, 0);
  173. search_rc.addCommand("Move selected to bottom", 2, 0, 0);
  174. search_rc.addCommand("Move selected after current", 3, 0, 0);
  175. search_rc.addCommand("Move selected together", 4, 0, 0);
  176. search_rc.addSeparator();
  177. search_rc.addCommand("Remove selected from playlist", 5, 0, 0);
  178. int result = search_rc.popAtMouse();
  179. delete search_rc;
  180. int lastselected = searchResults.getFirstItemSelected();
  181. int itemcounter = 1;
  182. if(result==1){
  183. PlEdit.moveTo (getPlEntry(lastselected), 0);
  184. while(searchResults.getNextItemSelected(lastselected) != -1){
  185. lastselected = searchResults.getNextItemSelected(lastselected);
  186. PlEdit.moveTo (getPlEntry(lastselected), itemcounter);
  187. itemcounter++;
  188. }
  189. PlEdit.showTrack(0);
  190. }
  191. else if(result==2){
  192. PlEdit.moveTo (getPlEntry(lastselected), PlEdit.getNumTracks ()-1);
  193. while(searchResults.getNextItemSelected(lastselected) != -1){
  194. lastselected = searchResults.getNextItemSelected(lastselected);
  195. PlEdit.moveTo (getPlEntry(lastselected)-itemcounter, PlEdit.getNumTracks ()-1);
  196. itemcounter++;
  197. }
  198. PlEdit.showTrack(PlEdit.getNumTracks ()-1);
  199. }
  200. else if(result==3){
  201. int align = 0;
  202. int orignalPos = PlEdit.getCurrentIndex();
  203. int temp = getPlEntry(lastselected);
  204. if(PlEdit.getCurrentIndex() > temp) align++;
  205. if(orignalPos > getPlEntry(lastselected)) PlEdit.moveTo (getPlEntry(lastselected), PlEdit.getCurrentIndex());
  206. else PlEdit.moveTo (getPlEntry(lastselected), PlEdit.getCurrentIndex()+1);
  207. while(searchResults.getNextItemSelected(lastselected) != -1){
  208. lastselected = searchResults.getNextItemSelected(lastselected);
  209. if(orignalPos != getPlEntry(lastselected)){
  210. if(orignalPos > getPlEntry(lastselected)){
  211. PlEdit.moveTo (getPlEntry(lastselected)-align, PlEdit.getCurrentIndex()+itemcounter);
  212. }
  213. else{
  214. PlEdit.moveTo (getPlEntry(lastselected), PlEdit.getCurrentIndex()+itemcounter+1);
  215. }
  216. temp = getPlEntry(lastselected);
  217. if(orignalPos > temp) align++;
  218. itemcounter++;
  219. }
  220. }
  221. PlEdit.showCurrentlyPlayingTrack();
  222. }
  223. else if(result==4){
  224. int startpos = getPlEntry(lastselected);
  225. while(searchResults.getNextItemSelected(lastselected) != -1){
  226. lastselected = searchResults.getNextItemSelected(lastselected);
  227. PlEdit.moveTo (getPlEntry(lastselected), startpos+itemcounter);
  228. itemcounter++;
  229. }
  230. PlEdit.showTrack(startpos);
  231. }
  232. else if(result==5){
  233. PlEdit.removeTrack (getPlEntry(lastselected));
  234. while(searchResults.getNextItemSelected(lastselected) != -1){
  235. lastselected = searchResults.getNextItemSelected(lastselected);
  236. PlEdit.removeTrack (getPlEntry(lastselected)-itemcounter);
  237. itemcounter++;
  238. }
  239. }
  240. else return;
  241. //hides the search
  242. setSearchBox(false);
  243. }
  244. int getPlEntry(int search_item){
  245. return stringToInteger(getToken(searchResults.getItemLabel(search_item,0), ". ", 0))-1;
  246. }
  247. setSearchBox(boolean onOff){
  248. if(onOff){
  249. searchBox.show();
  250. clearButton.show();
  251. clearButtonText.show();
  252. helpSearch.hide();
  253. searchBox.setFocus();
  254. }
  255. else{
  256. searchBox.hide();
  257. clearButton.hide();
  258. clearButtonText.hide();
  259. helpSearch.show();
  260. results_layout.hide();
  261. }
  262. }
  263. fakeSB.onLeftButtonDown(int x, int y){
  264. setSearchBox(true);
  265. }
  266. fakeSB.onRightButtonDown(int x, int y){
  267. search_rc = new PopupMenu;
  268. search_rc.addCommand("** Search history **", 0, 0, 1);
  269. search_rc.addSeparator();
  270. String history = getPublicString("cPro.PlaylistPro.history", "");
  271. boolean historyfound=false;
  272. for(int i=0;i<15;i++){
  273. String historyitem = getToken(history, ";", i);
  274. if(historyitem==""){
  275. if(i>0) historyfound=true;
  276. break;
  277. }
  278. search_rc.addCommand(historyitem, i+1, 0, 0);
  279. }
  280. if(historyfound) search_rc.addSeparator();
  281. search_rc.addCommand("Clear History", 100, 0, 0);
  282. int result = search_rc.popAtMouse();
  283. delete search_rc;
  284. if(result>0 && result<100){
  285. setSearchBox(true);
  286. searchBox.setText(getToken(history, ";", result-1));
  287. searchButton.leftClick();
  288. }
  289. else if(result==100){
  290. setPublicString("cPro.PlaylistPro.history", "");
  291. }
  292. }
  293. clearSearchBox(){
  294. searchBox.setText("");
  295. setSearchBox(false);
  296. }
  297. main_layout.onMove(){
  298. if(results_layout.isVisible()){
  299. results_layout.setXmlParam("x", integerToString(fakeSB.clientToScreenX(fakeSB.getLeft()-2)));
  300. results_layout.setXmlParam("y", integerToString(fakeSB.clientToScreenY(fakeSB.getTop() + fakeSB.getHeight())));
  301. results_layout.setXmlParam("ontop", "1");
  302. }
  303. }
  304. main_layout.onResize(int x, int y, int w, int h){
  305. if(results_layout.isVisible()) clearSearchBox();
  306. }
  307. frameGroup.onResize(int x, int y, int w, int h){
  308. if(results_layout.isVisible()) clearSearchBox();
  309. //if(playlist_search_attib.getData()=="0") return;
  310. if(h<102 || playlist_search_attrib.getData()=="0"){
  311. if(!topbar.isVisible()) return; //Don't do the same code over and over
  312. topbar.hide();
  313. plwh.setXmlParam("y", "0");
  314. plwh.setXmlParam("h", "0");
  315. }
  316. else{
  317. if(topbar.isVisible()) return; //Don't do the same code over and over
  318. topbar.show();
  319. plwh.setXmlParam("y", "30");
  320. plwh.setXmlParam("h", "-30");
  321. }
  322. }
  323. clearButton.onLeftClick(){
  324. clearSearchBox();
  325. }
  326. playlist_search_attrib.onDataChanged(){
  327. frameGroup.onResize(frameGroup.getLeft(), frameGroup.getTop(), frameGroup.getWidth(), frameGroup.getHeight());
  328. //debug("abc " + getData());
  329. if (getData() == "0"){
  330. topbar.hide();
  331. plwh.setXMLParam("y", "0");
  332. plwh.setXMLParam("h", "0");
  333. }
  334. else if(getData() == "1"){
  335. topbar.show();
  336. plwh.setXMLParam("y", "30");
  337. plwh.setXMLParam("h", "-30");
  338. }
  339. }
  340. String replaceString(string baseString, string toreplace, string replacedby){
  341. if (toreplace == "") return baseString;
  342. string sf1 = strupper(baseString);
  343. string sf2 = strupper(toreplace);
  344. int i = strsearch(sf1, sf2);
  345. if (i == -1) return baseString;
  346. string left = "", right = "";
  347. if (i != 0) left = strleft(baseString, i);
  348. if (strlen(basestring) - i - strlen(toreplace) != 0) {
  349. right = strright(basestring, strlen(basestring) - i - strlen(toreplace));
  350. }
  351. return left + replacedby + right;
  352. }