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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <stdlib.h>
#include <stdarg.h>
#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;
GtkWidget *p_child;
enum
{
LIST_ITEM = 0,
N_COLUMNS
};
static void init_list(GtkWidget *liste);
static void add_to_list(GtkWidget *liste, const gint str);
void on_cflchanged(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 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_cflchanged(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 \n", value);
/* remettre le destroy ci-dessous */
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);
/* --------- crÈation FenÍtre pop-up ------------- */
pwindow_tools = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *pscrollwin;
GtkWidget *p_window_tools_label1, *p_window_tools_label2;
GtkWidget *p_window_tools_grid, *liste;
GtkTreeSelection *selection;
/* -------- CrÈation Grid 2x3 --------- */
p_window_tools_grid = gtk_grid_new();
/* -------- ligne 1 : Callsign + bouton close ---------- */
GtkWidget *p_window_tools_close_button, *p_wt_close_im;
p_window_tools_label1 = gtk_label_new("Avion");
/* ------- Insertion Callsign dans GtkAlignment -------- */
p_window_tools_close_button = gtk_button_new();
p_wt_close_im = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
gtk_container_add(GTK_CONTAINER (p_window_tools_close_button), p_wt_close_im);
g_signal_connect(G_OBJECT(p_window_tools_close_button), "clicked", G_CALLBACK (close_wt), pwindow_tools);
/* -------- Insertion ligne 1 dans grid --------- */
gtk_grid_attach(GTK_GRID (p_window_tools_grid), p_window_tools_label1, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID (p_window_tools_grid), p_window_tools_close_button, 1, 0, 1, 1);
p_window_tools_label2 = gtk_label_new("CFL");
gtk_grid_attach(GTK_GRID (p_window_tools_grid), p_window_tools_label2, 0, 1, 2, 1);
/* -------- Ligne CFL ---------- */
pscrollwin = gtk_scrolled_window_new(NULL, NULL);
/* sans le size request, le widget a une hauteur de 0 pixel */
gtk_widget_set_size_request(pscrollwin , 80, 100);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(pscrollwin), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
liste = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(liste), FALSE);
init_list(liste);
int i;
for (i=490; i>410; i=i-20) {
add_to_list(liste, i);
}
for (i=410; i>50; i=i-10) {
add_to_list(liste, i);
}
gtk_container_add (GTK_CONTAINER(pscrollwin), GTK_WIDGET(liste));
gtk_grid_attach(GTK_GRID (p_window_tools_grid), pscrollwin, 0, 2, 2, 1);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(liste));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
gtk_container_add(GTK_CONTAINER(pwindow_tools), p_window_tools_grid);
// gtk_container_add(GTK_CONTAINER(pwindow_tools), pscrollwin);
gtk_widget_show_all(pwindow_tools);
g_signal_connect(selection, "changed", G_CALLBACK(on_cflchanged), NULL);
gtk_main ();
return 0;
} |
Partager