-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.c
130 lines (94 loc) · 3.47 KB
/
write.c
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
#include <gtk/gtk.h>
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
static gint
my_popup_handler (GtkWidget *widget, GdkEvent *event)
{
GtkMenu *menu;
GdkEventButton *event_button;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
/* The "widget" is the menu that was supplied when
* g_signal_connect_swapped() was called.
*/
menu = GTK_MENU (widget);
if (event->type == GDK_BUTTON_PRESS)
{
event_button = (GdkEventButton *) event;
if (event_button->button == 1)
{
gtk_widget_show_all(GTK_WIDGET(menu));
gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
event_button->button, event_button->time);
return TRUE;
}
}
return FALSE;
}
void save_file (gpointer user_data)
{
gchar filename[] = "testfile1.txt";
gchar *contents;
GtkTextIter *start, *end;
gtk_text_buffer_get_bounds (gtk_text_view_get_buffer(GTK_TEXT_VIEW (user_data)), start, end);
contents = gtk_text_buffer_get_text (gtk_text_view_get_buffer(GTK_TEXT_VIEW (user_data)), start, end, TRUE);
g_file_set_contents (filename, contents, -1, NULL);
g_free(contents);
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *text;
GtkWidget *menu;
GtkWidget *menuitem;
GtkWidget *menuitem1;
GtkWidget *menuitem2;
GdkColor color1;
GtkAccelGroup *accel_group;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
gtk_widget_set_size_request((window),600,400);
gtk_window_set_title(GTK_WINDOW(window),"Editor");
gtk_window_fullscreen(GTK_WINDOW(window));
gtk_window_set_keep_above(GTK_WINDOW(window),TRUE);
text=gtk_text_view_new();
// gtk_text_view_set_indent(GtkTextView *text_view,gint indent);
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text),260);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text),260);
gdk_color_parse("black",&color1);
gtk_widget_modify_base(text,GTK_STATE_NORMAL,&color1);
gdk_color_parse("green",&color1);
gtk_widget_modify_text(text,GTK_STATE_NORMAL,&color1);
menu=gtk_menu_new();
menuitem=gtk_menu_item_new_with_label ("Quit");
menuitem1=gtk_menu_item_new_with_label ("Save");
gtk_menu_attach(GTK_MENU(menu),menuitem1,1,2,2,3);
gtk_menu_attach(GTK_MENU(menu),menuitem,1,2,1,2);
gtk_container_add(GTK_CONTAINER(window),text);
//gtk_container_add(GTK_CONTAINER(window),menu);
g_signal_connect_swapped (text, "button_press_event",
G_CALLBACK (my_popup_handler), menu);
gtk_widget_show_all(GTK_WIDGET(window));
// gtk_widget_show_all((menu));
// gtk_menu_popup(GTK_MENU(menu),NULL,NULL,NULL,NULL,1,gtk_get_current_event_time());
g_signal_connect(menuitem,"button_press_event",G_CALLBACK(destroy),NULL);
g_signal_connect(menuitem1,"button_press_event",G_CALLBACK(save_file),text);
gtk_main();
return 0;
}