summaryrefslogtreecommitdiffstats
path: root/src/guitools.cs
blob: bdc9e8af82d3d138324f2e7b0892f2622f827257 (plain)
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
//  GFAX - Gnome fax application
//  Copyright (C) 2003 - 2008 George A. Farris
//
// 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 3 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

namespace gfax {
	using System;
    using System.IO;
	using System.Collections;
    using System.Reflection;
    using GLib;
	using Gtk;
	using Gdk;
	using Gnome;
	using Glade;
	using GtkSharp;

	// G_ProgressBar
	public class G_ProgressBar
	{
		[Glade.Widget] Gtk.Dialog HylafaxProgressDialog;
		[Glade.Widget] ProgressBar HylafaxProgressbar;
		[Glade.Widget] Gtk.Notebook HylafaxProgressNotebook;
		[Glade.Widget] Gtk.Button HylafaxProgressCancelButton;
		[Glade.Widget] Gtk.Button HylafaxProgressCloseButton;
		
		bool close = true;
		bool cancel = false;	// Cancel all jobs
		
		public G_ProgressBar ()
		{
			Glade.XML xml = new Glade.XML (null, "gfax.glade","HylafaxProgressDialog",null);
			xml.Autoconnect (this);
			
			HylafaxProgressNotebook.CurrentPage = 0;
			HylafaxProgressCloseButton.Hide();
			HylafaxProgressDialog.Show();
		}
		
		public bool Cancel
		{
			get { return cancel; }
		}
		
		private void on_HylafaxProgressCancelButton_clicked(object o, EventArgs args)
		{
			cancel = true;
		}
		
		private void on_HylafaxProgressCloseButton_clicked(object o, EventArgs args)
		{
			close = false;
			HylafaxProgressDialog.Destroy();
		}
		
		public void Close()
		{
			//close = false;
			//HylafaxProgressDialog.Hide();
			//HylafaxProgressDialog.Dispose();
			HylafaxProgressDialog.Destroy();
		}
		
		//public void Run ()
		//{
		//	HylafaxProgressDialog.Run();
		//}
		
		public double Fraction
		{
			set { HylafaxProgressbar.Fraction = value; }
		}
		
		public void Finished()
		{
			HylafaxProgressNotebook.CurrentPage = 1;
			HylafaxProgressCancelButton.Hide();
			HylafaxProgressCloseButton.Show();
			// TODO Bad, bad, slap your wrist boy, do timer function 
			// or fix this somehow.  Can I say 100% processor useage.
			//while (close) {
			//	while (Gtk.Application.EventsPending ())
            //    	Gtk.Application.RunIteration ();
			//}
		}
	}

	// G_Message class
	//
	// A simple message dialog
	public class G_Message
	{
		public G_Message (string s)
		{	
			MessageDialog d = new MessageDialog (
				null, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, s);
			d.Run ();
        	d.Destroy ();
		}
		
		public G_Message (string s, bool toplevel)
		{	
			Application.Init();
			MessageDialog d = new MessageDialog (
				null, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, s);
			d.Run ();
        	d.Destroy ();
			Application.Quit();
		}
	}
	
	public class G_Password
	{
		[Glade.Widget] Gtk.Dialog PasswordDialog;
		[Glade.Widget] Gtk.Entry PasswordEntry;
		
		string pass;			// the password entered
		bool cancel = false;	// Cancel the connection
		bool remember = false;	// Remember the password
		
		public G_Password ()
		{
			Glade.XML xml = new Glade.XML (null, "gfax.glade","PasswordDialog",null);
			xml.Autoconnect (this);
			
			PasswordDialog.Show();
		}
		
		public bool Cancel
		{
			get { return cancel; }
		}
		
		public bool RememberPassword
		{
			get { return remember; }
		}
		
		public string Password
		{
			get { return pass; }
		}
		
		public void Run ()
		{
			PasswordDialog.Run();
		}
		
		private void on_PasswordCheckbutton_toggled(object o, EventArgs args)
		{
			remember = true;
		}
		
		private void on_cancelbutton_clicked(object o, EventArgs args)
		{
			cancel = true;
			PasswordDialog.Hide();
		}
		
		private void on_PasswordEntry_activate(object o, EventArgs args)
		{
			pass = PasswordEntry.Text;	
			PasswordDialog.Hide();
		}
		
		private void on_okbutton_clicked(object o, EventArgs args)
		{
			pass = PasswordEntry.Text;	
			PasswordDialog.Hide();
		}
		
		public void Close()
		{
			PasswordDialog.Destroy();
		}
	}


	// ***********************************************************************
	//
	// G_GetFilename class
	//
	//	Implements a GTK+ fileselector with signal handler for Ok and Cancel
	//	Arguments:
	//		string title	: Title open file selection dialog
	//		string dir		: Starting directory

	public class G_GetFilename
	{
		string fname;
		public Gtk.FileSelection fs;
		
		public G_GetFilename (string title, string dir)
		{
			fs = new FileSelection(title);
			fs.Filename = dir;
			fs.Modal = true;
			fs.HideFileopButtons ();
			fs.OkButton.Clicked += new EventHandler (ok);
			fs.CancelButton.Clicked += new EventHandler (cancel);
			
/*			
           check_button = new CheckButton ("Show Fileops");
           check_button.Toggled += new EventHandler (show_fileops);
			fs.ActionArea.PackStart (check_button, false, false, 0);
    
           toggle_button = new ToggleButton ("Select Multiple");
           toggle_button.Clicked += new EventHandler (select_multiple);
           fs.ActionArea.PackStart (toggle_button, false, false, 0);
*/	
			fs.ShowAll();
			fs.Run ();
		}
		
		public void Destroy ()
		{
			fs.Destroy();
		}
		
		private void cancel (object o, EventArgs args)
		{
			//Console.WriteLine("[Cancel] file selection cancel");
			fs.Hide ();
		}
		
		private void ok (object o, EventArgs args)
		{
			//Console.WriteLine(fs.Filename);
			fname = fs.Filename;
			fs.Hide ();
		}
	
		public string Fname
		{
			get {return fname;}
			set {fname = value;}
		}			
	
		
	}

	public class CellRendererDateTime: CellRendererText
	{
		public string Format;
	}

	public class G_ListView
	{	
		Gtk.ListStore store;
		Gtk.TreeView tree;
		Gtk.TreeViewColumn column;
		Gtk.CellRendererText textrenderer;
		CellRendererDateTime datetimerenderer;
		Gtk.CellRendererPixbuf iconrenderer;
		Gtk.TreeIter iter;
		Value valnot = new Value(false);
		Value valone = new Value(true);

		const int COLUMN_0 = 0;
		const int COLUMN_1 = 1;
		const int COLUMN_2 = 2;
		const int ALL_COLUMNS = -1;	
		
		public G_ListView (Gtk.TreeView tr, Gtk.ListStore st)
		{
			store = st;
			tree = tr;
			iter = new Gtk.TreeIter();
		}
		
		public void AddColumnIcon (string iconname, int col)
		{
			column = new TreeViewColumn ();
			iconrenderer = new Gtk.CellRendererPixbuf ();
			column.Expand = false;
			column.Resizable = false;
			column.Clickable = false;
			column.Reorderable = false;
			column.Alignment = 0.5f;
			column.Widget = new Gtk.Image(
				Gtk.IconTheme.Default.LoadIcon (iconname, 16, 0)
			);
			column.Widget.Show();
			column.PackStart (iconrenderer, true);
			column.AddAttribute (iconrenderer, "pixbuf", col);
			tree.AppendColumn(column);
		}
		
		protected int DateTimeTreeIterCompareFunc (TreeModel model, TreeIter a, TreeIter b)
		{
			int col = -1;
			SortType sort;
			
			bool is_sorted = ((Gtk.ListStore)model).GetSortColumnId (out col, out sort);
			if(!is_sorted)
				return 0;

			object oA = model.GetValue (a, col);
			object oB = model.GetValue (b, col);

			if(oB == null)
				return 1;

			if(oA == null)
				return -1;

			DateTime dateA = (DateTime)oA;
			DateTime dateB = (DateTime)oB;

			return dateA.CompareTo(dateB);
		}
		
		protected void DateTimeColumnDataHandler (TreeViewColumn column,
			 CellRenderer cell, TreeModel model, TreeIter iter)
		{
			DateTime date = (DateTime)model.GetValue (iter, column.SortColumnId);
			CellRendererDateTime renderer = (CellRendererDateTime)cell;
			renderer.Text = date.ToString(renderer.Format);
		}
		
		public void AddColumnDateTime (string title, string format, int sortid, int col)
		{
			store.SetSortFunc(col, new TreeIterCompareFunc(DateTimeTreeIterCompareFunc));
			
			column = new TreeViewColumn ();
			datetimerenderer = new CellRendererDateTime ();
			datetimerenderer.Format = format == null ? "G" : format;
			column.Title = title;
			column.SortColumnId = sortid;
			column.Sizing = TreeViewColumnSizing.Autosize;
			column.Reorderable = true;
			column.Resizable = true;
			column.Expand = false;
			column.Alignment = 0.0f;
			column.PackStart (datetimerenderer, true);
			column.SetCellDataFunc (datetimerenderer, new TreeCellDataFunc(DateTimeColumnDataHandler));
			tree.AppendColumn(column);
		}
		
		public void AddColumnTitle (string title, int sortid, int col)
		{
			column = new TreeViewColumn ();
			textrenderer = new CellRendererText ();
			//text.Editable = true;object
			column.Title = title;
			column.SortColumnId = sortid;
			column.Sizing = TreeViewColumnSizing.Autosize;
			column.Reorderable = true;
			column.Resizable = true;
			column.Expand = false;
			column.Alignment = 0.0f;
			column.PackStart (textrenderer, true);
			column.AddAttribute (textrenderer, "text", col);
			tree.AppendColumn(column);
		}

// TODO move this to GfaxSend in gui.cs
		public void AddColumnTitleToggle (string title, int sortid, int col)
		{
			column = new TreeViewColumn ();
			CellRendererToggle toggle = new CellRendererToggle ();
			toggle.Activatable = true;
			toggle.Toggled += new ToggledHandler (toggle_it);
			column.Title = title;
			column.PackStart (toggle, false);
			column.AddAttribute (toggle, "active", col);
			tree.AppendColumn(column);
		}

		// we need to manually set the toggle when selected otherwise
		// it won't show in the gui.
		public void toggle_it (object o, Gtk.ToggledArgs args)
		{
			store.GetIterFromString(out iter, args.Path);
			bool tog = (bool)store.GetValue(iter, 0);
			store.SetValue(iter, 0, !tog);
		}
// end of move

		// Each arg is a new column
		public void AddTextToRow (params object[] args)
		{
			iter = store.AppendValues(args);
			tree.Model = store;
		}

		// returns a list of arrays of columns size
		public ArrayList GetAllRows()
		{
			ArrayList rows = new ArrayList();
						
			// Really get the whole row.
			string[] col = new string[store.NColumns];
			store.GetIterFirst(out iter);
			
			for ( int i = 0; i < store.NColumns; i++) {
				try {
					col[i] = (string)store.GetValue(iter, i);
					//Console.WriteLine("Column {0}",col[i]);
				}
				catch (Exception e) { }
			}
			rows.Add(col);
			
			#if DEBUG
			Console.WriteLine("got colums");
			#endif
			
			while (store.IterNext(ref iter)) {
				string[] coln = new string[store.NColumns];
				for ( int i = 0; i < store.NColumns; i++) {
					coln[i] = (string)store.GetValue(iter, i);
				}
				rows.Add(coln);
			}
			return rows;
		}
		
		// get multiple selections, return a list of objects
		public ArrayList GetSelections(int column)
		{
			ArrayList selects = new ArrayList();
			
			// Really get the whole row.
			if (column == ALL_COLUMNS) {
				store.GetIterFirst(out iter);
				if ( tree.Selection.IterIsSelected(iter)) {
					for ( int i = 0; i < store.NColumns; i++) {
						selects.Add(store.GetValue(iter, i));
					}
				}
				
				while (store.IterNext(ref iter)) {
					if ( tree.Selection.IterIsSelected(iter)) {
						for ( int i = 0; i < store.NColumns; i++) {
							selects.Add(store.GetValue(iter, i));
						}
					}
				}
			}
			else
			{
				store.GetIterFirst(out iter);
				if ( tree.Selection.IterIsSelected(iter)) {
					selects.Add(store.GetValue(iter, column));
				}
			
				while (store.IterNext(ref iter)) {
					if ( tree.Selection.IterIsSelected(iter)) {
						selects.Add(store.GetValue(iter, column));
					}
				}
			}
			return selects;
		}
		
		// get single selection
		public object GetSingleSelection ()
		{
			object sel = null;
			Gtk.TreeIter iter = new Gtk.TreeIter();
                        			
			store.GetIterFirst(out iter);
			if ( tree.Selection.IterIsSelected(iter)) {
				sel = store.GetValue(iter, 0);
			}
			
			while (store.IterNext(ref iter)) {
				if ( tree.Selection.IterIsSelected(iter)) {
					sel = store.GetValue(iter, 0);
				}
			}
			return sel;
		}
		
		public void RemoveSelectedRow ()
		{
			Gtk.TreeIter iter = new Gtk.TreeIter();
                        			
			store.GetIterFirst(out iter);
			if ( tree.Selection.IterIsSelected(iter)) {
				store.Remove(ref iter);
				return;
			}
			
			while (store.IterNext(ref iter)) {
				if ( tree.Selection.IterIsSelected(iter)) {
					store.Remove(ref iter);
					break;
				}
			}
			return;
		}
		
		//TODO get this to work
		public void RemoveSelectedRows ()
		{
			Gtk.TreeIter iter = new Gtk.TreeIter();
                        			
			store.GetIterFirst(out iter);
			if ( tree.Selection.IterIsSelected(iter)) {
				store.Remove(ref iter);
			}
			
			while (store.IterNext(ref iter)) {
				if ( tree.Selection.IterIsSelected(iter)) {
					store.Remove(ref iter);
				}
			}
			return;
		}


		public void UpdateColumnText (string oldString, string newString, int column)
		{
			Gtk.TreeIter iter = new Gtk.TreeIter();
                        			
			store.GetIterFirst(out iter);
			// Check for a null Stamp (no initial entry in the list)
			if (iter.Stamp == 0)
				return;
			
			if ( oldString == (string)store.GetValue(iter, column)) {
				GLib.Value val = new GLib.Value(newString);
  				store.SetValue (iter, column, val);
			}

			while (store.IterNext(ref iter)) {
				if ( oldString == (string)store.GetValue(iter, column)) {
					GLib.Value val1 = new GLib.Value(newString);
  					store.SetValue (iter, column, val1);
				}
			}
			
		}
	}
}