1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <stdlib.h>
#include <stdarg.h>
// string.h nécessaire pour éviter warning implicit declaration of function strcmp
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <gtk/gtk.h>
#ifndef M_PI
#define M_PI 3.141592653
#endif
GtkWidget *pwindow_tools;
enum
{
LIST_ITEM = 0,
N_COLUMNS
};
static void init_list(GtkWidget *liste);
static void add_to_list(GtkWidget *liste, const gint str);
void on_changed(GtkWidget *widget, gpointer label);
void close_wt(GtkWidget *button, gpointer data);
int int_to_str(int value, char *buf, unsigned int len);
/* -------------------------------------------------------
fonctions utilitaires int vers string
-------------------------------------------------------*/
int int_to_str(int value, char *buf, unsigned int len)
{
unsigned result = 0;
if (!buf)
return -1;
#define ADDCHAR(chr) \
if (result < len - 1) \
{ \
*buf++ = (chr); \
result++; \
}
int j = 0;
char int_to_str[16];
if (value < 0)
ADDCHAR('-');
char *ptr = int_to_str + sizeof(int_to_str) - 1;
do
{
int modulo = value % 10;
modulo = (modulo<0)?-modulo:modulo;
*ptr-- = modulo + '0';
value /= 10;
j++;
} while (value);
for (; j > 0; j--)
ADDCHAR(int_to_str[sizeof(int_to_str) - j]);
*buf = '\0';
return 0;
}
static void init_list(GtkWidget *liste)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkListStore *store;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", LIST_ITEM, NULL);
/* centrer les éléments de la liste */
g_object_set (renderer, "xalign", 0.5, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(liste), column);
/* 1 colonne de type int*/
store = gtk_list_store_new(1, G_TYPE_INT);
gtk_tree_view_set_model(GTK_TREE_VIEW(liste), GTK_TREE_MODEL(store));
g_object_unref(store);
}
static void add_to_list(GtkWidget *liste, const gint str)
{
GtkListStore *store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(liste)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
}
void on_changed(GtkWidget *widget, gpointer data)
{
/* widget est un GtkTreeSelection */
GtkTreeIter iter;
GtkTreeModel *model;
int value;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1);
g_print("value: %d", value);
/* ligne liée à l'erreur */
gtk_widget_destroy(GTK_WIDGET(pwindow_tools));
}
}
void close_wt(GtkWidget *button, gpointer data) {
gtk_main_quit();
}
/* -------------------------------------------------------
GTK Main
-------------------------------------------------------*/
int main (int argc, char *argv[]) {
gtk_init (&argc, &argv);
pwindow_tools = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(pwindow_tools), "Tool Box");
GtkWidget *p_window_tools_notebook;
GtkWidget *p_onglet2;
GtkWidget *p_child;
GtkWidget *liste, *label_liste;
GtkTreeSelection *selection;
/* -------- Création du Notebook ---------- */
p_window_tools_notebook = gtk_notebook_new();
gtk_widget_set_name( p_window_tools_notebook, "NoteBook");
/* -------- CFL ---------- */
liste = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(liste), FALSE);
init_list(liste);
int i;
for (i=410; i>350; i=i-10) {
add_to_list(liste, i);
}
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(liste));
g_signal_connect(selection, "changed", G_CALLBACK(on_changed), NULL);
p_child = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_size_request(p_child,80, 100);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(p_child), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
gtk_container_add (GTK_CONTAINER (p_child), liste);
p_onglet2 = gtk_label_new ("CFL");
gtk_label_set_angle(GTK_LABEL(p_onglet2), 270);
gtk_notebook_append_page (GTK_NOTEBOOK (p_window_tools_notebook), p_child, p_onglet2);
gtk_notebook_set_tab_pos(GTK_NOTEBOOK(p_window_tools_notebook), GTK_POS_LEFT);
gtk_container_add (GTK_CONTAINER (pwindow_tools), p_window_tools_notebook);
gtk_widget_show_all(pwindow_tools);
gtk_main ();
return 0;
} |
Partager