|
From: Emilien K. <cur...@us...> - 2005-08-24 15:43:41
|
Update of /cvsroot/wxdevcenter/StdPlugin/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30456/src Modified Files: BitmapProp.cpp Log Message: Rewrite the unit system. Index: BitmapProp.cpp =================================================================== RCS file: /cvsroot/wxdevcenter/StdPlugin/src/BitmapProp.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BitmapProp.cpp 24 Aug 2005 10:55:34 -0000 1.1 --- BitmapProp.cpp 24 Aug 2005 15:43:30 -0000 1.2 *************** *** 13,21 **** ////////////////////////////////////////////////////////////////////// // BitmapProperties ////////////////////////////////////////////////////////////////////// // Default constructor. ! BitmapProperties::BitmapProperties():wxObject() { } --- 13,53 ---- ////////////////////////////////////////////////////////////////////// + // Unités de mesures. + ////////////////////////////////////////////////////////////////////// + + namespace StdPlugin { + namespace Bitmap { + namespace Units { + SizeUnit Twip = {wxT("Twip"), wxT("twp"), 1.0}; + SizeUnit Point = {wxT("Point"), wxT("pt"), 20.0}; + SizeUnit Pica = {wxT("Pica"), wxT("p"), 240.0}; + SizeUnit Inch = {wxT("Inch"), wxT("in"), 1440.0}; + SizeUnit Millimeter = {wxT("Millimeter"), wxT("mm"), 1440.0*1/25.4}; + SizeUnit Centimeter = {wxT("Centimeter"), wxT("cm"), 1440.0*1/2.54}; + SizeUnit Meter = {wxT("Meter"), wxT("m"), 1440.0*1/0.0254}; + }}} + + + static SizeUnit SizeUnits[]= + { + Units::Twip, + Units::Point, + Units::Pica, + Units::Inch, + Units::Millimeter, + Units::Centimeter, + Units::Meter + }; + + + ////////////////////////////////////////////////////////////////////// // BitmapProperties ////////////////////////////////////////////////////////////////////// // Default constructor. ! BitmapProperties::BitmapProperties(): ! wxObject(), ! m_Size(0, 0), ! m_dRes(72) { } *************** *** 25,127 **** wxObject(), m_Size(BmpProp.GetSize()), ! m_Ratio(BmpProp.GetRatio()) { } // Constructor. ! BitmapProperties::BitmapProperties(wxSize sz, wxRealPoint ratio): wxObject(), m_Size(sz), ! m_Ratio(ratio) { } - // Retourne la résolution dans l'unité de taille demandée. - wxRealPoint BitmapProperties::GetResolution(SizeUnit unit)const - { - wxRealPoint sz(GetSize().x, GetSize().y); - sz.x *= GetRatio().x; - sz.y *= GetRatio().y; - return ConvertUnit(sz, Inch, unit); - } ! // Fixe la taille depuis une résolution. ! void BitmapProperties::SetSize(wxRealPoint res, SizeUnit unit) { ! res.x /= GetRatio().x; ! res.y /= GetRatio().y; ! res = ConvertUnit(res, unit, Inch); ! SetSize(wxSize((wxCoord)res.x, (wxCoord)res.y)); } ! // Fixe le ratio depuis une résolution. ! void BitmapProperties::SetRatio(wxRealPoint res, SizeUnit unit) { ! res.x /= GetSize().x; ! res.y /= GetSize().y; ! res = ConvertUnit(res, unit, Inch); ! SetSize(wxSize((wxCoord)res.x, (wxCoord)res.y)); } - // Convertit une longueur d'une unité à une autre. - double BitmapProperties::ConvertUnit(double d, SizeUnit unitIn, SizeUnit unitOut)const - { - double coef = 1; ! switch(unitIn) ! { ! case Point: ! coef = 1.0; ! break; ! case Inch: ! coef = 72.0; ! break; ! case Twip: ! coef = 1.0/20.0; ! break; ! case Millimeter: ! coef = 72.0/25.4; ! break; ! case Centimeter: ! coef = 72.0/2.54; ! break; ! case Meter: ! coef = 72.0/0.0254; ! break; ! default: ! break; ! } ! switch(unitOut) ! { ! case Point: ! coef = 1.0; ! break; ! case Inch: ! coef = 1/72.0; ! break; ! case Twip: ! coef = 20.0; ! break; ! case Millimeter: ! coef = 25.4/72.0; ! break; ! case Centimeter: ! coef = 2.54/72.0; ! break; ! case Meter: ! coef = 0.0254/72.0; ! break; ! default: ! break; ! } - return d*coef; } ! // Convertit une Taille d'une unité à une autre. ! wxRealPoint BitmapProperties::ConvertUnit(wxRealPoint pt, SizeUnit unitIn, SizeUnit unitOut)const { ! return wxRealPoint(ConvertUnit(pt.x, unitIn, unitOut), ConvertUnit(pt.y, unitIn, unitOut)); } --- 57,100 ---- wxObject(), m_Size(BmpProp.GetSize()), ! m_dRes(BmpProp.GetRes()) { } // Constructor. ! BitmapProperties::BitmapProperties(wxSize sz, double dRes): wxObject(), m_Size(sz), ! m_dRes(dRes) { } ! // Retourne la taille dans l'unité de taille demandée. ! wxRealPoint BitmapProperties::GetSize(const SizeUnit& unit)const { ! return wxRealPoint(SizeUnit::Convert(GetSize().x/GetRes(), Units::Point, unit), ! SizeUnit::Convert(GetSize().y/GetRes(), Units::Point, unit)); } ! // Retourne la résolution dans l'unité de taille demandée. ! double BitmapProperties::GetRes(const SizeUnit& unit)const { ! return SizeUnit::Convert(GetRes(), Units::Point, unit); } ! // Fixe la taille. ! void BitmapProperties::SetSize(wxRealPoint size, const SizeUnit& unit) ! { ! SetSize(wxSize( (wxCoord) (SizeUnit::Convert(size.x, unit, Units::Point) * GetRes()) , ! (wxCoord) (SizeUnit::Convert(size.y, unit, Units::Point) * GetRes()) )); } ! // Fixe la résolution. ! void BitmapProperties::SetRes(double dRes, const SizeUnit& unit) { ! SetRes(SizeUnit::Convert(dRes, unit, Units::Point)); } *************** *** 132,174 **** ////////////////////////////////////////////////////////////////////// BEGIN_EVENT_TABLE(BitmapPropertiesDialog, wxDialog) END_EVENT_TABLE() - - /** Type of size unit.*/ - static wxString s_strSizeUnit[]={ - "Point", - "Millimeter", - "Centimeter", - "Meter", - "Inch", - "Twip", - "Pixel" - }; - - /** Type of resolution unit.*/ - static wxString s_strResUnit[]={ - "Pixel/Point", - "Pixel/Millimeter", - "Pixel/Centimeter", - "Pixel/Meter", - "Pixel/Inch", - "Pixel/Twip", - }; - - // Standard constructor. ! BitmapPropertiesDialog::BitmapPropertiesDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size): ! wxDialog(parent, id, title, pos, size) { // Création des controles. ! m_pWidthTextCtrl = new wxTextCtrl(this, -1, wxT("21.0")); ! m_pHeightTextCtrl = new wxTextCtrl(this, -1, wxT("29.7")); ! m_pUnitChoice = new wxChoice(this, -1, wxDefaultPosition, wxDefaultSize, 7, s_strSizeUnit); ! m_pSizeTextCtrl = new wxStaticText(this, -1, wxT("")); ! m_pHResTextCtrl = new wxTextCtrl(this, -1, wxT("72.0")); ! m_pVResTextCtrl = new wxTextCtrl(this, -1, wxT("72.0")); ! m_pResUnitChoice = new wxChoice(this, -1, wxDefaultPosition, wxDefaultSize, 6, s_strResUnit); --- 105,137 ---- ////////////////////////////////////////////////////////////////////// + #define ID_WIDTH_TEXT_CTRL 20 + #define ID_HEIGHT_TEXT_CTRL 21 + #define ID_UNIT_CHOICE 22 + #define ID_SIZETEXT_CTRL 23 + #define ID_RES_TEXT_CTRL 24 + #define ID_RESUNIT_CHOICE 25 + BEGIN_EVENT_TABLE(BitmapPropertiesDialog, wxDialog) + EVT_INIT_DIALOG(BitmapPropertiesDialog::OnInitialUpdate) + + EVT_TEXT(ID_WIDTH_TEXT_CTRL, BitmapPropertiesDialog::OnSizeChanged) + EVT_TEXT(ID_HEIGHT_TEXT_CTRL, BitmapPropertiesDialog::OnSizeChanged) + + EVT_CHOICE(ID_UNIT_CHOICE, BitmapPropertiesDialog::OnChangeUnit) + EVT_CHOICE(ID_RESUNIT_CHOICE, BitmapPropertiesDialog::OnChangeUnit) END_EVENT_TABLE() // Standard constructor. ! BitmapPropertiesDialog::BitmapPropertiesDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name): ! wxDialog(parent, id, title, pos, size, style, name) { // Création des controles. ! m_pWidthTextCtrl = new wxTextCtrl(this, ID_WIDTH_TEXT_CTRL, wxT("21.0")); ! m_pHeightTextCtrl = new wxTextCtrl(this, ID_HEIGHT_TEXT_CTRL, wxT("29.7")); ! m_pUnitChoice = new wxChoice(this, ID_UNIT_CHOICE, wxDefaultPosition, wxDefaultSize, 0, NULL); ! m_pSizeTextCtrl = new wxStaticText(this, ID_SIZETEXT_CTRL, wxT("")); ! m_pResTextCtrl = new wxTextCtrl(this, ID_RES_TEXT_CTRL, wxT("72.0")); ! m_pResUnitChoice = new wxChoice(this, ID_RESUNIT_CHOICE, wxDefaultPosition, wxDefaultSize, 0, NULL); *************** *** 192,201 **** // Zone de résolution wxSizer* pResSizer = new wxStaticBoxSizer(wxVERTICAL, this, wxT("Résolution")); ! pLineSizer = new wxGridSizer(2, 3, 8, 8); ! pLineSizer->Add(new wxStaticText(this, -1, wxT("Resolution X : ")), 0, wxEXPAND|wxALL, 4); ! pLineSizer->Add(m_pHResTextCtrl, 0, wxEXPAND|wxALL, 4); ! pLineSizer->AddStretchSpacer(1); ! pLineSizer->Add(new wxStaticText(this, -1, wxT("Resolution Y : ")), 0, wxEXPAND|wxALL, 4); ! pLineSizer->Add(m_pVResTextCtrl, 0, wxEXPAND|wxALL, 4); pLineSizer->Add(m_pResUnitChoice, 0, wxEXPAND|wxALL, 4); pResSizer->Add(pLineSizer, 0, wxEXPAND|wxALL, 4); --- 155,161 ---- // Zone de résolution wxSizer* pResSizer = new wxStaticBoxSizer(wxVERTICAL, this, wxT("Résolution")); ! pLineSizer = new wxGridSizer(1, 3, 8, 8); ! pLineSizer->Add(new wxStaticText(this, -1, wxT("Resolution : ")), 0, wxEXPAND|wxALL, 4); ! pLineSizer->Add(m_pResTextCtrl, 0, wxEXPAND|wxALL, 4); pLineSizer->Add(m_pResUnitChoice, 0, wxEXPAND|wxALL, 4); pResSizer->Add(pLineSizer, 0, wxEXPAND|wxALL, 4); *************** *** 208,209 **** --- 168,272 ---- } + // Mise à jour initiale. + void BitmapPropertiesDialog::OnInitialUpdate(wxInitDialogEvent& WXUNUSED(event)) + { + long l, nb = sizeof(SizeUnits)/sizeof(SizeUnit); + + // Ajoute les unités de taille. + m_pUnitChoice->Append(wxT("Pixel"), (void*)NULL); + for(l=0; l<nb; l++) + m_pUnitChoice->Append(SizeUnits[l].name, (void*)&SizeUnits[l]); + m_pUnitChoice->SetSelection(0); + + // Ajoute les unités de résolution. + for(l=0; l<nb; l++) + m_pResUnitChoice->Append(wxT("Pixel/") + SizeUnits[l].name, (void*)&SizeUnits[l]); + m_pResUnitChoice->SetSelection(3); + + UpdateCtrl(); + } + + // Met à jour les controles. + void BitmapPropertiesDialog::UpdateCtrl(bool bUpdateSize, bool bUpdateRes) + { + int id; + SizeUnit* pUnit; + + // Zone de taille. + if(bUpdateSize) + { + id = m_pUnitChoice->GetSelection(); + if(id==wxNOT_FOUND || id==0) + { + m_pWidthTextCtrl->SetValue(wxString::Format(wxT("%d"),m_BmpProp.GetSize().x)); + m_pHeightTextCtrl->SetValue(wxString::Format(wxT("%d"),m_BmpProp.GetSize().y)); + } + else + { + pUnit = (SizeUnit*) m_pUnitChoice->GetClientData(id); + m_pWidthTextCtrl->SetValue(wxString::Format(wxT("%g"),m_BmpProp.GetSize(*pUnit).x)); + m_pHeightTextCtrl->SetValue(wxString::Format(wxT("%g"),m_BmpProp.GetSize(*pUnit).y)); + } + } + + // Zone de résolution. + if(bUpdateRes) + { + id = m_pResUnitChoice->GetSelection(); + if(id==wxNOT_FOUND) + { + m_pResTextCtrl->SetValue(wxT("###")); + } + else + { + pUnit = (SizeUnit*) m_pResUnitChoice->GetClientData(id); + m_pResTextCtrl->SetValue(wxString::Format(wxT("%g"),m_BmpProp.GetRes(*pUnit))); + } + } + + // Récapitulatif de taille. + m_pSizeTextCtrl->SetLabel(wxString::Format(wxT("%dx%d pixels - %gppp"), m_BmpProp.GetSize().x,m_BmpProp.GetSize().y, m_BmpProp.GetRes())); + + } + + // Interception d'un changement d'unté d'affichage. + void BitmapPropertiesDialog::OnChangeUnit(wxCommandEvent& WXUNUSED(event)) + { + UpdateCtrl(); + } + + // Interception d'un changement de taille. + void BitmapPropertiesDialog::OnSizeChanged(wxCommandEvent& WXUNUSED(event)) + { + SizeUnit* pUnit; + int id = m_pUnitChoice->GetSelection(); + + if(id==wxNOT_FOUND || id==0) + { + long lX, lY; + if(!m_pWidthTextCtrl->GetValue().ToLong(&lX, 0)) + lX = m_BmpProp.GetSize().x; + if(!m_pHeightTextCtrl->GetValue().ToLong(&lY, 0)) + lY = m_BmpProp.GetSize().y; + m_BmpProp.SetSize(wxSize(lX, lY)); + } + else + { + double dX, dY; + pUnit = (SizeUnit*) m_pUnitChoice->GetClientData(id); + if(!m_pWidthTextCtrl->GetValue().ToDouble(&dX)) + dX = m_BmpProp.GetSize(*pUnit).x; + if(!m_pHeightTextCtrl->GetValue().ToDouble(&dY)) + dY = m_BmpProp.GetSize(*pUnit).y; + m_BmpProp.SetSize(wxRealPoint(dX, dY), *pUnit); + } + + UpdateCtrl(false, true); + } + + // Interception d'un changement de résolution. + void BitmapPropertiesDialog::OnResChanged(wxCommandEvent& event) + { + UpdateCtrl(true, false); + } + |