"Fossies" - the Fresh Open Source Software Archive

Member "Celestia-1.6.2.2/src/celestia/winbookmarks.cpp" (12 Jan 2021, 31931 Bytes) of package /linux/misc/Celestia-1.6.2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "winbookmarks.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.6.2.1_vs_1.6.2.2.

    1 // winbookmarks.cpp
    2 // 
    3 // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
    4 //
    5 // Miscellaneous utilities for Locations UI implementation.
    6 //
    7 // This program is free software; you can redistribute it and/or
    8 // modify it under the terms of the GNU General Public License
    9 // as published by the Free Software Foundation; either version 2
   10 // of the License, or (at your option) any later version.
   11 
   12 #include "winbookmarks.h"
   13 #include "res/resource.h"
   14 #include <celutil/winutil.h>
   15 #include <iostream>
   16 
   17 using namespace std;
   18 
   19 bool dragging;
   20 HTREEITEM hDragItem;
   21 HTREEITEM hDropTargetItem;
   22 POINT dragPos;
   23 
   24 static const unsigned int StdItemMask = (TVIF_TEXT | TVIF_PARAM |
   25                                          TVIF_IMAGE | TVIF_SELECTEDIMAGE);
   26 
   27 static bool isTopLevel(const FavoritesEntry* fav)
   28 {
   29     return fav->parentFolder == "";
   30 }
   31 
   32 
   33 HTREEITEM PopulateBookmarksTree(HWND hTree, CelestiaCore* appCore, HINSTANCE appInstance)
   34 {
   35     //First create an image list for the icons in the control
   36     HIMAGELIST himlIcons;
   37     HICON hIcon;
   38     HTREEITEM hParent=NULL, hParentItem;
   39 
   40     //Create a masked image list large enough to hold the icons. 
   41     himlIcons = ImageList_Create(16, 16, ILC_MASK, 3, 0);
   42  
   43     // Load the icon resources, and add the icons to the image list.
   44     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_CLOSEDFOLDER)); 
   45     ImageList_AddIcon(himlIcons, hIcon);
   46     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_OPENFOLDER)); 
   47     ImageList_AddIcon(himlIcons, hIcon);
   48     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_ROOTFOLDER)); 
   49     ImageList_AddIcon(himlIcons, hIcon);
   50     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_BOOKMARK)); 
   51     ImageList_AddIcon(himlIcons, hIcon);
   52 
   53     // Associate the image list with the tree-view control.
   54     TreeView_SetImageList(hTree, himlIcons, TVSIL_NORMAL);
   55 
   56     FavoritesList* favorites = appCore->getFavorites();
   57     if (favorites != NULL)
   58     {
   59         // Create a subtree item called "Bookmarks"
   60         TVINSERTSTRUCTW tvis;
   61 
   62         tvis.hParent = TVI_ROOT;
   63         tvis.hInsertAfter = TVI_LAST;
   64         tvis.item.mask = StdItemMask;
   65         tvis.item.pszText = L"Bookmarks";
   66         tvis.item.lParam = NULL;
   67         tvis.item.iImage = 2;
   68         tvis.item.iSelectedImage = 2;
   69         if (hParent = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis))
   70         {
   71             FavoritesList::iterator iter = favorites->begin();
   72             while (iter != favorites->end())
   73             {
   74                 TVINSERTSTRUCTW tvis;
   75                 FavoritesEntry* fav = *iter;
   76 
   77                 // Is this a folder?
   78                 if (fav->isFolder)
   79                 {
   80                     // Create a subtree item
   81                     tvis.hParent = hParent;
   82                     tvis.hInsertAfter = TVI_LAST;
   83                     tvis.item.mask = StdItemMask;
   84                     wstring itemName = CurrentCPToWide(fav->name);
   85                     tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
   86                     tvis.item.lParam = reinterpret_cast<LPARAM>(fav);
   87                     tvis.item.iImage = 0;
   88                     tvis.item.iSelectedImage = 1;
   89 
   90                     if (hParentItem = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis))
   91                     {
   92                         FavoritesList::iterator subIter = favorites->begin();
   93                         while (subIter != favorites->end())
   94                         {
   95                             FavoritesEntry* child = *subIter;
   96 
   97                             // See if this entry is a child
   98                             if (!child->isFolder && child->parentFolder == fav->name)
   99                             {
  100                                 // Add items to sub tree
  101                                 tvis.hParent = hParentItem;
  102                                 tvis.hInsertAfter = TVI_LAST;
  103                                 tvis.item.mask = StdItemMask;
  104                                 wstring itemName = CurrentCPToWide(child->name);
  105                                 tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
  106                                 tvis.item.lParam = reinterpret_cast<LPARAM>(child);
  107                                 tvis.item.iImage = 3;
  108                                 tvis.item.iSelectedImage = 3;
  109                                 SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
  110                             }
  111                             subIter++;
  112                         }
  113 
  114                         // Expand each folder to display bookmark items
  115                         TreeView_Expand(hTree, hParentItem, TVE_EXPAND);
  116                     }
  117                 }
  118                 else if (isTopLevel(fav))
  119                 {
  120                     // Add item to root "Bookmarks"
  121                     tvis.hParent = hParent;
  122                     tvis.hInsertAfter = TVI_LAST;
  123                     tvis.item.mask = StdItemMask;
  124                     wstring itemName = CurrentCPToWide((*iter)->name);
  125                     tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
  126                     tvis.item.lParam = reinterpret_cast<LPARAM>(fav);
  127                     tvis.item.iImage = 3;
  128                     tvis.item.iSelectedImage = 3;
  129                     SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
  130                 }
  131 
  132                 iter++;
  133             }
  134         }
  135     }
  136 
  137     dragging = false;
  138 
  139     return hParent;
  140 }
  141 
  142 
  143 HTREEITEM PopulateBookmarkFolders(HWND hTree, CelestiaCore* appCore, HINSTANCE appInstance)
  144 {
  145     // First create an image list for the icons in the control
  146     HTREEITEM hParent=NULL;
  147     HIMAGELIST himlIcons;
  148     HICON hIcon;
  149 
  150     //Create a masked image list large enough to hold the icons. 
  151     himlIcons = ImageList_Create(16, 16, ILC_MASK, 3, 0);
  152  
  153     // Load the icon resources, and add the icons to the image list.
  154     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_CLOSEDFOLDER)); 
  155     ImageList_AddIcon(himlIcons, hIcon);
  156     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_OPENFOLDER)); 
  157     ImageList_AddIcon(himlIcons, hIcon);
  158     hIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_ROOTFOLDER)); 
  159     ImageList_AddIcon(himlIcons, hIcon);
  160 
  161     // Associate the image list with the tree-view control.
  162     TreeView_SetImageList(hTree, himlIcons, TVSIL_NORMAL);
  163 
  164     FavoritesList* favorites = appCore->getFavorites();
  165     if (favorites != NULL)
  166     {
  167         // Create a subtree item called "Bookmarks"
  168         TVINSERTSTRUCTW tvis;
  169 
  170         tvis.hParent = TVI_ROOT;
  171         tvis.hInsertAfter = TVI_LAST;
  172         tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
  173         tvis.item.pszText = L"Bookmarks";
  174         tvis.item.lParam = 0;
  175         tvis.item.iImage = 2;
  176         tvis.item.iSelectedImage = 2;
  177         if (hParent = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis))
  178         {
  179             FavoritesList::iterator iter = favorites->begin();
  180             while (iter != favorites->end())
  181             {
  182                 FavoritesEntry* fav = *iter;
  183 
  184                 if (fav->isFolder)
  185                 {
  186                     // Create a subtree item for the folder
  187                     TVINSERTSTRUCTW tvis;
  188                     tvis.hParent = hParent;
  189                     tvis.hInsertAfter = TVI_LAST;
  190                     tvis.item.mask = StdItemMask;
  191                     wstring itemName = CurrentCPToWide(fav->name);
  192                     tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
  193                     tvis.item.lParam = reinterpret_cast<LPARAM>(fav);
  194                     tvis.item.iImage = 0;
  195                     tvis.item.iSelectedImage = 1;
  196                     SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
  197                 }
  198 
  199                 iter++;
  200             }
  201 
  202             // Select "Bookmarks" folder
  203             TreeView_SelectItem(hTree, hParent);
  204         }
  205     }
  206 
  207     return hParent;
  208 }
  209 
  210 
  211 void BuildFavoritesMenu(HMENU menuBar,
  212                         CelestiaCore* appCore,
  213                         HINSTANCE appInstance,
  214                         ODMenu* odMenu)
  215 {
  216     // Add item to bookmarks menu
  217     int numStaticItems = 2; // The number of items defined in the .rc file.
  218 
  219     // Ugly dependence on menu defined in celestia.rc; this needs to change
  220     // if the bookmarks menu is moved, or if another item is added to the
  221     // menu bar.
  222     // TODO: Fix this dependency
  223     UINT bookmarksMenuPosition = 5;
  224 
  225     FavoritesList* favorites = appCore->getFavorites();
  226     if (favorites == NULL)
  227         return;
  228 
  229     MENUITEMINFO menuInfo;
  230     menuInfo.cbSize = sizeof(MENUITEMINFO);
  231     menuInfo.fMask = MIIM_SUBMENU;
  232     if (GetMenuItemInfo(menuBar, bookmarksMenuPosition, TRUE, &menuInfo))
  233     {
  234         HMENU bookmarksMenu = menuInfo.hSubMenu;
  235 
  236         // First, tear down existing menu beyond separator.
  237         while (DeleteMenu(bookmarksMenu, numStaticItems, MF_BYPOSITION))
  238             odMenu->DeleteItem(bookmarksMenu, numStaticItems);
  239 
  240         // Don't continue if there are no items in favorites
  241         if (favorites->size() == 0)
  242             return;
  243 
  244         // Insert separator
  245         menuInfo.cbSize = sizeof MENUITEMINFO;
  246         menuInfo.fMask = MIIM_TYPE | MIIM_STATE;
  247         menuInfo.fType = MFT_SEPARATOR;
  248         menuInfo.fState = MFS_UNHILITE;
  249         if (InsertMenuItem(bookmarksMenu, numStaticItems, TRUE, &menuInfo))
  250         {
  251             odMenu->AddItem(bookmarksMenu, numStaticItems);
  252             numStaticItems++;
  253         }
  254 
  255         // Add folders and their sub items
  256         int rootMenuIndex = numStaticItems;
  257         int rootResIndex = 0;
  258         FavoritesList::iterator iter = favorites->begin();
  259         while (iter != favorites->end())
  260         {
  261             FavoritesEntry* fav = *iter;
  262 
  263             // Is this a folder?
  264             if (fav->isFolder)
  265             {
  266                 // Create a submenu
  267                 HMENU subMenu;
  268                 if (subMenu = CreatePopupMenu())
  269                 {
  270                     // Create a menu item that displays a popup sub menu
  271                     menuInfo.cbSize = sizeof MENUITEMINFO;
  272                     menuInfo.fMask = MIIM_SUBMENU | MIIM_TYPE | MIIM_ID;
  273                     menuInfo.fType = MFT_STRING;
  274                     menuInfo.wID = ID_BOOKMARKS_FIRSTBOOKMARK + rootResIndex;
  275                     menuInfo.hSubMenu = subMenu;
  276                     menuInfo.dwTypeData = const_cast<char*>(fav->name.c_str());
  277 
  278                     if (InsertMenuItem(bookmarksMenu,
  279                                        rootMenuIndex,
  280                                        TRUE,
  281                                        &menuInfo))
  282                     {
  283                         odMenu->AddItem(bookmarksMenu, rootMenuIndex);
  284                         odMenu->SetItemImage(appInstance, menuInfo.wID,
  285                                              IDB_FOLDERCLOSED);
  286                         rootMenuIndex++;
  287 
  288                         // Now iterate through all Favorites and add items
  289                         // to this folder where parentFolder == folderName
  290                         int subMenuIndex = 0;
  291                         int childResIndex = 0;
  292                         string folderName = fav->name;
  293 
  294                         for (FavoritesList::iterator childIter = favorites->begin();
  295                              childIter != favorites->end();
  296                              childIter++, childResIndex++)
  297                         {
  298                             FavoritesEntry* child = *childIter;
  299                             if (!child->isFolder &&
  300                                 child->parentFolder == folderName)
  301                             {
  302                                 clog << "  " << child->name << '\n';
  303                                 // Add item to sub menu
  304                                 menuInfo.cbSize = sizeof MENUITEMINFO;
  305                                 menuInfo.fMask = MIIM_TYPE | MIIM_ID;
  306                                 menuInfo.fType = MFT_STRING;
  307                                 menuInfo.wID = ID_BOOKMARKS_FIRSTBOOKMARK + childResIndex;
  308                                 menuInfo.dwTypeData = const_cast<char*>(child->name.c_str());
  309                                 if (InsertMenuItem(subMenu, subMenuIndex, TRUE, &menuInfo))
  310                                 {
  311                                     odMenu->AddItem(subMenu, subMenuIndex);
  312                                     odMenu->SetItemImage(appInstance, menuInfo.wID, IDB_BOOKMARK);
  313                                     subMenuIndex++;
  314                                 }
  315                             }
  316                         }
  317 
  318                         // Add a disabled "(empty)" item if no items
  319                         // were added to sub menu
  320                         if (subMenuIndex == 0)
  321                         {
  322                             menuInfo.cbSize = sizeof MENUITEMINFO;
  323                             menuInfo.fMask = MIIM_TYPE | MIIM_STATE;
  324                             menuInfo.fType = MFT_STRING;
  325                             menuInfo.fState = MFS_DISABLED;
  326                             menuInfo.dwTypeData = "(empty)";
  327                             if (InsertMenuItem(subMenu, subMenuIndex, TRUE, &menuInfo))
  328                             {
  329                                 odMenu->AddItem(subMenu, subMenuIndex);
  330                             }
  331                         }
  332                     }
  333                 }
  334             }
  335 
  336             rootResIndex++;
  337             iter++;
  338         }
  339 
  340         // Add root bookmark items
  341         iter = favorites->begin();
  342         rootResIndex = 0;
  343         while (iter != favorites->end())
  344         {
  345             FavoritesEntry* fav = *iter;
  346 
  347             // Is this a non folder item?
  348             if (!fav->isFolder && isTopLevel(fav))
  349             {
  350                 // Append to bookmarksMenu
  351                 AppendMenu(bookmarksMenu, MF_STRING,
  352                            ID_BOOKMARKS_FIRSTBOOKMARK + rootResIndex,
  353                            const_cast<char*>(fav->name.c_str()));
  354 
  355                 odMenu->AddItem(bookmarksMenu, rootMenuIndex);
  356                 odMenu->SetItemImage(appInstance,
  357                                      ID_BOOKMARKS_FIRSTBOOKMARK + rootResIndex,
  358                                      IDB_BOOKMARK);
  359                 rootMenuIndex++;
  360             }
  361             iter++;
  362             rootResIndex++;
  363         }
  364     }
  365 }
  366 
  367 
  368 void AddNewBookmarkFolderInTree(HWND hTree, CelestiaCore* appCore, char* folderName)
  369 {
  370     // Add new item to bookmark item after other folders but before root items
  371     HTREEITEM hParent, hItem, hInsertAfter;
  372     TVINSERTSTRUCTW tvis;
  373     TVITEMW tvItem;
  374 
  375     hParent = TreeView_GetChild(hTree, TVI_ROOT);
  376     if (hParent)
  377     {
  378         // Find last "folder" in children of hParent
  379         hItem = TreeView_GetChild(hTree, hParent);
  380         while (hItem)
  381         {
  382             // Is this a "folder"
  383             tvItem.hItem = hItem;
  384             tvItem.mask = TVIF_HANDLE | TVIF_PARAM;
  385             if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  386             {
  387                 FavoritesEntry* fav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  388                 if (fav == NULL || fav->isFolder)
  389                     hInsertAfter = hItem;
  390             }
  391             hItem = TreeView_GetNextSibling(hTree, hItem);
  392         }
  393 
  394         FavoritesEntry* folderFav = new FavoritesEntry();
  395         folderFav->isFolder = true;
  396         folderFav->name = folderName;
  397 
  398         FavoritesList* favorites = appCore->getFavorites();
  399         favorites->insert(favorites->end(), folderFav);
  400 
  401         tvis.hParent = hParent;
  402         tvis.hInsertAfter = hInsertAfter;
  403         tvis.item.mask = StdItemMask;
  404         wstring itemName = CurrentCPToWide(folderName);
  405         tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
  406         tvis.item.lParam = reinterpret_cast<LPARAM>(folderFav);
  407         tvis.item.iImage = 2;
  408         tvis.item.iSelectedImage = 1;
  409         if (hItem = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis))
  410         {
  411             // Make sure root tree item is open and newly
  412             // added item is visible.
  413             TreeView_Expand(hTree, hParent, TVE_EXPAND);
  414 
  415             // Select the item
  416             TreeView_SelectItem(hTree, hItem);
  417         }
  418     }
  419 }
  420 
  421 
  422 void SyncTreeFoldersWithFavoriteFolders(HWND hTree, CelestiaCore* appCore)
  423 {
  424     FavoritesList* favorites = appCore->getFavorites();
  425     FavoritesList::iterator iter;
  426     TVITEMW tvItem;
  427     HTREEITEM hItem, hParent;
  428     wchar_t itemName[33];
  429     bool found;
  430 
  431     if (favorites != NULL)
  432     {
  433         // Scan through tree control folders and add any folder that does
  434         // not exist in Favorites.
  435         if (hParent = TreeView_GetChild(hTree, TVI_ROOT))
  436         {
  437             hItem = TreeView_GetChild(hTree, hParent);
  438             do
  439             {
  440                 // Get information on item
  441                 tvItem.hItem = hItem;
  442                 tvItem.mask = TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE;
  443                 tvItem.pszText = itemName;
  444                 tvItem.cchTextMax = sizeof(itemName) / sizeof(wchar_t);
  445                 if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  446                 {
  447                     // Skip non-folders.
  448                     if(tvItem.lParam == 0)
  449                         continue;
  450 
  451                     string name(WideToCurrentCP(itemName));
  452                     if (favorites->size() == 0)
  453                     {
  454                         // Just append the folder
  455                         appCore->addFavoriteFolder(name);
  456                         continue;
  457                     }
  458 
  459                     // Loop through favorites to find item = itemName
  460                     found = false;
  461                     iter = favorites->begin();
  462                     while (iter != favorites->end())
  463                     {
  464                         if ((*iter)->isFolder && (*iter)->name == name)
  465                         {
  466                             found = true;
  467                             break;
  468                         }
  469                         iter++;
  470                     }
  471 
  472                     if (!found)
  473                     {
  474                         // If not found in favorites, add it.
  475                         // We want all folders to appear before root items so this
  476                         // new folder must be inserted after the last item of the 
  477                         // last folder.
  478                         // Locate position of last folder.
  479                         FavoritesList::iterator folderIter = favorites->begin();
  480                         iter = favorites->begin();
  481                         while (iter != favorites->end())
  482                         {
  483                             if ((*iter)->isFolder)
  484                                 folderIter = iter;
  485 
  486                             iter++;
  487                         }
  488                         //Now iterate through items until end of folder found
  489                         folderIter++;
  490                         while (folderIter != favorites->end() && (*folderIter)->parentFolder != "")
  491                             folderIter++;
  492                         
  493                         //Insert item
  494                         appCore->addFavoriteFolder(name, &folderIter);
  495                     }
  496                 }
  497             } while (hItem = TreeView_GetNextSibling(hTree, hItem));
  498         }
  499     }
  500 }
  501 
  502 
  503 void InsertBookmarkInFavorites(HWND hTree, char* name, CelestiaCore* appCore)
  504 {
  505     FavoritesList* favorites = appCore->getFavorites();
  506     TVITEMW tvItem;
  507     HTREEITEM hItem;
  508     wchar_t itemName[33];
  509     string newBookmark(name);
  510 
  511     // SyncTreeFoldersWithFavoriteFolders(hTree, appCore);
  512 
  513     // Determine which tree item (folder) is selected (if any)
  514     hItem = TreeView_GetSelection(hTree);
  515     if (!TreeView_GetParent(hTree, hItem))
  516         hItem = NULL;
  517 
  518     if (hItem)
  519     {
  520         tvItem.hItem = hItem;
  521         tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  522         tvItem.pszText = itemName;
  523         tvItem.cchTextMax = sizeof(itemName) / sizeof(wchar_t);
  524         if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  525         {
  526             FavoritesEntry* fav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  527             if (fav != NULL && fav->isFolder)
  528             {
  529                 appCore->addFavorite(newBookmark, WideToCurrentCP(itemName));
  530             }
  531         }
  532     }
  533     else
  534     {
  535         // Folder not specified, add to end of favorites
  536         appCore->addFavorite(newBookmark, "");
  537     }
  538 }
  539 
  540 
  541 void DeleteBookmarkFromFavorites(HWND hTree, CelestiaCore* appCore)
  542 {
  543     FavoritesList* favorites = appCore->getFavorites();
  544     TVITEMW tvItem;
  545     HTREEITEM hItem;
  546     wchar_t itemName[33];
  547 
  548     hItem = TreeView_GetSelection(hTree);
  549     if (!hItem)
  550         return;
  551 
  552     // Get the selected item text (which is the bookmark name)
  553     tvItem.hItem = hItem;
  554     tvItem.mask = TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE;
  555     tvItem.pszText = itemName;
  556     tvItem.cchTextMax = sizeof(itemName) / sizeof(wchar_t);
  557     if (!SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  558         return;
  559 
  560     FavoritesEntry* fav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  561     if (!fav)
  562         return;
  563 
  564     // Delete the item from the tree view; give up if this fails for some
  565     // reason (it shouldn't . . .)
  566     if (!TreeView_DeleteItem(hTree, hItem))
  567         return;
  568 
  569     // Delete item in favorites, as well as all of it's children
  570     for (int i = favorites->size() - 1; i >= 0; --i)
  571     {
  572         FavoritesEntry *cur = favorites->at(i);
  573         if (cur == fav || (fav->isFolder && cur->parentFolder == WideToCurrentCP(itemName)))
  574         {
  575             favorites->erase(favorites->begin() + i);
  576         }
  577     }
  578 }
  579 
  580 
  581 void RenameBookmarkInFavorites(HWND hTree, char* newName, CelestiaCore* appCore)
  582 {
  583     FavoritesList* favorites = appCore->getFavorites();
  584     TVITEMW tvItem;
  585     HTREEITEM hItem;
  586     char itemName[33];
  587 
  588     // First get the selected item
  589     hItem = TreeView_GetSelection(hTree);
  590     if (!hItem)
  591         return;
  592 
  593     // Get the item text 
  594     tvItem.hItem = hItem;
  595     tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  596     wstring name = CurrentCPToWide(itemName);
  597     tvItem.pszText = const_cast<wchar_t*>(name.c_str());
  598     tvItem.cchTextMax = sizeof(itemName);
  599     if (!SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  600         return;
  601 
  602     FavoritesEntry* fav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  603     if (fav == NULL)
  604         return;
  605     
  606     tvItem.hItem = hItem;
  607     tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  608     name = CurrentCPToWide(newName);
  609     tvItem.pszText = const_cast<wchar_t*>(name.c_str());
  610     if (!SendMessage(hTree, TVM_SETITEMW, 0, (LPARAM)&tvItem))
  611         return;
  612 
  613     string oldName = fav->name;
  614     fav->name = newName;
  615 
  616     if (fav->isFolder)
  617     {
  618         FavoritesList::iterator iter = favorites->begin();
  619         while (iter != favorites->end())
  620         {
  621             if ((*iter)->parentFolder == oldName)
  622                 (*iter)->parentFolder = newName;
  623             iter++;
  624         }
  625     }
  626 }
  627 
  628 
  629 void MoveBookmarkInFavorites(HWND hTree, CelestiaCore* appCore)
  630 {
  631     FavoritesList* favorites = appCore->getFavorites();
  632     TVITEMW tvItem;
  633     TVINSERTSTRUCTW tvis;
  634     HTREEITEM hDragItemFolder, hDropItem;
  635     wchar_t dragItemName[33];
  636     wchar_t dragItemFolderName[33];
  637     wchar_t dropFolderName[33];
  638     bool bMovedInTree = false;
  639     FavoritesEntry* draggedFav = NULL;
  640     FavoritesEntry* dropFolderFav = NULL;
  641 
  642     // First get the target folder name
  643     tvItem.hItem = hDropTargetItem;
  644     tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  645     tvItem.pszText = dropFolderName;
  646     tvItem.cchTextMax = sizeof(dropFolderName) / sizeof(wchar_t);
  647     if (!SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  648         return;
  649 
  650     if (!TreeView_GetParent(hTree, hDropTargetItem))
  651         dropFolderName[0] = '\0';
  652 
  653     // Get the dragged item text
  654     tvItem.lParam = NULL;
  655     tvItem.hItem = hDragItem;
  656     tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  657     tvItem.pszText = dragItemName;
  658     tvItem.cchTextMax = sizeof(dragItemName) / sizeof(wchar_t);
  659     if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  660     {
  661         draggedFav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  662             
  663         // Get the dragged item folder
  664         if (hDragItemFolder = TreeView_GetParent(hTree, hDragItem))
  665         {
  666             tvItem.hItem = hDragItemFolder;
  667             tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
  668             tvItem.pszText = dragItemFolderName;
  669             tvItem.cchTextMax = sizeof(dragItemFolderName) / sizeof(wchar_t);
  670             if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  671             {
  672                 if (!TreeView_GetParent(hTree, hDragItemFolder))
  673                     dragItemFolderName[0] = '\0';
  674 
  675                 // Make sure drag and target folders are different
  676                 if (wcscmp(dragItemFolderName, dropFolderName))
  677                 {
  678                     // Delete tree item from source bookmark
  679                     if (TreeView_DeleteItem(hTree, hDragItem))
  680                     {
  681                         // Add item to dest bookmark
  682                         tvis.hParent = hDropTargetItem;
  683                         tvis.hInsertAfter = TVI_LAST;
  684                         tvis.item.mask = StdItemMask;
  685                         wstring itemName = dragItemName;
  686                         tvis.item.pszText = const_cast<wchar_t*>(itemName.c_str());
  687                         tvis.item.lParam = reinterpret_cast<LPARAM>(draggedFav);
  688                         tvis.item.iImage = 3;
  689                         tvis.item.iSelectedImage = 3;
  690                         if (hDropItem = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis))
  691                         {
  692                             TreeView_Expand(hTree, hDropTargetItem, TVE_EXPAND);
  693                                 
  694                             // Make the dropped item selected
  695                             TreeView_SelectItem(hTree, hDropItem);
  696 
  697                             bMovedInTree = true;
  698                         }
  699                     }
  700                 }
  701             }
  702         }
  703     }
  704 
  705     // Now perform the move in favorites
  706     if (bMovedInTree && draggedFav != NULL)
  707     {
  708         draggedFav->parentFolder = WideToCurrentCP(dropFolderName);
  709     }
  710 }
  711 
  712 
  713 bool isOrganizeBookmarksDragDropActive()
  714 {
  715     return dragging;
  716 }
  717 
  718 
  719 void OrganizeBookmarksOnBeginDrag(HWND hTree, LPNMTREEVIEW lpnmtv)
  720 {
  721     HIMAGELIST himl;    // handle to image list
  722     RECT rcItem;        // bounding rectangle of item
  723     DWORD dwLevel;      // heading level of item
  724     DWORD dwIndent;     // amount that child items are indented
  725 
  726     //Clear any selected item
  727     TreeView_SelectItem(hTree, NULL);
  728 
  729     // Tell the tree-view control to create an image to use
  730     // for dragging.
  731     hDragItem = lpnmtv->itemNew.hItem;
  732     himl = TreeView_CreateDragImage(hTree, hDragItem);
  733 
  734     // Get the bounding rectangle of the item being dragged.
  735     TreeView_GetItemRect(hTree, hDragItem, &rcItem, TRUE);
  736 
  737     // Get the heading level and the amount that the child items are
  738     // indented.
  739     dwLevel = lpnmtv->itemNew.lParam;
  740     dwIndent = (DWORD) SendMessage(hTree, TVM_GETINDENT, 0, 0);
  741 
  742     ImageList_DragShowNolock(TRUE);
  743 
  744     // Start the drag operation.
  745     ImageList_BeginDrag(himl, 0, 7, 7);
  746 
  747     // Hide the mouse pointer, and direct mouse input to the
  748     // parent window.
  749     ShowCursor(FALSE);
  750     SetCapture(GetParent(hTree));
  751     dragging = true;
  752 }
  753 
  754 
  755 void OrganizeBookmarksOnMouseMove(HWND hTree, LONG xCur, LONG yCur)
  756 {
  757     TVHITTESTINFO tvht;  // hit test information
  758     TVITEMW tvItem;
  759     HTREEITEM hItem;
  760 
  761     //Store away last drag position so timer can perform auto-scrolling.
  762     dragPos.x = xCur;
  763     dragPos.y = yCur;
  764 
  765     if (dragging)
  766     {
  767         // Drag the item to the current position of the mouse pointer.
  768         ImageList_DragMove(xCur, yCur);
  769         ImageList_DragLeave(hTree);
  770 
  771         // Find out if the pointer is on the item. If it is,
  772         // highlight the item as a drop target.
  773         tvht.pt.x = dragPos.x;
  774         tvht.pt.y = dragPos.y;
  775         if(hItem = TreeView_HitTest(hTree, &tvht))
  776         {
  777             // Only select folder items for drop targets
  778             tvItem.hItem = hItem;
  779             tvItem.mask = TVIF_PARAM | TVIF_HANDLE;
  780             if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  781             {
  782                 FavoritesEntry* fav = reinterpret_cast<FavoritesEntry*>(tvItem.lParam);
  783                 if (fav != NULL && fav->isFolder)
  784                 {
  785                     hDropTargetItem = hItem;
  786                     TreeView_SelectDropTarget(hTree, hDropTargetItem);
  787                 }
  788             }
  789         }
  790 
  791         ImageList_DragEnter(hTree, xCur, yCur);
  792     }
  793 }
  794 
  795 
  796 void OrganizeBookmarksOnLButtonUp(HWND hTree)
  797 {
  798     if (dragging)
  799     {
  800         ImageList_EndDrag();
  801         ImageList_DragLeave(hTree);
  802         ReleaseCapture();
  803         ShowCursor(TRUE);
  804         dragging = false;
  805 
  806         // Remove TVIS_DROPHILITED state from drop target item
  807         TreeView_SelectDropTarget(hTree, NULL);
  808     }
  809 }
  810 
  811 
  812 void DragDropAutoScroll(HWND hTree)
  813 {
  814     RECT rect;
  815     int i, count;
  816     HTREEITEM hItem;
  817 
  818     GetClientRect(hTree, &rect);
  819 
  820     ImageList_DragLeave(hTree);
  821 
  822     // See if we need to scroll.
  823     if (dragPos.y > rect.bottom - 10)
  824     {
  825         // If we are down towards the bottom but have not scrolled to the last
  826         // item, we need to scroll down.
  827         if (dragPos.x > rect.left && dragPos.x < rect.right)
  828         {
  829             SendMessage(hTree, WM_VSCROLL, SB_LINEDOWN, 0);
  830             count = TreeView_GetVisibleCount(hTree);
  831             hItem = TreeView_GetFirstVisible(hTree);
  832             for (i = 0; i < count - 1; i++)
  833                 hItem = TreeView_GetNextVisible(hTree, hItem);
  834 
  835             if (hItem)
  836             {
  837                 hDropTargetItem = hItem;
  838                 TreeView_SelectDropTarget(hTree, hDropTargetItem);
  839             }
  840         }
  841     }
  842     else if (dragPos.y < rect.top + 10)
  843     {
  844         // If we are up towards the top but have not scrolled to the first
  845         // item, we need to scroll up.
  846         if (dragPos.x > rect.left && dragPos.x < rect.right)
  847         {
  848             SendMessage(hTree, WM_VSCROLL, SB_LINEUP, 0);
  849             hItem = TreeView_GetFirstVisible(hTree);
  850             if(hItem)
  851             {
  852                 hDropTargetItem = hItem;
  853                 TreeView_SelectDropTarget(hTree, hDropTargetItem);
  854             }
  855         }
  856     }
  857 
  858     ImageList_DragEnter(hTree, dragPos.x, dragPos.y);
  859 }
  860 
  861 /*
  862 /////////////////////////////////////////////////////////////////////////////
  863 //  This function returns the handle of the tree item specified using standard
  864 //  path notation.
  865 /////////////////////////////////////////////////////////////////////////////
  866 HTREEITEM GetTreeViewItemHandle(HWND hTree, char* path, HTREEITEM hParent)
  867 {
  868     if (!hTree || !hParent)
  869         return NULL;
  870 
  871     char* cP;
  872     char_t itemName[33];
  873     char pathBuf[66];
  874     TVITEMW Item;
  875     HTREEITEM hItem;
  876 
  877     strcpy(pathBuf, path);
  878 
  879     if (cP = strchr(pathBuf, '/'))
  880         *cP = '\0';
  881 
  882     hItem = NULL;
  883     itemName[0] = '\0';
  884     Item.mask = TVIF_TEXT | TVIF_HANDLE;
  885     wstring name = CurrentCPToWide(itemName);
  886     Item.pszText = const_cast<wchar_t*>(name.c_str());
  887     Item.cchTextMax = sizeof(itemName);
  888     Item.hItem = hParent;
  889     if (SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem))
  890     {
  891         while (strcmp(pathBuf, itemName))
  892         {
  893             Item.hItem = TreeView_GetNextSibling(hTree, Item.hItem);
  894             if (!Item.hItem)
  895                 break;
  896 
  897             SendMessage(hTree, TVM_GETITEMW, 0, (LPARAM)&tvItem);
  898         }
  899         hItem = Item.hItem;
  900     }
  901     if (!hItem)
  902         return NULL;
  903 
  904     //Prepare to call recursively
  905     if (cP)
  906     {
  907         strcpy(pathBuf, cP + 1);
  908         hItem = TreeView_GetChild(hTree, hItem);
  909         hItem = GetTreeViewItemHandle(hTree, pathBuf, hItem);
  910     }
  911 
  912     return hItem;
  913 }
  914 */