t2panel.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. program test_panel;
  2. {$MODE OBJFPC}
  3. uses
  4. ncurses, panel, sysutils;
  5. Type
  6. PPWINDOW = ^PWINDOW;
  7. PPPANEL = ^PPANEL;
  8. const
  9. NLINES = 8;
  10. NCOLS = 32;
  11. procedure print_in_middle(win: PWINDOW; starty, startx, width: Longint; str: AnsiString; color: chtype);
  12. var
  13. slength: Longint;
  14. x, y: Longint;
  15. temp: Double;
  16. begin
  17. if win = nil then
  18. win := stdscr;
  19. getyx(win, y, x);
  20. if startx <> 0 then
  21. x := startx;
  22. if starty <> 0 then
  23. y := starty;
  24. if width = 0 then
  25. width := 80;
  26. slength := Length(str);
  27. temp := (width - slength)/ 2;
  28. x := startx + round(temp);
  29. wattron(win, color);
  30. mvwaddstr(win, y, x, PChar(str));
  31. wattroff(win, color);
  32. refresh();
  33. end;
  34. procedure win_show(win: PWINDOW; lab: AnsiString; label_color: Longint);
  35. var
  36. startx, starty, height, width: Longint;
  37. begin
  38. getbegyx(win, starty, startx);
  39. getmaxyx(win, height, width);
  40. box(win, 0, 0);
  41. mvwaddch(win, 2, 0, ACS_LTEE);
  42. mvwhline(win, 2, 1, ACS_HLINE, width - 2);
  43. mvwaddch(win, 2, width - 1, ACS_RTEE);
  44. print_in_middle(win, 1, 0, width, lab, COLOR_PAIR(label_color));
  45. end;
  46. procedure init_panels(pans: PPPANEL; n: Longint);
  47. var
  48. x, y, i: Longint;
  49. lab: AnsiString;
  50. win: PWINDOW;
  51. begin
  52. y := 2;
  53. x := 3;
  54. for i := 0 to n - 1 do
  55. begin
  56. win := newwin(NLINES, NCOLS, y, x);
  57. FmtStr(lab, 'Window Number %d', [i + 1]);
  58. win_show(win, lab, i + 1);
  59. pans[i] := new_panel(win);
  60. y += 2;
  61. x += 4;
  62. end
  63. end;
  64. procedure select(var oldp: PPANEL; newp: PPANEL);
  65. var
  66. win: PWINDOW;
  67. begin
  68. win := panel_window(oldp);
  69. wattroff(win,A_BOLD);
  70. box(win,0,0);
  71. win := panel_window(newp);
  72. wattron(win,A_BOLD);
  73. box(win,0,0);
  74. oldp := newp;
  75. end;
  76. var
  77. my_panels: array[0..4] of PPANEL;
  78. selected: PPANEL;
  79. ch: chtype;
  80. begin
  81. try
  82. (* Initialize curses *)
  83. initscr();
  84. start_color();
  85. cbreak();
  86. noecho();
  87. keypad(stdscr, TRUE);
  88. (* Initialize all the colors *)
  89. init_pair(1, COLOR_RED, COLOR_BLACK);
  90. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  91. init_pair(3, COLOR_BLUE, COLOR_BLACK);
  92. init_pair(4, COLOR_CYAN, COLOR_BLACK);
  93. init_pair(5, COLOR_YELLOW, COLOR_BLACK);
  94. init_panels(my_panels, 5);
  95. set_panel_userptr(my_panels[0], my_panels[4]);
  96. set_panel_userptr(my_panels[1], my_panels[3]);
  97. set_panel_userptr(my_panels[2], my_panels[1]);
  98. set_panel_userptr(my_panels[3], my_panels[0]);
  99. set_panel_userptr(my_panels[4], my_panels[2]);
  100. select(selected,my_panels[4]);
  101. (* Update the stacking order. 2nd panel will be on top *)
  102. update_panels();
  103. (* Show it on the screen *)
  104. attron(COLOR_PAIR(4));
  105. mvprintw(LINES - 5, 1, 't : top');
  106. mvprintw(LINES - 4, 1, 'h : show or hide toggle');
  107. mvprintw(LINES - 3, 1, '1..5, home, end, up, down, tab : navigate ');
  108. mvprintw(LINES - 2, 1, 'F1 : to Exit');
  109. attroff(COLOR_PAIR(4));
  110. doupdate();
  111. ch := getch;
  112. while ch <> KEY_F(1) do
  113. begin
  114. case ch of
  115. chtype('1'): select(selected,my_panels[0]);
  116. chtype('2'): select(selected,my_panels[1]);
  117. chtype('3'): select(selected,my_panels[2]);
  118. chtype('4'): select(selected,my_panels[3]);
  119. chtype('5'): select(selected,my_panels[4]);
  120. KEY_HOME: select(selected,panel_above(nil));
  121. KEY_END: select(selected,panel_below(nil));
  122. KEY_UP: select(selected,panel_above(selected));
  123. KEY_DOWN: select(selected,panel_below(selected));
  124. 9: select(selected,panel_userptr(selected));
  125. chtype('t'): top_panel(selected);
  126. chtype('h'):
  127. begin
  128. if panel_hidden(selected) = OK then
  129. hide_panel(selected)
  130. else
  131. show_panel(selected);
  132. end;
  133. else
  134. end;
  135. update_panels();
  136. doupdate();
  137. ch := getch;
  138. end;
  139. finally
  140. endwin();
  141. end;
  142. end.