/*
The actions.cpp file is to define the interaction action funcitons when the user does something.
*/
//includes for prototypes
#include "actions.h"
//double checks to makes sure the user didn't forget to save
void save_checker(int key)
{
g_print("Checking for none saved files...\n");
//for when the tab is closed by it's lonesome
if (key!=0)
{
if (gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(tab[key].buffer)))
{
g_print("Running Save Dialog...\n");
do
{
//holds the file selection widget proccess (file_sel)
gtk_dialog_run(GTK_DIALOG(save_check));
}
while (GTK_WIDGET_VISIBLE(save_check));
}
//since it's only one tab, we can return
return;
}
//goes through all the tabs to make sure their saved
for (int i=0; i<maxtabs && tmpall!=1 && canpass!=1;)
{
g_print("Checking Page...\n");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), i);
if (gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(tab[i+1].buffer)))
{
g_print("Running Save Dialog...\n");
do
{
//holds the file selection widget proccess (file_sel)
gtk_dialog_run(GTK_DIALOG(save_check));
}
while (GTK_WIDGET_VISIBLE(save_check) && tmpall!=1 && canpass!=1);
g_print("Save Dialog Closed\n");
}
i++;
}
//reset tmp holder
tmpall=0;
return;
}
//main quit app
gint destroyapp(GtkWidget *widget, gpointer gdata)
{
save_checker(0);
//checks to see if cancled was not clicked
if (canpass==0)
{
g_print("Quitting...\n");
gtk_main_quit();
}
else
{
canpass=0;
return 1;
}
return 0;
}
//file paramters checker
gint fileprmp()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
g_print("Checking Paramters...\n");
if (fileprmt)
{
g_print("Paramter Found\n");
g_print("Opening Paramter File...\n");
tab[curtab].filename=fileprmt;
gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), tab[curtab].scroller, gtk_label_new(g_path_get_basename(tab[curtab].filename)));
g_file_get_contents(tab[curtab].filename, &tab[curtab].contents, &tab[curtab].length, &tab[curtab].err);
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tab[curtab].buffer), tab[curtab].contents, -1);
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tab[curtab].buffer), FALSE);
g_print("Paramter File Opened\n");
}
else
{
g_print("No Paramter Found\n");
}
g_print("Paramter Checking Done\n");
return 0;
}
//To close the save_check, when a button is selected.
gint check_x()
{
//hide the widget for later open actions
g_print("Checking for Save_Check...\n");
if (GTK_WIDGET_VISIBLE(save_check))
{
g_print("Save_Check Visable\n");
gtk_widget_hide(save_check);
g_print("Save_Check Hidden\n");
}
return 0;
}
//event when "Canceled" button is clicked on the file selection widget
gint file_sel_x(GtkWidget *w, GtkFileSelection *fs)
{
//hide the widget for later open actions
gtk_widget_hide(filesel);
g_print("Hid File Selection\n");
//close the tab that was created
if (openpass==1)
{
on_tab_x();
openpass=0;
}
return 0;
}
//event when "Ok" button is clicked on the file selection widget
gint file_sel(GtkWidget *w, GtkFileSelection *fs)
{
//get filename from slection widget and hide the widget for later open actions
g_print("Retriving File Information...\n");
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
tab[curtab].filename=(gchar*)gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs));
gtk_widget_hide(filesel);
gtk_notebook_set_tab_label(GTK_NOTEBOOK(notebook), tab[curtab].scroller, gtk_label_new(g_path_get_basename(tab[curtab].filename)));
return 0;
}
//new event
gint on_new()
{
if (tabcount==maxtabs+1)
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), tabcount-2);
return 1;
}
g_print("Opening New Tab...\n");
newup=1;
tab[tabcount].buffer=gtk_source_buffer_new(NULL);
tab[tabcount].textview=gtk_source_view_new_with_buffer(tab[tabcount].buffer);
tab[tabcount].scroller=gtk_scrolled_window_new(NULL, NULL);
gtk_source_buffer_begin_not_undoable_action(tab[tabcount].buffer);
gtk_source_buffer_end_not_undoable_action(tab[tabcount].buffer);
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tab[tabcount].buffer), FALSE);
//syntax highlighting
on_syntax(tabcount);
//sets scrollers and editability for text_view
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(tab[tabcount].scroller), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(tab[tabcount].scroller), GTK_SHADOW_IN);
gtk_text_view_set_editable(GTK_TEXT_VIEW(tab[tabcount].textview), TRUE);
gtk_container_add(GTK_CONTAINER(tab[tabcount].scroller), tab[tabcount].textview);
//checks for first time for plus
if (tabcount==0)
{
gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), tab[tabcount].scroller, gtk_label_new("+"), maxtabs);
newup=1;
}
else
{
gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), tab[tabcount].scroller, gtk_label_new("..."), tabcount-1);
}
//programming environment
gtk_source_view_set_auto_indent(GTK_SOURCE_VIEW(tab[tabcount].textview), true);
gtk_source_view_set_highlight_current_line(GTK_SOURCE_VIEW(tab[tabcount].textview), true);
//show
gtk_widget_show(tab[tabcount].scroller);
gtk_widget_show(tab[tabcount].textview);
on_linen();
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), tabcount-1);
//switch to keep from looping
if (tabcount==0)
{
newup=1;
}
tabcount++;
return 0;
}
//function to check if the plus tab was clicked
gint on_tab_plus()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
if (curtab==tabcount && newup!=1)
{
on_new();
}
newup=0;
return 0;
}
//open file event
gint on_open()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks to see if the content has already been edited
g_print("Checking Modified Buffer...\n");
if (gtk_text_buffer_get_char_count(GTK_TEXT_BUFFER(tab[curtab].buffer)))
{
if (on_new()==1)
{
return 0;
}
}
openpass=1;
//checks if a file was previously opened and sets the default directory to it's location
g_print("Retriving Old Path...\n");
if (tab[curtab].filename)
{
gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), tab[curtab].filename);
}
//loops and waits for the widget to close
g_print("Starting File Selection...\n");
do
{
//holds the file selection widget proccess (file_sel)
gtk_dialog_run(GTK_DIALOG(filesel));
}
while (GTK_WIDGET_VISIBLE(filesel));
g_print("File Selection Done\n");
//get file information and set file contents to the buffer
g_file_get_contents(tab[curtab].filename, &tab[curtab].contents, &tab[curtab].length, &tab[curtab].err);
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tab[curtab].buffer), tab[curtab].contents, -1);
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tab[curtab].buffer), FALSE);
gtk_source_buffer_begin_not_undoable_action(tab[tabcount].buffer);
gtk_source_buffer_end_not_undoable_action(tab[tabcount].buffer);
//syntax highlighting
on_syntax(tabcount);
g_print("Wrote Content\n");
return 0;
}
//save file event
gint on_save()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks for save_check
check_x();
//checks to see if the filename isn't specified and opens the file selection process
g_print("Checking Old Filename...\n");
if (!tab[curtab].filename)
{
g_print("Old Path Not Found\n");
//loops and waits for the widget to close
g_print("Starting File Selection...\n");
do
{
//holds the file selection widget proccess (file_sel)
gtk_dialog_run(GTK_DIALOG(filesel));
}
while (GTK_WIDGET_VISIBLE(filesel));
g_print("File Selection Done\n");
//checks to see if the Canceled button was pressed instead of ok
if (!tab[curtab].filename)
{
return 0;
}
}
//delcare some temp vars
g_print("Retriving File...\n");
gchar *text;
gsize rbytes, wbytes;
GError *err = NULL;
FILE *fp;
//finds the iters
gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tab[curtab].buffer), &start);
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tab[curtab].buffer), &end);
//temp stores the content of the buffer
text=gtk_text_buffer_get_text(GTK_TEXT_BUFFER(tab[curtab].buffer), &start, &end, FALSE);
//converts the content to UTF-8
text=g_convert(text, -1, &tab[curtab].charset, "UTF-8", &rbytes, &wbytes, &err);
//open the file to be stored and puts the content in
fp=fopen(tab[curtab].filename, "w");
fputs(text, fp);
//set variations off, close the file, free up room
gtk_text_buffer_set_modified(GTK_TEXT_BUFFER(tab[curtab].buffer), FALSE);
fclose(fp);
g_free(text);
//syntax_open();
return 0;
}
//save as event
gint on_save_as()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks for save_check
check_x();
//checks if a file was previously opened and sets the default directory to it's location
g_print("Checking Old Path...\n");
if (tab[curtab].filename)
{
g_print("Defining Old File Path...\n");
gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel), tab[curtab].filename);
}
//loops and waits for the widget to close
g_print("Running File Selection...\n");
do
{
//holds the file selection widget proccess (file_sel)
gtk_dialog_run(GTK_DIALOG(filesel));
}
while (GTK_WIDGET_VISIBLE(filesel));
g_print("File Selection Done\n");
//checks to see if the Canceled button was pressed instead of ok
if (!tab[curtab].filename)
{
return 0;
}
//triggers the save event
on_save();
return 0;
}
//event when "Don't Save Any" button is clicked on the save_check
gint on_save_none()
{
//set to tell that this button was clicked
tmpall=1;
//hide the widget for later open actions
if (GTK_WIDGET_VISIBLE(save_check))
{
gtk_widget_hide(save_check);
g_print("Save_Check Hidden\n");
}
return 0;
}
//event when "Save All" button is clicked on the save_check
gint on_save_all()
{
//checks for save_check
check_x();
//goes through all the tabs to sve
for (int i=0; i<maxtabs;)
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), i);
if (gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(tab[i].buffer)))
{
on_save();
}
i++;
}
//destroy app
g_print("Quitting...\n");
gtk_main_quit();
return 0;
}
//Save All for menu
gint on_save_all_menu()
{
//goes through all the tabs to sve
for (int i=0; i<maxtabs;)
{
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), i);
if (gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(tab[i].buffer)))
{
on_save();
}
i++;
}
return 0;
}
//event when "Cancel" button is clicked on the save_check
gint on_save_cancel()
{
//sets the passing variable and returns
g_print("Canceling Quit");
//hide the widget for later open actions
if (GTK_WIDGET_VISIBLE(save_check))
{
gtk_widget_hide(save_check);
g_print("Save_Check Hidden\n");
}
canpass=1;
return 0;
}
//close tab event
gint on_tab_x()
{
curtab=gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
save_checker(curtab+1);
//checks for cancle
if (canpass!=1)
{
gtk_notebook_remove_page(GTK_NOTEBOOK(notebook), curtab);
g_print("Page Removed\n");
//moves all the tab data to match up
g_print("Moving Tab Data...\n");
for (int i=curtab; i<maxtabs-1;)
{
tab[i]=tab[i+1];
i++;
}
tabcount--;
//checks to see if all that is left is the plus
if (tabcount==1)
{
on_new();
}
//checks to see if the last tab before the plus was closed
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
if (curtab==tabcount)
{
on_prevp();
}
}
else
{
canpass=0;
}
return 0;
}
//search box event
gint on_search()
{
//loops and waits for the widget to close
g_print("Starting Search Box...\n");
do
{
//holds the file selection widget proccess (search_box)
gtk_dialog_run(GTK_DIALOG(search_box));
}
while (GTK_WIDGET_VISIBLE(search_box));
g_print("Search Box Done\n");
return 0;
}
//copy event
gint on_copy()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(tab[curtab].buffer), gtk_clipboard_get(GDK_NONE));
g_print("Copied\n");
return 0;
}
//paste event
gint on_paste()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(tab[curtab].buffer), gtk_clipboard_get(GDK_NONE), NULL, TRUE);
g_print("Pasted\n");
return 0;
}
//cut event
gint on_cut()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(tab[curtab].buffer), gtk_clipboard_get(GDK_NONE), TRUE);
g_print("Cut\n");
return 0;
}
//delete event
gint on_del()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
gtk_text_buffer_delete_selection(GTK_TEXT_BUFFER(tab[curtab].buffer), TRUE, TRUE);
g_print("Deleted\n");
return 0;
}
//select all event
gint on_sel_all()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//finds iters
gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tab[curtab].buffer), &start);
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(tab[curtab].buffer), &end);
//place the cursor at the end
gtk_text_buffer_place_cursor(GTK_TEXT_BUFFER(tab[curtab].buffer), &end);
//moves the selection bound from the end through the start
gtk_text_buffer_move_mark_by_name(GTK_TEXT_BUFFER(tab[curtab].buffer), "selection_bound", &start);
g_print("Selected All\n");
return 0;
}
//undo event
gint on_undo()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//undoes
gtk_source_buffer_undo(tab[curtab].buffer);
return 0;
}
//uredo event
gint on_redo()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//redoes
gtk_source_buffer_redo(tab[curtab].buffer);
return 0;
}
//next tab
gint on_nextp()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks to make sure the plus wasn't hit
if (curtab==tabcount-1)
{
gtk_notebook_set_page(GTK_NOTEBOOK(notebook), 0);
}
else
{
gtk_notebook_next_page(GTK_NOTEBOOK(notebook));
}
g_print("Next Tab\n");
return 0;
}
//previous tab
gint on_prevp()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks to see if it's at the begining
if (curtab==1)
{
gtk_notebook_set_page(GTK_NOTEBOOK(notebook), tabcount-2);
}
else
{
gtk_notebook_prev_page(GTK_NOTEBOOK(notebook));
}
g_print("Previous Tab\n");
return 0;
}
//fullscreen event switch
gint on_fullscn()
{
//temp vars
GtkItemFactory *ifactory;
gboolean state;
//checks for the checkmark sets the wordwrap accordingly
ifactory=gtk_item_factory_from_widget(menubar);
state=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(ifactory, "/View/Full Screen")));
if (!state)
{
gtk_window_unfullscreen(GTK_WINDOW(window));
}
else
{
gtk_window_fullscreen(GTK_WINDOW(window));
}
return 0;
}
//word wrap check box trigger
gint on_wrap()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//temp vars
GtkItemFactory *ifactory;
gboolean state;
//checks for the checkmark sets the wordwrap accordingly
ifactory=gtk_item_factory_from_widget(menubar);
state=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(ifactory, "/View/Word Wrap")));
for (int i=0; i<maxtabs;)
{
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(tab[i+1].textview), state ? GTK_WRAP_WORD : GTK_WRAP_NONE);
i++;
}
g_print("Word Wrap Switched\n");
return 0;
}
//line number check box trigger
gint on_linen()
{
curtab=1+gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
//checks for the checkmark sets the line numbering accordingly
ifactory=gtk_item_factory_from_widget(menubar);
state=gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(ifactory, "/View/Line Numbers")));
for (int i=0; i<maxtabs;)
{
gtk_source_view_set_show_line_numbers(GTK_SOURCE_VIEW(tab[i+1].textview), state);
//gtk_source_view_set_show_line_marks(GTK_SOURCE_VIEW(tab[i+1].textview), state);
i++;
}
g_print("Line Numbering Switched\n");
return 0;
}
//about diolog event
gint on_about()
{
//holds the information needed
g_print("Defining About Information...\n");
const gchar *name="Devchekio";
const gchar *version="v0.9.5";
const GdkPixbuf *logo=gdk_pixbuf_new_from_file("images/icon_256.png", tmppixbuf);
const gchar *website="http://www.blahertech.org/projects/devchekio/";
const gchar *copyright="Copyright Blahertech 2007-2008 Benjamin Jay Young";
const gchar *comments="GTK+ Check in/out code editor";
//authors
const gchar *authors[]=
{
"Ben Young (Project Leader) <Blaher>",
"Zach Serafini (Developer)",
"Harry Rickards (Linux Packages) <L33tmyst>",
"Thulasi R (Mac OSX Porter) <Travinan>",
NULL
};
//artists
const gchar *artists[]=
{
"James Nicholls (Logo Designer) <Niknaks1993>",
NULL
};
//sends the info to setup the about diolog box widget
g_print("Showing About Dialog...\n");
gtk_show_about_dialog(GTK_WINDOW(window),
"name", name,
"version", version,
"logo", logo,
"website", website,
"copyright", copyright,
"comments", comments,
"authors", authors,
"artists", artists,
NULL);
return 0;
}