exit_dialog.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 
 * exit_dialog.h: Dialog to allow the user to choose which data to
 * save on exit or to cancel exit.
 *
 * Copyright (C) 2007 Patrick Hallinan
 *
 * 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 2 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
18 19
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

/* TODO: Non-modal api */

#include <config.h>

#include "exit_dialog.h"

#include "intl.h"

#include <gtk/gtk.h>

#include <glib.h>

34
#define EXIT_DIALOG_TREEVIEW "EXIT_DIALOG_TREEVIEW"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

enum {
  CHECK_COL,
  NAME_COL,
  PATH_COL,
  DATA_COL, /* To store optional data (not shown in listview) */
  NUM_COL
};

static void selected_state_set_all   (GtkTreeView * treeview,
                                      gboolean      state);

static gint get_selected_items (GtkWidget * dialog,
                                exit_dialog_item_array_t ** items);

/* Event Handlers */
51 52 53
static void exit_dialog_destroy  (GtkWidget * exit_dialog,
                                  gpointer    data);

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static void select_all_clicked   (GtkButton * button,
                                  gpointer    data);

static void select_none_clicked  (GtkButton * button,
                                  gpointer    data);

static void toggle_check_button  (GtkCellRendererToggle * renderer,
                                  gchar * path,
                                  GtkTreeView * treeview);

/* A dialog to allow a user to select which unsaved files to save
 * (if any) or to abort exiting
 *
 * @param parent_window This is needed for modal behavior.
 * @param title Text display on the dialog's title bar.
 * @return The dialog.
 */
GtkWidget *
exit_dialog_make (GtkWindow * parent_window,
                  gchar *     title)
{
    GtkWidget * dialog = gtk_dialog_new_with_buttons (title, parent_window,
                                                      GTK_DIALOG_MODAL,
77
                                                      _("_Do Not Exit"),
78
                                                      EXIT_DIALOG_EXIT_CANCEL,
79
                                                      _("_Exit Without Save"),
80
                                                      EXIT_DIALOG_EXIT_NO_SAVE,
81
                                                      _("_Save Selected"),
82 83 84
                                                      EXIT_DIALOG_EXIT_SAVE_SELECTED,
                                                      NULL);

85
    GtkBox * vbox = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(dialog)));
86 87 88 89 90 91 92 93 94 95 96 97

    GtkWidget * label = gtk_label_new (_("The following are not saved:"));

    GtkWidget * scrolled;
    GtkWidget * button;
    GtkWidget * hbox;

    GtkWidget *         treeview;
    GtkListStore *      model;
    GtkCellRenderer *   renderer;
    GtkTreeViewColumn * column;

98 99
    GdkGeometry geometry = { 0 };

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    gtk_box_pack_start (vbox, label, FALSE, FALSE, 0);
    
    gtk_widget_show (label);

    /* Scrolled window for displaying things which need saving */
    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_box_pack_start (vbox, scrolled, TRUE, TRUE, 0);
    gtk_widget_show (scrolled);

    model = gtk_list_store_new (NUM_COL, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);

    treeview = gtk_tree_view_new_with_model ( GTK_TREE_MODEL(model));

    renderer = gtk_cell_renderer_toggle_new ();

    column   = gtk_tree_view_column_new_with_attributes (_("Save"), renderer,
116
                                                         "active", CHECK_COL,
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
                                                          NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

    g_signal_connect (G_OBJECT (renderer), "toggled",
                      G_CALLBACK (toggle_check_button), 
                      treeview);

    renderer = gtk_cell_renderer_text_new ();
    column   = gtk_tree_view_column_new_with_attributes (_("Name"), renderer,
                                                         "text", NAME_COL,
                                                          NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

    renderer = gtk_cell_renderer_text_new ();
    column   = gtk_tree_view_column_new_with_attributes (_("Path"), renderer,
                                                         "text", PATH_COL,
                                                          NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);


    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled), GTK_WIDGET (treeview));

 
    hbox = gtk_hbox_new (FALSE, 3);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
    gtk_widget_show (hbox);

    button = gtk_button_new_with_label (_("Select All"));
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 3);
    gtk_widget_show (button);

    g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (select_all_clicked), 
                      treeview);

    button = gtk_button_new_with_label (_("Select None"));
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 3);
    gtk_widget_show (button);

    g_signal_connect (G_OBJECT (button), "clicked",
                      G_CALLBACK (select_none_clicked), 
                      treeview);

    g_object_unref (model);
    gtk_widget_show (GTK_WIDGET (treeview));

    gtk_widget_show_all (GTK_WIDGET(vbox));

165 166 167 168 169
    g_object_set_data (G_OBJECT (dialog), EXIT_DIALOG_TREEVIEW,  treeview);
    
    g_signal_connect (G_OBJECT (dialog), "destroy",
                      G_CALLBACK (exit_dialog_destroy), 
                      treeview);
170 171 172 173 174 175 176

    /* golden ratio */
    geometry.min_aspect = 0.618;
    geometry.max_aspect = 1.618;
    geometry.win_gravity = GDK_GRAVITY_CENTER;
    gtk_window_set_geometry_hints (GTK_WINDOW (dialog), GTK_WIDGET (vbox), &geometry,
                                   GDK_HINT_ASPECT|GDK_HINT_WIN_GRAVITY);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    return dialog;
}

/**
 * Add name and path of a file that needs to be saved
 * @param dialog Exit dialog created with exit_dialog_make()
 * @param name User identifiable name of the thing which needs saving.
 * @param path File system path of the thing which needs saving.
 * @param optional_data Optional data to be returned with selected file info.
 */
void
exit_dialog_add_item (GtkWidget *    dialog,
                      const gchar *  name, 
                      const gchar *  path, 
                      const gpointer optional_data)
{
193
    GtkTreeView *  treeview;
194 195
    GtkTreeIter    iter;
    GtkListStore * model;
196 197
    const gchar *  name_copy = g_strdup (name);
    const gchar *  path_copy = g_strdup (path);
198

199
    treeview = g_object_get_data (G_OBJECT (dialog), EXIT_DIALOG_TREEVIEW);
200
  
201
    model = GTK_LIST_STORE (gtk_tree_view_get_model (treeview)); 
202 203 204 205 206

    gtk_list_store_append (model, &iter);

    gtk_list_store_set (model, &iter,
                        CHECK_COL, 1,
207 208
                        NAME_COL, name_copy,
                        PATH_COL, path_copy,
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                        DATA_COL, optional_data,
                        -1);
}

gint
exit_dialog_run (GtkWidget * dialog,
                 exit_dialog_item_array_t ** items)
{
    gint                       result;
    gint                       count;
    
    while (TRUE)
    {
        result = gtk_dialog_run ( GTK_DIALOG(dialog));

        switch (result) 
        {
            case EXIT_DIALOG_EXIT_CANCEL:
            case EXIT_DIALOG_EXIT_NO_SAVE:
                *items = NULL;
                return result;
230 231 232 233
	    case EXIT_DIALOG_EXIT_SAVE_SELECTED :
		break;
	    default : /* e.g. if closed by window manager button */
	        return EXIT_DIALOG_EXIT_CANCEL;
234 235 236 237 238 239 240 241 242 243
        }
    
        count = get_selected_items (dialog, items);
    
        if (count == 0)
        {
           GtkWidget * msg_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
                                                            GTK_DIALOG_MODAL,
                                                            GTK_MESSAGE_WARNING,
                                                            GTK_BUTTONS_YES_NO,
244
                                                            _("Nothing selected for saving.  Would you like to try again?"));
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

           gint yes_or_no = gtk_dialog_run (GTK_DIALOG (msg_dialog));

           gtk_widget_destroy (msg_dialog);

           if (yes_or_no == GTK_RESPONSE_NO)
           {
               return EXIT_DIALOG_EXIT_NO_SAVE;
           }
        }
        else
        {
            return EXIT_DIALOG_EXIT_SAVE_SELECTED;
        }
    }
}


/**
 * Gets the list of items selected for saving by the user.
 * @param dialog Exit dialog.
 * @param items Structure to hold the selected items.  Set to NULL if not items selected.
 * @return The number of selected items.
 */
gint
get_selected_items (GtkWidget * dialog,
                    exit_dialog_item_array_t ** items)
{
273
    GtkTreeView *              treeview;
274 275 276 277 278 279 280 281
    GtkTreeIter                iter;
    GtkListStore *             model;
    gboolean                   valid;
    GSList *                   list = NULL;
    GSList *                   list_iter;
    gint                       selected_count;
    gint                       i;

282
    treeview = g_object_get_data (G_OBJECT (dialog), EXIT_DIALOG_TREEVIEW);
283
  
284
    model = GTK_LIST_STORE (gtk_tree_view_get_model (treeview)); 
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  
    /* Get the first iter in the list */
    valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);

    /* Get the selected items */
    while (valid)
    {
        const char * name;
        const char * path;
        gpointer     data;
        gboolean     is_selected;

        gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 
                            CHECK_COL, &is_selected,
                            NAME_COL,  &name,
                            PATH_COL,  &path,
                            DATA_COL,  &data,
                            -1);

        if (is_selected)
        {
            exit_dialog_item_t * item = g_new (exit_dialog_item_t,1);
            item->name = name;
            item->path = path;
            item->data = data;
            list = g_slist_prepend (list, item);
        }
        
        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
    }

    selected_count = g_slist_length (list);

    if (selected_count > 0)
    {
        *items              = g_new (exit_dialog_item_array_t,1);
       (*items)->array_size = selected_count;
       (*items)->array      = g_new (exit_dialog_item_t, (*items)->array_size);
    
        list_iter = list;
        for(i = 0 ; i < selected_count ; i++)
        {
Hans Breuer's avatar
Hans Breuer committed
327
	    exit_dialog_item_t * item;
328
            g_assert(list_iter!=NULL); /* can't be if g_slist_length works */
Hans Breuer's avatar
Hans Breuer committed
329
            item = list_iter->data;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            (*items)->array[i].name = item->name;
            (*items)->array[i].path = item->path;
            (*items)->array[i].data = item->data;
            list_iter = g_slist_next(list_iter);
        }
    
        g_slist_free (list);
    }
    else
    {
      *items = NULL;
    }

    return selected_count;
}

/**
 * Free memory allocated for the exit_dialog_item_array_t.  This
 * will not free any memory not allocated by the dialog itself
 * @param items Item array struct returned by exit_dialog_run()
 */
void exit_dialog_free_items (exit_dialog_item_array_t * items)
{
  if (items)
  {
    int i;
    for (i = 0 ; i < items->array_size ; i++)
    {
      /* Cast is needed to remove warning because of const decl */
      g_free ( (char*)items->array[i].name);
      g_free ( (char*)items->array[i].path);
    }

    g_free (items);
  }
}

/** 
 * Handler for the check box (button) in the exit dialogs treeview.
 * This is needed to cause the check box to change state when the
 * user clicks it
 */
static void toggle_check_button (GtkCellRendererToggle * renderer,
                                 gchar * path,
                                 GtkTreeView * treeview)
{ 
    GtkTreeModel * model;
    GtkTreeIter    iter;
    gboolean       value;

    model = gtk_tree_view_get_model (treeview);
    if (gtk_tree_model_get_iter_from_string (model, &iter, path))
    {
        gtk_tree_model_get (model, &iter, CHECK_COL, &value, -1);
        gtk_list_store_set (GTK_LIST_STORE (model), &iter, CHECK_COL, !value, -1);
    }
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/*
 * Signal handler for "destroy" event to free memory allocated for the exit_dialog.
 *
 * @param exit_dialog 
 * @param data Exit dialog's treeview
 */
static void exit_dialog_destroy  (GtkWidget * exit_dialog,
                                  gpointer    data)
{
    GtkTreeView *  treeview;
    GtkTreeIter    iter;
    GtkTreeModel * model;
    gboolean       valid;
  
    treeview = g_object_get_data (G_OBJECT (exit_dialog), EXIT_DIALOG_TREEVIEW);

    model = GTK_TREE_MODEL (gtk_tree_view_get_model (treeview)); 
    
    /* Get the first iter in the list */
    valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);

    while (valid)
    {
        gchar * name = NULL;
        gchar * path = NULL;

        gtk_tree_model_get (model, &iter,
                            NAME_COL, &name,
                            PATH_COL, &path,
                            -1);

        g_free (name);
        g_free (path);

        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
    }
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/**
 * Sets the state of the save checkbox in the treeview to the
 * state specified by the caller.
 * @param treeview The treeview in the exit dialog box.
 * @param state Set to TRUE to select all, FALSE to de-select all.
 */
static void selected_state_set_all (GtkTreeView * treeview,
                                    gboolean      state)
{
    GtkTreeIter    iter;
    GtkListStore * model;
    gboolean       valid;

    model = GTK_LIST_STORE (gtk_tree_view_get_model (treeview)); 
  
    /* Get the first iter in the list */
    valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter);

    while (valid)
    {
        gtk_list_store_set (model, &iter,
                            CHECK_COL, state,
                            -1);

        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter);
    }
}

/**
 * Handler for Select All button.  Causes all checkboxes in the
 * exit dialog box treeview to be checked.
 * @param button The Select all button.
 * @param data The treeview
 */
static void select_all_clicked   (GtkButton * button,
                                  gpointer    data)
{
    selected_state_set_all (data, TRUE);
}

/**
 * Handler for Select All button.  Causes all checkboxes in the
 * exit dialog box treeview to be un-checked.
 * @param button The Select all button.
 * @param data The treeview
 */
static void select_none_clicked  (GtkButton * button,
                                  gpointer    data)
{
    selected_state_set_all (data, FALSE);
}