| 1 | // $Header$ |
|---|
| 2 | /*************************************************************************** |
|---|
| 3 | * Copyright (C) 2006-2007 by Guy Rutenberg * |
|---|
| 4 | * guyrutenberg@gmail.com * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program 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 this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include "wxDynamicBitmap.h" |
|---|
| 23 | #include <wx/wx.h> |
|---|
| 24 | |
|---|
| 25 | #include <iostream> |
|---|
| 26 | |
|---|
| 27 | wxDynamicBitmap::wxDynamicBitmap(wxWindow* parent, wxWindowID id, wxBitmap *bitmap, |
|---|
| 28 | const wxPoint& pos, const wxSize& size, |
|---|
| 29 | long style, const wxString& name) |
|---|
| 30 | { |
|---|
| 31 | wxControl::Create(parent,id,pos,size,style,wxDefaultValidator,name); |
|---|
| 32 | Connect(id, wxEVT_PAINT, wxPaintEventHandler(wxDynamicBitmap::OnPaint)); |
|---|
| 33 | Connect(id, wxEVT_LEFT_UP,wxMouseEventHandler(wxDynamicBitmap::OnClick)); |
|---|
| 34 | m_grayscale=false; |
|---|
| 35 | SetBitmap( bitmap); |
|---|
| 36 | |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | void wxDynamicBitmap::OnPaint(wxPaintEvent& event) |
|---|
| 40 | { |
|---|
| 41 | wxPaintDC dc(this); |
|---|
| 42 | PaintBitmap(dc); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | wxBitmap wxDynamicBitmap::GetBitmap() |
|---|
| 46 | { |
|---|
| 47 | return m_bitmap; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void wxDynamicBitmap::SetBitmap( wxBitmap *bitmap) |
|---|
| 51 | { |
|---|
| 52 | m_bitmap = *bitmap; |
|---|
| 53 | SetGrayScale(m_grayscale); |
|---|
| 54 | wxWindow::Refresh(); |
|---|
| 55 | wxWindow::Update(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void wxDynamicBitmap::PaintBitmap(wxDC& dc) |
|---|
| 59 | { |
|---|
| 60 | wxColour backgroundColour = GetBackgroundColour(); |
|---|
| 61 | if (!backgroundColour.Ok()) |
|---|
| 62 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
|---|
| 63 | dc.SetBrush(wxBrush(backgroundColour)); |
|---|
| 64 | dc.SetPen(wxPen(backgroundColour, 1)); |
|---|
| 65 | wxRect windowRect(wxPoint(0, 0), GetClientSize()); |
|---|
| 66 | |
|---|
| 67 | dc.DrawRectangle(windowRect); |
|---|
| 68 | |
|---|
| 69 | dc.DrawBitmap((m_grayscale?m_graybitmap:m_bitmap) , 0 , 0, true); |
|---|
| 70 | |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | wxSize wxDynamicBitmap::DoGetBestSize() const |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | return wxSize(m_bitmap.GetWidth(),m_bitmap.GetHeight()); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void wxDynamicBitmap::OnClick(wxMouseEvent& event) |
|---|
| 80 | { |
|---|
| 81 | //A hack that makes propogates wxEVT_COMMAND_BUTTON_CLICKED events to the parent of this control |
|---|
| 82 | wxCommandEvent newevent( wxEVT_COMMAND_BUTTON_CLICKED, GetId() ); |
|---|
| 83 | newevent.SetEventObject( this ); |
|---|
| 84 | GetEventHandler()->ProcessEvent( newevent ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * This function repaints the widget with a grayscale version of the currently |
|---|
| 89 | * displayed picture, or repaints it with the original version depending on the |
|---|
| 90 | * value of grayscale. |
|---|
| 91 | * \param grayscale [bool] if true repaints the image with grayscale version, |
|---|
| 92 | * otherwise reverts to the original picture |
|---|
| 93 | */ |
|---|
| 94 | void wxDynamicBitmap::SetGrayScale(bool grayscale) |
|---|
| 95 | { |
|---|
| 96 | m_grayscale = grayscale; |
|---|
| 97 | |
|---|
| 98 | //regenerate the grayscale bitmap; |
|---|
| 99 | if (grayscale) { |
|---|
| 100 | wxImage tempimage; |
|---|
| 101 | tempimage = m_bitmap.ConvertToImage(); |
|---|
| 102 | //ConvertToGrayScale(tempimage); |
|---|
| 103 | m_graybitmap = wxBitmap(tempimage.ConvertToGreyscale()); |
|---|
| 104 | } |
|---|
| 105 | wxWindow::Refresh(); |
|---|
| 106 | wxWindow::Update(); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void wxDynamicBitmap::ConvertToGrayScale(wxImage& image) const |
|---|
| 110 | { |
|---|
| 111 | double red2Gray = 0.297; |
|---|
| 112 | double green2Gray = 0.589; |
|---|
| 113 | double blue2Gray = 0.114; |
|---|
| 114 | int w = image.GetWidth(), h = image.GetHeight(); |
|---|
| 115 | unsigned char *data = image.GetData(); |
|---|
| 116 | int x,y; |
|---|
| 117 | for (y = 0; y < h; y++) |
|---|
| 118 | for (x = 0; x < w; x++) |
|---|
| 119 | { |
|---|
| 120 | long pos = (y * w + x) * 3; |
|---|
| 121 | char g = (char) (data[pos]*red2Gray + |
|---|
| 122 | data[pos+1]*green2Gray + |
|---|
| 123 | data[pos+2]*blue2Gray); |
|---|
| 124 | data[pos] = data[pos+1] = data[pos+2] = g; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|