list.pp 8.5 KB

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