basecmds.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * basecmds.cc - base list keyboard commands display
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <dpkg/i18n.h>
  26. #include <dpkg/dpkg.h>
  27. #include <dpkg/dpkg-db.h>
  28. #include "dselect.h"
  29. #include "helpmsgs.h"
  30. void baselist::kd_scrollon() {
  31. topofscreen += list_height;
  32. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  33. if (topofscreen < 0) topofscreen= 0;
  34. if (cursorline < topofscreen)
  35. setcursor(topofscreen);
  36. refreshlist();
  37. }
  38. void baselist::kd_scrollon1() {
  39. if (topofscreen >= nitems - list_height) return;
  40. topofscreen++;
  41. if (cursorline < topofscreen)
  42. setcursor(topofscreen);
  43. refreshlist();
  44. }
  45. void baselist::kd_scrollback() {
  46. topofscreen -= list_height;
  47. if (topofscreen < 0) topofscreen= 0;
  48. if (cursorline >= topofscreen + list_height)
  49. setcursor(topofscreen + list_height - 1);
  50. refreshlist();
  51. }
  52. void baselist::kd_scrollback1() {
  53. if (topofscreen <= 0) return;
  54. topofscreen--;
  55. if (cursorline >= topofscreen + list_height)
  56. setcursor(topofscreen + list_height - 1);
  57. refreshlist();
  58. }
  59. void baselist::kd_top() {
  60. topofscreen= 0; setcursor(0);
  61. }
  62. void baselist::kd_bottom() {
  63. topofscreen= nitems - list_height;
  64. if (topofscreen < 0) topofscreen= 0;
  65. setcursor(min(topofscreen + list_height - 1, nitems - 1));
  66. }
  67. void baselist::kd_redraw() {
  68. //#define RFSH(x) werase(x); redrawwin(x)
  69. // RFSH(listpad);
  70. // RFSH(infopad);
  71. // RFSH(colheadspad);
  72. // RFSH(thisstatepad);
  73. // RFSH(titlewin);
  74. // RFSH(whatinfowin); /* FIXME: why does ncurses need this? */
  75. clearok(curscr,TRUE);
  76. redrawall();
  77. debug(dbg_general, "baselist[%p]::kd_redraw() done", this);
  78. }
  79. void baselist::kd_searchagain() {
  80. if (!searchstring[0]) { beep(); return; }
  81. dosearch();
  82. }
  83. bool
  84. baselist::checksearch(char *str)
  85. {
  86. return true;
  87. }
  88. bool
  89. baselist::matchsearch(int index)
  90. {
  91. int lendiff, searchlen, i;
  92. const char *name;
  93. name = itemname(index);
  94. if (!name)
  95. return false; /* Skip things without a name (seperators). */
  96. searchlen=strlen(searchstring);
  97. lendiff = strlen(name) - searchlen;
  98. for (i=0; i<=lendiff; i++)
  99. if (strncasecmp(name + i, searchstring, searchlen) == 0)
  100. return true;
  101. return false;
  102. }
  103. void baselist::kd_search() {
  104. char newsearchstring[128];
  105. strcpy(newsearchstring,searchstring);
  106. werase(querywin);
  107. mvwaddstr(querywin,0,0, _("Search for ? "));
  108. echo(); /* FIXME: ncurses documentation or implementation. */
  109. if (wgetnstr(querywin,newsearchstring,sizeof(newsearchstring)-1) == ERR)
  110. searchstring[0]= 0;
  111. raise(SIGWINCH); /* FIXME: ncurses and xterm arrow keys. */
  112. noecho(); /* FIXME: ncurses. */
  113. if (whatinfo_height) { touchwin(whatinfowin); refreshinfo(); }
  114. else if (info_height) { touchwin(infopad); refreshinfo(); }
  115. else if (thisstate_height) redrawthisstate();
  116. else { touchwin(listpad); refreshlist(); }
  117. if (newsearchstring[0]) {
  118. if (!checksearch(newsearchstring)) {
  119. beep();
  120. return;
  121. }
  122. strcpy(searchstring,newsearchstring);
  123. dosearch();
  124. } else
  125. kd_searchagain();
  126. }
  127. void
  128. baselist::displayerror(const char *str)
  129. {
  130. const char *pr = _("Error: ");
  131. int prlen=strlen(pr);
  132. beep();
  133. werase(querywin);
  134. mvwaddstr(querywin,0,0,pr);
  135. mvwaddstr(querywin,0,prlen,str);
  136. wgetch(querywin);
  137. if (whatinfo_height) { touchwin(whatinfowin); refreshinfo(); }
  138. else if (info_height) { touchwin(infopad); refreshinfo(); }
  139. else if (thisstate_height) redrawthisstate();
  140. }
  141. void baselist::displayhelp(const struct helpmenuentry *helpmenu, int key) {
  142. int maxx, maxy, i, nextkey;
  143. getmaxyx(stdscr,maxy,maxx);
  144. wbkgdset(stdscr, ' ' | part_attr[helpscreen]);
  145. clearok(stdscr,TRUE);
  146. for (;;) {
  147. werase(stdscr);
  148. const struct helpmenuentry *hme = helpmenu;
  149. while (hme->key && hme->key != key)
  150. hme++;
  151. if (hme->key) {
  152. int x, y DPKG_ATTR_UNUSED;
  153. attrset(part_attr[helpscreen]);
  154. mvaddstr(1,0, gettext(hme->msg->text));
  155. attrset(part_attr[title]);
  156. mvaddstr(0,0, _("Help: "));
  157. addstr(gettext(hme->msg->title));
  158. getyx(stdscr,y,x);
  159. while (++x<maxx) addch(' ');
  160. attrset(part_attr[thisstate]);
  161. mvaddstr(maxy-1,0,
  162. _("Press ? for help menu, . for next topic, <space> to exit help."));
  163. getyx(stdscr,y,x);
  164. while (++x<maxx) addch(' ');
  165. move(maxy,maxx);
  166. attrset(A_NORMAL);
  167. nextkey= hme[1].key;
  168. } else {
  169. mvaddstr(1,1, _("Help information is available under the following topics:"));
  170. for (i=0, hme=helpmenu; hme->key; hme++,i++) {
  171. attrset(A_BOLD);
  172. mvaddch(i+3,3, hme->key);
  173. attrset(A_NORMAL);
  174. mvaddstr(i+3,6, gettext(hme->msg->title));
  175. }
  176. mvaddstr(i+4,1,
  177. _("Press a key from the list above, <space> or 'q' to exit help,\n"
  178. " or '.' (full stop) to read each help page in turn. "));
  179. nextkey= helpmenu[0].key;
  180. }
  181. refresh();
  182. key= getch();
  183. if (key == EOF) ohshite(_("error reading keyboard in help"));
  184. if (key == ' ' || key == 'q') {
  185. break;
  186. } else if (key == '?' || key == 'h') {
  187. key= 0;
  188. } else if (key == '.') {
  189. key= nextkey;
  190. }
  191. }
  192. werase(stdscr);
  193. clearok(stdscr,TRUE);
  194. wnoutrefresh(stdscr);
  195. redrawtitle();
  196. refreshcolheads();
  197. refreshlist();
  198. redrawthisstate();
  199. refreshinfo();
  200. wnoutrefresh(whatinfowin);
  201. }
  202. void baselist::kd_help() {
  203. displayhelp(helpmenulist(),0);
  204. }
  205. void baselist::setcursor(int index) {
  206. if (listpad && cursorline != -1) {
  207. redrawitemsrange(cursorline,cursorline+1);
  208. redraw1itemsel(cursorline,0);
  209. }
  210. if (cursorline != index) infotopofscreen= 0;
  211. cursorline= index;
  212. if (listpad) {
  213. redrawitemsrange(cursorline,cursorline+1);
  214. redraw1itemsel(cursorline,1);
  215. refreshlist();
  216. redrawthisstate();
  217. }
  218. redrawinfo();
  219. }
  220. void baselist::kd_down() {
  221. int ncursor= cursorline;
  222. // scroll by one line unless the bottom is already visible
  223. // or we're in the top half of the screen ...
  224. if (topofscreen < nitems - list_height &&
  225. ncursor >= topofscreen + list_height - 3) topofscreen++;
  226. // move cursor if there are any more ...
  227. if (cursorline+1 < nitems) ncursor++;
  228. setcursor(ncursor);
  229. }
  230. void baselist::kd_up() {
  231. int ncursor= cursorline;
  232. // scroll by one line if there are any lines not shown yet
  233. // and we're not in the bottom half the screen ...
  234. if (topofscreen > 0 &&
  235. ncursor <= topofscreen + 2) topofscreen--;
  236. // move cursor if there are any to move it to ...
  237. if (cursorline > 0) ncursor--;
  238. setcursor(ncursor);
  239. }
  240. void baselist::kd_iscrollon() {
  241. infotopofscreen += info_height;
  242. if (infotopofscreen > infolines - info_height)
  243. infotopofscreen= infolines - info_height;
  244. if (infotopofscreen < 0) infotopofscreen= 0;
  245. refreshinfo();
  246. }
  247. void baselist::kd_iscrollback() {
  248. infotopofscreen -= info_height;
  249. if (infotopofscreen < 0) infotopofscreen= 0;
  250. refreshinfo();
  251. }
  252. void baselist::kd_iscrollon1() {
  253. if (infotopofscreen >= infolines - info_height) return;
  254. infotopofscreen++; refreshinfo();
  255. }
  256. void baselist::kd_iscrollback1() {
  257. if (infotopofscreen <= 0) return;
  258. infotopofscreen--; refreshinfo();
  259. }
  260. void baselist::kd_panon() {
  261. leftofscreen += xmax/3;
  262. if (leftofscreen > total_width - xmax) leftofscreen= total_width - xmax;
  263. if (leftofscreen < 0) leftofscreen= 0;
  264. refreshcolheads(); refreshlist(); redrawthisstate(); refreshinfo();
  265. }
  266. void baselist::kd_panback() {
  267. leftofscreen -= xmax/3;
  268. if (leftofscreen < 0) leftofscreen= 0;
  269. refreshcolheads(); refreshlist(); redrawthisstate(); refreshinfo();
  270. }
  271. void baselist::kd_panon1() {
  272. leftofscreen++;
  273. if (leftofscreen > total_width - xmax) leftofscreen= total_width - xmax;
  274. if (leftofscreen < 0) leftofscreen= 0;
  275. refreshcolheads(); refreshlist(); redrawthisstate(); refreshinfo();
  276. }
  277. void baselist::kd_panback1() {
  278. leftofscreen--;
  279. if (leftofscreen < 0) leftofscreen= 0;
  280. refreshcolheads(); refreshlist(); redrawthisstate(); refreshinfo();
  281. }