+static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
+{
+ int from_sx, from_sy;
+ int to_sx, to_sy;
+
+ if (from_x > to_x)
+ swap_numbers(&from_x, &to_x);
+
+ if (from_y > to_y)
+ swap_numbers(&from_y, &to_y);
+
+ from_sx = SX + from_x * MINI_TILEX;
+ from_sy = SY + from_y * MINI_TILEY;
+ to_sx = SX + to_x * MINI_TILEX + MINI_TILEX - 1;
+ to_sy = SY + to_y * MINI_TILEY + MINI_TILEY - 1;
+
+ XSetForeground(display, gc, WhitePixel(display, screen));
+
+ XDrawLine(display, drawto, gc, from_sx, from_sy, to_sx, from_sy);
+ XDrawLine(display, drawto, gc, to_sx, from_sy, to_sx, to_sy);
+ XDrawLine(display, drawto, gc, to_sx, to_sy, from_sx, to_sy);
+ XDrawLine(display, drawto, gc, from_sx, to_sy, from_sx, from_sy);
+
+ XSetForeground(display, gc, BlackPixel(display, screen));
+
+ if (from_x == to_x && from_y == to_y)
+ MarkTileDirty(from_x/2, from_y/2);
+ else
+ redraw_mask |= REDRAW_FIELD;
+}
+
+static void SelectArea(int from_x, int from_y, int to_x, int to_y,
+ int element, boolean change_level)
+{
+ if (element == -1 || change_level)
+ DrawRectangle(from_x, from_y, to_x, to_y, -1, FALSE);
+ else
+ DrawAreaBorder(from_x, from_y, to_x, to_y);
+}
+
+/* values for CopyBrushExt() */
+#define CB_AREA_TO_BRUSH 0
+#define CB_BRUSH_TO_CURSOR 1
+#define CB_BRUSH_TO_LEVEL 2
+#define CB_DELETE_OLD_CURSOR 3
+
+static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y, int mode)
+{
+ static short brush_buffer[ED_FIELDX][ED_FIELDY];
+ static int brush_width, brush_height;
+ static int last_cursor_x = -1, last_cursor_y = -1;
+ static boolean delete_old_brush;
+ int x, y;
+
+ if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
+ return;
+
+ if (mode == CB_AREA_TO_BRUSH)
+ {
+ int from_lx, from_ly;
+
+ if (from_x > to_x)
+ swap_numbers(&from_x, &to_x);
+
+ if (from_y > to_y)
+ swap_numbers(&from_y, &to_y);
+
+ brush_width = to_x - from_x + 1;
+ brush_height = to_y - from_y + 1;
+
+ from_lx = from_x + level_xpos;
+ from_ly = from_y + level_ypos;
+
+ for (y=0; y<brush_height; y++)
+ for (x=0; x<brush_width; x++)
+ brush_buffer[x][y] = Feld[from_lx + x][from_ly + y];
+
+ delete_old_brush = FALSE;
+ }
+ else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
+ mode == CB_BRUSH_TO_LEVEL)
+ {
+ int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
+ int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
+ int cursor_from_x = cursor_x - brush_width / 2;
+ int cursor_from_y = cursor_y - brush_height / 2;
+ int border_from_x = cursor_x, border_from_y = cursor_y;
+ int border_to_x = cursor_x, border_to_y = cursor_y;
+
+ if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
+ CopyBrushExt(0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
+
+ if (!IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
+ {
+ delete_old_brush = FALSE;
+ return;
+ }
+
+ for (y=0; y<brush_height; y++)
+ {
+ for (x=0; x<brush_width; x++)
+ {
+ int sx = cursor_from_x + x;
+ int sy = cursor_from_y + y;
+ int lx = sx + level_xpos;
+ int ly = sy + level_ypos;
+ int element = (mode == CB_DELETE_OLD_CURSOR ? -1 : brush_buffer[x][y]);
+ boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
+
+ if (IN_LEV_FIELD(lx, ly) &&
+ sx >=0 && sx < ED_FIELDX && sy >=0 && sy < ED_FIELDY)
+ {
+ if (sx < border_from_x)
+ border_from_x = sx;
+ else if (sx > border_to_x)
+ border_to_x = sx;
+ if (sy < border_from_y)
+ border_from_y = sy;
+ else if (sy > border_to_y)
+ border_to_y = sy;
+
+ DrawLineElement(sx, sy, element, change_level);
+ }
+ }
+ }
+
+ /*
+ printf("%d, %d - %d, %d in level and screen\n",
+ border_from_x, border_from_y, border_to_x, border_to_y);
+ */
+
+ if (mode != CB_DELETE_OLD_CURSOR)
+ DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
+
+ if (mode == CB_BRUSH_TO_LEVEL)
+ CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
+
+ last_cursor_x = cursor_x;
+ last_cursor_y = cursor_y;
+ delete_old_brush = TRUE;
+ }
+}
+
+static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y)
+{
+ CopyBrushExt(from_x, from_y, to_x, to_y, CB_AREA_TO_BRUSH);
+}
+
+static void CopyBrushToLevel(int x, int y)
+{
+ CopyBrushExt(x, y, 0, 0, CB_BRUSH_TO_LEVEL);
+}
+
+static void CopyBrushToCursor(int x, int y)
+{
+ CopyBrushExt(x, y, 0, 0, CB_BRUSH_TO_CURSOR);
+}
+
+static void DeleteBrushFromCursor()
+{
+ CopyBrushExt(0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
+}
+