Commit eeb23266 by hounas

Teine commit. 04.03

parent c2247903
Showing with 183 additions and 0 deletions
#include <gtk/gtk.h>
/* TODO
Failide avamine
Failide salvestamine
*/
#ifdef SOURCEVIEW
#include <gtksourceview/gtksource.h>
#endif
#ifdef SOURCEVIEW
#define PEALKIRI "CEditor SourceView"
#else
#define PEALKIRI "CEditor TextView"
#endif
void AvaFail(GtkWidget *Nupp, gpointer 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);
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;
GError *Viga;
// 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);
} else printf("g_utf8_validate viga\n");
g_free(Sisu);
}
else
{
printf("Viga faili avamisel: %s\n", Viga->message);
g_error_free(Viga);
}
g_free(Nimi);
}
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);
}
// Uuenda näidatud rida ja veergu kursori asukoha järgi
void UuendaRidaVeerg(GtkTextBuffer *Puhver, GtkStatusbar *StatusBar)
{
GtkTextIter Iter;
// Leia kursori asukoht
gtk_text_buffer_get_iter_at_mark(Puhver, &Iter, gtk_text_buffer_get_insert(Puhver));
// Võta selle rida ja veerg
gint Rida = gtk_text_iter_get_line(&Iter);
gint Veerg = gtk_text_iter_get_line_offset(&Iter);
gtk_statusbar_pop(StatusBar, 0); // Võta eelmine tekst ära
gchar *Tekst = g_strdup_printf("%d:%d", Rida + 1, Veerg + 1); // Loo string rea ja veeruga
gtk_statusbar_push(StatusBar, 0, Tekst); // Näita teksti
g_free(Tekst);
}
// Kutsutakse copy/paste lohistamise korral, praegu lihtsalt uuendab rida/veergu
void TekstMarkSet(GtkTextBuffer *Puhver, const GtkTextIter *new_location, GtkTextMark *Mark, gpointer Data)
{
UuendaRidaVeerg(Puhver, GTK_STATUSBAR(Data));
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
// Aken
GtkWidget *Aken = gtk_window_new(GTK_WINDOW_TOPLEVEL); // Loo aken
gtk_window_set_position(GTK_WINDOW(Aken), GTK_WIN_POS_CENTER); // Aken ekraani keskel
gtk_window_set_default_size(GTK_WINDOW(Aken), 800, 600); // Akna suurus
gtk_window_set_title(GTK_WINDOW(Aken), PEALKIRI); // Pealkiri
// Close nupu callback
g_signal_connect_swapped(G_OBJECT(Aken), "destroy", G_CALLBACK(gtk_main_quit), NULL);
// Tekstiaken
#ifdef SOURCEVIEW
GtkWidget *View = gtk_source_view_new(); // Loo tekstiaken
#else
GtkWidget *View = gtk_text_view_new(); // Loo tekstiaken
#endif
// Teksti puhver
GtkTextBuffer *Puhver = gtk_text_view_get_buffer(GTK_TEXT_VIEW(View)); // Tekstiakna teksti puhver
// Menu bar
GtkWidget *MenuBar = gtk_menu_bar_new(); // Loo menu bar
GtkWidget *FailMenuNupp = gtk_menu_item_new_with_label("Fail"); // Fail
GtkWidget *AboutMenuNupp = gtk_menu_item_new_with_label("About"); // About
GtkWidget *FailMenu = gtk_menu_new(); // Fail menüü
GtkWidget *FailMenuUus = gtk_menu_item_new_with_label("Uus"); // Fail->Uus
GtkWidget *FailMenuAva = gtk_menu_item_new_with_label("Ava"); // Fail->Ava
GtkWidget *FailMenuSalvesta = gtk_menu_item_new_with_label("Salvesta"); // Fail->Salvesta
GtkWidget *FailMenuSalvestaN = gtk_menu_item_new_with_label("Salvesta nimega"); // Fail->Salvesta nimega
GtkWidget *FailMenuValju = gtk_menu_item_new_with_label("Välju"); // Fail->Välju
// Menüü nupud menu bar'i
gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), FailMenuNupp);
gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), AboutMenuNupp);
// Menüüd nuppudesse
gtk_menu_item_set_submenu(GTK_MENU_ITEM(FailMenuNupp), FailMenu);
// Fail menüü
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuUus); // Fail->Uus
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuAva); // Fail->Ava
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(G_OBJECT(FailMenuValju), "activate", G_CALLBACK(gtk_main_quit), NULL);
// Pane menubar box'i
GtkWidget *ScrollWindow = gtk_scrolled_window_new(NULL, NULL); //tekitame scrollitava akna
gtk_container_add(GTK_CONTAINER(ScrollWindow), View); //paigutame tesktiakna scrollitavasse aknasse
//standard scrollbar asjad
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ScrollWindow),
GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ScrollWindow),
GTK_SHADOW_ETCHED_IN);
// Box
GtkWidget *Box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); // Vertikaalne box, 0px vahe
gtk_box_pack_start(GTK_BOX(Box), MenuBar, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(Box), ScrollWindow, TRUE, TRUE, 0); // Pane tekstiaken box'i
gtk_container_add(GTK_CONTAINER(Aken), Box); //Pane box aknasse
// Status bar
GtkWidget *StatusBar = gtk_statusbar_new(); // Loo status bar
gtk_box_pack_start(GTK_BOX(Box), StatusBar, FALSE, FALSE, 0); // Pane statusbar box'i
// Teksti muutuse callback, uuendab rida/veergu
g_signal_connect(Puhver, "changed", G_CALLBACK(UuendaRidaVeerg), StatusBar);
// Copy/paste lohistamise callback, uuendab samuti
g_signal_connect_object(Puhver, "mark_set", G_CALLBACK(TekstMarkSet), StatusBar, 0);
gtk_widget_show_all(Aken); // Näita akent
UuendaRidaVeerg(Puhver, GTK_STATUSBAR(StatusBar)); // Esimene uuendus
gtk_main();
return 0;
}
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