PIPS
gtk_schoose2.c
Go to the documentation of this file.
1 /*
2 
3  $Id: gtk_schoose2.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /* Single choice handling */
28 
29 /* Difference with previous release:
30  1/ schoose_close() must be called in order to close the schoose window.
31  2/ schoose() has one more argument, because cancel button is created.
32  bb 04.06.91
33  */
34 /*
35  * forked to gtk_schoose2.c
36  * Edited by Johan GALL
37  *
38  */
39 
40 #include <stdio.h>
41 
42 #include <sys/time.h>
43 #include <sys/resource.h>
44 
45 #include "genC.h"
46 #include "misc.h"
47 
48 #undef test_undefined // also defined in glib included from gtk
49 #include <gtk/gtk.h>
50 #include "gpips.h"
51 
52 static GtkWidget *choice_label, *choices_list, *ok_button, *help,
54 static GtkListStore *choices;
55 
56 enum {
58 };
59 
60 static void (* apply_on_choice)(const char *);
61 static void (* apply_on_cancel)(void);
62 
63 #if 0
64 static void schoose_help_notify(GtkWidget * widget, gpointer data) {
65  display_help("SingleChoice");
66 }
67 #endif
68 
69 /* called when the "ok button" is clicked to validate an entry or when you select
70  * something with the menu associated to the beforementionned entry. */
71 static void schoose_ok_notify(GtkWidget * widget, gpointer data) {
72  GtkTreeIter iter;
73  GtkTreeModel ** model = NULL;
74  gchar * gc_choice;
75 
76  GtkTreeSelection * selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(
77  choices_list));
78 
79  if (!gtk_tree_selection_get_selected(selection, model, &iter)) {
80  prompt_user("Choose one item or cancel");
81  return;
82  }
83 
84  // gtk_tree_model_get(GTK_TREE_MODEL(*model), &iter,
85  // SC2_AVAILABLE_CHOICES_COLUMN_ID, &gc_choice, -1);
86 
87  gc_choice = strdup(gtk_label_get_text(GTK_LABEL(choice_label)));
88  // ----
89 
90  // curchoice = strdup((char *) xv_get(choice, PANEL_VALUE, 0));
91  //
92  // /* Modified to verify that an correct item is selected.
93  // RK, 21/05/1993. */
94  // nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, 0);
95  // item_is_in_the_list = FALSE;
96  // for (i = 0; i < nchoices; i++)
97  // if (strcmp((char *) xv_get(choices, PANEL_LIST_STRING, i), curchoice)
98  // == 0) {
99  // item_is_in_the_list = TRUE;
100  // break;
101  // }
102  // if (item_is_in_the_list == FALSE)
103  // prompt_user("You have to choose one item of the list!");
104  // else {
105  // /* Normal case : */
106  (*apply_on_choice)(gc_choice);
107  g_free(gc_choice);
108  schoose_close();
109 }
110 
111 /* schoose_close() can be called even when schoose window is already closed.
112  */
114  hide_window(schoose_window, NULL, NULL);
115  gtk_list_store_clear(choices);
116  gtk_label_set_text(GTK_LABEL(choice_label), "");
117 }
118 
119 void schoose_cancel_notify(GtkWidget * widget, gpointer data) {
120  schoose_close();
121  (*apply_on_cancel)();
122 }
123 
124 /* Function used to update the text panel according to the list panel: */
125 static void schoose_choice_callback(GtkTreeSelection * selection, gpointer data) {
126  GtkTreeIter iter;
127  GtkTreeModel * model;
128  gchar * gc_choice;
129 
130  // if (!gtk_tree_selection_get_selected(selection, &model, &iter)) {
131  // pips_assert("schoose_choice_notify: no item selected !", 0);
132  // return;
133  // }
134 
135  if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
136  gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
137  SC2_AVAILABLE_CHOICES_COLUMN_ID, &gc_choice, -1);
138  gtk_label_set_text(GTK_LABEL(choice_label), gc_choice);
139  g_free(gc_choice);
140  }
141 }
142 
143 /* Avoid the schoose_frame destruction and act as cancel: */
144 static void schoose_window_done_callback(GtkWidget * window, GdkEvent * ev,
145  gpointer data) {
146  (*apply_on_cancel)();
147  hide_window(window, NULL, NULL);
148 }
149 
150 void schoose(char * title, gen_array_t array, char * initial_choice,
151  void(*function_for_ok)(const char *), void(*function_for_cancel)(void)) {
152  guint i;
153  string name;
154  GtkTreeIter iter;
155  GtkTreeSelection * selection;
156  bool do_select = FALSE;
157  int argc = gen_array_nitems(array);
158 
159  apply_on_choice = function_for_ok;
160  apply_on_cancel = function_for_cancel;
161 
162  gtk_window_set_title(GTK_WINDOW(schoose_window), title);
163  gtk_list_store_clear(choices);
164 
165  if (initial_choice != NULL) {
166  do_select = TRUE;
167  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choices_list));
168  }
169 
170  for (i = 0; i < argc; i++) {
171  name = gen_array_item(array, i);
172  gtk_list_store_append(GTK_LIST_STORE(choices), &iter);
173  gtk_list_store_set(choices, &iter, SC2_AVAILABLE_CHOICES_COLUMN_ID,
174  name, -1);
175  if (do_select) // Initialise au choix initial ou default le premier
176  if (i == 0 || strcmp(initial_choice, name) == 0)
177  gtk_tree_selection_select_iter(GTK_TREE_SELECTION(selection),
178  &iter);
179  }
180  gtk_widget_show(schoose_window);
181 
182  /* move the pointer to the center of the query window */
183  // Sans vouloir remettre en question le design de la chose
184  // A priori je ne traduirai pas ça à moins qu'on ne le
185  // demande expressément...
186  //pointer_in_center_of_frame(schoose_frame);
187 }
188 
190  guint i;
191  GtkWidget *window_vbox, *buttons_hbox, *current_choice_hbox;
192  GtkTreeIter iter;
193  GtkTreeSelection * selection;
194 
195  window_vbox = gtk_vbox_new(FALSE, 0);
196  buttons_hbox = gtk_hbox_new(FALSE, 0);
197  current_choice_hbox = gtk_hbox_new(FALSE, 0);
198  gtk_box_pack_start(GTK_BOX(window_vbox), current_choice_hbox, FALSE, FALSE,
199  0);
200  gtk_container_add(GTK_CONTAINER(schoose_window), window_vbox);
201 
202  gtk_box_pack_start(GTK_BOX(current_choice_hbox), gtk_label_new(
203  "Current choice : "), FALSE, FALSE, 0);
204  choice_label = gtk_label_new("");
205  gtk_box_pack_start(GTK_BOX(current_choice_hbox), choice_label, FALSE,
206  FALSE, 0);
207 
208  choices = gtk_list_store_new(SC2_COLUMNS_NUMBER, G_TYPE_STRING);
209  for (i = 0; i < 5; i++) {
210  gtk_list_store_append(GTK_LIST_STORE(choices), &iter);
211  gtk_list_store_set(GTK_LIST_STORE(choices), &iter,
213  //gtk_tree_store_set(GTK_TREE_STORE(choices), &iter, SC2_AVAILABLE_CHOICES, "");
214  }
215 
216  // the "graphical" choice component is the GtkTreeView * choices_list
217  choices_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(choices));
218  GtkCellRenderer * renderer = gtk_cell_renderer_text_new();
219  GtkTreeViewColumn * column;
220  column = gtk_tree_view_column_new_with_attributes("Available choices",
221  renderer, "text", SC2_AVAILABLE_CHOICES_COLUMN_ID, NULL);
222  gtk_tree_view_append_column(GTK_TREE_VIEW(choices_list), column);
223 
224  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(choices_list));
225  gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection),
226  GTK_SELECTION_SINGLE);
227  g_signal_connect(selection, "changed", G_CALLBACK(schoose_choice_callback),
228  NULL);
229 
230  gtk_box_pack_start(GTK_BOX(window_vbox), choices_list, TRUE, FALSE, 0);
231  gtk_widget_show(choices_list);
232 
233  ok_button = gtk_button_new_with_label("OK");
234  g_signal_connect(GTK_OBJECT(ok_button), "clicked", G_CALLBACK(
235  schoose_ok_notify), NULL);
236  gtk_box_pack_start(GTK_BOX(buttons_hbox), ok_button, FALSE, FALSE, 5);
237 
238  cancel_button = gtk_button_new_with_label("Cancel");
239  g_signal_connect(GTK_OBJECT(cancel_button), "clicked", G_CALLBACK(
240  schoose_cancel_notify), NULL);
241  gtk_box_pack_start(GTK_BOX(buttons_hbox), cancel_button, FALSE, FALSE, 5);
242 
243  help = gtk_button_new_with_label("Help");
244  g_signal_connect(GTK_OBJECT(help), "clicked",
245  G_CALLBACK(schoose_ok_notify), NULL);
246  gtk_box_pack_start(GTK_BOX(buttons_hbox), help, FALSE, FALSE, 5);
247 
248  gtk_box_pack_start(GTK_BOX(window_vbox), buttons_hbox, FALSE, FALSE, 5);
249 
250  gtk_window_set_default(GTK_WINDOW(schoose_window), ok_button);
251 
252  g_signal_connect(GTK_OBJECT(schoose_window), "delete_event", G_CALLBACK(
254 
255  gtk_widget_show_all(window_vbox);
256 }
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
GtkWidget * schoose_window
Definition: gpips-local.h:36
void display_help(char *topic)
Definition: gtk_help.c:50
void prompt_user(string a_printf_format,...)
Definition: gtk_log.c:66
static GtkWidget * ok_button
Definition: gtk_schoose2.c:52
static GtkWidget * choice_label
Single choice handling.
Definition: gtk_schoose2.c:52
static GtkListStore * choices
Definition: gtk_schoose2.c:54
void schoose(char *title, gen_array_t array, char *initial_choice, void(*function_for_ok)(const char *), void(*function_for_cancel)(void))
Definition: gtk_schoose2.c:150
static GtkWidget * help
Definition: gtk_schoose2.c:52
void schoose_close()
schoose_close() can be called even when schoose window is already closed.
Definition: gtk_schoose2.c:113
static void(* apply_on_cancel)(void)
Definition: gtk_schoose2.c:61
static GtkWidget * cancel_button
Definition: gtk_schoose2.c:53
static void schoose_ok_notify(GtkWidget *widget, gpointer data)
called when the "ok button" is clicked to validate an entry or when you select something with the men...
Definition: gtk_schoose2.c:71
static void schoose_choice_callback(GtkTreeSelection *selection, gpointer data)
Function used to update the text panel according to the list panel:
Definition: gtk_schoose2.c:125
@ SC2_COLUMNS_NUMBER
Definition: gtk_schoose2.c:57
@ SC2_AVAILABLE_CHOICES_COLUMN_ID
Definition: gtk_schoose2.c:57
static void schoose_window_done_callback(GtkWidget *window, GdkEvent *ev, gpointer data)
Avoid the schoose_frame destruction and act as cancel:
Definition: gtk_schoose2.c:144
static void(* apply_on_choice)(const char *)
Definition: gtk_schoose2.c:60
void schoose_cancel_notify(GtkWidget *widget, gpointer data)
Definition: gtk_schoose2.c:119
void create_schoose_window()
Definition: gtk_schoose2.c:189
static GtkWidget * choices_list
Definition: gtk_schoose2.c:52
gint hide_window(GtkWidget *window, GdkEvent *ev __attribute__((unused)), gpointer data __attribute__((unused)))
Definition: gtk_utils.c:89
char * strdup()
static entity array
static void schoose_help_notify(Panel_item item, Event *event)
Definition: xv_schoose2.c:60