1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextimagedlg.cpp
4 // Author: Mingquan Yang
6 // Created: Wed 02 Jun 2010 11:27:23 CST
8 // Copyright: (c) Mingquan Yang
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/button.h"
24 #include "wx/combobox.h"
26 #include "wx/stattext.h"
27 #include "wx/textctrl.h"
30 #include "wx/statline.h"
32 #include "wx/richtext/richtextimagedlg.h"
33 #include "wx/richtext/richtextctrl.h"
40 * wxRichTextImageDialog type definition
43 IMPLEMENT_DYNAMIC_CLASS( wxRichTextImageDialog
, wxDialog
)
47 * wxRichTextImageDialog event table definition
50 BEGIN_EVENT_TABLE( wxRichTextImageDialog
, wxDialog
)
52 ////@begin wxRichTextImageDialog event table entries
53 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_PARA_UP
, wxRichTextImageDialog::OnRichtextimagedialogParaUpClick
)
55 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_DOWN
, wxRichTextImageDialog::OnRichtextimagedialogDownClick
)
57 ////@end wxRichTextImageDialog event table entries
63 * wxRichTextImageDialog constructors
66 wxRichTextImageDialog::wxRichTextImageDialog()
71 wxRichTextImageDialog::wxRichTextImageDialog( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
74 Create(parent
, id
, caption
, pos
, size
, style
);
79 * wxRichTextImageDlg creator
82 bool wxRichTextImageDialog::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
84 ////@begin wxRichTextImageDialog creation
85 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
86 wxDialog::Create( parent
, id
, caption
, pos
, size
, style
);
91 GetSizer()->SetSizeHints(this);
94 ////@end wxRichTextImageDialog creation
100 * wxRichTextImageDialog destructor
103 wxRichTextImageDialog::~wxRichTextImageDialog()
105 ////@begin wxRichTextImageDialog destruction
106 ////@end wxRichTextImageDialog destruction
111 * Member initialisation
114 void wxRichTextImageDialog::Init()
116 ////@begin wxRichTextImageDialog member initialisation
123 m_unitsOffset
= NULL
;
125 m_cancelButton
= NULL
;
126 ////@end wxRichTextImageDialog member initialisation
131 * Control creation for wxRichTextImageDlg
134 void wxRichTextImageDialog::CreateControls()
137 SetWindowVariant(wxWINDOW_VARIANT_SMALL
);
140 ////@begin wxRichTextImageDialog content construction
141 wxRichTextImageDialog
* itemDialog1
= this;
143 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
144 itemDialog1
->SetSizer(itemBoxSizer2
);
146 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxHORIZONTAL
);
147 itemBoxSizer2
->Add(itemBoxSizer3
, 0, wxGROW
|wxALL
, 5);
149 wxFlexGridSizer
* itemFlexGridSizer4
= new wxFlexGridSizer(0, 2, 0, 0);
150 itemBoxSizer3
->Add(itemFlexGridSizer4
, 0, wxALIGN_TOP
|wxRIGHT
, 5);
152 wxStaticText
* itemStaticText5
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Floating mode:"), wxDefaultPosition
, wxDefaultSize
, 0 );
153 itemFlexGridSizer4
->Add(itemStaticText5
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
155 wxArrayString m_floatStrings
;
156 m_floatStrings
.Add(_("None"));
157 m_floatStrings
.Add(_("Left"));
158 m_floatStrings
.Add(_("Right"));
159 m_float
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_FLOATING_MODE
, _("None"), wxDefaultPosition
, wxSize(80, -1), m_floatStrings
, wxCB_READONLY
);
160 m_float
->SetStringSelection(_("None"));
161 m_float
->SetHelpText(_("How the image will float relative to the text."));
162 if (wxRichTextImageDialog::ShowToolTips())
163 m_float
->SetToolTip(_("How the image will float relative to the text."));
164 itemFlexGridSizer4
->Add(m_float
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
166 wxStaticText
* itemStaticText7
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Width:"), wxDefaultPosition
, wxDefaultSize
, 0 );
167 itemFlexGridSizer4
->Add(itemStaticText7
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
169 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxHORIZONTAL
);
170 itemFlexGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
172 m_width
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_WIDTH
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
173 m_width
->SetHelpText(_("The image width to be shown - does not change the source image width."));
174 if (wxRichTextImageDialog::ShowToolTips())
175 m_width
->SetToolTip(_("The image width to be shown - does not change the source image width."));
176 itemBoxSizer8
->Add(m_width
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
178 wxArrayString m_unitsWStrings
;
179 m_unitsWStrings
.Add(_("px"));
180 m_unitsWStrings
.Add(_("cm"));
181 m_unitsW
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_W
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsWStrings
, wxCB_READONLY
);
182 m_unitsW
->SetStringSelection(_("px"));
183 m_unitsW
->SetHelpText(_("Units for the image width."));
184 if (wxRichTextImageDialog::ShowToolTips())
185 m_unitsW
->SetToolTip(_("Units for the image width."));
186 itemBoxSizer8
->Add(m_unitsW
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
188 wxStaticText
* itemStaticText11
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Height:"), wxDefaultPosition
, wxDefaultSize
, 0 );
189 itemFlexGridSizer4
->Add(itemStaticText11
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
191 wxBoxSizer
* itemBoxSizer12
= new wxBoxSizer(wxHORIZONTAL
);
192 itemFlexGridSizer4
->Add(itemBoxSizer12
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
194 m_height
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_HEIGHT
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
195 m_height
->SetHelpText(_("The image height to be shown - does not change the source image height."));
196 if (wxRichTextImageDialog::ShowToolTips())
197 m_height
->SetToolTip(_("The image height to be shown - does not change the source image height."));
198 itemBoxSizer12
->Add(m_height
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
200 wxArrayString m_unitsHStrings
;
201 m_unitsHStrings
.Add(_("px"));
202 m_unitsHStrings
.Add(_("cm"));
203 m_unitsH
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_H
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsHStrings
, wxCB_READONLY
);
204 m_unitsH
->SetStringSelection(_("px"));
205 m_unitsH
->SetHelpText(_("Units for the image height."));
206 if (wxRichTextImageDialog::ShowToolTips())
207 m_unitsH
->SetToolTip(_("Units for the image height."));
208 itemBoxSizer12
->Add(m_unitsH
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
210 wxStaticText
* itemStaticText15
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Image Vertical &Offset:"), wxDefaultPosition
, wxDefaultSize
, 0 );
211 itemFlexGridSizer4
->Add(itemStaticText15
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
213 wxBoxSizer
* itemBoxSizer16
= new wxBoxSizer(wxHORIZONTAL
);
214 itemFlexGridSizer4
->Add(itemBoxSizer16
, 1, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
216 m_offset
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
217 m_offset
->SetMaxLength(10);
218 m_offset
->SetHelpText(_("The vertical offset relative to the paragraph."));
219 if (wxRichTextImageDialog::ShowToolTips())
220 m_offset
->SetToolTip(_("The vertical offset relative to the paragraph."));
221 itemBoxSizer16
->Add(m_offset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
223 wxArrayString m_unitsOffsetStrings
;
224 m_unitsOffsetStrings
.Add(_("px"));
225 m_unitsOffsetStrings
.Add(_("cm"));
226 m_unitsOffset
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET_UNITS
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsOffsetStrings
, wxCB_READONLY
);
227 m_unitsOffset
->SetStringSelection(_("px"));
228 m_unitsOffset
->SetHelpText(_("Units for the image offset."));
229 if (wxRichTextImageDialog::ShowToolTips())
230 m_unitsOffset
->SetToolTip(_("Units for the image offset."));
231 itemBoxSizer16
->Add(m_unitsOffset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
233 wxStaticText
* itemStaticText19
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Move the image to:"), wxDefaultPosition
, wxDefaultSize
, 0 );
234 itemFlexGridSizer4
->Add(itemStaticText19
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
236 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxHORIZONTAL
);
237 itemFlexGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
239 wxButton
* itemButton21
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_PARA_UP
, _("&Previous Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
240 itemButton21
->SetHelpText(_("Moves the image to the previous paragraph."));
241 if (wxRichTextImageDialog::ShowToolTips())
242 itemButton21
->SetToolTip(_("Moves the image to the previous paragraph."));
243 itemBoxSizer20
->Add(itemButton21
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
245 wxButton
* itemButton22
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_DOWN
, _("&Next Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
246 itemButton22
->SetHelpText(_("Moves the image to the next paragraph."));
247 if (wxRichTextImageDialog::ShowToolTips())
248 itemButton22
->SetToolTip(_("Moves the image to the next paragraph."));
249 itemBoxSizer20
->Add(itemButton22
, 0, wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5);
251 wxStaticLine
* itemStaticLine23
= new wxStaticLine( itemDialog1
, wxID_STATIC
, wxDefaultPosition
, wxDefaultSize
, wxLI_HORIZONTAL
);
252 itemBoxSizer2
->Add(itemStaticLine23
, 0, wxGROW
|wxALL
, 5);
254 wxStdDialogButtonSizer
* itemStdDialogButtonSizer24
= new wxStdDialogButtonSizer
;
256 itemBoxSizer2
->Add(itemStdDialogButtonSizer24
, 0, wxGROW
|wxALL
, 5);
257 m_saveButton
= new wxButton( itemDialog1
, wxID_OK
, _("OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
258 m_saveButton
->SetHelpText(_("Click to confirm your changes."));
259 if (wxRichTextImageDialog::ShowToolTips())
260 m_saveButton
->SetToolTip(_("Click to confirm your changes."));
261 itemStdDialogButtonSizer24
->AddButton(m_saveButton
);
263 m_cancelButton
= new wxButton( itemDialog1
, wxID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
264 m_cancelButton
->SetHelpText(_("Click to discard your changes."));
265 if (wxRichTextImageDialog::ShowToolTips())
266 m_cancelButton
->SetToolTip(_("Click to discard your changes."));
267 itemStdDialogButtonSizer24
->AddButton(m_cancelButton
);
269 itemStdDialogButtonSizer24
->Realize();
271 ////@end wxRichTextImageDialog content construction
276 * Should we show tooltips?
279 bool wxRichTextImageDialog::ShowToolTips()
285 * Get bitmap resources
288 wxBitmap
wxRichTextImageDialog::GetBitmapResource( const wxString
& name
)
291 ////@begin wxRichTextImageDialog bitmap retrieval
294 ////@end wxRichTextImageDialog bitmap retrieval
301 wxIcon
wxRichTextImageDialog::GetIconResource( const wxString
& name
)
304 ////@begin wxRichTextImageDialog icon retrieval
307 ////@end wxRichTextImageDialog icon retrieval
311 * Set the image attribute
313 void wxRichTextImageDialog::SetImageAttr(const wxRichTextAttr
& textAttr
)
315 m_textAttr
= textAttr
;
317 TransferDataToWindow();
321 * Apply the new style
323 wxRichTextImage
* wxRichTextImageDialog::ApplyImageAttr()
325 wxRichTextImage
* image
= wxDynamicCast(m_image
, wxRichTextImage
);
327 TransferDataFromWindow();
328 if (m_buffer
->GetRichTextCtrl())
330 m_buffer
->GetRichTextCtrl()->SetImageStyle(image
, m_textAttr
);
335 void wxRichTextImageDialog::SetImageObject(wxRichTextImage
* image
, wxRichTextBuffer
* buffer
)
337 wxRichTextObject
* parent
= image
->GetParent();
342 SetImageAttr(image
->GetAttributes());
343 if (image
->GetImageCache().IsOk())
345 if (!m_textAttr
.GetTextBoxAttr().GetWidth().IsPresent() || m_textAttr
.GetTextBoxAttr().GetWidth().GetValue() <= 0)
347 m_textAttr
.GetTextBoxAttr().GetWidth().SetValue(image
->GetImageCache().GetWidth());
348 m_textAttr
.GetTextBoxAttr().GetWidth().SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
350 if (!m_textAttr
.GetTextBoxAttr().GetHeight().IsPresent() || m_textAttr
.GetTextBoxAttr().GetHeight().GetValue() <= 0)
352 m_textAttr
.GetTextBoxAttr().GetHeight().SetValue(image
->GetImageCache().GetHeight());
353 m_textAttr
.GetTextBoxAttr().GetHeight().SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
358 void wxRichTextImageDialog::SetDimensionValue(wxTextAttrDimension
& dim
, wxTextCtrl
* valueCtrl
, wxComboBox
* unitsCtrl
)
362 if (!dim
.IsPresent())
365 dim
.SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
368 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
371 float value
= float(dim
.GetValue()) / 10.0;
372 valueCtrl
->SetValue(wxString::Format(wxT("%.2f"), value
));
377 valueCtrl
->SetValue(wxString::Format(wxT("%d"), (int) dim
.GetValue()));
380 unitsCtrl
->SetSelection(unitsIdx
);
383 void wxRichTextImageDialog::GetDimensionValue(wxTextAttrDimension
& dim
, wxTextCtrl
* valueCtrl
, wxComboBox
* unitsCtrl
)
385 if (unitsCtrl
->GetSelection() == 1)
386 dim
.SetUnits(wxTEXT_ATTR_UNITS_TENTHS_MM
);
388 dim
.SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
391 if (ConvertFromString(valueCtrl
->GetValue(), value
, dim
.GetUnits()))
395 bool wxRichTextImageDialog::TransferDataToWindow()
397 m_float
->SetSelection(m_textAttr
.GetTextBoxAttr().GetFloatMode());
399 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetWidth(), m_width
, m_unitsW
);
400 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetHeight(), m_height
, m_unitsH
);
401 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetTop(), m_offset
, m_unitsOffset
);
406 bool wxRichTextImageDialog::TransferDataFromWindow()
408 m_textAttr
.GetTextBoxAttr().SetFloatMode(m_float
->GetSelection());
410 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetWidth(), m_width
, m_unitsW
);
411 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetHeight(), m_height
, m_unitsH
);
412 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetTop(), m_offset
, m_unitsOffset
);
417 bool wxRichTextImageDialog::ConvertFromString(const wxString
& string
, int& ret
, int scale
)
419 const wxChar
* chars
= string
.GetData();
424 for (unsigned int i
= 0; i
< string
.Len() && remain
; i
++)
426 if (!(chars
[i
] >= '0' && chars
[i
] <= '9') && !(scale
== wxTEXT_ATTR_UNITS_TENTHS_MM
&& chars
[i
] == '.'))
438 ret
= ret
* 10 + chars
[i
] - '0';
441 while (remain
-- > 0 && scale
== wxTEXT_ATTR_UNITS_TENTHS_MM
)
449 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_UP
451 void wxRichTextImageDialog::OnRichtextimagedialogParaUpClick( wxCommandEvent
& WXUNUSED(event
))
453 // Before editing this code, remove the block markers.
454 wxRichTextRange range
= m_image
->GetRange();
455 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
461 if (iter
->GetData() == m_parent
)
463 iter
= iter
->GetNext();
466 iter
= iter
->GetPrevious();
470 wxRichTextObject
*obj
= iter
->GetData();
471 wxRichTextRange rg
= obj
->GetRange();
472 m_image
= m_image
->Clone();
474 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
475 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
477 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));
482 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_DOWN
485 void wxRichTextImageDialog::OnRichtextimagedialogDownClick( wxCommandEvent
& WXUNUSED(event
))
487 // Before editing this code, remove the block markers.
488 wxRichTextRange range
= m_image
->GetRange();
489 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
495 if (iter
->GetData() == m_parent
)
497 iter
= iter
->GetNext();
500 iter
= iter
->GetNext();
504 wxRichTextObject
*obj
= iter
->GetData();
505 wxRichTextRange rg
= obj
->GetRange();
506 m_image
= m_image
->Clone();
508 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
509 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
511 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));