File: script-fu-progress.c

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (170 lines) | stat: -rw-r--r-- 5,201 bytes parent folder | download | duplicates (3)
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h> /* Gtk */

#include "script-fu-progress.h"

/* ScriptFu reports progress in app's status bar.
 *
 * ScriptFu alleviates scripts of explicit progress reporting,
 * by reporting progress whenever a script calls a PDB procedure.
 *
 * A script can also use the PDB progress functions,
 * e.g. install a callback.
 * If a script does not install a callback,
 * then the script's use of gimp_progress may interleave
 * with ScriptFu's own use.
 */


static gchar *main_proc_title      = NULL;
static gchar *last_command         = NULL;
static gint   command_count        = 0;
static gint   consec_command_count = 0;


/* Show progress by displaying a text.

 * This hides where the text is going.
 * Currently gimp_progress object displays to the app's status bar.
 * Formerly ScriptFu displayed to a progress bar in the plugin's dialog box.
 * In the future we might display somewhere else.
 *
 * Requires gimp_progress_init was called, else no effect.
 * Requires gimp_progress_install was NOT called, else just triggers
 * the installed callback.
 */
static void
script_fu_interface_progress_update (const gchar *text)
{
  g_debug ("%s calling progress_set_text", G_STRFUNC);
  gimp_progress_set_text (text);
  gimp_progress_pulse ();
}

/* Called when start a script run.
 * Not necessarily when a plugin process is starting.
 */
void
script_fu_progress_init (const gchar *title)
{
  g_free (main_proc_title);
  if (title)
    {
      gchar *ellipsis;

      main_proc_title = gimp_strip_uline (title);
      main_proc_title = g_strstrip (main_proc_title);
      ellipsis = g_strrstr (main_proc_title, "...");

      if (ellipsis && ellipsis - main_proc_title == strlen (main_proc_title) - 3)
        *ellipsis = '\0';
    }
  else
    {
      main_proc_title = g_strdup ("Script-Fu");
    }

  gimp_progress_init (main_proc_title);

  /* If this is the long running extension-script-fu, reset these vars.
   *
   * On quitting extension-script-fu or independent interpreter,
   * we may leak last_command, but only on exit.
   */
  g_clear_pointer (&last_command, g_free);
  command_count = 0;
  consec_command_count = 0;
}

/* Called whenever the interpreter calls the PDB.
 * A pulse of progress is: displaying name of PDB procedure executed.
 * Flashing names is like a spinning wheel.
 *
 * User's don't know the procedure names, this is less than ideal.
 *
 * Exclude calls to gimp-progress-<foo>,
 * when the script is itself implementing its own progress reporting.
 * No scripts in the Gimp repo implement their own progress.
 * This too is probably overkill, the name would flash by quickly.
 *
 * We report progress regardless of INTERACTIVE mode.
 *
 * Also does some optimization:
 * omits report for a long sequence of the same command.
 * This is probably overkill that could be eliminated.
 */
void
script_fu_progress_report (const gchar *command)
{
  /* If command is same as previous. */
  if (last_command &&
      strcmp (last_command, command) == 0)
    {
      /* Form a string <command> <number> */
      command_count++;

      if (! g_str_has_prefix (command, "gimp-progress-"))
        {
          gchar *new_command;

          if (main_proc_title)
            new_command = g_strdup_printf ("%s: %s <%d>", main_proc_title, command, command_count);
          else
            new_command = g_strdup_printf ("%s <%d>", command, command_count);
          script_fu_interface_progress_update (new_command);
          g_free (new_command);
        }
    }
  else
    {
      /* Not a consecutive command. */
      command_count = 1;

      g_free (last_command);
      last_command = g_strdup (command);

      if (! g_str_has_prefix (command, "gimp-progress-"))
        {
          gchar *new_command;

          if (main_proc_title)
            new_command = g_strdup_printf ("%s: %s", main_proc_title, command);
          else
            new_command = g_strdup (command);

          script_fu_interface_progress_update (new_command);

          g_free (new_command);
        }
      else
        {
          script_fu_interface_progress_update ("");
        }
    }

  /* In v2, ScriptFu was displaying progress to its own progress bar
   * in ScriptFu's own dialog.
   * In v2 needed: while (gtk_events_pending ())  gtk_main_iteration ();
   * In v3 gimp_progress goes to another process, the gimp app,
   * having it's own event loop.
   */
}