2
0

clist.pp 5.1 KB

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