list.pp 7.6 KB

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