gtkprinteroptionwidget.c 24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GtkPrinterOptionWidget
 * Copyright (C) 2006 Alexander Larsson  <alexl@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
24
#include <ctype.h>
25 26 27 28 29 30 31

#include "gtkintl.h"
#include "gtkalignment.h"
#include "gtkcheckbutton.h"
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"
#include "gtkcombobox.h"
32
#include "gtkcomboboxentry.h"
33 34 35 36
#include "gtkfilechooserbutton.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtkliststore.h"
37
#include "gtkradiobutton.h"
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
#include "gtkstock.h"
#include "gtktable.h"
#include "gtktogglebutton.h"
#include "gtkprivate.h"

#include "gtkprinteroptionwidget.h"
#include "gtkalias.h"

#define GTK_PRINTER_OPTION_WIDGET_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINTER_OPTION_WIDGET, GtkPrinterOptionWidgetPrivate))

static void gtk_printer_option_widget_finalize (GObject *object);

static void deconstruct_widgets (GtkPrinterOptionWidget *widget);
static void construct_widgets (GtkPrinterOptionWidget *widget);
static void update_widgets (GtkPrinterOptionWidget *widget);

struct GtkPrinterOptionWidgetPrivate
{
  GtkPrinterOption *source;
  gulong source_changed_handler;
  
  GtkWidget *check;
  GtkWidget *combo;
  GtkWidget *entry;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *filechooser;
66
  GtkWidget *box;
67 68 69 70 71 72 73 74 75
};

enum {
  CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
76
  PROP_SOURCE
77 78 79 80
};

static guint signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
81
G_DEFINE_TYPE (GtkPrinterOptionWidget, gtk_printer_option_widget, GTK_TYPE_HBOX)
82 83 84 85 86 87 88 89 90

static void gtk_printer_option_widget_set_property (GObject      *object,
						    guint         prop_id,
						    const GValue *value,
						    GParamSpec   *pspec);
static void gtk_printer_option_widget_get_property (GObject      *object,
						    guint         prop_id,
						    GValue       *value,
						    GParamSpec   *pspec);
91
static gboolean gtk_printer_option_widget_mnemonic_activate (GtkWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
92
							      gboolean  group_cycling);
93 94 95 96 97 98 99 100 101 102 103 104 105 106

static void
gtk_printer_option_widget_class_init (GtkPrinterOptionWidgetClass *class)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;

  object_class = (GObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;

  object_class->finalize = gtk_printer_option_widget_finalize;
  object_class->set_property = gtk_printer_option_widget_set_property;
  object_class->get_property = gtk_printer_option_widget_get_property;

107 108
  widget_class->mnemonic_activate = gtk_printer_option_widget_mnemonic_activate;

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
  g_type_class_add_private (class, sizeof (GtkPrinterOptionWidgetPrivate));  

  signals[CHANGED] =
    g_signal_new ("changed",
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrinterOptionWidgetClass, changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  g_object_class_install_property (object_class,
                                   PROP_SOURCE,
                                   g_param_spec_object ("source",
							P_("Source option"),
							P_("The PrinterOption backing this widget"),
							GTK_TYPE_PRINTER_OPTION,
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));

}

static void
gtk_printer_option_widget_init (GtkPrinterOptionWidget *widget)
{
  widget->priv = GTK_PRINTER_OPTION_WIDGET_GET_PRIVATE (widget); 
134 135

  gtk_box_set_spacing (GTK_BOX (widget), 12);
136 137 138 139 140
}

static void
gtk_printer_option_widget_finalize (GObject *object)
{
141 142
  GtkPrinterOptionWidget *widget = GTK_PRINTER_OPTION_WIDGET (object);
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
143
  
144
  if (priv->source)
145
    {
146 147
      g_object_unref (priv->source);
      priv->source = NULL;
148 149
    }
  
150
  G_OBJECT_CLASS (gtk_printer_option_widget_parent_class)->finalize (object);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

static void
gtk_printer_option_widget_set_property (GObject         *object,
					guint            prop_id,
					const GValue    *value,
					GParamSpec      *pspec)
{
  GtkPrinterOptionWidget *widget;
  
  widget = GTK_PRINTER_OPTION_WIDGET (object);

  switch (prop_id)
    {
    case PROP_SOURCE:
      gtk_printer_option_widget_set_source (widget, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_printer_option_widget_get_property (GObject    *object,
					guint       prop_id,
					GValue     *value,
					GParamSpec *pspec)
{
180 181
  GtkPrinterOptionWidget *widget = GTK_PRINTER_OPTION_WIDGET (object);
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
182 183 184 185

  switch (prop_id)
    {
    case PROP_SOURCE:
186
      g_value_set_object (value, priv->source);
187 188 189 190 191 192 193
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static gboolean
gtk_printer_option_widget_mnemonic_activate (GtkWidget *widget,
					     gboolean   group_cycling)
{
  GtkPrinterOptionWidget *powidget = GTK_PRINTER_OPTION_WIDGET (widget);
  GtkPrinterOptionWidgetPrivate *priv = powidget->priv;

  if (priv->check)
    return gtk_widget_mnemonic_activate (priv->check, group_cycling);
  if (priv->combo)
    return gtk_widget_mnemonic_activate (priv->combo, group_cycling);
  if (priv->entry)
    return gtk_widget_mnemonic_activate (priv->entry, group_cycling);

  return FALSE;
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static void
emit_changed (GtkPrinterOptionWidget *widget)
{
  g_signal_emit (widget, signals[CHANGED], 0);
}

GtkWidget *
gtk_printer_option_widget_new (GtkPrinterOption *source)
{
  return g_object_new (GTK_TYPE_PRINTER_OPTION_WIDGET, "source", source, NULL);
}

static void
source_changed_cb (GtkPrinterOption *source,
		   GtkPrinterOptionWidget  *widget)
{
  update_widgets (widget);
  emit_changed (widget);
}

void
Matthias Clasen's avatar
Matthias Clasen committed
232 233
gtk_printer_option_widget_set_source (GtkPrinterOptionWidget *widget,
				      GtkPrinterOption       *source)
234
{
235 236
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;

237 238 239
  if (source)
    g_object_ref (source);
  
240
  if (priv->source)
241
    {
242 243 244
      g_signal_handler_disconnect (priv->source,
				   priv->source_changed_handler);
      g_object_unref (priv->source);
245 246
    }

247
  priv->source = source;
248 249

  if (source)
250
    priv->source_changed_handler =
251 252 253 254 255 256 257 258
      g_signal_connect (source, "changed", G_CALLBACK (source_changed_cb), widget);

  construct_widgets (widget);
  update_widgets (widget);

  g_object_notify (G_OBJECT (widget), "source");
}

Matthias Clasen's avatar
Matthias Clasen committed
259 260 261 262 263 264
enum {
  NAME_COLUMN,
  VALUE_COLUMN,
  N_COLUMNS
};

265 266
static void
combo_box_set_model (GtkWidget *combo_box)
267 268 269
{
  GtkListStore *store;

Matthias Clasen's avatar
Matthias Clasen committed
270
  store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
271
  gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
272
  g_object_unref (store);
273 274 275 276 277 278
}

static void
combo_box_set_view (GtkWidget *combo_box)
{
  GtkCellRenderer *cell;
279 280 281 282

  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
Matthias Clasen's avatar
Matthias Clasen committed
283
                                  "text", NAME_COLUMN,
284 285 286 287 288 289 290 291 292 293 294
                                   NULL);
}

static GtkWidget *
combo_box_entry_new (void)
{
  GtkWidget *combo_box;
  combo_box = gtk_combo_box_entry_new ();

  combo_box_set_model (combo_box);

Matthias Clasen's avatar
Matthias Clasen committed
295
  gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo_box), NAME_COLUMN);
296 297 298 299 300 301 302 303 304 305 306 307

  return combo_box;
}

static GtkWidget *
combo_box_new (void)
{
  GtkWidget *combo_box;
  combo_box = gtk_combo_box_new ();

  combo_box_set_model (combo_box);
  combo_box_set_view (combo_box);
308 309 310 311 312

  return combo_box;
}
  
static void
Matthias Clasen's avatar
Matthias Clasen committed
313 314 315
combo_box_append (GtkWidget   *combo,
		  const gchar *display_text,
		  const gchar *value)
316 317 318 319 320 321 322 323 324 325
{
  GtkTreeModel *model;
  GtkListStore *store;
  GtkTreeIter iter;
  
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
  store = GTK_LIST_STORE (model);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
Matthias Clasen's avatar
Matthias Clasen committed
326 327
		      NAME_COLUMN, display_text,
		      VALUE_COLUMN, value,
328 329 330 331 332
		      -1);
}

struct ComboSet {
  GtkComboBox *combo;
Matthias Clasen's avatar
Matthias Clasen committed
333
  const gchar *value;
334 335 336
};

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
337 338 339 340
set_cb (GtkTreeModel *model, 
	GtkTreePath  *path, 
	GtkTreeIter  *iter, 
	gpointer      data)
341 342 343 344 345
{
  struct ComboSet *set_data = data;
  gboolean found;
  char *value;
  
Matthias Clasen's avatar
Matthias Clasen committed
346
  gtk_tree_model_get (model, iter, VALUE_COLUMN, &value, -1);
347 348 349 350 351 352 353 354 355 356
  found = (strcmp (value, set_data->value) == 0);
  g_free (value);
  
  if (found)
    gtk_combo_box_set_active_iter (set_data->combo, iter);

  return found;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
357 358
combo_box_set (GtkWidget   *combo,
	       const gchar *value)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
{
  GtkTreeModel *model;
  struct ComboSet set_data;
  
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));

  set_data.combo = GTK_COMBO_BOX (combo);
  set_data.value = value;
  gtk_tree_model_foreach (model, set_cb, &set_data);
}

static char *
combo_box_get (GtkWidget *combo)
{
  GtkTreeModel *model;
Matthias Clasen's avatar
Matthias Clasen committed
374
  gchar *value;
375 376
  GtkTreeIter iter;

377 378
  if (GTK_IS_COMBO_BOX_ENTRY (combo))
    {
Matthias Clasen's avatar
Matthias Clasen committed
379
      value = gtk_combo_box_get_active_text(GTK_COMBO_BOX (combo));
380 381 382 383 384
    }
  else
    {
      model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));

Matthias Clasen's avatar
Matthias Clasen committed
385
      value = NULL;
386
      if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
Matthias Clasen's avatar
Matthias Clasen committed
387
         gtk_tree_model_get (model, &iter, VALUE_COLUMN, &value, -1);
388
     }
Matthias Clasen's avatar
Matthias Clasen committed
389 390

  return value;
391 392 393 394 395 396
}


static void
deconstruct_widgets (GtkPrinterOptionWidget *widget)
{
397 398 399
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;

  if (priv->check)
400
    {
401 402
      gtk_widget_destroy (priv->check);
      priv->check = NULL;
403 404
    }
  
405
  if (priv->combo)
406
    {
407 408
      gtk_widget_destroy (priv->combo);
      priv->combo = NULL;
409 410
    }
  
411
  if (priv->entry)
412
    {
413 414
      gtk_widget_destroy (priv->entry);
      priv->entry = NULL;
415 416 417 418
    }

  /* make sure entry and combo are destroyed first */
  /* as we use the two of them to create the filechooser */
419
  if (priv->filechooser)
420
    {
421 422
      gtk_widget_destroy (priv->filechooser);
      priv->filechooser = NULL;
423 424
    }

425
  if (priv->image)
426
    {
427 428
      gtk_widget_destroy (priv->image);
      priv->image = NULL;
429 430
    }

431
  if (priv->label)
432
    {
433 434
      gtk_widget_destroy (priv->label);
      priv->label = NULL;
435 436 437 438
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
439
check_toggled_cb (GtkToggleButton        *toggle_button,
440 441
		  GtkPrinterOptionWidget *widget)
{
442 443 444 445
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;

  g_signal_handler_block (priv->source, priv->source_changed_handler);
  gtk_printer_option_set_boolean (priv->source,
446
				  gtk_toggle_button_get_active (toggle_button));
447
  g_signal_handler_unblock (priv->source, priv->source_changed_handler);
448 449 450 451
  emit_changed (widget);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
452
filesave_changed_cb (GtkWidget              *button,
453 454
                     GtkPrinterOptionWidget *widget)
{
455
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
Matthias Clasen's avatar
Matthias Clasen committed
456
  gchar *uri, *file;
457 458

  /* TODO: how do we support nonlocal file systems? */
459 460 461 462 463 464 465
  file = g_filename_from_utf8 (gtk_entry_get_text (GTK_ENTRY (priv->entry)),
			       -1, NULL, NULL, NULL);
  if (file == NULL)
    return;

  /* combine the value of the chooser with the value of the entry */
  g_signal_handler_block (priv->source, priv->source_changed_handler);  
466

467
  if (g_path_is_absolute (file))
468 469 470
    uri = g_filename_to_uri (file, NULL, NULL);
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
471 472
      gchar *path;

473
#ifdef G_OS_UNIX
474 475
      if (file[0] == '~' && file[1] == '/')
        {
Matthias Clasen's avatar
Matthias Clasen committed
476
          path = g_build_filename (g_get_home_dir (), file + 2, NULL);
477
	}
Matthias Clasen's avatar
Matthias Clasen committed
478
      else
479
#endif
Matthias Clasen's avatar
Matthias Clasen committed
480 481 482
        {
          gchar *directory;

483
          directory = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->combo));
Matthias Clasen's avatar
Matthias Clasen committed
484 485 486 487 488
          path = g_build_filename (directory, file, NULL);

          g_free (directory);
        }

489
      uri = g_filename_to_uri (path, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
490

491 492
      g_free (path);
    }
493
 
494 495
  if (uri)
    gtk_printer_option_set (priv->source, uri);
496

497
  g_free (uri);
Matthias Clasen's avatar
Matthias Clasen committed
498
  g_free (file);
499

500
  g_signal_handler_unblock (priv->source, priv->source_changed_handler);
501 502 503
  emit_changed (widget);
}

Matthias Clasen's avatar
Matthias Clasen committed
504 505 506 507 508
static gchar *
filter_numeric (const gchar *val,
                gboolean     allow_neg,
		gboolean     allow_dec,
                gboolean    *changed_out)
509 510 511 512 513 514 515 516 517 518
{
  gchar *filtered_val;
  int i, j;
  int len = strlen (val);
  gboolean dec_set = FALSE;

  filtered_val = g_malloc (len + 1);

  for (i = 0, j = 0; i < len; i++)
    {
Matthias Clasen's avatar
Matthias Clasen committed
519
      if (isdigit (val[i]))
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
        {
          filtered_val[j] = val[i];
	  j++;
	}
      else if (allow_dec && !dec_set && 
               (val[i] == '.' || val[i] == ','))
        {
	  /* allow one period or comma
	   * we should be checking locals
	   * but this is good enough for now
	   */
          filtered_val[j] = val[i];
	  dec_set = TRUE;
	  j++;
	}
      else if (allow_neg && i == 0 && val[0] == '-')
        {
          filtered_val[0] = val[0];
	  j++;
	}
    }

  filtered_val[j] = '\0';
  *changed_out = !(i == j);

  return filtered_val;
}
Matthias Clasen's avatar
Matthias Clasen committed
547

548
static void
Matthias Clasen's avatar
Matthias Clasen committed
549
combo_changed_cb (GtkWidget              *combo,
550 551
		  GtkPrinterOptionWidget *widget)
{
552
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
553 554 555 556
  gchar *value;
  gchar *filtered_val = NULL;
  gboolean changed;

557
  g_signal_handler_block (priv->source, priv->source_changed_handler);
558
  
559
  value = combo_box_get (combo);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

  /* handle some constraints */
  switch (priv->source->type)
    {
    case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE:
      filtered_val = filter_numeric (value, FALSE, FALSE, &changed);
      break;   
    case GTK_PRINTER_OPTION_TYPE_PICKONE_INT:
      filtered_val = filter_numeric (value, TRUE, FALSE, &changed);
      break;
    case GTK_PRINTER_OPTION_TYPE_PICKONE_REAL:
      filtered_val = filter_numeric (value, TRUE, TRUE, &changed);
      break;
    default:
      break;
    }

  if (filtered_val)
    {
      g_free (value);

      if (changed)
        {
          GtkEntry *entry;
	  
	  entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));

          gtk_entry_set_text (entry, filtered_val);
	}
      value = filtered_val;
    }

592
  if (value)
593
    gtk_printer_option_set (priv->source, value);
594
  g_free (value);
595
  g_signal_handler_unblock (priv->source, priv->source_changed_handler);
596 597 598 599
  emit_changed (widget);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
600
entry_changed_cb (GtkWidget              *entry,
601 602
		  GtkPrinterOptionWidget *widget)
{
603
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
Matthias Clasen's avatar
Matthias Clasen committed
604
  const gchar *value;
605
  
606
  g_signal_handler_block (priv->source, priv->source_changed_handler);
607 608
  value = gtk_entry_get_text (GTK_ENTRY (entry));
  if (value)
609 610
    gtk_printer_option_set (priv->source, value);
  g_signal_handler_unblock (priv->source, priv->source_changed_handler);
611 612 613 614
  emit_changed (widget);
}


615 616 617 618 619
static void
radio_changed_cb (GtkWidget              *button,
		  GtkPrinterOptionWidget *widget)
{
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
Matthias Clasen's avatar
Matthias Clasen committed
620
  gchar *value;
621 622 623 624 625 626 627 628 629 630
  
  g_signal_handler_block (priv->source, priv->source_changed_handler);
  value = g_object_get_data (G_OBJECT (button), "value");
  if (value)
    gtk_printer_option_set (priv->source, value);
  g_signal_handler_unblock (priv->source, priv->source_changed_handler);
  emit_changed (widget);
}

static void
Matthias Clasen's avatar
~  
Matthias Clasen committed
631 632
select_maybe (GtkWidget   *widget, 
	      const gchar *value)
633
{
Matthias Clasen's avatar
Matthias Clasen committed
634
  gchar *v = g_object_get_data (G_OBJECT (widget), "value");
635
      
Matthias Clasen's avatar
~  
Matthias Clasen committed
636 637 638
  if (strcmp (value, v) == 0)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
}
639

Matthias Clasen's avatar
~  
Matthias Clasen committed
640 641 642 643 644 645
static void
alternative_set (GtkWidget   *box,
		 const gchar *value)
{
  gtk_container_foreach (GTK_CONTAINER (box), 
			 (GtkCallback) select_maybe,
646
			 (gpointer) value);
647 648 649
}

static GSList *
Matthias Clasen's avatar
Matthias Clasen committed
650 651 652
alternative_append (GtkWidget              *box,
		    const gchar            *label,
                    const gchar            *value,
653
		    GtkPrinterOptionWidget *widget,
Matthias Clasen's avatar
Matthias Clasen committed
654
		    GSList                 *group)
655 656 657 658 659 660 661 662 663 664 665 666 667 668
{
  GtkWidget *button;

  button = gtk_radio_button_new_with_label (group, label);
  gtk_widget_show (button);
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  g_object_set_data (G_OBJECT (button), "value", (gpointer)value);
  g_signal_connect (button, "toggled", 
		    G_CALLBACK (radio_changed_cb), widget);

  return gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
}

669 670 671
static void
construct_widgets (GtkPrinterOptionWidget *widget)
{
672
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
673 674 675
  GtkPrinterOption *source;
  char *text;
  int i;
676
  GSList *group;
677

678
  source = priv->source;
679 680 681 682 683
  
  deconstruct_widgets (widget);
  
  if (source == NULL)
    {
684 685 686 687 688 689
      priv->combo = combo_box_new ();
      combo_box_append (priv->combo,_("Not available"), "None");
      gtk_combo_box_set_active (GTK_COMBO_BOX (priv->combo), 0);
      gtk_widget_set_sensitive (priv->combo, FALSE);
      gtk_widget_show (priv->combo);
      gtk_box_pack_start (GTK_BOX (widget), priv->combo, TRUE, TRUE, 0);
690 691 692 693
    }
  else switch (source->type)
    {
    case GTK_PRINTER_OPTION_TYPE_BOOLEAN:
694 695 696 697
      priv->check = gtk_check_button_new_with_mnemonic (source->display_text);
      g_signal_connect (priv->check, "toggled", G_CALLBACK (check_toggled_cb), widget);
      gtk_widget_show (priv->check);
      gtk_box_pack_start (GTK_BOX (widget), priv->check, TRUE, TRUE, 0);
698 699
      break;
    case GTK_PRINTER_OPTION_TYPE_PICKONE:
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD:
    case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE:
    case GTK_PRINTER_OPTION_TYPE_PICKONE_REAL:
    case GTK_PRINTER_OPTION_TYPE_PICKONE_INT:
    case GTK_PRINTER_OPTION_TYPE_PICKONE_STRING:
      if (source->type == GTK_PRINTER_OPTION_TYPE_PICKONE)
        {
          priv->combo = combo_box_new ();
	}
      else
        {
          priv->combo = combo_box_entry_new ();

          if (source->type == GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD ||
	      source->type == GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE)
	    {
              GtkEntry *entry;

	      entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->combo)));

              gtk_entry_set_visibility (entry, FALSE); 
	    }
        }
       

725
      for (i = 0; i < source->num_choices; i++)
726
	  combo_box_append (priv->combo,
727 728
			    source->choices_display[i],
			    source->choices[i]);
729 730 731
      gtk_widget_show (priv->combo);
      gtk_box_pack_start (GTK_BOX (widget), priv->combo, TRUE, TRUE, 0);
      g_signal_connect (priv->combo, "changed", G_CALLBACK (combo_changed_cb), widget);
732

733
      text = g_strdup_printf ("%s:", source->display_text);
734
      priv->label = gtk_label_new_with_mnemonic (text);
735
      g_free (text);
736
      gtk_widget_show (priv->label);
737
      break;
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

    case GTK_PRINTER_OPTION_TYPE_ALTERNATIVE:
      group = NULL;
      priv->box = gtk_hbox_new (FALSE, 12);
      gtk_widget_show (priv->box);
      gtk_box_pack_start (GTK_BOX (widget), priv->box, TRUE, TRUE, 0);
      for (i = 0; i < source->num_choices; i++)
	group = alternative_append (priv->box,
				    source->choices_display[i],
				    source->choices[i],
				    widget,
				    group);

      if (source->display_text)
	{
	  text = g_strdup_printf ("%s:", source->display_text);
	  priv->label = gtk_label_new_with_mnemonic (text);
	  g_free (text);
	  gtk_widget_show (priv->label);
	}
      break;

760
    case GTK_PRINTER_OPTION_TYPE_STRING:
761 762 763 764
      priv->entry = gtk_entry_new ();
      gtk_widget_show (priv->entry);
      gtk_box_pack_start (GTK_BOX (widget), priv->entry, TRUE, TRUE, 0);
      g_signal_connect (priv->entry, "changed", G_CALLBACK (entry_changed_cb), widget);
765

766
      text = g_strdup_printf ("%s:", source->display_text);
767
      priv->label = gtk_label_new_with_mnemonic (text);
768
      g_free (text);
769
      gtk_widget_show (priv->label);
770 771 772 773 774 775 776

      break;

    case GTK_PRINTER_OPTION_TYPE_FILESAVE:
      {
        GtkWidget *label;
        
777 778 779
        priv->filechooser = gtk_table_new (2, 2, FALSE);
        gtk_table_set_row_spacings (GTK_TABLE (priv->filechooser), 6);
        gtk_table_set_col_spacings (GTK_TABLE (priv->filechooser), 12);
780 781

        /* TODO: make this a gtkfilechooserentry once we move to GTK */
782
        priv->entry = gtk_entry_new ();
783 784
        priv->combo = gtk_file_chooser_button_new (source->display_text,
                                                   GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
785

786 787
        label = gtk_label_new_with_mnemonic (_("_Name:"));
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
788
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->entry);
789

790
        gtk_table_attach (GTK_TABLE (priv->filechooser), label,
791 792 793
                          0, 1, 0, 1, GTK_FILL, 0,
                          0, 0);

794
        gtk_table_attach (GTK_TABLE (priv->filechooser), priv->entry,
795 796 797
                          1, 2, 0, 1, GTK_FILL, 0,
                          0, 0);

798 799
        label = gtk_label_new_with_mnemonic (_("_Save in folder:"));
        gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
800
        gtk_label_set_mnemonic_widget (GTK_LABEL (label), priv->combo);
801

802
        gtk_table_attach (GTK_TABLE (priv->filechooser), label,
803 804 805
                          0, 1, 1, 2, GTK_FILL, 0,
                          0, 0);

806
        gtk_table_attach (GTK_TABLE (priv->filechooser), priv->combo,
807 808 809
                          1, 2, 1, 2, GTK_FILL, 0,
                          0, 0);

810 811
        gtk_widget_show_all (priv->filechooser);
        gtk_box_pack_start (GTK_BOX (widget), priv->filechooser, TRUE, TRUE, 0);
812

813
        g_signal_connect (priv->entry, "changed", G_CALLBACK (filesave_changed_cb), widget);
814

815
        g_signal_connect (priv->combo, "selection-changed", G_CALLBACK (filesave_changed_cb), widget);
816 817 818 819 820 821
      }
      break;
    default:
      break;
    }

822 823
  priv->image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU);
  gtk_box_pack_start (GTK_BOX (widget), priv->image, FALSE, FALSE, 0);
824 825 826 827 828
}

static void
update_widgets (GtkPrinterOptionWidget *widget)
{
829
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;
830 831
  GtkPrinterOption *source;

832
  source = priv->source;
833 834 835
  
  if (source == NULL)
    {
836
      gtk_widget_hide (priv->image);
837 838 839 840 841 842 843
      return;
    }

  switch (source->type)
    {
    case GTK_PRINTER_OPTION_TYPE_BOOLEAN:
      if (strcmp (source->value, "True") == 0)
844
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->check), TRUE);
845
      else
846
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->check), FALSE);
847 848
      break;
    case GTK_PRINTER_OPTION_TYPE_PICKONE:
849
      combo_box_set (priv->combo, source->value);
850
      break;
851 852 853
    case GTK_PRINTER_OPTION_TYPE_ALTERNATIVE:
      alternative_set (priv->box, source->value);
      break;
854
    case GTK_PRINTER_OPTION_TYPE_STRING:
855
      gtk_entry_set_text (GTK_ENTRY (priv->entry), source->value);
856 857 858
      break;
    case GTK_PRINTER_OPTION_TYPE_FILESAVE:
      {
859 860 861 862 863 864 865 866 867 868
        gchar *filename = g_filename_from_uri (source->value, NULL, NULL);
        if (filename != NULL)
          {
            gchar *basename, *dirname, *text;

            basename = g_path_get_basename (filename);
            dirname = g_path_get_dirname (filename);
            text = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);

            if (text != NULL)
869
              gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
870 871 872 873 874 875 876 877 878 879
            if (g_path_is_absolute (dirname))
              gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (priv->combo),
                                                   dirname);
            g_free (text);
            g_free (basename);
            g_free (dirname);
            g_free (filename);
          }
	else
	  gtk_entry_set_text (GTK_ENTRY (priv->entry), source->value);
880 881 882 883 884 885 886
	break;
      }
    default:
      break;
    }

  if (source->has_conflict)
887
    gtk_widget_show (priv->image);
888
  else
889
    gtk_widget_hide (priv->image);
890 891 892
}

gboolean
Matthias Clasen's avatar
Matthias Clasen committed
893
gtk_printer_option_widget_has_external_label (GtkPrinterOptionWidget *widget)
894 895 896 897 898 899 900 901 902 903
{
  return widget->priv->label != NULL;
}

GtkWidget *
gtk_printer_option_widget_get_external_label (GtkPrinterOptionWidget  *widget)
{
  return widget->priv->label;
}

Matthias Clasen's avatar
Matthias Clasen committed
904 905
const gchar *
gtk_printer_option_widget_get_value (GtkPrinterOptionWidget *widget)
906
{
907 908 909 910
  GtkPrinterOptionWidgetPrivate *priv = widget->priv;

  if (priv->source)
    return priv->source->value;
911 912 913 914 915 916
  
  return "";
}

#define __GTK_PRINTER_OPTION_WIDGET_C__
#include "gtkaliasdef.c"