testzvt.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. {
  2. TestZVT - An FPC Example Program demonstrating the most common use
  3. of ZVTTerm in a GNOME application.
  4. Copyright (C) 2002 Andrew Johnson <[email protected]>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. **********************************************************************}
  18. Program TestZVT;
  19. (* Try to Execute mc (midnight commander) instead of sh *)
  20. {$Define exec_mc}
  21. Uses
  22. SysUtils,
  23. { Linux/UNIX Unit, for execvp }
  24. {$IfDef ver1_0}linux{$Else}Unix{$EndIF},
  25. { Standard GTK+ 1.x Interface }
  26. glib, gdk, gtk,
  27. { Standard GNOME 1.x Interface }
  28. libgnome, libgnomeui,
  29. { Standard libzvt 1.x Interface }
  30. libzvt;
  31. const
  32. (* what to execvp in terminal widget *)
  33. {$Ifdef exec_mc}
  34. Command : PChar = 'mc';
  35. Params : array[0..1] of PChar = ('TERM=xterm', nil);
  36. {$else}
  37. Command : PChar = 'sh';
  38. Params : array[0..0] of PChar = (nil);
  39. {$EndIf}
  40. Terminals : Longint = 0;//# of terminals currently open
  41. (* Program Information for GNOME & About Box *)
  42. ProgramName : PChar = 'TestZVT';
  43. ProgramVersion : PChar = '1.0';
  44. (* Information for About Box *)
  45. Copyright : PChar = 'Copyright (C) 2002 Andrew Johnson';
  46. Authors : array[0..1] of PChar = ('Andrew Johnson <[email protected]>', nil);
  47. Comments : PChar = 'An FPC Example Program demonstrating the most common use of ZVTTerm in a GNOME application.';
  48. var
  49. app, mdichild : pointer;
  50. Procedure quit_testzvt(Widget : PGTKWidget; Data : Pointer); cdecl;
  51. begin
  52. (* Quite Main Loop *)
  53. gtk_main_quit;
  54. end;
  55. Procedure exit_terminal(Widget : PGTKWidget; Data : Pointer); cdecl;
  56. begin
  57. (* Destroy terminal on process exit, and quit if only terminal open *)
  58. gnome_mdi_remove_view(App, Data, 1);
  59. Dec(Terminals);
  60. end;
  61. Procedure close_activechild(Widget : PGTKWidget; Data : Pointer); cdecl;
  62. begin
  63. (* close active view *)
  64. exit_terminal(Widget, gnome_mdi_get_active_view(App));
  65. end;
  66. Procedure new_child(Widget : PGTKWidget; Data : Pointer); cdecl;
  67. begin
  68. (* create new view& set active *)
  69. gnome_mdi_add_view(app, mdichild);
  70. end;
  71. Procedure about_testzvt(Widget : PGTKWidget; Data : Pointer); cdecl;
  72. var
  73. AboutBox : Pointer;
  74. begin
  75. (* Create and Run an About Box *)
  76. AboutBox := gnome_about_new(gnome_app_id, ProgramVersion, Copyright,
  77. @Authors[0],Comments,nil);
  78. gnome_dialog_set_parent(AboutBox, GTK_Window(gnome_mdi_get_active_window(App)));
  79. gnome_dialog_run_and_close(AboutBox);
  80. end;
  81. Procedure show_terminal(Widget : PGTKWidget; Data : Pointer); cdecl;
  82. begin
  83. (* fork terminal process, and Exec Command *)
  84. If zvt_term_forkpty(ZVT_TERM(Widget), ZVT_TERM_DO_UTMP_LOG or ZVT_TERM_DO_WTMP_LOG or ZVT_TERM_DO_LASTLOG) = 0 then
  85. execvp (Command, @Command, @Params[0]);
  86. (* close app when fork'ed terminal process finishes/dies *)
  87. gtk_signal_connect (GTK_OBJECT(Widget), 'child_died', GTK_SIGNAL_FUNC (@exit_terminal), Data);
  88. end;
  89. Function NewTerminalView: PGTKWidget; cdecl;
  90. var
  91. hBox, SB, Term : gPointer;
  92. begin
  93. (* Create hbox for layout of Terminal/Scrollbar *)
  94. hBox := gtk_hbox_new(FALSE, 0);
  95. term := zvt_term_new_with_size(80,30);//start with average size
  96. (* Set up terminal options *)
  97. zvt_term_set_shadow_type(term, GTK_SHADOW_IN);//give the terminal a small indented frame
  98. zvt_term_set_font_name(term, '-misc-fixed-medium-r-normal-*-12-200-*-*-c-75-*-*');
  99. zvt_term_set_scrollback(term, 10000);//give a decent amount of scrollback
  100. zvt_term_set_scroll_on_keystroke(term, True);//default on most terminals
  101. zvt_term_set_scroll_on_output(term, False);//default on most terminals
  102. zvt_term_set_background(ZVT_TERM (term), nil, False, 0);//ensure is not transparent
  103. gtk_signal_connect_after(term, 'show', GTK_SIGNAL_FUNC (@show_terminal), hBox);
  104. (* Create scrollbar *)
  105. sb := gtk_vscrollbar_new(GTK_ADJUSTMENT (ZVT_TERM(term)^.adjustment));
  106. GTK_WIDGET_UNSET_FLAGS(sb, GTK_CAN_FOCUS);//Should never capture keyboard
  107. (* Pack Box *)
  108. gtk_box_pack_start(hBox, term, TRUE, TRUE, 0);
  109. gtk_box_pack_start(hBox, sb, FALSE, TRUE, 0);
  110. gtk_object_set_data(hbox, 'caption', Pchar('Terminal #' + IntToStr(Terminals)));
  111. gtk_widget_show_all(hBox);
  112. NewTerminalView := hBox;
  113. Inc(Terminals);
  114. end;
  115. Function CreateMDIChildWidget : Pointer;
  116. var
  117. child : Pointer;
  118. begin
  119. child := gnome_mdi_generic_child_new('Terminal');
  120. gnome_mdi_generic_child_set_view_creator(child, @NewTerminalView, nil);
  121. CreateMDIChildWidget := child;
  122. end;
  123. var
  124. file_menu : array[0..4] of TGnomeUIInfo;
  125. help_menu : array[0..1] of TGnomeUIInfo;
  126. Menus : array[0..2] of TGnomeUIInfo;
  127. begin
  128. (* Initialize GNOME with Current Program Name and Version *)
  129. gnome_init(ProgramName, ProgramVersion, argc, argv);
  130. (* Create Main App *)
  131. app := gnome_mdi_new(gnome_app_id, 'FPC GNOME ZVT Test');
  132. gtk_signal_connect(app, 'destroy', GTK_SIGNAL_FUNC (@quit_testzvt), nil);
  133. (* Create Stock Menus *)
  134. file_menu[0] := GNOMEUIINFO_MENU_NEW_ITEM('New Shell Process', 'Opens a new shell process', @new_child, nil);
  135. file_menu[1] := GNOMEUIINFO_MENU_CLOSE_ITEM(@close_activechild,nil);
  136. file_menu[2] := GNOMEUIINFO_SEPARATOR;
  137. file_menu[3] := GNOMEUIINFO_MENU_EXIT_ITEM(@quit_testzvt,nil);
  138. file_menu[4] := GNOMEUIINFO_END;
  139. help_menu[0] := GNOMEUIINFO_MENU_ABOUT_ITEM(@about_testzvt, app);
  140. help_menu[1] := GNOMEUIINFO_END;
  141. menus[0] := GNOMEUIINFO_MENU_FILE_TREE(@file_menu[0]);
  142. menus[1] := GNOMEUIINFO_MENU_HELP_TREE(@help_menu[0]);
  143. menus[2] := GNOMEUIINFO_END;
  144. mdichild := CreateMDIChildWidget;
  145. (* Set App Menu/Contents, and Show All *)
  146. gnome_mdi_set_mode(App, GNOME_MDI_NOTEBOOK);
  147. gnome_mdi_set_menubar_template(App, @Menus[0]);
  148. gnome_mdi_open_toplevel(app);
  149. gnome_mdi_add_child(app, mdichild);
  150. gnome_mdi_add_view(app, mdichild);
  151. gnome_mdi_add_view(app, mdichild);
  152. (* Run Main Loop *)
  153. gtk_main();
  154. (* cleanup and exit *)
  155. gtk_exit(0);
  156. end.