tips_dialog.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001,2002,2003 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Andreas Klust <klust@users.sf.net>
00009  * WWW Home: http://gxsm.sf.net
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00027 
00028 #include <config.h>
00029 #include <gtk/gtk.h>
00030 #include <gnome.h>
00031 #include <libgnome/libgnome.h>
00032 
00033 #include <gdk/gdk.h>
00034 
00035 #include "tips_dialog.h"
00036 #include "wilber-stm.h"
00037 #include "wilber-afm.h"
00038 #include "wilber-spa.h"
00039 
00040 #include "glbvars.h"
00041 
00042 static int  tips_dialog_hide (GtkWidget *widget, gpointer data);
00043 static int  tips_show_next (GtkWidget *widget, gpointer data);
00044 static void tips_toggle_update (GtkWidget *widget, gpointer data);
00045 static void read_tips_file(char *filename);
00046 
00047 static GtkWidget *tips_dialog = NULL;
00048 static GtkWidget *tips_label;
00049 static char **    tips_text = NULL;
00050 static int        tips_count = 0;
00051 static int        old_show_tips;
00052 
00053 static int        last_tip  = 0;
00054 static int        show_tips = -1;
00055 
00056 static gint       twc_id = 0;
00057 
00058 gchar *gxsm_data_directory (){ 
00059   static gchar dd[] = PACKAGE_TIPS_DIR;
00060   return dd;
00061 }
00062 
00063 gint draw_wilber(void *pv){
00064         GtkWidget *preview = (GtkWidget*)pv;
00065   static int i=0;
00066   static gint w[]    = { wilberstm_width, wilberafm_width, wilberspa_width, 0 };
00067   static gint h[]    = { wilberstm_height, wilberafm_height, wilberspa_height, 0 };
00068   static gchar* d[] = { wilberstm_data, wilberafm_data, wilberspa_data, NULL };
00069   guchar *utemp, *src, *dest;
00070   gint x,y;
00071 
00072   if(!w[i]) i=0;
00073   gtk_preview_size (GTK_PREVIEW (preview), w[i], h[i]);
00074   utemp = g_new (guchar, w[i] * 3);
00075   src = (guchar *)d[i];
00076   for (y = 0; y < h[i]; y++)
00077     {
00078       dest = utemp;
00079       for (x = 0; x < w[i]; x++)
00080         {
00081           HEADER_PIXEL(src, dest);
00082           dest += 3;
00083         }
00084       gtk_preview_draw_row (GTK_PREVIEW (preview), utemp,
00085                             0, y, w[i]); 
00086     }
00087   g_free(utemp);
00088   ++i;
00089   gtk_widget_hide (preview);
00090   gtk_widget_show (preview);
00091   return TRUE;
00092 }
00093 
00094 void
00095 tips_dialog_create ()
00096 {
00097   GtkWidget *vbox;
00098   GtkWidget *hbox1;
00099   GtkWidget *hbox2;
00100   GtkWidget *bbox;
00101   GtkWidget *vbox_bbox2;
00102   GtkWidget *bbox2;
00103   GtkWidget *frame;
00104   static GtkWidget *preview;
00105   GtkWidget *button_close;
00106   GtkWidget *button_next;
00107   GtkWidget *button_prev;
00108   GtkWidget *vbox_check;
00109   GtkWidget *button_check;
00110   gchar  *   temp;
00111 
00112   XsmRescourceManager xrm("GUI-global","Tips");
00113   show_tips = xrm.GetBool("ShowTips", TRUE);
00114   xrm.Get("LastTip", &last_tip, "0");
00115   ++last_tip;
00116 
00117   if (tips_count == 0)
00118     {
00119       temp = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s",
00120                               gxsm_data_directory (),
00121                               _("gxsm_tips.txt"));
00122       read_tips_file (temp);
00123       g_free (temp);
00124     }
00125 
00126   if (last_tip >= tips_count || last_tip < 0)
00127     last_tip = 0;
00128 
00129   if (!tips_dialog)
00130     {
00131       tips_dialog = gtk_dialog_new ();
00132       gtk_window_set_wmclass (GTK_WINDOW (tips_dialog), "tip_of_the_day", "Gxsm");
00133       gtk_window_set_title (GTK_WINDOW (tips_dialog), _("GXSM Tip of the Day"));
00134       gtk_window_set_position (GTK_WINDOW (tips_dialog), GTK_WIN_POS_CENTER);
00135       gtk_signal_connect (GTK_OBJECT (tips_dialog), "delete_event",
00136                           GTK_SIGNAL_FUNC (tips_dialog_hide), NULL);
00137       /* destroy the tips window if the mainlevel gtk_main() function is left */
00138       gtk_quit_add_destroy (1, GTK_OBJECT (tips_dialog));
00139 
00140       vbox = GTK_DIALOG(tips_dialog)->vbox;
00141 
00142       hbox1 = gtk_hbox_new (FALSE, 5);
00143       gtk_container_set_border_width (GTK_CONTAINER (hbox1), 10);
00144       gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, TRUE, 0);
00145       gtk_widget_show (hbox1);
00146 
00147       hbox2 = gtk_hbox_new (FALSE, 5);
00148       gtk_container_set_border_width (GTK_CONTAINER (hbox2), 10);
00149       gtk_box_pack_end (GTK_BOX (vbox), hbox2, FALSE, TRUE, 0);
00150       gtk_widget_show (hbox2);
00151       
00152       bbox = gtk_hbutton_box_new ();
00153       gtk_box_pack_end (GTK_BOX (hbox2), bbox, FALSE, FALSE, 0);
00154       gtk_widget_show (bbox);
00155 
00156       vbox_bbox2 = gtk_vbox_new (FALSE, 0);
00157       gtk_box_pack_end (GTK_BOX (hbox2), vbox_bbox2, FALSE, FALSE, 15);
00158       gtk_widget_show (vbox_bbox2);
00159 
00160       bbox2 = gtk_hbox_new (TRUE, 5); 
00161       gtk_box_pack_end (GTK_BOX (vbox_bbox2), bbox2, TRUE, FALSE, 0);
00162       gtk_widget_show(bbox2);
00163 
00164       preview = gtk_preview_new (GTK_PREVIEW_COLOR);
00165 
00166       draw_wilber(preview);
00167 
00168       frame = gtk_frame_new (NULL);
00169       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
00170       gtk_box_pack_end (GTK_BOX (hbox1), frame, FALSE, TRUE, 3);
00171       gtk_container_add (GTK_CONTAINER (frame), preview);
00172       gtk_widget_show (preview);
00173       gtk_widget_show (frame);
00174 
00175       tips_label = gtk_label_new (tips_text[last_tip]);
00176       gtk_label_set_justify (GTK_LABEL (tips_label), GTK_JUSTIFY_LEFT);
00177       gtk_box_pack_start (GTK_BOX (hbox1), tips_label, TRUE, TRUE, 3);
00178       gtk_widget_show (tips_label);
00179 
00180       button_prev = gtk_button_new_with_label (_("Previous Tip"));
00181 //      button_prev = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
00182       GTK_WIDGET_UNSET_FLAGS (button_prev, GTK_RECEIVES_DEFAULT);
00183       gtk_signal_connect (GTK_OBJECT (button_prev), "clicked",
00184                           GTK_SIGNAL_FUNC (tips_show_next),
00185                           (gpointer) "prev");
00186  
00187       gtk_widget_show (button_prev);
00188 
00189       button_next = gtk_button_new_with_label (_("Next Tip"));
00190 //      button_next = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
00191       GTK_WIDGET_UNSET_FLAGS (button_next, GTK_RECEIVES_DEFAULT);
00192       gtk_signal_connect (GTK_OBJECT (button_next), "clicked",
00193                           GTK_SIGNAL_FUNC (tips_show_next),
00194                           (gpointer) "next");
00195       gtk_widget_show (button_next);
00196 
00197 //      button_close = gtk_button_new_with_label (_("Close"));
00198       button_close = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
00199       GTK_WIDGET_SET_FLAGS (button_close, GTK_CAN_DEFAULT);
00200       gtk_window_set_default (GTK_WINDOW (tips_dialog), button_close);
00201       gtk_signal_connect (GTK_OBJECT (button_close), "clicked",
00202                           GTK_SIGNAL_FUNC (tips_dialog_hide), NULL);
00203       gtk_widget_show (button_close);
00204 
00205       vbox_check = gtk_vbox_new (FALSE, 0); 
00206       gtk_box_pack_start (GTK_BOX (hbox2), vbox_check, FALSE, TRUE, 0);
00207       gtk_widget_show (vbox_check);
00208 
00209       button_check = gtk_check_button_new_with_label (_("Show next time"));
00210       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_check),
00211                                     show_tips);
00212       gtk_signal_connect (GTK_OBJECT (button_check), "toggled",
00213                           GTK_SIGNAL_FUNC (tips_toggle_update),
00214                           (gpointer) &show_tips);
00215 //      gtk_box_pack_start (GTK_BOX (vbox_check), button_check, TRUE, FALSE, 0);
00216       gtk_widget_show (button_check);
00217 
00218       gtk_dialog_add_action_widget (GTK_DIALOG(tips_dialog), button_check, 3);
00219       gtk_dialog_add_action_widget (GTK_DIALOG(tips_dialog), button_next, 1);
00220       gtk_dialog_add_action_widget (GTK_DIALOG(tips_dialog), button_prev, 0);
00221       gtk_dialog_add_action_widget (GTK_DIALOG(tips_dialog), button_close, 2);
00222 
00223       old_show_tips = show_tips;
00224 
00225       /*  Connect the "F1" help key  */
00226       //      gimp_help_connect_help_accel (tips_dialog,
00227       //                                    gimp_standard_help_func,
00228       //                                    "dialogs/tip_of_the_day.html");
00229     }
00230 
00231   if (!GTK_WIDGET_VISIBLE (tips_dialog))
00232     {
00233       gtk_widget_show (tips_dialog);
00234     }
00235   else
00236     {
00237       gdk_window_raise (tips_dialog->window);
00238     }
00239 
00240   twc_id = gtk_timeout_add(3000, draw_wilber, preview);
00241 
00242 }
00243 
00244 static int
00245 tips_dialog_hide (GtkWidget *widget,
00246                   gpointer data)
00247 {
00248   gtk_timeout_remove(twc_id);
00249   gtk_widget_hide (tips_dialog);
00250 
00251   /* the last-shown-tip is now saved in sessionrc */
00252   /*
00253   if (show_tips != old_show_tips)
00254     {
00255       GList *update = NULL; // options that should be updated in .gimprc
00256       GList *remove = NULL; // options that should be commented out
00257 
00258       update = g_list_append (update, "show-tips");
00259       remove = g_list_append (remove, "dont-show-tips");
00260       old_show_tips = show_tips;
00261       save_gimprc (&update, &remove);
00262       g_list_free (update);
00263       g_list_free (remove);
00264     }
00265   */
00266 
00267   XsmRescourceManager xrm("GUI-global","Tips");
00268   xrm.Put("LastTip", last_tip);
00269   xrm.PutBool("ShowTips", show_tips);
00270 
00271   return TRUE;
00272 }
00273 
00274 static int
00275 tips_show_next (GtkWidget *widget,
00276                 gpointer  data)
00277 {
00278   if (!strcmp ((char *)data, "prev"))
00279     {
00280       last_tip--;
00281       if (last_tip < 0)
00282         last_tip = tips_count - 1;
00283     }
00284   else
00285     {
00286       last_tip++;
00287       if (last_tip >= tips_count)
00288         last_tip = 0;
00289     }
00290   gtk_label_set (GTK_LABEL (tips_label), tips_text[last_tip]);
00291   return FALSE;
00292 }
00293 
00294 static void
00295 tips_toggle_update (GtkWidget *widget,
00296                     gpointer   data)
00297 {
00298   int *toggle_val;
00299 
00300   toggle_val = (int *) data;
00301 
00302   if (GTK_TOGGLE_BUTTON (widget)->active)
00303     *toggle_val = TRUE;
00304   else
00305     *toggle_val = FALSE;
00306 }
00307 
00308 static void
00309 store_tip (char *str)
00310 {
00311   tips_count++;
00312   tips_text = (gchar**)g_realloc(tips_text, sizeof(char *) * tips_count);
00313   tips_text[tips_count - 1] = str;
00314 }
00315 
00316 static void
00317 read_tips_file (char *filename)
00318 {
00319   FILE *fp;
00320   char *tip = NULL;
00321   char *str = NULL;
00322 
00323   fp = fopen (filename, "rt");
00324   if (!fp)
00325     {
00326       store_tip (_("Your GXSM tips file appears to be missing!\n"
00327                  "There should be a file called gxsm_tips.txt in the\n"
00328                  "GXSM data directory.  Please check your installation."));
00329       return;
00330     }
00331 
00332   str = g_new (char, 1024);
00333   while (!feof (fp))
00334     {
00335       if (!fgets (str, 1024, fp))
00336         continue;
00337       
00338       if (str[0] == '#' || str[0] == '\n')
00339         {
00340           if (tip != NULL)
00341             {
00342               tip[strlen (tip) - 1] = '\000';
00343               store_tip (tip);
00344               tip = NULL;
00345             }
00346         }
00347       else
00348         {
00349           if (tip == NULL)
00350             {
00351               tip = (gchar*)g_malloc (strlen (str) + 1);
00352               strcpy (tip, str);
00353             }
00354           else
00355             {
00356               tip = (gchar*)g_realloc (tip, strlen (tip) + strlen (str) + 1);
00357               strcat (tip, str);
00358             }
00359         }
00360     }
00361   if (tip != NULL)
00362     store_tip (tip);
00363   g_free (str);
00364   fclose (fp);
00365 }

Generated on Sat Apr 1 09:04:05 2006 for GXSM by  doxygen 1.4.6