1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 272 273 274 275 276 277 278 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* 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
* version 3 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
* 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, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpbase/gimpbase.h"
#include "gimpwidgets.h"
#include "libgimp/libgimp-intl.h"
/**
* SECTION: gimpwidgets
* @title: GimpWidgets
* @short_description: A collection of convenient widget constructors,
* standard callbacks and helper functions.
*
* A collection of convenient widget constructors, standard callbacks
* and helper functions.
**/
/**
* gimp_int_radio_group_new: (skip)
* @in_frame: %TRUE if you want a #GtkFrame around the
* radio button group.
* @frame_title: (nullable):
* The title of the Frame or %NULL if you don't want a
* title.
* @radio_button_callback: (scope notified): The callback each button's
* "toggled" signal will be connected with.
* @radio_button_callback_data: (closure radio_button_callback):
* The data which will be passed to g_signal_connect().
* @radio_button_callback_destroy: (destroy radio_button_callback_data):
* @initial: The @item_data of the initially pressed radio button.
* @...: A %NULL-terminated @va_list describing
* the radio buttons.
*
* Convenience function to create a group of radio buttons embedded into
* a #GtkFrame or #GtkBox. This function does the same thing as
* gimp_radio_group_new2(), but it takes integers as @item_data instead of
* pointers, since that is a very common case (mapping an enum to a radio
* group).
*
* Returns: (transfer full): A #GtkFrame or #GtkBox (depending on @in_frame).
**/
GtkWidget *
gimp_int_radio_group_new (gboolean in_frame,
const gchar *frame_title,
GCallback radio_button_callback,
gpointer radio_button_callback_data,
GDestroyNotify radio_button_callback_destroy,
gint initial, /* item_data */
/* specify radio buttons as va_list:
* const gchar *label,
* gint item_data,
* GtkWidget **widget_ptr,
*/
...)
{
GtkWidget *vbox;
GtkWidget *button;
GSList *group;
/* radio button variables */
const gchar *label;
gint item_data;
gpointer item_ptr;
GtkWidget **widget_ptr;
va_list args;
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
group = NULL;
if (radio_button_callback_destroy)
g_object_weak_ref (G_OBJECT (vbox),
(GWeakNotify) radio_button_callback_destroy,
radio_button_callback_data);
/* create the radio buttons */
va_start (args, initial);
label = va_arg (args, const gchar *);
while (label)
{
item_data = va_arg (args, gint);
widget_ptr = va_arg (args, GtkWidget **);
item_ptr = GINT_TO_POINTER (item_data);
if (label != GINT_TO_POINTER (1))
button = gtk_radio_button_new_with_mnemonic (group, label);
else
button = gtk_radio_button_new (group);
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
if (item_data)
{
g_object_set_data (G_OBJECT (button), "gimp-item-data", item_ptr);
/* backward compatibility */
g_object_set_data (G_OBJECT (button), "user_data", item_ptr);
}
if (widget_ptr)
*widget_ptr = button;
if (initial == item_data)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
g_signal_connect (button, "toggled",
radio_button_callback,
radio_button_callback_data);
gtk_widget_show (button);
label = va_arg (args, const gchar *);
}
va_end (args);
if (in_frame)
{
GtkWidget *frame;
frame = gimp_frame_new (frame_title);
gtk_container_add (GTK_CONTAINER (frame), vbox);
gtk_widget_show (vbox);
return frame;
}
return vbox;
}
/**
* gimp_int_radio_group_set_active:
* @radio_button: Pointer to a #GtkRadioButton.
* @item_data: The @item_data of the radio button you want to select.
*
* Calls gtk_toggle_button_set_active() with the radio button that was created
* with a matching @item_data. This function does the same thing as
* gimp_radio_group_set_active(), but takes integers as @item_data instead
* of pointers.
**/
void
gimp_int_radio_group_set_active (GtkRadioButton *radio_button,
gint item_data)
{
GtkWidget *button;
GSList *group;
g_return_if_fail (GTK_IS_RADIO_BUTTON (radio_button));
for (group = gtk_radio_button_get_group (radio_button);
group;
group = g_slist_next (group))
{
button = GTK_WIDGET (group->data);
if (g_object_get_data (G_OBJECT (button), "gimp-item-data") ==
GINT_TO_POINTER (item_data))
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
return;
}
}
}
static void
gimp_random_seed_update (GtkWidget *widget,
gpointer data)
{
GtkWidget *spinbutton = data;
/* Generate a new seed if the "New Seed" button was clicked or
* of the "Randomize" toggle is activated
*/
if (! GTK_IS_TOGGLE_BUTTON (widget) ||
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
{
gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton),
(guint) g_random_int ());
}
}
/**
* gimp_random_seed_new:
* @seed: A pointer to the variable which stores the random seed.
* @random_seed: A pointer to a boolean indicating whether seed should be
* initialised randomly or not.
*
* Creates a widget that allows the user to control how the random number
* generator is initialized.
*
* Returns: (transfer full): A #GtkBox containing a #GtkSpinButton for
* the seed and a #GtkButton for setting a random seed.
**/
GtkWidget *
gimp_random_seed_new (guint *seed,
gboolean *random_seed)
{
GtkWidget *hbox;
GtkWidget *toggle;
GtkWidget *spinbutton;
GtkAdjustment *adj;
GtkWidget *button;
g_return_val_if_fail (seed != NULL, NULL);
g_return_val_if_fail (random_seed != NULL, NULL);
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
/* If we're being asked to generate a random seed, generate one. */
if (*random_seed)
*seed = g_random_int ();
adj = gtk_adjustment_new (*seed, 0, (guint32) -1, 1, 10, 0);
spinbutton = gimp_spin_button_new (adj, 1.0, 0);
gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
gtk_widget_show (spinbutton);
g_signal_connect (adj, "value-changed",
G_CALLBACK (gimp_uint_adjustment_update),
seed);
gimp_help_set_help_data (spinbutton,
_("Use this value for random number generator "
"seed - this allows you to repeat a "
"given \"random\" operation"), NULL);
button = gtk_button_new_with_mnemonic (_("_New Seed"));
g_object_set (gtk_bin_get_child (GTK_BIN (button)),
"margin-start", 2,
"margin-end", 2,
NULL);
gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
gtk_widget_show (button);
/* Send spinbutton as data so that we can change the value in
* gimp_random_seed_update()
*/
g_signal_connect (button, "clicked",
G_CALLBACK (gimp_random_seed_update),
spinbutton);
gimp_help_set_help_data (button,
_("Seed random number generator with a generated "
"random number"),
NULL);
toggle = gtk_check_button_new_with_mnemonic (_("_Randomize"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), *random_seed);
gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
gtk_widget_show (toggle);
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
random_seed);
/* Need to create a new seed when the "Randomize" toggle is activated */
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_random_seed_update),
spinbutton);
g_object_set_data (G_OBJECT (hbox), "spinbutton", spinbutton);
g_object_set_data (G_OBJECT (hbox), "button", button);
g_object_set_data (G_OBJECT (hbox), "toggle", toggle);
g_object_bind_property (toggle, "active",
spinbutton, "sensitive",
G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
g_object_bind_property (toggle, "active",
button, "sensitive",
G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
return hbox;
}
typedef struct
{
GimpChainButton *chainbutton;
gboolean chain_constrains_ratio;
gdouble orig_x;
gdouble orig_y;
gdouble last_x;
gdouble last_y;
} GimpCoordinatesData;
static void
gimp_coordinates_callback (GtkWidget *widget,
GimpCoordinatesData *data)
{
gdouble new_x;
gdouble new_y;
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 (data->chainbutton))
{
if (data->chain_constrains_ratio)
{
if ((data->orig_x != 0) && (data->orig_y != 0))
{
g_signal_handlers_block_by_func (widget,
gimp_coordinates_callback,
data);
if (ROUND (new_x) != ROUND (data->last_x))
{
data->last_x = new_x;
new_y = (new_x * data->orig_y) / data->orig_x;
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 1,
new_y);
data->last_y
= gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 1);
}
else if (ROUND (new_y) != ROUND (data->last_y))
{
data->last_y = new_y;
new_x = (new_y * data->orig_x) / data->orig_y;
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 0,
new_x);
data->last_x
= gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0);
}
g_signal_handlers_unblock_by_func (widget,
gimp_coordinates_callback,
data);
}
}
else
{
if (new_x != data->last_x)
{
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 1, new_x);
data->last_y = data->last_x
= gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 1);
}
else if (new_y != data->last_y)
{
gimp_size_entry_set_refval (GIMP_SIZE_ENTRY (widget), 0, new_y);
data->last_x = data->last_y
= gimp_size_entry_get_refval (GIMP_SIZE_ENTRY (widget), 0);
}
}
}
else
{
if (new_x != data->last_x)
data->last_x = new_x;
if (new_y != data->last_y)
data->last_y = new_y;
}
}
static void
gimp_coordinates_data_free (GimpCoordinatesData *data)
{
g_slice_free (GimpCoordinatesData, data);
}
static void
gimp_coordinates_chainbutton_toggled (GimpChainButton *button,
GimpSizeEntry *entry)
{
if (gimp_chain_button_get_active (button))
{
GimpCoordinatesData *data;
data = g_object_get_data (G_OBJECT (entry), "coordinates-data");
data->orig_x = gimp_size_entry_get_refval (entry, 0);
data->orig_y = gimp_size_entry_get_refval (entry, 1);
}
}
/**
* gimp_coordinates_new:
* @unit: The initial unit of the #GimpUnitMenu.
* @unit_format: A printf-like unit-format string as is used with
* gimp_unit_menu_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_width: The horizontal size 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%.
*
* Convenience function that creates 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.
*
* Returns: (transfer full): The new #GimpSizeEntry.
**/
GtkWidget *
gimp_coordinates_new (GimpUnit *unit,
const gchar *unit_format,
gboolean menu_show_pixels,
gboolean menu_show_percent,
gint spinbutton_width,
GimpSizeEntryUpdatePolicy update_policy,
gboolean chainbutton_active,
gboolean chain_constrains_ratio,
const gchar *xlabel,
gdouble x,
gdouble xres,
gdouble lower_boundary_x,
gdouble upper_boundary_x,
gdouble xsize_0, /* % */
gdouble xsize_100, /* % */
const gchar *ylabel,
gdouble y,
gdouble yres,
gdouble lower_boundary_y,
gdouble upper_boundary_y,
gdouble ysize_0, /* % */
gdouble ysize_100 /* % */)
{
GimpCoordinatesData *data;
GtkAdjustment *adjustment;
GtkWidget *spinbutton;
GtkWidget *sizeentry;
GtkWidget *chainbutton;
adjustment = gtk_adjustment_new (1, 0, 1, 1, 10, 0);
spinbutton = gimp_spin_button_new (adjustment, 1.0, 2);
gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
if (spinbutton_width > 0)
{
if (spinbutton_width < 17)
gtk_entry_set_width_chars (GTK_ENTRY (spinbutton), spinbutton_width);
else
gtk_widget_set_size_request (spinbutton, spinbutton_width, -1);
}
sizeentry = gimp_size_entry_new (1, unit, unit_format,
menu_show_pixels,
menu_show_percent,
FALSE,
spinbutton_width,
update_policy);
gimp_size_entry_add_field (GIMP_SIZE_ENTRY (sizeentry),
GTK_SPIN_BUTTON (spinbutton), NULL);
gtk_grid_attach (GTK_GRID (sizeentry), spinbutton, 1, 0, 1, 1);
gtk_widget_show (spinbutton);
gimp_size_entry_set_unit (GIMP_SIZE_ENTRY (sizeentry),
(update_policy == GIMP_SIZE_ENTRY_UPDATE_RESOLUTION) ||
(menu_show_pixels == FALSE) ?
gimp_unit_inch () : gimp_unit_pixel ());
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);
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);
}
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, 0.0);
gimp_size_entry_attach_label (GIMP_SIZE_ENTRY (sizeentry),
ylabel, 1, 0, 0.0);
chainbutton = gimp_chain_button_new (GIMP_CHAIN_RIGHT);
if (chainbutton_active)
gimp_chain_button_set_active (GIMP_CHAIN_BUTTON (chainbutton), TRUE);
gtk_grid_attach (GTK_GRID (sizeentry), chainbutton, 2, 0, 1, 2);
gtk_widget_show (chainbutton);
data = g_slice_new (GimpCoordinatesData);
data->chainbutton = GIMP_CHAIN_BUTTON (chainbutton);
data->chain_constrains_ratio = chain_constrains_ratio;
data->orig_x = x;
data->orig_y = y;
data->last_x = x;
data->last_y = y;
g_object_set_data_full (G_OBJECT (sizeentry), "coordinates-data",
data,
(GDestroyNotify) gimp_coordinates_data_free);
g_signal_connect (sizeentry, "value-changed",
G_CALLBACK (gimp_coordinates_callback),
data);
g_object_set_data (G_OBJECT (sizeentry), "chainbutton", chainbutton);
g_signal_connect (chainbutton, "toggled",
G_CALLBACK (gimp_coordinates_chainbutton_toggled),
sizeentry);
return sizeentry;
}
/*
* Standard Callbacks
*/
/**
* gimp_toggle_button_update:
* @widget: A #GtkToggleButton.
* @data: (out) (type gboolean): A return location for the value of
* gtk_toggle_button_get_active().
**/
void
gimp_toggle_button_update (GtkWidget *widget,
gpointer data)
{
gboolean *toggle_val = (gboolean *) data;
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
*toggle_val = TRUE;
else
*toggle_val = FALSE;
}
/**
* gimp_radio_button_update:
* @widget: A #GtkRadioButton.
* @data: (out) (type gint): A return location for the value of
* `GPOINTER_TO_INT (g_object_get_data (@widget, "gimp-item-data"))`.
**/
void
gimp_radio_button_update (GtkWidget *widget,
gpointer data)
{
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
{
gint *toggle_val = (gint *) data;
*toggle_val = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
"gimp-item-data"));
}
}
/**
* gimp_int_adjustment_update:
* @adjustment: A #GtkAdjustment.
* @data: (out) (type gint): A return location for the @adjustment's value.
*
* Note that the #GtkAdjustment's value (which is a #gdouble) will be
* rounded with RINT().
**/
void
gimp_int_adjustment_update (GtkAdjustment *adjustment,
gpointer data)
{
gint *val = (gint *) data;
*val = RINT (gtk_adjustment_get_value (adjustment));
}
/**
* gimp_uint_adjustment_update:
* @adjustment: A #GtkAdjustment.
* @data: (out) (type guint): A return location for the @adjustment's value.
*
* Note that the #GtkAdjustment's value (which is a #gdouble) will be rounded
* with (#guint) (value + 0.5).
**/
void
gimp_uint_adjustment_update (GtkAdjustment *adjustment,
gpointer data)
{
guint *val = (guint *) data;
*val = (guint) (gtk_adjustment_get_value (adjustment) + 0.5);
}
/**
* gimp_float_adjustment_update:
* @adjustment: A #GtkAdjustment.
* @data: (out) (type gfloat): A return location for the @adjustment's value.
**/
void
gimp_float_adjustment_update (GtkAdjustment *adjustment,
gpointer data)
{
gfloat *val = (gfloat *) data;
*val = gtk_adjustment_get_value (adjustment);
}
/**
* gimp_double_adjustment_update:
* @adjustment: A #GtkAdjustment.
* @data: (out) (type gdouble): A return location for the @adjustment's value.
**/
void
gimp_double_adjustment_update (GtkAdjustment *adjustment,
gpointer data)
{
gdouble *val = (gdouble *) data;
*val = gtk_adjustment_get_value (adjustment);
}
|