Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2005 TightVNC Team. All Rights Reserved. |
| 2 | * |
| 3 | * Developed by Dennis Syrovatsky |
| 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | * |
Peter Åstrand | 7877cd6 | 2009-02-25 16:15:48 +0000 | [diff] [blame] | 20 | * |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | // -=- FTListView.cxx |
| 25 | |
| 26 | #include <vncviewer/FTListView.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | using namespace rfb::win32; |
| 30 | |
| 31 | FTListView::FTListView(HWND hListView) |
| 32 | { |
| 33 | m_bInitialized = false; |
| 34 | m_hListView = hListView; |
| 35 | m_fileInfo.free(); |
| 36 | } |
| 37 | |
| 38 | FTListView::~FTListView() |
| 39 | { |
| 40 | m_fileInfo.free(); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | bool |
| 45 | FTListView::initialize(HINSTANCE hInst) |
| 46 | { |
| 47 | if (m_bInitialized) return false; |
| 48 | |
| 49 | initImageList(hInst); |
| 50 | |
| 51 | RECT Rect; |
| 52 | GetClientRect(m_hListView, &Rect); |
| 53 | Rect.right -= GetSystemMetrics(SM_CXHSCROLL); |
| 54 | int xwidth0 = (int) (0.35 * Rect.right); |
| 55 | int xwidth1 = (int) (0.22 * Rect.right); |
| 56 | int xwidth2 = (int) (0.43 * Rect.right); |
| 57 | |
| 58 | addColumn("Name", 0, xwidth0, LVCFMT_LEFT); |
| 59 | addColumn("Size", 1, xwidth1, LVCFMT_RIGHT); |
| 60 | addColumn("Data", 2, xwidth2, LVCFMT_LEFT); |
| 61 | |
| 62 | ListView_SetExtendedListViewStyleEx(m_hListView, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | FTListView::onGetDispInfo(NMLVDISPINFO *pDI) |
| 69 | { |
| 70 | if (m_fileInfo.getFlagsAt(pDI->item.iItem) & FT_ATTR_DIR) { |
| 71 | pDI->item.iImage = 0; |
| 72 | } else { |
| 73 | pDI->item.iImage = 1; |
| 74 | } |
| 75 | |
| 76 | switch (pDI->item.iSubItem) |
| 77 | { |
| 78 | case 0: |
| 79 | pDI->item.pszText = m_fileInfo.getNameAt(pDI->item.iItem); |
| 80 | break; |
| 81 | case 1: |
| 82 | { |
| 83 | unsigned int flags = m_fileInfo.getFlagsAt(pDI->item.iItem); |
| 84 | switch(flags & 0x000000FF) |
| 85 | { |
| 86 | case FT_ATTR_FILE: |
| 87 | { |
| 88 | char buf[32]; |
| 89 | unsigned int size = m_fileInfo.getSizeAt(pDI->item.iItem); |
| 90 | sprintf(buf, "%u", size); |
| 91 | pDI->item.pszText = buf; |
| 92 | } |
| 93 | break; |
| 94 | case FT_ATTR_DIR: |
| 95 | pDI->item.pszText = ""; |
| 96 | break; |
| 97 | default: |
| 98 | pDI->item.pszText = "Unspecified"; |
| 99 | } |
| 100 | } |
| 101 | break; |
| 102 | case 2: |
| 103 | { |
| 104 | unsigned int data = m_fileInfo.getDataAt(pDI->item.iItem); |
| 105 | if (data == 0) { |
| 106 | pDI->item.pszText = "Unspecified"; |
| 107 | } else { |
| 108 | FILETIME ft; |
| 109 | FolderManager fm; |
| 110 | fm.getFiletime(data, &ft); |
| 111 | |
| 112 | SYSTEMTIME st; |
| 113 | FileTimeToSystemTime(&ft, &st); |
| 114 | |
| 115 | char pDateTimeStr[1024]; |
| 116 | char timeFmt[128]; |
| 117 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, timeFmt, 128); |
| 118 | char dateFmt[128]; |
| 119 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, dateFmt, 128); |
| 120 | |
| 121 | char timeStr[128]; |
| 122 | GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, timeFmt, timeStr, 128); |
| 123 | char dateStr[128]; |
| 124 | GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, dateFmt, dateStr, 128); |
| 125 | |
| 126 | sprintf(pDateTimeStr, "%s %s", dateStr, timeStr); |
| 127 | pDI->item.pszText = pDateTimeStr; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | FTListView::addItems(FileInfo *pFI) |
| 138 | { |
| 139 | m_fileInfo.add(pFI); |
| 140 | LVITEM LVItem; |
| 141 | LVItem.mask = LVIF_TEXT | LVIF_STATE | LVIF_IMAGE; |
| 142 | LVItem.state = 0; |
| 143 | LVItem.stateMask = 0; |
| 144 | for (unsigned int i = 0; i < m_fileInfo.getNumEntries(); i++) { |
| 145 | LVItem.iItem = i; |
| 146 | LVItem.iSubItem = 0; |
| 147 | LVItem.iImage = I_IMAGECALLBACK; |
| 148 | LVItem.pszText = LPSTR_TEXTCALLBACK; |
| 149 | ListView_InsertItem(m_hListView, &LVItem); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | FTListView::deleteAllItems() |
| 155 | { |
| 156 | ListView_DeleteAllItems(m_hListView); |
| 157 | m_fileInfo.free(); |
| 158 | } |
| 159 | |
| 160 | char * |
| 161 | FTListView::getActivateItemName(LPNMITEMACTIVATE lpnmia) |
| 162 | { |
| 163 | return m_fileInfo.getNameAt(lpnmia->iItem); |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | FTListView::getSelectedItems(FileInfo *pFI) |
| 168 | { |
| 169 | int selCount = ListView_GetSelectedCount(m_hListView); |
| 170 | int selItem = ListView_GetSelectionMark(m_hListView); |
| 171 | if ((selCount < 1) || (selItem < 0)) return -1; |
| 172 | |
| 173 | selItem = -1; |
| 174 | selItem = ListView_GetNextItem(m_hListView, selItem, LVNI_SELECTED); |
| 175 | do { |
| 176 | pFI->add(m_fileInfo.getFullDataAt(selItem)); |
| 177 | selItem = ListView_GetNextItem(m_hListView, selItem, LVNI_SELECTED); |
| 178 | } while (selItem >= 0); |
| 179 | |
| 180 | return selCount; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | FTListView::initImageList(HINSTANCE hInst) |
| 185 | { |
| 186 | m_hImageList = ImageList_Create(16, 16, ILC_MASK, 2, 2); |
| 187 | |
| 188 | HICON hiconItem = LoadIcon(hInst, MAKEINTRESOURCE(IDI_FTDIR)); |
| 189 | ImageList_AddIcon(m_hImageList, hiconItem); |
| 190 | DestroyIcon(hiconItem); |
| 191 | |
| 192 | hiconItem = LoadIcon(hInst, MAKEINTRESOURCE(IDI_FTFILE)); |
| 193 | ImageList_AddIcon(m_hImageList, hiconItem); |
| 194 | DestroyIcon(hiconItem); |
| 195 | |
| 196 | ListView_SetImageList(m_hListView, m_hImageList, LVSIL_SMALL); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | FTListView::addColumn(char *iText, int iOrder, int xWidth, int alignFmt) |
| 201 | { |
| 202 | LVCOLUMN lvc; |
| 203 | lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVCF_ORDER; |
| 204 | lvc.fmt = alignFmt; |
| 205 | lvc.iSubItem = iOrder; |
| 206 | lvc.pszText = iText; |
| 207 | lvc.cchTextMax = 32; |
| 208 | lvc.cx = xWidth; |
| 209 | lvc.iOrder = iOrder; |
| 210 | ListView_InsertColumn(m_hListView, iOrder, &lvc); |
| 211 | } |