dialog-eclipse.cpp
来自「celestia源代码」· C++ 代码 · 共 479 行 · 第 1/2 页
CPP
479 行
/* FIXME: should be a transient, but then there are focus issues */ gtk_window_set_modal(GTK_WINDOW(calwindow), TRUE); gtk_window_set_type_hint(GTK_WINDOW(calwindow), GDK_WINDOW_TYPE_HINT_DOCK); gtk_window_set_decorated(GTK_WINDOW(calwindow), FALSE); gtk_window_set_resizable(GTK_WINDOW(calwindow), FALSE); gtk_window_stick(GTK_WINDOW(calwindow)); GtkWidget* calendar = gtk_calendar_new(); /* Load date structure stored in the button's data */ selDate* date = (selDate *)g_object_get_data(G_OBJECT(button), "eclipsedata"); gtk_calendar_select_month(GTK_CALENDAR(calendar), date->month - 1, date->year); gtk_calendar_select_day(GTK_CALENDAR(calendar), date->day); gtk_container_add(GTK_CONTAINER(calwindow), calendar); gtk_widget_show(calendar); int x, y, i, j; gdk_window_get_origin(GDK_WINDOW(GTK_WIDGET(button)->window), &x, &y); gtk_widget_translate_coordinates(GTK_WIDGET(button), GTK_WIDGET(ed->window), 10, 10, &i, &j); gtk_window_move(GTK_WINDOW(calwindow), x + i, y + j); g_signal_connect(calendar, "day-selected-double-click", G_CALLBACK(calDateSelect), button); gtk_window_present(GTK_WINDOW(calwindow)); g_object_set_data_full(G_OBJECT (button), "calendar", calwindow, (GDestroyNotify)gtk_widget_destroy); } } else { /* Pushed out */ if (calwindow) { /* Destroys the calendar */ g_object_set_data(G_OBJECT (button), "calendar", NULL); calwindow = NULL; } }}/* CALLBACK: "SetTime/Goto" in Eclipse Finder */static gint eclipseGoto(GtkButton*, EclipseData* ed){ GValue value = { 0, 0 }; /* Initialize GValue to 0 */ GtkTreeIter iter; GtkTreeModel* model; int time[6]; Simulation* sim = ed->app->simulation; /* Nothing selected */ if (ed->sel == NULL) return FALSE; /* IF prevents selection while list is being updated */ if (!gtk_tree_selection_get_selected(ed->sel, &model, &iter)) return FALSE; /* Tedious method of extracting the desired time. * However, still better than parsing a single string. */ for (int i = 0; i < 6; i++) { gtk_tree_model_get_value(model, &iter, i+5, &value); time[i] = g_value_get_int(&value); g_value_unset(&value); } /* Retrieve the selected body */ gtk_tree_model_get_value(model, &iter, 11, &value); Body* body = (Body *)g_value_get_pointer(&value); g_value_unset(&value); /* Set time based on retrieved values */ astro::Date d(time[0], time[1], time[2]); d.hour = time[3]; d.minute = time[4]; d.seconds = (double)time[5]; sim->setTime((double)d); /* The rest is directly from the Windows eclipse code */ Selection target(body); Selection ref(body->getSystem()->getStar()); /* Use the phase lock coordinate system to set a position * on the line between the sun and the body where the eclipse * is occurring. */ sim->setFrame(FrameOfReference(astro::PhaseLock, target, ref)); sim->update(0.0); double distance = astro::kilometersToMicroLightYears(target.radius() * 4.0); RigidTransform to; to.rotation = Quatd::yrotation(PI); to.translation = Point3d(0, 0, -distance); sim->gotoLocation(to, 2.5); return TRUE;}/* CALLBACK: Double-click on the Eclipse Finder Listbox */static gint eclipse2Click(GtkWidget*, GdkEventButton* event, EclipseData* ed){ if (event->type == GDK_2BUTTON_PRESS) { /* Double-click, same as hitting the select and go button */ return eclipseGoto(NULL, ed); } return FALSE;}/* CALLBACK: Compute button in Eclipse Finder */static void eclipseCompute(GtkButton* button, EclipseData* ed){ GtkTreeIter iter; /* Set the cursor to a watch and force redraw */ gdk_window_set_cursor(GTK_WIDGET(button)->window, gdk_cursor_new(GDK_WATCH)); gtk_main_iteration(); /* Clear the listbox */ gtk_list_store_clear(ed->eclipseListStore); /* Create the dates in a more suitable format */ astro::Date from(ed->d1->year, ed->d1->month, ed->d1->day); astro::Date to(ed->d2->year, ed->d2->month, ed->d2->day); /* Initialize the eclipse finder */ EclipseFinder ef(ed->app->core, ed->body, ed->bSolar ? Eclipse::Solar : Eclipse::Moon, (double)from, (double)to); vector<Eclipse> eclipseListRaw = ef.getEclipses(); for (std::vector<Eclipse>::iterator i = eclipseListRaw.begin(); i != eclipseListRaw.end(); i++) { /* Handle 0 case */ if ((*i).planete == "None") { gtk_list_store_append(ed->eclipseListStore, &iter); gtk_list_store_set(ed->eclipseListStore, &iter, 0, (*i).planete.c_str(), -1); continue; } char d[12], strStart[10], strEnd[10]; astro::Date start((*i).startTime); astro::Date end((*i).endTime); sprintf(d, "%d-%02d-%02d", (*i).date->year, (*i).date->month, (*i).date->day); sprintf(strStart, "%02d:%02d:%02d", start.hour, start.minute, (int)start.seconds); sprintf(strEnd, "%02d:%02d:%02d", end.hour, end.minute, (int)end.seconds); /* Set time to middle time so that eclipse it right on earth */ astro::Date timeToSet = (start + end) / 2.0f; /* Add item to the list. * Entries 5-11 are not displayed and store data. */ gtk_list_store_append(ed->eclipseListStore, &iter); gtk_list_store_set(ed->eclipseListStore, &iter, 0, (*i).planete.c_str(), 1, (*i).sattelite.c_str(), 2, d, 3, strStart, 4, strEnd, 5, timeToSet.year, 6, timeToSet.month, 7, timeToSet.day, 8, timeToSet.hour, 9, timeToSet.minute, 10, (int)timeToSet.seconds, 11, (*i).body, -1); } /* Set the cursor back */ gdk_window_set_cursor(GTK_WIDGET(button)->window, gdk_cursor_new(GDK_LEFT_PTR));}/* CALLBACK: When Eclipse Body is selected */static void eclipseBodySelect(GtkMenuShell* menu, EclipseData* ed){ GtkWidget* item = gtk_menu_get_active(GTK_MENU(menu)); GList* list = gtk_container_children(GTK_CONTAINER(menu)); int itemIndex = g_list_index(list, item); /* Set string according to body array */ sprintf(ed->body, "%s", eclipsePlanetTitles[itemIndex]);}/* CALLBACK: When Eclipse Type (Solar:Moon) is selected */static void eclipseTypeSelect(GtkMenuShell* menu, EclipseData* ed){ GtkWidget* item = gtk_menu_get_active(GTK_MENU(menu)); GList* list = gtk_container_children(GTK_CONTAINER(menu)); int itemIndex = g_list_index(list, item); /* Solar eclipse */ if (itemIndex == 0) ed->bSolar = 1; /* Moon eclipse */ else ed->bSolar = 0;}/* CALLBACK: When Eclipse is selected in Eclipse Finder */static void listEclipseSelect(GtkTreeSelection* sel, EclipseData* ed){ /* Simply set the selection pointer to this data item */ ed->sel = sel;}/* CALLBACK: Destroy Window */static void eclipseDestroy(GtkWidget* w, gint, EclipseData* ed){ gtk_widget_destroy(GTK_WIDGET(w)); g_free(ed->d1); g_free(ed->d2); g_free(ed);}/* HELPER: set a date string in a button */static void setButtonDateString(GtkToggleButton *button, int year, int month, int day){ char date[50]; sprintf(date, "%d %s %d", day, monthOptions[month - 1], year); gtk_button_set_label(GTK_BUTTON(button), date);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?