menuselect_gtk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <gtk/gtk.h>
  5. #include "menuselect.h"
  6. enum {
  7. /*! The row name */
  8. COLUMN_NAME,
  9. /*! Whether this row is enabled */
  10. COLUMN_SELECTED,
  11. /*! Dependencies */
  12. COLUMN_DEPS,
  13. /*! Optional dependencies */
  14. COLUMN_USES,
  15. /*! Conflicts */
  16. COLUMN_CNFS,
  17. /*! Description */
  18. COLUMN_DESC,
  19. /*! Number of columns, must be the last element in the enum */
  20. NUM_COLUMNS,
  21. };
  22. static void handle_save(GtkWidget *w, gpointer data);
  23. static void handle_about(GtkWidget *w, gpointer data);
  24. static void handle_quit(GtkWidget *w, gpointer data);
  25. static GtkItemFactoryEntry menu_items[] = {
  26. { "/_File", NULL, NULL, 0, "<Branch>" },
  27. { "/File/_Save And Quit", "<control>S", handle_save, 0, "<StockItem>", GTK_STOCK_SAVE },
  28. { "/File/sep1", NULL, NULL, 0, "<Separator>" },
  29. { "/File/_Quit", "<CTRL>Q", handle_quit, 0, "<StockItem>", GTK_STOCK_QUIT },
  30. { "/_Help", NULL, NULL, 0, "<LastBranch>" },
  31. { "/_Help/About", NULL, handle_about, 0, "<Item>" },
  32. };
  33. static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
  34. static GtkTreeView *tree;
  35. static GtkWidget *window;
  36. /* 0, save ... non-zero, don't save */
  37. static int main_res = 1;
  38. static int change_made = 0;
  39. static void handle_save(GtkWidget *w, gpointer data)
  40. {
  41. main_res = 0;
  42. gtk_main_quit();
  43. }
  44. static void handle_about(GtkWidget *w, gpointer data)
  45. {
  46. GtkWidget *dialog;
  47. dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
  48. GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
  49. "GMenuselect - http://www.asterisk.org/\n"
  50. "Russell Bryant <russell@digium.com>\n"
  51. "Copyright (C) 2007\n");
  52. gtk_dialog_run(GTK_DIALOG(dialog));
  53. gtk_widget_destroy(dialog);
  54. }
  55. static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
  56. {
  57. return FALSE;
  58. }
  59. static void handle_quit(GtkWidget *widget, gpointer data)
  60. {
  61. gtk_main_quit();
  62. }
  63. static void destroy(GtkWidget *widget, gpointer data)
  64. {
  65. GtkWidget *dialog;
  66. gint response;
  67. if (!main_res || !change_made) {
  68. gtk_main_quit();
  69. return;
  70. }
  71. dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_MODAL,
  72. GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "Save before quit?");
  73. response = gtk_dialog_run(GTK_DIALOG(dialog));
  74. gtk_widget_destroy(dialog);
  75. if (response == GTK_RESPONSE_YES)
  76. main_res = 0;
  77. gtk_main_quit();
  78. }
  79. static void toggled_handler(GtkCellRendererToggle *renderer, gchar *path, gpointer data)
  80. {
  81. gchar *cat_num_str, *mem_num_str;
  82. int cat_num, mem_num;
  83. int i = 0;
  84. struct category *cat;
  85. struct member *mem;
  86. GtkTreeStore *store = data;
  87. GtkTreeModel *model;
  88. GtkTreeIter cat_iter, mem_iter;
  89. mem_num_str = alloca(strlen(path)) + 1;
  90. strcpy(mem_num_str, path);
  91. cat_num_str = strsep(&mem_num_str, ":");
  92. if (!mem_num_str || !*mem_num_str)
  93. return;
  94. cat_num = atoi(cat_num_str);
  95. mem_num = atoi(mem_num_str);
  96. AST_LIST_TRAVERSE(&categories, cat, list) {
  97. if (i == cat_num)
  98. break;
  99. i++;
  100. }
  101. if (!cat)
  102. return;
  103. i = 0;
  104. AST_LIST_TRAVERSE(&cat->members, mem, list) {
  105. if (i == mem_num)
  106. break;
  107. i++;
  108. }
  109. if (!mem)
  110. return;
  111. toggle_enabled(mem);
  112. model = gtk_tree_view_get_model(tree);
  113. gtk_tree_model_get_iter_first(model, &cat_iter);
  114. for (i = 0; i < cat_num; i++) {
  115. if (!gtk_tree_model_iter_next(model, &cat_iter))
  116. break;
  117. }
  118. if (i != cat_num)
  119. return;
  120. if (!gtk_tree_model_iter_children(model, &mem_iter, &cat_iter))
  121. return;
  122. for (i = 0; i < mem_num; i++) {
  123. if (!gtk_tree_model_iter_next(model, &mem_iter))
  124. break;
  125. }
  126. if (i != mem_num)
  127. return;
  128. gtk_tree_store_set(store, &mem_iter, COLUMN_SELECTED, mem->enabled, -1);
  129. change_made = 1;
  130. }
  131. static void row_activated_handler(GtkTreeView *treeview, GtkTreePath *path,
  132. GtkTreeViewColumn *col, gpointer data)
  133. {
  134. GtkTreeIter iter;
  135. GtkTreeModel *model;
  136. GtkTreeStore *store = data;
  137. gchar *name;
  138. struct category *cat;
  139. struct member *mem;
  140. model = gtk_tree_view_get_model(treeview);
  141. if (!gtk_tree_model_get_iter(model, &iter, path))
  142. return;
  143. gtk_tree_model_get(model, &iter, COLUMN_NAME, &name, -1);
  144. AST_LIST_TRAVERSE(&categories, cat, list) {
  145. AST_LIST_TRAVERSE(&cat->members, mem, list) {
  146. if (strcmp(name, mem->name))
  147. continue;
  148. toggle_enabled(mem);
  149. gtk_tree_store_set(store, &iter, COLUMN_SELECTED, mem->enabled, -1);
  150. change_made = 1;
  151. break;
  152. }
  153. if (mem)
  154. break;
  155. }
  156. g_free(name);
  157. }
  158. static GtkWidget *get_menubar_menu(GtkWidget *window)
  159. {
  160. GtkItemFactory *item_factory;
  161. GtkAccelGroup *accel_group;
  162. /* Make an accelerator group (shortcut keys) */
  163. accel_group = gtk_accel_group_new();
  164. /* Make an ItemFactory (that makes a menubar) */
  165. item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>",
  166. accel_group);
  167. /* This function generates the menu items. Pass the item factory,
  168. the number of items in the array, the array itself, and any
  169. callback data for the menu items. */
  170. gtk_item_factory_create_items(item_factory, nmenu_items, menu_items, NULL);
  171. /* Attach the new accelerator group to the window. */
  172. gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
  173. /* Finally, return the actual menu bar created by the item factory. */
  174. return gtk_item_factory_get_widget(item_factory, "<main>");
  175. }
  176. int run_menu(void)
  177. {
  178. int argc = 0;
  179. char **argv = NULL;
  180. GtkWidget *s_window;
  181. GtkCellRenderer *renderer;
  182. GtkTreeViewColumn *column;
  183. GtkTreeStore *store;
  184. struct category *cat;
  185. struct member *mem;
  186. GtkWidget *main_vbox;
  187. GtkWidget *menubar;
  188. gtk_init(&argc, &argv);
  189. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  190. gtk_widget_set_size_request(window, 640, 480);
  191. gtk_window_set_title(GTK_WINDOW(window), "GMenuselect");
  192. main_vbox = gtk_vbox_new(FALSE, 1);
  193. gtk_container_set_border_width(GTK_CONTAINER(main_vbox), 1);
  194. gtk_container_add(GTK_CONTAINER(window), main_vbox);
  195. menubar = get_menubar_menu(window);
  196. gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, FALSE, 0);
  197. s_window = gtk_scrolled_window_new(NULL, NULL);
  198. g_signal_connect(G_OBJECT(window), "delete_event",
  199. G_CALLBACK(delete_event), NULL);
  200. g_signal_connect(G_OBJECT(window), "destroy",
  201. G_CALLBACK(destroy), NULL);
  202. store = gtk_tree_store_new(NUM_COLUMNS,
  203. G_TYPE_STRING, /* COLUMN_NAME */
  204. G_TYPE_BOOLEAN, /* COLUMN_SELECTED */
  205. G_TYPE_STRING, /* COLUMN_DEPS */
  206. G_TYPE_STRING, /* COLUMN_USES */
  207. G_TYPE_STRING, /* COLUMN_CNFS */
  208. G_TYPE_STRING); /* COLUMN_DESC */
  209. AST_LIST_TRAVERSE(&categories, cat, list) {
  210. GtkTreeIter iter, iter2;
  211. gtk_tree_store_append(store, &iter, NULL);
  212. gtk_tree_store_set(store, &iter,
  213. COLUMN_NAME, cat->displayname,
  214. COLUMN_SELECTED, TRUE,
  215. -1);
  216. AST_LIST_TRAVERSE(&cat->members, mem, list) {
  217. char name_buf[64];
  218. char dep_buf[64] = "";
  219. char use_buf[64] = "";
  220. char cnf_buf[64] = "";
  221. struct reference *dep;
  222. struct reference *use;
  223. struct reference *cnf;
  224. AST_LIST_TRAVERSE(&mem->deps, dep, list) {
  225. strncat(dep_buf, dep->displayname, sizeof(dep_buf) - strlen(dep_buf) - 1);
  226. strncat(dep_buf, dep->member ? "(M)" : "(E)", sizeof(dep_buf) - strlen(dep_buf) - 1);
  227. if (AST_LIST_NEXT(dep, list))
  228. strncat(dep_buf, ", ", sizeof(dep_buf) - strlen(dep_buf) - 1);
  229. }
  230. AST_LIST_TRAVERSE(&mem->uses, use, list) {
  231. strncat(use_buf, use->displayname, sizeof(use_buf) - strlen(use_buf) - 1);
  232. if (AST_LIST_NEXT(use, list))
  233. strncat(use_buf, ", ", sizeof(use_buf) - strlen(use_buf) - 1);
  234. }
  235. AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
  236. strncat(cnf_buf, cnf->displayname, sizeof(cnf_buf) - strlen(cnf_buf) - 1);
  237. strncat(cnf_buf, cnf->member ? "(M)" : "(E)", sizeof(cnf_buf) - strlen(cnf_buf) - 1);
  238. if (AST_LIST_NEXT(cnf, list))
  239. strncat(cnf_buf, ", ", sizeof(cnf_buf) - strlen(cnf_buf) - 1);
  240. }
  241. if (mem->is_separator) {
  242. snprintf(name_buf, sizeof(name_buf), "--- %s ---", mem->name);
  243. } else {
  244. snprintf(name_buf, sizeof(name_buf), "%s", mem->name);
  245. }
  246. if (mem->depsfailed == HARD_FAILURE)
  247. strncat(name_buf, " (Failed Deps.)", sizeof(name_buf) - strlen(name_buf) - 1);
  248. if (mem->conflictsfailed == HARD_FAILURE)
  249. strncat(name_buf, " (In Conflict)", sizeof(name_buf) - strlen(name_buf) - 1);
  250. gtk_tree_store_append(store, &iter2, &iter);
  251. gtk_tree_store_set(store, &iter2,
  252. COLUMN_NAME, name_buf,
  253. COLUMN_SELECTED, mem->enabled,
  254. COLUMN_DEPS, dep_buf,
  255. COLUMN_USES, use_buf,
  256. COLUMN_CNFS, cnf_buf,
  257. COLUMN_DESC, mem->displayname,
  258. -1);
  259. }
  260. }
  261. tree = (GtkTreeView *) gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
  262. #if GTK_CHECK_VERSION(2,10,0)
  263. gtk_tree_view_set_enable_tree_lines(tree, TRUE);
  264. gtk_tree_view_set_grid_lines(tree, GTK_TREE_VIEW_GRID_LINES_BOTH);
  265. #endif
  266. renderer = gtk_cell_renderer_text_new();
  267. column = gtk_tree_view_column_new_with_attributes("Name",
  268. renderer, "text", COLUMN_NAME, NULL);
  269. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  270. renderer = gtk_cell_renderer_toggle_new();
  271. column = gtk_tree_view_column_new_with_attributes("Selected",
  272. renderer, "active", COLUMN_SELECTED, NULL);
  273. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  274. g_signal_connect(renderer, "toggled", (GCallback) toggled_handler, store);
  275. renderer = gtk_cell_renderer_text_new();
  276. column = gtk_tree_view_column_new_with_attributes("Depends On",
  277. renderer, "text", COLUMN_DEPS, NULL);
  278. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  279. renderer = gtk_cell_renderer_text_new();
  280. column = gtk_tree_view_column_new_with_attributes("Can Use",
  281. renderer, "text", COLUMN_USES, NULL);
  282. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  283. renderer = gtk_cell_renderer_text_new();
  284. column = gtk_tree_view_column_new_with_attributes("Conflicts With",
  285. renderer, "text", COLUMN_CNFS, NULL);
  286. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  287. renderer = gtk_cell_renderer_text_new();
  288. column = gtk_tree_view_column_new_with_attributes("Description",
  289. renderer, "text", COLUMN_DESC, NULL);
  290. gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  291. g_signal_connect(tree, "row-activated", (GCallback) row_activated_handler, store);
  292. gtk_container_add(GTK_CONTAINER(s_window), GTK_WIDGET(tree));
  293. gtk_box_pack_end(GTK_BOX(main_vbox), s_window, TRUE, TRUE, 0);
  294. gtk_widget_show_all(window);
  295. gtk_main();
  296. return main_res;
  297. }