gimpsessioninfo-dockable.c 8.91 KB
Newer Older
Michael Natterer's avatar
Michael Natterer committed
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpsessioninfo-dockable.c
Michael Natterer's avatar
Michael Natterer committed
5 6
 * Copyright (C) 2001-2007 Michael Natterer <mitch@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
Michael Natterer's avatar
Michael Natterer committed
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
Michael Natterer's avatar
Michael Natterer committed
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Michael Natterer's avatar
Michael Natterer committed
19 20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpconfig/gimpconfig.h"

#include "widgets-types.h"

#include "gimpcontainerview-utils.h"
30
#include "gimpcontainerview.h"
Michael Natterer's avatar
Michael Natterer committed
31 32 33
#include "gimpdialogfactory.h"
#include "gimpdock.h"
#include "gimpdockable.h"
34 35
#include "gimpsessioninfo-aux.h"
#include "gimpsessioninfo-dockable.h"
36
#include "gimpsessionmanaged.h"
37
#include "gimptoolbox.h"
Michael Natterer's avatar
Michael Natterer committed
38 39 40 41


enum
{
42
  SESSION_INFO_DOCKABLE_LOCKED,
Michael Natterer's avatar
Michael Natterer committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  SESSION_INFO_DOCKABLE_TAB_STYLE,
  SESSION_INFO_DOCKABLE_VIEW_SIZE,
  SESSION_INFO_DOCKABLE_AUX
};


/*  public functions  */

GimpSessionInfoDockable *
gimp_session_info_dockable_new (void)
{
  return g_slice_new0 (GimpSessionInfoDockable);
}

void
58
gimp_session_info_dockable_free (GimpSessionInfoDockable *info)
Michael Natterer's avatar
Michael Natterer committed
59
{
60
  g_return_if_fail (info != NULL);
Michael Natterer's avatar
Michael Natterer committed
61

62
  if (info->identifier)
63 64 65 66
    {
      g_free (info->identifier);
      info->identifier = NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
67

68
  if (info->aux_info)
Michael Natterer's avatar
Michael Natterer committed
69
    {
70 71
      g_list_free_full (info->aux_info,
                        (GDestroyNotify) gimp_session_info_aux_free);
72
      info->aux_info = NULL;
Michael Natterer's avatar
Michael Natterer committed
73 74
    }

75
  g_slice_free (GimpSessionInfoDockable, info);
Michael Natterer's avatar
Michael Natterer committed
76 77 78
}

void
79 80
gimp_session_info_dockable_serialize (GimpConfigWriter        *writer,
                                      GimpSessionInfoDockable *info)
Michael Natterer's avatar
Michael Natterer committed
81
{
82 83 84
  GEnumClass  *enum_class;
  GEnumValue  *enum_value;
  const gchar *tab_style = "icon";
Michael Natterer's avatar
Michael Natterer committed
85 86

  g_return_if_fail (writer != NULL);
87
  g_return_if_fail (info != NULL);
Michael Natterer's avatar
Michael Natterer committed
88

89
  enum_class = g_type_class_ref (GIMP_TYPE_TAB_STYLE);
Michael Natterer's avatar
Michael Natterer committed
90

91 92
  gimp_config_writer_open (writer, "dockable");
  gimp_config_writer_string (writer, info->identifier);
Michael Natterer's avatar
Michael Natterer committed
93

94 95 96 97 98 99
  if (info->locked)
    {
      gimp_config_writer_open (writer, "locked");
      gimp_config_writer_close (writer);
    }

100
  enum_value = g_enum_get_value (enum_class, info->tab_style);
Michael Natterer's avatar
Michael Natterer committed
101

102 103
  if (enum_value)
    tab_style = enum_value->value_nick;
Michael Natterer's avatar
Michael Natterer committed
104

105 106 107
  gimp_config_writer_open (writer, "tab-style");
  gimp_config_writer_print (writer, tab_style, -1);
  gimp_config_writer_close (writer);
Michael Natterer's avatar
Michael Natterer committed
108

109 110 111 112
  if (info->view_size > 0)
    {
      gimp_config_writer_open (writer, "preview-size");
      gimp_config_writer_printf (writer, "%d", info->view_size);
Michael Natterer's avatar
Michael Natterer committed
113
      gimp_config_writer_close (writer);
114
    }
Michael Natterer's avatar
Michael Natterer committed
115

116 117
  if (info->aux_info)
    gimp_session_info_aux_serialize (writer, info->aux_info);
Michael Natterer's avatar
Michael Natterer committed
118

119
  gimp_config_writer_close (writer);
Michael Natterer's avatar
Michael Natterer committed
120

121
  g_type_class_unref (enum_class);
Michael Natterer's avatar
Michael Natterer committed
122 123 124
}

GTokenType
125 126 127
gimp_session_info_dockable_deserialize (GScanner                 *scanner,
                                        gint                      scope,
                                        GimpSessionInfoDockable **dockable)
Michael Natterer's avatar
Michael Natterer committed
128
{
129
  GimpSessionInfoDockable *info;
Michael Natterer's avatar
Michael Natterer committed
130 131 132 133 134
  GEnumClass              *enum_class;
  GEnumValue              *enum_value;
  GTokenType               token;

  g_return_val_if_fail (scanner != NULL, G_TOKEN_LEFT_PAREN);
135
  g_return_val_if_fail (dockable != NULL, G_TOKEN_LEFT_PAREN);
Michael Natterer's avatar
Michael Natterer committed
136

137 138
  g_scanner_scope_add_symbol (scanner, scope, "locked",
                              GINT_TO_POINTER (SESSION_INFO_DOCKABLE_LOCKED));
Michael Natterer's avatar
Michael Natterer committed
139 140 141 142 143 144 145
  g_scanner_scope_add_symbol (scanner, scope, "tab-style",
                              GINT_TO_POINTER (SESSION_INFO_DOCKABLE_TAB_STYLE));
  g_scanner_scope_add_symbol (scanner, scope, "preview-size",
                              GINT_TO_POINTER (SESSION_INFO_DOCKABLE_VIEW_SIZE));
  g_scanner_scope_add_symbol (scanner, scope, "aux-info",
                              GINT_TO_POINTER (SESSION_INFO_DOCKABLE_AUX));

146
  info = gimp_session_info_dockable_new ();
Michael Natterer's avatar
Michael Natterer committed
147 148 149 150

  enum_class = g_type_class_ref (GIMP_TYPE_TAB_STYLE);

  token = G_TOKEN_STRING;
151
  if (! gimp_scanner_parse_string (scanner, &info->identifier))
Michael Natterer's avatar
Michael Natterer committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    goto error;

  token = G_TOKEN_LEFT_PAREN;

  while (g_scanner_peek_next_token (scanner) == token)
    {
      token = g_scanner_get_next_token (scanner);

      switch (token)
        {
        case G_TOKEN_LEFT_PAREN:
          token = G_TOKEN_SYMBOL;
          break;

        case G_TOKEN_SYMBOL:
          switch (GPOINTER_TO_INT (scanner->value.v_symbol))
            {
169 170 171 172
            case SESSION_INFO_DOCKABLE_LOCKED:
              info->locked = TRUE;
              break;

Michael Natterer's avatar
Michael Natterer committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
            case SESSION_INFO_DOCKABLE_TAB_STYLE:
              token = G_TOKEN_IDENTIFIER;
              if (g_scanner_peek_next_token (scanner) != token)
                goto error;

              g_scanner_get_next_token (scanner);

              enum_value = g_enum_get_value_by_nick (enum_class,
                                                     scanner->value.v_identifier);

              if (! enum_value)
                enum_value = g_enum_get_value_by_name (enum_class,
                                                       scanner->value.v_identifier);

              if (enum_value)
188
                info->tab_style = enum_value->value;
Michael Natterer's avatar
Michael Natterer committed
189 190 191 192
              break;

            case SESSION_INFO_DOCKABLE_VIEW_SIZE:
              token = G_TOKEN_INT;
193
              if (! gimp_scanner_parse_int (scanner, &info->view_size))
Michael Natterer's avatar
Michael Natterer committed
194 195 196 197 198
                goto error;
              break;

            case SESSION_INFO_DOCKABLE_AUX:
              token = gimp_session_info_aux_deserialize (scanner,
199
                                                         &info->aux_info);
Michael Natterer's avatar
Michael Natterer committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
              if (token != G_TOKEN_LEFT_PAREN)
                goto error;
              break;

            default:
              goto error;
            }
          token = G_TOKEN_RIGHT_PAREN;
          break;

        case G_TOKEN_RIGHT_PAREN:
          token = G_TOKEN_LEFT_PAREN;
          break;

        default:
          break;
        }
    }

219 220
  *dockable = info;

Michael Natterer's avatar
Michael Natterer committed
221 222
  g_type_class_unref (enum_class);

223
  g_scanner_scope_remove_symbol (scanner, scope, "locked");
Michael Natterer's avatar
Michael Natterer committed
224 225 226 227 228 229 230
  g_scanner_scope_remove_symbol (scanner, scope, "tab-style");
  g_scanner_scope_remove_symbol (scanner, scope, "preview-size");
  g_scanner_scope_remove_symbol (scanner, scope, "aux-info");

  return token;

 error:
231 232 233
  *dockable = NULL;

  gimp_session_info_dockable_free (info);
Michael Natterer's avatar
Michael Natterer committed
234 235 236 237 238
  g_type_class_unref (enum_class);

  return token;
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
GimpSessionInfoDockable *
gimp_session_info_dockable_from_widget (GimpDockable *dockable)
{
  GimpSessionInfoDockable *info;
  GimpDialogFactoryEntry  *entry;
  GimpContainerView       *view;
  gint                     view_size = -1;

  g_return_val_if_fail (GIMP_IS_DOCKABLE (dockable), NULL);

  gimp_dialog_factory_from_widget (GTK_WIDGET (dockable), &entry);

  g_return_val_if_fail (entry != NULL, NULL);

  info = gimp_session_info_dockable_new ();

255
  info->locked     = gimp_dockable_get_locked (dockable);
256
  info->identifier = g_strdup (entry->identifier);
257
  info->tab_style  = gimp_dockable_get_tab_style (dockable);
258 259 260 261 262 263 264 265 266 267
  info->view_size  = -1;

  view = gimp_container_view_get_by_dockable (dockable);

  if (view)
    view_size = gimp_container_view_get_view_size (view, NULL);

  if (view_size > 0 && view_size != entry->view_size)
    info->view_size = view_size;

268 269 270
  if (GIMP_IS_SESSION_MANAGED (dockable))
    info->aux_info =
      gimp_session_managed_get_aux_info (GIMP_SESSION_MANAGED (dockable));
271 272 273 274

  return info;
}

Michael Natterer's avatar
Michael Natterer committed
275 276 277 278 279 280 281 282 283 284 285 286 287
GimpDockable *
gimp_session_info_dockable_restore (GimpSessionInfoDockable *info,
                                    GimpDock                *dock)
{
  GtkWidget *dockable;

  g_return_val_if_fail (info != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_DOCK (dock), NULL);

  if (info->view_size < GIMP_VIEW_SIZE_TINY ||
      info->view_size > GIMP_VIEW_SIZE_GIGANTIC)
    info->view_size = -1;

288 289 290 291 292
  dockable =
    gimp_dialog_factory_dockable_new (gimp_dock_get_dialog_factory (dock),
                                      dock,
                                      info->identifier,
                                      info->view_size);
Michael Natterer's avatar
Michael Natterer committed
293 294 295

  if (dockable)
    {
296 297 298 299 300 301 302
      /*  gimp_dialog_factory_dockable_new() might return an already
       *  existing singleton dockable, return NULL so our caller won't
       *  try to add it to another dockbook
       */
      if (gimp_dockable_get_dockbook (GIMP_DOCKABLE (dockable)))
        return NULL;

303
      gimp_dockable_set_locked    (GIMP_DOCKABLE (dockable), info->locked);
Michael Natterer's avatar
Michael Natterer committed
304 305 306
      gimp_dockable_set_tab_style (GIMP_DOCKABLE (dockable), info->tab_style);

      if (info->aux_info)
307 308
        gimp_session_managed_set_aux_info (GIMP_SESSION_MANAGED (dockable),
                                           info->aux_info);
Michael Natterer's avatar
Michael Natterer committed
309 310 311 312
    }

  return GIMP_DOCKABLE (dockable);
}