File: matrixview.c

package info (click to toggle)
nip2 8.9.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 23,404 kB
  • sloc: ansic: 64,076; sh: 4,681; yacc: 1,133; makefile: 927; lex: 386; xml: 40; perl: 17
file content (963 lines) | stat: -rw-r--r-- 25,143 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
273
274
275
276
277
278
279
280
281
282
283
284
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
327
328
329
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
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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
547
548
549
550
551
552
553
554
555
556
557
558
559
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/* run the display for an input matrixview in a workspace 
 */

/*

    Copyright (C) 1991-2003 The National Gallery

    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 along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define DEBUG
 */

#include "ip.h"

/* Round N down to P boundary.
 */
#define ROUND_DOWN(N,P) ((N) - ((N) % P))

/* Round N up to P boundary.
 */
#define ROUND_UP(N,P) (ROUND_DOWN( (N) + (P) - 1, (P) ))

/* The size in cells at which we switch from displaying the whole matrix to
 * displaying part of it in a scrolled window.
 */
static const int matrixview_max_width = 7;
static const int matrixview_max_height = 10;

/* Show a matrix with fixed-width columns.
 */
static const int matrixview_column_width = 70;

/* Limit number of sub-widgets with this ... could be prefs?
 */
static const int matrixview_max_cells = 100;

static GraphicviewClass *parent_class = NULL;

static void
matrixview_destroy( GtkObject *object )
{
    	Matrixview *matrixview;

    	g_return_if_fail( object != NULL );
    	g_return_if_fail( IS_MATRIXVIEW( object ) );

#ifdef DEBUG
    	printf( "matrixview_destroy\n" );
#endif /*DEBUG*/

    	matrixview = MATRIXVIEW( object );

    	/* My instance destroy stuff.
    	 */
    	IM_FREEF( g_slist_free, matrixview->items );

    	GTK_OBJECT_CLASS( parent_class )->destroy( object );
}

static gboolean
matrixview_scan_text( Matrixview *matrixview, GtkWidget *txt, 
    	double *out, gboolean *changed )
{
    	double v;

    	if( !get_geditable_double( txt, &v ) ) 
    		return( FALSE );

    	if( *out != v ) {
    		*out = v;
    		*changed = TRUE;
    	}

    	return( TRUE );
}

/* Search and read all text widgets and refill matrix. set_dirty this symbol
 * if there was a change. Return non-NULL if we found an error.
 */
static void *
matrixview_scan( View *view )
{
    	Matrixview *matrixview = MATRIXVIEW( view );
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );
	int width = matrix->value.width;
	int height = matrix->value.height;
    	Expr *expr = HEAPMODEL( matrix )->row->expr;

    	gboolean changed;
    	int x, y;
    	GSList *p;

#ifdef DEBUG
    	printf( "matrixview_scan\n" );
#endif /*DEBUG*/

    	/* Should be text widgets there ... either text or tslider.
    	 */
    	if( matrixview->display != MATRIX_DISPLAY_TEXT && 
    		matrixview->display != MATRIX_DISPLAY_TEXT_SCALE_OFFSET && 
    		matrixview->display != MATRIX_DISPLAY_SLIDER )
    		return( NULL );

    	expr_error_clear( expr );
    	changed = FALSE;

    	/* Check for scale and offset, if present.
    	 */
    	if( matrixview->scale && 
		!matrixview_scan_text( matrixview,
			matrixview->scale, &matrix->scale, &changed ) ) {
		expr_error_set( expr );
    		return( view );
	}
    	if( matrixview->offset && 
		!matrixview_scan_text( matrixview, 
			matrixview->offset, &matrix->offset, &changed ) ) {
		expr_error_set( expr );
    		return( view );
    	}

    	/* Loop thru' all matrix widgets. tsliders have text fields we must
	 * scan too.
    	 */
    	if( matrixview->items ) 
    		for( p = matrixview->items, y = 0; y < height; y++ )
    			for( x = 0; x < width; x++, p = p->next ) {
    				GtkWidget *item = GTK_WIDGET( p->data );
    				GtkWidget *entry = TSLIDER( item )->entry;
				int i = x + y * width;

    				if( !matrixview_scan_text( matrixview, entry,
    					&matrix->value.coeff[i], &changed ) ) {
					error_top( _( "Bad value." ) );
					error_sub( _( "Cell (%d, %d):\n%s" ), 
    						x, y, error_get_sub() );
					expr_error_set( expr );

    					return( view );
    				}
    			}

	if( matrixview->store ) {
		GtkTreeModel *tree = GTK_TREE_MODEL( matrixview->store );

		GtkTreeIter iter;

		gtk_tree_model_get_iter_first( tree, &iter );

		for( y = 0; y < height; y++ ) {
			for( x = 0; x < width; x++ ) {
				double *out = 
					&matrix->value.coeff[x + y * width];

				double d;

				gtk_tree_model_get( tree, &iter, x, &d, -1 );

				if( *out != d ) {
					*out = d;
					changed = TRUE;
				}
			}

			gtk_tree_model_iter_next( tree, &iter );
		}
	}

    	if( changed ) 
    		classmodel_update( CLASSMODEL( matrix ) ) ;

    	return( VIEW_CLASS( parent_class )->scan( view ) );
}

/* Change to a toggle widget. 
 */
/*ARGSUSED*/
static void
matrixview_toggle_change_cb( GtkWidget *widget, Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );
    	int pos = g_slist_index( matrixview->items, widget );
	int x = pos % matrixview->width;
	int y = pos / matrixview->width;
	int i = x + y * matrix->value.width;

#ifdef DEBUG
    	printf( "matrixview_toggle_change_cb\n" );
#endif /*DEBUG*/

    	/* Cycle value.
    	 */
    	switch( (int) matrix->value.coeff[i] ) {
    	case 0:
    		matrix->value.coeff[i] = 128.0;
    		break;

    	case 255:
    		matrix->value.coeff[i] = 0.0;
    		break;

    	default:
    		matrix->value.coeff[i] = 255.0;
    		break;
    	}

    	classmodel_update( CLASSMODEL( matrix ) );
    	symbol_recalculate_all();
}

/* Build a set of toggle items for a matrix. 
 */
static void
matrixview_toggle_build( Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );

    	int x, y;
    	int cx, cy;

    	matrixview->table = gtk_table_new( 
		matrixview->height, matrixview->width, TRUE );
    	gtk_box_pack_start( GTK_BOX( matrixview->box ), 
    		matrixview->table, FALSE, FALSE, 0 );

    	/* Find the centre position, if there is one. We give this a special
    	 * name; it is highlit by our resource file.
    	 */
    	cx = -1; cy = -1;
    	if( matrix->value.height & 0x1 )
    		cy = matrix->value.height >> 1;
    	if( matrix->value.width & 0x1 )
    		cx = matrix->value.width >> 1;

    	/* Build contents.
    	 */
    	for( y = 0; y < matrixview->height; y++ )
    		for( x = 0; x < matrixview->width; x++ ) {
    			GtkWidget *but;

    			but = gtk_button_new_with_label( "0" );
    			gtk_signal_connect( GTK_OBJECT( but ), "clicked", 
    				GTK_SIGNAL_FUNC( matrixview_toggle_change_cb ),
    				matrixview );
    			if( x == cx && y == cy )
    				gtk_widget_set_name( but, "centre_widget" );
			/*

				FIXME ... this b0rks thanks to pangolayout
				confusion

    			set_fixed( GTK_BIN( but )->child, 1 );
			 */

    			gtk_table_attach( GTK_TABLE( matrixview->table ), but,
    				x, x + 1, y, y + 1, GTK_FILL, GTK_FILL, 2, 2 );
    			matrixview->items = 
    				g_slist_append( matrixview->items, but );
    		}
}

/* Change to a scale in a Tslider. 
 */
/*ARGSUSED*/
static void
matrixview_slider_change_cb( Tslider *tslider, Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );
    	int pos = g_slist_index( matrixview->items, tslider );
	int x = pos % matrixview->width;
	int y = pos / matrixview->width;
	int i = x + y * matrix->value.width;

    	g_assert( pos >= 0 );

    	/* Install value.
    	 */
    	if( matrix->value.coeff[i] != tslider->svalue ) {
    		matrix->value.coeff[i] = tslider->svalue;

    		classmodel_update( CLASSMODEL( matrix ) );
    		symbol_recalculate_all();
    	}
}

/* Build a set of slider items for a matrix. 
 */
static void
matrixview_slider_build( Matrixview *matrixview )
{
    	int x, y;

    	matrixview->table = gtk_table_new( matrixview->height, 
		matrixview->width, TRUE );
    	gtk_box_pack_start( GTK_BOX( matrixview->box ), 
    		matrixview->table, TRUE, TRUE, 0 );

    	for( y = 0; y < matrixview->height; y++ )
    		for( x = 0; x < matrixview->width; x++ ) {
    			Tslider *tslider = tslider_new();

    			tslider_set_conversions( tslider, NULL, NULL );
    			tslider->from = -2;
    			tslider->to = 2;
    			tslider->digits = 3;

    			gtk_signal_connect_object( GTK_OBJECT( tslider ), 
    				"text_changed",
    				GTK_SIGNAL_FUNC( view_changed_cb ), 
    				GTK_OBJECT( matrixview ) );
    			gtk_signal_connect_object( GTK_OBJECT( tslider ), 
    				"activate", 
    				GTK_SIGNAL_FUNC( view_activate_cb ), 
    				GTK_OBJECT( matrixview ) );
    			gtk_signal_connect( GTK_OBJECT( tslider ), 
    				"slider_changed", 
    				GTK_SIGNAL_FUNC( matrixview_slider_change_cb ),
    				matrixview );

    			gtk_container_set_border_width( 
    				GTK_CONTAINER( tslider ), 2 );
    			gtk_table_attach_defaults( 
    				GTK_TABLE( matrixview->table ), 
    				GTK_WIDGET( tslider ),
    				x, x + 1, y, y + 1 );
    			matrixview->items = g_slist_append( matrixview->items, 
    				tslider );
    		}
}

static gboolean
matrixview_text_focus_in( GtkWidget *entry, GdkEvent *event, void *data )
{
    	gtk_editable_select_region( GTK_EDITABLE( entry ), 0, -1 );

	return( FALSE );
}

static gboolean
matrixview_text_focus_out( GtkWidget *entry, GdkEvent *event, void *data )
{
    	gtk_editable_select_region( GTK_EDITABLE( entry ), 0, 0 );

	return( FALSE );
}

static void
matrixview_text_connect( Matrixview *matrixview, GtkWidget *txt )
{
    	gtk_signal_connect_object( GTK_OBJECT( txt ), "changed",
    		GTK_SIGNAL_FUNC( view_changed_cb ), 
    		GTK_OBJECT( matrixview ) );
    	gtk_signal_connect_object( GTK_OBJECT( txt ), "activate",
    		GTK_SIGNAL_FUNC( view_activate_cb ), 
    		GTK_OBJECT( matrixview ) );

    	/* Select text on focus-in, deselect on focus out.
    	 */
    	gtk_signal_connect( GTK_OBJECT( txt ), "focus_in_event",
    		GTK_SIGNAL_FUNC( matrixview_text_focus_in ), NULL );
    	gtk_signal_connect( GTK_OBJECT( txt ), "focus_out_event",
    		GTK_SIGNAL_FUNC( matrixview_text_focus_out ), NULL );
}

static void
matrixview_text_build_scale_offset( Matrixview *matrixview )
{
	GtkSizeGroup *group;

    	matrixview->cbox = gtk_vbox_new( FALSE, 2 );
        gtk_box_pack_end( GTK_BOX( matrixview->box ), 
    		GTK_WIDGET( matrixview->cbox ), FALSE, FALSE, 0 );

	group = gtk_size_group_new( GTK_SIZE_GROUP_HORIZONTAL );

	matrixview->scale = 
		build_glabeltext4( matrixview->cbox, group, _( "Scale" ) );
	gtk_entry_set_width_chars( GTK_ENTRY( matrixview->scale ), 6 );
    	matrixview_text_connect( matrixview, matrixview->scale );

	matrixview->offset = 
		build_glabeltext4( matrixview->cbox, group, _( "Offset" ) );
	gtk_entry_set_width_chars( GTK_ENTRY( matrixview->offset ), 6 );
    	matrixview_text_connect( matrixview, matrixview->offset );

	UNREF( group );
}

/* Make a GtkListStore from a MatrixValue.
 */
GtkListStore *
matrixview_liststore_new( MatrixValue *matrixvalue )
{
	int width = matrixvalue->width;
	int height = matrixvalue->height;

	GType *types;
	int i, y;
	GtkListStore *store;

	types = g_new( GType, width );
	for( i = 0; i < width; i++ )
		types[i] = G_TYPE_DOUBLE;
	store = gtk_list_store_newv( width, types );
	g_free( types );

	for( y = 0; y < height; y++ ) {
		GtkTreeIter iter;

		gtk_list_store_append( store, &iter );

		for( i = 0; i < width; i++ ) 
			gtk_list_store_set( store, &iter, 
				i, matrixvalue->coeff[y * width + i], -1 );
	}

	return( store );
}

static void
matrixview_edited_cb( GtkCellRendererText *renderer, 
	char *path, char *new_text, void *user_data )
{
	Matrixview *matrixview = MATRIXVIEW( user_data );
	GtkTreeModel *tree = GTK_TREE_MODEL( matrixview->store );
	GtkTreeIter iter;

	if( gtk_tree_model_get_iter_from_string( tree, &iter, path ) ) {
		int col = GPOINTER_TO_INT( g_object_get_data( 
			G_OBJECT( renderer ), "nip2_column_num" ) );

		gtk_list_store_set( GTK_LIST_STORE( tree ), &iter, 
			col, atof( new_text ),
			-1 ); 

		view_scannable_register( VIEW( matrixview ) );
		symbol_recalculate_all();
	}
}

static void
matrixview_cell_data_cb( GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
	GtkTreeModel *tree, GtkTreeIter *iter, void *data )
{
	int col = GPOINTER_TO_INT( g_object_get_data( 
		G_OBJECT( cell ), "nip2_column_num" ) );
	double d;
	char buf[256];

	gtk_tree_model_get( tree, iter, col, &d, -1 );
	vips_snprintf( buf, 256, "%g", d ); 
	g_object_set( cell, "text", buf, NULL );
}

/* Build a set of text items for a matrix. 
 */
static void
matrixview_text_build( Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );

	int i;
	GtkTreeViewColumn *column;
    	int cell_height;
	GtkTreeSelection *selection;

	if( !matrix->value.coeff )
		return;

	matrixview->store = matrixview_liststore_new( &matrix->value );
	matrixview->sheet = gtk_tree_view_new_with_model( 
		GTK_TREE_MODEL( matrixview->store ) );
	gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( matrixview->sheet ),
		FALSE );

	/* Stops a harmless compiler warning.
	 */
	column = NULL;

	for( i = 0; i < matrix->value.width; i++ ) {
		GtkCellRenderer *renderer;
		char buf[256];

		renderer = gtk_cell_renderer_text_new();
		g_object_set( renderer, "editable", TRUE, NULL );
		g_object_set_data( G_OBJECT( renderer ), 
			"nip2_column_num", GINT_TO_POINTER( i ) );
		g_signal_connect( G_OBJECT( renderer ), "edited",
			G_CALLBACK( matrixview_edited_cb ), matrixview );

		column = gtk_tree_view_column_new();
		gtk_tree_view_column_set_sizing( column, 
			GTK_TREE_VIEW_COLUMN_FIXED );
		gtk_tree_view_column_set_fixed_width( column, 
			matrixview_column_width );
		im_snprintf( buf, 256, "%d", i );
		gtk_tree_view_column_set_title( column, buf );
		gtk_tree_view_column_pack_start( column, renderer, FALSE );
		gtk_tree_view_column_set_attributes( column, renderer, 
			"text", i, 
			NULL );
		gtk_tree_view_column_set_cell_data_func( column, renderer, 
			matrixview_cell_data_cb, NULL, NULL ); 
		gtk_tree_view_append_column( GTK_TREE_VIEW( matrixview->sheet ),
			column );
	}

	gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW( matrixview->sheet ),
		TRUE );
	gtk_tree_view_column_cell_get_size( column,
		NULL, NULL, NULL, NULL, &cell_height );

	selection = gtk_tree_view_get_selection( 
		GTK_TREE_VIEW( matrixview->sheet ) );
	gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
	gtk_tree_view_set_rubber_banding( GTK_TREE_VIEW( matrixview->sheet ), 
		TRUE );

	gtk_tree_view_set_grid_lines( GTK_TREE_VIEW( matrixview->sheet ), 
		GTK_TREE_VIEW_GRID_LINES_BOTH );

	if( matrix->value.width > matrixview_max_width || 
		matrix->value.height > matrixview_max_height ) {
		GtkRequisition requisition;
		gint spacing;
		int border;
		int width, height;

		if( matrix->value.width > matrixview_max_width )
			gtk_tree_view_set_headers_visible( 
				GTK_TREE_VIEW( matrixview->sheet ),
				TRUE );

		matrixview->swin = gtk_scrolled_window_new( NULL, NULL );
		gtk_scrolled_window_set_policy( 
			GTK_SCROLLED_WINDOW( matrixview->swin ),
			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
		gtk_container_add( GTK_CONTAINER( matrixview->swin ), 
			matrixview->sheet );

		/* Calculate how big we should make the scrolled window. We
		 * need to leave space for the scrollbars.
		 */
		gtk_widget_size_request( 
			gtk_scrolled_window_get_hscrollbar( 
				GTK_SCROLLED_WINDOW( matrixview->swin ) ),
			&requisition );
		gtk_widget_style_get( GTK_WIDGET( matrixview->swin ),
			"scrollbar-spacing", &spacing,
			NULL );
		border = requisition.height + spacing;

		/* Subarea of matrix we show, in cells.
		 */
		width = IM_MIN( matrix->value.width, matrixview_max_width );
		height = IM_MIN( matrix->value.height, matrixview_max_height );

		/* Convert to pixels.
		 */
		width *= matrixview_column_width;
		height *= cell_height;

		/* Will we be showing scrollbars? Need to add a bit.
		 */
		if( matrixview->width > matrixview_max_width )
			height += border;
		if( matrixview->height > matrixview_max_height )
			width += border;

		gtk_widget_set_size_request( GTK_WIDGET( matrixview->swin ), 
			width + 5, height + 5 );

		gtk_box_pack_start( GTK_BOX( matrixview->box ), 
			matrixview->swin, FALSE, FALSE, 0 );
	}
	else {
		gtk_box_pack_start( GTK_BOX( matrixview->box ), 
			matrixview->sheet, FALSE, FALSE, 0 );
	}

    	if( matrixview->display == MATRIX_DISPLAY_TEXT_SCALE_OFFSET )
    		/* Make the scale/offset widgets too.
    		 */
    		matrixview_text_build_scale_offset( matrixview );
}

/* Set the label on a toggle button to reflect its value.
 */
static void
matrixview_toggle_set_label( GtkWidget *button, double v )
{
    	GtkWidget *label = GTK_BIN( button )->child;

    	g_return_if_fail( GTK_IS_LABEL( label ) );

    	switch( (int) v ) {
    	case 0:
    		set_glabel( label, "0" );
    		break;

    	case 255:
    		set_glabel( label, "1" );
    		break;

    	default:
    		set_glabel( label, "*" );
    		break;
    	}
}

/* Refresh a set of toggle items for a matrix. 
 */
static void
matrixview_toggle_refresh( Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );

    	int x, y;
    	GSList *p;

    	for( p = matrixview->items, y = 0; y < matrixview->height; y++ )
    		for( x = 0; x < matrixview->width; x++, p = p->next ) {
    			GtkWidget *wid = GTK_WIDGET( p->data );
    			int i = x + y * matrix->value.width;

    			matrixview_toggle_set_label( wid, 
				matrix->value.coeff[i] );
    		}
}

/* Refresh a set of slider items for a matrix. 
 */
static void
matrixview_slider_refresh( Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );

    	int x, y;
    	GSList *p;

    	for( p = matrixview->items, y = 0; y < matrixview->height; y++ )
    		for( x = 0; x < matrixview->width; x++, p = p->next ) {
    			Tslider *tslider = TSLIDER( p->data );
    			int i = x + y * matrix->value.width;

    			tslider->value = matrix->value.coeff[i];
    			tslider->svalue = matrix->value.coeff[i];

    			tslider_changed( tslider );
    		}
}

static void
matrixview_text_set( Matrixview *matrixview, GtkWidget *txt, double val )
{
    	if( txt ) {
    		gtk_signal_handler_block_by_data( 
    			GTK_OBJECT( txt ), matrixview );
    		set_gentry( txt, "%g", val ); 
    		gtk_signal_handler_unblock_by_data( 
    			GTK_OBJECT( txt ), matrixview );
    	}
}

/* Fill the widgets!
 */
static void
matrixview_text_refresh( Matrixview *matrixview )
{
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );
	MatrixValue *matrixvalue = &matrix->value;
	int width = matrixvalue->width;
	int height = matrixvalue->height;
	GtkTreeModel *tree = GTK_TREE_MODEL( matrixview->store );

	int x, y;
	GtkTreeIter iter;

	if( !matrixvalue->coeff )
		return;

    	matrixview_text_set( matrixview, matrixview->scale, matrix->scale );
    	matrixview_text_set( matrixview, matrixview->offset, matrix->offset );

	gtk_tree_model_get_iter_first( tree, &iter );

	for( y = 0; y < height; y++ ) {
		for( x = 0; x < width; x++ ) 
			gtk_list_store_set( matrixview->store, &iter, 
				x, matrixvalue->coeff[x + y * width], 
				-1 );

		gtk_tree_model_iter_next( tree, &iter );
	}
}

static void
matrixview_refresh( vObject *vobject )
{
    	Matrixview *matrixview = MATRIXVIEW( vobject );
    	Matrix *matrix = MATRIX( VOBJECT( matrixview )->iobject );

    	gboolean built;
    	gboolean hclip;
    	gboolean vclip;
	int width, height;
	int i;

    	built = FALSE;
    	hclip = FALSE;
    	vclip = FALSE;

	/* Find required size ... limit displays which are tables of widgets 
	 * to avoid huge slowness.
	 */
	width = matrix->value.width;
	height = matrix->value.height;

	if( matrix->display == MATRIX_DISPLAY_TOGGLE ||
		matrix->display == MATRIX_DISPLAY_SLIDER ) {
		if( width * height > matrixview_max_cells ) {
			if( width > height ) {
				width = IM_CLIP( 1, 
					matrixview_max_cells / height, 
					matrix->value.width );
				hclip = TRUE;
			}
			else {
				height = IM_CLIP( 1, 
					matrixview_max_cells / width, 
					matrix->value.height );
				vclip = TRUE;
			}
		}

		/* Clip twice to make sure we clip in both directions if 
		 * necessary.
		 */
		if( width * height > matrixview_max_cells ) {
			if( width > height ) {
				width = IM_CLIP( 1, 
					matrixview_max_cells / height, 
					matrix->value.width );
				hclip = TRUE;
			}
			else {
				height = IM_CLIP( 1, 
					matrixview_max_cells / width, 
					matrix->value.height );
				vclip = TRUE;
			}
		}
	}

#ifdef DEBUG
    	printf( "matrixview_refresh\n" );
#endif /*DEBUG*/

    	/* Is there a UI already there we can reuse? Has to be same size and
    	 * type.
    	 */
    	if( matrixview->display != matrix->display || 
    		matrixview->width != width || 
    		matrixview->height != height ) {
    		/* Kill old UI stuff.
    		 */
    		IM_FREEF( gtk_widget_destroy, matrixview->sheet );
    		IM_FREEF( gtk_widget_destroy, matrixview->table );
    		IM_FREEF( gtk_widget_destroy, matrixview->swin );
    		IM_FREEF( g_slist_free, matrixview->items );
    		IM_FREEF( gtk_widget_destroy, matrixview->cbox );
    		matrixview->scale = NULL;
    		matrixview->offset = NULL;

		/* So the builders know how many widgets to make.
		 */
    		matrixview->width = width;
    		matrixview->height = height;
    		matrixview->display = matrix->display;

    		/* Make new contents. 
    		 */
		switch( matrix->display ) {
		case MATRIX_DISPLAY_TOGGLE:
			matrixview_toggle_build( matrixview );
			break;

		case MATRIX_DISPLAY_SLIDER:
			matrixview_slider_build( matrixview );
			break;

		case MATRIX_DISPLAY_TEXT:
		case MATRIX_DISPLAY_TEXT_SCALE_OFFSET:
			matrixview_text_build( matrixview );
			break;

		default:
			g_assert( FALSE );
		}

		if( hclip ) {
			gtk_table_resize( GTK_TABLE( matrixview->table ), 
				matrixview->height, matrixview->width + 1 );

			for( i = 0; i < matrixview->height; i++ ) {
				GtkWidget *lab;

				lab = gtk_label_new( "---" );
				gtk_table_attach( 
					GTK_TABLE( matrixview->table ), lab,
					matrixview->width, 
					matrixview->width + 1, 
					i, i + 1, 
					GTK_FILL, GTK_FILL, 2, 2 );
			}
		}

		if( vclip ) {
			gtk_table_resize( GTK_TABLE( matrixview->table ), 
				matrixview->height + 1, matrixview->width );

			for( i = 0; i < matrixview->width; i++ ) {
				GtkWidget *lab;

				lab = gtk_label_new( "|" );
				gtk_table_attach( 
					GTK_TABLE( matrixview->table ), lab,
					i, i + 1, 
					matrixview->height, 
					matrixview->height + 1, 
					GTK_FILL, GTK_FILL, 2, 2 );
			}
		}

    		built = TRUE;
    	}

    	switch( matrixview->display ) {
    	case MATRIX_DISPLAY_TOGGLE:
    		matrixview_toggle_refresh( matrixview );
    		break;

    	case MATRIX_DISPLAY_SLIDER:
    		matrixview_slider_refresh( matrixview );
    		break;

    	case MATRIX_DISPLAY_TEXT:
    	case MATRIX_DISPLAY_TEXT_SCALE_OFFSET:
    		matrixview_text_refresh( matrixview );
    		break;

    	default:
    		g_assert( FALSE );
    	}

    	/* If we've built a new display, need to show after _refresh.
    	 */
    	if( built ) {
    		gtk_widget_show_all( GTK_WIDGET( matrixview ) );
    		view_resize( VIEW( matrixview ) );
    	}

    	VOBJECT_CLASS( parent_class )->refresh( vobject );
}

static void
matrixview_class_init( MatrixviewClass *class )
{
    	GtkObjectClass *object_class = (GtkObjectClass *) class;
    	vObjectClass *vobject_class = (vObjectClass *) class;
    	ViewClass *view_class = (ViewClass *) class;

	parent_class = g_type_class_peek_parent( class );

    	object_class->destroy = matrixview_destroy;

    	/* Create signals.
    	 */

    	/* Init methods.
    	 */
    	vobject_class->refresh = matrixview_refresh;

    	view_class->scan = matrixview_scan;
}

static void
matrixview_init( Matrixview *matrixview )
{
#ifdef DEBUG
    	printf( "matrixview_init\n" );
#endif /*DEBUG*/

    	matrixview->box = gtk_hbox_new( FALSE, 12 );
        gtk_box_pack_start( GTK_BOX( matrixview ), 
    		GTK_WIDGET( matrixview->box ), FALSE, FALSE, 0 );

    	/* Build on 1st refresh.
    	 */
    	matrixview->store = NULL;
    	matrixview->sheet = NULL;
    	matrixview->swin = NULL;
    	matrixview->table = NULL;
    	matrixview->items = NULL;
    	matrixview->width = -1;
    	matrixview->height = -1;
    	matrixview->cbox = NULL;
    	matrixview->scale = NULL;
    	matrixview->offset = NULL;
}

GtkType
matrixview_get_type( void )
{
    	static GtkType matrixview_type = 0;

    	if( !matrixview_type ) {
    		static const GtkTypeInfo info = {
    			"Matrixview",
    			sizeof( Matrixview ),
    			sizeof( MatrixviewClass ),
    			(GtkClassInitFunc) matrixview_class_init,
    			(GtkObjectInitFunc) matrixview_init,
    			/* reserved_1 */ NULL,
    			/* reserved_2 */ NULL,
    			(GtkClassInitFunc) NULL,
    		};

    		matrixview_type = gtk_type_unique( TYPE_GRAPHICVIEW, &info );
    	}

    	return( matrixview_type );
}

View *
matrixview_new( void )
{
    	Matrixview *matrixview = gtk_type_new( TYPE_MATRIXVIEW );

    	return( VIEW( matrixview ) );
}