GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Apr 24, 2014 4:07 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: A mini project : dictionary with Btree
PostPosted: Fri Mar 20, 2009 6:19 pm 
Offline
Familiar Face

Joined: Fri Mar 06, 2009 4:57 pm
Posts: 8
I've done a dict program (not complete, code only).
It's very simple.

http://www.mediafire.com/?l0mywjyygyz

theme.xml:
[code=]
<!--
Generated with glade3 3.4.5 on Tue Mar 17 02:41:48 2009
-->

<interface>

<object class="GtkWindow" id="main_window">
<property name="width_request">600</property>
<property name="height_request">400</property>
<property comments="Simple Dictionary Created By Luckymancvp" name="title" translatable="yes">Simple Dictionary Created By Luckymancvp</property>
<property name="modal">True</property>
<property name="window_position">GTK_WIN_POS_CENTER</property>
<property name="icon">116590553.png</property>

<child>

<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="spacing">10</property>

<child>

<object class="GtkHBox" id="hbox2">
<property name="visible">True</property>

<child>
<placeholder/>
</child>

<child>

<object class="GtkEntry" id="entry_word">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>

<packing>
<property name="position">1</property>
</packing>
</child>

<child>

<object class="GtkButton" id="button_add">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">gtk-add</property>
<property name="use_stock">True</property>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>

<child>

<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="spacing">10</property>

<child>

<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>

<child>

<object class="GtkButton" id="button_edit">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="tooltip-text" translatable="yes">Edit this word's meaning</property>
<property name="label" translatable="yes">gtk-edit</property>
<property name="use_stock">True</property>
</object>

<packing>
<property name="expand">False</property>
</packing>
</child>

<child>

<object class="GtkButton" id="button_del">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="tooltip-text" translatable="yes">Delete this word from dict</property>
<property name="label" translatable="yes">gtk-delete</property>
<property name="use_stock">True</property>
</object>

<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>

<child>

<object class="GtkToggleButton" id="toggle_soundex">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">Soundex</property>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>

<child>

<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
<property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>

<child>

<object class="GtkTreeView" id="tree_word">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="border_width">14</property>
<property name="headers_visible">False</property>
<property name="show_expanders">False</property>
</object>
</child>
</object>

<packing>
<property name="position">1</property>
</packing>
</child>

<child>

<object class="GtkScrolledWindow" id="scrolledwindow3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
<property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>

<child>

<object class="GtkTextView" id="text_display">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">False</property>
</object>
</child>
</object>

<packing>
<property name="position">2</property>
</packing>
</child>

<child>

<object class="GtkTreeView" id="tree_words">
<property name="can_focus">True</property>
<property name="border_width">14</property>
<property name="headers_visible">False</property>
<property name="show_expanders">False</property>
</object>

<packing>
<property name="position">3</property>
</packing>
</child>
</object>

<packing>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>

<object class="GtkWindow" id="add_window">
<property name="width_request">300</property>
<property name="height_request">200</property>
<property name="title" translatable="yes">Define a new word</property>
<property name="window_position">GTK_WIN_POS_CENTER_ALWAYS</property>
<property name="icon">116590553.png</property>
<property name="deletable">False</property>

<child>

<object class="GtkVBox" id="vbox3">
<property name="visible">True</property>
<property name="spacing">5</property>

<child>

<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">GTK_SHADOW_NONE</property>

<child>

<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="left_padding">12</property>

<child>

<object class="GtkEntry" id="entry_addn">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
</child>
</object>
</child>

<child type="label">

<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="label" translatable="yes"><b>Type words here</b></property>
<property name="use_markup">True</property>
</object>
</child>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>

<child>

<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">GTK_SHADOW_NONE</property>

<child>

<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="left_padding">12</property>

<child>

<object class="GtkTextView" id="textview_add">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
</child>
</object>
</child>

<child type="label">

<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="label" translatable="yes"><b>Define meaning of this word</b></property>
<property name="use_markup">True</property>
</object>
</child>
</object>

<packing>
<property name="position">1</property>
</packing>
</child>

<child>

<object class="GtkHBox" id="hbox3">
<property name="visible">True</property>

<child>
<placeholder/>
</child>

<child>

<object class="GtkButton" id="button_addn">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">gtk-apply</property>
<property name="use_stock">True</property>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">2</property>
<property name="position">1</property>
</packing>
</child>

<child>

<object class="GtkButton" id="button_canceln">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="label" translatable="yes">gtk-cancel</property>
<property name="use_stock">True</property>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="padding">2</property>
<property name="position">2</property>
</packing>
</child>
</object>

<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>

<object class="GtkAboutDialog" id="aboutdialog">
<property name="border_width">5</property>
<property name="title" translatable="yes">About Glade</property>
<property name="resizable">False</property>
<property name="window_position">GTK_WIN_POS_CENTER_ON_PARENT</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
<property name="has_separator">False</property>
<property name="program_name">Simple Dictionary</property>

<property name="copyright" translatable="yes">
Created by luckymancvp & Darknight
Based on gtk 2.0 & glade 3.0
</property>
<property name="website">2e-class.blogspot.com</property>
<property name="website_label" translatable="yes">2e-class.blogspot.com</property>
<property name="license" translatable="yes">Không biết nói jì cả, ngượng quá</property>
<property name="authors">Đặng Thanh Tú & Đõ Việt Cường</property>
<property name="documenters">I don't know I written it</property>
<property name="translator_credits" translatable="yes">Traselated by google traslated</property>
<property name="artists">A im ac</property>
<property name="logo">Image00001.jpg</property>

<child internal-child="vbox">

<object class="GtkVBox" id="dialog-vbox1">
<property name="visible">True</property>
<property name="spacing">2</property>

<child>
<placeholder/>
</child>

<child internal-child="action_area">

<object class="GtkHButtonBox" id="dialog-action_area1">
<property name="visible">True</property>
<property name="layout_style">GTK_BUTTONBOX_END</property>
</object>

<packing>
<property name="expand">False</property>
<property name="pack_type">GTK_PACK_END</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
[/code]

main.c
[code=]
# include <gtk/gtk.h>
# include "dict.h"

GtkBuilder *builder;
GtkWidget *main_window;
GtkWidget *add_window;
GtkWidget *tree_word;
GtkWidget *text_display;
GtkWidget *entry_word;
GtkWidget *button_add;
GtkWidget *button_del;
GtkWidget *button_edit;
GtkWidget *button_addn;
GtkWidget *textview_add;
GtkWidget *entry_addn;
GtkWidget *button_canceln;
GtkWidget *toggle_soundex;
GtkWidget *tree_words;
GtkAboutDialog *aboutdialog;

BTA *dict,*sdict;
enum
{
COL_WORD=0,
COL_NUM
};
enum
{
SORTID_WORD = 0,
};

void on_main_window_destroy (GtkObject *object, gpointer user_data);
void on_add_window_destroy (GtkObject *object, gpointer user_data);
void Startup_window();
void Add_word_to_tree_view();
void Initiation_tree(GtkWidget **view);
static void onSelectionChanged (GtkTreeSelection *sel, GtkListStore *liststore);
void Sync_tree_view(GtkWidget *tree_word, char *txt);
void on_entry_word_changed (GtkEditable *editable,
gpointer user_data);
static void on_button_add_clicked (GtkWidget *button, gpointer data);
static void on_button_addn_clicked (GtkWidget *button, gpointer data);
static void on_button_del_clicked (GtkWidget *button, gpointer data);
static void on_toggle_soundex_clicked (GtkWidget *button, gpointer data);
static gboolean delete_event( GtkWidget *widget,GdkEvent *event,gpointer data );
static gboolean delete_event_main( GtkWidget *widget,GdkEvent *event,gpointer data );


int main(int argc,char *argv[])
{
gtk_init(&argc,&argv);
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"theme.xml",NULL);

main_window=GTK_WIDGET(gtk_builder_get_object(builder,"main_window"));
add_window=GTK_WIDGET(gtk_builder_get_object(builder,"add_window"));
tree_word=GTK_WIDGET(gtk_builder_get_object(builder,"tree_word"));
text_display=GTK_WIDGET(gtk_builder_get_object(builder,"text_display"));
entry_word=GTK_WIDGET(gtk_builder_get_object(builder,"entry_word"));
button_add=GTK_WIDGET(gtk_builder_get_object(builder,"button_add"));
button_addn=GTK_WIDGET(gtk_builder_get_object(builder,"button_addn"));
button_edit=GTK_WIDGET(gtk_builder_get_object(builder,"button_edit"));
button_del=GTK_WIDGET(gtk_builder_get_object(builder,"button_del"));
entry_addn=GTK_WIDGET(gtk_builder_get_object(builder,"entry_addn"));
textview_add=GTK_WIDGET(gtk_builder_get_object(builder,"textview_add"));
button_canceln=GTK_WIDGET(gtk_builder_get_object(builder,"button_canceln"));
tree_words=GTK_WIDGET(gtk_builder_get_object(builder,"tree_words"));
toggle_soundex=GTK_WIDGET(gtk_builder_get_object(builder,"toggle_soundex"));
aboutdialog=GTK_WIDGET(gtk_builder_get_object(builder,"aboutdialog"));

gtk_builder_connect_signals(builder,NULL);

g_signal_connect(main_window,"destroy",on_main_window_destroy,NULL);
g_signal_connect(add_window,"destroy",on_add_window_destroy,NULL);
g_signal_connect(entry_word,"changed",on_entry_word_changed,NULL);
g_signal_connect(button_add,"clicked",on_button_add_clicked,NULL);
g_signal_connect(button_del,"clicked",on_button_del_clicked,NULL);
g_signal_connect(button_addn,"clicked",on_button_addn_clicked,NULL);
g_signal_connect(button_edit,"clicked",on_button_add_clicked,NULL);
g_signal_connect(button_canceln,"clicked",on_add_window_destroy,NULL);
g_signal_connect(toggle_soundex,"clicked",on_toggle_soundex_clicked,NULL);
g_signal_connect (G_OBJECT (add_window), "delete_event",G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (main_window), "delete_event",G_CALLBACK (delete_event_main), NULL);
g_signal_connect (aboutdialog, "delete_event",G_CALLBACK (gtk_main_quit), NULL);

g_object_unref(G_OBJECT(builder));
Startup_window();
gtk_widget_show(main_window);
//g_signal_connect(main_window, "delete_event", gtk_main_quit, NULL);
gtk_main();
return 0;
}

void
on_main_window_destroy (GtkObject *object, gpointer user_data)
{
btcls(dict);
btcls(sdict);
gtk_main_quit();
}

void
on_add_window_destroy (GtkObject *object, gpointer user_data)
{
gtk_widget_hide(add_window);
gtk_widget_show(main_window);
}

void Add_word_to_tree_views()
{
GtkListStore * liststore;
GtkTreeIter iter;

liststore=gtk_tree_view_get_model(GTK_TREE_VIEW(tree_words));
g_object_ref(liststore);
gtk_tree_view_set_model(GTK_TREE_VIEW(tree_words), NULL);
gtk_list_store_append(liststore,&iter);
gtk_list_store_set(liststore,&iter,COL_WORD,"fuck",-1);
gtk_tree_view_set_model(GTK_TREE_VIEW(tree_words), liststore);
g_object_unref(liststore);
}

void Startup_window()
{
Initiation(&dict,&sdict);
Initiation_tree(&tree_word);
Initiation_tree(&tree_words);
Add_word_to_tree_view();
// Add_word_to_tree_views();
}

gint sort_iter_compare_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer userdata)
{
gint sortcol = GPOINTER_TO_INT(userdata);
gint ret = 0;

switch (sortcol)
{
case SORTID_WORD:
{
gchar *name1, *name2;
gtk_tree_model_get(model, a, COL_WORD, &name1, -1);
gtk_tree_model_get(model, b, COL_WORD, &name2, -1);

if (name1 == NULL || name2 == NULL)
{
if (name1 == NULL && name2 == NULL)
break; /* both equal => ret = 0 */

ret = (name1 == NULL) ? -1 : 1;
}
else
{
ret = g_utf8_collate(name1,name2);
}

g_free(name1);
g_free(name2);
}
break;

default:
g_return_val_if_reached(0);
}
return ret;
}

void Add_word_to_tree_view()
{
GtkListStore * liststore;
GtkTreeIter iter;

liststore=gtk_tree_view_get_model(GTK_TREE_VIEW(tree_word));
g_object_ref(liststore);
gtk_tree_view_set_model(GTK_TREE_VIEW(tree_word), NULL);
char word[wlength],mean[mlength];
int size;
btsel(dict, "", mean, sizeof(char)*mlength, &size);
while ( btseln(dict, word, mean, sizeof(char)*mlength, &size)==0 )
{
gtk_list_store_append(liststore,&iter);
gtk_list_store_set(liststore,&iter,COL_WORD,word,-1);
}
gtk_tree_view_set_model(GTK_TREE_VIEW(tree_word), liststore);
g_object_unref(liststore);
}

void Initiation_tree(GtkWidget **view)
{
GtkCellRenderer *renderer;
GtkTreeSelection *sel;
GtkListStore *liststore;
GtkTreeIter iter;
GtkTreeSortable *sortable;

liststore=gtk_list_store_new(COL_NUM,G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(*view),GTK_TREE_MODEL(liststore));
renderer=gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(*view),
-1,
"WORD",
renderer,
"text",
COL_WORD,NULL);

sortable = GTK_TREE_SORTABLE(liststore);
gtk_tree_sortable_set_sort_func(sortable, SORTID_WORD, sort_iter_compare_func,
GINT_TO_POINTER(SORTID_WORD), NULL);
gtk_tree_sortable_set_sort_column_id(sortable, SORTID_WORD, GTK_SORT_ASCENDING);

sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(*view));
gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
g_signal_connect(sel, "changed", G_CALLBACK(onSelectionChanged), liststore);
}

void show_text(char mean[])
{
GtkTextBuffer *buffer=gtk_text_view_get_buffer (text_display);
gtk_text_buffer_set_text(buffer,mean,strlen(mean));
}

static void
onSelectionChanged (GtkTreeSelection *sel, GtkListStore *liststore)
{
GtkTreeIter selected_row;
gchar *word;
char mean[mlength];
gint size;
if (gtk_tree_selection_get_selected(sel, NULL, &selected_row))
{
gtk_tree_model_get(GTK_TREE_MODEL(liststore),&selected_row,COL_WORD,&word,-1);
btsel(dict,word, mean,mlength,&size);
show_text(mean);
g_free(word);
}
}

int Compare(char *word,char *txt)
{
int i,m=strlen(txt);
if (m>strlen(word)) return 1;
for (i=0;i<m;i++)
if (word[i]!=txt[i]) return 1;
return 0;
}

void make_soundex_list(char *txt)
{
GtkListStore *liststore;
GtkTreeIter iter;
GtkTreeSelection *sel;
GtkTreePath *path;


liststore=gtk_tree_view_get_model(GTK_TREE_VIEW(tree_words));

gtk_list_store_clear(liststore);
char word[wlength],mean[mlength],soundex[mlength],sd[mlength];
int col=0,size;


Cal_SoundEx(sd, txt, 4,1);
btsel(sdict, "", soundex, sizeof(char)*wlength, &size);
while ( btseln(sdict, word, soundex,sizeof(char)*wlength, &size)==0)
if (strcmp(soundex,sd)==0){
btsel(dict,word, mean,mlength,&size);
gtk_list_store_append(liststore,&iter);
gtk_list_store_set(liststore,&iter,COL_WORD,word,-1);

}
}

void Sync_tree_view(GtkWidget *tree_word, char *txt)
{
GtkListStore *liststore;
GtkTreeIter iter;
GtkTreeSelection *sel;
GtkTreePath *path;
GtkListStore *liststore1;
char *word;
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_word));
liststore=gtk_tree_view_get_model(GTK_TREE_VIEW(tree_word));
gtk_tree_model_get_iter_first (GTK_TREE_MODEL(liststore), &iter);
do
{
gtk_tree_model_get(GTK_TREE_MODEL(liststore),&iter,COL_WORD,&word,-1);
if (Compare(word,txt)==0)
{
path=gtk_tree_model_get_path(GTK_TREE_MODEL(liststore),&iter);
gtk_tree_view_set_cursor(tree_word,path,0,FALSE);
break;
}
}while ( gtk_tree_model_iter_next (liststore,&iter));
free(word);

}

void
on_entry_word_changed (GtkEditable *editable,
gpointer user_data)
{
const char *txt;

g_assert(GTK_IS_ENTRY(entry_word));
txt = gtk_entry_get_text(GTK_ENTRY(entry_word));

if (txt && *txt)
{
Sync_tree_view(tree_word,txt);
if(gtk_toggle_button_get_active(toggle_soundex)){
make_soundex_list(txt);
//Sync_tree_view(tree_word,txt);
}

}

}

static void
on_button_add_clicked (GtkWidget *button, gpointer data)
{
gtk_widget_hide(main_window);
gtk_widget_show(add_window);
}

static void
on_button_addn_clicked (GtkWidget *button, gpointer data)
{
GtkListStore *liststore;
GtkTreeIter iter;
GtkTextIter start;
GtkTextIter end;

char *word,*mean;

g_assert(GTK_IS_ENTRY(entry_addn));
word = gtk_entry_get_text(GTK_ENTRY(entry_addn));

GtkTextBuffer *buffer=gtk_text_view_get_buffer (textview_add);
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
mean = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

if (Add_word(dict,sdict,word,mean))
{

liststore = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_word));
gtk_list_store_append(liststore,&iter);
gtk_list_store_set(liststore,&iter,COL_WORD,word,-1);

free(word);free(mean);
}
gtk_button_clicked(button_canceln);
}

static void on_button_del_clicked (GtkWidget *button, gpointer data)
{
// Get tree information
GtkTreeSelection *sel;
GtkListStore *liststore;
GtkTreeIter selected_row;
liststore = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_word));
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_word));

// Finish get --> get word
gchar *word;
if (gtk_tree_selection_get_selected(sel, NULL, &selected_row))
{
gtk_tree_model_get(GTK_TREE_MODEL(liststore),&selected_row,COL_WORD,&word,-1);
Del_word(dict,sdict,word);
gtk_list_store_remove(liststore,&selected_row);
g_free(word);
}
}

static void on_toggle_soundex_clicked (GtkWidget *button, gpointer data)
{
if(gtk_toggle_button_get_active(toggle_soundex)){
gtk_widget_show(tree_words);
}
else gtk_widget_hide(tree_words);

}

static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_button_clicked(button_canceln);
return TRUE;
}

static gboolean delete_event_main( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
gtk_dialog_run(aboutdialog);
//gtk_show_about_dialog (NULL,"authors",NULL);
//gtk_widget_show(GTK_WIDGET(aboutdialog));
return TRUE;
}
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group