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

OpenGL

开发平台:

Visual C++

  1. /*
  2.  *  Celestia GTK+ Front-End
  3.  *  Copyright (C) 2005 Pat Suwalski <pat@suwalski.net>
  4.  *
  5.  *  This program 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.  *  $Id: dialog-solar.cpp,v 1.2 2005-12-13 06:19:57 suwalski Exp $
  11.  */
  12. #include <gtk/gtk.h>
  13. #include <celengine/body.h>
  14. #include <celengine/selection.h>
  15. #include <celengine/simulation.h>
  16. #include <celengine/solarsys.h>
  17. #include <celengine/star.h>
  18. #include <celengine/stardb.h>
  19. #include "dialog-solar.h"
  20. #include "actions.h"
  21. #include "common.h"
  22. /* Declarations: Callbacks */
  23. static void treeSolarSelect(GtkTreeSelection* sel, AppData* app);
  24. /* Declarations: Helpers */
  25. static void addPlanetarySystemToTree(const PlanetarySystem* sys,
  26.                                      GtkTreeStore* solarTreeStore,
  27.                                      GtkTreeIter* parent);
  28. static void loadNearestStarSystem(AppData* data, GtkWidget* solarTree,
  29.                                   GtkTreeStore* solarTreeStore);
  30. /* ENTRY: Navigation -> Solar System Browser... */
  31. void dialogSolarBrowser(AppData* app)
  32. {
  33. GtkWidget *solarTree = NULL;
  34. GtkTreeStore *solarTreeStore = NULL;
  35. GtkWidget *browser = gtk_dialog_new_with_buttons("Solar System Browser",
  36.                                                  GTK_WINDOW(app->mainWindow),
  37.                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
  38.                                                  GTK_STOCK_CLOSE,
  39.                                                  GTK_RESPONSE_CLOSE,
  40.                                                  NULL);
  41. app->simulation->setSelection(Selection((Star *) NULL));
  42.  
  43. /* Solar System Browser */
  44. GtkWidget *mainbox = gtk_vbox_new(FALSE, CELSPACING);
  45. gtk_container_set_border_width(GTK_CONTAINER(mainbox), CELSPACING);
  46. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(browser)->vbox), mainbox, TRUE, TRUE, 0);
  47. GtkWidget *scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  48. gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
  49.                                 GTK_POLICY_AUTOMATIC,
  50.                                 GTK_POLICY_ALWAYS);
  51. gtk_box_pack_start(GTK_BOX(mainbox), scrolled_win, TRUE, TRUE, 0);
  52. /* Set the tree store to have 2 visible cols, two hidden. The hidden ones
  53.  * store pointer to the row's object and its Selection::Type. */
  54. solarTreeStore = gtk_tree_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_INT);
  55. solarTree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(solarTreeStore));
  56. gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(solarTree), TRUE);
  57. gtk_container_add(GTK_CONTAINER(scrolled_win), solarTree);
  58. GtkCellRenderer *renderer;
  59. GtkTreeViewColumn *column;
  60. for (int i = 0; i < 2; i++) {
  61. renderer = gtk_cell_renderer_text_new();
  62. column = gtk_tree_view_column_new_with_attributes (ssTitles[i], renderer, "text", i, NULL);
  63. gtk_tree_view_append_column(GTK_TREE_VIEW(solarTree), column);
  64. gtk_tree_view_column_set_min_width(column, 200);
  65. }
  66.  
  67. loadNearestStarSystem(app, solarTree, solarTreeStore);
  68. GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(solarTree));
  69. g_signal_connect(selection, "changed", G_CALLBACK(treeSolarSelect), app);
  70. /* Common Buttons */
  71. GtkWidget *hbox = gtk_hbox_new(TRUE, CELSPACING);
  72. if (buttonMake(hbox, "Center", (GtkSignalFunc)actionCenterSelection, app))
  73. return;
  74. if (buttonMake(hbox, "Go To", (GtkSignalFunc)actionGotoSelection, app))
  75. return;
  76. gtk_box_pack_start(GTK_BOX(mainbox), hbox, FALSE, FALSE, 0);
  77. g_signal_connect(browser, "response", G_CALLBACK(gtk_widget_destroy), browser);
  78. gtk_widget_set_usize(browser, 500, 400); /* Absolute Size, urghhh */
  79. gtk_widget_show_all(browser);
  80. }
  81. /* CALLBACK: When Object is selected in Solar System Browser */
  82. static void treeSolarSelect(GtkTreeSelection* sel, AppData* app)
  83. {
  84. gpointer item;
  85. gint type;
  86. GValue value = { 0, 0 }; /* Initialize empty GValue */
  87. GtkTreeIter iter;
  88. GtkTreeModel* model;
  89. gtk_tree_selection_get_selected(sel, &model, &iter);
  90. /* Retrieve the item (Body/Star) */
  91. gtk_tree_model_get_value(model, &iter, 2, &value);
  92. item = g_value_get_pointer(&value);
  93. g_value_unset(&value);
  94. /* Retrieve if isStar */
  95. gtk_tree_model_get_value(model, &iter, 3, &value);
  96. type = g_value_get_int(&value);
  97. g_value_unset(&value);
  98. if (type == Selection::Type_Star)
  99. app->simulation->setSelection(Selection((Star *)item));
  100. else if (type == Selection::Type_Body)
  101. app->simulation->setSelection(Selection((Body *)item));
  102. else
  103. g_warning("Unexpected selection type selected.");
  104. }
  105. /* HELPER: Recursively populate GtkTreeView with objects in PlanetarySystem */
  106. static void addPlanetarySystemToTree(const PlanetarySystem* sys, GtkTreeStore* solarTreeStore, GtkTreeIter* parent)
  107. {
  108. const char *name;
  109. const char *type;
  110. Body* world;
  111. const PlanetarySystem* satellites;
  112. GtkTreeIter child;
  113. for (int i = 0; i < sys->getSystemSize(); i++)
  114. {
  115. world = sys->getBody(i);
  116. name = g_strdup(world->getName().c_str());
  117. switch(world->getClassification())
  118. {
  119. case Body::Planet:
  120. type = "Planet";
  121. break;
  122. case Body::Moon:
  123. type = "Moon";
  124. break;
  125. case Body::Asteroid:
  126. type = "Asteroid";
  127. break;
  128. case Body::Comet:
  129. type = "Comet";
  130. break;
  131. case Body::Spacecraft:
  132. type = "Spacecraft";
  133. break;
  134. case Body::Unknown:
  135. default:
  136. type = "-";
  137. break;
  138. }
  139. satellites = world->getSatellites();
  140. /* Add child */
  141. gtk_tree_store_append(solarTreeStore, &child, parent);
  142. gtk_tree_store_set(solarTreeStore, &child,
  143.                    0, name,
  144.                    1, type,
  145.                    2, (gpointer)world,
  146.                    3, Selection::Type_Body, /* not Star */
  147.                    -1);
  148. /* Recurse */
  149. if (satellites != NULL)
  150. addPlanetarySystemToTree(satellites, solarTreeStore, &child);
  151. }
  152. }
  153. /* HELPER: Retrieves closest system and calls addPlanetarySystemToTree to
  154.  *         populate. */
  155. static void loadNearestStarSystem(AppData* app, GtkWidget* solarTree, GtkTreeStore* solarTreeStore)
  156. {
  157. const char* name;
  158. char type[30];
  159. const Star* nearestStar;
  160. const SolarSystem* solarSys = app->simulation->getNearestSolarSystem();
  161. StarDatabase *stardb = app->simulation->getUniverse()->getStarCatalog();
  162. g_assert(stardb);
  163. GtkTreeIter top;
  164. gtk_tree_store_clear(solarTreeStore);
  165. gtk_tree_store_append(solarTreeStore, &top, NULL);
  166. if (solarSys != NULL)
  167. {
  168. nearestStar = solarSys->getStar();
  169. name = g_strdup(stardb->getStarName(*nearestStar).c_str());
  170. sprintf(type, "%s Star", nearestStar->getSpectralType());
  171. /* Set up the top-level node. */
  172. gtk_tree_store_set(solarTreeStore, &top,
  173.                    0, name,
  174.                    1, &type,
  175.                    2, (gpointer)nearestStar,
  176.                    3, Selection::Type_Star, /* Is Star */
  177.                    -1);
  178. const PlanetarySystem* planets = solarSys->getPlanets();
  179. if (planets != NULL)
  180. addPlanetarySystemToTree(planets, solarTreeStore, &top);
  181. /* Open up the top node */
  182. GtkTreePath* path = gtk_tree_model_get_path(GTK_TREE_MODEL(solarTreeStore), &top);
  183. gtk_tree_view_expand_row(GTK_TREE_VIEW(solarTree), path, FALSE);
  184. }
  185. else
  186. gtk_tree_store_set(solarTreeStore, &top, 0, "No Planetary Bodies", -1);
  187. }