Commit 7d8b28dc by Castro

töötav otsingu bar

parent 0b24014e
...@@ -17,6 +17,7 @@ typedef struct ...@@ -17,6 +17,7 @@ typedef struct
GtkTextView *Puhver; // Vahekaardi tekstipuhver GtkTextView *Puhver; // Vahekaardi tekstipuhver
GtkWidget *ScrollWindow; // Tekstiakna scroll GtkWidget *ScrollWindow; // Tekstiakna scroll
GtkWidget *StatusBar; // Status bar GtkWidget *StatusBar; // Status bar
GtkWidget *search_entry;
} tab; } tab;
typedef struct typedef struct
......
#include <gtk/gtk.h> #include "search.h"
/* TODO void find (GtkTextView *text_view, const gchar *text, GtkTextIter *iter)
Failide avamine
Failide salvestamine
*/
#ifdef SOURCEVIEW
#include <gtksourceview/gtksource.h>
#endif
#ifdef SOURCEVIEW
#define PEALKIRI "CEditor SourceView"
#else
#define PEALKIRI "CEditor TextView"
#endif
//struckt Otsi_akna jaoks
typedef struct _App
{
GtkWidget *text_view;
GtkWidget *search_entry;
} App;
// Uuenda näidatud rida ja veergu kursori asukoha järgi
void UuendaRidaVeerg(GtkTextBuffer *Puhver, GtkStatusbar *StatusBar)
{ {
GtkTextIter Iter; GtkTextIter mstart, mend;
// Leia kursori asukoht GtkTextBuffer *buffer;
gtk_text_buffer_get_iter_at_mark(Puhver, &Iter, gtk_text_buffer_get_insert(Puhver)); gboolean found;
// 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); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
} found = gtk_text_iter_forward_search (iter, text, 0, &mstart, &mend, NULL);
// Kutsutakse copy/paste lohistamise korral, praegu lihtsalt uuendab rida/veergu if (found)
void TekstMarkSet(GtkTextBuffer *Puhver, const GtkTextIter *new_location, GtkTextMark *Mark, gpointer Data) {
{ gtk_text_buffer_select_range (buffer, &mstart, &mend);
UuendaRidaVeerg(Puhver, GTK_STATUSBAR(Data)); gtk_text_buffer_create_mark (buffer, "last_pos", &mend, FALSE);
}
} }
// funktsioon selleks kui search nuppu vajutatakse // funktsioon selleks kui search nuppu vajutatakse
void Otsi_nupp_vajutatud (GtkWidget *Otsi_nupp, App *app) void Otsi_nupp_vajutatud (GtkWidget *search_button, tab *Tab)
{ {
const gchar *text; const gchar *text;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
GtkTextIter iter; GtkTextIter iter;
GtkTextIter mstart, mend;
gboolean found;
text = gtk_entry_get_text (GTK_ENTRY (app->search_entry)); text = gtk_entry_get_text (GTK_ENTRY (Tab->search_entry));
/* Get the buffer associated with the text view widget. */ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (Tab->TextView));
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->text_view));
/* Search from the start from buffer for text. */
gtk_text_buffer_get_start_iter (buffer, &iter); gtk_text_buffer_get_start_iter (buffer, &iter);
found = gtk_text_iter_forward_search (&iter, text, 0, &mstart, &mend, NULL);
if (found) find (GTK_TEXT_VIEW (Tab->TextView), text, &iter);
{
/* If found, hilight the text. */
gtk_text_buffer_select_range (buffer, &mstart, &mend);
}
} }
// funktsioon selleks kui jargmine_nupp vajutatakse
int main(int argc, char **argv) void Jargmine_nupp_vajutatud (GtkWidget *next_button, tab *Tab)
{ {
gtk_init(&argc, &argv); const gchar *text;
GtkTextBuffer *buffer;
// Aken GtkTextMark *last_pos;
GtkWidget *Aken = gtk_window_new(GTK_WINDOW_TOPLEVEL); // Loo aken GtkTextIter iter;
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);
// Box
GtkWidget *Box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); // Vertikaalne box, 0px vahe
gtk_container_add(GTK_CONTAINER(Aken), Box); // Pane box aknasse
// 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
// Search
GtkWidget *Otsi_nupp = gtk_button_new_with_label ("Otsi"); // Otsi nupp
GtkWidget *Otsi_aken = gtk_scrolled_window_new (NULL, NULL); // Otsi aken
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (Otsi_aken),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
App app;
gtk_box_pack_start (GTK_BOX (Box), Otsi_nupp, FALSE, FALSE, 0);
app.search_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (Box), app.search_entry, TRUE, TRUE, 0);
g_signal_connect (G_OBJECT (Otsi_nupp), "clicked",
G_CALLBACK (Otsi_nupp_vajutatud), &app);
// 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
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuSalvesta); // Fail->Salvesta
gtk_menu_shell_append(GTK_MENU_SHELL(FailMenu), FailMenuSalvestaN); // Fail->Salvesta nimega
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
gtk_box_pack_start(GTK_BOX(Box), MenuBar, FALSE, FALSE, 0);
// paneb Otsi_aken boxi
gtk_box_pack_start (GTK_BOX (Box), Otsi_aken, TRUE, TRUE, 0);
app.text_view = gtk_text_view_new ();
gtk_container_add (GTK_CONTAINER (Otsi_aken), app.text_view);
// Tekstiaken text = gtk_entry_get_text (GTK_ENTRY (Tab->search_entry));
#ifdef SOURCEVIEW
GtkWidget *View = gtk_source_view_new(); // Loo tekstiaken
#else
GtkWidget *View = gtk_text_view_new(); // Loo tekstiaken
#endif
gtk_box_pack_start(GTK_BOX(Box), View, TRUE, TRUE, 0); // Pane tekstiaken box'i
// Teksti puhver buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (Tab->TextView));
GtkTextBuffer *Puhver = gtk_text_view_get_buffer(GTK_TEXT_VIEW(View)); // Tekstiakna teksti puhver
// Status bar last_pos = gtk_text_buffer_get_mark (buffer, "last_pos");
GtkWidget *StatusBar = gtk_statusbar_new(); // Loo status bar if (last_pos == NULL)
gtk_box_pack_start(GTK_BOX(Box), StatusBar, FALSE, FALSE, 0); // Pane statusbar box'i return;
// 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 gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos);
UuendaRidaVeerg(Puhver, GTK_STATUSBAR(StatusBar)); // Esimene uuendus find (GTK_TEXT_VIEW (Tab->TextView), text, &iter);
}
gtk_main();
return 0;
}
#ifndef SEARCH_H
#define SEARCH_H
#include "ceditor.h"
#ifdef SOURCEVIEW
#include <gtksourceview/gtksource.h>
#endif
void find (GtkTextView *text_view, const gchar *text, GtkTextIter *iter);
void Otsi_nupp_vajutatud (GtkWidget *search_button, tab *app);
void Jargmine_nupp_vajutatud (GtkWidget *next_button, tab *app);
#endif // SEARCH_H
...@@ -27,12 +27,34 @@ void LisaTab(ui *UI) ...@@ -27,12 +27,34 @@ void LisaTab(ui *UI)
#endif #endif
Tab->Puhver = gtk_text_view_get_buffer(GTK_TEXT_VIEW(Tab->TextView)); // Tekstiakna teksti puhver Tab->Puhver = gtk_text_view_get_buffer(GTK_TEXT_VIEW(Tab->TextView)); // Tekstiakna teksti puhver
// Otsing
GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
gtk_container_add (GTK_CONTAINER (Tab->Aken), vbox);
GtkWidget *hbox = gtk_hbox_new (FALSE, 2);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
Tab->search_entry = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (hbox), Tab->search_entry, TRUE, TRUE, 0);
GtkWidget *Otsi_nupp = gtk_button_new_with_label ("Otsi"); // Otsi nupp
gtk_box_pack_start (GTK_BOX (hbox), Otsi_nupp, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (Otsi_nupp), "clicked",
G_CALLBACK (Otsi_nupp_vajutatud), &UI->Tabid[TabidN]);
GtkWidget *Jargmine_nupp = gtk_button_new_with_label ("Järgmine"); // Järmine nupp
gtk_box_pack_start (GTK_BOX (hbox), Jargmine_nupp, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (Jargmine_nupp), "clicked",
G_CALLBACK (Jargmine_nupp_vajutatud), &UI->Tabid[TabidN]);
// Scroll window // Scroll window
Tab->ScrollWindow = gtk_scrolled_window_new(NULL, NULL); // Loo scroll aken Tab->ScrollWindow = gtk_scrolled_window_new(NULL, NULL); // Loo scroll aken
gtk_container_add(GTK_CONTAINER(Tab->ScrollWindow), Tab->TextView); // Scroll aknasse läheb tekstiaken gtk_container_add(GTK_CONTAINER(Tab->ScrollWindow), Tab->TextView); // Scroll aknasse läheb tekstiaken
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Tab->ScrollWindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // Konfigureerimine gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Tab->ScrollWindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); // Konfigureerimine
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(Tab->ScrollWindow), GTK_SHADOW_ETCHED_IN); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(Tab->ScrollWindow), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start (GTK_BOX (vbox), Tab->ScrollWindow, TRUE, TRUE, 0);
// Status bar // Status bar
Tab->StatusBar = gtk_statusbar_new(); Tab->StatusBar = gtk_statusbar_new();
// Teksti muutuse callback, uuendab rida/veergu // Teksti muutuse callback, uuendab rida/veergu
...@@ -42,7 +64,7 @@ void LisaTab(ui *UI) ...@@ -42,7 +64,7 @@ void LisaTab(ui *UI)
// Box // Box
Tab->Box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); // Vertikaalne box, 0px vahe Tab->Box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); // Vertikaalne box, 0px vahe
gtk_box_pack_start(GTK_BOX(Tab->Box), Tab->ScrollWindow, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(Tab->Box), vbox, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(Tab->Box), Tab->StatusBar, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(Tab->Box), Tab->StatusBar, FALSE, FALSE, 0);
// Pealkiri // Pealkiri
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "ceditor.h" #include "ceditor.h"
#include "statusbar.h" #include "statusbar.h"
#include "textview.h" #include "textview.h"
#include "search.h"
void LisaTab(ui *UI); void LisaTab(ui *UI);
void KustutaTab(ui *UI, int Indeks); void KustutaTab(ui *UI, int Indeks);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "ceditor.h" #include "ceditor.h"
#include "tab.h" #include "tab.h"
#include "menubar.h" #include "menubar.h"
#include "search.h"
#ifdef SOURCEVIEW #ifdef SOURCEVIEW
#define PEALKIRI "CEditor SourceView" #define PEALKIRI "CEditor SourceView"
......
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