RSS
 

First Enlightenment App

01 Apr

Well Enlightenment has come a long ways. I remember getting e16 over 10 years ago and i thought it was awesome back then. It has only gotten better. I have been following the progress over the years and have tried e17 several times. Well now it looks like e17 is getting stable and a new stable release will be out. Now it may be another year i don't know, but things are looking very good.

I have two apps that i want to do the first is a gui front-end for notmuchmail. And the other is just a personal project to see if something is very feasible. I will give more input once i have it in a viewable state, right now it would be labelled as vaporware.

Well first things first i need to learn E, and so i started with the EFL cookbook on the enlightenment website. Well it must be out of date because i had to fix some things to get the code to compile. Here is the code that i am currently using.

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <Ewl.h>

#define PROG    "EWL Text Viewer"

/* globals */
static Ewl_Widget *main_win = NULL;
static Ewl_Widget *fd_win = NULL;

/* pre-declarations */
static void destroy_cb(Ewl_Widget *, void *, void *);
static void destroy_filedialog_cb(Ewl_Widget *, void *, void *);
static void open_file_cb(Ewl_Widget *, void *, void *);
static void home_cb(Ewl_Widget *win, void *ev, void *data);
static void file_menu_open_cb(Ewl_Widget *, void *, void *);
static void key_up_cb(Ewl_Widget *, void *, void *);

static char *read_file(char *);
static void mk_gui(void);


/* lets go */
int main(int argc, char ** argv) {
    ewl_init(&argc, argv);
    mk_gui();
    ewl_main();
    return 0;
}

/* build the main gui */
static void mk_gui(void) {
      Ewl_Widget *box = NULL, *menu_bar = NULL;
      Ewl_Widget *text_area = NULL, *scroll = NULL;

    /* create the main window */
    main_win = ewl_window_new();
    ewl_window_title_set(EWL_WINDOW(main_win), PROG);
    ewl_window_name_set(EWL_WINDOW(main_win), PROG);
    ewl_window_class_set(EWL_WINDOW(main_win), PROG);

    ewl_object_size_request(EWL_OBJECT(main_win), 200, 300);
    ewl_object_fill_policy_set(EWL_OBJECT(main_win), EWL_FLAG_FILL_FILL);

   // ewl_callback_append(main_win, EWL_CALLBACK_DELETE_WINDOW, destroy_cb, NULL);
    ewl_widget_show(main_win);
   
    /* create the main container */
    box = ewl_vbox_new();
    ewl_container_child_append(EWL_CONTAINER(main_win), box);
    ewl_object_fill_policy_set(EWL_OBJECT(box), EWL_FLAG_FILL_FILL);
    ewl_widget_show(box);
   
    /* create the menu bar */
    menu_bar = ewl_hbox_new();
    ewl_container_child_append(EWL_CONTAINER(box), menu_bar);
    ewl_object_fill_policy_set(EWL_OBJECT(menu_bar), EWL_FLAG_FILL_HSHRINK);
    ewl_object_alignment_set(EWL_OBJECT(menu_bar), EWL_FLAG_ALIGN_LEFT);
    ewl_box_spacing_set(EWL_BOX(menu_bar), 4);
    ewl_object_padding_set(EWL_OBJECT(menu_bar), 5, 5, 5, 5);
    ewl_widget_show(menu_bar);
 
    /* create the scrollpane */
    scroll = ewl_scrollpane_new();
    ewl_container_child_append(EWL_CONTAINER(box), scroll);
    ewl_object_fill_policy_set(EWL_OBJECT(scroll), EWL_FLAG_FILL_FILL);
    /*ewl_scrollpane_hscrollbar_flag_set(EWL_SCROLLPANE(scroll),
                                        EWL_SCROLLPANE_FLAG_AUTO_VISIBLE);
    ewl_scrollpane_vscrollbar_flag_set(EWL_SCROLLPANE(scroll),
                                        EWL_SCROLLPANE_FLAG_AUTO_VISIBLE);
*/
    ewl_widget_show(scroll);

    /* create the text area */
    char *content = "This is some content";
    text_area = ewl_text_new();
    ewl_text_text_set(EWL_TEXT(text_area), content);
    ewl_container_child_append(EWL_CONTAINER(scroll), text_area);
    ewl_object_padding_set(EWL_OBJECT(text_area), 1, 1, 1, 1);
    ewl_widget_show(text_area);
   
    /* create the menu */
    {
        Ewl_Widget *file_menu = NULL, *item = NULL;
  
        /* create the file menu */
        file_menu = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(file_menu), "File");
        ewl_container_child_append(EWL_CONTAINER(menu_bar), file_menu);
        ewl_widget_show(file_menu);
  
        /* add the open entry to the file menu */
        item = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(item), "Open");
        ewl_container_child_append(EWL_CONTAINER(file_menu), item);
        ewl_callback_append(item, EWL_CALLBACK_CLICKED, file_menu_open_cb,
                                                                text_area);
        ewl_widget_show(item);
  
        /* add the quit entry to the file menu */
        item = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(item), "Quit");
        ewl_container_child_append(EWL_CONTAINER(file_menu), item);
        ewl_callback_append(item, EWL_CALLBACK_CLICKED, destroy_cb, NULL);
        ewl_widget_show(item);
    }

   }

/* destroy the app */
static void destroy_cb(Ewl_Widget *win, void *ev, void *data) {
    ewl_widget_destroy(win);
    ewl_main_quit();
}

/* the file menu open button callback */
static void file_menu_open_cb(Ewl_Widget *win, void *ev, void *data) {
    Ewl_Widget *fd = NULL;
    Ewl_Widget *box = NULL;
    Ewl_Widget *home = NULL;

    /* create the file dialog window */
    fd_win = ewl_window_new();
    ewl_window_title_set(EWL_WINDOW(fd_win), PROG " -- file dialog");
    ewl_window_name_set(EWL_WINDOW(fd_win), PROG " -- file dialog");
    ewl_window_class_set(EWL_WINDOW(fd_win), PROG " -- file dialog");
    ewl_object_size_request(EWL_OBJECT(fd_win), 500, 400);
    ewl_object_fill_policy_set(EWL_OBJECT(fd_win),
                EWL_FLAG_FILL_FILL | EWL_FLAG_FILL_SHRINK);
    ewl_callback_append(fd_win, EWL_CALLBACK_DELETE_WINDOW,
                                destroy_filedialog_cb, NULL);
    ewl_widget_show(fd_win);

    /* fd win container */
    box = ewl_vbox_new();
    ewl_container_child_append(EWL_CONTAINER(fd_win), box);
    ewl_object_fill_policy_set(EWL_OBJECT(box),
                EWL_FLAG_FILL_FILL | EWL_FLAG_FILL_SHRINK);
    ewl_widget_show(box);

    /* the file dialog */
    fd = ewl_filedialog_new();
    ewl_callback_append(fd, EWL_CALLBACK_VALUE_CHANGED, open_file_cb, data);
    ewl_container_child_append(EWL_CONTAINER(box), fd);

    /* add a home button */
    home = ewl_button_new();
    ewl_button_label_set(EWL_BUTTON(home), "Home");
    ewl_callback_append(home, EWL_CALLBACK_CLICKED, home_cb, fd);
    ewl_object_fill_policy_set(EWL_OBJECT(home), EWL_FLAG_FILL_HFILL);
    ewl_container_child_append(EWL_CONTAINER(fd), home);
    ewl_widget_show(home);

    ewl_widget_show(fd);
}

/* close the file dialog */
static void destroy_filedialog_cb(Ewl_Widget *win, void *ev, void *data) {
    ewl_widget_hide(win);
    ewl_widget_destroy(win);
}

/* the file dialog open button callback */
static void open_file_cb(Ewl_Widget *w, void *ev, void *data) {
    char *text = NULL;
    char *filename;
    int *response = (int *)ev;

    switch (*response) {
        case EWL_STOCK_OPEN:
            filename = ewl_filedialog_selected_file_get(EWL_FILEDIALOG(w));
            text = read_file(filename);
            break;

        case EWL_STOCK_CANCEL:
            break;
    }

    if (text) {
        ewl_text_text_set(EWL_TEXT(data), text);
        free(text);
    }          
    text = NULL;

    ewl_widget_hide(fd_win);
}


/* the fd home button is clicked */
static void home_cb(Ewl_Widget *win, void *ev, void *data) {
    char *home = NULL;
    Ewl_Filedialog *fd = data;
   
    home = getenv("HOME");
    if (home)
        ewl_filedialog_directory_set(fd, home);
}  

/* read a file */
static char *read_file(char *file) {
    char *text = NULL;
    FILE *f = NULL;
    int read = 0, st_ret = 0;
    struct stat s;
   
    f = fopen(file, "r");
    st_ret = stat(file, &s);

    if (st_ret != 0) {
        if (errno == ENOENT)
            printf("not a file %s\n", file);
        return NULL;
    }

    text = (char *)malloc(s.st_size * sizeof(char));
    read = fread(text, sizeof(char), s.st_size, f);
        
    fclose(f);
    return text;
}


/* a key was pressed */
static void key_up_cb(Ewl_Widget *win, void *ev, void *data) {
    Ewl_Event_Key_Down *e = (Ewl_Event_Key_Down *)ev;
    //Ewl_Scrollpane *scroll = (Ewl_Scrollpane *)data;
   double val = 0;
    if (!strcmp(e->base.keyname, "q")) {
        destroy_cb(win, ev, data);

    } else if (!strcmp(e->base.keyname, "Left")) {
        //double val = ewl_scrollpane_hscrollbar_value_get(EWL_SCROLLPANE(scroll));
        //double step = ewl_scrollpane_hscrollbar_step_get(EWL_SCROLLPANE(scroll));

        if (val != 0)
          //  ewl_scrollpane_hscrollbar_value_set(EWL_SCROLLPANE(scroll),
            //                                                    val - step);
             printf("here");
    } else if (!strcmp(e->base.keyname, "Right")) {
        //double val = ewl_scrollpane_hscrollbar_value_get(EWL_SCROLLPANE(scroll));
        //double step = ewl_scrollpane_hscrollbar_step_get(EWL_SCROLLPANE(scroll));

        if (val != 1)
           // ewl_scrollpane_vscrollbar_value_set(EWL_SCROLLPANE(scroll),
             //                                                   val + step);
        printf("here");
    } else if (!strcmp(e->base.keyname, "Up")) {
        //double val = ewl_scrollpane_vscrollbar_value_get(EWL_SCROLLPANE(scroll));
       // double step = ewl_scrollpane_vscrollbar_step_get(EWL_SCROLLPANE(scroll));

        if (val != 0)
         //   ewl_scrollpane_vscrollbar_value_set(EWL_SCROLLPANE(scroll),
          //                                                      val - step);
           printf("here");
    } else if (!strcmp(e->base.keyname, "Down")) {
        //double val = ewl_scrollpane_vscrollbar_value_get(EWL_SCROLLPANE(scroll));
        //double step = ewl_scrollpane_vscrollbar_step_get(EWL_SCROLLPANE(scroll));
   
        if (val != 1)
          //  ewl_scrollpane_vscrollbar_value_set(EWL_SCROLLPANE(scroll),
            //                                                    val + step);
            printf("here");
    }
}

You can compile the application with the following:

gcc -Wall -o ewl_text main.c `pkg-config --cflags --libs ewl`

The problem i am having is with the ewl_scrollpane_vscrollbar_* functions. They are throwing errors when i try to use them.

implicit declaration of function ‘ewl_scrollpane_hscrollbar_flag_set’

So i posted my question out on the forum, and i hope to get some help on fixing it. The app does not have scroll bars, and if you load a good size document it looses it's menus from time to time. Not the best, but it's my first shot at this.

If you have any advice let me know.

Update 4/02/2010

I found this other getting started page. Thought i would share it as well. A few things are wrong in it. On line 114 or around that you need to find ecore_list_goto_first(files); and replace it with ecore_list_first_goto(files); Even after that i could not get files = ecore_file_ls(dir); to work. So i commented out the section. The main goal of this was to figure out why when i closed my app i would get a segfault. Well it seems that it's a problem with the version of enlightenment i am using.

 

Leave a Reply

Comments are closed