t1panel.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. {
  2. Example 17. Panel Hiding and Showing example
  3. from ncurses howto
  4. }
  5. program test_panel;
  6. {$MODE OBJFPC}
  7. uses
  8. ncurses, panel, sysutils;
  9. Type
  10. PANEL_DATA = record
  11. hide: Boolean; (* TRUE if panel is hidden *)
  12. end;
  13. PPWINDOW = ^PWINDOW;
  14. const
  15. NLINES = 10;
  16. NCOLS = 40;
  17. procedure print_in_middle(win: PWINDOW; starty, startx, width: Longint; str: AnsiString; color: chtype);
  18. var
  19. slength, x, y: Longint;
  20. temp: Double;
  21. begin
  22. if win = nil then
  23. win := stdscr;
  24. getyx(win, y, x);
  25. if startx <> 0 then
  26. x := startx;
  27. if starty <> 0 then
  28. y := starty;
  29. if width = 0 then
  30. width := 80;
  31. slength := Length(str);
  32. temp := (width - slength)/ 2;
  33. x := startx + round(temp);
  34. wattron(win, color);
  35. mvwaddstr(win, y, x, PChar(str));
  36. wattroff(win, color);
  37. refresh();
  38. end;
  39. (* Show the window with a border and a label *)
  40. procedure win_show(win: PWINDOW; lab: AnsiString; label_color: Longint);
  41. var
  42. startx, starty, height, width: Smallint;
  43. begin
  44. getbegyx(win, starty, startx);
  45. getmaxyx(win, height, width);
  46. box(win, 0, 0);
  47. mvwaddch(win, 2, 0, ACS_LTEE);
  48. mvwhline(win, 2, 1, ACS_HLINE, width - 2);
  49. mvwaddch(win, 2, width - 1, ACS_RTEE);
  50. print_in_middle(win, 1, 0, width, lab, COLOR_PAIR(label_color));
  51. end;
  52. (* Put all the windows *)
  53. procedure init_wins(wins: PPWINDOW; n: Longint);
  54. var
  55. x, y, i: Longint;
  56. lab: AnsiString;
  57. begin
  58. y := 2;
  59. x := 10;
  60. for i := 0 to n - 1 do
  61. begin
  62. wins[i] := newwin(NLINES, NCOLS, y, x);
  63. FmtStr(lab, 'Window Number %d', [i + 1]);
  64. win_show(wins[i], lab, i + 1);
  65. y += 3;
  66. x += 7;
  67. end
  68. end;
  69. var
  70. my_wins: array[0..2] of PWINDOW;
  71. my_panels: array[0..2] of PPANEL;
  72. panel_datas: array[0..2] of PANEL_DATA;
  73. temp: ^PANEL_DATA;
  74. ch: chtype;
  75. begin
  76. try
  77. (* Initialize curses *)
  78. initscr();
  79. start_color();
  80. cbreak();
  81. noecho();
  82. keypad(stdscr, TRUE);
  83. (* Initialize all the colors *)
  84. init_pair(1, COLOR_RED, COLOR_BLACK);
  85. init_pair(2, COLOR_GREEN, COLOR_BLACK);
  86. init_pair(3, COLOR_BLUE, COLOR_BLACK);
  87. init_pair(4, COLOR_CYAN, COLOR_BLACK);
  88. init_wins(my_wins, 3);
  89. (* Attach a panel to each window *) (* Order is bottom up *)
  90. my_panels[0] := new_panel(my_wins[0]); (* Push 0, order: stdscr-0 *)
  91. my_panels[1] := new_panel(my_wins[1]); (* Push 1, order: stdscr-0-1 *)
  92. my_panels[2] := new_panel(my_wins[2]); (* Push 2, order: stdscr-0-1-2 *)
  93. (* Initialize panel datas saying that nothing is hidden *)
  94. panel_datas[0].hide := FALSE;
  95. panel_datas[1].hide := FALSE;
  96. panel_datas[2].hide := FALSE;
  97. set_panel_userptr(my_panels[0], @panel_datas[0]);
  98. set_panel_userptr(my_panels[1], @panel_datas[1]);
  99. set_panel_userptr(my_panels[2], @panel_datas[2]);
  100. (* Update the stacking order. 2nd panel will be on top *)
  101. update_panels();
  102. (* Show it on the screen *)
  103. attron(COLOR_PAIR(4));
  104. mvprintw(LINES - 3, 0, 'Show or Hide a window with "a"(first window) "b"(Second Window) "c"(Third Window)');
  105. mvprintw(LINES - 2, 0, 'F1 to Exit');
  106. attroff(COLOR_PAIR(4));
  107. doupdate();
  108. ch := getch;
  109. while ch <> KEY_F(1) do
  110. begin
  111. case ch of
  112. chtype('a'):
  113. begin
  114. temp := panel_userptr(my_panels[0]);
  115. if temp^.hide = FALSE then
  116. begin
  117. hide_panel(my_panels[0]);
  118. temp^.hide := TRUE;
  119. end
  120. else
  121. begin
  122. show_panel(my_panels[0]);
  123. temp^.hide := FALSE;
  124. end
  125. end;
  126. chtype('b'):
  127. begin
  128. temp := panel_userptr(my_panels[1]);
  129. if temp^.hide = FALSE then
  130. begin
  131. hide_panel(my_panels[1]);
  132. temp^.hide := TRUE;
  133. end
  134. else
  135. begin
  136. show_panel(my_panels[1]);
  137. temp^.hide := FALSE;
  138. end
  139. end;
  140. chtype('c'):
  141. begin
  142. temp := panel_userptr(my_panels[2]);
  143. if temp^.hide = FALSE then
  144. begin
  145. hide_panel(my_panels[2]);
  146. temp^.hide := TRUE;
  147. end
  148. else
  149. begin
  150. show_panel(my_panels[2]);
  151. temp^.hide := FALSE;
  152. end
  153. end
  154. else
  155. end;
  156. update_panels();
  157. doupdate();
  158. ch := getch;
  159. end;
  160. finally
  161. endwin();
  162. end;
  163. end.