winssbrowser.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:6k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // winssbrowser.h
  2. // 
  3. // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
  4. //
  5. // Solar system browser tool for Windows.
  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. #include <string>
  12. #include <sstream>
  13. #include <algorithm>
  14. #include <set>
  15. #include <windows.h>
  16. #include <commctrl.h>
  17. #include "winssbrowser.h"
  18. #include "celutil/winutil.h"
  19. #include "res/resource.h"
  20. using namespace std;
  21. HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel, void* data,
  22.                         HTREEITEM parent)
  23.     TVITEM tvi; 
  24.     TVINSERTSTRUCT tvins; 
  25.     static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST; 
  26. #if 0 
  27.     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; 
  28. #endif
  29.     tvi.mask = TVIF_TEXT | TVIF_PARAM;
  30.  
  31.     // Set the text of the item. 
  32.     tvi.pszText = lpszItem; 
  33.     tvi.cchTextMax = lstrlen(lpszItem); 
  34.     // Save the heading level in the item's application-defined 
  35.     // data area. 
  36.     tvi.lParam = (LPARAM) data; 
  37.  
  38.     tvins.item = tvi; 
  39.     tvins.hInsertAfter = hPrev; 
  40.     tvins.hParent = parent;
  41.  
  42.     // Add the item to the tree view control. 
  43.     hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEM, 0, 
  44.                                     (LPARAM) (LPTVINSERTSTRUCT) &tvins); 
  45.  
  46. #if 0 
  47.     // The new item is a child item. Give the parent item a 
  48.     // closed folder bitmap to indicate it now has child items. 
  49.     if (nLevel > 1)
  50.     { 
  51.         hti = TreeView_GetParent(hwndTV, hPrev); 
  52.         tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
  53.         tvi.hItem = hti; 
  54.         // tvi.iImage = g_nClosed; 
  55.         // tvi.iSelectedImage = g_nClosed; 
  56.         TreeView_SetItem(hwndTV, &tvi); 
  57.     }
  58. #endif 
  59.  
  60.     return hPrev; 
  61. }
  62. void AddPlanetarySystemToTree(const PlanetarySystem* sys, HWND treeView, int level, HTREEITEM parent)
  63. {
  64.     for (int i = 0; i < sys->getSystemSize(); i++)
  65.     {
  66.         Body* world = sys->getBody(i);
  67.         if (world != NULL && world->getClassification() != Body::Invisible && !world->getName().empty())
  68.         {
  69.             HTREEITEM item;
  70.             item = AddItemToTree(treeView, 
  71.                                  const_cast<char*>(UTF8ToCurrentCP(world->getName(true)).c_str()),
  72.                                  level,
  73.                                  static_cast<void*>(world),
  74.                                  parent);
  75.             const PlanetarySystem* satellites = world->getSatellites();
  76.             if (satellites != NULL)
  77.                 AddPlanetarySystemToTree(satellites, treeView, level + 1, item);
  78.         }
  79.     }
  80. }
  81. BOOL APIENTRY SolarSystemBrowserProc(HWND hDlg,
  82.                                      UINT message,
  83.                                      UINT wParam,
  84.                                      LONG lParam)
  85. {
  86.     SolarSystemBrowser* browser = reinterpret_cast<SolarSystemBrowser*>(GetWindowLong(hDlg, DWL_USER));
  87.     switch (message)
  88.     {
  89.     case WM_INITDIALOG:
  90.         {
  91.             SolarSystemBrowser* browser = reinterpret_cast<SolarSystemBrowser*>(lParam);
  92.             if (browser == NULL)
  93.                 return EndDialog(hDlg, 0);
  94.             SetWindowLong(hDlg, DWL_USER, lParam);
  95.             HWND hwnd = GetDlgItem(hDlg, IDC_SSBROWSER_TREE);
  96.             const SolarSystem* solarSys = browser->appCore->getSimulation()->getNearestSolarSystem();
  97.             if (solarSys != NULL)
  98.             {
  99.                 Universe* u = browser->appCore->getSimulation()->getUniverse();
  100.                 string starNameString = UTF8ToCurrentCP(u->getStarCatalog()->getStarName(*(solarSys->getStar())));
  101.                 HTREEITEM rootItem = AddItemToTree(hwnd, const_cast<char*>(starNameString.c_str()), 1, NULL,
  102.                                                    (HTREEITEM) TVI_ROOT);
  103.                 const PlanetarySystem* planets = solarSys->getPlanets();
  104.                 if (planets != NULL)
  105.                     AddPlanetarySystemToTree(planets, hwnd, 2, rootItem);
  106.                 SendMessage(hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) rootItem); 
  107.             }
  108.         }
  109.         return(TRUE);
  110.     case WM_DESTROY:
  111.         if (browser != NULL && browser->parent != NULL)
  112.         {
  113.             SendMessage(browser->parent, WM_COMMAND, IDCLOSE,
  114.                         reinterpret_cast<LPARAM>(browser));
  115.         }
  116.         break;
  117.     case WM_COMMAND:
  118.         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  119.         {
  120.             if (browser != NULL && browser->parent != NULL)
  121.             {
  122.                 SendMessage(browser->parent, WM_COMMAND, IDCLOSE,
  123.                             reinterpret_cast<LPARAM>(browser));
  124.             }
  125.             EndDialog(hDlg, 0);
  126.             return TRUE;
  127.         }
  128.         else if (LOWORD(wParam) == IDC_BUTTON_CENTER)
  129.         {
  130.             browser->appCore->charEntered('c');
  131.         }
  132.         else if (LOWORD(wParam) == IDC_BUTTON_GOTO)
  133.         {
  134.             browser->appCore->charEntered('G');
  135.         }
  136.         break;
  137.     case WM_NOTIFY:
  138.         {
  139.             LPNMHDR hdr = (LPNMHDR) lParam;
  140.             
  141.             if (hdr->code == TVN_SELCHANGED)
  142.             {
  143.                 LPNMTREEVIEW nm = (LPNMTREEVIEW) lParam;
  144.                 Body* body = reinterpret_cast<Body*>(nm->itemNew.lParam);
  145.                 if (body != NULL)
  146.                 {
  147.                     browser->appCore->getSimulation()->setSelection(Selection(body));
  148.                 }
  149.                 else
  150.                 {
  151.                     // If the body is NULL, assume that this is the tree item for
  152.                     // the sun.
  153.                     const SolarSystem* solarSys = browser->appCore->getSimulation()->getNearestSolarSystem();
  154.                     if (solarSys != NULL && solarSys->getStar() != NULL)
  155.                         browser->appCore->getSimulation()->setSelection(Selection(const_cast<Star*>(solarSys->getStar())));
  156.                 }
  157.             }
  158.         }
  159.     }
  160.     return FALSE;
  161. }
  162. SolarSystemBrowser::SolarSystemBrowser(HINSTANCE appInstance,
  163.                                        HWND _parent,
  164.                                        CelestiaCore* _appCore) :
  165.     appCore(_appCore),
  166.     parent(_parent)
  167. {
  168.     hwnd = CreateDialogParam(appInstance,
  169.                              MAKEINTRESOURCE(IDD_SSBROWSER),
  170.                              parent,
  171.                              SolarSystemBrowserProc,
  172.                              reinterpret_cast<LONG>(this));
  173. }
  174. SolarSystemBrowser::~SolarSystemBrowser()
  175. {
  176.     SetWindowLong(hwnd, DWL_USER, 0);
  177. }