home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / tips_dialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  9.5 KB  |  329 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <string.h>
  25.  
  26. #include <gtk/gtk.h>
  27.  
  28. #include "tips_dialog.h"
  29. #include "gimprc.h"
  30. #include "gimpui.h"
  31.  
  32. #include "libgimp/gimpenv.h"
  33.  
  34. #include "libgimp/gimpintl.h"
  35.  
  36. #include "wilber.h"
  37.  
  38.  
  39. #define TIPS_DIR_NAME   "tips"
  40.  
  41. static void   tips_dialog_destroy (GtkWidget *widget, gpointer data);
  42. static void   tips_show_previous  (GtkWidget *widget, gpointer data);
  43. static void   tips_show_next      (GtkWidget *widget, gpointer data);
  44. static void   tips_toggle_update  (GtkWidget *widget, gpointer data);
  45. static void   read_tips_file      (gchar *filename);
  46.  
  47. static GtkWidget  *tips_dialog = NULL;
  48. static GtkWidget  *tips_label;
  49. static gchar     **tips_text = NULL;
  50. static gint        tips_count = 0;
  51. static gint        old_show_tips;
  52.  
  53. void
  54. tips_dialog_create (void)
  55. {
  56.   GtkWidget *vbox;
  57.   GtkWidget *vbox2;
  58.   GtkWidget *hbox;
  59.   GtkWidget *bbox;
  60.   GtkWidget *frame;
  61.   GtkWidget *preview;
  62.   GtkWidget *button;
  63.   gchar     *temp;
  64.   guchar    *utemp;
  65.   guchar    *src;
  66.   guchar    *dest;
  67.   gint       x;
  68.   gint       y;
  69.  
  70.   if (tips_count == 0)
  71.     {
  72.       temp = g_strdup_printf ("%s" G_DIR_SEPARATOR_S TIPS_DIR_NAME
  73.                               G_DIR_SEPARATOR_S "%s",
  74.                   gimp_data_directory (),
  75.                               _("gimp_tips.txt"));
  76.       read_tips_file (temp);
  77.       g_free (temp);
  78.     }
  79.  
  80.   if (last_tip >= tips_count || last_tip < 0)
  81.     last_tip = 0;
  82.  
  83.   if (!tips_dialog)
  84.     {
  85.       tips_dialog = gtk_window_new (GTK_WINDOW_DIALOG);
  86.       gtk_window_set_wmclass (GTK_WINDOW (tips_dialog), "tip_of_the_day", "Gimp");
  87.       gtk_window_set_title (GTK_WINDOW (tips_dialog), _("GIMP Tip of the Day"));
  88.       gtk_window_set_position (GTK_WINDOW (tips_dialog), GTK_WIN_POS_CENTER);
  89.       gtk_window_set_policy (GTK_WINDOW (tips_dialog), FALSE, TRUE, FALSE);
  90.  
  91.       gtk_signal_connect (GTK_OBJECT (tips_dialog), "delete_event",
  92.               GTK_SIGNAL_FUNC (tips_dialog_destroy),
  93.               NULL);
  94.       gtk_signal_connect (GTK_OBJECT (tips_dialog), "destroy",
  95.               GTK_SIGNAL_FUNC (gtk_widget_destroyed),
  96.               &tips_dialog);
  97.  
  98.       /* destroy the tips window if the mainlevel gtk_main() function is left */
  99.       gtk_quit_add_destroy (1, GTK_OBJECT (tips_dialog));
  100.  
  101.       vbox = gtk_vbox_new (FALSE, 0);
  102.       gtk_container_add (GTK_CONTAINER (tips_dialog), vbox);      
  103.       gtk_widget_show (vbox);
  104.  
  105.       hbox = gtk_hbox_new (FALSE, 5);
  106.       gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
  107.       gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  108.       gtk_widget_show (hbox);
  109.  
  110.       tips_label = gtk_label_new (tips_text[last_tip]);
  111.       gtk_label_set_justify (GTK_LABEL (tips_label), GTK_JUSTIFY_LEFT);
  112.       gtk_misc_set_alignment (GTK_MISC (tips_label), 0.5, 0.5);
  113.       gtk_box_pack_start (GTK_BOX (hbox), tips_label, TRUE, TRUE, 3);
  114.       gtk_widget_show (tips_label);
  115.      
  116.       vbox2 = gtk_vbox_new (FALSE, 0);
  117.       gtk_box_pack_end (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
  118.       frame = gtk_frame_new (NULL);
  119.       gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  120.       gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, FALSE, 0);
  121.  
  122.       preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  123.       gtk_preview_size (GTK_PREVIEW (preview), wilber_width, wilber_height);
  124.       utemp = g_new (guchar, wilber_width * 3);
  125.       src = (guchar *)wilber_data;
  126.       for (y = 0; y < wilber_height; y++)
  127.     {
  128.       dest = utemp;
  129.       for (x = 0; x < wilber_width; x++)
  130.         {
  131.           HEADER_PIXEL (src, dest);
  132.           dest += 3;
  133.         }
  134.       gtk_preview_draw_row (GTK_PREVIEW (preview), utemp, 0, y, wilber_width); 
  135.     }
  136.       g_free(utemp);
  137.       gtk_container_add (GTK_CONTAINER (frame), preview);
  138.       gtk_widget_show (preview);
  139.       gtk_widget_show (frame);
  140.       gtk_widget_show (vbox2);
  141.  
  142.       hbox = gtk_hbox_new (FALSE, 15);
  143.       gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
  144.       gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  145.       gtk_widget_show (hbox);
  146.       
  147.       button = 
  148.     gtk_check_button_new_with_label (_("Show tip next time GIMP starts"));
  149.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
  150.                     show_tips);
  151.       gtk_signal_connect (GTK_OBJECT (button), "toggled",
  152.               GTK_SIGNAL_FUNC (tips_toggle_update),
  153.               (gpointer) &show_tips);
  154.       gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  155.       gtk_widget_show (button);
  156.  
  157.       old_show_tips = show_tips;
  158.  
  159.       bbox = gtk_hbutton_box_new ();
  160.       gtk_box_pack_end (GTK_BOX (hbox), bbox, FALSE, FALSE, 0);
  161.       gtk_widget_show (bbox);
  162.  
  163.       button = gtk_button_new_with_label (_("Close"));
  164.       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  165.       gtk_window_set_default (GTK_WINDOW (tips_dialog), button);
  166.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  167.               GTK_SIGNAL_FUNC (tips_dialog_destroy),
  168.               NULL);
  169.       gtk_container_add (GTK_CONTAINER (bbox), button);
  170.       gtk_widget_show (button);
  171.  
  172.       bbox = gtk_hbutton_box_new ();
  173.       gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
  174.       gtk_button_box_set_spacing (GTK_BUTTON_BOX (bbox), 5);
  175.       gtk_box_pack_end (GTK_BOX (hbox), bbox, FALSE, FALSE, 0);
  176.       gtk_widget_show (bbox);
  177.  
  178.       button = gtk_button_new_with_label (_("Previous Tip"));
  179.       GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  180.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  181.               GTK_SIGNAL_FUNC (tips_show_previous),
  182.               NULL);
  183.       gtk_container_add (GTK_CONTAINER (bbox), button);
  184.       gtk_widget_show (button);
  185.  
  186.       button = gtk_button_new_with_label (_("Next Tip"));
  187.       GTK_WIDGET_UNSET_FLAGS (button, GTK_RECEIVES_DEFAULT);
  188.       gtk_signal_connect (GTK_OBJECT (button), "clicked",
  189.               GTK_SIGNAL_FUNC (tips_show_next),
  190.               NULL);
  191.       gtk_container_add (GTK_CONTAINER (bbox), button);
  192.       gtk_widget_show (button);
  193.  
  194.      /*  Connect the "F1" help key  */
  195.       gimp_help_connect_help_accel (tips_dialog,
  196.                     gimp_standard_help_func,
  197.                     "dialogs/tip_of_the_day.html");
  198.     }
  199.  
  200.   if (!GTK_WIDGET_VISIBLE (tips_dialog))
  201.     {
  202.       gtk_widget_show (tips_dialog);
  203.     }
  204.   else
  205.     {
  206.       gdk_window_raise (tips_dialog->window);
  207.     }
  208. }
  209.  
  210. static void
  211. tips_dialog_destroy (GtkWidget *widget,
  212.              gpointer   data)
  213. {
  214.   GList *update = NULL; /* options that should be updated in .gimprc */
  215.   GList *remove = NULL; /* options that should be commented out */
  216.  
  217.   gtk_widget_destroy (tips_dialog);
  218.  
  219.   /* the last-shown-tip is now saved in sessionrc */
  220.  
  221.   if (show_tips != old_show_tips)
  222.     {
  223.       update = g_list_append (update, "show-tips");
  224.       remove = g_list_append (remove, "dont-show-tips");
  225.       old_show_tips = show_tips;
  226.       save_gimprc (&update, &remove);
  227.     }
  228.   g_list_free (update);
  229.   g_list_free (remove);
  230. }
  231.  
  232. static void
  233. tips_show_previous (GtkWidget *widget,
  234.             gpointer  data)
  235. {
  236.   last_tip--;
  237.  
  238.   if (last_tip < 0)
  239.     last_tip = tips_count - 1;
  240.  
  241.   gtk_label_set_text (GTK_LABEL (tips_label), tips_text[last_tip]);
  242. }
  243.  
  244. static void
  245. tips_show_next (GtkWidget *widget,
  246.         gpointer   data)
  247. {
  248.   last_tip++;
  249.  
  250.   if (last_tip >= tips_count)
  251.     last_tip = 0;
  252.  
  253.   gtk_label_set_text (GTK_LABEL (tips_label), tips_text[last_tip]);
  254. }
  255.  
  256. static void
  257. tips_toggle_update (GtkWidget *widget,
  258.             gpointer   data)
  259. {
  260.   gint *toggle_val;
  261.  
  262.   toggle_val = (gint *) data;
  263.  
  264.   if (GTK_TOGGLE_BUTTON (widget)->active)
  265.     *toggle_val = TRUE;
  266.   else
  267.     *toggle_val = FALSE;
  268. }
  269.  
  270. static void
  271. store_tip (gchar *str)
  272. {
  273.   tips_count++;
  274.   tips_text = g_realloc (tips_text, sizeof (gchar *) * tips_count);
  275.   tips_text[tips_count - 1] = str;
  276. }
  277.  
  278. static void
  279. read_tips_file (gchar *filename)
  280. {
  281.   FILE *fp;
  282.   gchar *tip = NULL;
  283.   gchar *str = NULL;
  284.  
  285.   fp = fopen (filename, "rt");
  286.   if (!fp)
  287.     {
  288.       store_tip (_("Your GIMP tips file appears to be missing!\n"
  289.            "There should be a file called gimp_tips.txt in\n"
  290.            "the tips subdirectory of the GIMP data directory.\n"
  291.            "Please check your installation."));
  292.       return;
  293.     }
  294.  
  295.   str = g_new (char, 1024);
  296.   while (!feof (fp))
  297.     {
  298.       if (!fgets (str, 1024, fp))
  299.     continue;
  300.    
  301.       if (str[0] == '#' || str[0] == '\n')
  302.     {
  303.       if (tip != NULL)
  304.         {
  305.           tip[strlen (tip) - 1] = '\000';
  306.           store_tip (tip);
  307.           tip = NULL;
  308.         }
  309.     }
  310.       else
  311.     {
  312.       if (tip == NULL)
  313.         {
  314.           tip = g_malloc (strlen (str) + 1);
  315.           strcpy (tip, str);
  316.         }
  317.       else
  318.         {
  319.           tip = g_realloc (tip, strlen (tip) + strlen (str) + 1);
  320.           strcat (tip, str);
  321.         }
  322.     }
  323.     }
  324.   if (tip != NULL)
  325.     store_tip (tip);
  326.   g_free (str);
  327.   fclose (fp);
  328. }
  329.