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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*********************************************************************
* Name: Paint.cpp
* Purpose: Implements simple wxWidgets application with GUI.
* Author: Hengjian Jia
* Created: Hengjian Jia
* Copyright: Hengjian Jia
* License: wxWidgets license (www.wxwidgets.org)
*
* Notes:
*********************************************************************/
#include <wx/wx.h>
#include <iostream>
#include <vector>
#define wxID_SET_RED 1
#define wxID_SET_BLACK 0
#define wxID_SET_BLUE 2
#define wxID_SET_CYAN 4
#define wxID_SET_GREEN 3
wxBitmap *Image;
wxClientDC *Canvas;
wxMemoryDC *Memory;
wxPen wxBLUE_PEN(*wxBLUE);
int element=-1;
using namespace std;
vector<wxPoint> Start;
vector<wxPoint> End;
vector<wxPen> Pen;
// application class
class MyFrame : public wxFrame
{
public:
// Constructor
MyFrame(const wxString& title, long style=wxDEFAULT_FRAME_STYLE);
// Event handlers
void OnMouseDown(wxMouseEvent& event);
void OnMouseUp(wxMouseEvent& event);
void OnUndo(wxCommandEvent& event);
void OnClear(wxCommandEvent& event);
void OnSetPenRed(wxCommandEvent& event);
void OnSetPenBlue(wxCommandEvent& event);
void OnSetPenGreen(wxCommandEvent& event);
void OnSetPenCyan(wxCommandEvent& event);
void OnSetPenBlack(wxCommandEvent& event);
void OnOpenImage(wxCommandEvent& event);
private:
int CurrentPen;
bool assigned;
bool FileLoaded;
};
class MyApp : public wxApp
{
public:
// function called at the application initialization
virtual bool OnInit();
};
DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)
void MyFrame::OnSetPenRed(wxCommandEvent& event)
{
CurrentPen=wxID_SET_RED;
assigned=true;
}
void MyFrame::OnSetPenBlue(wxCommandEvent& event)
{
CurrentPen=wxID_SET_BLUE;
assigned=true;
}
void MyFrame::OnSetPenGreen(wxCommandEvent& event)
{
CurrentPen=wxID_SET_GREEN;
assigned=true;
}
void MyFrame::OnSetPenCyan(wxCommandEvent& event)
{
CurrentPen=wxID_SET_CYAN;
assigned=true;
}
void MyFrame::OnSetPenBlack(wxCommandEvent& event)
{
CurrentPen=wxID_SET_BLACK;
assigned=true;
}
void MyFrame::OnMouseDown(wxMouseEvent& event)
{
if(assigned==false)
Canvas=new wxClientDC(this);
wxPoint position=event.GetLogicalPosition(wxClientDC(this));
Start.push_back(position);
assigned=true;
}
void MyFrame::OnMouseUp(wxMouseEvent& event)
{
wxPoint position=event.GetLogicalPosition(*Canvas);
End.push_back(position);
element++;
if(CurrentPen==wxID_SET_RED)
Canvas->SetPen(*wxRED_PEN);
else if(CurrentPen==wxID_SET_BLUE)
Canvas->SetPen(wxBLUE_PEN);
else if(CurrentPen==wxID_SET_GREEN)
Canvas->SetPen(*wxGREEN_PEN);
else if(CurrentPen==wxID_SET_CYAN)
Canvas->SetPen(*wxCYAN_PEN);
else
Canvas->SetPen(*wxBLACK_PEN);
Canvas->DrawLine(Start.at(element),End.at(element));
Pen.push_back(Canvas->GetPen());
}
void MyFrame::OnUndo(wxCommandEvent& event)
{
if(element<=-1)
return;
Canvas->Clear();
for(int i=0;i<element;i++)
{
Canvas->SetPen(Pen.at(i));
Canvas->DrawLine(Start.at(i),End.at(i));
}
Start.pop_back();
End.pop_back();
element--;
}
void MyFrame::OnOpenImage(wxCommandEvent& event)
{
if(assigned==false)
Canvas=new wxClientDC(this);
Memory=new wxMemoryDC;
wxFileDialog * FileDialog = new wxFileDialog(this);
if (FileDialog->ShowModal() == wxID_OK)
{
wxString FileName = FileDialog->GetPath();
if(FileLoaded==false)
Image=new wxBitmap;
if(Image->LoadFile(FileName,wxBITMAP_TYPE_ANY)==false)
{
wxMessageDialog *Error = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR);
Error->ShowModal();
delete Error;
delete FileDialog;
return;
}
}
Canvas->Clear();
for(;element>-1;element--)
{
Pen.pop_back();
Start.pop_back();
End.pop_back();
}
Memory->SelectObject(*Image);
Canvas->Blit(50, 0, Image->GetWidth(), Image->GetHeight(), Memory, 0, 0, wxCOPY, true);
FileLoaded=true;
delete FileDialog;
}
void MyFrame::OnClear(wxCommandEvent& event)
{
Canvas->Clear();
for(;element>-1;element--)
{
Pen.pop_back();
Start.pop_back();
End.pop_back();
}
}
bool MyApp::OnInit()
{
// create a new frame and set it as the top most application window
MyFrame *frame = new MyFrame(wxT("Paint"));
// show main frame
frame->Show(true);
// enter the application's main loop
return true;
}
MyFrame::MyFrame(const wxString& title, long style):
wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(1024,800))
{
CurrentPen=wxID_SET_BLACK;
assigned=false;
FileLoaded=false;
wxInitAllImageHandlers();
wxPanel *panel = new wxPanel(this);
new wxButton(panel,wxID_UNDO,wxT("Undo"),wxPoint(0,0),wxSize(50,20));
new wxButton(panel,wxID_OPEN,wxT("Open"),wxPoint(0,20),wxSize(50,20));
new wxButton(panel,wxID_CLEAR,wxT("Clear"),wxPoint(0,40),wxSize(50,20));
new wxButton(panel,wxID_SET_BLACK,wxT("Black"),wxPoint(0,60),wxSize(50,20));
new wxButton(panel,wxID_SET_RED,wxT("Red"),wxPoint(0,80),wxSize(50,20));
new wxButton(panel,wxID_SET_BLUE,wxT("Blue"),wxPoint(0,100),wxSize(50,20));
new wxButton(panel,wxID_SET_GREEN,wxT("Green"),wxPoint(0,120),wxSize(50,20));
new wxButton(panel,wxID_SET_CYAN,wxT("Cyan"),wxPoint(0,140),wxSize(50,20));
panel->Connect(wxEVT_LEFT_DOWN,wxMouseEventHandler(MyFrame::OnMouseDown));
panel->Connect(wxEVT_LEFT_UP,wxMouseEventHandler(MyFrame::OnMouseUp));
panel->Connect(wxID_SET_RED,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenRed));
panel->Connect(wxID_SET_BLUE,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenBlue));
panel->Connect(wxID_SET_GREEN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenGreen));
panel->Connect(wxID_SET_CYAN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenCyan));
panel->Connect(wxID_SET_BLACK,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenBlack));
panel->Connect(wxID_UNDO,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnUndo));
panel->Connect(wxID_OPEN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnOpenImage));
panel->Connect(wxID_CLEAR,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnClear));
}
|