#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdio.h>
#include <glib.h>
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *view;
GtkWidget *findview;
GtkWidget *findwindow;
GtkWidget *menubar;
GtkTextBuffer *buffer;
int arraypos[1000];
int position = 0;
GtkWidget *findradio1;
GtkWidget *findradio2;
void deleteall();
void Messagebox(char *data)
{
GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,data );
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy( GTK_WIDGET(dialog));
}
void NumericalMessagebox(int number)
{
char *textbuffer = (char*)malloc(sizeof(int));
itoa(number,textbuffer, 10);
GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,textbuffer );
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy( GTK_WIDGET(dialog));
}
int FindWord(char *T,char *W,int pos)
{
int flen = strlen(T);
int slen = strlen(W);
int i;
int counter = 0;
for (i = pos; i < flen; i++)
{
if(strncmp(T+i, W, slen)==0){
arraypos[counter] = i;
counter++;
}
}
return counter;
}
char * GetText(GtkWidget *tview)
{
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tview));
GtkTextIter iterstart, iterend;
gtk_text_buffer_get_start_iter(buffer,&iterstart);
gtk_text_buffer_get_end_iter(buffer,&iterend);
char * data = gtk_text_buffer_get_text(buffer,&iterstart,&iterend,0);
return data;
}
void opendialog() {
GtkWidget *dialog;
char *filename;
dialog = gtk_file_chooser_dialog_new ("Open File",
NULL,
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)
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
}
gtk_widget_destroy (dialog);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
FILE * pFile = fopen(g_locale_from_utf8(filename,-1,0,0,0), "r");
fseek (pFile, 0, SEEK_END);
int filesize = ftell (pFile);
fseek (pFile, 0, SEEK_SET);
char *data =(char*) malloc (filesize);
fread(data,1,filesize,pFile);
data[filesize] = 0;
fclose (pFile);
gtk_text_buffer_set_text(buffer,data,strlen(data));
free(data);
}
void savedialog()
{
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Save File",
NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
char *filename;
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
}
gtk_widget_destroy (dialog);
char *data = GetText(view);
FILE * pFile = fopen (g_locale_from_utf8(filename,-1,0,0,0), "w");
fwrite (data , sizeof(char), strlen(data), pFile);
fclose (pFile);
free(data);
}
void openfontdialog()
{
GtkWidget *gfs = gtk_font_selection_dialog_new("Select font");
gtk_font_selection_dialog_set_font_name(gfs,"Serif 20");
gtk_font_selection_dialog_set_preview_text(gfs,"Hello");
gtk_widget_show(gfs);
int result = gtk_dialog_run(GTK_DIALOG(gfs));
if ( result = GTK_RESPONSE_OK)
{
gchar *gfn = gtk_font_selection_dialog_get_font_name(gfs);
gtk_widget_modify_font(view,pango_font_description_from_string(gfn));
gtk_widget_hide(gfs);
}
if ( result = GTK_RESPONSE_CANCEL)
{
gtk_widget_hide(gfs);
}
}
void gettextdialog(GtkWidget *widget)
{
char *data1 = GetText(view);
char *data2 = GetText(findview);
int counter = FindWord(data1,data2,0);
if (position > counter - 1) position = counter - 1;
if (position <= 0) position = 0;
//NumericalMessagebox(position);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(findradio1)))
{
GtkTextIter start;
gtk_text_buffer_get_start_iter(buffer,&start);
gtk_text_iter_set_offset(&start,arraypos[position]);
gtk_text_buffer_place_cursor(buffer,&start);
gtk_widget_hide_all(findwindow);
position++;
}
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(findradio2)))
{
GtkTextIter start;
gtk_text_buffer_get_start_iter(buffer,&start);
gtk_text_iter_set_offset(&start,arraypos[position]);
gtk_text_buffer_place_cursor(buffer,&start);
gtk_widget_hide_all(findwindow);
position--;
}
}
void openfinddialog()
{
gtk_widget_show_all(findwindow);
}
void insertdate()
{
GtkTextIter iter;
const time_t timer = time(NULL);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
gtk_text_buffer_get_end_iter(buffer,&iter);
gtk_text_buffer_insert(buffer, &iter, "\n", -1);
gtk_text_buffer_insert(buffer, &iter, ctime(&timer), -1);
}
void deleteall()
{
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
GtkTextIter siter,eiter;
gtk_text_buffer_get_start_iter(buffer,&siter);
gtk_text_buffer_get_end_iter(buffer,&eiter);
gtk_text_buffer_delete(buffer,&siter,&eiter);
}
void selectall()
{
GtkTextIter iter;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
gtk_text_buffer_create_tag(buffer, "lmarg", "left_margin",0, NULL);
gtk_text_buffer_create_tag(buffer, "bg", "background", "gray", NULL);
char *data = GetText(view);
deleteall();
gtk_text_buffer_get_start_iter(buffer,&iter);
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, data, -1, "lmarg", "bg", NULL);
}
void unselectall()
{
char *data = GetText(view);
deleteall();
GtkTextIter iter;
gtk_text_buffer_get_start_iter(buffer,&iter);
gtk_text_buffer_insert(buffer, &iter, data, -1);
}
void indentall()
{
char *data = GetText(view);
int i;
GtkTextIter iter;
gtk_text_buffer_get_start_iter(buffer,&iter);
int counter = 0;
for (i = 0; i < strlen(data);i++)
{
if (data[i] == 10) counter++;
}
for (i = 0; i <= counter; i++)
{
gtk_text_iter_set_line(&iter,i);
gtk_text_buffer_insert(buffer, &iter, " ", -1);
}
}
void gotoline()
{
}
void textchange()
{
position = 0;
}
void clicked_radio1()
{
if (gtk_toggle_button_get_active (GTK_RADIO_BUTTON(findradio1))) position+=2;
}
void clicked_radio2()
{
if (gtk_toggle_button_get_active (GTK_RADIO_BUTTON(findradio2))) position-=2;
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Text editor");
int iwidth = gdk_screen_get_width(gdk_screen_get_default());
int iheight = gdk_screen_get_height(gdk_screen_get_default());
gtk_window_set_default_size(GTK_WINDOW(window), iwidth,iheight);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
view = gtk_text_view_new();
GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_fixed_put(GTK_FIXED(fixed), scrolled_window,0,50);
gtk_widget_set_size_request(scrolled_window,iwidth -50, iheight - 250);
gtk_container_add(GTK_CONTAINER(scrolled_window), view);
gtk_widget_set_size_request(view,iwidth -50, iheight - 250);
menubar = gtk_menu_bar_new();
gtk_fixed_put(GTK_FIXED(fixed), menubar,0,0);
// section menu
GtkWidget *firstmenu = gtk_menu_new();
GtkWidget *secondmenu = gtk_menu_new();
GtkWidget *thrimenu = gtk_menu_new();
GtkWidget *item_1 = gtk_menu_item_new_with_label("File");
GtkWidget *item_2 = gtk_menu_item_new_with_label("Open file");
GtkWidget *item_3 = gtk_menu_item_new_with_label("Save file");
GtkWidget *item_4 = gtk_menu_item_new_with_label("Exit");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item_1),firstmenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), item_1);
gtk_menu_shell_append(GTK_MENU_SHELL(firstmenu), item_2);
gtk_menu_shell_append(GTK_MENU_SHELL(firstmenu), item_3);
gtk_menu_shell_append(GTK_MENU_SHELL(firstmenu), item_4);
g_signal_connect(G_OBJECT(item_2), "activate", G_CALLBACK(opendialog),NULL);
g_signal_connect(G_OBJECT(item_3), "activate", G_CALLBACK(savedialog),NULL);
g_signal_connect(G_OBJECT(item_4), "activate",G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *sitem_1 = gtk_menu_item_new_with_label("Manipulations");
GtkWidget *sitem_2 = gtk_menu_item_new_with_label("Find");
GtkWidget *sitem_3 = gtk_menu_item_new_with_label("Time/Date");
GtkWidget *sitem_4 = gtk_menu_item_new_with_label("Delete All");
GtkWidget *sitem_5 = gtk_menu_item_new_with_label("Select All");
GtkWidget *sitem_6 = gtk_menu_item_new_with_label("Unselect All");
GtkWidget *sitem_7 = gtk_menu_item_new_with_label("Indent all");
GtkWidget *sitem_8 = gtk_menu_item_new_with_label("Goto line");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(sitem_1),secondmenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), sitem_1);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_2);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_3);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_4);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_5);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_6);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_7);
gtk_menu_shell_append(GTK_MENU_SHELL(secondmenu), sitem_8);
g_signal_connect(G_OBJECT(sitem_2), "activate", G_CALLBACK(openfinddialog),NULL);
g_signal_connect(G_OBJECT(sitem_3), "activate", G_CALLBACK(insertdate),NULL);
g_signal_connect(G_OBJECT(sitem_4), "activate", G_CALLBACK(deleteall),NULL);
g_signal_connect(G_OBJECT(sitem_5), "activate", G_CALLBACK(selectall),NULL);
g_signal_connect(G_OBJECT(sitem_6), "activate", G_CALLBACK(unselectall),NULL);
g_signal_connect(G_OBJECT(sitem_7), "activate", G_CALLBACK(indentall),NULL);
g_signal_connect(G_OBJECT(sitem_8), "activate", G_CALLBACK(gotoline),NULL);
GtkWidget *titem_1 = gtk_menu_item_new_with_label("Format");
GtkWidget *titem_2 = gtk_menu_item_new_with_label("Font");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(titem_1),thrimenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), titem_1);
gtk_menu_shell_append(GTK_MENU_SHELL(thrimenu), titem_2);
g_signal_connect(G_OBJECT(titem_2), "activate", G_CALLBACK(openfontdialog),NULL);
// section menu
g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
/// findwindow
findwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(findwindow),"Find text");
gtk_window_set_position(GTK_WINDOW(findwindow), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(findwindow), 200,200);
GtkWidget *findfixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(findwindow), findfixed);
GtkWidget *vbox = gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(findfixed), vbox);
gtk_fixed_put(GTK_FIXED(findfixed),vbox,0,25);
findview = gtk_text_view_new();
buffer = gtk_text_view_get_buffer(findview);
gtk_widget_set_size_request(findview,200,25);
gtk_box_pack_start(GTK_BOX(vbox), findview, FALSE, FALSE, 10);
GtkWidget *findbutton = gtk_button_new_with_label("Find text");
gtk_widget_set_size_request(findbutton,200,25);
gtk_box_pack_start(GTK_BOX(vbox), findbutton, FALSE, FALSE, 0);
findradio1 = gtk_radio_button_new_with_label(NULL,"forward");
gtk_box_pack_start(GTK_BOX(vbox),findradio1, FALSE, FALSE, 10);
findradio2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (findradio1),"backward");
gtk_box_pack_start(GTK_BOX(vbox),findradio2, FALSE, FALSE, 0);
g_signal_connect(G_OBJECT(findbutton), "clicked", G_CALLBACK(gettextdialog),findwindow);
g_signal_connect(G_OBJECT(buffer), "changed", G_CALLBACK(textchange),findwindow);
g_signal_connect(G_OBJECT(findradio1), "clicked", G_CALLBACK(clicked_radio1),findradio1);
g_signal_connect(G_OBJECT(findradio2), "clicked", G_CALLBACK(clicked_radio2),findradio2);
gtk_main();
return 0;
}
|