dialog-time.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-time.cpp,v 1.3 2005-12-13 03:54:40 suwalski Exp $
  11.  */
  12. #include <gtk/gtk.h>
  13. #include <time.h>
  14. #include <celengine/astro.h>
  15. #include <celengine/simulation.h>
  16. #include "dialog-time.h"
  17. #include "common.h"
  18. /* Declarations: Callbacks */
  19. static gboolean intAdjChanged(GtkAdjustment* adj, int *val);
  20. static gboolean zoneChosen(GtkComboBox *menu, int* timezone);
  21. static gboolean monthChosen(GtkComboBox *menu, int* month);
  22. /* Declarations: Helpers */
  23. static void chooseOption(GtkWidget *hbox, const char *str, char *choices[],
  24.                          int *val, GtkSignalFunc chosen);
  25. static void intSpin(GtkWidget *hbox, const char *str, int min, int max,
  26.                     int *val, const char *sep);
  27. /* ENTRY: Dialog initializer */
  28. void dialogSetTime(AppData* app)
  29. {
  30. int timezone = 1;
  31. GtkWidget *stimedialog = gtk_dialog_new_with_buttons("Set Time",
  32.                                                      GTK_WINDOW(app->mainWindow),
  33.                                                      GTK_DIALOG_DESTROY_WITH_PARENT,
  34.                                                      GTK_STOCK_OK,
  35.                                                      GTK_RESPONSE_OK,
  36.                                                      "Set Current Time",
  37.                                                      GTK_RESPONSE_ACCEPT,
  38.                                                      GTK_STOCK_CANCEL,
  39.                                                      GTK_RESPONSE_CANCEL,
  40.                                                      NULL);
  41. if (app->showLocalTime)
  42. timezone = 2;
  43. GtkWidget *hbox = gtk_hbox_new(FALSE, 6);
  44. GtkWidget *frame = gtk_frame_new("Time");
  45. GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
  46. astro::Date date(app->simulation->getTime() +
  47.                  astro::secondsToJulianDate(app->core->getTimeZoneBias()));
  48. gtk_widget_show(align);
  49. gtk_widget_show(frame);
  50. gtk_container_add(GTK_CONTAINER(align),GTK_WIDGET(hbox));
  51. gtk_container_add(GTK_CONTAINER(frame),GTK_WIDGET(align));
  52. gtk_container_set_border_width (GTK_CONTAINER (frame), 7);
  53. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(stimedialog)->vbox), frame, FALSE, FALSE, 0);
  54.     
  55. int seconds = (int)date.seconds;
  56. intSpin(hbox, "Hour", 0, 23, &date.hour, ":");
  57. intSpin(hbox, "Minute", 0, 59, &date.minute, ":");
  58. intSpin(hbox, "Second", 0, 59, &seconds, "  ");
  59. chooseOption(hbox,
  60.              "Timezone",
  61.              (char **)timeOptions,
  62.              &timezone,
  63.              GTK_SIGNAL_FUNC(zoneChosen));
  64. gtk_widget_show_all(hbox);
  65. hbox = gtk_hbox_new(FALSE, 6);
  66. frame = gtk_frame_new("Date");
  67. align=gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
  68. gtk_container_set_border_width (GTK_CONTAINER (frame), 7);
  69. chooseOption(hbox,
  70.              "Month",
  71.              (char **)monthOptions,
  72.              &date.month,
  73.              GTK_SIGNAL_FUNC(monthChosen));
  74. /* (Hopefully, noone will need to go beyond these years :-) */
  75. intSpin(hbox, "Day", 1, 31, &date.day, ",");
  76. intSpin(hbox, "Year", -9999, 9999, &date.year, " ");
  77. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(stimedialog)->vbox), frame, FALSE, FALSE, 0);
  78. gtk_container_add(GTK_CONTAINER(align),GTK_WIDGET(hbox));
  79. gtk_container_add(GTK_CONTAINER(frame),GTK_WIDGET(align));
  80. gtk_widget_show(align);
  81. gtk_widget_show(frame);
  82. gtk_widget_show_all(hbox);
  83. gtk_dialog_set_default_response(GTK_DIALOG(stimedialog), GTK_RESPONSE_OK);
  84. gint button = gtk_dialog_run(GTK_DIALOG(stimedialog));
  85. /* Set the selected seconds value back to the Date struct */
  86. date.seconds = seconds;
  87. if (button == GTK_RESPONSE_ACCEPT)
  88. /* Set current time and exit. */
  89. app->simulation->setTime((double)time(NULL) / 86400.0 + (double)astro::Date(1970, 1, 1));
  90. else if (button == GTK_RESPONSE_OK)
  91. /* Set entered time and exit */
  92. app->simulation->setTime((double)date - ((timezone == 1) ? 0 : astro::secondsToJulianDate(tzOffsetAtDate(date))));
  93. gtk_widget_destroy(stimedialog);
  94. }
  95. /* CALLBACK: spinner value changed */
  96. static gboolean intAdjChanged(GtkAdjustment* adj, int *val)
  97. {
  98. if (val)
  99. {
  100. *val = (int)adj->value;
  101. return TRUE;
  102. }
  103. return FALSE;
  104. }
  105. /* CALLBACK: time zone selected from drop-down */
  106. static gboolean zoneChosen(GtkComboBox *menu, gboolean* timezone)
  107. {
  108. *timezone = gtk_combo_box_get_active(menu) + 1;
  109. return TRUE;
  110. }
  111. /* CALLBACK: month selected from drop-down */
  112. static gboolean monthChosen(GtkComboBox *menu, int* month)
  113. {
  114. if (month)
  115. *month = gtk_combo_box_get_active(menu) + 1;
  116. return TRUE;
  117. }
  118. /* HELPER: creates one of the several drop-down boxes */
  119. static void chooseOption(GtkWidget *hbox, const char *str, char *choices[], int *val, GtkSignalFunc chosen)
  120. {
  121. GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  122. GtkWidget *label = gtk_label_new(str);
  123. gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  124. GtkWidget* combo = gtk_combo_box_new_text();
  125. for(unsigned int i = 0; choices[i]; i++)
  126. {
  127. gtk_combo_box_append_text(GTK_COMBO_BOX(combo), choices[i]);
  128. }
  129. gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
  130. gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, TRUE, 7);
  131. gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 2);
  132. gtk_combo_box_set_active(GTK_COMBO_BOX(combo), (*val - 1));
  133. g_signal_connect(GTK_OBJECT(combo), "changed", G_CALLBACK(chosen), (gpointer)val);
  134. }
  135. /* HELPER: creates spinner */
  136. static void intSpin(GtkWidget *hbox, const char *str, int min, int max, int *val, const char *sep)
  137. {
  138. GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  139. GtkWidget *label = gtk_label_new(str);
  140. gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  141. GtkAdjustment *adj = (GtkAdjustment *) gtk_adjustment_new ((float)*val, (float) min, (float) max,
  142.                                                            1.0, 5.0, 0.0);
  143. GtkWidget *spinner = gtk_spin_button_new (adj, 1.0, 0);
  144. gtk_spin_button_set_numeric(GTK_SPIN_BUTTON (spinner), TRUE);
  145. gtk_spin_button_set_wrap (GTK_SPIN_BUTTON (spinner), TRUE);
  146. gtk_spin_button_set_snap_to_ticks(GTK_SPIN_BUTTON (spinner),TRUE);
  147. gtk_entry_set_max_length(GTK_ENTRY (spinner), ((max<99)?2:4) );
  148. gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
  149. gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
  150. if ((sep) && (*sep))
  151. {
  152. gtk_widget_show (label);
  153. GtkWidget *hbox2 = gtk_hbox_new(FALSE, 3);
  154. label = gtk_label_new(sep);
  155. gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
  156. gtk_box_pack_start (GTK_BOX (hbox2), spinner, FALSE, FALSE, 0);
  157. gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
  158. gtk_box_pack_start (GTK_BOX (vbox), hbox2, TRUE, TRUE, 7);
  159. gtk_widget_show (label);
  160. gtk_widget_show (hbox2);
  161. }
  162. else
  163. {
  164. gtk_box_pack_start (GTK_BOX (vbox), spinner, TRUE, TRUE, 7);
  165. }
  166. g_signal_connect(GTK_OBJECT(adj), "value-changed",
  167.                  G_CALLBACK(intAdjChanged), val);
  168. }