Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

winssbrowser.cpp

Go to the documentation of this file.
00001 // winssbrowser.h
00002 // 
00003 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
00004 //
00005 // Solar system browser tool for Windows.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 
00012 #include <string>
00013 #include <sstream>
00014 #include <algorithm>
00015 #include <set>
00016 #include <windows.h>
00017 #include <commctrl.h>
00018 #include "winssbrowser.h"
00019 
00020 #include "res/resource.h"
00021 
00022 using namespace std;
00023 
00024 
00025 HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel, void* data,
00026                         HTREEITEM parent)
00027 { 
00028     TVITEM tvi; 
00029     TVINSERTSTRUCT tvins; 
00030     static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST; 
00031 
00032 #if 0 
00033     tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM; 
00034 #endif
00035     tvi.mask = TVIF_TEXT | TVIF_PARAM;
00036  
00037     // Set the text of the item. 
00038     tvi.pszText = lpszItem; 
00039     tvi.cchTextMax = lstrlen(lpszItem); 
00040  
00041     // Save the heading level in the item's application-defined 
00042     // data area. 
00043     tvi.lParam = (LPARAM) data; 
00044  
00045     tvins.item = tvi; 
00046     tvins.hInsertAfter = hPrev; 
00047     tvins.hParent = parent;
00048  
00049     // Add the item to the tree view control. 
00050     hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEM, 0, 
00051                                     (LPARAM) (LPTVINSERTSTRUCT) &tvins); 
00052  
00053 #if 0 
00054     // The new item is a child item. Give the parent item a 
00055     // closed folder bitmap to indicate it now has child items. 
00056     if (nLevel > 1)
00057     { 
00058         hti = TreeView_GetParent(hwndTV, hPrev); 
00059         tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
00060         tvi.hItem = hti; 
00061         // tvi.iImage = g_nClosed; 
00062         // tvi.iSelectedImage = g_nClosed; 
00063         TreeView_SetItem(hwndTV, &tvi); 
00064     }
00065 #endif 
00066  
00067     return hPrev; 
00068 }
00069 
00070 
00071 void AddPlanetarySystemToTree(const PlanetarySystem* sys, HWND treeView, int level, HTREEITEM parent)
00072 {
00073     for (int i = 0; i < sys->getSystemSize(); i++)
00074     {
00075         Body* world = sys->getBody(i);
00076         HTREEITEM item;
00077         item = AddItemToTree(treeView, 
00078                              const_cast<char*>(world->getName().c_str()),
00079                              level,
00080                              static_cast<void*>(world),
00081                              parent);
00082 
00083         const PlanetarySystem* satellites = world->getSatellites();
00084         if (satellites != NULL)
00085             AddPlanetarySystemToTree(satellites, treeView, level + 1, item);
00086     }
00087 }
00088 
00089 
00090 BOOL APIENTRY SolarSystemBrowserProc(HWND hDlg,
00091                                      UINT message,
00092                                      UINT wParam,
00093                                      LONG lParam)
00094 {
00095     SolarSystemBrowser* browser = reinterpret_cast<SolarSystemBrowser*>(GetWindowLong(hDlg, DWL_USER));
00096 
00097     switch (message)
00098     {
00099     case WM_INITDIALOG:
00100         {
00101             SolarSystemBrowser* browser = reinterpret_cast<SolarSystemBrowser*>(lParam);
00102             if (browser == NULL)
00103                 return EndDialog(hDlg, 0);
00104             SetWindowLong(hDlg, DWL_USER, lParam);
00105 
00106             HWND hwnd = GetDlgItem(hDlg, IDC_SSBROWSER_TREE);
00107             const SolarSystem* solarSys = browser->appCore->getSimulation()->getNearestSolarSystem();
00108             if (solarSys != NULL)
00109             {
00110                 HTREEITEM rootItem = AddItemToTree(hwnd, "Sun", 1, NULL,
00111                                                    (HTREEITEM) TVI_ROOT);
00112                 const PlanetarySystem* planets = solarSys->getPlanets();
00113                 if (planets != NULL)
00114                     AddPlanetarySystemToTree(planets, hwnd, 2, rootItem);
00115 
00116                 SendMessage(hwnd, TVM_EXPAND, TVE_EXPAND, (LPARAM) rootItem); 
00117             }
00118         }
00119         return(TRUE);
00120 
00121     case WM_DESTROY:
00122         if (browser != NULL && browser->parent != NULL)
00123         {
00124             SendMessage(browser->parent, WM_COMMAND, IDCLOSE,
00125                         reinterpret_cast<LPARAM>(browser));
00126         }
00127         break;
00128 
00129     case WM_COMMAND:
00130         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00131         {
00132             if (browser != NULL && browser->parent != NULL)
00133             {
00134                 SendMessage(browser->parent, WM_COMMAND, IDCLOSE,
00135                             reinterpret_cast<LPARAM>(browser));
00136             }
00137             EndDialog(hDlg, 0);
00138             return TRUE;
00139         }
00140         else if (LOWORD(wParam) == IDC_BUTTON_CENTER)
00141         {
00142             browser->appCore->charEntered('c');
00143         }
00144         else if (LOWORD(wParam) == IDC_BUTTON_GOTO)
00145         {
00146             browser->appCore->charEntered('G');
00147         }
00148         break;
00149 
00150     case WM_NOTIFY:
00151         {
00152             LPNMHDR hdr = (LPNMHDR) lParam;
00153             
00154             if (hdr->code == TVN_SELCHANGED)
00155             {
00156                 LPNMTREEVIEW nm = (LPNMTREEVIEW) lParam;
00157                 Body* body = reinterpret_cast<Body*>(nm->itemNew.lParam);
00158                 if (body != NULL)
00159                 {
00160                     browser->appCore->getSimulation()->setSelection(Selection(body));
00161                 }
00162                 else
00163                 {
00164                     // If the body is NULL, assume that this is the tree item for
00165                     // the sun.
00166                     const SolarSystem* solarSys = browser->appCore->getSimulation()->getNearestSolarSystem();
00167                     if (solarSys != NULL && solarSys->getStar() != NULL)
00168                         browser->appCore->getSimulation()->setSelection(Selection(const_cast<Star*>(solarSys->getStar())));
00169                 }
00170             }
00171         }
00172     }
00173 
00174     return FALSE;
00175 }
00176 
00177 
00178 SolarSystemBrowser::SolarSystemBrowser(HINSTANCE appInstance,
00179                                        HWND _parent,
00180                                        CelestiaCore* _appCore) :
00181     appCore(_appCore),
00182     parent(_parent)
00183 {
00184     hwnd = CreateDialogParam(appInstance,
00185                              MAKEINTRESOURCE(IDD_SSBROWSER),
00186                              parent,
00187                              SolarSystemBrowserProc,
00188                              reinterpret_cast<LONG>(this));
00189 }
00190 
00191 
00192 SolarSystemBrowser::~SolarSystemBrowser()
00193 {
00194     SetWindowLong(hwnd, DWL_USER, 0);
00195 }

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1