clist.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. $Id$
  3. This file extracted from the Gtk tutorial.
  4. clist.c
  5. Converted from C to Pascal by Frank Loemker
  6. <[email protected]>
  7. }
  8. program clist;
  9. uses
  10. glib,Gdk,Gtk;
  11. { User clicked the 'Add List' button. }
  12. procedure button_add_clicked (data: PGtkCList ); cdecl;
  13. { Something silly to add to the list. 4 rows of 2 columns each }
  14. const drink : array[0..3,0..1] of Pgchar =
  15. (('Milk', '3 Oz'),
  16. ('Water', '6 l'),
  17. ('Carrots', '2'),
  18. ('Snakes', '55'));
  19. var indx : integer ;
  20. begin
  21. { Here we do the actual adding of the text. It's done once for
  22. each row. }
  23. for indx:=0 to 3 do
  24. gtk_clist_append (data, @drink[indx]);
  25. end;
  26. { User clicked the 'Clear List' button. }
  27. procedure button_clear_clicked (data : PGtkCList ); cdecl;
  28. begin
  29. { Clear the list using gtk_clist_clear. This is much faster than
  30. calling gtk_clist_remove once for each row. }
  31. gtk_clist_clear (data);
  32. end;
  33. { The user clicked the 'Hide/Show titles' button. }
  34. procedure button_hide_show_clicked (data : PGtkCList ); cdecl;
  35. const flag:integer = 0;
  36. begin
  37. { Just a flag to remember the status. 0 = currently visible }
  38. if flag = 0 then begin
  39. { Hide the titles and set the flag to 1 }
  40. gtk_clist_column_titles_hide (data);
  41. inc (flag);
  42. end else begin
  43. { Show the titles and reset flag to 0 }
  44. gtk_clist_column_titles_show (data);
  45. dec (flag);
  46. end;
  47. end;
  48. { If we come here, then the user has selected a row in the list. }
  49. procedure selection_made (thelist : PGtkCLIST ; row, column: gint;
  50. event : PGdkEventButton ; data : gpointer); cdecl;
  51. var text : Pgchar;
  52. begin
  53. { Get the text that is stored in the selected row and column
  54. which was clicked in. We will receive it as a pointer in the
  55. argument text. }
  56. gtk_clist_get_text(thelist, row, column, @text);
  57. { Just prints some information about the selected row }
  58. writeln ('You selected row ',row,
  59. '. More specifically you clicked in column ',column,
  60. ', and the text in this cell is ',text,#10);
  61. end;
  62. const
  63. titles: array[0..1] of Pgchar = ('Ingredients','Amount');
  64. var
  65. window,vbox,hbox,scroll, thelist,
  66. button_add, button_clear,button_hide_show : PGtkWidget;
  67. begin
  68. gtk_init (@argc, @argv);
  69. gtk_rc_init;
  70. window := gtk_window_new(gtk_WINDOW_TOPLEVEL);
  71. gtk_widget_set_usize(PGtkWIDGET(window), 300, 150);
  72. gtk_window_set_title(PGtkWINDOW(window), 'GtkCList Example');
  73. gtk_signal_connect(PGtkOBJECT(window),'destroy',
  74. tGtksignalfunc(@gtk_main_quit),
  75. NIL);
  76. vbox := gtk_vbox_new(false, 5);
  77. gtk_container_set_border_width(PGtkCONTAINER(vbox), 5);
  78. gtk_container_add(PGtkCONTAINER(window), vbox);
  79. { Create the ScrolledWindow to pack the CList in. }
  80. scroll := gtk_scrolled_window_new (NULL,NULL);
  81. gtk_scrolled_window_set_policy (PGtkSCROLLEDWINDOW(scroll),
  82. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  83. gtk_box_pack_start(PGtkBOX(vbox), scroll, true, true, 0);
  84. { Create the GtkCList. For this example we use 2 columns }
  85. thelist := gtk_clist_new_with_titles (2,titles);
  86. gtk_container_add (PGtkContainer(scroll),thelist);
  87. { When a selection is made, we want to know about it. The callback
  88. used is selection_made, and it's code can be found above }
  89. gtk_signal_connect(PGtkOBJECT(thelist), 'select_row',
  90. tGtksignalfunc(@selection_made),
  91. NIL);
  92. { It isn't necessary to shadow the border, but it looks nice :) }
  93. gtk_clist_set_shadow_type(PGtkCLIST(thelist), gtk_SHADOW_OUT);
  94. { What however is important, is that we set the column widths as
  95. they will never be right otherwise. Note that the columns are
  96. numbered from 0 and up (to 1 in this case). }
  97. gtk_clist_set_column_width (PGtkCLIST(thelist), 0, 150);
  98. gtk_clist_set_column_width (PGtkCLIST(thelist), 1, 100);
  99. { Create the buttons and add them to the window. See the button
  100. tutorial for more examples and comments on this. }
  101. hbox := gtk_hbox_new(false, 0);
  102. gtk_box_pack_start(PGtkBOX(vbox), hbox, false, true, 0);
  103. button_add := gtk_button_new_with_label('Add List');
  104. button_clear := gtk_button_new_with_label('Clear List');
  105. button_hide_show := gtk_button_new_with_label('Hide/Show titles');
  106. gtk_box_pack_start (PGtkBOX(hbox), button_add, true, true, 0);
  107. gtk_box_pack_start (PGtkBOX(hbox), button_clear, true, true, 0);
  108. gtk_box_pack_start (PGtkBOX(hbox), button_hide_show, true, true, 0);
  109. { Connect our callbacks to the three buttons }
  110. gtk_signal_connect_object(PGtkOBJECT(button_add), 'clicked',
  111. tGtksignalfunc(@button_add_clicked),
  112. gpointer(thelist));
  113. gtk_signal_connect_object(PGtkOBJECT(button_clear), 'clicked',
  114. tGtksignalfunc(@button_clear_clicked),
  115. gpointer (thelist));
  116. gtk_signal_connect_object(PGtkOBJECT(button_hide_show), 'clicked',
  117. tGtksignalfunc(@button_hide_show_clicked),
  118. gpointer (thelist));
  119. { The interface is completely set up so we show all the widgets and
  120. enter the gtk_main loop }
  121. gtk_widget_show_all(window);
  122. gtk_main();
  123. end.
  124. {
  125. $Log$
  126. Revision 1.2 2000-07-13 11:33:18 michael
  127. + removed logs
  128. }