1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002-2003 Romain Lievin <roms@tilp.info>
10 #include <glade/glade.h>
13 #include <gdk/gdkkeysyms.h>
24 SINGLE_VIEW, SPLIT_VIEW, FULL_VIEW
28 OPT_NORMAL, OPT_ALL, OPT_PROMPT
31 static gint view_mode = FULL_VIEW;
32 static gboolean show_name = TRUE;
33 static gboolean show_range = TRUE;
34 static gboolean show_value = TRUE;
35 static gboolean resizeable = FALSE;
36 static int opt_mode = OPT_NORMAL;
38 GtkWidget *main_wnd = NULL;
39 GtkWidget *tree1_w = NULL; // left frame
40 GtkWidget *tree2_w = NULL; // right frame
41 GtkWidget *text_w = NULL;
42 GtkWidget *hpaned = NULL;
43 GtkWidget *vpaned = NULL;
44 GtkWidget *back_btn = NULL;
45 GtkWidget *save_btn = NULL;
46 GtkWidget *save_menu_item = NULL;
48 GtkTextTag *tag1, *tag2;
51 GtkTreeStore *tree1, *tree2, *tree;
52 GtkTreeModel *model1, *model2;
53 static GtkTreeIter *parents[256];
56 static struct menu *current; // current node for SINGLE view
57 static struct menu *browsed; // browsed node for SPLIT view
60 COL_OPTION, COL_NAME, COL_NO, COL_MOD, COL_YES, COL_VALUE,
61 COL_MENU, COL_COLOR, COL_EDIT, COL_PIXBUF,
62 COL_PIXVIS, COL_BTNVIS, COL_BTNACT, COL_BTNINC, COL_BTNRAD,
66 static void display_list(void);
67 static void display_tree(struct menu *menu);
68 static void display_tree_part(void);
69 static void update_tree(struct menu *src, GtkTreeIter * dst);
70 static void set_node(GtkTreeIter * node, struct menu *menu, gchar ** row);
71 static gchar **fill_row(struct menu *menu);
72 static void conf_changed(void);
74 /* Helping/Debugging Functions */
76 static const char *dbg_sym_flags(int val)
82 if (val & SYMBOL_CONST)
83 strcat(buf, "const/");
84 if (val & SYMBOL_CHECK)
85 strcat(buf, "check/");
86 if (val & SYMBOL_CHOICE)
87 strcat(buf, "choice/");
88 if (val & SYMBOL_CHOICEVAL)
89 strcat(buf, "choiceval/");
90 if (val & SYMBOL_VALID)
91 strcat(buf, "valid/");
92 if (val & SYMBOL_OPTIONAL)
93 strcat(buf, "optional/");
94 if (val & SYMBOL_WRITE)
95 strcat(buf, "write/");
96 if (val & SYMBOL_CHANGED)
97 strcat(buf, "changed/");
98 if (val & SYMBOL_NO_WRITE)
99 strcat(buf, "no_write/");
101 buf[strlen(buf) - 1] = '\0';
107 static void replace_button_icon(GladeXML *xml, GdkDrawable *window,
108 GtkStyle *style, gchar *btn_name, gchar **xpm)
112 GtkToolButton *button;
115 pixmap = gdk_pixmap_create_from_xpm_d(window, &mask,
116 &style->bg[GTK_STATE_NORMAL],
119 button = GTK_TOOL_BUTTON(glade_xml_get_widget(xml, btn_name));
120 image = gtk_image_new_from_pixmap(pixmap, mask);
121 gtk_widget_show(image);
122 gtk_tool_button_set_icon_widget(button, image);
125 /* Main Window Initialization */
126 static void init_main_window(const gchar *glade_file)
130 GtkTextBuffer *txtbuf;
133 xml = glade_xml_new(glade_file, "window1", NULL);
135 g_error("GUI loading failed !\n");
136 glade_xml_signal_autoconnect(xml);
138 main_wnd = glade_xml_get_widget(xml, "window1");
139 hpaned = glade_xml_get_widget(xml, "hpaned1");
140 vpaned = glade_xml_get_widget(xml, "vpaned1");
141 tree1_w = glade_xml_get_widget(xml, "treeview1");
142 tree2_w = glade_xml_get_widget(xml, "treeview2");
143 text_w = glade_xml_get_widget(xml, "textview3");
145 back_btn = glade_xml_get_widget(xml, "button1");
146 gtk_widget_set_sensitive(back_btn, FALSE);
148 widget = glade_xml_get_widget(xml, "show_name1");
149 gtk_check_menu_item_set_active((GtkCheckMenuItem *) widget,
152 widget = glade_xml_get_widget(xml, "show_range1");
153 gtk_check_menu_item_set_active((GtkCheckMenuItem *) widget,
156 widget = glade_xml_get_widget(xml, "show_data1");
157 gtk_check_menu_item_set_active((GtkCheckMenuItem *) widget,
160 save_btn = glade_xml_get_widget(xml, "button3");
161 save_menu_item = glade_xml_get_widget(xml, "save1");
162 conf_set_changed_callback(conf_changed);
164 style = gtk_widget_get_style(main_wnd);
165 widget = glade_xml_get_widget(xml, "toolbar1");
167 replace_button_icon(xml, main_wnd->window, style,
168 "button4", (gchar **) xpm_single_view);
169 replace_button_icon(xml, main_wnd->window, style,
170 "button5", (gchar **) xpm_split_view);
171 replace_button_icon(xml, main_wnd->window, style,
172 "button6", (gchar **) xpm_tree_view);
174 txtbuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_w));
175 tag1 = gtk_text_buffer_create_tag(txtbuf, "mytag1",
177 "weight", PANGO_WEIGHT_BOLD,
179 tag2 = gtk_text_buffer_create_tag(txtbuf, "mytag2",
180 /*"style", PANGO_STYLE_OBLIQUE, */
183 gtk_window_set_title(GTK_WINDOW(main_wnd), rootmenu.prompt->text);
185 gtk_widget_show(main_wnd);
188 static void init_tree_model(void)
192 tree = tree2 = gtk_tree_store_new(COL_NUMBER,
193 G_TYPE_STRING, G_TYPE_STRING,
194 G_TYPE_STRING, G_TYPE_STRING,
195 G_TYPE_STRING, G_TYPE_STRING,
196 G_TYPE_POINTER, GDK_TYPE_COLOR,
197 G_TYPE_BOOLEAN, GDK_TYPE_PIXBUF,
198 G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
199 G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
201 model2 = GTK_TREE_MODEL(tree2);
203 for (parents[0] = NULL, i = 1; i < 256; i++)
204 parents[i] = (GtkTreeIter *) g_malloc(sizeof(GtkTreeIter));
206 tree1 = gtk_tree_store_new(COL_NUMBER,
207 G_TYPE_STRING, G_TYPE_STRING,
208 G_TYPE_STRING, G_TYPE_STRING,
209 G_TYPE_STRING, G_TYPE_STRING,
210 G_TYPE_POINTER, GDK_TYPE_COLOR,
211 G_TYPE_BOOLEAN, GDK_TYPE_PIXBUF,
212 G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
213 G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
215 model1 = GTK_TREE_MODEL(tree1);
218 static void init_left_tree(void)
220 GtkTreeView *view = GTK_TREE_VIEW(tree1_w);
221 GtkCellRenderer *renderer;
222 GtkTreeSelection *sel;
223 GtkTreeViewColumn *column;
225 gtk_tree_view_set_model(view, model1);
226 gtk_tree_view_set_headers_visible(view, TRUE);
227 gtk_tree_view_set_rules_hint(view, TRUE);
229 column = gtk_tree_view_column_new();
230 gtk_tree_view_append_column(view, column);
231 gtk_tree_view_column_set_title(column, "Options");
233 renderer = gtk_cell_renderer_toggle_new();
234 gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
236 gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
238 "active", COL_BTNACT,
239 "inconsistent", COL_BTNINC,
240 "visible", COL_BTNVIS,
241 "radio", COL_BTNRAD, NULL);
242 renderer = gtk_cell_renderer_text_new();
243 gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
245 gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
251 sel = gtk_tree_view_get_selection(view);
252 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
253 gtk_widget_realize(tree1_w);
256 static void renderer_edited(GtkCellRendererText * cell,
257 const gchar * path_string,
258 const gchar * new_text, gpointer user_data);
260 static void init_right_tree(void)
262 GtkTreeView *view = GTK_TREE_VIEW(tree2_w);
263 GtkCellRenderer *renderer;
264 GtkTreeSelection *sel;
265 GtkTreeViewColumn *column;
268 gtk_tree_view_set_model(view, model2);
269 gtk_tree_view_set_headers_visible(view, TRUE);
270 gtk_tree_view_set_rules_hint(view, TRUE);
272 column = gtk_tree_view_column_new();
273 gtk_tree_view_append_column(view, column);
274 gtk_tree_view_column_set_title(column, "Options");
276 renderer = gtk_cell_renderer_pixbuf_new();
277 gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
279 gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
281 "pixbuf", COL_PIXBUF,
282 "visible", COL_PIXVIS, NULL);
283 renderer = gtk_cell_renderer_toggle_new();
284 gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
286 gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
288 "active", COL_BTNACT,
289 "inconsistent", COL_BTNINC,
290 "visible", COL_BTNVIS,
291 "radio", COL_BTNRAD, NULL);
292 renderer = gtk_cell_renderer_text_new();
293 gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
295 gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
301 renderer = gtk_cell_renderer_text_new();
302 gtk_tree_view_insert_column_with_attributes(view, -1,
307 renderer = gtk_cell_renderer_text_new();
308 gtk_tree_view_insert_column_with_attributes(view, -1,
313 renderer = gtk_cell_renderer_text_new();
314 gtk_tree_view_insert_column_with_attributes(view, -1,
319 renderer = gtk_cell_renderer_text_new();
320 gtk_tree_view_insert_column_with_attributes(view, -1,
325 renderer = gtk_cell_renderer_text_new();
326 gtk_tree_view_insert_column_with_attributes(view, -1,
333 g_signal_connect(G_OBJECT(renderer), "edited",
334 G_CALLBACK(renderer_edited), NULL);
336 column = gtk_tree_view_get_column(view, COL_NAME);
337 gtk_tree_view_column_set_visible(column, show_name);
338 column = gtk_tree_view_get_column(view, COL_NO);
339 gtk_tree_view_column_set_visible(column, show_range);
340 column = gtk_tree_view_get_column(view, COL_MOD);
341 gtk_tree_view_column_set_visible(column, show_range);
342 column = gtk_tree_view_get_column(view, COL_YES);
343 gtk_tree_view_column_set_visible(column, show_range);
344 column = gtk_tree_view_get_column(view, COL_VALUE);
345 gtk_tree_view_column_set_visible(column, show_value);
348 for (i = 0; i < COL_VALUE; i++) {
349 column = gtk_tree_view_get_column(view, i);
350 gtk_tree_view_column_set_resizable(column, TRUE);
354 sel = gtk_tree_view_get_selection(view);
355 gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
359 /* Utility Functions */
362 static void text_insert_help(struct menu *menu)
364 GtkTextBuffer *buffer;
365 GtkTextIter start, end;
366 const char *prompt = menu_get_prompt(menu);
367 struct gstr help = str_new();
369 menu_get_ext_help(menu, &help);
371 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_w));
372 gtk_text_buffer_get_bounds(buffer, &start, &end);
373 gtk_text_buffer_delete(buffer, &start, &end);
374 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_w), 15);
376 gtk_text_buffer_get_end_iter(buffer, &end);
377 gtk_text_buffer_insert_with_tags(buffer, &end, prompt, -1, tag1,
379 gtk_text_buffer_insert_at_cursor(buffer, "\n\n", 2);
380 gtk_text_buffer_get_end_iter(buffer, &end);
381 gtk_text_buffer_insert_with_tags(buffer, &end, str_get(&help), -1, tag2,
387 static void text_insert_msg(const char *title, const char *message)
389 GtkTextBuffer *buffer;
390 GtkTextIter start, end;
391 const char *msg = message;
393 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_w));
394 gtk_text_buffer_get_bounds(buffer, &start, &end);
395 gtk_text_buffer_delete(buffer, &start, &end);
396 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_w), 15);
398 gtk_text_buffer_get_end_iter(buffer, &end);
399 gtk_text_buffer_insert_with_tags(buffer, &end, title, -1, tag1,
401 gtk_text_buffer_insert_at_cursor(buffer, "\n\n", 2);
402 gtk_text_buffer_get_end_iter(buffer, &end);
403 gtk_text_buffer_insert_with_tags(buffer, &end, msg, -1, tag2,
408 /* Main Windows Callbacks */
410 void on_save_activate(GtkMenuItem * menuitem, gpointer user_data);
411 gboolean on_window1_delete_event(GtkWidget * widget, GdkEvent * event,
414 GtkWidget *dialog, *label;
417 if (!conf_get_changed())
420 dialog = gtk_dialog_new_with_buttons("Warning !",
421 GTK_WINDOW(main_wnd),
424 GTK_DIALOG_DESTROY_WITH_PARENT),
430 GTK_RESPONSE_CANCEL, NULL);
431 gtk_dialog_set_default_response(GTK_DIALOG(dialog),
432 GTK_RESPONSE_CANCEL);
434 label = gtk_label_new("\nSave configuration ?\n");
435 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
436 gtk_widget_show(label);
438 result = gtk_dialog_run(GTK_DIALOG(dialog));
440 case GTK_RESPONSE_YES:
441 on_save_activate(NULL, NULL);
443 case GTK_RESPONSE_NO:
445 case GTK_RESPONSE_CANCEL:
446 case GTK_RESPONSE_DELETE_EVENT:
448 gtk_widget_destroy(dialog);
456 void on_window1_destroy(GtkObject * object, gpointer user_data)
463 on_window1_size_request(GtkWidget * widget,
464 GtkRequisition * requisition, gpointer user_data)
469 if (widget->window == NULL)
470 gtk_window_get_default_size(GTK_WINDOW(main_wnd), &w, &h);
472 gdk_window_get_size(widget->window, &w, &h);
478 gtk_paned_set_position(GTK_PANED(vpaned), 2 * h / 3);
482 /* Menu & Toolbar Callbacks */
486 load_filename(GtkFileSelection * file_selector, gpointer user_data)
490 fn = gtk_file_selection_get_filename(GTK_FILE_SELECTION
494 text_insert_msg("Error", "Unable to load configuration !");
496 display_tree(&rootmenu);
499 void on_load1_activate(GtkMenuItem * menuitem, gpointer user_data)
503 fs = gtk_file_selection_new("Load file...");
504 g_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(fs)->ok_button),
506 G_CALLBACK(load_filename), (gpointer) fs);
507 g_signal_connect_swapped(GTK_OBJECT
508 (GTK_FILE_SELECTION(fs)->ok_button),
509 "clicked", G_CALLBACK(gtk_widget_destroy),
511 g_signal_connect_swapped(GTK_OBJECT
512 (GTK_FILE_SELECTION(fs)->cancel_button),
513 "clicked", G_CALLBACK(gtk_widget_destroy),
519 void on_save_activate(GtkMenuItem * menuitem, gpointer user_data)
521 if (conf_write(NULL))
522 text_insert_msg("Error", "Unable to save configuration !");
523 conf_write_autoconf(0);
528 store_filename(GtkFileSelection * file_selector, gpointer user_data)
532 fn = gtk_file_selection_get_filename(GTK_FILE_SELECTION
536 text_insert_msg("Error", "Unable to save configuration !");
538 gtk_widget_destroy(GTK_WIDGET(user_data));
541 void on_save_as1_activate(GtkMenuItem * menuitem, gpointer user_data)
545 fs = gtk_file_selection_new("Save file as...");
546 g_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(fs)->ok_button),
548 G_CALLBACK(store_filename), (gpointer) fs);
549 g_signal_connect_swapped(GTK_OBJECT
550 (GTK_FILE_SELECTION(fs)->ok_button),
551 "clicked", G_CALLBACK(gtk_widget_destroy),
553 g_signal_connect_swapped(GTK_OBJECT
554 (GTK_FILE_SELECTION(fs)->cancel_button),
555 "clicked", G_CALLBACK(gtk_widget_destroy),
561 void on_quit1_activate(GtkMenuItem * menuitem, gpointer user_data)
563 if (!on_window1_delete_event(NULL, NULL, NULL))
564 gtk_widget_destroy(GTK_WIDGET(main_wnd));
568 void on_show_name1_activate(GtkMenuItem * menuitem, gpointer user_data)
570 GtkTreeViewColumn *col;
572 show_name = GTK_CHECK_MENU_ITEM(menuitem)->active;
573 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), COL_NAME);
575 gtk_tree_view_column_set_visible(col, show_name);
579 void on_show_range1_activate(GtkMenuItem * menuitem, gpointer user_data)
581 GtkTreeViewColumn *col;
583 show_range = GTK_CHECK_MENU_ITEM(menuitem)->active;
584 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), COL_NO);
586 gtk_tree_view_column_set_visible(col, show_range);
587 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), COL_MOD);
589 gtk_tree_view_column_set_visible(col, show_range);
590 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), COL_YES);
592 gtk_tree_view_column_set_visible(col, show_range);
597 void on_show_data1_activate(GtkMenuItem * menuitem, gpointer user_data)
599 GtkTreeViewColumn *col;
601 show_value = GTK_CHECK_MENU_ITEM(menuitem)->active;
602 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), COL_VALUE);
604 gtk_tree_view_column_set_visible(col, show_value);
609 on_set_option_mode1_activate(GtkMenuItem *menuitem, gpointer user_data)
611 opt_mode = OPT_NORMAL;
612 gtk_tree_store_clear(tree2);
613 display_tree(&rootmenu); /* instead of update_tree to speed-up */
618 on_set_option_mode2_activate(GtkMenuItem *menuitem, gpointer user_data)
621 gtk_tree_store_clear(tree2);
622 display_tree(&rootmenu); /* instead of update_tree to speed-up */
627 on_set_option_mode3_activate(GtkMenuItem *menuitem, gpointer user_data)
629 opt_mode = OPT_PROMPT;
630 gtk_tree_store_clear(tree2);
631 display_tree(&rootmenu); /* instead of update_tree to speed-up */
635 void on_introduction1_activate(GtkMenuItem * menuitem, gpointer user_data)
638 const gchar *intro_text =
639 "Welcome to gconfig, the GTK+ graphical configuration tool.\n"
640 "For each option, a blank box indicates the feature is disabled, a\n"
641 "check indicates it is enabled, and a dot indicates that it is to\n"
642 "be compiled as a module. Clicking on the box will cycle through the three states.\n"
644 "If you do not see an option (e.g., a device driver) that you\n"
645 "believe should be present, try turning on Show All Options\n"
646 "under the Options menu.\n"
647 "Although there is no cross reference yet to help you figure out\n"
648 "what other options must be enabled to support the option you\n"
649 "are interested in, you can still view the help of a grayed-out\n"
652 dialog = gtk_message_dialog_new(GTK_WINDOW(main_wnd),
653 GTK_DIALOG_DESTROY_WITH_PARENT,
655 GTK_BUTTONS_CLOSE, "%s", intro_text);
656 g_signal_connect_swapped(GTK_OBJECT(dialog), "response",
657 G_CALLBACK(gtk_widget_destroy),
659 gtk_widget_show_all(dialog);
663 void on_about1_activate(GtkMenuItem * menuitem, gpointer user_data)
666 const gchar *about_text =
667 "gconfig is copyright (c) 2002 Romain Lievin <roms@lpg.ticalc.org>.\n"
668 "Based on the source code from Roman Zippel.\n";
670 dialog = gtk_message_dialog_new(GTK_WINDOW(main_wnd),
671 GTK_DIALOG_DESTROY_WITH_PARENT,
673 GTK_BUTTONS_CLOSE, "%s", about_text);
674 g_signal_connect_swapped(GTK_OBJECT(dialog), "response",
675 G_CALLBACK(gtk_widget_destroy),
677 gtk_widget_show_all(dialog);
681 void on_license1_activate(GtkMenuItem * menuitem, gpointer user_data)
684 const gchar *license_text =
685 "gconfig is released under the terms of the GNU GPL v2.\n"
686 "For more information, please see the source code or\n"
687 "visit http://www.fsf.org/licenses/licenses.html\n";
689 dialog = gtk_message_dialog_new(GTK_WINDOW(main_wnd),
690 GTK_DIALOG_DESTROY_WITH_PARENT,
692 GTK_BUTTONS_CLOSE, "%s", license_text);
693 g_signal_connect_swapped(GTK_OBJECT(dialog), "response",
694 G_CALLBACK(gtk_widget_destroy),
696 gtk_widget_show_all(dialog);
700 void on_back_clicked(GtkButton * button, gpointer user_data)
702 enum prop_type ptype;
704 current = current->parent;
705 ptype = current->prompt ? current->prompt->type : P_UNKNOWN;
707 current = current->parent;
710 if (current == &rootmenu)
711 gtk_widget_set_sensitive(back_btn, FALSE);
715 void on_load_clicked(GtkButton * button, gpointer user_data)
717 on_load1_activate(NULL, user_data);
721 void on_single_clicked(GtkButton * button, gpointer user_data)
723 view_mode = SINGLE_VIEW;
724 gtk_widget_hide(tree1_w);
730 void on_split_clicked(GtkButton * button, gpointer user_data)
733 view_mode = SPLIT_VIEW;
734 gtk_widget_show(tree1_w);
735 gtk_window_get_default_size(GTK_WINDOW(main_wnd), &w, &h);
736 gtk_paned_set_position(GTK_PANED(hpaned), w / 2);
738 gtk_tree_store_clear(tree2);
741 /* Disable back btn, like in full mode. */
742 gtk_widget_set_sensitive(back_btn, FALSE);
746 void on_full_clicked(GtkButton * button, gpointer user_data)
748 view_mode = FULL_VIEW;
749 gtk_widget_hide(tree1_w);
751 gtk_tree_store_clear(tree2);
752 display_tree(&rootmenu);
753 gtk_widget_set_sensitive(back_btn, FALSE);
757 void on_collapse_clicked(GtkButton * button, gpointer user_data)
759 gtk_tree_view_collapse_all(GTK_TREE_VIEW(tree2_w));
763 void on_expand_clicked(GtkButton * button, gpointer user_data)
765 gtk_tree_view_expand_all(GTK_TREE_VIEW(tree2_w));
769 /* CTree Callbacks */
771 /* Change hex/int/string value in the cell */
772 static void renderer_edited(GtkCellRendererText * cell,
773 const gchar * path_string,
774 const gchar * new_text, gpointer user_data)
776 GtkTreePath *path = gtk_tree_path_new_from_string(path_string);
778 const char *old_def, *new_def;
782 if (!gtk_tree_model_get_iter(model2, &iter, path))
785 gtk_tree_model_get(model2, &iter, COL_MENU, &menu, -1);
788 gtk_tree_model_get(model2, &iter, COL_VALUE, &old_def, -1);
791 sym_set_string_value(sym, new_def);
793 update_tree(&rootmenu, NULL);
795 gtk_tree_path_free(path);
798 /* Change the value of a symbol and update the tree */
799 static void change_sym_value(struct menu *menu, gint col)
801 struct symbol *sym = menu->sym;
809 else if (col == COL_MOD)
811 else if (col == COL_YES)
816 switch (sym_get_type(sym)) {
819 if (!sym_tristate_within_range(sym, newval))
821 sym_set_tristate_value(sym, newval);
822 if (view_mode == FULL_VIEW)
823 update_tree(&rootmenu, NULL);
824 else if (view_mode == SPLIT_VIEW) {
825 update_tree(browsed, NULL);
828 else if (view_mode == SINGLE_VIEW)
829 display_tree_part(); //fixme: keep exp/coll
839 static void toggle_sym_value(struct menu *menu)
844 sym_toggle_tristate_value(menu->sym);
845 if (view_mode == FULL_VIEW)
846 update_tree(&rootmenu, NULL);
847 else if (view_mode == SPLIT_VIEW) {
848 update_tree(browsed, NULL);
851 else if (view_mode == SINGLE_VIEW)
852 display_tree_part(); //fixme: keep exp/coll
855 static gint column2index(GtkTreeViewColumn * column)
859 for (i = 0; i < COL_NUMBER; i++) {
860 GtkTreeViewColumn *col;
862 col = gtk_tree_view_get_column(GTK_TREE_VIEW(tree2_w), i);
871 /* User click: update choice (full) or goes down (single) */
873 on_treeview2_button_press_event(GtkWidget * widget,
874 GdkEventButton * event, gpointer user_data)
876 GtkTreeView *view = GTK_TREE_VIEW(widget);
878 GtkTreeViewColumn *column;
883 #if GTK_CHECK_VERSION(2,1,4) // bug in ctree with earlier version of GTK
884 gint tx = (gint) event->x;
885 gint ty = (gint) event->y;
888 gtk_tree_view_get_path_at_pos(view, tx, ty, &path, &column, &cx,
891 gtk_tree_view_get_cursor(view, &path, &column);
896 if (!gtk_tree_model_get_iter(model2, &iter, path))
898 gtk_tree_model_get(model2, &iter, COL_MENU, &menu, -1);
900 col = column2index(column);
901 if (event->type == GDK_2BUTTON_PRESS) {
902 enum prop_type ptype;
903 ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
905 if (ptype == P_MENU && view_mode != FULL_VIEW && col == COL_OPTION) {
906 // goes down into menu
909 gtk_widget_set_sensitive(back_btn, TRUE);
910 } else if (col == COL_OPTION) {
911 toggle_sym_value(menu);
912 gtk_tree_view_expand_row(view, path, TRUE);
915 if (col == COL_VALUE) {
916 toggle_sym_value(menu);
917 gtk_tree_view_expand_row(view, path, TRUE);
918 } else if (col == COL_NO || col == COL_MOD
920 change_sym_value(menu, col);
921 gtk_tree_view_expand_row(view, path, TRUE);
928 /* Key pressed: update choice */
930 on_treeview2_key_press_event(GtkWidget * widget,
931 GdkEventKey * event, gpointer user_data)
933 GtkTreeView *view = GTK_TREE_VIEW(widget);
935 GtkTreeViewColumn *column;
940 gtk_tree_view_get_cursor(view, &path, &column);
944 if (event->keyval == GDK_space) {
945 if (gtk_tree_view_row_expanded(view, path))
946 gtk_tree_view_collapse_row(view, path);
948 gtk_tree_view_expand_row(view, path, FALSE);
951 if (event->keyval == GDK_KP_Enter) {
953 if (widget == tree1_w)
956 gtk_tree_model_get_iter(model2, &iter, path);
957 gtk_tree_model_get(model2, &iter, COL_MENU, &menu, -1);
959 if (!strcasecmp(event->string, "n"))
961 else if (!strcasecmp(event->string, "m"))
963 else if (!strcasecmp(event->string, "y"))
967 change_sym_value(menu, col);
973 /* Row selection changed: update help */
975 on_treeview2_cursor_changed(GtkTreeView * treeview, gpointer user_data)
977 GtkTreeSelection *selection;
981 selection = gtk_tree_view_get_selection(treeview);
982 if (gtk_tree_selection_get_selected(selection, &model2, &iter)) {
983 gtk_tree_model_get(model2, &iter, COL_MENU, &menu, -1);
984 text_insert_help(menu);
989 /* User click: display sub-tree in the right frame. */
991 on_treeview1_button_press_event(GtkWidget * widget,
992 GdkEventButton * event, gpointer user_data)
994 GtkTreeView *view = GTK_TREE_VIEW(widget);
996 GtkTreeViewColumn *column;
1000 gint tx = (gint) event->x;
1001 gint ty = (gint) event->y;
1004 gtk_tree_view_get_path_at_pos(view, tx, ty, &path, &column, &cx,
1009 gtk_tree_model_get_iter(model1, &iter, path);
1010 gtk_tree_model_get(model1, &iter, COL_MENU, &menu, -1);
1012 if (event->type == GDK_2BUTTON_PRESS) {
1013 toggle_sym_value(menu);
1015 display_tree_part();
1018 display_tree_part();
1021 gtk_widget_realize(tree2_w);
1022 gtk_tree_view_set_cursor(view, path, NULL, FALSE);
1023 gtk_widget_grab_focus(tree2_w);
1029 /* Fill a row of strings */
1030 static gchar **fill_row(struct menu *menu)
1032 static gchar *row[COL_NUMBER];
1033 struct symbol *sym = menu->sym;
1037 enum prop_type ptype;
1040 for (i = COL_OPTION; i <= COL_COLOR; i++)
1042 bzero(row, sizeof(row));
1044 ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
1047 g_strdup_printf("%s %s %s %s",
1048 ptype == P_COMMENT ? "***" : "",
1049 menu_get_prompt(menu),
1050 ptype == P_COMMENT ? "***" : "",
1051 sym && !sym_has_value(sym) ? "(NEW)" : "");
1053 if (opt_mode == OPT_ALL && !menu_is_visible(menu))
1054 row[COL_COLOR] = g_strdup("DarkGray");
1055 else if (opt_mode == OPT_PROMPT &&
1056 menu_has_prompt(menu) && !menu_is_visible(menu))
1057 row[COL_COLOR] = g_strdup("DarkGray");
1059 row[COL_COLOR] = g_strdup("Black");
1063 row[COL_PIXBUF] = (gchar *) xpm_menu;
1064 if (view_mode == SINGLE_VIEW)
1065 row[COL_PIXVIS] = GINT_TO_POINTER(TRUE);
1066 row[COL_BTNVIS] = GINT_TO_POINTER(FALSE);
1069 row[COL_PIXBUF] = (gchar *) xpm_void;
1070 row[COL_PIXVIS] = GINT_TO_POINTER(FALSE);
1071 row[COL_BTNVIS] = GINT_TO_POINTER(FALSE);
1074 row[COL_PIXBUF] = (gchar *) xpm_void;
1075 row[COL_PIXVIS] = GINT_TO_POINTER(FALSE);
1076 row[COL_BTNVIS] = GINT_TO_POINTER(TRUE);
1082 row[COL_NAME] = g_strdup(sym->name);
1084 sym_calc_value(sym);
1085 sym->flags &= ~SYMBOL_CHANGED;
1087 if (sym_is_choice(sym)) { // parse childs for getting final value
1089 struct symbol *def_sym = sym_get_choice_value(sym);
1090 struct menu *def_menu = NULL;
1092 row[COL_BTNVIS] = GINT_TO_POINTER(FALSE);
1094 for (child = menu->list; child; child = child->next) {
1095 if (menu_is_visible(child)
1096 && child->sym == def_sym)
1102 g_strdup(menu_get_prompt(def_menu));
1104 if (sym->flags & SYMBOL_CHOICEVAL)
1105 row[COL_BTNRAD] = GINT_TO_POINTER(TRUE);
1107 stype = sym_get_type(sym);
1110 if (GPOINTER_TO_INT(row[COL_PIXVIS]) == FALSE)
1111 row[COL_BTNVIS] = GINT_TO_POINTER(TRUE);
1112 if (sym_is_choice(sym))
1116 val = sym_get_tristate_value(sym);
1119 row[COL_NO] = g_strdup("N");
1120 row[COL_VALUE] = g_strdup("N");
1121 row[COL_BTNACT] = GINT_TO_POINTER(FALSE);
1122 row[COL_BTNINC] = GINT_TO_POINTER(FALSE);
1125 row[COL_MOD] = g_strdup("M");
1126 row[COL_VALUE] = g_strdup("M");
1127 row[COL_BTNINC] = GINT_TO_POINTER(TRUE);
1130 row[COL_YES] = g_strdup("Y");
1131 row[COL_VALUE] = g_strdup("Y");
1132 row[COL_BTNACT] = GINT_TO_POINTER(TRUE);
1133 row[COL_BTNINC] = GINT_TO_POINTER(FALSE);
1137 if (val != no && sym_tristate_within_range(sym, no))
1138 row[COL_NO] = g_strdup("_");
1139 if (val != mod && sym_tristate_within_range(sym, mod))
1140 row[COL_MOD] = g_strdup("_");
1141 if (val != yes && sym_tristate_within_range(sym, yes))
1142 row[COL_YES] = g_strdup("_");
1147 def = sym_get_string_value(sym);
1148 row[COL_VALUE] = g_strdup(def);
1149 row[COL_EDIT] = GINT_TO_POINTER(TRUE);
1150 row[COL_BTNVIS] = GINT_TO_POINTER(FALSE);
1158 /* Set the node content with a row of strings */
1159 static void set_node(GtkTreeIter * node, struct menu *menu, gchar ** row)
1165 pix = gdk_pixbuf_new_from_xpm_data((const char **)
1168 gdk_color_parse(row[COL_COLOR], &color);
1169 gdk_colormap_alloc_colors(gdk_colormap_get_system(), &color, 1,
1170 FALSE, FALSE, &success);
1172 gtk_tree_store_set(tree, node,
1173 COL_OPTION, row[COL_OPTION],
1174 COL_NAME, row[COL_NAME],
1175 COL_NO, row[COL_NO],
1176 COL_MOD, row[COL_MOD],
1177 COL_YES, row[COL_YES],
1178 COL_VALUE, row[COL_VALUE],
1179 COL_MENU, (gpointer) menu,
1181 COL_EDIT, GPOINTER_TO_INT(row[COL_EDIT]),
1183 COL_PIXVIS, GPOINTER_TO_INT(row[COL_PIXVIS]),
1184 COL_BTNVIS, GPOINTER_TO_INT(row[COL_BTNVIS]),
1185 COL_BTNACT, GPOINTER_TO_INT(row[COL_BTNACT]),
1186 COL_BTNINC, GPOINTER_TO_INT(row[COL_BTNINC]),
1187 COL_BTNRAD, GPOINTER_TO_INT(row[COL_BTNRAD]),
1190 g_object_unref(pix);
1194 /* Add a node to the tree */
1195 static void place_node(struct menu *menu, char **row)
1197 GtkTreeIter *parent = parents[indent - 1];
1198 GtkTreeIter *node = parents[indent];
1200 gtk_tree_store_append(tree, node, parent);
1201 set_node(node, menu, row);
1205 /* Find a node in the GTK+ tree */
1206 static GtkTreeIter found;
1209 * Find a menu in the GtkTree starting at parent.
1211 static GtkTreeIter *gtktree_iter_find_node(GtkTreeIter *parent,
1212 struct menu *tofind)
1215 GtkTreeIter *child = &iter;
1219 valid = gtk_tree_model_iter_children(model2, child, parent);
1223 gtk_tree_model_get(model2, child, 6, &menu, -1);
1225 if (menu == tofind) {
1226 memcpy(&found, child, sizeof(GtkTreeIter));
1230 ret = gtktree_iter_find_node(child, tofind);
1234 valid = gtk_tree_model_iter_next(model2, child);
1242 * Update the tree by adding/removing entries
1243 * Does not change other nodes
1245 static void update_tree(struct menu *src, GtkTreeIter * dst)
1247 struct menu *child1;
1248 GtkTreeIter iter, tmp;
1249 GtkTreeIter *child2 = &iter;
1251 GtkTreeIter *sibling;
1253 struct menu *menu1, *menu2;
1255 if (src == &rootmenu)
1258 valid = gtk_tree_model_iter_children(model2, child2, dst);
1259 for (child1 = src->list; child1; child1 = child1->next) {
1266 gtk_tree_model_get(model2, child2, COL_MENU,
1269 menu2 = NULL; // force adding of a first child
1272 printf("%*c%s | %s\n", indent, ' ',
1273 menu1 ? menu_get_prompt(menu1) : "nil",
1274 menu2 ? menu_get_prompt(menu2) : "nil");
1277 if ((opt_mode == OPT_NORMAL && !menu_is_visible(child1)) ||
1278 (opt_mode == OPT_PROMPT && !menu_has_prompt(child1)) ||
1279 (opt_mode == OPT_ALL && !menu_get_prompt(child1))) {
1282 if (gtktree_iter_find_node(dst, menu1) != NULL) {
1283 memcpy(&tmp, child2, sizeof(GtkTreeIter));
1284 valid = gtk_tree_model_iter_next(model2,
1286 gtk_tree_store_remove(tree2, &tmp);
1288 return; /* next parent */
1290 goto reparse; /* next child */
1295 if (menu1 != menu2) {
1296 if (gtktree_iter_find_node(dst, menu1) == NULL) { // add node
1297 if (!valid && !menu2)
1301 gtk_tree_store_insert_before(tree2,
1304 set_node(child2, menu1, fill_row(menu1));
1307 } else { // remove node
1308 memcpy(&tmp, child2, sizeof(GtkTreeIter));
1309 valid = gtk_tree_model_iter_next(model2,
1311 gtk_tree_store_remove(tree2, &tmp);
1313 return; // next parent
1315 goto reparse; // next child
1317 } else if (sym && (sym->flags & SYMBOL_CHANGED)) {
1318 set_node(child2, menu1, fill_row(menu1));
1322 update_tree(child1, child2);
1325 valid = gtk_tree_model_iter_next(model2, child2);
1330 /* Display the whole tree (single/split/full view) */
1331 static void display_tree(struct menu *menu)
1334 struct property *prop;
1336 enum prop_type ptype;
1338 if (menu == &rootmenu) {
1340 current = &rootmenu;
1343 for (child = menu->list; child; child = child->next) {
1344 prop = child->prompt;
1346 ptype = prop ? prop->type : P_UNKNOWN;
1349 sym->flags &= ~SYMBOL_CHANGED;
1351 if ((view_mode == SPLIT_VIEW)
1352 && !(child->flags & MENU_ROOT) && (tree == tree1))
1355 if ((view_mode == SPLIT_VIEW) && (child->flags & MENU_ROOT)
1359 if ((opt_mode == OPT_NORMAL && menu_is_visible(child)) ||
1360 (opt_mode == OPT_PROMPT && menu_has_prompt(child)) ||
1361 (opt_mode == OPT_ALL && menu_get_prompt(child)))
1362 place_node(child, fill_row(child));
1364 printf("%*c%s: ", indent, ' ', menu_get_prompt(child));
1365 printf("%s", child->flags & MENU_ROOT ? "rootmenu | " : "");
1366 printf("%s", prop_get_type_name(ptype));
1369 printf("%s", sym_type_name(sym->type));
1371 printf("%s", dbg_sym_flags(sym->flags));
1376 if ((view_mode != FULL_VIEW) && (ptype == P_MENU)
1380 if (((menu != &rootmenu) && !(menu->flags & MENU_ROOT))
1381 || (view_mode == FULL_VIEW)
1382 || (view_mode == SPLIT_VIEW))*/
1384 /* Change paned position if the view is not in 'split mode' */
1385 if (view_mode == SINGLE_VIEW || view_mode == FULL_VIEW) {
1386 gtk_paned_set_position(GTK_PANED(hpaned), 0);
1389 if (((view_mode == SINGLE_VIEW) && (menu->flags & MENU_ROOT))
1390 || (view_mode == FULL_VIEW)
1391 || (view_mode == SPLIT_VIEW)) {
1393 display_tree(child);
1399 /* Display a part of the tree starting at current node (single/split view) */
1400 static void display_tree_part(void)
1403 gtk_tree_store_clear(tree2);
1404 if (view_mode == SINGLE_VIEW)
1405 display_tree(current);
1406 else if (view_mode == SPLIT_VIEW)
1407 display_tree(browsed);
1408 gtk_tree_view_expand_all(GTK_TREE_VIEW(tree2_w));
1411 /* Display the list in the left frame (split view) */
1412 static void display_list(void)
1415 gtk_tree_store_clear(tree1);
1418 display_tree(&rootmenu);
1419 gtk_tree_view_expand_all(GTK_TREE_VIEW(tree1_w));
1423 static void fixup_rootmenu(struct menu *menu)
1426 static int menu_cnt = 0;
1428 menu->flags |= MENU_ROOT;
1429 for (child = menu->list; child; child = child->next) {
1430 if (child->prompt && child->prompt->type == P_MENU) {
1432 fixup_rootmenu(child);
1434 } else if (!menu_cnt)
1435 fixup_rootmenu(child);
1441 int main(int ac, char *av[])
1452 /* Determine GUI path */
1453 env = getenv(SRCTREE);
1455 glade_file = g_strconcat(env, "/scripts/kconfig/gconf.glade", NULL);
1456 else if (av[0][0] == '/')
1457 glade_file = g_strconcat(av[0], ".glade", NULL);
1459 glade_file = g_strconcat(g_get_current_dir(), "/", av[0], ".glade", NULL);
1462 if (ac > 1 && av[1][0] == '-') {
1468 conf_set_message_callback(NULL);
1472 printf("%s [-s] <config>\n", av[0]);
1480 fixup_rootmenu(&rootmenu);
1483 /* Load the interface and connect signals */
1484 init_main_window(glade_file);
1489 switch (view_mode) {
1491 display_tree_part();
1497 display_tree(&rootmenu);
1506 static void conf_changed(void)
1508 bool changed = conf_get_changed();
1509 gtk_widget_set_sensitive(save_btn, changed);
1510 gtk_widget_set_sensitive(save_menu_item, changed);