Hello,
I write a simple text editor with the Entry widget (elementary 1.7).
But when I use that in a "big" file (for example 300 lines),
I have an memory corrupted error (it works for small file).
The Entry widget has a maximal number of character?
Kind regards
Oboeuf David
#include <Elementary.h>
#include <stdio.h>
#include <string.h>
char *keyword[] = {
"#define ", "#elif ", "#endif ", "#if ", "#ifdef ", "#ifndef ", "#include ",
"/*", "*/", "//", "NULL", "auto ", "break ", "case ", "char ", "const ",
"continue ", "default ", "do " ,"double ", "else ", "enum ", "extern ",
"float ", "for ", "goto ", "if ", "int ", "long ", "register ", "return ",
"short ", "signed ", "sizeof ", "static ","struct ", "switch ", "typedef ",
"union ", "unsigned ", "void ", "volatile ", "while"
};
typedef struct donnee{
Evas_Object *win;
Evas_Object *inwin;
Evas_Object *fileselect;
Evas_Object *entry;
char* txt;
char save_as;
}donnee;
static void
on_color(void *data, Evas_Object *obj, void *event_info)
{
donnee *dat = (donnee*)data;
char *name;
char buf[28];
int i = 0, j = 0;
int pos = elm_entry_cursor_pos_get(dat->entry);
int pos_init, pos_rel;
elm_entry_cursor_line_begin_set(dat->entry);
pos_init = elm_entry_cursor_pos_get(dat->entry);
elm_entry_cursor_selection_begin(dat->entry);
elm_entry_cursor_line_end_set(dat->entry);
elm_entry_cursor_selection_end(dat->entry);
dat->txt = elm_entry_markup_to_utf8(elm_entry_selection_get(dat->entry));
char *mark = strdup(elm_entry_selection_get(dat->entry));
elm_entry_select_none(dat->entry);
// if(mark) printf("mark %s ",mark);
for(i = 0; i < 43; i++)
{
name = dat->txt;
do{
name = strstr(name, keyword[i]);
snprintf(buf, sizeof(buf), "<hilight>%s</hilight>", keyword[i]);
if(name){
pos_rel = name - dat->txt + pos_init;
switch(i) {
case 8:
if( !strstr(mark, "*/</hilight>") ){
elm_entry_cursor_pos_set(dat->entry, pos_rel + strlen(keyword[i]) );
elm_entry_entry_insert(dat->entry, "</hilight>");
}
break;
case 7:
if( !strstr(mark, "<hilight>/*") ){
elm_entry_cursor_pos_set(dat->entry, pos_rel);
elm_entry_entry_insert(dat->entry, "<hilight>");
}
break;
case 9:
if( !strstr(mark, "<hilight>//") ){
elm_entry_cursor_pos_set(dat->entry, pos_rel);
elm_entry_entry_insert(dat->entry, "<hilight>");
elm_entry_cursor_line_end_set(dat->entry);
elm_entry_entry_insert(dat->entry, " </hilight>");
}
break;
default:
if( !strstr(mark, buf) ){
elm_entry_cursor_pos_set(dat->entry, pos_rel);
elm_entry_entry_insert(dat->entry, "<hilight>");
elm_entry_cursor_pos_set(dat->entry, pos_rel + strlen(keyword[i]) );
elm_entry_entry_insert(dat->entry, "</hilight>");
}
break;
}
name += strlen(keyword[i]);
}
memset(buf,'\n',sizeof(buf));
}while(name != NULL);
}
free(dat->txt);
free(mark);
// printf("text : %s\n", elm_entry_entry_get(dat->entry));
elm_entry_cursor_pos_set(dat->entry,pos);
}
static void
fileselector_done(void *data, Evas_Object *obj, void *event_info)
{
const char *selected = event_info;
donnee *dat = (donnee*)data;
if (selected)
{
elm_entry_file_set(dat->entry, selected, ELM_TEXT_FORMAT_PLAIN_UTF8);
elm_win_title_set(dat->win, selected);
evas_object_del(dat->fileselect);
evas_object_del(dat->inwin);
if(dat->save_as)
elm_entry_entry_set(dat->entry, dat->txt);
}
else
{
evas_object_del(dat->fileselect);
evas_object_del(dat->inwin);
}
elm_entry_file_save(dat->entry);
dat->save_as = 0;
elm_entry_cursor_begin_set(dat->entry);
do{
on_color(dat,obj,event_info);
}while(elm_entry_cursor_down(dat->entry));
elm_entry_cursor_begin_set(dat->entry);
}
static void
on_done(void *data, Evas_Object *obj, void *event_info)
{
/* quit the mainloop (elm_run) */
elm_exit();
}
static void
new_file(void *data, Evas_Object *obj, void *event_info)
{
donnee *dat = (donnee*)data;
elm_win_title_set(dat->win, "New File");
elm_entry_entry_set(dat->entry,NULL);
elm_object_focus_set(dat->entry, EINA_TRUE);
}
static void
open(void *data, Evas_Object *obj, void *event_info)
{
donnee *dat = (donnee*)data;
dat->inwin = elm_win_inwin_add(dat->win);
dat->fileselect = elm_fileselector_add(dat->inwin);
elm_fileselector_path_set(dat->fileselect, getenv("HOME"));
elm_fileselector_is_save_set(dat->fileselect, EINA_TRUE);
evas_object_smart_callback_add(dat->fileselect, "done", fileselector_done, dat);
evas_object_size_hint_weight_set(dat->fileselect, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(dat->fileselect, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(dat->fileselect);
elm_win_inwin_content_set(dat->inwin, dat->fileselect);
elm_win_inwin_activate(dat->inwin);
}
static void
on_save(void *data, Evas_Object *obj, void *event_info)
{
donnee *dat = (donnee*)data;
printf("save\n");
elm_entry_file_save(dat->entry);
printf("save end\n");
}
static void
on_save_as(void *data, Evas_Object *obj, void *event_info)
{
donnee *dat = (donnee*)data;
dat->txt = elm_entry_entry_get(dat->entry);
dat->save_as = 1;
open(data, obj, event_info);
}
EAPI int
elm_main(int argc, char **argv)
{
donnee dat;
Evas_Object *win, *bg, *box, *bx2, *lab, *entry, *btn_quit, *btn_save;
Evas_Object *btn_open, *btn_new, *btn_save_as, *btn_synt, *btn_bold;
int h, w, x, y;
char str[80];
/* new window - do the usual and give it a name, title and delete handler */
win = elm_win_add(NULL, "E.T.", ELM_WIN_BASIC);
elm_win_title_set(win, "New File");
/* when the user clicks "close" on a window there is a request to delete */
evas_object_smart_callback_add(win, "delete,request", on_done, NULL);
evas_object_resize(win, 800, 800);
dat.win = win;
dat.save_as = 0;
/* add a standard bg */
bg = elm_bg_add(win);
/* not allow bg to expand. let's limit dialog size to contents */
evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
/* add object as a resize object for the window (controls window minimum
* size as well as gets resized if window is resized) */
elm_win_resize_object_add(win, bg);
evas_object_show(bg);
/* add a box object - default is vertical. a box holds children in a row,
* either horizontally or vertically. nothing more. */
box = elm_box_add(win);
/* make the box hotizontal */
elm_box_horizontal_set(box, EINA_FALSE);
evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
/* add object as a resize object for the window (controls window minimum
* size as well as gets resized if window is resized) */
elm_win_resize_object_add(win, box);
evas_object_show(box);
entry = elm_entry_add(win);
elm_entry_scrollable_set(entry, EINA_TRUE);
elm_entry_autosave_set(entry, EINA_FALSE);
evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
elm_entry_line_wrap_set(entry, ELM_WRAP_NONE);
evas_object_smart_callback_add(entry, "changed,user", on_color, &dat);
elm_box_pack_end(box, entry);
evas_object_show(entry);
elm_object_focus_set(entry, EINA_TRUE);
dat.entry = entry;
/* add a box object for buttons- default is vertical. a box holds children in a row,
* either horizontally or vertically. nothing more. */
bx2 = elm_box_add(win);
/* make the box hotizontal */
elm_box_horizontal_set(bx2, EINA_TRUE);
evas_object_size_hint_weight_set(bx2, 0.0, 0.0);
/* add object as a resize object for the window (controls window minimum
* size as well as gets resized if window is resized) */
elm_box_pack_end(box, bx2);
evas_object_show(bx2);
btn_new = elm_button_add(win);
elm_object_part_text_set(btn_new, NULL, "New");
elm_box_pack_end(bx2, btn_new);
evas_object_show(btn_new);
evas_object_smart_callback_add(btn_new, "clicked", new_file, &dat);
btn_open = elm_button_add(win);
elm_object_part_text_set(btn_open, NULL, "Open");
elm_box_pack_end(bx2, btn_open);
evas_object_show(btn_open);
evas_object_smart_callback_add(btn_open, "clicked", open, &dat);
btn_save = elm_button_add(win);
elm_object_part_text_set(btn_save, NULL, "Save");
elm_box_pack_end(bx2, btn_save);
evas_object_show(btn_save);
evas_object_smart_callback_add(btn_save, "clicked", on_save, &dat);
btn_save_as = elm_button_add(win);
elm_object_part_text_set(btn_save_as, NULL, "Save As");
elm_box_pack_end(bx2, btn_save_as);
evas_object_show(btn_save_as);
evas_object_smart_callback_add(btn_save_as, "clicked", on_save_as, &dat);
btn_synt = elm_button_add(win);
elm_object_part_text_set(btn_synt, NULL, "Syntaxic color");
elm_box_pack_end(bx2, btn_synt);
evas_object_show(btn_synt);
evas_object_smart_callback_add(btn_synt, "clicked", on_color, &dat);
btn_quit = elm_button_add(win);
elm_object_part_text_set(btn_quit, NULL, "Quit");
elm_box_pack_end(bx2, btn_quit);
evas_object_show(btn_quit);
evas_object_smart_callback_add(btn_quit, "clicked", on_done, NULL);
evas_object_show(win);
elm_run();
return 0;
}
ELM_MAIN()
------------------------------------------------------------------------------
Don't let slow site performance ruin your business. Deploy New Relic APM
Deploy New Relic app performance management and know exactly
what is happening inside your Ruby, Python, PHP, Java, and .NET app
Try New Relic at no cost today and get our sweet Data Nerd shirt too!
http://p.sf.net/sfu/newrelic-dev2dev
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel