list.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. {
  2. $Id$
  3. This file extracted from the GTK tutorial.
  4. list.c
  5. Converted from C to Pascal by Frank Loemker
  6. <[email protected]>
  7. }
  8. program list;
  9. uses
  10. glib,gdk,gtk;
  11. { this is our data identification string to store data in list items }
  12. const
  13. list_item_data_key : pgchar = 'list_item_data';
  14. Function itos (I : Longint) : String;
  15. Var S : String[15];
  16. begin
  17. Str (I,S);
  18. itos:=S;
  19. end;
  20. { this is the signal handler that got connected to button
  21. press/release events of the GtkList }
  22. procedure sigh_button_event (gtklist: pGtkWidget;event:pGdkEventButton;frame:pGtkWidget);cdecl;
  23. var
  24. dlist, free_list : pGList;
  25. new_prisoner : pGtkWidget;
  26. list_item : pGtkWidget;
  27. begin
  28. { we only do something if the third (rightmost mouse button
  29. was released }
  30. if (event^.thetype=GDK_BUTTON_RELEASE) and
  31. (event^.button=1) then
  32. begin
  33. { fetch the currently selected list item which
  34. will be our next prisoner ;) }
  35. dlist := pGTKLIST(gtklist)^.selection;
  36. if dlist<>nil then
  37. new_prisoner := pGTKWIDGET(dlist^.data)
  38. else
  39. new_prisoner := nil;
  40. { look for already prisoned list items, we
  41. will put them back into the list
  42. remember to free the doubly linked list that
  43. gtk_container_children() returns }
  44. dlist := gtk_container_children(pGTKCONTAINER(frame));
  45. free_list := dlist;
  46. while dlist<>nil do
  47. begin
  48. list_item := dlist^.data;
  49. gtk_widget_reparent(list_item, gtklist);
  50. dlist := dlist^.next;
  51. end;
  52. g_list_free(free_list);
  53. { if we have a new prisoner, remove him from the
  54. GtkList and put him into the frame 'Prison'
  55. we need to unselect the item before }
  56. if new_prisoner<>nil then
  57. begin
  58. gtk_list_unselect_child(pGTKLIST(gtklist),new_prisoner);
  59. gtk_widget_reparent(new_prisoner, frame);
  60. end;
  61. end;
  62. end;
  63. { this is the signal handler that gets called if GtkList
  64. emits the 'selection_changed' signal }
  65. procedure sigh_print_selection (gtklist : pGtkWidget;func_data : gpointer);cdecl;
  66. var dlist : pGList;
  67. list_item : pGtkObject;
  68. item_data_string : pgchar;
  69. begin
  70. { fetch the doubly linked list of selected items
  71. of the GtkList, remember to treat this as read-only! }
  72. dlist := pGTKLIST(gtklist)^.selection;
  73. { if there are no selected items there is nothing more
  74. to do than just telling the user so }
  75. if dlist=nil then
  76. writeln ('Selection cleared')
  77. else
  78. begin
  79. { ok, we got a selection and so we print it }
  80. write ('The selection is a ');
  81. { get the list item from the doubly linked list
  82. and then query the data associated with list_item_data_key
  83. we then just print it }
  84. while dlist<>nil do
  85. begin
  86. list_item := pGTKOBJECT(dlist^.data);
  87. item_data_string := gtk_object_get_data(list_item,list_item_data_key);
  88. write (pchar(item_data_string),' ');
  89. dlist := dlist^.next;
  90. end;
  91. writeln;
  92. end;
  93. end;
  94. { main function to set up the user interface }
  95. var
  96. separator, window, vbox, scrolled_window,
  97. frame, thelist, button, list_item : pGtkWidget;
  98. dlist : pGList;
  99. i : guint;
  100. buffer : array [0..63] of gchar;
  101. thelabel : pGtkWidget;
  102. str : pgchar;
  103. begin
  104. { initialize gtk+ (and subsequently gdk) }
  105. gtk_init(@argc, @argv);
  106. gtk_rc_init;
  107. { create a window to put all the widgets in
  108. connect gtk_main_quit() to the 'destroy' event of
  109. the window to handle window manager close-window-events }
  110. window := gtk_window_new(GTK_WINDOW_TOPLEVEL);
  111. gtk_window_set_title(pGTKWINDOW(window), 'GtkList Example');
  112. gtk_signal_connect(pGTKOBJECT(window),'destroy',GTK_SIGNAL_FUNC(@gtk_main_quit),nil);
  113. { inside the window we need a box to arrange the widgets
  114. vertically }
  115. vbox := gtk_vbox_new(false, 5);
  116. gtk_container_set_border_width(pGTKCONTAINER(vbox), 5);
  117. gtk_container_add(pGTKCONTAINER(window), vbox);
  118. { this is the scolled window to put the GtkList widget inside }
  119. scrolled_window := gtk_scrolled_window_new(nil, nil);
  120. gtk_widget_set_usize(scrolled_window, 250, 150);
  121. gtk_box_pack_start (pGTKBOX(vbox), scrolled_window, true, true, 0);
  122. { create the GtkList widget
  123. connect the sigh_print_selection() signal handler
  124. function to the 'selection_changed' signal of the GtkList
  125. to print out the selected items each time the selection
  126. has changed }
  127. thelist := gtk_list_new();
  128. gtk_list_set_selection_mode (pGtkList(thelist),GTK_SELECTION_BROWSE);
  129. gtk_scrolled_window_add_with_viewport (pGtkScrolledWindow(scrolled_window), thelist);
  130. gtk_signal_connect(pGTKOBJECT(thelist),'selection_changed',GTK_SIGNAL_FUNC(@sigh_print_selection),nil);
  131. { we create a 'Prison' to put a list item in ;) }
  132. frame := gtk_frame_new('Prison');
  133. gtk_widget_set_usize(frame, 200, 50);
  134. gtk_container_set_border_width(pGTKCONTAINER(frame), 5);
  135. gtk_frame_set_shadow_type(pGTKFRAME(frame), GTK_SHADOW_OUT);
  136. gtk_box_pack_start (pGTKBOX(vbox), frame, false, true, 0);
  137. { connect the sigh_button_event() signal handler to the GtkList
  138. wich will handle the 'arresting' of list items }
  139. gtk_signal_connect(pGTKOBJECT(thelist),'button_release_event',GTK_SIGNAL_FUNC(@sigh_button_event),frame);
  140. { create a separator }
  141. separator := gtk_hseparator_new();
  142. gtk_box_pack_start (pGTKBOX(vbox), separator, false, true, 0);
  143. { finaly create a button and connect it's 'clicked' signal
  144. to the destroyment of the window }
  145. button := gtk_button_new_with_label('Close');
  146. gtk_box_pack_start (pGTKBOX(vbox), button, false, true, 0);
  147. gtk_signal_connect_object(pGTKOBJECT(button),'clicked',GTK_SIGNAL_FUNC(@gtk_widget_destroy),pGTKOBJECT(window));
  148. { now we create 5 list items, each having it´s own
  149. label and add them to the GtkList using gtk_container_add()
  150. also we query the text string from the label and
  151. associate it with the list_item_data_key for each list item }
  152. for i := 0 to 4 do
  153. begin
  154. buffer:='ListItemContainer with Label #'+itos(i)+#0;
  155. thelabel := gtk_label_new(buffer);
  156. list_item := gtk_list_item_new();
  157. gtk_container_add(pGTKCONTAINER(list_item), thelabel);
  158. gtk_container_add(pGTKCONTAINER(thelist), list_item);
  159. gtk_label_get(pGTKLABEL(thelabel), @str);
  160. gtk_object_set_data(pGTKOBJECT(list_item),list_item_data_key,str);
  161. end;
  162. { here, we are creating another 5 labels, this time
  163. we use gtk_list_item_new_with_label() for the creation
  164. we can't query the text string from the label because
  165. we don't have the labels pointer and therefore
  166. we just associate the list_item_data_key of each
  167. list item with the same text string
  168. for adding of the list items we put them all into a doubly
  169. linked list (GList), and then add them by a single call to
  170. gtk_list_append_items()
  171. because we use g_list_prepend() to put the items into the
  172. doubly linked list, their order will be descending (instead
  173. of ascending when using g_list_append()) }
  174. dlist := nil;
  175. for i:=5 to 9 do
  176. begin
  177. buffer:='List Item with Label '+itos(i)+#0;
  178. list_item := gtk_list_item_new_with_label(buffer);
  179. dlist := g_list_prepend(dlist, list_item);
  180. gtk_object_set_data(pGTKOBJECT(list_item),list_item_data_key,pchar('ListItem with integrated Label'));
  181. end;
  182. gtk_list_append_items(pGTKLIST(thelist), dlist);
  183. { finaly we want to see the window, don't we? ;) }
  184. gtk_widget_show_all(window);
  185. { fire up the main event loop of gtk }
  186. gtk_main();
  187. { we get here after gtk_main_quit() has been called which
  188. happens if the main window gets destroyed }
  189. end.
  190. {
  191. $Log$
  192. Revision 1.3 2000-09-09 20:59:15 peter
  193. * win32 updates
  194. Revision 1.2 2000/07/13 11:33:18 michael
  195. + removed logs
  196. }