実践Linux                 TOPへ  Cプログラミング目次へ

Glade2/GTK+  2010年5月

●テキスト・ビューを使った簡単なエディター

ファイル選択ダイアログを使用したファイルのオープンと保存。
編集では、cut、copy、pasteの機能(ショートカットキー)。

アプリケーション→プログラミング→Gladeを起動して、新規→GTK+プロジェクト

@ウィンドウの作成
A垂直ボックス2段の配置
B上段にメニュー・バー
C下段にテキスト・ビュー
Dファイル選択ダイアロクの作成

gtkでは変数をグローバル化してデータをやりとりする代わりに、やりとりしたいデータをオブジェクトにカプセル化して受け渡しする機能がある。
g_object_set_data (G_OBJECT(textview), “entry”, (gpointer) entry1); //textviewオブジェクトにキー“entry”とentry1のデータをセット
entry = GTK_WIDGET (g_object_get_data(G_OBJECT (textview), “entry”)); //g_object_get_dataでデータを受け取る



プログラムのダウンロード

実行ファイルはsrc/project3です。


●プログラム・ソース
赤字箇所は編集した場所。

main.c ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gtk/gtk.h>

#include “interface.h”
#include “support.h”

int
main (int argc, char *argv[])
{
GtkWidget *window1;

#ifdef ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, “UTF-8”);
textdomain (GETTEXT_PACKAGE);
#endif

gtk_set_locale ();
gtk_init (&argc, &argv);

add_pixmap_directory (PACKAGE_DATA_DIR “/” PACKAGE “/pixmaps”);

/*
* The following code was added by Glade to create one of each component
* (except popup menus), just so that you see something after building
* the project. Delete any components that you don’t want shown initially.
*/
window1 = create_window1 ();
gtk_widget_show (window1);

g_signal_connect (G_OBJECT (window1), “destroy”,
G_CALLBACK (gtk_main_quit), NULL);


gtk_main ();
return 0;
}

interface.h ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
GtkWidget* create_window1 (void);
GtkWidget* create_filechooserdialog1 (void);

interface.c ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include “callbacks.h”
#include “interface.h”
#include “support.h”

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
g_object_set_data_full (G_OBJECT (component), name, \
gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget*
create_window1 (void)
{
GtkWidget *window1;
GtkWidget *vbox1;
GtkWidget *menubar1;
GtkWidget *menuitem1;
GtkWidget *menu1;
GtkWidget *_______n_1;
GtkWidget *_______o_1;
GtkWidget *_______s_1;
GtkWidget *________________a_1;
GtkWidget *separatormenuitem1;
GtkWidget *_______q_1;
GtkWidget *menuitem2;
GtkWidget *menu2;
GtkWidget *_____________t_1;
GtkWidget *__________c_1;
GtkWidget *_____________p_1;
GtkWidget *_______d_1;
GtkWidget *menuitem3;
GtkWidget *menu3;
GtkWidget *menuitem4;
GtkWidget *menu4;
GtkWidget *_______a_1;
GtkWidget *scrolledwindow1;
GtkWidget *textview1;
GtkAccelGroup *accel_group;

accel_group = gtk_accel_group_new ();

window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), _(“window1”));

vbox1 = gtk_vbox_new (FALSE, 0);
gtk_widget_show (vbox1);
gtk_container_add (GTK_CONTAINER (window1), vbox1);

menubar1 = gtk_menu_bar_new ();
gtk_widget_show (menubar1);
gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);

menuitem1 = gtk_menu_item_new_with_mnemonic (_(“\343\203\225\343\202\241\343\202\244\343\203\253(_F)”));
gtk_widget_show (menuitem1);
gtk_container_add (GTK_CONTAINER (menubar1), menuitem1);

menu1 = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem1), menu1);

_______n_1 = gtk_image_menu_item_new_from_stock (“gtk-new”, accel_group);
gtk_widget_show (_______n_1);
gtk_container_add (GTK_CONTAINER (menu1), _______n_1);
gtk_widget_add_accelerator(_______n_1, “activate”, accel_group, GDK_N, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Nの設定

_______o_1 = gtk_image_menu_item_new_from_stock (“gtk-open”, accel_group);
gtk_widget_show (_______o_1);
gtk_container_add (GTK_CONTAINER (menu1), _______o_1);
gtk_widget_add_accelerator(_______o_1, “activate”, accel_group, GDK_O, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Oの設定

_______s_1 = gtk_image_menu_item_new_from_stock (“gtk-save”, accel_group);
gtk_widget_show (_______s_1);
gtk_container_add (GTK_CONTAINER (menu1), _______s_1);
gtk_widget_add_accelerator(_______s_1, “activate”, accel_group, GDK_S, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Sの設定

________________a_1 = gtk_image_menu_item_new_from_stock (“gtk-save-as”, accel_group);
gtk_widget_show (________________a_1);
gtk_container_add (GTK_CONTAINER (menu1), ________________a_1);
gtk_widget_add_accelerator(________________a_1, “activate”, accel_group, GDK_A, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Aの設定

separatormenuitem1 = gtk_separator_menu_item_new ();
gtk_widget_show (separatormenuitem1);
gtk_container_add (GTK_CONTAINER (menu1), separatormenuitem1);
gtk_widget_set_sensitive (separatormenuitem1, FALSE);

_______q_1 = gtk_image_menu_item_new_from_stock (“gtk-quit”, accel_group);
gtk_widget_show (_______q_1);
gtk_container_add (GTK_CONTAINER (menu1), _______q_1);
gtk_widget_add_accelerator(_______q_1, “activate”, accel_group, GDK_Q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Qの設定

menuitem2 = gtk_menu_item_new_with_mnemonic (_(“\347\267\250\351\233\206(_E)”));
gtk_widget_show (menuitem2);
gtk_container_add (GTK_CONTAINER (menubar1), menuitem2);

menu2 = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem2), menu2);

_____________t_1 = gtk_image_menu_item_new_from_stock (“gtk-cut”, accel_group);
gtk_widget_show (_____________t_1);
gtk_container_add (GTK_CONTAINER (menu2), _____________t_1);
gtk_widget_add_accelerator(_____________t_1, “activate”, accel_group, GDK_X, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Xの設定

__________c_1 = gtk_image_menu_item_new_from_stock (“gtk-copy”, accel_group);
gtk_widget_show (__________c_1);
gtk_container_add (GTK_CONTAINER (menu2), __________c_1);
gtk_widget_add_accelerator(__________c_1, “activate”, accel_group, GDK_C, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Cの設定

_____________p_1 = gtk_image_menu_item_new_from_stock (“gtk-paste”, accel_group);
gtk_widget_show (_____________p_1);
gtk_container_add (GTK_CONTAINER (menu2), _____________p_1);
gtk_widget_add_accelerator(_____________p_1, “activate”, accel_group, GDK_V, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Vの設定

_______d_1 = gtk_image_menu_item_new_from_stock (“gtk-delete”, accel_group);
gtk_widget_show (_______d_1);
gtk_container_add (GTK_CONTAINER (menu2), _______d_1);
gtk_widget_add_accelerator(_______d_1, “activate”, accel_group, GDK_D, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE); //ショートカットキーctrl+Dの設定

menuitem3 = gtk_menu_item_new_with_mnemonic (_(“\350\241\250\347\244\272(_V)”));
gtk_widget_show (menuitem3);
gtk_container_add (GTK_CONTAINER (menubar1), menuitem3);

menu3 = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem3), menu3);

menuitem4 = gtk_menu_item_new_with_mnemonic (_(“\343\203\230\343\203\253\343\203\227(_H)”));
gtk_widget_show (menuitem4);
gtk_container_add (GTK_CONTAINER (menubar1), menuitem4);

menu4 = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem4), menu4);

_______a_1 = gtk_menu_item_new_with_mnemonic (_(“\346\203\205\345\240\261(_A)”));
gtk_widget_show (_______a_1);
gtk_container_add (GTK_CONTAINER (menu4), _______a_1);

scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolledwindow1);
gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);

textview1 = gtk_text_view_new ();
gtk_widget_show (textview1);
gtk_container_add (GTK_CONTAINER (scrolledwindow1), textview1);

g_signal_connect ((gpointer) _______n_1, “activate”,
G_CALLBACK (on________n_1_activate),
textview1);
g_signal_connect ((gpointer) _______o_1, “activate”,
G_CALLBACK (on________o_1_activate),
textview1);
g_signal_connect ((gpointer) _______s_1, “activate”,
G_CALLBACK (on________s_1_activate),
textview1);
g_signal_connect ((gpointer) ________________a_1, “activate”,
G_CALLBACK (on_________________a_1_activate),
textview1);
g_signal_connect ((gpointer) _______q_1, “activate”,
G_CALLBACK (on________q_1_activate),
NULL);
g_signal_connect ((gpointer) _____________t_1, “activate”,
G_CALLBACK (on______________t_1_activate),
textview1);
g_signal_connect ((gpointer) __________c_1, “activate”,
G_CALLBACK (on___________c_1_activate),
textview1);
g_signal_connect ((gpointer) _____________p_1, “activate”,
G_CALLBACK (on______________p_1_activate),
textview1);
g_signal_connect ((gpointer) _______d_1, “activate”,
G_CALLBACK (on________d_1_activate),
textview1);
g_signal_connect ((gpointer) _______a_1, “activate”,
G_CALLBACK (on________a_1_activate),
NULL);

/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, “window1”);
GLADE_HOOKUP_OBJECT (window1, vbox1, “vbox1”);
GLADE_HOOKUP_OBJECT (window1, menubar1, “menubar1”);
GLADE_HOOKUP_OBJECT (window1, menuitem1, “menuitem1”);
GLADE_HOOKUP_OBJECT (window1, menu1, “menu1”);
GLADE_HOOKUP_OBJECT (window1, _______n_1, “_______n_1”);
GLADE_HOOKUP_OBJECT (window1, _______o_1, “_______o_1”);
GLADE_HOOKUP_OBJECT (window1, _______s_1, “_______s_1”);
GLADE_HOOKUP_OBJECT (window1, ________________a_1, “________________a_1”);
GLADE_HOOKUP_OBJECT (window1, separatormenuitem1, “separatormenuitem1”);
GLADE_HOOKUP_OBJECT (window1, _______q_1, “_______q_1”);
GLADE_HOOKUP_OBJECT (window1, menuitem2, “menuitem2”);
GLADE_HOOKUP_OBJECT (window1, menu2, “menu2”);
GLADE_HOOKUP_OBJECT (window1, _____________t_1, “_____________t_1”);
GLADE_HOOKUP_OBJECT (window1, __________c_1, “__________c_1”);
GLADE_HOOKUP_OBJECT (window1, _____________p_1, “_____________p_1”);
GLADE_HOOKUP_OBJECT (window1, _______d_1, “_______d_1”);
GLADE_HOOKUP_OBJECT (window1, menuitem3, “menuitem3”);
GLADE_HOOKUP_OBJECT (window1, menu3, “menu3”);
GLADE_HOOKUP_OBJECT (window1, menuitem4, “menuitem4”);
GLADE_HOOKUP_OBJECT (window1, menu4, “menu4”);
GLADE_HOOKUP_OBJECT (window1, _______a_1, “_______a_1”);
GLADE_HOOKUP_OBJECT (window1, scrolledwindow1, “scrolledwindow1”);
GLADE_HOOKUP_OBJECT (window1, textview1, “textview1”);

gtk_window_add_accel_group (GTK_WINDOW (window1), accel_group);

return window1;
}

GtkWidget*
create_filechooserdialog1 (void)
{
GtkWidget *filechooserdialog1;
GtkWidget *dialog_vbox1;
GtkWidget *dialog_action_area1;
GtkWidget *button1;
GtkWidget *button2;

filechooserdialog1 = gtk_file_chooser_dialog_new (“”, NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL);
gtk_window_set_type_hint (GTK_WINDOW (filechooserdialog1), GDK_WINDOW_TYPE_HINT_DIALOG);

dialog_vbox1 = GTK_DIALOG (filechooserdialog1)->vbox;
gtk_widget_show (dialog_vbox1);

dialog_action_area1 = GTK_DIALOG (filechooserdialog1)->action_area;
gtk_widget_show (dialog_action_area1);
gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);

button1 = gtk_button_new_from_stock (“gtk-cancel”);
gtk_widget_show (button1);
gtk_dialog_add_action_widget (GTK_DIALOG (filechooserdialog1), button1, GTK_RESPONSE_CANCEL);
GTK_WIDGET_SET_FLAGS (button1, GTK_CAN_DEFAULT);

button2 = gtk_button_new_from_stock (“gtk-open”);
gtk_widget_show (button2);
gtk_dialog_add_action_widget (GTK_DIALOG (filechooserdialog1), button2, GTK_RESPONSE_OK);
GTK_WIDGET_SET_FLAGS (button2, GTK_CAN_DEFAULT);

/* Store pointers to all widgets, for use by lookup_widget(). */
GLADE_HOOKUP_OBJECT_NO_REF (filechooserdialog1, filechooserdialog1, “filechooserdialog1”);
GLADE_HOOKUP_OBJECT_NO_REF (filechooserdialog1, dialog_vbox1, “dialog_vbox1”);
GLADE_HOOKUP_OBJECT_NO_REF (filechooserdialog1, dialog_action_area1, “dialog_action_area1”);
GLADE_HOOKUP_OBJECT (filechooserdialog1, button1, “button1”);
GLADE_HOOKUP_OBJECT (filechooserdialog1, button2, “button2”);

gtk_widget_grab_default (button2);
return filechooserdialog1;
}

callbacks.h ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
#include <gtk/gtk.h>


void
on________n_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on________o_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on________s_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on_________________a_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on________q_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on______________t_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on___________c_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on______________p_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on________d_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

void
on________a_1_activate (GtkMenuItem *menuitem,
gpointer user_data);

callbacks.c ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gtk/gtk.h>

#include “callbacks.h”
#include “interface.h”
#include “support.h”

#include <string.h> //strncat
#include <stdlib.h> //exit

char fname[FILENAME_MAX] = “”;

void on_button1_clicked (GtkMenuItem *menuitem, gpointer user_data);
void on_button2_clicked (GtkMenuItem *menuitem, gpointer user_data);
void on_button3_clicked (GtkMenuItem *menuitem, gpointer user_data);



void
on________n_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;

textview = GTK_TEXT_VIEW (user_data);
buffer = gtk_text_view_get_buffer (textview);
gtk_text_buffer_set_text (buffer, “”, -1);

strcpy(fname, “”);

}


void
on________o_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
#define BUFSIZE 65536
#define BUFSIZE_s 1024

GtkTextView *textview;
GtkWidget *dialog;
gint response;
gchar *filename;
GtkTextBuffer *buffer;
FILE *fp;
char text[BUFSIZE] = “”; //空で初期化しておかないと最初にへんな文字が入るなどトラブルが生じる
char buf[BUFSIZE_s];

textview = GTK_TEXT_VIEW (user_data);

dialog = create_filechooserdialog1 ();
gtk_widget_show_all (dialog);

response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK) //button2が押されたとき
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
//g_print (“%s\n”, filename);

buffer = gtk_text_view_get_buffer (textview);

if ((fp = fopen(filename, “r”)) == NULL)
{
fprintf(stderr,”could not open %s!\n”,filename);

exit(1);
}
while (fgets(buf, BUFSIZE_s, fp) != NULL)
{
strncat(text, buf, BUFSIZE - strlen(text));
}
//printf(“%s”, text);
gtk_text_buffer_set_text (buffer, text, -1);
if(fclose(fp))
{
fprintf(stderr,”could not close %s!\n”,filename);
}
strcpy(fname, filename);
g_free (filename);
}
else if (response == GTK_RESPONSE_CANCEL) //button1が押されたとき
{
g_print (“Cancel button was pressed.\n”);
}
else
{
g_print (“Another response was recieved.\n”);
}
gtk_widget_destroy (dialog);

}


void
on________s_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;
GtkTextIter start, end;
FILE *fp;
gchar *text;

textview = GTK_TEXT_VIEW (user_data);

if (strcmp(fname, “”) == 0)
{
fprintf(stderr,”no file!\n”);
on_________________a_1_activate(NULL, textview);
}
else
{
buffer = gtk_text_view_get_buffer (textview);
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);

if ((fp = fopen(fname, “w”)) == NULL)
{
fprintf(stderr,”could not open %s!\n”,fname);
exit(1);
}
fputs(text,fp);
if(fclose(fp))
{
fprintf(stderr,”could not close %s!\n”,fname);
}
g_free (text);
}

}

void
on_________________a_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
#define BUFSIZE 65536
#define BUFSIZE_s 1024

GtkWidget *window1;
GtkWidget *fixed1;
GtkWidget *entry1;
GtkWidget *label1;
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *button1;
GtkTextView *textview;

textview = GTK_TEXT_VIEW (user_data);

window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window1), _(“file name”));

fixed1 = gtk_fixed_new ();
gtk_widget_show (fixed1);
gtk_container_add (GTK_CONTAINER (window1), fixed1);

entry1 = gtk_entry_new ();
gtk_widget_show (entry1);
gtk_fixed_put (GTK_FIXED (fixed1), entry1, 24, 32);
gtk_widget_set_size_request (entry1, 408, 24);
gtk_entry_set_text (GTK_ENTRY (entry1), _(“/root/sample.txt”));
gtk_entry_set_invisible_char (GTK_ENTRY (entry1), 8226);
g_object_set_data (G_OBJECT(textview), “entry1”, (gpointer) entry1); //textviewオブジェクトにキーとデータをセット
g_object_set_data (G_OBJECT(textview), “window1”, (gpointer) window1); //textviewオブジェクトにキーとデータをセット

label1 = gtk_label_new (_(“\343\203\225\343\202\241\343\202\244\343\203\253\345\220\215\357\274\210\343\203\207\343\202\243\343\203\254\343\202\257\343\203\210\343\203\252\343\201\213\343\202\211\343\203\225\343\203\253\346\214\207\345\256\232\357\274\211\n”));
gtk_widget_show (label1);
gtk_fixed_put (GTK_FIXED (fixed1), label1, 16, 8);
gtk_widget_set_size_request (label1, 264, 16);

button2 = gtk_button_new_with_mnemonic (_(“\343\202\255\343\203\243\343\203\263\343\202\273\343\203\253”));
gtk_widget_show (button2);
gtk_fixed_put (GTK_FIXED (fixed1), button2, 320, 72);
gtk_widget_set_size_request (button2, 96, 24);

button3 = gtk_button_new_with_mnemonic (_(“\357\274\257\357\274\253”));
gtk_widget_show (button3);
gtk_fixed_put (GTK_FIXED (fixed1), button3, 440, 72);
gtk_widget_set_size_request (button3, 64, 24);

button1 = gtk_button_new_with_mnemonic (_(“\351\226\213\343\201\217”));
gtk_widget_show (button1);
gtk_fixed_put (GTK_FIXED (fixed1), button1, 464, 32);
gtk_widget_set_size_request (button1, 56, 24);

gtk_widget_show_all (window1);

g_signal_connect ((gpointer) button1, “clicked”,
G_CALLBACK (on_button1_clicked),
(gpointer) entry1);
g_signal_connect ((gpointer) button2, “clicked”,
G_CALLBACK (on_button2_clicked),
(gpointer) window1);
g_signal_connect ((gpointer) button3, “clicked”,
G_CALLBACK (on_button3_clicked),
(gpointer) textview);

}


void
on________q_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
gtk_main_quit ();
}


void
on______________t_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;
GtkClipboard *clipboard;

textview = GTK_TEXT_VIEW (user_data);
buffer = gtk_text_view_get_buffer (textview);
clipboard = gtk_clipboard_get (GDK_NONE);

gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
}


void
on___________c_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;
GtkClipboard *clipboard;

textview = GTK_TEXT_VIEW (user_data);
buffer = gtk_text_view_get_buffer (textview);
clipboard = gtk_clipboard_get (GDK_NONE);

gtk_text_buffer_copy_clipboard (buffer, clipboard);

}


void
on______________p_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;
GtkClipboard *clipboard;

textview = GTK_TEXT_VIEW (user_data);
buffer = gtk_text_view_get_buffer (textview);
clipboard = gtk_clipboard_get (GDK_NONE);

gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);

}


void
on________d_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkTextView *textview;
GtkTextBuffer *buffer;
//GtkTextIter start, end;

textview = GTK_TEXT_VIEW (user_data);
buffer = gtk_text_view_get_buffer (textview);

//gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
//gtk_text_buffer_delete (buffer, &start, &end);
gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);

}


void
on________a_1_activate (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkWidget *dialog;
GtkAboutDialog *about;
GdkPixbuf *pixbuf;
const gchar *authors[] = {“GTK1”, NULL};
const gchar *documenters[] = {“GTK2”, NULL};
const gchar *translators = “GTK3”;

dialog = gtk_about_dialog_new ();
about = GTK_ABOUT_DIALOG (dialog);
gtk_about_dialog_set_name (about, “GtkAboutDialog-Sample”);
gtk_about_dialog_set_authors (about, authors);
gtk_about_dialog_set_documenters (about, documenters);
gtk_about_dialog_set_translator_credits (about, translators);
gtk_about_dialog_set_version (about, “1.0.0”);
gtk_about_dialog_set_copyright (about, “Copyright (C) 2009”);
gtk_about_dialog_set_comments (about,
“This is a GtkAboutDialog sample “
“program.”);
gtk_about_dialog_set_website (about, “file:///...”);

pixbuf = gdk_pixbuf_new_from_file (“gnome-tigert.png”, NULL);
gtk_about_dialog_set_logo (about, pixbuf);
g_object_unref (pixbuf);

gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);

gtk_widget_show_all (dialog);

}

/////////////////////////////////////////////////////////////////////////////////////////////////

void
on_button1_clicked (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkWidget *dialog;
GtkEntry *entry;
gint response;
gchar *filename;

entry = GTK_ENTRY (user_data);

dialog = create_filechooserdialog1 ();
gtk_widget_show_all (dialog);

response = gtk_dialog_run (GTK_DIALOG (dialog));
if (response == GTK_RESPONSE_OK) //dialogのbutton2が押されたとき
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_entry_set_text (entry, filename);
}
else if (response == GTK_RESPONSE_CANCEL) //dialogのbutton1が押されたとき
{
g_print (“Cancel button was pressed.\n”);
}
else
{
g_print (“Another response was recieved.\n”);
}
gtk_widget_destroy (dialog);
}


void
on_button2_clicked (GtkMenuItem *menuitem,
gpointer user_data)
{
GtkWidget *window;

window = GTK_WIDGET (user_data);
gtk_widget_destroy (window);
}


void
on_button3_clicked (GtkMenuItem *menuitem,
gpointer user_data)
{
#define BUFSIZE 65536

GtkTextView *textview;
GtkTextIter start, end;
GtkTextBuffer *buffer;
FILE *fp;
gchar *text;
GtkWidget *entry;
GtkWidget *window;

textview = GTK_TEXT_VIEW (user_data);

entry = GTK_WIDGET (g_object_get_data(G_OBJECT (textview), “entry1”)); //g_object_get_dataでデータを受け取る
window = GTK_WIDGET (g_object_get_data(G_OBJECT (textview), “window1”)); //g_object_get_dataでデータを受け取る

buffer = gtk_text_view_get_buffer (textview);
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);

strcpy(fname, gtk_entry_get_text(GTK_ENTRY(entry)));

if ((fp = fopen(fname, “w”)) == NULL)
{
fprintf(stderr,”could not open %s!\n”,fname);
exit(1);
}
fputs(text,fp);
//printf(“%s”, text);
if(fclose(fp))
{
fprintf(stderr,”could not close %s!\n”,fname);
}
gtk_widget_destroy (window);
}



TOPへ  Cプログラミング目次へ