~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/display/gimpdisplayshell-title.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
Import upstream version 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <string.h>
 
22
 
 
23
#include <gtk/gtk.h>
 
24
 
 
25
#include "libgimpbase/gimpbase.h"
 
26
 
 
27
#include "display-types.h"
 
28
 
 
29
#include "config/gimpdisplayconfig.h"
 
30
 
 
31
#include "core/gimp.h"
 
32
#include "core/gimpcontainer.h"
 
33
#include "core/gimpimage.h"
 
34
#include "core/gimpitem.h"
 
35
#include "core/gimpunit.h"
 
36
 
 
37
#include "file/file-utils.h"
 
38
 
 
39
#ifdef __GNUC__
 
40
#warning FIXME #include "dialogs/dialogs-types.h"
 
41
#endif
 
42
#include "dialogs/dialogs-types.h"
 
43
#include "dialogs/info-window.h"
 
44
 
 
45
#include "gimpdisplay.h"
 
46
#include "gimpdisplayshell.h"
 
47
#include "gimpdisplayshell-scale.h"
 
48
#include "gimpdisplayshell-title.h"
 
49
#include "gimpstatusbar.h"
 
50
 
 
51
#include "gimp-intl.h"
 
52
 
 
53
 
 
54
#define MAX_TITLE_BUF 256
 
55
 
 
56
 
 
57
static gboolean gimp_display_shell_update_title_idle (gpointer          data);
 
58
static void     gimp_display_shell_format_title      (GimpDisplayShell *gdisp,
 
59
                                                      gchar            *title,
 
60
                                                      gint              title_len,
 
61
                                                      const gchar      *format);
 
62
 
 
63
 
 
64
/*  public functions  */
 
65
 
 
66
void
 
67
gimp_display_shell_title_init (GimpDisplayShell *shell)
 
68
{
 
69
  GimpDisplayConfig *config;
 
70
  gchar              title[MAX_TITLE_BUF];
 
71
 
 
72
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
73
 
 
74
  config = GIMP_DISPLAY_CONFIG (shell->gdisp->gimage->gimp->config);
 
75
 
 
76
  gimp_display_shell_format_title (shell, title, sizeof (title),
 
77
                                   config->image_status_format);
 
78
 
 
79
  gimp_statusbar_push (GIMP_STATUSBAR (shell->statusbar), "title", title);
 
80
}
 
81
 
 
82
void
 
83
gimp_display_shell_title_update (GimpDisplayShell *shell)
 
84
{
 
85
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
86
 
 
87
  if (shell->title_idle_id)
 
88
    g_source_remove (shell->title_idle_id);
 
89
 
 
90
  shell->title_idle_id = g_idle_add (gimp_display_shell_update_title_idle,
 
91
                                     shell);
 
92
}
 
93
 
 
94
 
 
95
/*  private functions  */
 
96
 
 
97
static gboolean
 
98
gimp_display_shell_update_title_idle (gpointer data)
 
99
{
 
100
  GimpDisplayShell  *shell;
 
101
  GimpDisplayConfig *config;
 
102
  gchar              title[MAX_TITLE_BUF];
 
103
 
 
104
  shell  = GIMP_DISPLAY_SHELL (data);
 
105
  config = GIMP_DISPLAY_CONFIG (shell->gdisp->gimage->gimp->config);
 
106
 
 
107
  shell->title_idle_id = 0;
 
108
 
 
109
  /* format the title */
 
110
  gimp_display_shell_format_title (shell, title, sizeof (title),
 
111
                                   config->image_title_format);
 
112
  gdk_window_set_title (GTK_WIDGET (shell)->window, title);
 
113
 
 
114
  /* format the statusbar */
 
115
  if (strcmp (config->image_title_format, config->image_status_format))
 
116
    {
 
117
      gimp_display_shell_format_title (shell, title, sizeof (title),
 
118
                                       config->image_status_format);
 
119
    }
 
120
 
 
121
  gimp_statusbar_replace (GIMP_STATUSBAR (shell->statusbar), "title", title);
 
122
 
 
123
#ifdef __GNUC__
 
124
#warning FIXME: dont call info_window_update() here.
 
125
#endif
 
126
  info_window_update (shell->gdisp);
 
127
 
 
128
  return FALSE;
 
129
}
 
130
 
 
131
 
 
132
static gint print (gchar       *buf,
 
133
                   gint         len,
 
134
                   gint         start,
 
135
                   const gchar *fmt,
 
136
                   ...) G_GNUC_PRINTF (4, 5);
 
137
 
 
138
static gint
 
139
print (gchar       *buf,
 
140
       gint         len,
 
141
       gint         start,
 
142
       const gchar *fmt,
 
143
       ...)
 
144
{
 
145
  va_list args;
 
146
  gint    printed;
 
147
 
 
148
  va_start (args, fmt);
 
149
 
 
150
  printed = g_vsnprintf (buf + start, len - start, fmt, args);
 
151
  if (printed < 0)
 
152
    printed = len - start;
 
153
 
 
154
  va_end (args);
 
155
 
 
156
  return printed;
 
157
}
 
158
 
 
159
static void
 
160
gimp_display_shell_format_title (GimpDisplayShell *shell,
 
161
                                 gchar            *title,
 
162
                                 gint              title_len,
 
163
                                 const gchar      *format)
 
164
{
 
165
  Gimp      *gimp;
 
166
  GimpImage *image;
 
167
  gint       num, denom;
 
168
  gint       i = 0;
 
169
 
 
170
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
171
 
 
172
  image = shell->gdisp->gimage;
 
173
  gimp  = image->gimp;
 
174
 
 
175
  gimp_display_shell_scale_get_fraction (shell->scale, &num, &denom);
 
176
 
 
177
  while (i < title_len && *format)
 
178
    {
 
179
      switch (*format)
 
180
        {
 
181
        case '%':
 
182
          format++;
 
183
          switch (*format)
 
184
            {
 
185
            case 0:
 
186
              /* format string ends within %-sequence, print literal '%' */
 
187
 
 
188
            case '%':
 
189
              title[i++] = '%';
 
190
              break;
 
191
 
 
192
            case 'f': /* pruned filename */
 
193
              {
 
194
                const gchar *uri = gimp_image_get_uri (image);
 
195
                gchar       *basename;
 
196
 
 
197
                basename = file_utils_uri_to_utf8_basename (uri);
 
198
 
 
199
                i += print (title, title_len, i, "%s", basename);
 
200
 
 
201
                g_free (basename);
 
202
              }
 
203
              break;
 
204
 
 
205
            case 'F': /* full filename */
 
206
              {
 
207
                gchar *filename;
 
208
                const gchar *uri = gimp_image_get_uri (image);
 
209
 
 
210
                filename = file_utils_uri_to_utf8_filename (uri);
 
211
 
 
212
                i += print (title, title_len, i, "%s", filename);
 
213
 
 
214
                g_free (filename);
 
215
              }
 
216
              break;
 
217
 
 
218
            case 'p': /* PDB id */
 
219
              i += print (title, title_len, i, "%d", gimp_image_get_ID (image));
 
220
              break;
 
221
 
 
222
            case 'i': /* instance */
 
223
              i += print (title, title_len, i, "%d", shell->gdisp->instance);
 
224
              break;
 
225
 
 
226
            case 't': /* type */
 
227
              {
 
228
                const gchar *image_type_str = NULL;
 
229
                gboolean     empty          = gimp_image_is_empty (image);
 
230
 
 
231
                switch (gimp_image_base_type (image))
 
232
                  {
 
233
                  case GIMP_RGB:
 
234
                    image_type_str = empty ? _("RGB-empty") : _("RGB");
 
235
                    break;
 
236
                  case GIMP_GRAY:
 
237
                    image_type_str = empty ? _("grayscale-empty") : _("grayscale");
 
238
                    break;
 
239
                  case GIMP_INDEXED:
 
240
                    image_type_str = empty ? _("indexed-empty") : _("indexed");
 
241
                    break;
 
242
                  default:
 
243
                    g_assert_not_reached ();
 
244
                    break;
 
245
                  }
 
246
 
 
247
                i += print (title, title_len, i, "%s", image_type_str);
 
248
              }
 
249
              break;
 
250
 
 
251
            case 's': /* user source zoom factor */
 
252
              i += print (title, title_len, i, "%d", denom);
 
253
              break;
 
254
 
 
255
            case 'd': /* user destination zoom factor */
 
256
              i += print (title, title_len, i, "%d", num);
 
257
              break;
 
258
 
 
259
            case 'z': /* user zoom factor (percentage) */
 
260
              i += print (title, title_len, i,
 
261
                          shell->scale >= 0.15 ? "%.0f" : "%.2f",
 
262
                          100 * shell->scale);
 
263
              break;
 
264
 
 
265
            case 'D': /* dirty flag */
 
266
              if (format[1] == 0)
 
267
                {
 
268
                  /* format string ends within %D-sequence, print literal '%D' */
 
269
                  i += print (title, title_len, i, "%%D");
 
270
                  break;
 
271
                }
 
272
              if (image->dirty)
 
273
                title[i++] = format[1];
 
274
              format++;
 
275
              break;
 
276
 
 
277
            case 'C': /* clean flag */
 
278
              if (format[1] == 0)
 
279
                {
 
280
                  /* format string ends within %C-sequence, print literal '%C' */
 
281
                  i += print (title, title_len, i, "%%C");
 
282
                  break;
 
283
                }
 
284
              if (! image->dirty)
 
285
                title[i++] = format[1];
 
286
              format++;
 
287
              break;
 
288
 
 
289
            case 'B': /* dirty flag (long) */
 
290
              if (image->dirty)
 
291
                i += print (title, title_len, i, "%s",
 
292
                            _("(modified)"));
 
293
              break;
 
294
 
 
295
            case 'A': /* clean flag (long) */
 
296
              if (! image->dirty)
 
297
                i += print (title, title_len, i, "%s",
 
298
                            _("(clean)"));
 
299
              break;
 
300
 
 
301
            case 'm': /* memory used by image */
 
302
              {
 
303
                GimpObject *object = GIMP_OBJECT (image);
 
304
                gchar      *str;
 
305
 
 
306
                str = gimp_memsize_to_string (gimp_object_get_memsize (object,
 
307
                                                                       NULL));
 
308
 
 
309
                i += print (title, title_len, i, "%s", str);
 
310
 
 
311
                g_free (str);
 
312
              }
 
313
              break;
 
314
 
 
315
            case 'l': /* number of layers */
 
316
              i += print (title, title_len, i, "%d",
 
317
                          gimp_container_num_children (image->layers));
 
318
              break;
 
319
 
 
320
            case 'L': /* number of layers (long) */
 
321
              {
 
322
                gint num = gimp_container_num_children (image->layers);
 
323
 
 
324
                i += print (title, title_len, i,
 
325
                            num == 1 ? _("1 layer") : _("%d layers"), num);
 
326
              }
 
327
              break;
 
328
 
 
329
            case 'n': /* active drawable name */
 
330
              {
 
331
                GimpDrawable *drawable = gimp_image_active_drawable (image);
 
332
 
 
333
                if (drawable)
 
334
                  i += print (title, title_len, i, "%s",
 
335
                              gimp_object_get_name (GIMP_OBJECT (drawable)));
 
336
                else
 
337
                  i += print (title, title_len, i, "%s", _("(none)"));
 
338
              }
 
339
              break;
 
340
 
 
341
            case 'P': /* active drawable PDB id */
 
342
              {
 
343
                GimpDrawable *drawable = gimp_image_active_drawable (image);
 
344
 
 
345
                if (drawable)
 
346
                  i += print (title, title_len, i, "%d",
 
347
                              gimp_item_get_ID (GIMP_ITEM (drawable)));
 
348
                else
 
349
                  i += print (title, title_len, i, "%s", _("(none)"));
 
350
              }
 
351
              break;
 
352
 
 
353
            case 'W': /* width in real-world units */
 
354
              if (shell->unit != GIMP_UNIT_PIXEL)
 
355
                {
 
356
                  gchar unit_format[8];
 
357
 
 
358
                  g_snprintf (unit_format, sizeof (unit_format), "%%.%df",
 
359
                              _gimp_unit_get_digits (gimp, shell->unit) + 1);
 
360
                  i += print (title, title_len, i, unit_format,
 
361
                              (image->width *
 
362
                               _gimp_unit_get_factor (gimp, shell->unit) /
 
363
                               image->xresolution));
 
364
                  break;
 
365
                }
 
366
              /* else fallthru */
 
367
            case 'w': /* width in pixels */
 
368
              i += print (title, title_len, i, "%d", image->width);
 
369
              break;
 
370
 
 
371
            case 'H': /* height in real-world units */
 
372
              if (shell->unit != GIMP_UNIT_PIXEL)
 
373
                {
 
374
                  gchar unit_format[8];
 
375
 
 
376
                  g_snprintf (unit_format, sizeof (unit_format), "%%.%df",
 
377
                              _gimp_unit_get_digits (gimp, shell->unit) + 1);
 
378
                  i += print (title, title_len, i, unit_format,
 
379
                              (image->height *
 
380
                               _gimp_unit_get_factor (gimp, shell->unit) /
 
381
                               image->yresolution));
 
382
                  break;
 
383
                }
 
384
              /* else fallthru */
 
385
            case 'h': /* height in pixels */
 
386
              i += print (title, title_len, i, "%d", image->height);
 
387
              break;
 
388
 
 
389
            case 'u': /* unit symbol */
 
390
              i += print (title, title_len, i, "%s",
 
391
                          _gimp_unit_get_symbol (gimp, shell->unit));
 
392
              break;
 
393
 
 
394
            case 'U': /* unit abbreviation */
 
395
              i += print (title, title_len, i, "%s",
 
396
                          _gimp_unit_get_abbreviation (gimp, shell->unit));
 
397
              break;
 
398
 
 
399
              /* Other cool things to be added:
 
400
               * %r = xresolution
 
401
               * %R = yresolution
 
402
               * %� = image's fractal dimension
 
403
               * %� = the answer to everything
 
404
               */
 
405
 
 
406
            default:
 
407
              /* format string contains unknown %-sequence, print it literally */
 
408
              i += print (title, title_len, i, "%%%c", *format);
 
409
              break;
 
410
            }
 
411
          break;
 
412
 
 
413
        default:
 
414
          title[i++] = *format;
 
415
          break;
 
416
        }
 
417
 
 
418
      format++;
 
419
    }
 
420
 
 
421
  title[MIN (i, title_len - 1)] = '\0';
 
422
}