/********************************
Title : imagedialog.h
Authors : David Boys, Mike MacIntyre
Class Definition for  ImageDialog Superclass
Last updated : August 8, 2000
*********************************/

#ifndef __IMAGEDIALOG_H__
#define __IMAGEDIALOG_H__

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "filecanvas.h"

#include <wx/treectrl.h>
#include <wx/textctrl.h>
#include <wx/settings.h>

#include <map>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

/*  Begin Members table

               Member                       |          Purpose 
                                      
    wxTextCtrl   * enter_box;               |   receives file specs for search      
    wxStaticText * preview_label;           |   label for medium-sized image
    wxStaticText * filter_label;            |   label for wxChoice of filter
    wxStaticText * file_label;              |   label for enter_box
    wxButton     * ok_button;               |   
    wxButton     * cancel_button;           |
    PreviewPanel * preview;                 |   preview image placed here
    wxChoice     * filter;                  |   for filespec selection
    FileCanvas   * scroll_page;             |   images found in search displayed here
    wxTreeCtrl   * tree;                    |   specific instances need to be defined by subclass
    const wxString  config_dir;             |   stores path to applications setup directory
    map <wxString, wxString> & icon_files;  |   <path to real image, path to iconized image>
    int config_icon_size;                   |   user selected size for iconized image
    wxPanel      * preview_info;            |   holds the header and the label
    wxStaticText * preview_info_label;      |   displays width  and height and byte size of preview image
    wxStaticText * preview_info_header;     |   header  for preview_info_label
    const wxString config_dir;              |   allows access to configuration directory
    const wxString map_filename;            |   allows retrieval and rewrite of file configured for map
    vector <wxString> temp_selections;      |   stores the images currently selected by user
    vector <wxString> selected_files;       |   stores the images selected by user after OK button press
    map <wxString, wxString>  icon_files;   |   associates path to original image with path to iconized image
    int config_icon_size;                   |   size selected by user of the icons that appear after a successful search
    wxString filespec_choices[CHOICE_SIZE]; |   choices that appear on the wxChoice control
    wxString filespec_filters[CHOICE_SIZE]; |   specific filespecs associated with above choices
    vector <wxString> panel_creation_files; |   holds the vector of filenames that results from a search
    int panel_Y;                            |   used to format panel placement on canvas
    int panel_X;                            |   used to format panel placement on canvas
    int panel_counter;                      |   used to format panel placement on canvas
    int panels_per_column;                  |   used to format panel placement on canvas
    int max_panel_width;                    |   used to format panel placement on canvas
    wxTimer  panel_timer;                   |   used instead of loop to create panels based on panel_creation_files vector



    End  Members Table  */


const double TREE_WIDTH_RATIO = .25;
const double TREE_HEIGHT_RATIO = .60;
const double PREVIEW_HEIGHT_RATIO = .75;
const double TEXT_CTRLS_WIDTH_RATIO = .70;
const double ROUNDING = 0.5;
const double FILE_CANVAS_WIDTH_RATIO = 1 - TREE_WIDTH_RATIO;

const int TEXT_CTRLS_WIDTH = 250;
const int BORDER = 5;
const int MAX_TEXT_CTRLS_WIDTH = 350;
const int TREE_CANVAS_SPACES = 3 * BORDER;
const int TREE_PREVIEW_SPACES = 3 * BORDER;
const int PREVIEW_LABEL_HEIGHT = 20;
const int CHOICE_SIZE = 8;
const int FILESPEC_SIZE = CHOICE_SIZE;
const int WIDTH_SPACES = 15;
class PreviewPanel;

class ImageDialog : public wxDialog
{
public:
    ImageDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
                const wxSize& size, const wxString & map_filename, const wxString & config_dir,
                 int icon_size);
    virtual ~ImageDialog();
    void parse_textctrl(wxString & path, wxString & filespec);

    virtual void make_file_vector(const wxString & path_and_filespec)
    {}

    void enable_filespec_controls();
    wxString get_full_path(const wxTreeItemId  & id);
    void create_panels(const vector<wxString> & files);	
    wxString get_filespec () const;
    int ShowModal();
    bool get_icon(wxImage * image, const wxString & full_path);
    bool get_original_image(wxImage * image, const wxString & full_path, bool & map_changed);
    void iconize_image( wxImage * image, int max_size) const;
    wxString generate_filename(const wxString & full_path) const ;
    void clear_preview();
    void process_ok();
    int max_length_of_labels() const ;
    int max_height_of_labels() const ;
    void show_preview_info(int h, int w, unsigned long size);
    void hide_preview_info();
    void clear_preview_info();

    const vector<wxString> & get_temp_selections() const
    {
        return temp_selections;
    }


    void clear_panels()
    {
        temp_selections.clear();
        scroll_page->clear_panels();
    }

    const vector<wxString> &  get_image_files()
    {
        return selected_files;
    }

    const wxString text_box_value() const
    {
        return enter_box->GetValue();
    }

    void clear_text_box()
    {
        enter_box->SetValue(wxEmptyString);
    }


protected:
    wxTreeCtrl   * tree;
    void set_tree_size_and_position();

private:
    wxTextCtrl   * enter_box;
    wxStaticText * preview_label;
    wxStaticText * filter_label;
    wxStaticText * file_label;	
    wxButton     * ok_button;
    wxButton     * cancel_button;
    PreviewPanel * preview;
    wxChoice     * filter;
    FileCanvas   * scroll_page;

    wxPanel      * preview_info;
    wxStaticText * preview_info_label;
    wxStaticText * preview_info_header;

    const wxString config_dir;
    const wxString map_filename;

    vector <wxString> temp_selections;
    vector <wxString> selected_files;


    map <wxString, wxString> icon_files;
    int config_icon_size;
    bool map_changed;


    wxString filespec_choices[CHOICE_SIZE];
    wxString filespec_filters[CHOICE_SIZE];

    void read_map_from_file();	
    void write_map_to_file() const;
    void init_filespec_choices();
    bool iconized_image_exists(const wxString & full_path) const;


// ****************************************************************************************************
//   Variables and functions  to be shared between timer event and selection event when building the panel

    vector <wxString> panel_creation_files;
    int panel_Y;
    int panel_X;
    int panel_counter;
    int panels_per_column;
    int max_panel_width;
    wxTimer panel_timer;

public:
    void add_to_panel_creation_files(const wxString & full_path)
    {
        panel_creation_files.push_back(full_path);
    }
    void start_panel_timer();
    void reverse_panel_creation_files();
    void begin_panel_creation();
    void OnTimer();


    DECLARE_EVENT_TABLE()
};

enum
{
    ID_img_canvas = 1,
    ID_enter_box,
    ID_filter,
};


#endif
