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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  * Copyright (C) 1998 Andy Thomas (alt@picnic.demon.co.uk)
  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.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include <glib.h>
  23.  
  24. #include "apptypes.h"
  25. #include "gimpui.h"
  26. #include "palette_entries.h"
  27. #include "palette_select.h"
  28. #include "paletteP.h"
  29.  
  30. #include "libgimp/gimpintl.h"
  31.  
  32. /*  List of active dialogs  */
  33. static GSList *active_dialogs = NULL;
  34.  
  35. /*  local function prototypes  */
  36. static gint   palette_select_button_press   (GtkWidget *, GdkEventButton *, gpointer);
  37. static void   palette_select_close_callback (GtkWidget *, gpointer);
  38. static void   palette_select_edit_callback  (GtkWidget *, gpointer);
  39.  
  40. /*  public functions  */
  41.  
  42. PaletteSelect *
  43. palette_select_new (gchar *title,
  44.             gchar *initial_palette)
  45. {
  46.   PaletteEntries *p_entries = NULL;
  47.   PaletteSelect  *psp;
  48.   GSList     *list;
  49.   GtkWidget  *vbox;
  50.   GtkWidget  *hbox;
  51.   GtkWidget  *scrolled_win;
  52.   gchar      *titles[3];
  53.   gint        select_pos;
  54.  
  55.   palette_select_palette_init ();
  56.  
  57.   psp = g_new (PaletteSelect, 1);
  58.   psp->callback_name = NULL;
  59.   
  60.   /*  The shell and main vbox  */
  61.   psp->shell = gimp_dialog_new (title ? title : _("Palette Selection"),
  62.                 "palette_selection",
  63.                 gimp_standard_help_func,
  64.                 "dialogs/palette_selection.html",
  65.                 GTK_WIN_POS_MOUSE,
  66.                 FALSE, TRUE, FALSE,
  67.  
  68.                 _("Edit"), palette_select_edit_callback,
  69.                 psp, NULL, NULL, TRUE, FALSE,
  70.                 _("Close"), palette_select_close_callback,
  71.                 psp, NULL, NULL, FALSE, TRUE,
  72.  
  73.                 NULL);
  74.  
  75.   vbox = gtk_vbox_new (FALSE, 1);
  76.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 1);
  77.   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (psp->shell)->vbox), vbox);
  78.  
  79.   hbox = gtk_hbox_new (FALSE, 8);
  80.   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
  81.   gtk_widget_show (hbox);
  82.  
  83.   /* clist preview of gradients */
  84.   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  85.   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
  86.                   GTK_POLICY_AUTOMATIC,
  87.                   GTK_POLICY_ALWAYS);
  88.   gtk_box_pack_start (GTK_BOX (hbox), scrolled_win, TRUE, TRUE, 0); 
  89.   gtk_widget_show (scrolled_win);
  90.  
  91.   titles[0] = _("Palette");
  92.   titles[1] = _("Ncols");
  93.   titles[2] = _("Name");
  94.   psp->clist = gtk_clist_new_with_titles (3, titles);
  95.   gtk_clist_set_shadow_type (GTK_CLIST (psp->clist), GTK_SHADOW_IN);
  96.   gtk_clist_set_row_height (GTK_CLIST (psp->clist), SM_PREVIEW_HEIGHT + 2);
  97.   gtk_clist_set_use_drag_icons (GTK_CLIST (psp->clist), FALSE);
  98.   gtk_clist_column_titles_passive (GTK_CLIST (psp->clist));
  99.   gtk_widget_set_usize (psp->clist, 203, 200);
  100.   gtk_clist_set_column_width (GTK_CLIST (psp->clist), 0, SM_PREVIEW_WIDTH + 2);
  101.   gtk_container_add (GTK_CONTAINER (scrolled_win), psp->clist);
  102.   gtk_widget_show (psp->clist);
  103.  
  104.   select_pos = -1;
  105.   if (initial_palette && strlen (initial_palette))
  106.     {
  107.       for (list = palette_entries_list; list; list = g_slist_next (list))
  108.     {
  109.       p_entries = (PaletteEntries *) list->data;
  110.       
  111.       if (strcmp (p_entries->name, initial_palette) > 0)
  112.         break;
  113.  
  114.       select_pos++;
  115.     }
  116.     }
  117.  
  118.   gtk_widget_realize (psp->shell);
  119.   psp->gc = gdk_gc_new (psp->shell->window);  
  120.   
  121.   palette_clist_init (psp->clist, psp->shell, psp->gc);
  122.   gtk_signal_connect (GTK_OBJECT (psp->clist), "button_press_event",
  123.               GTK_SIGNAL_FUNC (palette_select_button_press),
  124.               (gpointer) psp);
  125.  
  126.   /* Now show the dialog */
  127.   gtk_widget_show (vbox);
  128.   gtk_widget_show (psp->shell);
  129.  
  130.   if (select_pos != -1) 
  131.     {
  132.       gtk_clist_select_row (GTK_CLIST (psp->clist), select_pos, -1);
  133.       gtk_clist_moveto (GTK_CLIST (psp->clist), select_pos, 0, 0.0, 0.0); 
  134.     }
  135.   else
  136.     gtk_clist_select_row (GTK_CLIST (psp->clist), 0, -1);
  137.  
  138.   active_dialogs = g_slist_append (active_dialogs, psp);
  139.  
  140.   return psp;
  141. }
  142.  
  143. void
  144. palette_select_clist_insert_all (PaletteEntries *p_entries)
  145. {
  146.   PaletteEntries *chk_entries;
  147.   PaletteSelect *psp; 
  148.   GSList *list;
  149.   gint pos = 0;
  150.  
  151.   for (list = palette_entries_list; list; list = g_slist_next (list))
  152.     {
  153.       chk_entries = (PaletteEntries *) list->data;
  154.       
  155.       /*  to make sure we get something!  */
  156.       if (chk_entries == NULL)
  157.     return;
  158.  
  159.       if (strcmp (p_entries->name, chk_entries->name) == 0)
  160.     break;
  161.  
  162.       pos++;
  163.     }
  164.  
  165.   for (list = active_dialogs; list; list = g_slist_next (list))
  166.     {
  167.       psp = (PaletteSelect *) list->data;
  168.  
  169.       gtk_clist_freeze (GTK_CLIST (psp->clist));
  170.       palette_clist_insert (psp->clist, psp->shell, psp->gc, p_entries, pos);
  171.       gtk_clist_thaw (GTK_CLIST (psp->clist));
  172.     }
  173. }
  174.  
  175. void
  176. palette_select_set_text_all (PaletteEntries *entries)
  177. {
  178.   PaletteEntries *p_entries = NULL;
  179.   PaletteSelect *psp; 
  180.   GSList *list;
  181.   gchar *num_buf;
  182.   gint pos = 0;
  183.  
  184.   for (list = palette_entries_list; list;  list = g_slist_next (list))
  185.     {
  186.       p_entries = (PaletteEntries *) list->data;
  187.       
  188.       if (p_entries == entries)
  189.     break;
  190.  
  191.       pos++;
  192.     }
  193.  
  194.   if (p_entries == NULL)
  195.     return; /* This is actually an error */
  196.  
  197.   num_buf = g_strdup_printf ("%d",p_entries->n_colors);;
  198.  
  199.   for (list = active_dialogs; list; list = g_slist_next (list))
  200.     {
  201.       psp = (PaletteSelect *) list->data;
  202.  
  203.       gtk_clist_set_text (GTK_CLIST (psp->clist), pos, 1, num_buf);
  204.     }
  205.  
  206.   g_free (num_buf);
  207. }
  208.  
  209. void
  210. palette_select_refresh_all ()
  211. {
  212.   PaletteSelect *psp; 
  213.   GSList *list;
  214.  
  215.   for (list = active_dialogs; list; list = g_slist_next (list))
  216.     {
  217.       psp = (PaletteSelect *) list->data;
  218.  
  219.       gtk_clist_freeze (GTK_CLIST (psp->clist));
  220.       gtk_clist_clear (GTK_CLIST (psp->clist));
  221.       palette_clist_init (psp->clist, psp->shell, psp->gc);
  222.       gtk_clist_thaw (GTK_CLIST (psp->clist));
  223.     }
  224. }
  225.  
  226. /*  local functions  */
  227.  
  228. static gint
  229. palette_select_button_press (GtkWidget      *widget,
  230.                  GdkEventButton *bevent,
  231.                  gpointer        data)
  232. {
  233.   PaletteSelect *psp;
  234.  
  235.   psp = (PaletteSelect *) data;
  236.  
  237.   if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS)
  238.     {
  239.       palette_select_edit_callback (widget, data);
  240.  
  241.       return TRUE;
  242.     }
  243.  
  244.   return FALSE;
  245. }
  246.  
  247. static void
  248. palette_select_edit_callback (GtkWidget *widget,
  249.                   gpointer   data)
  250. {
  251.   PaletteEntries *p_entries = NULL;
  252.   PaletteSelect *psp = (PaletteSelect *) data;
  253.   GList *sel_list;
  254.  
  255.   sel_list = GTK_CLIST (psp->clist)->selection;
  256.  
  257.   while (sel_list)
  258.     {
  259.       gint row;
  260.  
  261.       row = GPOINTER_TO_INT (sel_list->data);
  262.  
  263.       p_entries = 
  264.     (PaletteEntries *) gtk_clist_get_row_data (GTK_CLIST (psp->clist), row);
  265.  
  266.       palette_create_edit (p_entries);
  267.  
  268.       /* One only */
  269.       return;
  270.     }
  271. }
  272.  
  273. static void
  274. palette_select_free (PaletteSelect *psp)
  275. {
  276.   if (psp)
  277.     {
  278.       /*
  279.       if(psp->callback_name)
  280.     g_free (gsp->callback_name);
  281.       */
  282.  
  283.       /* remove from active list */
  284.       active_dialogs = g_slist_remove (active_dialogs, psp); 
  285.  
  286.       g_free (psp);
  287.     }
  288. }
  289.  
  290. static void
  291. palette_select_close_callback (GtkWidget *widget,
  292.                    gpointer   data)
  293. {
  294.   PaletteSelect *psp;
  295.  
  296.   psp = (PaletteSelect *) data;
  297.  
  298.   if (GTK_WIDGET_VISIBLE (psp->shell))
  299.     gtk_widget_hide (psp->shell);
  300.  
  301.   gtk_widget_destroy (psp->shell); 
  302.   palette_select_free (psp); 
  303. }
  304.