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 | * |
| 20 | * TightVNC distribution homepage on the Web: http://www.tightvnc.com/ |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | // -=- FTBrpwseDlg.cxx |
| 25 | |
| 26 | #include <vncviewer/FTBrowseDlg.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | using namespace rfb::win32; |
| 30 | |
| 31 | FTBrowseDlg::FTBrowseDlg(FTDialog *pFTDlg) |
| 32 | { |
| 33 | m_pFTDlg = pFTDlg; |
| 34 | m_hwndDlg = NULL; |
| 35 | m_hwndTree = NULL; |
| 36 | m_hParentItem = NULL; |
| 37 | } |
| 38 | |
| 39 | FTBrowseDlg::~FTBrowseDlg() |
| 40 | { |
| 41 | destroy(); |
| 42 | } |
| 43 | |
| 44 | bool |
| 45 | FTBrowseDlg::create() |
| 46 | { |
| 47 | m_hwndDlg = CreateDialogParam(GetModuleHandle(0), MAKEINTRESOURCE(IDD_FTBROWSE), |
| 48 | m_pFTDlg->getWndHandle(), (DLGPROC) FTBrowseDlgProc, |
| 49 | (LONG) this); |
| 50 | |
| 51 | if (m_hwndDlg == NULL) return false; |
| 52 | |
| 53 | m_hwndTree = GetDlgItem(m_hwndDlg, IDC_FTBROWSETREE); |
| 54 | |
| 55 | ShowWindow(m_hwndDlg, SW_SHOW); |
| 56 | UpdateWindow(m_hwndDlg); |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | FTBrowseDlg::destroy() |
| 63 | { |
| 64 | EndDialog(m_hwndDlg, 0); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | FTBrowseDlg::addItems(FileInfo *pFI) |
| 69 | { |
| 70 | TVITEM tvi; |
| 71 | TVINSERTSTRUCT tvins; |
| 72 | |
| 73 | if (pFI->getNumEntries() <= 0) return; |
| 74 | |
| 75 | for (unsigned int i = 0; i < pFI->getNumEntries(); i++) |
| 76 | { |
| 77 | tvi.mask = TVIF_TEXT; |
| 78 | tvi.pszText = pFI->getNameAt(i);; |
| 79 | tvins.hParent = m_hParentItem; |
| 80 | tvins.item = tvi; |
| 81 | tvins.hParent = TreeView_InsertItem(m_hwndTree, &tvins); |
| 82 | TreeView_InsertItem(m_hwndTree, &tvins); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | char * |
| 87 | FTBrowseDlg::getTVPath(HTREEITEM hTItem) |
| 88 | { |
| 89 | char path[FT_FILENAME_SIZE]; |
| 90 | char szText[FT_FILENAME_SIZE]; |
| 91 | |
| 92 | TVITEM tvi; |
| 93 | path[0] = '\0'; |
| 94 | |
| 95 | do { |
| 96 | tvi.mask = TVIF_TEXT | TVIF_HANDLE; |
| 97 | tvi.hItem = hTItem; |
| 98 | tvi.pszText = szText; |
| 99 | tvi.cchTextMax = FT_FILENAME_SIZE; |
| 100 | TreeView_GetItem(m_hwndTree, &tvi); |
| 101 | sprintf(path, "%s\\%s", path, tvi.pszText); |
| 102 | hTItem = TreeView_GetParent(m_hwndTree, hTItem); |
| 103 | } while(hTItem != NULL); |
| 104 | |
| 105 | return pathInvert(path); |
| 106 | } |
| 107 | |
| 108 | char * |
| 109 | FTBrowseDlg::pathInvert(char *pPath) |
| 110 | { |
| 111 | int len = strlen(pPath); |
| 112 | m_szPath[0] = '\0'; |
| 113 | char *pos = NULL; |
| 114 | |
| 115 | while ((pos = strrchr(pPath, '\\')) != NULL) { |
| 116 | if (strlen(m_szPath) == 0) { |
| 117 | strcpy(m_szPath, (pos + 1)); |
| 118 | } else { |
| 119 | sprintf(m_szPath, "%s\\%s", m_szPath, (pos + 1)); |
| 120 | } |
| 121 | *pos = '\0'; |
| 122 | } |
| 123 | |
| 124 | m_szPath[len] = '\0'; |
| 125 | return m_szPath; |
| 126 | } |
| 127 | |
| 128 | char * |
| 129 | FTBrowseDlg::getPath() |
| 130 | { |
| 131 | GetDlgItemText(m_hwndDlg, IDC_FTBROWSEPATH, m_szPath, FT_FILENAME_SIZE); |
| 132 | return m_szPath; |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | FTBrowseDlg::deleteChildItems() |
| 137 | { |
| 138 | while (TreeView_GetChild(m_hwndTree, m_hParentItem) != NULL) { |
| 139 | TreeView_DeleteItem(m_hwndTree, TreeView_GetChild(m_hwndTree, m_hParentItem)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | BOOL CALLBACK |
| 144 | FTBrowseDlg::FTBrowseDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 145 | { |
| 146 | FTBrowseDlg *_this = (FTBrowseDlg *) GetWindowLong(hwnd, GWL_USERDATA); |
| 147 | switch (uMsg) |
| 148 | { |
| 149 | case WM_INITDIALOG: |
| 150 | { |
| 151 | SetWindowLong(hwnd, GWL_USERDATA, lParam); |
| 152 | return FALSE; |
| 153 | } |
| 154 | break; |
| 155 | case WM_COMMAND: |
| 156 | { |
| 157 | switch (LOWORD(wParam)) |
| 158 | { |
| 159 | case IDOK: |
| 160 | _this->m_pFTDlg->onEndBrowseDlg(true); |
| 161 | return FALSE; |
| 162 | case IDCANCEL: |
| 163 | _this->m_pFTDlg->onEndBrowseDlg(false); |
| 164 | return FALSE; |
| 165 | } |
| 166 | } |
| 167 | break; |
| 168 | case WM_NOTIFY: |
| 169 | switch (LOWORD(wParam)) |
| 170 | { |
| 171 | case IDC_FTBROWSETREE: |
| 172 | switch (((LPNMHDR) lParam)->code) |
| 173 | { |
| 174 | case TVN_SELCHANGED: |
| 175 | SetDlgItemText(hwnd, IDC_FTBROWSEPATH, _this->getTVPath(((NMTREEVIEW *) lParam)->itemNew.hItem)); |
| 176 | return FALSE; |
| 177 | // case TVN_ITEMEXPANDING: |
| 178 | case TVN_ITEMEXPANDED: |
| 179 | { |
| 180 | NMTREEVIEW *nmCode = (NMTREEVIEW *) lParam; |
| 181 | if (nmCode->action == 2) { |
| 182 | _this->m_hParentItem = nmCode->itemNew.hItem; |
| 183 | _this->deleteChildItems(); |
| 184 | _this->m_pFTDlg->getBrowseItems(_this->getTVPath(_this->m_hParentItem)); |
| 185 | } |
| 186 | } |
| 187 | return FALSE; |
| 188 | } |
| 189 | break; |
| 190 | case WM_CLOSE: |
| 191 | _this->m_pFTDlg->onEndBrowseDlg(false); |
| 192 | return FALSE; |
| 193 | } |
| 194 | } |
| 195 | return 0; |
| 196 | } |