3.6. Extending The Text Box Script

3.6.1. Handling Undo Correctly

When creating a script, you want to give your users the ability to undo their actions, should they make a mistake. This is easily accomplished by calling the functions gimp-undo-push-group-start and gimp-undo-push-group-end around the code that manipulates the image. You can think of them as matched statements that let GIMP know when to start and stop recording manipulations on the image, so that those manipulations can later be undone.

If you are creating a new image entirely, it doesn't make sense to use these functions because you're not changing an existing image. However, when you are changing an existing image, you most surely want to use these functions.

Undoing a script works nearly flawlessly when using these functions.

3.6.2. Extending The Script A Little More

Now that we have a very handy-dandy script to create text boxes, let's add two features to it:

  • Currently, the image is resized to fit exactly around the text — there's no room for anything, like drop shadows or special effects (even though many scripts will automatically resize the image as necessary). Let's add a buffer around the text, and even let the user specify how much buffer to add as a percentage of the size of the resultant text.

  • This script could easily be used in other scripts that work with text. Let's extend it so that it returns the image and the layers, so other scripts can call this script and use the image and layers we create.

3.6.3. Modifying The Parameters And The Registration Function

To let the user specify the amount of buffer, we'll add a paramet