OSDN Git Service

とりあえず実行できるようになった。
[winaudioj/stedx.git] / icon.cpp
1 /*
2   ==============================================================================
3
4    This file is part of the async
5    Copyright 2005-11 by Satoshi Fujiwara.
6
7    async can be redistributed and/or modified under the terms of the
8    GNU General Public License, as published by the Free Software Foundation;
9    either version 2 of the License, or (at your option) any later version.
10
11    async is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with async; if not, visit www.gnu.org/licenses or write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
19    Boston, MA 02111-1307 USA
20
21   ==============================================================================
22 */
23
24 #include "StdAfx.h"
25
26 #if _DEBUG
27 #define _CRTDBG_MAP_ALLOC
28 #include <crtdbg.h>
29 #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
30 #endif
31
32 #include "sf_windows.h"
33 #include "icon.h"
34 #include "application.h"
35
36 namespace sf {
37   bitmap_holder icon::default_mono_bitmap(CreateBitmap(32,32,1,1,NULL));
38
39   icon::icon(uint32_t id) 
40     : icon_((HICON)::LoadImageW(application::instance()->instance_handle(),MAKEINTRESOURCE(id),IMAGE_ICON,0,0,LR_DEFAULTCOLOR))
41   {
42
43   };
44
45   icon::icon(icon_holder& ic)
46   {
47     icon_.reset(::CopyIcon(ic.get()));
48     analyze();
49   }
50
51   icon::icon(icon_holder&& ic)
52   {
53     std::swap(ic,icon_);
54     analyze();
55   }
56
57   icon::icon(bitmap_holder& bmp_color,int width,int height) 
58     : width_(width),height_(height)
59   {
60     BITMAP bmp;
61     ICONINFO ii;
62     ::GetObjectW(bmp_color.get(),sizeof(BITMAP),&bmp);
63     bits_per_pixel_ = bmp.bmBitsPixel;
64
65     ii.fIcon = TRUE;
66     ii.xHotspot = 0;
67     ii.yHotspot = 0;
68     ii.hbmColor = bmp_color.get();
69
70     if(width == 32 && height == 32){
71       ii.hbmMask = default_mono_bitmap.get();
72       icon_.reset(::CreateIconIndirect(&ii));
73     } else {
74       bitmap_holder hmb(::CreateBitmap(width,height,1,1,NULL));
75       ii.hbmMask = hmb.get();
76       icon_.reset(::CreateIconIndirect(&ii));
77     }
78
79   }
80
81   icon::icon(ID2D1BitmapPtr& ptr)
82   {
83     ID2D1FactoryPtr factory;
84     ptr->GetFactory(&factory);
85
86     D2D1_SIZE_U size(ptr->GetPixelSize());
87     // ビットマップヘッダのセットアップ
88     BITMAPV5HEADER bi = {0};
89     bi.bV5Size           = sizeof(BITMAPV5HEADER);
90     bi.bV5Width           = size.width;
91     bi.bV5Height          = size.height;
92     bi.bV5Planes = 1;
93     bi.bV5BitCount = 32;
94     bi.bV5Compression = BI_BITFIELDS;
95     bi.bV5RedMask   =  0x00FF0000;
96     bi.bV5GreenMask =  0x0000FF00;
97     bi.bV5BlueMask  =  0x000000FF;
98     bi.bV5AlphaMask =  0xFF000000; 
99
100     // デスクトップHDCの取得
101     get_dc dc(NULL);
102     
103     // DIBセクションの作成
104     void *bits;// 得られるビットマップ
105     bitmap_holder bmp(
106       ::CreateDIBSection(
107         dc.get(),reinterpret_cast<BITMAPINFO *>(&bi),DIB_RGB_COLORS,&bits,NULL,0));
108     { 
109       // 互換DCの作成
110       compatible_dc cdc(dc.get());
111       {
112         // 描画先への切り替え
113         select_object s(cdc.get(),bmp.get());
114
115         // DC互換レンダーターゲットのセットアップ
116         D2D1_RENDER_TARGET_PROPERTIES 
117           props = D2D1::RenderTargetProperties(
118           D2D1_RENDER_TARGET_TYPE_DEFAULT,
119           D2D1::PixelFormat(
120               DXGI_FORMAT_B8G8R8A8_UNORM,
121               D2D1_ALPHA_MODE_PREMULTIPLIED),
122           0,
123           0,
124           D2D1_RENDER_TARGET_USAGE_NONE,
125           D2D1_FEATURE_LEVEL_DEFAULT
126         );
127
128         ID2D1DCRenderTargetPtr dcr;
129         throw_if_err<>()(factory->CreateDCRenderTarget(&props,dcr.GetAddressOf()));
130         RECT rect = {0,0,size.width,size.height};
131         // 互換DCへのバインド
132         throw_if_err<>()(dcr->BindDC(cdc.get(),&rect));
133         dcr->DrawBitmap(ptr.Get());
134       }
135     }
136     icon(bmp,size.width,size.height);
137   };
138
139
140   icon::icon(boost::filesystem::wpath& path)
141   {
142     icon_.reset(
143       reinterpret_cast<HICON>(
144         LoadImageW(NULL,path.native().c_str(),IMAGE_ICON,0,0,LR_DEFAULTSIZE | LR_LOADFROMFILE)));
145   }
146
147   icon::~icon()
148   {
149
150   }
151
152   void icon::analyze()
153   {
154     ::ICONINFOEXW info;
155     ::GetIconInfoExW(icon_.get(),&info);
156     BITMAP bmp;
157     ::GetObjectW(info.hbmColor,sizeof(BITMAP),&bmp);
158     width_ = bmp.bmWidth;
159     height_ = bmp.bmHeight;
160     bits_per_pixel_ = bmp.bmBitsPixel;
161   }
162
163   icon& icon::operator= (icon& i)
164   {
165     BOOST_ASSERT(icon_ != i.icon_);
166     if(icon_ == i.icon_) return *this;
167     icon_.reset(::CopyIcon(i.icon_.get()));
168     width_ = i.width_;
169     height_ = i.height_;
170     bits_per_pixel_ = i.bits_per_pixel_;
171     return *this;
172   }
173
174   //icon_ptr icon::create_icon()
175   //{
176  
177   //  // ビットマップヘッダのセットアップ
178   //  BITMAPV5HEADER bi = {0};
179   //  bi.bV5Size           = sizeof(BITMAPV5HEADER);
180   //  bi.bV5Width           = width_;
181   //  bi.bV5Height          = height_;
182   //  bi.bV5Planes = 1;
183   //  bi.bV5BitCount = 32;
184   //  bi.bV5Compression = BI_BITFIELDS;
185   //  bi.bV5RedMask   =  0x00FF0000;
186   //  bi.bV5GreenMask =  0x0000FF00;
187   //  bi.bV5BlueMask  =  0x000000FF;
188   //  bi.bV5AlphaMask =  0xFF000000; 
189
190   //  // デスクトップHDCの取得
191   //  get_dc dc(NULL);
192
193   //  // DIBセクションの作成
194   //  void *bits;// 得られるビットマップ
195   //  gdi_object<HBITMAP> bmp(
196   //    ::CreateDIBSection(
197   //      dc.get(),reinterpret_cast<BITMAPINFO *>(&bi),DIB_RGB_COLORS,&bits,NULL,0));
198   //  { 
199   //    // 互換DCの作成
200   //    compatible_dc cdc(dc.get());
201   //    {
202   //      // 描画先への切り替え
203   //      select_object s(cdc.get(),bmp);
204
205   //      // DC互換レンダーターゲットのセットアップ
206   //      D2D1_RENDER_TARGET_PROPERTIES 
207   //        props = D2D1::RenderTargetProperties(
208   //        D2D1_RENDER_TARGET_TYPE_DEFAULT,
209   //        D2D1::PixelFormat(
210   //            DXGI_FORMAT_B8G8R8A8_UNORM,
211   //            D2D1_ALPHA_MODE_PREMULTIPLIED),
212   //        0,
213   //        0,
214   //        D2D1_RENDER_TARGET_USAGE_NONE,
215   //        D2D1_FEATURE_LEVEL_DEFAULT
216   //      );
217
218   //      ID2D1DCRenderTargetPtr dcr;
219   //      throw_if_err<>()(factory_->CreateDCRenderTarget(&props,&dcr));
220   //      RECT rect = {0,0,w,h};
221   //      // 互換DCへのバインド
222   //      throw_if_err<>()(dcr->BindDC(cdc.get(),&rect));
223
224   //      // ブラシのセットアップ(背景の赤丸)
225   //      ID2D1SolidColorBrushPtr brush_e;
226   //      throw_if_err<>()(
227   //        dcr->CreateSolidColorBrush(
228   //          D2D1::ColorF(D2D1::ColorF::Red,0.8f), &brush_e));
229
230   //      // アイコンに描画する文字の生成
231   //      std::wstring t(L"S.F.\nTimer");
232   //      D2D1_RECT_F l = D2D1::RectF(0.0f,0.0f,width_,height_);
233   //      // Text Formatの作成
234   //      IDWriteTextFormatPtr f;
235   //      write_factory_->CreateTextFormat(
236   //      L"メイリオ",                // Font family name.
237   //      NULL,                       // Font collection (NULL sets it to use the system font collection).
238   //      DWRITE_FONT_WEIGHT_REGULAR,
239   //      DWRITE_FONT_STYLE_NORMAL,
240   //      DWRITE_FONT_STRETCH_NORMAL,
241   //      10.0f,
242   //      L"ja-jp",
243   //      &f
244   //      );
245   //      f->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
246   //      f->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
247
248   //      // 文字描画用ブラシのセットアップ
249   //      ID2D1SolidColorBrushPtr brush;
250   //      dcr->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &brush);
251   //      // 描画開始
252   //      dcr->BeginDraw();
253   //      // ビットマップクリア
254   //      dcr->Clear(D2D1::ColorF(D2D1::ColorF::Black,0.0f));
255   //      // 赤丸を描く
256   //      dcr->FillEllipse(D2D1::Ellipse(D2D1::Point2F(16.0f,16.0f),14,14),brush_e);
257   //      // テキストを表示する
258   //      dcr->DrawTextW(t.c_str(),t.size(),f,&l,brush);
259   //      // 描画終了
260   //      dcr->EndDraw();
261   //    }
262   //  }
263   //}
264 }