/*
 * freescope - Free source browser
 * Copyright (C) 2001  Olivier Deme
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 * FILE:        cursui.h
 *
 * DESCRIPTION: This file defines the class CursUI. This class is a database
 *              user and provides an curses user interface.
 *
 */

#ifndef _CURSUI_H_
#define _CURSUI_H_

/************/
/* INCLUDES */
/************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <memory>
#include <string>
#include <vector>
#include <list>
#include "dbuser.h"
#include "siglink.h"

#ifdef HAVE_LIBNCURSES
#include <ncurses.h>
#else /* HAVE_LIBNCURSES */
#include <curses.h>
#endif /* HAVE_LIBNCURSES */

#include <form.h>
#include <panel.h>


/*********************/
/* NAMESPACE SYMBOLS */
/*********************/

using std::vector;
using std::string;
using std::list;


/*********************/
/* CLASS DEFINITIONS */
/*********************/

class CursUI : public DbUser
{
    // TYPE DEFINITIONS
private:
    typedef enum
    {
        FIELD_LABEL_C_SYMBOL = 0,
        FIELD_LABEL_DEFINITION,
        FIELD_LABEL_FUNC_CALLED,
        FIELD_LABEL_FUNC_CALLER,
        FIELD_LABEL_FIND_TEXT,
#ifdef HAVE_TEXT_CHANGE_CAPABILITY
        FIELD_LABEL_CHANGE_TEXT,
#endif // HAVE_TEXT_CHANGE_CAPABILITY
        FIELD_LABEL_FIND_REGEXP,
        FIELD_LABEL_FIND_FILE,
        FIELD_LABEL_INCLUDE,
        FIELD_C_SYMBOL,
        FIELD_DEFINITION,
        FIELD_FUNC_CALLED,
        FIELD_FUNC_CALLER,
        FIELD_FIND_TEXT,
#ifdef HAVE_TEXT_CHANGE_CAPABILITY
        FIELD_CHANGE_TEXT,
#endif // HAVE_TEXT_CHANGE_CAPABILITY
        FIELD_FIND_REGEXP,
        FIELD_FIND_FILE,
        FIELD_INCLUDE
    } field_t;

    typedef enum
    {
        KEY_TAB         = '\t',
        KEY_QUIT        = '' 
    } key_t;

    typedef enum
    {
        RES_FIELDS       = 5
    } result_fields_t;

    typedef enum
    {
        MODE_QUERY,
        MODE_SELECT
    } mode_t;

    typedef struct
    {
        string  filename;
        string  file_without_path;
        string  scope;
        string  lineno;
        string  line;
    } result_elem_t;

    typedef struct
    {
        FIELD*  field;
        string  query;
    } query_t;


    // CONSTRUCTORS
public:
             CursUI ();
    virtual ~CursUI () {};

    // METHODS
public:
    static void on_resize      (int signo, void* arg);
    static void on_exit        (int signo, void* arg);
           void display_result ();

protected:
    virtual void do_refresh    () throw ();
    virtual void do_loop       () throw ();
            void do_init       ();
            void clear_results ();
            void terminate     (const string& reason);

private:
    virtual void create_user_fields   ();
    virtual void create_result_fields ();
    virtual void do_key               (int key);
    virtual void set_status           (string status, bool center = true);
    virtual void do_request           ();
    virtual void show_next_results    ();
    virtual void show_prev_results    ();
    virtual void format_next_results  ();
    virtual void start_editor         (int index);
    virtual void highlight            (bool highlight);
    virtual void highlightNext        ();
    virtual void highlightPrev        ();
    virtual void show_result_status   ();
            void show_history         ();
            bool show_next_completion ();
            bool show_prev_completion ();
            void select_changes       ();
            void apply_changed_text   ();


    // ATTRIBUTES
private:
    int                           m_wndHeight;
    int                           m_wndWidth;
    FIELD*                        m_field_query [FIELD_INCLUDE + 2];
    FORM*                         m_form_query;
    FIELD*                        m_field_res [RES_FIELDS * 100];
    FORM*                         m_form_result;
    PANEL*                        m_panel_user;
    PANEL*                        m_panel_results;
    PANEL*                        m_panel_status;
    string                        m_query_label [FIELD_LABEL_INCLUDE + 1];
    unsigned int                  m_maxResults;
    vector<result_elem_t>         m_fmt_results; // Formatted results
    unsigned int                  m_curr_fmt_result;
    unsigned int                  m_results_per_page;
    bool                          m_first_results;
    mode_t                        m_mode;
    list<query_t>                 m_history;
    list<query_t>::iterator       m_history_it;
    bool                          m_next_completion;
    string                        m_stub;
    bool                          m_selecting_changes;
    vector<bool>                  m_selection;
    string                        m_old_value;
    string                        m_new_value;
    bool                          m_all_selected;
};

#endif // _CURSUI_H_


syntax highlighted by Code2HTML, v. 0.9.1