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 private import std.stdio;
37 
38 private import glib.Str;
39 
40 /**
41  * This tests the GtkD loading and display and image file
42  */
43 class TestImage : VBox
44 {
45 	Table table;
46 	FileChooserDialog fs;
47 	ScrolledWindow sw;
48 
49 	Window window;
50 
51 	this(Window window)
52 	{
53 		this.window = window;
54 		debug(1)
55 		{
56 			writeln("instantiating TestImage");
57 		}
58 
59 		super(false,8);
60 
61 		sw = new ScrolledWindow(null,null);
62 
63 		sw.addWithViewport(initTable());
64 
65 		ButtonBox hBox = HButtonBox.createActionBox();
66 		Button loadDir = new Button("Load Files", &loadImages);
67 		hBox.packStart(loadDir,false,false,0);
68 
69 		packStart(sw,true,true,0);
70 		packStart(hBox,false,false,0);
71 
72 	}
73 
74 	Table initTable()
75 	{
76 
77 		string[] pngs;
78 
79 		pngs ~= "images/gtkD_bevel.png";
80 		pngs ~= "images/gtkDlogo_a.png";
81 		pngs ~= "images/gtkD_logo_plain.png";
82 		pngs ~= "images/gtkD_logo_small.png";
83 		pngs ~= "images/gtkD_icon_1.png";
84 		pngs ~= "images/gtkDlogo_a_small.png";
85 		pngs ~= "images/gtkD_logo.png";
86 		pngs ~= "images/gtkD_logo_too_small.png";
87 
88 
89 		return loadTable(pngs);
90 	}
91 
92 	private Table loadTable(string[] imageFiles)
93 	{
94 		//Table table = new Table(1,1,false);
95 		if ( table  is  null )
96 		{
97 			table = new Table(1,1,false);
98 		}
99 		else
100 		{
101 			table.removeAll();
102 		}
103 
104 
105 		int row = 0;
106 		int col = 0;
107 
108 		Image image;
109 
110 
111 //		Window progressWindow = new Window();//WindowType.POPUP);
112 //		progressWindow.setBorderWidth(10);
113 //		ProgressBar progressBar = new ProgressBar();
114 //		progressWindow.add(progressBar);
115 //		progressWindow.show();
116 
117 
118 		for ( int i=0 ; i<imageFiles.length ;i++)
119 		{
120 			string fileName = imageFiles[i];
121 			if ( fileName[0] != '/' )
122 			{
123 				fileName = fileName;
124 			}
125 			image = new Image(fileName);
126 			//image.addOnEnterNotify(&onEnter);
127 			//image.addOnLeaveNotify(&onLeave);
128 			debug(trace) writefln("adding image %s to table at %s,%s", fileName, col, row);
129 			table.resize(col+1, row+1);
130 			table.attach(image,col,col+1,row,row+1,AttachOptions.FILL,AttachOptions.FILL,4,4);
131 			++col;
132 			if ( col == 8 )
133 			{
134 				col = 0;
135 				++row;
136 			}
137 
138 		}
139 		return table;
140 	}
141 
142 private import glib.ListSG;
143 
144 	void loadImages(Button button)
145 	{
146 		if ( fs  is  null )
147 		{
148 			string[] a;
149 			ResponseType[] r;
150 			a ~= "Lets go!";
151 			a ~= "Please don't";
152 			r ~= ResponseType.ACCEPT;
153 			r ~= ResponseType.CANCEL;
154 			fs = new FileChooserDialog("File Selection", window, FileChooserAction.OPEN, a, r);
155 		}
156 		fs.setSelectMultiple(true);
157 		ResponseType response = cast(ResponseType) fs.run();
158 		if ( response == ResponseType.ACCEPT )
159 		{
160 			string[] fileNames;
161 			ListSG list = fs.getFilenames();
162 
163 
164 			for ( int i = 0; i<list.length() ; i++)
165 			{
166 				debug(trace) writefln("Testmage.loadImages.File selected = %s",
167 						Str.toString(cast(char*)list.nthData(i)));
168 				fileNames ~= Str.toString(cast(char*)list.nthData(i));
169 			}
170 
171 			loadTable(fileNames);
172 		}
173 		fs.hide();
174 	}
175 
176 	void onEnter(Widget widget)
177 	{
178 		writeln("TestImage.mouseEnterNotify");
179 		//return true;
180 	}
181 	void onLeave(Widget widget)
182 	{
183 		writeln("TestImage.mouseLeaveNotify");
184 		//return true;
185 	}
186 
187 
188 }