+#include "wx/tokenzr.h"
+#include "wx/mimetype.h"
+#include "wx/image.h"
+#include "wx/module.h"
+#include "wx/config.h"
+#include "wx/imaglist.h"
+#include "wx/dir.h"
+#include "wx/artprov.h"
+
+#if wxUSE_TOOLTIPS
+ #include "wx/tooltip.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef __UNIX__
+ #include <dirent.h>
+ #include <pwd.h>
+ #ifndef __VMS
+ # include <grp.h>
+ #endif
+#endif
+
+#ifdef __WATCOMC__
+ #include <direct.h>
+#endif
+
+# include <time.h>
+#include <unistd.h>
+
+//-----------------------------------------------------------------------------
+// wxFileData
+//-----------------------------------------------------------------------------
+
+class wxFileData : public wxObject
+{
+public:
+ wxFileData() { }
+ wxFileData( const wxString &name, const wxString &fname );
+ wxString GetName() const;
+ wxString GetFullName() const;
+ wxString GetHint() const;
+ wxString GetEntry( int num );
+ bool IsDir();
+ bool IsLink();
+ bool IsExe();
+ long GetSize();
+ void MakeItem( wxListItem &item );
+ void SetNewName( const wxString &name, const wxString &fname );
+
+private:
+ wxString m_name;
+ wxString m_fileName;
+ long m_size;
+ int m_hour;
+ int m_minute;
+ int m_year;
+ int m_month;
+ int m_day;
+ wxString m_permissions;
+ bool m_isDir;
+ bool m_isLink;
+ bool m_isExe;
+
+ DECLARE_DYNAMIC_CLASS(wxFileData);
+};
+
+//-----------------------------------------------------------------------------
+// wxFileCtrl
+//-----------------------------------------------------------------------------
+
+class wxFileCtrl : public wxListCtrl
+{
+public:
+ wxFileCtrl();
+ wxFileCtrl( wxWindow *win,
+ wxWindowID id,
+ const wxString &dirName,
+ const wxString &wild,
+ const wxPoint &pos = wxDefaultPosition,
+ const wxSize &size = wxDefaultSize,
+ long style = wxLC_LIST,
+ const wxValidator &validator = wxDefaultValidator,
+ const wxString &name = wxT("filelist") );
+ void ChangeToListMode();
+ void ChangeToReportMode();
+ void ChangeToIconMode();
+ void ShowHidden( bool show = TRUE );
+ long Add( wxFileData *fd, wxListItem &item );
+ void UpdateFiles();
+ virtual void StatusbarText( wxChar *WXUNUSED(text) ) {};
+ void MakeDir();
+ void GoToParentDir();
+ void GoToHomeDir();
+ void GoToDir( const wxString &dir );
+ void SetWild( const wxString &wild );
+ void GetDir( wxString &dir );
+ void OnListDeleteItem( wxListEvent &event );
+ void OnListDeleteAllItems( wxListEvent &event );
+ void OnListEndLabelEdit( wxListEvent &event );
+
+ // Associate commonly used UI controls with wxFileCtrl so that they can be
+ // disabled when they cannot be used (e.g. can't go to parent directory
+ // if wxFileCtrl already is in the root dir):
+ void SetGoToParentControl(wxWindow *ctrl) { m_goToParentControl = ctrl; }
+ void SetNewDirControl(wxWindow *ctrl) { m_newDirControl = ctrl; }
+
+private:
+ wxString m_dirName;
+ bool m_showHidden;
+ wxString m_wild;
+
+ wxWindow *m_goToParentControl;
+ wxWindow *m_newDirControl;
+
+ DECLARE_DYNAMIC_CLASS(wxFileCtrl);
+ DECLARE_EVENT_TABLE()
+};
+
+// ----------------------------------------------------------------------------
+// private classes - icons list management
+// ----------------------------------------------------------------------------
+
+class wxFileIconEntry : public wxObject
+{
+public:
+ wxFileIconEntry(int i) { id = i; }
+
+ int id;
+};
+
+
+class wxFileIconsTable
+{
+public:
+ wxFileIconsTable();
+
+ int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString);
+ wxImageList *GetImageList() { return &m_ImageList; }
+
+protected:
+ wxImageList m_ImageList;
+ wxHashTable m_HashTable;
+};
+
+static wxFileIconsTable *g_IconsTable = NULL;
+
+#define FI_FOLDER 0
+#define FI_UNKNOWN 1
+#define FI_EXECUTABLE 2
+
+wxFileIconsTable::wxFileIconsTable() :
+ m_ImageList(16, 16),
+ m_HashTable(wxKEY_STRING)
+{
+ m_HashTable.DeleteContents(TRUE);
+ // FI_FOLDER:
+ m_ImageList.Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_CMN_DIALOG));
+ // FI_UNKNOWN:
+ m_ImageList.Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_CMN_DIALOG));
+ // FI_EXECUTABLE:
+ if (GetIconID(wxEmptyString, _T("application/x-executable")) == FI_UNKNOWN)
+ {
+ m_ImageList.Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_CMN_DIALOG));
+ m_HashTable.Delete(_T("exe"));
+ m_HashTable.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE));
+ }
+ /* else put into list by GetIconID
+ (KDE defines application/x-executable for *.exe and has nice icon)
+ */
+}
+
+
+
+#if wxUSE_MIMETYPE
+// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
+// one icon and we won't resize it
+
+static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
+{
+ wxImage small(16, 16);
+ unsigned char *p1, *p2, *ps;
+ unsigned char mr = img.GetMaskRed(),
+ mg = img.GetMaskGreen(),
+ mb = img.GetMaskBlue();
+
+ unsigned x, y;
+ unsigned sr, sg, sb, smask;
+
+ p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = small.GetData();
+ small.SetMaskColour(mr, mr, mr);
+
+ for (y = 0; y < 16; y++)
+ {
+ for (x = 0; x < 16; x++)
+ {
+ sr = sg = sb = smask = 0;
+ if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
+ sr += p1[0], sg += p1[1], sb += p1[2];
+ else smask++;
+ p1 += 3;
+ if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
+ sr += p1[0], sg += p1[1], sb += p1[2];
+ else smask++;
+ p1 += 3;
+ if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
+ sr += p2[0], sg += p2[1], sb += p2[2];
+ else smask++;
+ p2 += 3;
+ if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
+ sr += p2[0], sg += p2[1], sb += p2[2];
+ else smask++;
+ p2 += 3;
+
+ if (smask > 2)
+ ps[0] = ps[1] = ps[2] = mr;
+ else
+ ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
+ ps += 3;
+ }
+ p1 += 32 * 3, p2 += 32 * 3;
+ }
+
+ return wxBitmap(small);
+}
+
+// finds empty borders and return non-empty area of image:
+static wxImage CutEmptyBorders(const wxImage& img)
+{
+ unsigned char mr = img.GetMaskRed(),
+ mg = img.GetMaskGreen(),
+ mb = img.GetMaskBlue();
+ unsigned char *dt = img.GetData(), *dttmp;
+ unsigned w = img.GetWidth(), h = img.GetHeight();
+
+ unsigned top, bottom, left, right, i;
+ bool empt;
+
+#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
+#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
+
+ for (empt = TRUE, top = 0; empt && top < h; top++)
+ {
+ MK_DTTMP(0, top);
+ for (i = 0; i < w; i++, dttmp+=3)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--)
+ {
+ MK_DTTMP(0, bottom);
+ for (i = 0; i < w; i++, dttmp+=3)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = TRUE, left = 0; empt && left < w; left++)
+ {
+ MK_DTTMP(left, 0);
+ for (i = 0; i < h; i++, dttmp+=3*w)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = TRUE, right = w-1; empt && right > left; right--)
+ {
+ MK_DTTMP(right, 0);
+ for (i = 0; i < h; i++, dttmp+=3*w)
+ NOEMPTY_PIX(empt)
+ }
+ top--, left--, bottom++, right++;
+
+ return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
+}
+#endif // wxUSE_MIMETYPE
+
+
+
+int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
+{
+#if wxUSE_MIMETYPE
+ if (!extension.IsEmpty())
+ {
+ wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable.Get(extension);
+ if (entry) return (entry -> id);
+ }
+
+ wxFileType *ft = (mime.IsEmpty()) ?
+ wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
+ wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
+ wxIcon ic;
+ if (ft == NULL || (!ft -> GetIcon(&ic)) || (!ic.Ok()))
+ {
+ int newid = FI_UNKNOWN;
+ m_HashTable.Put(extension, new wxFileIconEntry(newid));
+ return newid;
+ }
+ wxImage img = ic.ConvertToImage();
+ delete ft;
+
+ int id = m_ImageList.GetImageCount();
+ if (img.GetWidth() == 16 && img.GetHeight() == 16)
+ m_ImageList.Add(wxBitmap(img));
+ else
+ {
+ if (img.GetWidth() != 32 || img.GetHeight() != 32)
+ m_ImageList.Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32)));
+ else
+ m_ImageList.Add(CreateAntialiasedBitmap(img));
+ }
+ m_HashTable.Put(extension, new wxFileIconEntry(id));
+ return id;
+
+#else // !wxUSE_MIMETYPE
+
+ if (extension == wxT("exe"))
+ return FI_EXECUTABLE;
+ else
+ return FI_UNKNOWN;
+#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
+}
+
+
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------