gimpsessioninfo-dock.c 10.6 KB
Newer Older
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpsessioninfo-dock.c
5 6
 * Copyright (C) 2001-2007 Michael Natterer <mitch@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
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
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 <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23 24
#include <string.h>

25 26 27 28 29 30 31 32
#include <gtk/gtk.h>

#include "libgimpconfig/gimpconfig.h"

#include "widgets-types.h"

#include "gimpdialogfactory.h"
#include "gimpdock.h"
33
#include "gimpdockbook.h"
34
#include "gimpdockcontainer.h"
35
#include "gimpdockwindow.h"
36
#include "gimpsessioninfo.h"
37 38 39
#include "gimpsessioninfo-aux.h"
#include "gimpsessioninfo-book.h"
#include "gimpsessioninfo-dock.h"
40
#include "gimpsessioninfo-private.h"
41
#include "gimptoolbox.h"
42 43 44 45


enum
{
46
  SESSION_INFO_SIDE,
47
  SESSION_INFO_POSITION,
48 49 50 51
  SESSION_INFO_BOOK
};


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static GimpAlignmentType gimp_session_info_dock_get_side (GimpDock *dock);


static GimpAlignmentType
gimp_session_info_dock_get_side (GimpDock *dock)
{
  GimpAlignmentType result   = -1;
  GtkWidget        *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (dock));

  if (GIMP_IS_DOCK_CONTAINER (toplevel))
    {
      GimpDockContainer *container = GIMP_DOCK_CONTAINER (toplevel);

      result = gimp_dock_container_get_dock_side (container, dock);
    }

  return result;
}


72 73
/*  public functions  */

74
GimpSessionInfoDock *
75
gimp_session_info_dock_new (const gchar *dock_type)
76
{
77 78 79
  GimpSessionInfoDock *dock_info = NULL;

  dock_info = g_slice_new0 (GimpSessionInfoDock);
80
  dock_info->dock_type = g_strdup (dock_type);
81
  dock_info->side      = -1;
82 83

  return dock_info;
84 85
}

86
void
87 88 89 90
gimp_session_info_dock_free (GimpSessionInfoDock *dock_info)
{
  g_return_if_fail (dock_info != NULL);

91
  if (dock_info->dock_type)
92
    {
93 94
      g_free (dock_info->dock_type);
      dock_info->dock_type = NULL;
95 96
    }

97 98
  if (dock_info->books)
    {
99 100
      g_list_free_full (dock_info->books,
                        (GDestroyNotify) gimp_session_info_book_free);
101 102 103 104 105 106 107 108 109
      dock_info->books = NULL;
    }

  g_slice_free (GimpSessionInfoDock, dock_info);
}

void
gimp_session_info_dock_serialize (GimpConfigWriter    *writer,
                                  GimpSessionInfoDock *dock_info)
110
{
111
  GList *list;
112 113

  g_return_if_fail (writer != NULL);
114
  g_return_if_fail (dock_info != NULL);
115

116
  gimp_config_writer_open (writer, dock_info->dock_type);
117

118 119 120 121
  if (dock_info->side != -1)
    {
      const char *side_text =
        dock_info->side == GIMP_ALIGN_LEFT ? "left" : "right";
122

123 124 125 126 127
      gimp_config_writer_open (writer, "side");
      gimp_config_writer_print (writer, side_text, strlen (side_text));
      gimp_config_writer_close (writer);
    }

128
  if (dock_info->position != 0)
129 130 131 132 133 134 135 136 137
    {
      gint position;

      position = gimp_session_info_apply_position_accuracy (dock_info->position);

      gimp_config_writer_open (writer, "position");
      gimp_config_writer_printf (writer, "%d", position);
      gimp_config_writer_close (writer);
    }
138

139
  for (list = dock_info->books; list; list = g_list_next (list))
140
    gimp_session_info_book_serialize (writer, list->data);
141 142 143 144 145

  gimp_config_writer_close (writer);
}

GTokenType
146 147
gimp_session_info_dock_deserialize (GScanner             *scanner,
                                    gint                  scope,
148
                                    GimpSessionInfoDock **dock_info,
149
                                    const gchar          *dock_type)
150 151 152 153
{
  GTokenType token;

  g_return_val_if_fail (scanner != NULL, G_TOKEN_LEFT_PAREN);
154
  g_return_val_if_fail (dock_info != NULL, G_TOKEN_LEFT_PAREN);
155

156 157
  g_scanner_scope_add_symbol (scanner, scope, "side",
                              GINT_TO_POINTER (SESSION_INFO_SIDE));
158 159
  g_scanner_scope_add_symbol (scanner, scope, "position",
                              GINT_TO_POINTER (SESSION_INFO_POSITION));
160 161 162
  g_scanner_scope_add_symbol (scanner, scope, "book",
                              GINT_TO_POINTER (SESSION_INFO_BOOK));

163
  *dock_info = gimp_session_info_dock_new (dock_type);
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  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))
            {
180 181
              GimpSessionInfoBook *book;

182 183 184 185 186 187 188 189 190 191 192 193 194
            case SESSION_INFO_SIDE:
              token = G_TOKEN_IDENTIFIER;
              if (g_scanner_peek_next_token (scanner) != token)
                break;

              g_scanner_get_next_token (scanner);

              if (strcmp ("left", scanner->value.v_identifier) == 0)
                (*dock_info)->side = GIMP_ALIGN_LEFT;
              else
                (*dock_info)->side = GIMP_ALIGN_RIGHT;
              break;

195 196 197 198 199 200
            case SESSION_INFO_POSITION:
              token = G_TOKEN_INT;
              if (! gimp_scanner_parse_int (scanner, &((*dock_info)->position)))
                (*dock_info)->position = 0;
              break;

201 202 203
            case SESSION_INFO_BOOK:
              g_scanner_set_scope (scanner, scope + 1);
              token = gimp_session_info_book_deserialize (scanner, scope + 1,
204
                                                          &book);
205 206

              if (token == G_TOKEN_LEFT_PAREN)
207
                {
208
                  (*dock_info)->books = g_list_append ((*dock_info)->books, book);
209 210
                  g_scanner_set_scope (scanner, scope);
                }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
              else
                return token;

              break;

            default:
              return token;
            }
          token = G_TOKEN_RIGHT_PAREN;
          break;

        case G_TOKEN_RIGHT_PAREN:
          token = G_TOKEN_LEFT_PAREN;
          break;

        default:
          break;
        }
    }

  g_scanner_scope_remove_symbol (scanner, scope, "book");
232
  g_scanner_scope_remove_symbol (scanner, scope, "position");
233
  g_scanner_scope_remove_symbol (scanner, scope, "side");
234 235 236 237

  return token;
}

238
GimpSessionInfoDock *
239 240
gimp_session_info_dock_from_widget (GimpDock *dock)
{
241 242 243
  GimpSessionInfoDock *dock_info;
  GList               *list;
  GtkWidget           *parent;
244 245 246

  g_return_val_if_fail (GIMP_IS_DOCK (dock), NULL);

247 248 249
  dock_info = gimp_session_info_dock_new (GIMP_IS_TOOLBOX (dock) ?
                                          "gimp-toolbox" :
                                          "gimp-dock");
250

251
  for (list = gimp_dock_get_dockbooks (dock); list; list = g_list_next (list))
252 253 254 255 256
    {
      GimpSessionInfoBook *book;

      book = gimp_session_info_book_from_widget (list->data);

257
      dock_info->books = g_list_prepend (dock_info->books, book);
258 259
    }

260
  dock_info->books = g_list_reverse (dock_info->books);
261
  dock_info->side  = gimp_session_info_dock_get_side (dock);
262

263 264 265 266 267 268 269 270 271 272
  parent = gtk_widget_get_parent (GTK_WIDGET (dock));

  if (GTK_IS_PANED (parent))
    {
      GtkPaned *paned = GTK_PANED (parent);

      if (GTK_WIDGET (dock) == gtk_paned_get_child2 (paned))
        dock_info->position = gtk_paned_get_position (paned);
    }

273
  return dock_info;
274 275
}

276
GimpDock *
277 278
gimp_session_info_dock_restore (GimpSessionInfoDock *dock_info,
                                GimpDialogFactory   *factory,
279
                                GdkMonitor          *monitor,
280
                                GimpDockContainer   *dock_container)
281
{
282 283 284 285
  gint           n_books = 0;
  GtkWidget     *dock;
  GList         *iter;
  GimpUIManager *ui_manager;
286

287
  g_return_val_if_fail (GIMP_IS_DIALOG_FACTORY (factory), NULL);
288
  g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);
289

290
  ui_manager = gimp_dock_container_get_ui_manager (dock_container);
291
  dock       = gimp_dialog_factory_dialog_new (factory, monitor,
292
                                               ui_manager,
293
                                               NULL,
294
                                               dock_info->dock_type,
295 296
                                               -1 /*view_size*/,
                                               FALSE /*present*/);
297

298
  g_return_val_if_fail (GIMP_IS_DOCK (dock), NULL);
299

300 301 302
  /* Add the dock to the dock window immediately so the stuff in the
   * dock has access to e.g. a dialog factory
   */
303 304 305
  gimp_dock_container_add_dock (dock_container,
                                GIMP_DOCK (dock),
                                dock_info);
306

307 308 309
  /* Note that if it is a toolbox, we will get here even though we
   * don't have any books
   */
310
  for (iter = dock_info->books;
311 312
       iter;
       iter = g_list_next (iter))
313
    {
314
      GimpSessionInfoBook *book_info = iter->data;
315 316
      GtkWidget           *dockbook;

317 318
      dockbook = GTK_WIDGET (gimp_session_info_book_restore (book_info,
                                                             GIMP_DOCK (dock)));
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
      if (dockbook)
        {
          GtkWidget *parent = gtk_widget_get_parent (dockbook);

          n_books++;

          if (GTK_IS_PANED (parent))
            {
              GtkPaned *paned = GTK_PANED (parent);

              if (dockbook == gtk_paned_get_child2 (paned))
                gtk_paned_set_position (paned, book_info->position);
            }
        }
    }

  /* Now remove empty dockbooks from the list, check the comment in
   * gimp_session_info_book_restore() which explains why the dock
   * can contain empty dockbooks at all
   */
340
  if (dock_info->books)
341 342 343 344 345 346
    {
      GList *books;

      books = g_list_copy (gimp_dock_get_dockbooks (GIMP_DOCK (dock)));

      while (books)
347
        {
348 349 350 351 352 353 354 355 356
          GtkContainer *dockbook = books->data;
          GList        *children = gtk_container_get_children (dockbook);

          if (children)
            {
              g_list_free (children);
            }
          else
            {
357
              g_object_ref (dockbook);
358
              gimp_dock_remove_book (GIMP_DOCK (dock), GIMP_DOCKBOOK (dockbook));
359 360 361
              gtk_widget_destroy (GTK_WIDGET (dockbook));
              g_object_unref (dockbook);

362 363
              n_books--;
            }
364

365
          books = g_list_remove (books, dockbook);
366 367 368
        }
    }

369
  /*  if we removed all books again, the dock was destroyed, so bail out  */
370
  if (dock_info->books && n_books == 0)
371 372 373 374
    {
      return NULL;
    }

375
  gtk_widget_show (dock);
376 377

  return GIMP_DOCK (dock);
378
}