1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 module TestImage;
20 
21 //debug = trace
22 
23 private import gtk.VBox;
24 
25 private import gtk.Table;
26 private import gtk.FileChooserDialog;
27 private import gtk.Button;
28 private import gtk.Widget;
29 private import gtk.ScrolledWindow;
30 private import gtk.ButtonBox;
31 private import gtk.HButtonBox;
32 private import gtk.Image;
33 
34 private import gtk.Window;
35 
36 version(Tango) private import tango.io.Stdout;
37 version(Tango) private import tango.stdc.stdio;
38 else private import std.stdio;
39 
40 private import glib.Str;
41 
42 /**
43  * This tests the GtkD loading and display and image file
44  */
45 class TestImage : VBox
46 {
47 	Table table;
48 	FileChooserDialog fs;
49 	ScrolledWindow sw;
50 
51 	Window window;
52 
53 	this(Window window)
54 	{
55 		this.window = window;
56 		debug(1)
57 		{
58 			printf("instantiating TestImage\n");
59 		}
60 
61 		super(false,8);
62 
63 		sw = new ScrolledWindow(null,null);
64 
65 		sw.addWithViewport(initTable());
66 
67 		ButtonBox hBox = HButtonBox.createActionBox();
68 		Button loadDir = new Button("Load Files", &loadImages);
69 		hBox.packStart(loadDir,false,false,0);
70 
71 		packStart(sw,true,true,0);
72 		packStart(hBox,false,false,0);
73 
74 	}
75 
76 	Table initTable()
77 	{
78 
79 		string[] pngs;
80 
81 		pngs ~= "images/gtkD_bevel.png";
82 		pngs ~= "images/gtkDlogo_a.png";
83 		pngs ~= "images/gtkD_logo_plain.png";
84 		pngs ~= "images/gtkD_logo_small.png";
85 		pngs ~= "images/gtkD_icon_1.png";
86 		pngs ~= "images/gtkDlogo_a_small.png";
87 		pngs ~= "images/gtkD_logo.png";
88 		pngs ~= "images/gtkD_logo_too_small.png";
89 
90 
91 		return loadTable(pngs);
92 	}
93 
94 	private Table loadTable(string[] imageFiles)
95 	{
96 		//Table table = new Table(1,1,false);
97 		if ( table  is  null )
98 		{
99 			table = new Table(1,1,false);
100 		}
101 		else
102 		{
103 			table.removeAll();
104 		}
105 
106 
107 		int row = 0;
108 		int col = 0;
109 
110 		Image image;
111 
112 
113 //		Window progressWindow = new Window();//WindowType.POPUP);
114 //		progressWindow.setBorderWidth(10);
115 //		ProgressBar progressBar = new ProgressBar();
116 //		progressWindow.add(progressBar);
117 //		progressWindow.show();
118 
119 
120 		for ( int i=0 ; i<imageFiles.length ;i++)
121 		{
122 			string fileName = imageFiles[i];
123 			if ( fileName[0] != '/' )
124 			{
125 				fileName = fileName;
126 			}
127 			image = new Image(fileName);
128 			//image.addOnEnterNotify(&onEnter);
129 			//image.addOnLeaveNotify(&onLeave);
130 			debug(trace) version(Tango) Stdout.format("adding image {} to table at {},{}", fileName, col, row).newline;
131 			else writefln("adding image %s to table at %s,%s", fileName, col, row);
132 			table.resize(col+1, row+1);
133 			table.attach(image,col,col+1,row,row+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
134 			++col;
135 			if ( col == 8 )
136 			{
137 				col = 0;
138 				++row;
139 			}
140 
141 		}
142 		return table;
143 	}
144 
145 private import glib.ListSG;
146 
147 	void loadImages(Button button)
148 	{
149 		if ( fs  is  null )
150 		{
151 			string[] a;
152 			ResponseType[] r;
153 			a ~= "Lets go!";
154 			a ~= "Please don't";
155 			r ~= ResponseType.ACCEPT;
156 			r ~= ResponseType.CANCEL;
157 			fs = new FileChooserDialog("File Selection", window, FileChooserAction.OPEN, a, r);
158 		}
159 		fs.setSelectMultiple(true);
160 		ResponseType response = cast(ResponseType) fs.run();
161 		if ( response == ResponseType.ACCEPT )
162 		{
163 			string[] fileNames;
164 			ListSG list = fs.getFilenames();
165 
166 
167 			for ( int i = 0; i<list.length() ; i++)
168 			{
169 				debug(trace) version(Tango) Stdout.format("Testmage.loadImages.File selected = {}",
170 						Str.toString(cast(char*)list.nthData(i))).newline;
171 				else writefln("Testmage.loadImages.File selected = %s",
172 						Str.toString(cast(char*)list.nthData(i)));
173 				fileNames ~= Str.toString(cast(char*)list.nthData(i));
174 			}
175 
176 			loadTable(fileNames);
177 		}
178 		fs.hide();
179 	}
180 
181 	void onEnter(Widget widget)
182 	{
183 		printf("TestImage.mouseEnterNotify\n");
184 		//return true;
185 	}
186 	void onLeave(Widget widget)
187 	{
188 		printf("TestImage.mouseLeaveNotify\n");
189 		//return true;
190 	}
191 
192 
193 }