+ snprintf(part, MAX_CB_PART_SIZE + 1, "%s%03d", prefix, element_mapped);
+ strcat(text, part);
+ }
+
+ strcat(text, "\n");
+ }
+
+ if (mode == CB_BRUSH_TO_CLIPBOARD ||
+ mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
+ SDL_SetClipboardText(text);
+ else
+ printf("%s", text);
+
+ return;
+ }
+
+ if (mode == CB_CLIPBOARD_TO_BRUSH)
+ {
+ if (edit_mode != ED_MODE_DRAWING)
+ return;
+
+ if (!SDL_HasClipboardText())
+ {
+ Request("Clipboard is empty!", REQ_CONFIRM);
+
+ return;
+ }
+
+ boolean copy_to_brush = (draw_with_brush ||
+ drawing_function == GADGET_ID_GRAB_BRUSH);
+
+ // this will delete the old brush, if already drawing with a brush
+ if (copy_to_brush)
+ ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
+
+ // initialization is required for "odd" (incomplete) clipboard content
+ for (x = 0; x < MAX_LEV_FIELDX; x++)
+ for (y = 0; y < MAX_LEV_FIELDY; y++)
+ brush_buffer[x][y] = EL_EMPTY;
+
+ brush_width = 0;
+ brush_height = 0;
+ x = 0;
+ y = 0;
+
+ char *clipboard_text = SDL_GetClipboardText();
+ char *ptr = clipboard_text;
+ boolean stop = FALSE;
+
+ while (*ptr && !stop)
+ {
+ boolean prefix_found = FALSE;
+
+ // level sketch element number prefixes (may be multi-byte characters)
+ char *prefix_list[] = { "`", "¸" };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
+ {
+ char *prefix = prefix_list[i];
+
+ // check if string is large enough for prefix
+ if (strlen(ptr) < strlen(prefix))
+ {
+ stop = TRUE;
+
+ break;
+ }
+
+ // check if string starts with prefix
+ if (strPrefix(ptr, prefix))
+ {
+ ptr += strlen(prefix);
+
+ prefix_found = TRUE;
+
+ break;
+ }
+ }
+
+ // continue with next character if prefix not found
+ if (!prefix_found)
+ {
+ ptr++; // !!! FIX THIS for real UTF-8 handling !!!
+
+ continue;
+ }
+
+ // continue with next character if prefix not found
+ if (strlen(ptr) < 3)
+ break;
+
+ if (ptr[0] >= '0' && ptr[0] <= '9' &&
+ ptr[1] >= '0' && ptr[1] <= '9' &&
+ ptr[2] >= '0' && ptr[2] <= '9')
+ {
+ int element = ((ptr[0] - '0') * 100 +
+ (ptr[1] - '0') * 10 +
+ (ptr[2] - '0'));
+
+ ptr += 3;
+
+ if (element >= NUM_FILE_ELEMENTS)
+ element = EL_UNKNOWN;
+
+ brush_buffer[x][y] = element;
+
+ brush_width = MAX(x + 1, brush_width);
+ brush_height = MAX(y + 1, brush_height);
+
+ x++;
+
+ if (x >= MAX_LEV_FIELDX || *ptr == '\n')
+ {
+ x = 0;
+ y++;
+
+ if (y >= MAX_LEV_FIELDY)
+ stop = TRUE;
+ }