From 0a88e797f454eed5e11d6e73e241ae6accd20388 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sat, 11 Feb 2012 17:06:35 -0300 Subject: [PATCH] Show the clipboard image size as default size in NewFileCommand. --- src/commands/cmd_new_file.cpp | 9 +++++++++ src/util/clipboard.cpp | 17 +++++++++++++++++ src/util/clipboard.h | 6 ++++++ src/util/clipboard_win32.h | 17 +++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/commands/cmd_new_file.cpp b/src/commands/cmd_new_file.cpp index 8dfdd6baf..b663ed528 100644 --- a/src/commands/cmd_new_file.cpp +++ b/src/commands/cmd_new_file.cpp @@ -35,6 +35,7 @@ #include "raster/palette.h" #include "raster/sprite.h" #include "ui_context.h" +#include "util/clipboard.h" #include "widgets/color_bar.h" #include @@ -98,6 +99,14 @@ void NewFileCommand::onExecute(Context* context) bg = get_config_int("NewSprite", "Background", 4); // Default = Background color ncolors = get_config_int("NewSprite", "Colors", 256); + // If the clipboard contains an image, we can show the size of the + // clipboard as default image size. + gfx::Size clipboardSize; + if (clipboard::get_image_size(clipboardSize)) { + w = clipboardSize.w; + h = clipboardSize.h; + } + width->setTextf("%d", MAX(1, w)); height->setTextf("%d", MAX(1, h)); colors->setTextf("%d", MID(2, ncolors, 256)); diff --git a/src/util/clipboard.cpp b/src/util/clipboard.cpp index 5dfbeed8b..1fcacbdb2 100644 --- a/src/util/clipboard.cpp +++ b/src/util/clipboard.cpp @@ -198,3 +198,20 @@ void clipboard::paste() if (src_image != clipboard_image) image_free(src_image); } + +bool clipboard::get_image_size(gfx::Size& size) +{ +#ifdef ALLEGRO_WINDOWS + // Get the image from the clipboard. + return get_win32_clipboard_bitmap_size(size); +#else + if (clipboard_image != NULL) { + size.w = clipboard_image->w; + size.h = clipboard_image->h; + return true; + } + else + return false; +#endif +} + diff --git a/src/util/clipboard.h b/src/util/clipboard.h index 7b018a0b8..5cc773f0b 100644 --- a/src/util/clipboard.h +++ b/src/util/clipboard.h @@ -19,6 +19,7 @@ #ifndef UTIL_CLIPBOARD_H_INCLUDED #define UTIL_CLIPBOARD_H_INCLUDED +#include "gfx/size.h" #include "gui/base.h" class Image; @@ -34,6 +35,11 @@ namespace clipboard { void copy_image(Image* image, Palette* palette); void paste(); + // Returns true and fills the specified "size"" with the image's + // size in the clipboard, or return false in case that the clipboard + // doesn't contain an image at all. + bool get_image_size(gfx::Size& size); + } // namespace clipboard #endif diff --git a/src/util/clipboard_win32.h b/src/util/clipboard_win32.h index fc93a2980..54f0ebb50 100644 --- a/src/util/clipboard_win32.h +++ b/src/util/clipboard_win32.h @@ -325,3 +325,20 @@ static void get_win32_clipboard_bitmap(Image*& image, Palette*& palette) CloseClipboard(); } + +static bool get_win32_clipboard_bitmap_size(gfx::Size& size) +{ + bool result = false; + + if (win32_clipboard_contains_bitmap() && + OpenClipboard(win_get_window())) { + BITMAPINFO* bi = (BITMAPINFO*)GetClipboardData(CF_DIB); + if (bi) { + size.w = bi->bmiHeader.biWidth; + size.h = ABS(bi->bmiHeader.biHeight); + result = true; + } + CloseClipboard(); + } + return result; +}