| 1 |
public class DesktopFolder.FolderGrid <T> { |
| 2 |
public List <FolderGridRow <T> > rows; |
| 3 |
public int total_cols = 0; |
| 4 |
public int total_rows = 0; |
| 5 |
|
| 6 |
/** |
| 7 |
* Constructor |
| 8 |
* @param int total_cols the total cols for the grid (grid can grow rows but not columns) |
| 9 |
*/ |
| 10 |
public FolderGrid (int total_cols, int total_rows) { |
| 11 |
this.rows = new List <FolderGridRow>(); |
| 12 |
this.total_cols = total_cols; |
| 13 |
this.total_rows = total_rows; |
| 14 |
this.util_create_row (total_rows - 1); |
| 15 |
} |
| 16 |
|
| 17 |
public void print () { |
| 18 |
debug ("printing grid:"); |
| 19 |
for (int irow = 0; irow < this.rows.length (); irow++) { |
| 20 |
FolderGridRow row = this.rows.nth_data (irow); |
| 21 |
string srow = ""; |
| 22 |
for (int icol = 0; icol < row.cols.length; icol++) { |
| 23 |
T col = row.cols[icol]; |
| 24 |
srow = srow + (col == null ? "0" : "X"); |
| 25 |
} |
| 26 |
debug (srow); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @name replace |
| 32 |
* @descriptin replace in the grid the item1 with the item2 |
| 33 |
* @param {T} item1 the item1 to replace |
| 34 |
* @param {Gdk.Point} pos1 the position of the item1 in the grid |
| 35 |
* @param {T} item2 the item2 to replace |
| 36 |
* @param {Gdk.Point} pos2 the position of the item2 in the grid |
| 37 |
*/ |
| 38 |
public void replace (T item1, Gdk.Point pos1, T item2, Gdk.Point pos2) { |
| 39 |
// debug ("replace - start"); |
| 40 |
this.rows.nth_data (pos1.y).cols[pos1.x] = item2; |
| 41 |
this.rows.nth_data (pos2.y).cols[pos2.x] = item1; |
| 42 |
// debug ("replace - end"); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @name CompareAllocations |
| 47 |
* @description Comparator of GtkAllocation objects to order the selection with the keyboard |
| 48 |
* @return {bool} if the a element is greater than the b element |
| 49 |
*/ |
| 50 |
public delegate bool IsSame <T> (T a, T b); |
| 51 |
|
| 52 |
/** |
| 53 |
* @name get_item_cell_position |
| 54 |
* @description return the position for a certain item inside the grid |
| 55 |
* @param {T} item the item to search |
| 56 |
* @param {IsSame} comparator the items comparator implementation |
| 57 |
* @return {Point} the point (row,col) which is occupied by the item (null if none there) |
| 58 |
*/ |
| 59 |
public Gdk.Point ? get_item_cell_position (T item, IsSame <T> comparator) { |
| 60 |
for (int irow = 0; irow < this.rows.length (); irow++) { |
| 61 |
FolderGridRow row = this.rows.nth_data (irow); |
| 62 |
for (int icol = 0; icol < row.cols.length; icol++) { |
| 63 |
T col = row.cols[icol]; |
| 64 |
if (col != null && comparator (col, item)) { |
| 65 |
var point = Gdk.Point (); |
| 66 |
point.x = icol; |
| 67 |
point.y = irow; |
| 68 |
return point; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @name get_item_at_cell |
| 77 |
* @description return the item at the cell specified |
| 78 |
* @param {Gdk.Point} the cell x,y position |
| 79 |
* @return {T} the item at that cell or null if none |
| 80 |
*/ |
| 81 |
public T ? get_item_at_cell (Gdk.Point pos) { |
| 82 |
// debug ("get_item_at_cell: %d, %d", pos.x, pos.y); |
| 83 |
if (this.rows.length () > pos.y) { |
| 84 |
if (this.rows.nth_data (pos.y).cols.length > pos.x) { |
| 85 |
// debug ("this.rows.length(%d) - cols(%d)", (int) this.rows.length (), this.rows.nth_data (pos.y).cols.length); |
| 86 |
return this.rows.nth_data (pos.y).cols[pos.x]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @name get_cell |
| 95 |
* @description return the cell for the pixel x,y |
| 96 |
* @param {int} sensitivity the sensitivity used at the arrangement |
| 97 |
* @param {int} padding the padding used at the arrangmeent |
| 98 |
* @param {int} x the x pixel |
| 99 |
* @param {int} y the y pixel |
| 100 |
* @return {Gdk.Point} the cell(x,y) |
| 101 |
*/ |
| 102 |
public Gdk.Point get_cell (int sensitivity, int padding, int x, int y) { |
| 103 |
int cell_x = (x + DesktopFolder.ICON_DEFAULT_WIDTH / 2) / (sensitivity + padding); |
| 104 |
int cell_y = (y + DesktopFolder.ICON_DEFAULT_WIDTH / 2) / (sensitivity + padding); |
| 105 |
var point = Gdk.Point (); |
| 106 |
point.x = cell_x; |
| 107 |
point.y = cell_y; |
| 108 |
|
| 109 |
if (point.x < 0) { |
| 110 |
point.x = 0; |
| 111 |
} |
| 112 |
if (point.y < 0) { |
| 113 |
point.y = 0; |
| 114 |
} |
| 115 |
return point; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @name put |
| 120 |
* @description put a new data into the grid at a concrete position |
| 121 |
* @param int row the row position of the grid |
| 122 |
* @param int col the column position of the grid |
| 123 |
* @param T data the data to put |
| 124 |
*/ |
| 125 |
public void put (int row, int col, T data) { |
| 126 |
util_create_row (row); |
| 127 |
this.rows.nth_data (row).cols[col] = data; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @name util_create_row |
| 132 |
* @description create a new row for the list of cells (we can grow vertically) |
| 133 |
* |
| 134 |
*/ |
| 135 |
private void util_create_row (int row) { |
| 136 |
if (rows.length () <= row) { |
| 137 |
// we must create enough rows |
| 138 |
for (int i = ((int) this.rows.length ()) - 1; i < row; i++) { |
| 139 |
FolderGridRow <T> new_row = new FolderGridRow <T> (total_cols); |
| 140 |
this.rows.append (new_row); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @name get_next_gap |
| 147 |
* @description find a gap inside the current structure and put there the item |
| 148 |
* @param FolderWindow parent_window the parent panel in which the items are placed |
| 149 |
* @param T item the item to put at the gap position |
| 150 |
* @param int padding the padding between cells |
| 151 |
* @param {bool} vertically if the sort orientation is vertically or not |
| 152 |
* @return {Gdk.Point} the x,y point to draw the item |
| 153 |
*/ |
| 154 |
public Gdk.Point get_next_gap (FolderWindow parent_window, T item, int padding, bool vertically) { |
| 155 |
// getting the header panel |
| 156 |
int margin = FolderArrangement.DEFAULT_EXTERNAL_MARGIN; |
| 157 |
|
| 158 |
if (vertically) { |
| 159 |
if (this.rows.length () > 0) { |
| 160 |
FolderGridRow first_row = this.rows.nth_data (0); |
| 161 |
for (int icol = 0; icol < first_row.cols.length; icol++) { |
| 162 |
for (int irow = 0; irow < this.rows.length (); irow++) { |
| 163 |
FolderGridRow row = this.rows.nth_data (irow); |
| 164 |
if (row.cols[icol] == null) { |
| 165 |
row.cols[icol] = item; |
| 166 |
Gdk.Point point = Gdk.Point (); |
| 167 |
point.y = (irow * DesktopFolder.ICON_DEFAULT_WIDTH) + (irow * padding); |
| 168 |
point.x = margin + (icol * DesktopFolder.ICON_DEFAULT_WIDTH) + (icol * padding); |
| 169 |
return point; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} else { |
| 175 |
for (int irow = 0; irow < this.rows.length (); irow++) { |
| 176 |
FolderGridRow row = this.rows.nth_data (irow); |
| 177 |
for (int icol = 0; icol < row.cols.length; icol++) { |
| 178 |
if (row.cols[icol] == null) { |
| 179 |
row.cols[icol] = item; |
| 180 |
Gdk.Point point = Gdk.Point (); |
| 181 |
point.y = (irow * DesktopFolder.ICON_DEFAULT_WIDTH) + (irow * padding); |
| 182 |
point.x = margin + (icol * DesktopFolder.ICON_DEFAULT_WIDTH) + (icol * padding); |
| 183 |
return point; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
// no gap found, lets create a new row |
| 191 |
int last_row = (int) this.rows.length (); |
| 192 |
this.util_create_row (last_row); |
| 193 |
this.rows.nth_data (last_row).cols[0] = item; |
| 194 |
Gdk.Point point = Gdk.Point (); |
| 195 |
point.y = (last_row * DesktopFolder.ICON_DEFAULT_WIDTH) + (last_row * padding); |
| 196 |
point.x = margin; |
| 197 |
return point; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @name build_grid_structure |
| 202 |
* @description build an array describing the grid structure inside the panel. |
| 203 |
* This map is useful to try to structure and align all the items inside the panel |
| 204 |
* @param FolderWindow parent_window the parent panel in which the items are placed |
| 205 |
* @param {int} padding the internal padding for elements |
| 206 |
* @param {ItemSettings} lookin_for_item (optiona) the item we are trying to look in the structure (in case there are various items in the same cell, the looked one is prefered) |
| 207 |
* @return List with the ItemSettings inside, null are empty places |
| 208 |
*/ |
| 209 |
public static FolderGrid build_grid_structure (FolderWindow parent_window, int padding, ItemSettings ? looking_for_item = null) { |
| 210 |
FolderSettings settings = parent_window.get_manager ().get_settings (); |
| 211 |
int width = settings.w; |
| 212 |
int height = settings.h; |
| 213 |
|
| 214 |
// getting all the items defined |
| 215 |
List <ItemSettings> items = new List <ItemSettings> (); |
| 216 |
for (int i = 0; i < settings.items.length; i++) { |
| 217 |
ItemSettings is = ItemSettings.parse (settings.items[i]); |
| 218 |
items.append (is); |
| 219 |
} |
| 220 |
|
| 221 |
int margin = FolderArrangement.DEFAULT_EXTERNAL_MARGIN; |
| 222 |
|
| 223 |
Gtk.Allocation title_allocation; |
| 224 |
parent_window.get_titlebar ().get_allocation (out title_allocation); |
| 225 |
int header = title_allocation.height + margin; |
| 226 |
|
| 227 |
width = width - margin - margin; // removing margin |
| 228 |
height = height - header; |
| 229 |
int total_cols = width / (DesktopFolder.ICON_DEFAULT_WIDTH + padding); |
| 230 |
int total_rows = height / (DesktopFolder.ICON_DEFAULT_WIDTH + padding); |
| 231 |
|
| 232 |
// we create a cell structure of allowed items, it is a list of rows, |
| 233 |
// inside each row is an array with all the columns |
| 234 |
FolderGrid <ItemSettings> grid = new FolderGrid <ItemSettings> (total_cols, total_rows); |
| 235 |
|
| 236 |
// now, ordering current items in the structure to see gaps |
| 237 |
for (int i = 0; i < items.length (); i++) { |
| 238 |
ItemSettings item = items.nth_data (i); |
| 239 |
int x = item.x; |
| 240 |
int y = item.y; |
| 241 |
|
| 242 |
int row = (int) (y / (DesktopFolder.ICON_DEFAULT_WIDTH + padding)); |
| 243 |
int col = (int) (x / (DesktopFolder.ICON_DEFAULT_WIDTH + padding)); |
| 244 |
|
| 245 |
if (col >= total_cols) { |
| 246 |
col = total_cols - 1; |
| 247 |
} |
| 248 |
|
| 249 |
if (row < 0) { |
| 250 |
row = 0; |
| 251 |
} |
| 252 |
if (col < 0) { |
| 253 |
col = 0; |
| 254 |
} |
| 255 |
// filling the gap with the item |
| 256 |
if (looking_for_item == null) { |
| 257 |
grid.put (row, col, item); |
| 258 |
} else { |
| 259 |
// checking if in the same position is already an item |
| 260 |
if (grid.rows.length () > row && grid.rows.nth_data (row).cols.length > col) { |
| 261 |
ItemSettings current = grid.rows.nth_data (row).cols[col]; |
| 262 |
if (current == null || current.name != looking_for_item.name) { |
| 263 |
// replacing |
| 264 |
grid.put (row, col, item); |
| 265 |
} else { |
| 266 |
} |
| 267 |
} else { |
| 268 |
grid.put (row, col, item); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return grid; |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
public class DesktopFolder.FolderGridRow <T> { |
| 279 |
public T[] cols; |
| 280 |
|
| 281 |
public FolderGridRow (int total_cols) { |
| 282 |
this.cols = new T[total_cols]; |
| 283 |
} |
| 284 |
} |