Bumped copyright dates for 2013
[barry.git] / desktop / src / ContactPhotoWidget.cc
bloba5f9bb73562f916d5e80653c67cabd13810a2ee7
1 ///
2 /// \file ContactPhotoWidget.cc
3 /// Bitmap button that shows a Contact::Image photo
4 ///
6 /*
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>
26 #include <iostream>
27 #include <fstream>
28 #include "wxi18n.h"
30 using namespace std;
32 #define MAX_IMAGE_HEIGHT 60
33 #define DEFAULT_IMAGE_WIDTH 50
35 ContactPhotoWidget::ContactPhotoWidget(wxWindow *parent,
36 wxWindowID id,
37 Barry::Contact &rec)
38 : m_rec(rec)
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"
46 // for more info.
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) );
81 return width;
84 void ContactPhotoWidget::PromptAndSave(wxWindow *parent)
86 if( !m_rec.Image.size() ) {
87 wxMessageBox(_W("There is no photo available to save."),
88 _W("No Photo"),
89 wxICON_INFORMATION | wxOK, this);
90 return;
93 // TRANSLATORS: this is a file selector string. See "Image files"
94 // for more info.
95 wxString filter = _W("JPEG files");
96 filter += _T(" (*.jpg)|*.jpg");
98 wxFileDialog dlg(parent, _W("Save Photo as JPEG..."), _T(""), _T(""),
99 filter,
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(""),
111 m_file_filter,
112 wxFD_OPEN | wxFD_PREVIEW);
113 if( dlg.ShowModal() != wxID_OK )
114 return false;
116 // Load image in whatever format it's in
117 wxImage image;
118 if( !image.LoadFile(dlg.GetPath()) ) {
119 wxMessageBox(_W("Unable to load selected photo."),
120 _W("Photo Load Error"),
121 wxICON_ERROR | wxOK, this);
122 return false;
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."),
129 _W("Photo Convert"),
130 wxICON_ERROR | wxOK);
131 return false;
134 // Store into Contact record
135 const char
136 *begin = (char*)stream.GetOutputStreamBuffer()->GetBufferStart(),
137 *end = (char*)stream.GetOutputStreamBuffer()->GetBufferEnd();
138 int size = end - begin;
139 m_rec.Image.assign(begin, size);
141 // Update our button
142 LoadRecImage(MAX_IMAGE_HEIGHT);
143 SetBitmapLabel(*m_bitmap);
144 SetSize(m_bitmap->GetWidth(), m_bitmap->GetHeight());
145 return true;
148 void ContactPhotoWidget::DeletePhoto()
150 // zap the record
151 m_rec.Image.clear();
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)
164 wxMemoryDC dc;
165 dc.SelectObject(bm);
167 // resources
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)
174 .GetPointSize();
175 wxFont font(pointsize, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL,
176 wxFONTWEIGHT_NORMAL);
178 // trim fontsize so it fits
179 wxSize line1_extent, line2_extent;
180 do {
181 font.SetPointSize(pointsize--);
182 dc.SetFont(font);
183 line1_extent = dc.GetTextExtent(line1);
184 line2_extent = dc.GetTextExtent(line2);
185 } while( line1_extent.GetWidth() > width ||
186 line2_extent.GetWidth() > width);
188 // setup DC
189 dc.SetPen(pen);
190 dc.SetBrush(brush);
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;
201 // draw
202 dc.DrawRectangle(0, 0, width, height);
203 dc.DrawText(line1, x1_start, y1_start);
204 dc.DrawText(line2, x2_start, y2_start);
206 // cleanup
207 dc.SetPen(wxNullPen);
208 dc.SetBrush(wxNullBrush);