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

common.cpp

Go to the documentation of this file.
00001 /*
00002  *  Celestia GTK+ Front-End
00003  *  Copyright (C) 2005 Pat Suwalski <pat@suwalski.net>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  $Id: common.cpp,v 1.4 2005/12/13 06:19:57 suwalski Exp $
00011  */
00012 
00013 #include <fstream>
00014 #include <gtk/gtk.h>
00015 #include <time.h>
00016 
00017 #include <celengine/astro.h>
00018 #include <celengine/galaxy.h>
00019 #include <celengine/render.h>
00020 #include <celestia/celestiacore.h>
00021 
00022 #include "common.h"
00023 
00024 
00025 /* Returns the offset of the timezone at date */
00026 gint tzOffsetAtDate(astro::Date date)
00027 {
00028         #ifdef WIN32
00029         /* This does not correctly handle DST. Unfortunately, no way to find
00030          * out what UTC offset is at specified date in Windows */
00031         return -timezone;
00032         #else
00033         time_t time = (time_t)astro::julianDateToSeconds(date - astro::Date(1970, 1, 1));
00034         struct tm *d = localtime(&time);
00035         
00036         return (gint)d->tm_gmtoff;
00037         #endif
00038 }
00039 
00040 
00041 /* Updates the time zone in the core based on valid timezone data */
00042 void updateTimeZone(AppData* app, gboolean local)
00043 {
00044         if (local)
00045                 /* Always base current time zone on simulation date */
00046                 app->core->setTimeZoneBias(tzOffsetAtDate(app->simulation->getTime()));
00047         else
00048                 app->core->setTimeZoneBias(0);
00049 }
00050 
00051 
00052 /* Creates a button. Used in several dialogs. */
00053 gint buttonMake(GtkWidget *hbox, const char *txt, GtkSignalFunc func, gpointer data)
00054 {
00055         GtkWidget* button = gtk_button_new_with_label(txt);
00056 
00057         gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 0);
00058         g_signal_connect(GTK_OBJECT(button), "pressed", func, data);
00059         
00060         return 0;
00061 }
00062 
00063 
00064 /* creates a group of radioButtons. Used in several dialogs. */
00065 void makeRadioItems(const char* const *labels, GtkWidget *box, GtkSignalFunc sigFunc, GtkToggleButton **gads, gpointer data)
00066 {
00067         GSList *group = NULL;
00068 
00069         for (gint i=0; labels[i]; i++)
00070         {
00071                 GtkWidget *button = gtk_radio_button_new_with_label(group, labels[i]);
00072 
00073                 if (gads)
00074                         gads[i] = GTK_TOGGLE_BUTTON(button);
00075 
00076                 group = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
00077                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (i == 0)?1:0);
00078 
00079                 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
00080                 gtk_widget_show (button);
00081                 g_signal_connect(GTK_OBJECT(button), "pressed", sigFunc, GINT_TO_POINTER(i));
00082 
00083                 if (data != NULL)
00084                         g_object_set_data(G_OBJECT(button), "data", data);
00085         }
00086 }
00087 
00088 
00089 /* Gets the contents of a file and sanitizes formatting */
00090 char *readFromFile(const char *fname)
00091 {
00092         ifstream textFile(fname, ios::in);
00093         string s("");
00094         if (!textFile.is_open())
00095         {
00096                 s = "Unable to open file '";
00097                 s += fname ;
00098                 s += "', probably due to improper installation !\n";
00099         }
00100 
00101         char c;
00102         while(textFile.get(c))
00103         {
00104                 if (c == '\t')
00105                         s += "        ";  /* 8 spaces */
00106                 else if (c == '\014')     /* Ctrl+L (form feed) */
00107                         s += "\n\n\n\n";
00108                 else
00109                         s += c;
00110         }
00111         return g_strdup(s.c_str());
00112 }
00113 
00114 
00115 /* Returns width of the non-fullscreen window */
00116 int getWinWidth(AppData* app)
00117 {
00118         if (app->fullScreen)
00119                 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(app->mainWindow), "sizeX"));
00120         else
00121                 return app->glArea->allocation.width;
00122 }
00123 
00124 
00125 /* Returns height of the non-fullscreen window */
00126 int getWinHeight(AppData* app)
00127 {
00128         if (app->fullScreen)
00129                 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(app->mainWindow), "sizeY"));
00130         else
00131                 return app->glArea->allocation.height;
00132 }
00133 
00134 
00135 /* Returns X-position of the non-fullscreen window */
00136 int getWinX(AppData* app)
00137 {
00138         int positionX;
00139         
00140         if (app->fullScreen)
00141                 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(app->mainWindow), "positionX"));
00142         else
00143         {
00144                 gtk_window_get_position(GTK_WINDOW(app->mainWindow), &positionX, NULL);
00145                 return positionX;
00146         }
00147 }
00148 
00149 
00150 /* Returns Y-position of the non-fullscreen window */
00151 int getWinY(AppData* app)
00152 {
00153         int positionY;
00154         
00155         if (app->fullScreen)
00156                 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(app->mainWindow), "positionY"));
00157         else
00158         {
00159                 gtk_window_get_position(GTK_WINDOW(app->mainWindow), NULL, &positionY);
00160                 return positionY;
00161         }
00162 }
00163 
00164 
00165 /* Sanitizes and sets Ambient Light */
00166 void setSaneAmbientLight(AppData* app, float value)
00167 {
00168         if (value < 0.0 || value > 1.0)
00169                 value = amLevels[1]; /* Default to "Low" */
00170         
00171         app->renderer->setAmbientLightLevel(value);
00172 }
00173 
00174 
00175 /* Sanitizes and sets Visual Magnitude */
00176 void setSaneVisualMagnitude(AppData* app, float value)
00177 {
00178         if (value < 0.0 || value > 100.0)
00179                 value = 8.5f; /* Default from Simulation::Simulation() */
00180         
00181         app->simulation->setFaintestVisible(value);
00182 }
00183 
00184 
00185 /* Sanitizes and sets Galaxy Light Gain */
00186 void setSaneGalaxyLightGain(float value)
00187 {
00188         if (value < 0.0 || value > 1.0)
00189                 value = 0.0f; /* Default */
00190         
00191         Galaxy::setLightGain(value);
00192 }
00193 
00194 
00195 /* Sanitizes and sets HUD Verbosity */
00196 void setSaneVerbosity(AppData* app, int value)
00197 {
00198         if (value < 0 || value > 2)
00199                 value = 1; /* Default to "Terse" */
00200         
00201         app->core->setHudDetail(value);
00202 }
00203 
00204 
00205 /* Sanitizes and sets Star Style */
00206 void setSaneStarStyle(AppData* app, Renderer::StarStyle value)
00207 {
00208         if (value < Renderer::FuzzyPointStars || value > Renderer::ScaledDiscStars)
00209                 value = Renderer::FuzzyPointStars;
00210         
00211         app->renderer->setStarStyle(value);
00212 }
00213 
00214 
00215 /* Sanitizes and sets Altername Surface Name */
00216 void setSaneAltSurface(AppData* app, char* value)
00217 {
00218         if (value == NULL)
00219                 value = (char*)"";
00220         
00221         app->simulation->getActiveObserver()->setDisplayedSurface(value);
00222 }
00223 
00224 
00225 /* Sanitizes and sets Window Size */
00226 void setSaneWinSize(AppData* app, int x, int y)
00227 {
00228         int screenX = gdk_screen_get_width(gdk_screen_get_default());
00229         int screenY = gdk_screen_get_height(gdk_screen_get_default());
00230         
00231         if (x < 320 || x > screenX || y < 240 || y > screenY)
00232         {
00233                 x = 640;
00234                 y = 480;
00235         }
00236         
00237         gtk_widget_set_size_request(app->glArea, x, y);
00238 }
00239 
00240 
00241 /* Sanitizes and sets Window Position */
00242 void setSaneWinPosition(AppData* app, int x, int y)
00243 {
00244         int screenX = gdk_screen_get_width(gdk_screen_get_default());
00245         int screenY = gdk_screen_get_height(gdk_screen_get_default());
00246 
00247         /* This one is different than the others because we don't have a default */
00248         if (x > 0 && x < screenX && y > 0 && y < screenY)
00249         {
00250                 gtk_window_move(GTK_WINDOW(app->mainWindow), x, y);
00251         }
00252 }
00253 
00254 
00255 /* Sets default render flags. Exists because the defaults are a little lame. */
00256 void setDefaultRenderFlags(AppData* app)
00257 {
00258         app->renderer->setRenderFlags(Renderer::ShowAtmospheres |
00259                                       Renderer::ShowAutoMag |
00260                                       Renderer::ShowStars |
00261                                       Renderer::ShowPlanets |
00262                                       Renderer::ShowSmoothLines |
00263                                       Renderer::ShowCometTails |
00264                                       Renderer::ShowRingShadows |
00265                                       Renderer::ShowCloudMaps |
00266                                       Renderer::ShowRingShadows |
00267                                       Renderer::ShowEclipseShadows |
00268                                       Renderer::ShowGalaxies |
00269                                       Renderer::ShowNebulae |
00270                                       Renderer::ShowNightMaps);
00271 }

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