2 /// \file ContactPhotoWidget.cc
3 /// Bitmap button that shows a Contact::Image photo
7 Copyright (C) 2011-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "ContactPhotoWidget.h"
23 #include "windowids.h"
24 #include <barry/barry.h>
25 #include <wx/mstream.h>
32 #define MAX_IMAGE_HEIGHT 60
33 #define DEFAULT_IMAGE_WIDTH 50
35 ContactPhotoWidget::ContactPhotoWidget(wxWindow
*parent
,
40 // TRANSLATORS: this is the first part of a file selector string,
41 // as in: "Images files (*.bmp;*.jpg;*.png)"
42 // The file types are not part of the translation.
43 m_file_filter
= _W("Image files");
44 m_file_filter
+= _T(" (*.bmp;*.jpg;*.png;*.xmp;*.tif)|*.bmp;*.jpg;*.png;*.xmp;*.tif;*.tiff|");
45 // TRANSLATORS: this is a file selector string. See "Image files"
47 m_file_filter
+= _W("All files");
48 m_file_filter
+= _T(" (*.*)|*.*");
50 // limit size of image to 60 px height
51 int max_height
= MAX_IMAGE_HEIGHT
, width
= 0;
53 if( m_rec
.Image
.size() ) {
54 width
= LoadRecImage(max_height
);
57 // anything loaded? if not, load "empty" bitmap
58 if( !m_bitmap
.get() ) {
59 width
= DEFAULT_IMAGE_WIDTH
;
60 max_height
= MAX_IMAGE_HEIGHT
;
61 m_bitmap
.reset( new wxBitmap(width
, max_height
) );
62 DrawNoPhoto(*m_bitmap
, width
, max_height
);
65 // have bitmap, create our bitmap button
66 Create(parent
, id
, *m_bitmap
, wxDefaultPosition
,
67 wxSize(width
, max_height
));
70 int ContactPhotoWidget::LoadRecImage(int max_height
)
72 // load m_rec.Image into a wxBitmap
73 wxMemoryInputStream
stream(m_rec
.Image
.data(), m_rec
.Image
.size());
74 wxImage
jpeg(stream
, wxBITMAP_TYPE_JPEG
);
76 float ratio
= (float)max_height
/ jpeg
.GetHeight();
77 int width
= jpeg
.GetWidth() * ratio
;
79 jpeg
.Rescale(width
, max_height
, wxIMAGE_QUALITY_HIGH
);
80 m_bitmap
.reset( new wxBitmap(jpeg
) );
84 void ContactPhotoWidget::PromptAndSave(wxWindow
*parent
)
86 if( !m_rec
.Image
.size() ) {
87 wxMessageBox(_W("There is no photo available to save."),
89 wxICON_INFORMATION
| wxOK
, this);
93 // TRANSLATORS: this is a file selector string. See "Image files"
95 wxString filter
= _W("JPEG files");
96 filter
+= _T(" (*.jpg)|*.jpg");
98 wxFileDialog
dlg(parent
, _W("Save Photo as JPEG..."), _T(""), _T(""),
100 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
| wxFD_PREVIEW
);
101 if( dlg
.ShowModal() == wxID_OK
) {
102 ofstream
ofs(dlg
.GetPath().utf8_str(), ios::binary
);
103 ofs
.write(m_rec
.Image
.data(), m_rec
.Image
.size());
107 /// Returns true if a new image has been loaded (may want to resize)
108 bool ContactPhotoWidget::PromptAndLoad(wxWindow
*parent
)
110 wxFileDialog
dlg(parent
, _W("Load Photo..."), _T(""), _T(""),
112 wxFD_OPEN
| wxFD_PREVIEW
);
113 if( dlg
.ShowModal() != wxID_OK
)
116 // Load image in whatever format it's in
118 if( !image
.LoadFile(dlg
.GetPath()) ) {
119 wxMessageBox(_W("Unable to load selected photo."),
120 _W("Photo Load Error"),
121 wxICON_ERROR
| wxOK
, this);
125 // Save image to memory as a JPEG
126 wxMemoryOutputStream stream
;
127 if( !image
.SaveFile(stream
, wxBITMAP_TYPE_JPEG
) ) {
128 wxMessageBox(_W("Unable to convert image to JPEG."),
130 wxICON_ERROR
| wxOK
);
134 // Store into Contact record
136 *begin
= (char*)stream
.GetOutputStreamBuffer()->GetBufferStart(),
137 *end
= (char*)stream
.GetOutputStreamBuffer()->GetBufferEnd();
138 int size
= end
- begin
;
139 m_rec
.Image
.assign(begin
, size
);
142 LoadRecImage(MAX_IMAGE_HEIGHT
);
143 SetBitmapLabel(*m_bitmap
);
144 SetSize(m_bitmap
->GetWidth(), m_bitmap
->GetHeight());
148 void ContactPhotoWidget::DeletePhoto()
153 // replace with message
154 wxSize client
= GetClientSize();
155 int width
= client
.GetWidth();
156 int height
= client
.GetHeight();
157 m_bitmap
.reset( new wxBitmap(width
, height
) );
158 DrawNoPhoto(*m_bitmap
, width
, height
);
159 SetBitmapLabel(*m_bitmap
);
162 void ContactPhotoWidget::DrawNoPhoto(wxBitmap
&bm
, int width
, int height
)
168 wxColour
textcolour(0xa9, 0xa5, 0xa2);
169 wxColour
background(0xed, 0xec, 0xeb);
170 wxPen
pen(background
);
171 wxBrush
brush(background
);
172 wxString
line1(_W("No")), line2(_W("Photo"));
173 int pointsize
=wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
)
175 wxFont
font(pointsize
, wxFONTFAMILY_SWISS
, wxFONTSTYLE_NORMAL
,
176 wxFONTWEIGHT_NORMAL
);
178 // trim fontsize so it fits
179 wxSize line1_extent
, line2_extent
;
181 font
.SetPointSize(pointsize
--);
183 line1_extent
= dc
.GetTextExtent(line1
);
184 line2_extent
= dc
.GetTextExtent(line2
);
185 } while( line1_extent
.GetWidth() > width
||
186 line2_extent
.GetWidth() > width
);
191 dc
.SetTextForeground(textcolour
);
192 dc
.SetTextBackground(background
);
194 // calculate position
195 int total_height
= line1_extent
.GetHeight() + line2_extent
.GetHeight();
196 int y1_start
= (height
- total_height
) / 2;
197 int y2_start
= y1_start
+ line1_extent
.GetHeight();
198 int x1_start
= (width
- line1_extent
.GetWidth()) / 2;
199 int x2_start
= (width
- line2_extent
.GetWidth()) / 2;
202 dc
.DrawRectangle(0, 0, width
, height
);
203 dc
.DrawText(line1
, x1_start
, y1_start
);
204 dc
.DrawText(line2
, x2_start
, y2_start
);
207 dc
.SetPen(wxNullPen
);
208 dc
.SetBrush(wxNullBrush
);