OSDN Git Service

イニシャルコミット。
[marathon/ShapeFusion.git] / Shapes / BitmapView.cpp
1 /*
2  * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
3  *
4  * ShapeFusion is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ShapeFusion is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with ShapeFusion; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 #include "BitmapView.h"
19 #include "utilities.h"
20
21 BEGIN_EVENT_TABLE(BitmapView, wxScrolledWindow)
22         EVT_PAINT(BitmapView::OnPaint)
23         EVT_LEFT_DOWN(BitmapView::OnDrag)
24         EVT_LEFT_UP(BitmapView::OnDrag)
25         EVT_MOTION(BitmapView::OnDrag)
26         EVT_SIZE(BitmapView::OnSize)
27 END_EVENT_TABLE()
28
29 BitmapView::BitmapView(wxWindow *parent):
30         wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER | wxFULL_REPAINT_ON_RESIZE),
31         mEncBmp(NULL), mColorTable(NULL), mDragging(false)
32 {
33         SetBackgroundColour(wxColour(255, 255, 255));
34         SetScrollRate(1, 1);
35         mWhiteTransparency = true;
36         mInvisiblePen.SetColour(255, 255, 255);
37 }
38
39 void BitmapView::OnPaint(wxPaintEvent& e)
40 {
41         wxPaintDC   tempdc(this);
42         int                     vw, vh, cw, ch;
43
44         GetVirtualSize(&vw, &vh);
45         DoPrepareDC(tempdc);
46         GetClientSize(&cw, &ch);
47         tempdc.SetPen(mInvisiblePen);
48         if (mEncBmp != NULL && mColorTable != NULL)
49                 tempdc.DrawBitmap(mDecBmp, vw/2 - mDecBmp.GetWidth()/2, vh/2 - mDecBmp.GetHeight()/2);
50 }
51
52 // handle mouse drag (pan view)
53 void BitmapView::OnDrag(wxMouseEvent &e)
54 {
55         if (e.ButtonDown()) {
56                 // start panning
57                 int scroll_x, scroll_y;
58
59                 GetViewStart(&scroll_x, &scroll_y);
60                 mDragging = true;
61                 mDragStartX = e.GetPosition().x + scroll_x;
62                 mDragStartY = e.GetPosition().y + scroll_y;
63         } else if (e.Dragging() && mDragging) {
64                 // pan
65                 int     dx = mDragStartX - e.GetPosition().x,
66                         dy = mDragStartY - e.GetPosition().y;
67
68                 Scroll(dx, dy);
69         } else if (e.ButtonUp()) {
70                 // end panning
71                 mDragging = false;
72         }
73 }
74
75 void BitmapView::OnSize(wxSizeEvent &e)
76 {
77         int cw, ch,
78                 vw = (mEncBmp == NULL) ? 0 : mEncBmp->Width(),
79                 vh = (mEncBmp == NULL) ? 0 : mEncBmp->Height();
80         
81         GetClientSize(&cw, &ch);
82         if (vw < cw)
83                 vw = cw;
84         if (vh < ch)
85                 vh = ch;
86         SetVirtualSize(vw, vh);
87 }
88
89 void BitmapView::SetTranspPixelsDisplay(bool show)
90 {
91         mWhiteTransparency = show;
92         if (mEncBmp != NULL && mEncBmp->Pixels() != NULL && mColorTable != NULL)
93                 mDecBmp = wxBitmap(ShapesBitmapToImage(mEncBmp, mColorTable, mWhiteTransparency));
94         Refresh();
95 }
96
97 // add a new ShapesBitmap to the thumbnail list
98 void BitmapView::SetBitmap(ShapesBitmap *bp)
99 {
100         mEncBmp = bp;
101         if (mEncBmp != NULL) {
102                 if (mEncBmp->Pixels() != NULL) {
103                         // adjust sizes
104                         int     cw, ch,
105                                 vw = mEncBmp->Width(),
106                                 vh = mEncBmp->Height();
107
108                         GetClientSize(&cw, &ch);
109                         if (vw < cw)
110                                 vw = cw;
111                         if (vh < ch)
112                                 vh = ch;
113                         SetVirtualSize(vw, vh);
114                         // decode bitmap
115                         if (mColorTable != NULL)
116                                 mDecBmp = wxBitmap(ShapesBitmapToImage(mEncBmp, mColorTable, mWhiteTransparency));
117                         Refresh();
118                 } else {
119                         wxLogError(wxT("[BitmapView] Addes a bitmap with NULL pixels"));
120                         SetVirtualSize(0, 0);
121                 }
122         } else {
123                 SetVirtualSize(0, 0);
124         }
125 }
126
127 ShapesBitmap *BitmapView::GetBitmap(void) const
128 {
129         return mEncBmp;
130 }
131
132 // call this before SettingBitmap!
133 void BitmapView::SetColorTable(ShapesColorTable *ct)
134 {
135         mColorTable = ct;
136         if (mEncBmp != NULL && mEncBmp->Pixels() != NULL && mColorTable != NULL)
137                 mDecBmp = wxBitmap(ShapesBitmapToImage(mEncBmp, mColorTable, mWhiteTransparency));
138         Refresh();
139 }
140