-
Notifications
You must be signed in to change notification settings - Fork 4
/
sedit.c
548 lines (426 loc) · 16.3 KB
/
sedit.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
//Widget for Search Bar
typedef struct _SearchBar {
GtkWidget *search_entry;
GtkWidget *sbutton;
GtkWidget *nbutton;
GtkWidget *qbutton;
GtkWidget *text_view;
} SearchBar;
//Widget for open-dialog
typedef struct _OpenDialog{
GtkWidget *window;
GtkTextBuffer *buffer;
gchar *filename;
} OpenDialog;
//Widget for new dialog
typedef struct _NewDialog{
GtkWidget *window;
GtkWidget *text_view;
GtkTextBuffer *buffer;
GtkWidget *statusbar;
} NewDialog;
//Function invoked when save menu item is clicked
void save_dialog_selected(GtkWidget *widget, OpenDialog *sdlog){
GtkWidget *dialog;
gint response;
GtkTextBuffer *buffer;
if (strcmp( gtk_window_get_title(GTK_WINDOW(sdlog->window)) , "Untitled") == 0)
{
dialog = gtk_file_chooser_dialog_new("Save File", GTK_WINDOW(sdlog->window),
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_SAVE, GTK_RESPONSE_APPLY,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
NULL);
response = gtk_dialog_run(GTK_DIALOG(dialog));
if (response == GTK_RESPONSE_APPLY)
{
gchar *filename;
gchar *contents;
GtkTextIter start, end;
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
buffer = sdlog->buffer;
gtk_text_buffer_get_bounds(buffer, &start, &end);
contents = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
g_file_set_contents(filename, contents, -1, NULL);
gtk_window_set_title(GTK_WINDOW(sdlog->window), filename);
sdlog->filename = filename;
}
else if (response == GTK_RESPONSE_CANCEL)
{
gtk_widget_destroy(dialog);
return;
}
gtk_widget_destroy(dialog);
}
else
{
GtkTextIter start, end;
const gchar *filename = gtk_window_get_title(GTK_WINDOW(sdlog->window));
gchar *contents;
gtk_text_buffer_get_bounds(sdlog->buffer, &start, &end);
contents = gtk_text_buffer_get_text(sdlog->buffer, &start, &end, FALSE);
g_file_set_contents(filename, contents, -1, NULL);
}
}
//Function invoked when new menu item is clicked
void new_dialog_selected(GtkWidget *widget, NewDialog *ndlog){
gtk_widget_show(ndlog->text_view);
gtk_widget_show(ndlog->statusbar);
gtk_window_set_title(GTK_WINDOW(ndlog->window), "Untitled");
gtk_text_buffer_set_text(ndlog->buffer, "", -1);
}
//Function invoked when open menu item is clicked
void open_dialog_selected(GtkWidget *widget, OpenDialog *odlog){
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open File",
GTK_WINDOW(odlog->window),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
gchar *filename;
gchar *contents;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
g_file_get_contents(filename, &contents, NULL, NULL);
gtk_text_buffer_set_text(odlog->buffer, contents, -1);
gtk_window_set_title(GTK_WINDOW(odlog->window), filename);
odlog->filename=filename;
g_free (filename);
}
gtk_widget_destroy(dialog);
}
//Functions related to search-bar
void find (GtkTextView *text_view, const gchar *text, GtkTextIter *iter)
{
GtkTextIter mstart, mend;
gboolean found;
GtkTextBuffer *buffer;
GtkTextMark *last_pos;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
found = gtk_text_iter_forward_search (iter, text, 0, &mstart, &mend, NULL);
if (found)
{
gtk_text_buffer_select_range (buffer, &mstart, &mend);
last_pos = gtk_text_buffer_create_mark (buffer, "last_pos",
&mend, FALSE);
gtk_text_view_scroll_mark_onscreen (text_view, last_pos);
}
}
void close_button_clicked (GtkWidget *close_button, SearchBar *sbar)
{
gtk_widget_hide(sbar->search_entry);
gtk_widget_hide(sbar->sbutton);
gtk_widget_hide(sbar->nbutton);
gtk_widget_hide(sbar->qbutton);
}
void find_menu_selected(GtkWidget *widget, SearchBar *sbar)
{
gtk_widget_show(sbar->search_entry);
gtk_widget_show(sbar->sbutton);
gtk_widget_show(sbar->nbutton);
gtk_widget_show(sbar->qbutton);
}
void search_button_clicked (GtkWidget *search_button, SearchBar *sbar)
{
const gchar *text;
GtkTextBuffer *buffer;
GtkTextIter iter;
text = gtk_entry_get_text (GTK_ENTRY (sbar->search_entry));
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (sbar->text_view));
gtk_text_buffer_get_start_iter (buffer, &iter);
find (GTK_TEXT_VIEW (sbar->text_view), text, &iter);
}
void next_button_clicked (GtkWidget *next_button, SearchBar *sbar)
{
const gchar *text;
GtkTextBuffer *buffer;
GtkTextMark *last_pos;
GtkTextIter iter;
text = gtk_entry_get_text (GTK_ENTRY (sbar->search_entry));
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (sbar->text_view));
last_pos = gtk_text_buffer_get_mark (buffer, "last_pos");
if (last_pos == NULL)
return;
gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos);
find (GTK_TEXT_VIEW (sbar->text_view), text, &iter);
}
//Function invoked when font menu item selected
void select_font(GtkWidget *widget, GtkWidget *widget1) {
GtkResponseType result;
GtkWidget *dialog = gtk_font_selection_dialog_new("Select Font");
result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY) {
PangoFontDescription *font_desc;
gchar *fontname = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG(dialog));
font_desc = pango_font_description_from_string(fontname);
gtk_widget_modify_font(GTK_WIDGET(widget1), font_desc);
g_free(fontname);
}
gtk_widget_destroy(dialog);
}
//Function invoked when about menu item is clicked
void show_about(GtkWidget *widget, gpointer data) {
GtkWidget *dialog = gtk_about_dialog_new();
gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "sedit");
gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),
"\nsedit is minimal text editor based on GTK +2 and C . \n");
gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog),
"http://www.incessantmeraki.com");
gtk_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
}
//Function invoked when copy option is used
void copy_to_clipboard(GtkWidget *widget, GtkTextBuffer *buffer){
GtkClipboard *clipboard;
clipboard = gtk_clipboard_get(GDK_NONE);
gtk_text_buffer_copy_clipboard(buffer, clipboard);
}
//Function invoked when cut option is used
void cut_to_clipboard(GtkWidget *widget, GtkTextBuffer *buffer){
GtkClipboard *clipboard;
clipboard = gtk_clipboard_get(GDK_NONE);
gtk_text_buffer_cut_clipboard(buffer, clipboard,TRUE);
}
//Function invoked when paste option is used
void paste_from_clipboard(GtkWidget *widget, GtkTextBuffer *buffer){
GtkClipboard *clipboard;
clipboard = gtk_clipboard_get(GDK_NONE);
gtk_text_buffer_paste_clipboard(buffer, clipboard,NULL,TRUE);
}
//Function invoked when text buffer is changed
void update_statusbar(GtkTextBuffer *buffer,
GtkStatusbar *statusbar) {
gchar *msg;
gint row, col;
GtkTextIter iter;
gtk_statusbar_pop(statusbar, 0);
gtk_text_buffer_get_iter_at_mark(buffer,
&iter, gtk_text_buffer_get_insert(buffer));
row = gtk_text_iter_get_line(&iter);
col = gtk_text_iter_get_line_offset(&iter);
msg = g_strdup_printf("Col: %d Ln: %d", col+1, row+1);
gtk_statusbar_push(statusbar, 0, msg);
g_free(msg);
}
//Function invoked when cursor changes
void mark_set_callback(GtkTextBuffer *buffer,
const GtkTextIter *new_location, GtkTextMark *mark, gpointer data) {
update_statusbar(buffer, GTK_STATUSBAR(data));
}
int main(int argc, char *argv[]) {
//Definitions
//Contains definitions for layout related items
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox;
//Contains Menubar, Menu and menuitems inside it in an iterative way
GtkWidget *menubar;
GtkWidget *fileMenu;
GtkWidget *editMenu;
GtkWidget *optionsMenu;
GtkWidget *helpMenu;
GtkWidget *fileMi;
GtkWidget *newMi;
GtkWidget *openMi;
GtkWidget *saveMi;
GtkWidget *quitMi;
GtkWidget *editMi;
GtkWidget *cutMi;
GtkWidget *copyMi;
GtkWidget *pasteMi;
GtkWidget *optionsMi;
GtkWidget *findMi;
GtkWidget *fontMi;
GtkWidget *helpMi;
GtkWidget *aboutMi;
//Contains statusBar definition
GtkWidget *statusbar;
//contains Text view and related definitions
GtkWidget *text_view;
GtkTextBuffer *buffer;
GtkWidget* scrolledwindow;
//searchbar definition
SearchBar sbar;
//Miscellaneous declaration
// GtkClipboard *clipboard;
GtkAccelGroup *accel_group = NULL;
//initialize our gtk engine
gtk_init(&argc, &argv);
//properties for the main window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_window_set_title(GTK_WINDOW(window), "sedit");
//Setting up vbox and hbox
vbox = gtk_vbox_new(FALSE, 2);
hbox = gtk_hbox_new(FALSE, 2);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
//initilize accel group which enables shortcut(accelerator) keys
accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
//initialize menu bar
menubar = gtk_menu_bar_new();
// Setting File Menu //
//initialize file menu and define file items in it
fileMenu = gtk_menu_new();
fileMi = gtk_menu_item_new_with_mnemonic("_File");
newMi = gtk_menu_item_new_with_label("New");
openMi = gtk_menu_item_new_with_label("Open");
saveMi = gtk_menu_item_new_with_label("Save");
quitMi = gtk_menu_item_new_with_label("Quit");
//linking menu and its respective menu items
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), newMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), openMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), saveMi);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quitMi);
//Defining shortcut keys for menu items
gtk_widget_add_accelerator(newMi, "activate", accel_group,
GDK_n, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(openMi, "activate", accel_group,
GDK_o, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(saveMi, "activate", accel_group,
GDK_s, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(quitMi, "activate", accel_group,
GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
//attaching FileMenu to menubar
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
//Setting Edit Menu //
//initialize edit menu and define file items in it
editMenu = gtk_menu_new();
editMi = gtk_menu_item_new_with_mnemonic("_Edit");
cutMi = gtk_menu_item_new_with_label("Cut");
copyMi = gtk_menu_item_new_with_label("Copy");
pasteMi = gtk_menu_item_new_with_label("Paste");
//linking edit menu and its respective menu items
gtk_menu_item_set_submenu(GTK_MENU_ITEM(editMi), editMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), cutMi);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), copyMi);
gtk_menu_shell_append(GTK_MENU_SHELL(editMenu), pasteMi);
//Defining shortcut keys for edit menu items
gtk_widget_add_accelerator(cutMi, "activate", accel_group,
GDK_x, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(copyMi, "activate", accel_group,
GDK_c, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
gtk_widget_add_accelerator(pasteMi, "activate", accel_group,
GDK_v, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
//attaching EditMenu to menubar
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), editMi);
//Setting Options Menu //
//initialize options menu and define file items in it
optionsMenu = gtk_menu_new();
optionsMi = gtk_menu_item_new_with_mnemonic("_Options");
findMi = gtk_menu_item_new_with_label("Find");
fontMi = gtk_menu_item_new_with_label("Font");
//linking options menu and its respective menu items
gtk_menu_item_set_submenu(GTK_MENU_ITEM(optionsMi), optionsMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(optionsMenu), findMi);
gtk_menu_shell_append(GTK_MENU_SHELL(optionsMenu), fontMi);
//Defining shortcut keys for edit menu items
gtk_widget_add_accelerator(findMi, "activate", accel_group,
GDK_f, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
//attaching optionsMenu to menubar
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), optionsMi);
//Setting Help Menu //
//initialize help menu and define file items in it
helpMenu = gtk_menu_new();
helpMi = gtk_menu_item_new_with_mnemonic("_Help");
aboutMi = gtk_menu_item_new_with_label("About");
//linking help menu and its respective menu items
gtk_menu_item_set_submenu(GTK_MENU_ITEM(helpMi), helpMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(helpMenu), aboutMi);
//attaching helpMenu to menubar
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), helpMi);
//connecting layout of verticalbox to menubar
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
//Initializing scrolledwindows
scrolledwindow = gtk_scrolled_window_new(NULL,NULL);
//Initializing textviewer and displaying it.
text_view = gtk_text_view_new ();
gtk_container_add(GTK_CONTAINER(scrolledwindow),text_view);
gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, 1, 1, 0);
// Initializing buffer
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
//Set up search bar
sbar.text_view = text_view;
sbar.search_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (hbox), sbar.search_entry, TRUE, TRUE, 0);
sbar.sbutton = gtk_button_new_with_label ("Search");
gtk_box_pack_start (GTK_BOX (hbox),sbar.sbutton, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (sbar.sbutton), "clicked",
G_CALLBACK (search_button_clicked), &sbar);
sbar.nbutton = gtk_button_new_with_label ("Next");
gtk_box_pack_start (GTK_BOX (hbox), sbar.nbutton, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (sbar.nbutton), "clicked",
G_CALLBACK (next_button_clicked), &sbar);
sbar.qbutton = gtk_button_new_with_label ("Close");
gtk_box_pack_start (GTK_BOX (hbox), sbar.qbutton, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (sbar.qbutton), "clicked",
G_CALLBACK (close_button_clicked), &sbar);
//initializing open dialog
OpenDialog odlog;
odlog.window = window;
odlog.buffer= buffer;
//initializing new dialog
NewDialog ndlog;
ndlog.window=window;
ndlog.buffer = buffer;
ndlog.text_view = text_view;
//initializing save dialog
OpenDialog sdlog;
sdlog.window=window;
sdlog.buffer=buffer;
//Initializing statusbar and displaying it.
statusbar = gtk_statusbar_new();
gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
//initializing status bar to new dialog
ndlog.statusbar = statusbar;
//connect functions to events that is signalled by gtk
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(quitMi), "activate",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(copyMi),"activate",
G_CALLBACK(copy_to_clipboard),buffer);
g_signal_connect(G_OBJECT(cutMi),"activate",
G_CALLBACK(cut_to_clipboard),buffer);
g_signal_connect(G_OBJECT(aboutMi),"activate",
G_CALLBACK(show_about),(gpointer) window);
g_signal_connect(G_OBJECT(pasteMi),"activate",
G_CALLBACK(paste_from_clipboard),buffer);
g_signal_connect(G_OBJECT(fontMi),"activate",
G_CALLBACK(select_font),text_view);
g_signal_connect(G_OBJECT(findMi),"activate",
G_CALLBACK(find_menu_selected), &sbar);
g_signal_connect(G_OBJECT(openMi),"activate",
G_CALLBACK(open_dialog_selected), &odlog);
g_signal_connect(G_OBJECT(newMi),"activate",
G_CALLBACK(new_dialog_selected), &ndlog);
g_signal_connect(G_OBJECT(saveMi),"activate",
G_CALLBACK(save_dialog_selected), &sdlog);
g_signal_connect(buffer, "changed",
G_CALLBACK(update_statusbar), statusbar);
g_signal_connect_object(buffer, "mark_set",
G_CALLBACK(mark_set_callback), statusbar, 0);
// display all the windows and enter into event loop
gtk_widget_show_all(window);
//Hide find options at the start
gtk_widget_hide(sbar.search_entry);
gtk_widget_hide(sbar.sbutton);
gtk_widget_hide(sbar.nbutton);
gtk_widget_hide(sbar.qbutton);
//hide the text_viewer in the beginning
gtk_widget_hide(text_view);
//hide status bar
gtk_widget_hide(statusbar);
//Display line and column number in status bar
update_statusbar(buffer, GTK_STATUSBAR(statusbar));
gtk_main();
return 0;
}