Commit d0313963 by karudu

Failide salvestamine

parent 76a271c2
Showing with 34 additions and 3 deletions
......@@ -4,7 +4,7 @@
#endif
/* TODO
Failide salvestamine
Uus fail, nimeta salvestamine
Scrollbar
*/
......@@ -39,6 +39,7 @@ void TekstMarkSet(GtkTextBuffer *Puhver, const GtkTextIter *new_location, GtkTex
void AvaFail(GtkWidget *Nupp, gpointer Data)
{
GtkTextBuffer *Puhver = GTK_TEXT_BUFFER(Data);
GtkWidget *Dialog = gtk_file_chooser_dialog_new("Ava fail", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
"Tühista", GTK_RESPONSE_CANCEL,
"Ava", GTK_RESPONSE_ACCEPT, NULL);
......@@ -47,13 +48,13 @@ void AvaFail(GtkWidget *Nupp, gpointer Data)
{
char *Nimi = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(Dialog));
gchar *Sisu;
GError *Viga;
GError *Viga = NULL;
// TODO paremad veateated
if(g_file_get_contents(Nimi, &Sisu, NULL, &Viga))
{
if(g_utf8_validate(Sisu, -1, NULL))
{
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(Data), Sisu, -1);
gtk_text_buffer_set_text(Puhver, Sisu, -1);
} else printf("g_utf8_validate viga\n");
g_free(Sisu);
}
......@@ -67,6 +68,35 @@ void AvaFail(GtkWidget *Nupp, gpointer Data)
gtk_widget_destroy(Dialog);
}
void SalvestaFail(GtkWidget *Nupp, gpointer Data)
{
GtkTextBuffer *Puhver = GTK_TEXT_BUFFER(Data);
GtkWidget *Dialog = gtk_file_chooser_dialog_new("Salvesta fail", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
"Tühista", GTK_RESPONSE_CANCEL,
"Salvesta", GTK_RESPONSE_ACCEPT, NULL);
gint Vastus = gtk_dialog_run(GTK_DIALOG(Dialog));
if(Vastus == GTK_RESPONSE_ACCEPT)
{
char *Nimi = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(Dialog));
gchar *Sisu;
GtkTextIter TekstA, TekstL;
gtk_text_buffer_get_start_iter(Puhver, &TekstA);
gtk_text_buffer_get_end_iter(Puhver, &TekstL);
Sisu = gtk_text_buffer_get_text(Puhver, &TekstA, &TekstL, TRUE);
gtk_text_buffer_set_modified(Puhver, FALSE);
GError *Viga = NULL;
if(g_file_set_contents(Nimi, Sisu, -1, &Viga) == FALSE)
{
printf("Viga faili salvestamisel: %s\n", Viga->message);
g_error_free(Viga);
}
g_free(Nimi);
g_free(Sisu);
}
gtk_widget_destroy(Dialog);
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
......@@ -114,6 +144,7 @@ int main(int argc, char **argv)
g_signal_connect(FailMenuAva, "activate", G_CALLBACK(AvaFail), Puhver);
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuSalvesta); // Fail->Salvesta
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuSalvestaN); // Fail->Salvesta nimega
g_signal_connect(FailMenuSalvestaN, "activate", G_CALLBACK(SalvestaFail), Puhver);
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), gtk_separator_menu_item_new());
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuValju); // Fail->Välju
g_signal_connect(FailMenuValju, "activate", G_CALLBACK(gtk_main_quit), NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment