がんばって英語で書くブログ
英語勉強中なので、ブログも英語で書こうかと。
スポンサーサイト
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

GTK+ studying10
This is GTK+ memo. using progress bar.


#include <gtk/gtk.h>

struct progress_data {
GtkWidget *bar;
int counter;
int timer;
};

static inline gchar *make_string(struct progress_data *progress)
{
return g_strdup_printf("count is %d\n", progress->counter++);
}

static gboolean progressbar_timeout(gpointer data)
{
struct progress_data *progress = (struct progress_data *) data;
gdouble new_val;
gchar *p = NULL;

new_val = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(progress->bar)) + 0.01;
if (new_val > 1.0)
new_val = 0.0;

gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress->bar), new_val);

p = make_string(progress);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), p);
g_free(p);

if (progress->counter > 100) {
GtkWidget *dlg;

gtk_timeout_remove(progress->timer);
dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"finished");
gtk_dialog_run(GTK_DIALOG(dlg));

g_free(progress);

gtk_main_quit();
}


return TRUE;
}

void create_window(void)
{
GtkWidget *window;
GtkWidget *vbox1;
GtkWidget *align;
gchar *p;
struct progress_data *progress;

progress = g_malloc(sizeof(struct progress_data));
progress->counter = 0;
progress->timer = 0;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "progress bar test");

gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

vbox1 = gtk_vbox_new(FALSE,0);

align = gtk_alignment_new(0.5, 0.5, 0, 0);
gtk_box_pack_start(GTK_BOX(vbox1), align, FALSE, FALSE, 0);
gtk_widget_show(align);

progress->bar = gtk_progress_bar_new();
gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(progress->bar), GTK_PROGRESS_LEFT_TO_RIGHT);

p = make_string(progress);
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress->bar), p);
g_free(p);

gtk_container_add(GTK_CONTAINER(align), progress->bar);
gtk_widget_show(progress->bar);

progress->timer = gtk_timeout_add(10, progressbar_timeout, progress);

gtk_container_add(GTK_CONTAINER(window), vbox1);
gtk_widget_show(vbox1);

gtk_widget_show(window);

}

int main(int argc, char **argv)
{
gtk_init(&argc, &argv);

create_window();

gtk_main();

return 0;
}



テーマ:英語日記 - ジャンル:学問・文化・芸術

GTK+ studying9
This is GTK+ memo. using vpaned.


#include <gtk/gtk.h>

static void insert_text(GtkTextBuffer *buffer,
char *text)
{
GtkTextIter ite;
gchar *p;

p = g_strdup_printf("This window is %s\nabcde\nfghij\nklmno\npqrst\nuvwsyz\n", text);

gtk_text_buffer_get_iter_at_offset(buffer, &ite, 0);

gtk_text_buffer_insert(buffer, &ite, p, -1);

g_free(p);

}

static GtkWidget *create_scrolled_window(void)
{
GtkWidget *win;

win = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(win),
GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);

return win;
}

static GtkWidget *create_text_common(char *text)
{
GtkWidget *win;
GtkWidget *view;
GtkTextBuffer *buffer;

view = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

win = create_scrolled_window();

gtk_container_add(GTK_CONTAINER(win), view);

insert_text(buffer, text);

gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);

gtk_widget_show_all(win);

return win;

}

static GtkWidget *create_text2(void)
{
return create_text_common("test2");
}

static GtkWidget *create_text1(void)
{
return create_text_common("test1");
}

void create_window(void)
{
GtkWidget *window;
GtkWidget *vbox1;
GtkWidget *vpaned;
GtkWidget *text1;
GtkWidget *text2;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "vpaned test");

gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

vbox1 = gtk_vbox_new(FALSE,0);

vpaned = gtk_vpaned_new();

gtk_container_add(GTK_CONTAINER(vbox1), vpaned);

gtk_widget_show(vpaned);

text1 = create_text1();
gtk_paned_add1(GTK_PANED(vpaned), text1);
gtk_widget_show(text1);

text2 = create_text2();
gtk_paned_add2(GTK_PANED(vpaned), text2);
gtk_widget_show(text2);

gtk_container_add(GTK_CONTAINER(window), vbox1);
gtk_widget_show(vbox1);

gtk_widget_show(window);

}

int main(int argc, char **argv)
{
gtk_init(&argc, &argv);

create_window();

gtk_main();

return 0;
}


テーマ:英語日記 - ジャンル:学問・文化・芸術

GTK+ studying8
This is GTK+ memo. using tree view(it can single and multi select)


#include <gtk/gtk.h>
#define USE_MULTI_SELECT 1

static void print_selected_text(gpointer data, gpointer user_data)
{
gchar *text;
GtkTreePath *path = (GtkTreePath *) data;
GtkTreeModel *model = (GtkTreeModel *) user_data;
GtkTreeIter ite;
int idx = 0;

idx = gtk_tree_path_get_indices(path)[0];
gtk_tree_model_get_iter(model, &ite, path);

gtk_tree_model_get(model, &ite, 0, &text, -1);

g_print("selected text is [%d:%s]\n", idx, text);
}

static gboolean button_clicked(GtkWidget *widget,
gpointer data)
{
GList *list;
GtkWidget *tree = (GtkWidget *) data;
GtkTreeSelection *selection;
GtkTreeModel *model;

selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));

#ifdef USE_MULTI_SELECT
list = gtk_tree_selection_get_selected_rows(selection, &model);
g_list_foreach(list, print_selected_text, model);
#else
if (gtk_tree_selection_get_selected(selection, &model, &ite)) {
gchar *text;

gtk_tree_model_get(model, &ite, 0, &text, -1);
g_print("selected value is [%s]\n",text);
g_free(text);
}
#endif /* USE_MULTI_SELECT */

return FALSE;
}

static GtkWidget *create_button(void *data)
{
GtkWidget *button;

button = gtk_button_new_with_label("ok");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), data);

return button;
}

static GtkWidget *create_tree(void)
{
GtkWidget *tree;
GtkTreeViewColumn *col;
GtkListStore *model;
GtkTreeIter ite;
GtkCellRenderer *cell;
int i = 0;
model = gtk_list_store_new(1, G_TYPE_STRING);

tree = gtk_tree_view_new();
gtk_tree_view_set_model(GTK_TREE_VIEW(tree),
GTK_TREE_MODEL(model));

for (i = 0; i < 5; i++) {
gchar *msg = g_strdup_printf("test%d", i);

gtk_list_store_append(GTK_LIST_STORE(model), &ite);
gtk_list_store_set(GTK_LIST_STORE(model), &ite,
0, msg, -1);
g_free(msg);
}

cell = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new_with_attributes("messages",
cell,
"text",
0,
NULL);

gtk_tree_view_append_column(GTK_TREE_VIEW(tree),
GTK_TREE_VIEW_COLUMN(col));

return tree;
}

static gboolean cursor_changed(GtkWidget *widget,
gpointer data)
{
GtkTreeSelection *selection;
gint cnt = 0;

selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
cnt = gtk_tree_selection_count_selected_rows(selection) + 1;

g_print("count is %d\n", cnt);

return TRUE;
}

void create_window(void)
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *vbox1;
GtkWidget *vbox2;
GtkWidget *button;
GtkWidget *separator;
GtkWidget *tree;
GtkTreeSelection *selection;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "tree view test");

gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

hbox = gtk_hbox_new(FALSE, 0);
vbox1 = gtk_vbox_new(FALSE,0);
vbox2 = gtk_vbox_new(FALSE, 0);

gtk_container_add(GTK_CONTAINER(vbox1), hbox);

tree = create_tree();
gtk_widget_set_size_request(GTK_WIDGET(tree), 180, 200);

selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));

#ifdef USE_MULTI_SELECT
/* we can multi select */
gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
#endif /* USE_MULTI_SELECT */
g_signal_connect(G_OBJECT(tree), "cursor-changed",
G_CALLBACK(cursor_changed), NULL);

gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE, TRUE, 0);

gtk_widget_show(tree);

gtk_widget_show(hbox);

gtk_container_add(GTK_CONTAINER(window), vbox1);
gtk_widget_show(vbox1);

separator = gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox1), separator, FALSE, TRUE, 0);
gtk_widget_show(separator);

button = create_button(tree);
gtk_box_pack_start(GTK_BOX(vbox2), button, TRUE, TRUE, 0);
gtk_widget_show(button);

gtk_container_add(GTK_CONTAINER(vbox1), vbox2);
gtk_widget_show(vbox2);

gtk_widget_show(window);

}

int main(int argc, char **argv)
{
gtk_init(&argc, &argv);

create_window();

gtk_main();

return 0;
}



テーマ:英語日記 - ジャンル:学問・文化・芸術

GTK+ studying7
This is GTK+ memo. using combo box.


#include <gtk/gtk.h>

static gboolean button_clicked(GtkWidget *widget,
gpointer data)
{
GtkWidget *combo = (GtkWidget *) data;

gchar *str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(combo));
gint idx = gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
g_print("active text is [%s(index:%d)]\n", str, idx);

return FALSE;
}

static GtkWidget *create_button(void *data)
{
GtkWidget *button;

button = gtk_button_new_with_label("ok");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), data);

return button;
}

void create_window(void)
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *vbox1;
GtkWidget *vbox2;
GtkWidget *button;
GtkWidget *separator;
GtkWidget *combo;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "combo box test");

gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

hbox = gtk_hbox_new(FALSE, 0);
vbox1 = gtk_vbox_new(FALSE,0);
vbox2 = gtk_vbox_new(FALSE, 0);

gtk_container_add(GTK_CONTAINER(vbox1), hbox);

combo = gtk_combo_box_new_text();
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "test1");
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "test2");
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "test3");
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "test4");
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "test5");
gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);

gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);

gtk_widget_show(combo);

gtk_widget_show(hbox);

gtk_container_add(GTK_CONTAINER(window), vbox1);
gtk_widget_show(vbox1);

separator = gtk_hseparator_new ();
gtk_box_pack_start(GTK_BOX(vbox1), separator, FALSE, TRUE, 0);
gtk_widget_show(separator);

button = create_button(combo);
gtk_box_pack_start(GTK_BOX(vbox2), button, TRUE, TRUE, 0);
gtk_widget_show(button);

gtk_container_add(GTK_CONTAINER(vbox1), vbox2);
gtk_widget_show(vbox2);

gtk_widget_show(window);

}

int main(int argc, char **argv)
{
gtk_init(&argc, &argv);

create_window();

gtk_main();

return 0;
}



テーマ:英語日記 - ジャンル:学問・文化・芸術

GTK+ studying6
This is GTK+ memo. using spin button.


#include <gtk/gtk.h>

static gboolean button_clicked(GtkWidget *widget,
gpointer data)
{
GtkWidget *spin = (GtkWidget *) data;

gint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));

g_print("selected value is [%d]\n", value);

return FALSE;
}

static GtkWidget *create_button(void *data)
{
GtkWidget *button;

button = gtk_button_new_with_label("ok");
g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), data);

return button;
}

void create_window(void)
{
GtkWidget *window;
GtkWidget *hbox;
GtkWidget *vbox1;
GtkWidget *vbox2;
GtkWidget *button;
GtkWidget *separator;
GtkWidget *spin;
GtkObject *adjstment;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "spin button test");

gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

hbox = gtk_hbox_new(FALSE, 0);
vbox1 = gtk_vbox_new(FALSE,0);
vbox2 = gtk_vbox_new(FALSE, 0);

gtk_container_add(GTK_CONTAINER(vbox1), hbox);

/* range is 0 to 10 */
adjstment = gtk_adjustment_new(0.0, 0.0, 10.0, 1.0, 1.0, 0.0);

spin = gtk_spin_button_new(GTK_ADJUSTMENT(adjstment), 0, 0);

gtk_box_pack_start(GTK_BOX(hbox), spin, TRUE, TRUE, 0);
gtk_widget_show(spin);

gtk_widget_show(hbox);

gtk_container_add(GTK_CONTAINER(window), vbox1);
gtk_widget_show(vbox1);

separator = gtk_hseparator_new ();
gtk_box_pack_start(GTK_BOX(vbox1), separator, FALSE, TRUE, 0);
gtk_widget_show(separator);

button = create_button(spin);
gtk_box_pack_start(GTK_BOX(vbox2), button, TRUE, TRUE, 0);
gtk_widget_show(button);

gtk_container_add(GTK_CONTAINER(vbox1), vbox2);
gtk_widget_show(vbox2);

gtk_widget_show(window);

}

int main(int argc, char **argv)
{
gtk_init(&argc, &argv);

create_window();

gtk_main();

return 0;
}


テーマ:英語日記 - ジャンル:学問・文化・芸術



上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。