-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreview_frame.cpp
More file actions
158 lines (129 loc) · 3.93 KB
/
preview_frame.cpp
File metadata and controls
158 lines (129 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2016 Frode Roxrud Gill
// See LICENSE file for license
#ifdef __GNUG__
#pragma implementation "preview_frame.h"
#endif
#include <wx/dcclient.h>
#include "preview_frame.h"
#include "app.h"
using namespace PhotoMailer;
// the preview window icon
#include "photomailer_preview.xpm"
BEGIN_EVENT_TABLE(PreviewFrame, wxMiniFrame)
EVT_CLOSE(PreviewFrame::OnClose)
EVT_SIZE(PreviewFrame::OnSize)
EVT_PAINT(PreviewFrame::OnPaint)
EVT_THREAD(PREVIEW_EVENT, PreviewFrame::OnPreviewEvent)
END_EVENT_TABLE()
wxIMPLEMENT_CLASS(PreviewFrame, wxMiniFrame);
PreviewFrame::PreviewFrame(wxWindow* parent, wxWindowID id, const wxString& title,
const wxPoint& pos, const wxSize& size, long style)
: wxMiniFrame(parent, id, title, pos, size, style)
{
SetIcon(wxIcon(photomailer_preview_xpm));
SetSize(-1,-1);
#if wxVERSION_NUMBER >= 2900
if(!wxPersistenceManager::Get().Find(this)) {
wxPersistenceManager::Get().RegisterAndRestore(this);
} else {
wxPersistenceManager::Get().Restore(this);
}
#endif
}
void PreviewFrame::OnClose(wxCloseEvent& WXUNUSED(event))
{
::wxGetApp().OnPreviewFrameClosed();
Destroy();
}
void PreviewFrame::OnSize(wxSizeEvent& event)
{
ShowPhoto();
event.Skip(true);
}
void PreviewFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxMutexLocker lock(m_preview_mutex);
if (m_selected_photo_bitmap.IsOk())
{
wxPaintDC dc(this);
wxSize dc_size = dc.GetSize();
dc.DrawBitmap(m_selected_photo_bitmap,
(dc_size.GetWidth()-m_selected_photo_bitmap.GetWidth())/2,
(dc_size.GetHeight()-m_selected_photo_bitmap.GetHeight())/2);
}
}
void PreviewFrame::OnPreviewEvent(wxThreadEvent& event)
{
wxMutexLocker lock(m_preview_mutex);
PreviewEventPayload* payload = event.GetPayload<PreviewEventPayload*>();
if (payload)
{
const wxBitmap* bitmap = payload->GetBitmap();
m_selected_photo_bitmap = bitmap->GetSubBitmap(wxRect(0, 0, bitmap->GetWidth(), bitmap->GetHeight()));
wxString filename;
payload->GetFilename(filename);
delete payload;
SetTitle(_("Preview - ")+filename);
Refresh();
}
}
void PreviewFrame::ShowPhoto()
{
PreviewThread* thread = new PreviewThread(this, GetClientSize());
if (thread)
{
thread->Run();
}
}
PreviewThread::PreviewThread(wxEvtHandler* event_handler, const wxSize& size)
: wxThread(),
m_event_handler(event_handler),
m_size(size)
{
::wxGetApp().GetMainFrame()->RegisterThread(this);
}
PreviewThread::~PreviewThread()
{
::wxGetApp().GetMainFrame()->UnregisterThread(this);
}
wxThread::ExitCode PreviewThread::Entry()
{
if (TestDestroy())
return CleanupAndExit(-1);
wxImage image;
wxString filename;
if (!::wxGetApp().GetMainFrame()->GetSelectedPhoto(image, filename) || !image.IsOk())
return CleanupAndExit(-1);
int dc_width = m_size.GetWidth();
int dc_height = m_size.GetHeight();
int image_width = image.GetWidth();
int image_height = image.GetHeight();
if (0==dc_width || 0==dc_height || 0==image_width || 0==image_height)
return CleanupAndExit(-1);
float dc_ratio = static_cast<float>(dc_width)/static_cast<float>(dc_height);
float image_ratio = static_cast<float>(image_width)/static_cast<float>(image_height);
if (dc_ratio>image_ratio)
{
image_width = static_cast<float>(dc_height)*image_ratio;
image_height = dc_height;
}
else
{
image_width = dc_width;
image_height = static_cast<float>(dc_width)/image_ratio;
}
wxThreadEvent* threadEvent = new wxThreadEvent(wxEVT_THREAD, PREVIEW_EVENT);
PreviewEventPayload* payload = new PreviewEventPayload(image.Scale(image_width, image_height, wxIMAGE_QUALITY_NORMAL), filename);
threadEvent->SetPayload<PreviewEventPayload*>(payload);
::wxQueueEvent(m_event_handler, threadEvent);
return CleanupAndExit(0);
}
wxThread::ExitCode PreviewThread::CleanupAndExit(int exit_code)
{
return reinterpret_cast<wxThread::ExitCode>(exit_code);
}
PreviewEventPayload::PreviewEventPayload(const wxImage& image, const wxString& filename)
{
m_bitmap = wxBitmap(image);
m_filename = filename;
}