gimpwidgets.c 38.9 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1999 Peter Mattis and Spencer Kimball
3 4 5 6 7 8 9
 *
 * gimpwidgets.c
 * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10 11 12 13 14
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 16 17 18 19 20 21 22
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23 24
#include "config.h"

25 26
#include <gtk/gtk.h>

27
#include "gimpchainbutton.h"
28
#include "gimphelpui.h"
29
#include "gimppixmap.h"
30
#include "gimpunitmenu.h"
31
#include "gimpwidgets.h"
32 33
#include "gimpmath.h"

34
#include "libgimp-intl.h"
35

36

37
/*
38
 *  Widget Constructors
39 40
 */

41 42
/**
 * gimp_option_menu_new:
43 44
 * @menu_only: #TRUE if the function should return a #GtkMenu only.
 * @...: A #NULL terminated @va_list describing the menu items.
45
 *
46
 * Returns: A #GtkOptionMenu or a #GtkMenu (depending on @menu_only).
47 48
 *
 */
49
GtkWidget *
50
gimp_option_menu_new (gboolean            menu_only,
51 52

		      /* specify menu items as va_list:
53 54 55 56 57 58
		       *  gchar          *label,
		       *  GtkSignalFunc   callback,
		       *  gpointer        data,
		       *  gpointer        user_data,
		       *  GtkWidget     **widget_ptr,
		       *  gboolean        active
59 60
		       */

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		       ...)
{
  GtkWidget *menu;
  GtkWidget *menuitem;

  /*  menu item variables  */
  gchar          *label;
  GtkSignalFunc   callback;
  gpointer        data;
  gpointer        user_data;
  GtkWidget     **widget_ptr;
  gboolean        active;

  va_list args;
  gint    i;
  gint    initial_index;

  menu = gtk_menu_new ();

  /*  create the menu items  */
  initial_index = 0;
  va_start (args, menu_only);
  label = va_arg (args, gchar*);
  for (i = 0; label; i++)
    {
      callback   = va_arg (args, GtkSignalFunc);
      data       = va_arg (args, gpointer);
      user_data  = va_arg (args, gpointer);
      widget_ptr = va_arg (args, GtkWidget **);
      active     = va_arg (args, gboolean);

92 93 94 95 96 97 98 99 100 101 102
      if (strcmp (label, "---"))
	{
	  menuitem = gtk_menu_item_new_with_label (label);

	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			      callback,
			      data);

	  if (user_data)
	    gtk_object_set_user_data (GTK_OBJECT (menuitem), user_data);
	}
103
      else
104 105 106
	{
	  menuitem = gtk_menu_item_new ();
	}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

      gtk_menu_append (GTK_MENU (menu), menuitem);

      if (widget_ptr)
	*widget_ptr = menuitem;

      gtk_widget_show (menuitem);

      /*  remember the initial menu item  */
      if (active)
	initial_index = i;

      label = va_arg (args, gchar*);
    }
  va_end (args);

  if (! menu_only)
    {
      GtkWidget *optionmenu;

      optionmenu = gtk_option_menu_new ();
      gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);

      /*  select the initial menu item  */
      gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), initial_index);

      return optionmenu;
    }

  return menu;
}

139 140
/**
 * gimp_option_menu_new2:
141 142 143 144 145 146
 * @menu_only: #TRUE if the function should return a #GtkMenu only.
 * @menu_item_callback: The callback each menu item's "activate" signal will
 *                      be connected with.
 * @data: The data which will be passed to gtk_signal_connect().
 * @initial: The @user_data of the initially selected menu item.
 * @...: A #NULL terminated @va_list describing the menu items.
147
 *
148
 * Returns: A #GtkOptionMenu or a #GtkMenu (depending on @menu_only).
149 150
 *
 */
151 152 153 154 155 156 157 158 159 160 161 162 163
GtkWidget *
gimp_option_menu_new2 (gboolean        menu_only,
		       GtkSignalFunc   menu_item_callback,
		       gpointer        data,
		       gpointer        initial, /* user_data */

		       /* specify menu items as va_list:
			*  gchar      *label,
			*  gpointer    user_data,
			*  GtkWidget **widget_ptr,
			*/

		       ...)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
  GtkWidget *menu;
  GtkWidget *menuitem;

  /*  menu item variables  */
  gchar      *label;
  gpointer    user_data;
  GtkWidget **widget_ptr;

  va_list args;
  gint    i;
  gint    initial_index;

  menu = gtk_menu_new ();

  /*  create the menu items  */
  initial_index = 0;
  va_start (args, initial);
  label = va_arg (args, gchar*);
  for (i = 0; label; i++)
    {
      user_data  = va_arg (args, gpointer);
186
      widget_ptr = va_arg (args, GtkWidget **);
187

188 189 190 191 192 193 194 195 196 197 198
      if (strcmp (label, "---"))
	{
	  menuitem = gtk_menu_item_new_with_label (label);

	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			      menu_item_callback,
			      data);

	  if (user_data)
	    gtk_object_set_user_data (GTK_OBJECT (menuitem), user_data);
	}
199
      else
200 201 202
	{
	  menuitem = gtk_menu_item_new ();
	}
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      gtk_menu_append (GTK_MENU (menu), menuitem);

      if (widget_ptr)
	*widget_ptr = menuitem;

      gtk_widget_show (menuitem);

      /*  remember the initial menu item  */
      if (user_data == initial)
	initial_index = i;

      label = va_arg (args, gchar*);
    }
  va_end (args);

219 220 221 222 223 224
  if (! menu_only)
    {
      GtkWidget *optionmenu;

      optionmenu = gtk_option_menu_new ();
      gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
225

226 227 228 229 230
      /*  select the initial menu item  */
      gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), initial_index);

      return optionmenu;
    }
231

232
  return menu;
233 234
}

235 236
/**
 * gimp_option_menu_set_history:
237 238 239
 * @option_menu: A #GtkOptionMenu as returned by gimp_option_menu_new() or
 *               gimp_option_menu_new2().
 * @user_data: The @user_data of the menu item you want to select.
240 241
 *
 */
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
void
gimp_option_menu_set_history (GtkOptionMenu *option_menu,
			      gpointer       user_data)
{
  GtkWidget *menu_item;
  GList     *list;
  gint       history = 0;

  g_return_if_fail (option_menu);
  g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));

  for (list = GTK_MENU_SHELL (option_menu->menu)->children;
       list;
       list = g_list_next (list))
    {
      menu_item = GTK_WIDGET (list->data);

      if (GTK_IS_LABEL (GTK_BIN (menu_item)->child) &&
	  gtk_object_get_user_data (GTK_OBJECT (menu_item)) == user_data)
	{
	  break;
	}

      history++;
    }

  if (list)
    gtk_option_menu_set_history (option_menu, history);
}

272 273
/**
 * gimp_radio_group_new:
274 275 276
 * @in_frame: #TRUE if you want a #GtkFrame around the radio button group.
 * @frame_title: The title of the Frame or #NULL if you don't want a title.
 * @...: A #NULL terminated @va_list describing the radio buttons.
277
 *
278
 * Returns: A #GtkFrame or #GtkVbox (depending on @in_frame).
279 280
 *
 */
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
GtkWidget *
gimp_radio_group_new (gboolean            in_frame,
		      gchar              *frame_title,

		      /* specify radio buttons as va_list:
		       *  gchar          *label,
		       *  GtkSignalFunc   callback,
		       *  gpointer        data,
		       *  gpointer        user_data,
		       *  GtkWidget     **widget_ptr,
		       *  gboolean        active,
		       */

		      ...)
{
  GtkWidget *vbox;
  GtkWidget *button;
  GSList    *group;

  /*  radio button variables  */
  gchar          *label;
  GtkSignalFunc   callback;
  gpointer        data;
  gpointer        user_data;
  GtkWidget     **widget_ptr;
  gboolean        active;

  va_list args;

  vbox = gtk_vbox_new (FALSE, 1);

  group = NULL;

  /*  create the radio buttons  */
  va_start (args, frame_title);
  label = va_arg (args, gchar*);
  while (label)
    {
      callback   = va_arg (args, GtkSignalFunc);
      data       = va_arg (args, gpointer);
      user_data  = va_arg (args, gpointer);
322
      widget_ptr = va_arg (args, GtkWidget **);
323 324
      active     = va_arg (args, gboolean);

325 326 327 328 329
      if (label != (gpointer) 1)
	button = gtk_radio_button_new_with_label (group, label);
      else
	button = gtk_radio_button_new (group);

330 331 332 333 334 335 336 337 338 339 340 341
      group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

      if (user_data)
	gtk_object_set_user_data (GTK_OBJECT (button), user_data);

      if (widget_ptr)
	*widget_ptr = button;

      if (active)
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

342
      gtk_signal_connect (GTK_OBJECT (button), "toggled",
343
			  callback,
344 345
			  data);

346 347 348 349 350 351 352
      gtk_widget_show (button);

      label = va_arg (args, gchar*);
    }
  va_end (args);

  if (in_frame)
353 354 355
    {
      GtkWidget *frame;

Michael Natterer's avatar
Michael Natterer committed
356 357
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);

358 359 360 361 362 363
      frame = gtk_frame_new (frame_title);
      gtk_container_add (GTK_CONTAINER (frame), vbox);
      gtk_widget_show (vbox);

      return frame;
    }
364 365 366 367

  return vbox;
}

368 369
/**
 * gimp_radio_group_new2:
370 371 372 373 374 375 376
 * @in_frame: #TRUE if you want a #GtkFrame around the radio button group.
 * @frame_title: The title of the Frame or #NULL if you don't want a title.
 * @radio_button_callback: The callback each button's "toggled" signal will
 *                         be connected with.
 * @data: The data which will be passed to gtk_signal_connect().
 * @initial: The @user_data of the initially pressed radio button.
 * @...: A #NULL terminated @va_list describing the radio buttons.
377
 *
378
 * Returns: A #GtkFrame or #GtkVbox (depending on @in_frame).
379 380
 *
 */
381 382 383
GtkWidget *
gimp_radio_group_new2 (gboolean        in_frame,
		       gchar          *frame_title,
384
		       GtkSignalFunc   radio_button_callback,
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		       gpointer        data,
		       gpointer        initial, /* user_data */

		       /* specify radio buttons as va_list:
			*  gchar      *label,
			*  gpointer    user_data,
			*  GtkWidget **widget_ptr,
			*/

		       ...)
{
  GtkWidget *vbox;
  GtkWidget *button;
  GSList    *group;

  /*  radio button variables  */
  gchar      *label;
  gpointer    user_data;
  GtkWidget **widget_ptr;

  va_list args;

  vbox = gtk_vbox_new (FALSE, 1);

  group = NULL;

  /*  create the radio buttons  */
  va_start (args, initial);
  label = va_arg (args, gchar*);
  while (label)
    {
      user_data  = va_arg (args, gpointer);
417
      widget_ptr = va_arg (args, GtkWidget **);
418

419 420 421 422 423
      if (label != (gpointer) 1)
	button = gtk_radio_button_new_with_label (group, label);
      else
	button = gtk_radio_button_new (group);

424 425 426 427 428 429 430 431 432 433 434 435
      group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
      gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

      if (user_data)
	gtk_object_set_user_data (GTK_OBJECT (button), user_data);

      if (widget_ptr)
	*widget_ptr = button;

      if (initial == user_data)
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

436
      gtk_signal_connect (GTK_OBJECT (button), "toggled",
437
			  radio_button_callback,
438 439
			  data);

440 441 442 443 444 445 446
      gtk_widget_show (button);

      label = va_arg (args, gchar*);
    }
  va_end (args);

  if (in_frame)
447 448 449
    {
      GtkWidget *frame;

Michael Natterer's avatar
Michael Natterer committed
450 451
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);

452 453 454 455 456 457
      frame = gtk_frame_new (frame_title);
      gtk_container_add (GTK_CONTAINER (frame), vbox);
      gtk_widget_show (vbox);

      return frame;
    }
458 459 460 461

  return vbox;
}

462 463
/**
 * gimp_spin_button_new:
464 465 466 467 468 469 470 471 472 473 474 475 476
 * @adjustment: Returns the spinbutton's #GtkAdjustment.
 * @value: The initial value of the spinbutton.
 * @lower: The lower boundary.
 * @upper: The uppper boundary.
 * @step_increment: The spinbutton's step increment.
 * @page_increment: The spinbutton's page increment (mouse button 2).
 * @page_size: The spinbutton's page size.
 * @climb_rate: The spinbutton's climb rate.
 * @digits: The spinbutton's number of decimal digits.
 *
 * This function is a shortcut for gtk_adjustment_new() and a subsequent
 * gtk_spin_button_new() and does some more initialisation stuff like
 * setting a standard minimun horizontal size.
477
 *
478
 * Returns: A #GtkSpinbutton and it's #GtkAdjustment.
479 480
 *
 */
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
GtkWidget *
gimp_spin_button_new (GtkObject **adjustment,  /* return value */
		      gfloat      value,
		      gfloat      lower,
		      gfloat      upper,
		      gfloat      step_increment,
		      gfloat      page_increment,
		      gfloat      page_size,
		      gfloat      climb_rate,
		      guint       digits)
{
  GtkWidget *spinbutton;

  *adjustment = gtk_adjustment_new (value, lower, upper,
				    step_increment, page_increment, page_size);

  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (*adjustment),
				    climb_rate, digits);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				   GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, -1);

  return spinbutton;
}

507 508 509 510 511 512 513 514 515 516 517
static void
gimp_scale_entry_unconstrained_adjustment_callback (GtkAdjustment *adjustment,
						    GtkAdjustment *other_adj)
{
  gtk_signal_handler_block_by_data (GTK_OBJECT (other_adj), adjustment);

  gtk_adjustment_set_value (other_adj, adjustment->value);

  gtk_signal_handler_unblock_by_data (GTK_OBJECT (other_adj), adjustment);
}

518 519
/**
 * gimp_scale_entry_new:
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
 * @table: The #GtkTable the widgets will be attached to.
 * @column: The column to start with.
 * @row: The row to attach the widgets.
 * @text: The text for the #GtkLabel which will appear left of the #GtkHScale.
 * @scale_usize: The minimum horizontal size of the #GtkHScale.
 * @spinbutton_usize: The minimum horizontal size of the #GtkSpinButton.
 * @value: The initial value.
 * @lower: The lower boundary.
 * @upper: The upper boundary.
 * @step_increment: The step increment.
 * @page_increment: The page increment.
 * @digits: The number of decimal digits.
 * @constrain: #TRUE if the range of possible values of the #GtkSpinButton
 *             should be the same as of the #GtkHScale.
 * @unconstrained_lower: The spinbutton's lower boundary
 *                       if @constrain == #FALSE.
 * @unconstrained_upper: The spinbutton's upper boundary
 *                       if @constrain == #FALSE.
 * @tooltip: A tooltip message for the scale and the spinbutton.
539
 * @help_data: The widgets' help_data (see gimp_help_set_help_data()).
540 541 542 543
 *
 * This function creates a #GtkLabel, a #GtkHScale and a #GtkSpinButton and
 * attaches them to a 3-column #GtkTable.
 *
544
 * Note that if you pass a @tooltip or @help_data to this function you'll
545
 * have to initialize GIMP's help system with gimp_help_init() before using it.
546
 *
547
 * Returns: The #GtkSpinButton's #GtkAdjustment.
548 549
 *
 */
550 551 552 553 554 555 556 557 558 559 560 561 562
GtkObject *
gimp_scale_entry_new (GtkTable *table,
		      gint      column,
		      gint      row,
		      gchar    *text,
		      gint      scale_usize,
		      gint      spinbutton_usize,
		      gfloat    value,
		      gfloat    lower,
		      gfloat    upper,
		      gfloat    step_increment,
		      gfloat    page_increment,
		      guint     digits,
563 564 565
		      gboolean  constrain,
		      gfloat    unconstrained_lower,
		      gfloat    unconstrained_upper,
566
		      gchar    *tooltip,
567
		      gchar    *help_data)
568 569 570 571 572
{
  GtkWidget *label;
  GtkWidget *scale;
  GtkWidget *spinbutton;
  GtkObject *adjustment;
573
  GtkObject *return_adj;
574 575 576 577 578 579 580 581

  label = gtk_label_new (text);
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label,
                    column, column + 1, row, row + 1,
                    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  if (! constrain &&
      unconstrained_lower <= lower &&
      unconstrained_upper >= upper)
    {
      GtkObject *constrained_adj;

      constrained_adj = gtk_adjustment_new (value, lower, upper,
					    step_increment, page_increment,
					    0.0);

      spinbutton = gimp_spin_button_new (&adjustment, value,
					 unconstrained_lower,
					 unconstrained_upper,
					 step_increment, page_increment, 0.0,
					 1.0, digits);

      gtk_signal_connect
	(GTK_OBJECT (constrained_adj), "value_changed",
	 GTK_SIGNAL_FUNC (gimp_scale_entry_unconstrained_adjustment_callback),
	 adjustment);

      gtk_signal_connect
	(GTK_OBJECT (adjustment), "value_changed",
	 GTK_SIGNAL_FUNC (gimp_scale_entry_unconstrained_adjustment_callback),
	 constrained_adj);

      return_adj = adjustment;

      adjustment = constrained_adj;
    }
  else
    {
      spinbutton = gimp_spin_button_new (&adjustment, value, lower, upper,
					 step_increment, page_increment, 0.0,
					 1.0, digits);

      return_adj = adjustment;
    }
    
621 622 623 624 625 626 627 628 629 630
  if (spinbutton_usize > 0)
    gtk_widget_set_usize (spinbutton, spinbutton_usize, -1);

  scale = gtk_hscale_new (GTK_ADJUSTMENT (adjustment));
  if (scale_usize > 0)
    gtk_widget_set_usize (scale, scale_usize, -1);
  gtk_scale_set_digits (GTK_SCALE (scale), digits);
  gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
  gtk_table_attach (GTK_TABLE (table), scale,
		    column + 1, column + 2, row, row + 1,
631
		    GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
632 633 634 635
  gtk_widget_show (scale);

  gtk_table_attach (GTK_TABLE (table), spinbutton,
		    column + 2, column + 3, row, row + 1,
636
		    GTK_SHRINK, GTK_SHRINK, 0, 0);
637 638
  gtk_widget_show (spinbutton);

639
  if (tooltip || help_data)
640
    {
641 642
      gimp_help_set_help_data (scale, tooltip, help_data);
      gimp_help_set_help_data (spinbutton, tooltip, help_data);
643 644
    }

Michael Natterer's avatar
Michael Natterer committed
645 646 647 648
  gtk_object_set_data (GTK_OBJECT (return_adj), "label", label);
  gtk_object_set_data (GTK_OBJECT (return_adj), "scale", scale);
  gtk_object_set_data (GTK_OBJECT (return_adj), "spinbutton", spinbutton);

649
  return return_adj;
650 651
}

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
static void
gimp_random_seed_toggle_update (GtkWidget *widget,
				gpointer   data)
{
  gint *toggle_val;

  toggle_val = (gint *) data;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    *toggle_val = (gint) gtk_object_get_data (GTK_OBJECT (widget),
					      "time_true");
  else
    *toggle_val = (gint) gtk_object_get_data (GTK_OBJECT (widget),
					      "time_false");

  gimp_toggle_button_sensitive_update (GTK_TOGGLE_BUTTON (widget));
}

670 671
/**
 * gimp_random_seed_new:
672 673 674 675 676 677
 * @seed: A pointer to the variable which stores the random seed.
 * @use_time: A pointer to the variable which stores the @use_time
 *            toggle boolean.
 * @time_true: The value to write to @use_time if the toggle button is checked.
 * @time_false: The value to write to @use_time if the toggle button is
 *              unchecked.
678
 *
679 680 681 682 683 684
 * Note that this widget automatically sets tooltips with
 * gimp_help_set_help_data(), so you'll have to initialize GIMP's help
 * system with gimp_help_init() before using it.
 *
 * Returns: A #GtkHBox containing a #GtkSpinButton for the random seed and
 *          a #GtkToggleButton for toggling the @use_time behaviour.
685 686
 *
 */
687
GtkWidget *
688 689 690 691
gimp_random_seed_new (gint       *seed,
		      gint       *use_time,
		      gint        time_true,
		      gint        time_false)
692 693 694 695
{
  GtkWidget *hbox;
  GtkWidget *spinbutton;
  GtkObject *adj;
696
  GtkWidget *button;
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

  hbox = gtk_hbox_new (FALSE, 4);

  spinbutton = gimp_spin_button_new (&adj, *seed,
                                     0, G_MAXRAND, 1, 10, 0, 1, 0);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
                      GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
                      seed);
  gtk_widget_show (spinbutton);

  gimp_help_set_help_data (spinbutton,
                           _("If the \"Time\" button is not pressed, "
                             "use this value for random number generator "
                             "seed - this allows you to repeat a "
                             "given \"random\" operation"), NULL);

714 715 716
  button = gtk_toggle_button_new_with_label (_("Time"));
  gtk_misc_set_padding (GTK_MISC (GTK_BIN (button)->child), 2, 0);
  gtk_signal_connect (GTK_OBJECT (button), "toggled",
717 718
                      GTK_SIGNAL_FUNC (gimp_random_seed_toggle_update),
                      use_time);
719 720 721 722
  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  gimp_help_set_help_data (button,
723 724 725 726
                           _("Seed random number generator from the current "
                             "time - this guarantees a reasonable "
                             "randomization"), NULL);

727
  gtk_object_set_data (GTK_OBJECT (button), "time_true",
728
		       (gpointer) time_true);
729
  gtk_object_set_data (GTK_OBJECT (button), "time_false",
730 731
		       (gpointer) time_false);

732
  gtk_object_set_data (GTK_OBJECT (button), "inverse_sensitive",
733 734
		       spinbutton);

735
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
736 737
                                *use_time == time_true);

738 739 740
  gtk_object_set_data (GTK_OBJECT (hbox), "spinbutton", spinbutton);
  gtk_object_set_data (GTK_OBJECT (hbox), "togglebutton", button);

741 742 743
  return hbox;
}

744 745 746
typedef struct
{
  GimpChainButton *chainbutton;
747 748 749
  gboolean         chain_constrains_ratio;
  gdouble          orig_x;
  gdouble          orig_y;
750 751 752 753 754
  gdouble          last_x;
  gdouble          last_y;
} GimpCoordinatesData;

static void
755
gimp_coordinates_callback (GtkWidget *widget,
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
			   gpointer   data)
{
  GimpCoordinatesData *gcd;
  gdouble new_x;
  gdouble new_y;

  gcd = (GimpCoordinatesData *) data;

  new_x = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0);
  new_y = gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 1);

  if (gimp_chain_button_get_active (gcd->chainbutton))
    {
      gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "value_changed");

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
      if (gcd->chain_constrains_ratio)
	{
	  if ((gcd->orig_x != 0) && (gcd->orig_y != 0))
	    {
	      if (new_x != gcd->last_x)
		{
		  gcd->last_x = new_x;
		  gcd->last_y = new_y = (new_x * gcd->orig_y) / gcd->orig_x;
		  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 1,
					      new_y);
		}
	      else if (new_y != gcd->last_y)
		{
		  gcd->last_y = new_y;
		  gcd->last_x = new_x = (new_y * gcd->orig_x) / gcd->orig_y;
		  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 0,
					      new_x);
		}
	    }
	}
      else
	{
	  if (new_x != gcd->last_x)
	    {
	      gcd->last_y = new_y = gcd->last_x = new_x;
	      gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 1, new_x);
	    }
	  else if (new_y != gcd->last_y)
	    {
	      gcd->last_x = new_x = gcd->last_y = new_y;
	      gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 0, new_y);
	    }
	}
804 805 806 807 808 809 810 811 812 813
    }
  else
    {
      if (new_x != gcd->last_x)
        gcd->last_x = new_x;
      if (new_y != gcd->last_y)
        gcd->last_y = new_y;
    }     
}

814 815
/**
 * gimp_coordinates_new:
816
 * @unit: The initial unit of the #GimpUnitMenu.
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
 * @unit_format: The unit format string as passed to gimp_size_entry_new().
 * @menu_show_pixels: #TRUE if the #GimpUnitMenu should contain an item for
 *                    GIMP_UNIT_PIXEL.
 * @menu_show_percent: #TRUE if the #GimpUnitMenu should contain an item for
 *                     GIMP_UNIT_PERCENT.
 * @spinbutton_usize: The horizontal usize of the #GimpSizeEntry's
 *                    #GtkSpinButton's.
 * @update_policy: The update policy for the #GimpSizeEntry.
 * @chainbutton_active: #TRUE if the attached #GimpChainButton should be
 *                      active.
 * @chain_constrains_ratio: #TRUE if the chainbutton should constrain the
 *                          fields' aspect ratio. If #FALSE, the values will
 *                          be constrained.
 * @xlabel: The label for the X coordinate.
 * @x: The initial value of the X coordinate.
 * @xres: The horizontal resolution in DPI.
 * @lower_boundary_x: The lower boundary of the X coordinate.
 * @upper_boundary_x: The upper boundary of the X coordinate.
 * @xsize_0: The X value which will be treated as 0%.
 * @xsize_100: The X value which will be treated as 100%.
 * @ylabel: The label for the Y coordinate.
 * @y: The initial value of the Y coordinate.
 * @yres: The vertical resolution in DPI.
 * @lower_boundary_y: The lower boundary of the Y coordinate.
 * @upper_boundary_y: The upper boundary of the Y coordinate.
 * @ysize_0: The Y value which will be treated as 0%.
 * @ysize_100: The Y value which will be treated as 100%.
844
 *
845 846 847
 * Returns: A #GimpSizeEntry with two fields for x/y coordinates/sizes with
 *          a #GimpChainButton attached to constrain either the two fields'
 *          values or the ratio between them.
848 849
 *
 */
850
GtkWidget *
851
gimp_coordinates_new (GimpUnit         unit,
852 853 854 855
		      gchar           *unit_format,
		      gboolean         menu_show_pixels,
		      gboolean         menu_show_percent,
		      gint             spinbutton_usize,
856
		      GimpSizeEntryUpdatePolicy  update_policy,
857

858 859 860
		      gboolean         chainbutton_active,
		      gboolean         chain_constrains_ratio,

861 862 863
		      gchar           *xlabel,
		      gdouble          x,
		      gdouble          xres,
864 865 866 867
		      gdouble          lower_boundary_x,
		      gdouble          upper_boundary_x,
		      gdouble          xsize_0,   /* % */
		      gdouble          xsize_100, /* % */
868 869 870 871

		      gchar           *ylabel,
		      gdouble          y,
		      gdouble          yres,
872 873 874 875
		      gdouble          lower_boundary_y,
		      gdouble          upper_boundary_y,
		      gdouble          ysize_0,   /* % */
		      gdouble          ysize_100  /* % */)
876 877 878 879 880
{
  GimpCoordinatesData *gcd;
  GtkObject *adjustment;
  GtkWidget *spinbutton;
  GtkWidget *sizeentry;
881
  GtkWidget *chainbutton;
882 883 884 885 886 887 888 889 890

  spinbutton = gimp_spin_button_new (&adjustment, 1, 0, 1, 1, 10, 1, 1, 2);
  sizeentry = gimp_size_entry_new (1, unit, unit_format,
				   menu_show_pixels,
				   menu_show_percent,
				   FALSE,
				   spinbutton_usize,
				   update_policy);
  gtk_table_set_col_spacing (GTK_TABLE (sizeentry), 0, 4);  
891
  gtk_table_set_col_spacing (GTK_TABLE (sizeentry), 2, 4);  
892 893 894 895 896
  gimp_size_entry_add_field (GIMP_SIZE_ENTRY (sizeentry),
                             GTK_SPIN_BUTTON (spinbutton), NULL);
  gtk_table_attach_defaults (GTK_TABLE (sizeentry), spinbutton, 1, 2, 0, 1);
  gtk_widget_show (spinbutton);

897
  gimp_size_entry_set_unit (GIMP_SIZE_ENTRY (sizeentry),
898
			    (update_policy == GIMP_SIZE_ENTRY_UPDATE_RESOLUTION) || (menu_show_pixels == FALSE) ?
899
			    GIMP_UNIT_INCH : GIMP_UNIT_PIXEL);
900 901 902 903 904 905 906 907

  gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (sizeentry), 0, xres, TRUE);
  gimp_size_entry_set_resolution (GIMP_SIZE_ENTRY (sizeentry), 1, yres, TRUE);
  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (sizeentry), 0,
					 lower_boundary_x, upper_boundary_x);
  gimp_size_entry_set_refval_boundaries (GIMP_SIZE_ENTRY (sizeentry), 1,
					 lower_boundary_y, upper_boundary_y);

908 909 910 911 912 913 914 915
  if (menu_show_percent)
    {
      gimp_size_entry_set_size (GIMP_SIZE_ENTRY (sizeentry), 0,
				xsize_0, xsize_100);
      gimp_size_entry_set_size (GIMP_SIZE_ENTRY (sizeentry), 1,
				ysize_0, ysize_100);
    }

916 917 918 919 920 921
  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (sizeentry), 0, x);
  gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (sizeentry), 1, y);

  gimp_size_entry_attach_label (GIMP_SIZE_ENTRY (sizeentry), xlabel, 0, 0, 1.0);
  gimp_size_entry_attach_label (GIMP_SIZE_ENTRY (sizeentry), ylabel, 1, 0, 1.0);

922
  chainbutton = gimp_chain_button_new (GIMP_CHAIN_RIGHT);
923
  if (chainbutton_active)
924 925
    gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (chainbutton), TRUE);
  gtk_table_attach (GTK_TABLE (sizeentry), chainbutton, 2, 3, 0, 2,
926
		    GTK_SHRINK | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
927
  gtk_widget_show (chainbutton);
928 929

  gcd = g_new (GimpCoordinatesData, 1);
930
  gcd->chainbutton            = GIMP_CHAIN_BUTTON (chainbutton);
931 932 933 934 935
  gcd->chain_constrains_ratio = chain_constrains_ratio;
  gcd->orig_x                 = x;
  gcd->orig_y                 = y;
  gcd->last_x                 = x;
  gcd->last_y                 = y;
936 937 938 939 940 941 942 943 944

  gtk_signal_connect_object (GTK_OBJECT (sizeentry), "destroy",
			     GTK_SIGNAL_FUNC (g_free),
			     (GtkObject *) gcd);

  gtk_signal_connect (GTK_OBJECT (sizeentry), "value_changed", 
                      GTK_SIGNAL_FUNC (gimp_coordinates_callback),
                      gcd);

945 946
  gtk_object_set_data (GTK_OBJECT (sizeentry), "chainbutton", chainbutton);

947 948 949
  return sizeentry;
}

950 951 952 953
typedef struct
{
  GtkAdjustment *adjustment;
  GtkAdjustment *divided_adj;
954
  guint          mem_size_unit;
955 956 957 958 959 960 961
} GimpMemSizeEntryData;

static void
gimp_mem_size_entry_callback (GtkAdjustment *adj,
			      gpointer       data)
{
  GimpMemSizeEntryData *gmsed;
962
  guint new_value;
963 964 965 966 967 968 969 970 971 972 973 974

  gmsed = (GimpMemSizeEntryData *)data;
  new_value = adj->value * gmsed->mem_size_unit;

  gtk_adjustment_set_value (gmsed->adjustment, new_value);
}

static void
gimp_mem_size_unit_callback (GtkWidget *widget,
			     gpointer   data)
{
  GimpMemSizeEntryData *gmsed;
975 976
  guint divided_mem_size;
  guint new_unit;
977 978 979

  gmsed = (GimpMemSizeEntryData *)data;

980
  new_unit = (guint) gtk_object_get_user_data (GTK_OBJECT (widget));
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

  if (new_unit && new_unit != gmsed->mem_size_unit)
    {
      divided_mem_size = gmsed->adjustment->value / new_unit;
      gmsed->mem_size_unit = new_unit;

      gtk_adjustment_set_value (GTK_ADJUSTMENT (gmsed->divided_adj), divided_mem_size);
    }
}

/**
 * gimp_mem_size_entry_new:
 * @adjustment: The adjustment containing the memsize and it's limits.
 *
 * Returns: A #GtkHBox with a #GtkSpinButton and a #GtkOptionMenu.
 */
GtkWidget *
gimp_mem_size_entry_new (GtkAdjustment *adjustment)
{
  GtkWidget *hbox;
  GtkObject *divided_adj;
  GtkWidget *spinbutton;
  GtkWidget *optionmenu;

  GimpMemSizeEntryData *gmsed;
1006 1007
  guint mem_size_unit = 1;
  guint divided_mem_size;  
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  gint i;

  gmsed = g_new (GimpMemSizeEntryData, 1);

  for (i = 0; i < 3; i++)
    {
      if ( (gint)adjustment->value % (mem_size_unit * 1024) != 0 )
	break;
      mem_size_unit *= 1024;
    }
  divided_mem_size =  adjustment->value / mem_size_unit;

  hbox = gtk_hbox_new (FALSE, 2);
  spinbutton =
    gimp_spin_button_new (&divided_adj, divided_mem_size,
1023
			  0.0, (4069.0 * 1024 * 1024 - 1), 1.0, 16.0, 0.0,
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
			  1.0, 0.0);
  gtk_signal_connect (GTK_OBJECT (divided_adj), "value_changed",
		      GTK_SIGNAL_FUNC (gimp_mem_size_entry_callback),
		      gmsed);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  optionmenu =
    gimp_option_menu_new2 (FALSE, gimp_mem_size_unit_callback,
			   gmsed, (gpointer) mem_size_unit,

			   _("Bytes"),     (gpointer) 1, NULL,
			   _("KiloBytes"), (gpointer) 1024, NULL,
			   _("MegaBytes"), (gpointer) (1024 * 1024), NULL,

			   NULL);
  gtk_box_pack_start (GTK_BOX (hbox), optionmenu, FALSE, FALSE, 0);
  gtk_widget_show (optionmenu);

  gtk_signal_connect_object (GTK_OBJECT (hbox), "destroy",
			     GTK_SIGNAL_FUNC (gtk_object_unref),
			     GTK_OBJECT (adjustment));
  gtk_signal_connect_object (GTK_OBJECT (hbox), "destroy",
			     GTK_SIGNAL_FUNC (g_free),
			     (GtkObject *) gmsed);

  gmsed->adjustment = adjustment;
  gmsed->divided_adj = GTK_ADJUSTMENT (divided_adj);
  gmsed->mem_size_unit = mem_size_unit;

  gtk_object_set_data (GTK_OBJECT (hbox), "spinbutton", spinbutton);
  gtk_object_set_data (GTK_OBJECT (hbox), "optionmenu", optionmenu);

  return hbox;
}

1060 1061
/**
 * gimp_pixmap_button_new:
1062 1063
 * @xpm_data: The XPM data which will be passed to gimp_pixmap_new().
 * @text: An optional text which will appear right of the pixmap.
1064
 *
1065
 * Returns: A #GtkButton with a #GimpPixmap and an optional #GtkLabel.
1066 1067
 *
 */
1068
GtkWidget *
1069 1070
gimp_pixmap_button_new (gchar **xpm_data,
			gchar  *text)
1071 1072 1073 1074 1075 1076
{
  GtkWidget *button;
  GtkWidget *pixmap;

  button = gtk_button_new ();
  pixmap = gimp_pixmap_new (xpm_data);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

  if (text)
    {
      GtkWidget *abox;
      GtkWidget *hbox;
      GtkWidget *label;

      abox = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
      gtk_container_add (GTK_CONTAINER (button), abox);
      gtk_widget_show (abox);

      hbox = gtk_hbox_new (FALSE, 0);
      gtk_container_add (GTK_CONTAINER (abox), hbox);
      gtk_widget_show (hbox);

      gtk_box_pack_start (GTK_BOX (hbox), pixmap, FALSE, FALSE, 4);
      gtk_widget_show (pixmap);

      label = gtk_label_new (text);
      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 4);
      gtk_widget_show (label);
    }
  else
    {
      gtk_container_add (GTK_CONTAINER (button), pixmap);
      gtk_widget_show (pixmap);
    }

1105 1106 1107 1108

  return button;
}

1109
/*
1110
 *  Standard Callbacks
1111 1112
 */

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
/**
 * gimp_toggle_button_sensitive_update:
 * @toggle_button: The #GtkToggleButton the "set_sensitive" and
 *                 "inverse_sensitive" lists are attached to.
 *
 * If you attached a pointer to a #GtkWidget with gtk_object_set_data() and
 * the "set_sensitive" key to the #GtkToggleButton, the sensitive state of
 * the attached widget will be set according to the toggle button's
 * "active" state.
 *
 * You can attach an arbitrary list of widgets by attaching another
 * "set_sensitive" data pointer to the first widget (and so on...).
 *
 * This function can also set the sensitive state according to the toggle
 * button's inverse "active" state by attaching widgets with the
 * "inverse_sensitive" key.
 *
 */
void
1132
gimp_toggle_button_sensitive_update (GtkToggleButton *toggle_button)
1133 1134
{
  GtkWidget *set_sensitive;
1135
  gboolean   active;
1136

1137
  active = gtk_toggle_button_get_active (toggle_button);
1138 1139

  set_sensitive =
1140
    gtk_object_get_data (GTK_OBJECT (toggle_button), "set_sensitive");
1141 1142
  while (set_sensitive)
    {
1143
      gtk_widget_set_sensitive (set_sensitive, active);
1144 1145 1146 1147 1148
      set_sensitive =
        gtk_object_get_data (GTK_OBJECT (set_sensitive), "set_sensitive");
    }

  set_sensitive =
1149
    gtk_object_get_data (GTK_OBJECT (toggle_button), "inverse_sensitive");
1150 1151
  while (set_sensitive)
    {
1152
      gtk_widget_set_sensitive (set_sensitive, ! active);
1153 1154 1155 1156 1157
      set_sensitive =
        gtk_object_get_data (GTK_OBJECT (set_sensitive), "inverse_sensitive");
    }
}

1158 1159
/**
 * gimp_toggle_button_update:
1160 1161 1162 1163 1164
 * @widget: A #GtkToggleButton.
 * @data: A pointer to a #gint variable which will store the value of
 *        gtk_toggle_button_get_active().
 *
 * Note that this function calls gimp_toggle_button_sensitive_update().
1165 1166
 *
 */
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
void
gimp_toggle_button_update (GtkWidget *widget,
			   gpointer   data)
{
  gint *toggle_val;

  toggle_val = (gint *) data;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    *toggle_val = TRUE;
  else
    *toggle_val = FALSE;

  gimp_toggle_button_sensitive_update (GTK_TOGGLE_BUTTON (widget));
}

1183 1184
/**
 * gimp_radio_button_update:
1185 1186 1187 1188 1189
 * @widget: A #GtkRadioButton.
 * @data: A pointer to a #gint variable which will store the value of
 *        (#gint) gtk_object_get_user_data().
 *
 * Note that this function calls gimp_toggle_button_sensitive_update().
1190 1191
 *
 */
1192 1193 1194 1195 1196 1197
void
gimp_radio_button_update (GtkWidget *widget,
			  gpointer   data)
{
  gint *toggle_val;

1198
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1199 1200 1201 1202 1203
    {
      toggle_val = (gint *) data;

      *toggle_val = (gint) gtk_object_get_user_data (GTK_OBJECT (widget));
    }
1204 1205

  gimp_toggle_button_sensitive_update (GTK_TOGGLE_BUTTON (widget));
1206 1207
}

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
/**
 * gimp_menu_item_update:
 * @widget: A #GtkMenuItem.
 * @data: A pointer to a #gint variable which will store the value of
 *        (#gint) gtk_object_get_user_data().
 *
 */
void
gimp_menu_item_update (GtkWidget *widget,
		       gpointer   data)
{
  gint *item_val;

  item_val = (gint *) data;

  *item_val = (gint) gtk_object_get_user_data (GTK_OBJECT (widget));
}

1226 1227
/**
 * gimp_int_adjustment_update:
1228 1229 1230 1231 1232 1233
 * @adjustment: A #GtkAdjustment.
 * @data: A pointer to a #gint variable which will store the adjustment's
 *        value.
 *
 * Note that the #GtkAdjustment's value (which is a #gfloat) will be rounded
 * with (#gint) (value + 0.5).
1234 1235
 *
 */
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
void
gimp_int_adjustment_update (GtkAdjustment *adjustment,
			    gpointer       data)
{
  gint *val;

  val = (gint *) data;
  *val = (gint) (adjustment->value + 0.5);
}

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
/**
 * gimp_uint_adjustment_update:
 * @adjustment: A #GtkAdjustment.
 * @data: A pointer to a #guint variable which will store the adjustment's
 *        value.
 *
 * Note that the #GtkAdjustment's value (which is a #gfloat) will be rounded
 * with (#guint) (value + 0.5).
 *
 */
void
gimp_uint_adjustment_update (GtkAdjustment *adjustment,
			     gpointer       data)
{
  guint *val;

  val = (guint *) data;
  *val = (guint) (adjustment->value + 0.5);
}

1266 1267
/**
 * gimp_float_adjustment_update:
1268 1269 1270
 * @adjustment: A #GtkAdjustment.
 * @data: A pointer to a #gfloat varaiable which willl store the adjustment's
 *        value.
1271 1272
 *
 */
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
void
gimp_float_adjustment_update (GtkAdjustment *adjustment,
			      gpointer       data)
{
  gfloat *val;

  val = (gfloat *) data;
  *val = adjustment->value;
}

1283 1284
/**
 * gimp_double_adjustment_update:
1285 1286 1287
 * @adjustment: A #GtkAdjustment.
 * @data: A pointer to a #gdouble variable which will store the adjustment's
 *        value.
1288 1289
 *
 */
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
void
gimp_double_adjustment_update (GtkAdjustment *adjustment,
			       gpointer       data)
{
  gdouble *val;

  val = (gdouble *) data;
  *val = adjustment->value;
}

1300 1301
/**
 * gimp_unit_menu_update:
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
 * @widget: A #GimpUnitMenu.
 * @data: A pointer to a #GimpUnit variable which will store the unit menu's
 *        value.
 *
 * This callback can set the number of decimal digits of an arbitrary number
 * of #GtkSpinButton's. To use this functionality, attach the spinbuttons
 * as list of data pointers attached with gtk_object_set_data() with the
 * "set_digits" key.
 *
 * See gimp_toggle_button_sensitive_update() for a description of how
 * to set up the list.
1313 1314
 *
 */
1315 1316 1317 1318
void
gimp_unit_menu_update (GtkWidget *widget,
		       gpointer   data)
{
1319
  GimpUnit  *val;
1320 1321 1322
  GtkWidget *spinbutton;
  gint       digits;

1323
  val = (GimpUnit *) data;
1324 1325
  *val = gimp_unit_menu_get_unit (GIMP_UNIT_MENU (widget));

1326 1327
  digits = ((*val == GIMP_UNIT_PIXEL) ? 0 :
	    ((*val == GIMP_UNIT_PERCENT) ? 2 :
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	     (MIN (6, MAX (3, gimp_unit_get_digits (*val))))));

  spinbutton =
    gtk_object_get_data (GTK_OBJECT (widget), "set_digits");
  while (spinbutton)
    {
      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), digits);
      spinbutton = gtk_object_get_data (GTK_OBJECT (spinbutton), "set_digits");
    }
}

1339
/*
1340
 *  Helper Functions
1341 1342
 */

1343 1344
/**
 * gimp_table_attach_aligned:
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
 * @table: The #GtkTable the widgets will be attached to.
 * @column: The column to start with.
 * @row: The row to attach the eidgets.
 * @label_text: The text for the #GtkLabel which will be attached left of the
 *              widget.
 * @xalign: The horizontal alignment of the #GtkLabel.
 * @yalign: The vertival alignment of the #GtkLabel.
 * @widget: The #GtkWidget to attach right of the label.
 * @colspan: The number of columns the widget will use.
 * @left_align: #TRUE if the widget should be left-aligned.
 *
 * Note that the @label_text can be #NULL and that the widget will be attached
 * starting at (@column + 1) in this case, too.
1358 1359
 *
 */
1360 1361
void
gimp_table_attach_aligned (GtkTable  *table,
1362
			   gint       column,
1363
			   gint       row,
1364
			   gchar     *label_text,
1365 1366 1367
			   gfloat     xalign,
			   gfloat     yalign,
			   GtkWidget *widget,
1368 1369
			   gint       colspan,
			   gboolean   left_align)
1370 1371 1372 1373 1374 1375 1376 1377
{
  if (label_text)
    {
      GtkWidget *label;

      label = gtk_label_new (label_text);
      gtk_misc_set_alignment (GTK_MISC (label), xalign, yalign);
      gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
1378 1379
      gtk_table_attach (table, label,
			column, column + 1,
1380
			row, row + 1,
1381
			GTK_FILL, GTK_FILL, 0, 0);
1382 1383 1384
      gtk_widget_show (label);
    }

1385
  if (left_align)
1386 1387 1388 1389 1390
    {
      GtkWidget *alignment;

      alignment = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
      gtk_container_add (GTK_CONTAINER (alignment), widget);
1391 1392 1393
      gtk_widget_show (widget);

      widget = alignment;
1394
    }
1395 1396 1397 1398 1399

  gtk_table_attach (table, widget,
		    column + 1, column + 1 + colspan,
		    row, row + 1,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1400 1401 1402

  gtk_widget_show (widget);
}