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"
20 #include "wx/button.h"
21 #include "wx/combobox.h"
23 #include "wx/stattext.h"
24 #include "wx/textctrl.h"
27 #include "wx/statline.h"
29 #include "wx/richtext/richtextimagedlg.h"
30 #include "wx/richtext/richtextctrl.h"
37 * wxRichTextImageDialog type definition
40 IMPLEMENT_DYNAMIC_CLASS( wxRichTextImageDialog
, wxDialog
)
44 * wxRichTextImageDialog event table definition
47 BEGIN_EVENT_TABLE( wxRichTextImageDialog
, wxDialog
)
49 ////@begin wxRichTextImageDialog event table entries
50 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_PARA_UP
, wxRichTextImageDialog::OnRichtextimagedialogParaUpClick
)
52 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_DOWN
, wxRichTextImageDialog::OnRichtextimagedialogDownClick
)
54 ////@end wxRichTextImageDialog event table entries
60 * wxRichTextImageDialog constructors
63 wxRichTextImageDialog::wxRichTextImageDialog()
68 wxRichTextImageDialog::wxRichTextImageDialog( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
71 Create(parent
, id
, caption
, pos
, size
, style
);
76 * wxRichTextImageDlg creator
79 bool wxRichTextImageDialog::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
81 ////@begin wxRichTextImageDialog creation
82 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
83 wxDialog::Create( parent
, id
, caption
, pos
, size
, style
);
88 GetSizer()->SetSizeHints(this);
91 ////@end wxRichTextImageDialog creation
97 * wxRichTextImageDialog destructor
100 wxRichTextImageDialog::~wxRichTextImageDialog()
102 ////@begin wxRichTextImageDialog destruction
103 ////@end wxRichTextImageDialog destruction
108 * Member initialisation
111 void wxRichTextImageDialog::Init()
113 ////@begin wxRichTextImageDialog member initialisation
120 m_unitsOffset
= NULL
;
122 m_cancelButton
= NULL
;
123 ////@end wxRichTextImageDialog member initialisation
128 * Control creation for wxRichTextImageDlg
131 void wxRichTextImageDialog::CreateControls()
134 SetWindowVariant(wxWINDOW_VARIANT_SMALL
);
137 ////@begin wxRichTextImageDialog content construction
138 wxRichTextImageDialog
* itemDialog1
= this;
140 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
141 itemDialog1
->SetSizer(itemBoxSizer2
);
143 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxHORIZONTAL
);
144 itemBoxSizer2
->Add(itemBoxSizer3
, 0, wxGROW
|wxALL
, 5);
146 wxFlexGridSizer
* itemFlexGridSizer4
= new wxFlexGridSizer(0, 2, 0, 0);
147 itemBoxSizer3
->Add(itemFlexGridSizer4
, 0, wxALIGN_TOP
|wxRIGHT
, 5);
149 wxStaticText
* itemStaticText5
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Floating mode:"), wxDefaultPosition
, wxDefaultSize
, 0 );
150 itemFlexGridSizer4
->Add(itemStaticText5
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
152 wxArrayString m_floatStrings
;
153 m_floatStrings
.Add(_("None"));
154 m_floatStrings
.Add(_("Left"));
155 m_floatStrings
.Add(_("Right"));
156 m_float
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_FLOATING_MODE
, _("None"), wxDefaultPosition
, wxSize(80, -1), m_floatStrings
, wxCB_READONLY
);
157 m_float
->SetStringSelection(_("None"));
158 m_float
->SetHelpText(_("How the image will float relative to the text."));
159 if (wxRichTextImageDialog::ShowToolTips())
160 m_float
->SetToolTip(_("How the image will float relative to the text."));
161 itemFlexGridSizer4
->Add(m_float
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
163 wxStaticText
* itemStaticText7
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Width:"), wxDefaultPosition
, wxDefaultSize
, 0 );
164 itemFlexGridSizer4
->Add(itemStaticText7
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
166 wxBoxSizer
* itemBoxSizer8
= new wxBoxSizer(wxHORIZONTAL
);
167 itemFlexGridSizer4
->Add(itemBoxSizer8
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
169 m_width
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_WIDTH
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
170 m_width
->SetHelpText(_("The image width to be shown - does not change the source image width."));
171 if (wxRichTextImageDialog::ShowToolTips())
172 m_width
->SetToolTip(_("The image width to be shown - does not change the source image width."));
173 itemBoxSizer8
->Add(m_width
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
175 wxArrayString m_unitsWStrings
;
176 m_unitsWStrings
.Add(_("px"));
177 m_unitsWStrings
.Add(_("cm"));
178 m_unitsW
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_W
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsWStrings
, wxCB_READONLY
);
179 m_unitsW
->SetStringSelection(_("px"));
180 m_unitsW
->SetHelpText(_("Units for the image width."));
181 if (wxRichTextImageDialog::ShowToolTips())
182 m_unitsW
->SetToolTip(_("Units for the image width."));
183 itemBoxSizer8
->Add(m_unitsW
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
185 wxStaticText
* itemStaticText11
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Height:"), wxDefaultPosition
, wxDefaultSize
, 0 );
186 itemFlexGridSizer4
->Add(itemStaticText11
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
188 wxBoxSizer
* itemBoxSizer12
= new wxBoxSizer(wxHORIZONTAL
);
189 itemFlexGridSizer4
->Add(itemBoxSizer12
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
191 m_height
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_HEIGHT
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
192 m_height
->SetHelpText(_("The image height to be shown - does not change the source image height."));
193 if (wxRichTextImageDialog::ShowToolTips())
194 m_height
->SetToolTip(_("The image height to be shown - does not change the source image height."));
195 itemBoxSizer12
->Add(m_height
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
197 wxArrayString m_unitsHStrings
;
198 m_unitsHStrings
.Add(_("px"));
199 m_unitsHStrings
.Add(_("cm"));
200 m_unitsH
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_H
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsHStrings
, wxCB_READONLY
);
201 m_unitsH
->SetStringSelection(_("px"));
202 m_unitsH
->SetHelpText(_("Units for the image height."));
203 if (wxRichTextImageDialog::ShowToolTips())
204 m_unitsH
->SetToolTip(_("Units for the image height."));
205 itemBoxSizer12
->Add(m_unitsH
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
207 wxStaticText
* itemStaticText15
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Image Vertical &Offset:"), wxDefaultPosition
, wxDefaultSize
, 0 );
208 itemFlexGridSizer4
->Add(itemStaticText15
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
210 wxBoxSizer
* itemBoxSizer16
= new wxBoxSizer(wxHORIZONTAL
);
211 itemFlexGridSizer4
->Add(itemBoxSizer16
, 1, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
213 m_offset
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
214 m_offset
->SetMaxLength(10);
215 m_offset
->SetHelpText(_("The vertical offset relative to the paragraph."));
216 if (wxRichTextImageDialog::ShowToolTips())
217 m_offset
->SetToolTip(_("The vertical offset relative to the paragraph."));
218 itemBoxSizer16
->Add(m_offset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
220 wxArrayString m_unitsOffsetStrings
;
221 m_unitsOffsetStrings
.Add(_("px"));
222 m_unitsOffsetStrings
.Add(_("cm"));
223 m_unitsOffset
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET_UNITS
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsOffsetStrings
, wxCB_READONLY
);
224 m_unitsOffset
->SetStringSelection(_("px"));
225 m_unitsOffset
->SetHelpText(_("Units for the image offset."));
226 if (wxRichTextImageDialog::ShowToolTips())
227 m_unitsOffset
->SetToolTip(_("Units for the image offset."));
228 itemBoxSizer16
->Add(m_unitsOffset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
230 wxStaticText
* itemStaticText19
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Move the image to:"), wxDefaultPosition
, wxDefaultSize
, 0 );
231 itemFlexGridSizer4
->Add(itemStaticText19
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
233 wxBoxSizer
* itemBoxSizer20
= new wxBoxSizer(wxHORIZONTAL
);
234 itemFlexGridSizer4
->Add(itemBoxSizer20
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
236 wxButton
* itemButton21
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_PARA_UP
, _("&Previous Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
237 itemButton21
->SetHelpText(_("Moves the image to the previous paragraph."));
238 if (wxRichTextImageDialog::ShowToolTips())
239 itemButton21
->SetToolTip(_("Moves the image to the previous paragraph."));
240 itemBoxSizer20
->Add(itemButton21
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
242 wxButton
* itemButton22
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_DOWN
, _("&Next Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
243 itemButton22
->SetHelpText(_("Moves the image to the next paragraph."));
244 if (wxRichTextImageDialog::ShowToolTips())
245 itemButton22
->SetToolTip(_("Moves the image to the next paragraph."));
246 itemBoxSizer20
->Add(itemButton22
, 0, wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5);
248 wxStaticLine
* itemStaticLine23
= new wxStaticLine( itemDialog1
, wxID_STATIC
, wxDefaultPosition
, wxDefaultSize
, wxLI_HORIZONTAL
);
249 itemBoxSizer2
->Add(itemStaticLine23
, 0, wxGROW
|wxALL
, 5);
251 wxStdDialogButtonSizer
* itemStdDialogButtonSizer24
= new wxStdDialogButtonSizer
;
253 itemBoxSizer2
->Add(itemStdDialogButtonSizer24
, 0, wxGROW
|wxALL
, 5);
254 m_saveButton
= new wxButton( itemDialog1
, wxID_OK
, _("OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
255 m_saveButton
->SetHelpText(_("Click to confirm your changes."));
256 if (wxRichTextImageDialog::ShowToolTips())
257 m_saveButton
->SetToolTip(_("Click to confirm your changes."));
258 itemStdDialogButtonSizer24
->AddButton(m_saveButton
);
260 m_cancelButton
= new wxButton( itemDialog1
, wxID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
261 m_cancelButton
->SetHelpText(_("Click to discard your changes."));
262 if (wxRichTextImageDialog::ShowToolTips())
263 m_cancelButton
->SetToolTip(_("Click to discard your changes."));
264 itemStdDialogButtonSizer24
->AddButton(m_cancelButton
);
266 itemStdDialogButtonSizer24
->Realize();
268 ////@end wxRichTextImageDialog content construction
273 * Should we show tooltips?
276 bool wxRichTextImageDialog::ShowToolTips()
282 * Get bitmap resources
285 wxBitmap
wxRichTextImageDialog::GetBitmapResource( const wxString
& name
)
288 ////@begin wxRichTextImageDialog bitmap retrieval
291 ////@end wxRichTextImageDialog bitmap retrieval
298 wxIcon
wxRichTextImageDialog::GetIconResource( const wxString
& name
)
301 ////@begin wxRichTextImageDialog icon retrieval
304 ////@end wxRichTextImageDialog icon retrieval
308 * Set the image attribute
310 void wxRichTextImageDialog::SetImageAttr(const wxRichTextAttr
& textAttr
)
312 m_textAttr
= textAttr
;
314 TransferDataToWindow();
318 * Apply the new style
320 wxRichTextImage
* wxRichTextImageDialog::ApplyImageAttr()
322 wxRichTextImage
* image
= wxDynamicCast(m_image
, wxRichTextImage
);
324 TransferDataFromWindow();
325 if (m_buffer
->GetRichTextCtrl())
327 m_buffer
->GetRichTextCtrl()->SetImageStyle(image
, m_textAttr
);
332 void wxRichTextImageDialog::SetImageObject(wxRichTextImage
* image
, wxRichTextBuffer
* buffer
)
334 wxRichTextObject
* parent
= image
->GetParent();
339 SetImageAttr(image
->GetAttributes());
340 if (image
->GetImageCache().IsOk())
342 if (!m_textAttr
.GetTextBoxAttr().GetWidth().IsPresent() || m_textAttr
.GetTextBoxAttr().GetWidth().GetValue() <= 0)
344 m_textAttr
.GetTextBoxAttr().GetWidth().SetValue(image
->GetImageCache().GetWidth());
345 m_textAttr
.GetTextBoxAttr().GetWidth().SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
347 if (!m_textAttr
.GetTextBoxAttr().GetHeight().IsPresent() || m_textAttr
.GetTextBoxAttr().GetHeight().GetValue() <= 0)
349 m_textAttr
.GetTextBoxAttr().GetHeight().SetValue(image
->GetImageCache().GetHeight());
350 m_textAttr
.GetTextBoxAttr().GetHeight().SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
355 void wxRichTextImageDialog::SetDimensionValue(wxTextAttrDimension
& dim
, wxTextCtrl
* valueCtrl
, wxComboBox
* unitsCtrl
)
359 if (!dim
.IsPresent())
362 dim
.SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
365 if (dim
.GetUnits() == wxTEXT_ATTR_UNITS_TENTHS_MM
)
368 float value
= float(dim
.GetValue()) / 10.0;
369 valueCtrl
->SetValue(wxString::Format(wxT("%.2f"), value
));
374 valueCtrl
->SetValue(wxString::Format(wxT("%d"), (int) dim
.GetValue()));
377 unitsCtrl
->SetSelection(unitsIdx
);
380 void wxRichTextImageDialog::GetDimensionValue(wxTextAttrDimension
& dim
, wxTextCtrl
* valueCtrl
, wxComboBox
* unitsCtrl
)
382 if (unitsCtrl
->GetSelection() == 1)
383 dim
.SetUnits(wxTEXT_ATTR_UNITS_TENTHS_MM
);
385 dim
.SetUnits(wxTEXT_ATTR_UNITS_PIXELS
);
388 if (ConvertFromString(valueCtrl
->GetValue(), value
, dim
.GetUnits()))
392 bool wxRichTextImageDialog::TransferDataToWindow()
394 m_float
->SetSelection(m_textAttr
.GetTextBoxAttr().GetFloatMode());
396 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetWidth(), m_width
, m_unitsW
);
397 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetHeight(), m_height
, m_unitsH
);
398 SetDimensionValue(m_textAttr
.GetTextBoxAttr().GetTop(), m_offset
, m_unitsOffset
);
403 bool wxRichTextImageDialog::TransferDataFromWindow()
405 m_textAttr
.GetTextBoxAttr().SetFloatMode(m_float
->GetSelection());
407 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetWidth(), m_width
, m_unitsW
);
408 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetHeight(), m_height
, m_unitsH
);
409 GetDimensionValue(m_textAttr
.GetTextBoxAttr().GetTop(), m_offset
, m_unitsOffset
);
414 bool wxRichTextImageDialog::ConvertFromString(const wxString
& string
, int& ret
, int scale
)
416 const wxChar
* chars
= string
.GetData();
421 for (unsigned int i
= 0; i
< string
.Len() && remain
; i
++)
423 if (!(chars
[i
] >= '0' && chars
[i
] <= '9') && !(scale
== wxTEXT_ATTR_UNITS_TENTHS_MM
&& chars
[i
] == '.'))
435 ret
= ret
* 10 + chars
[i
] - '0';
438 while (remain
-- > 0 && scale
== wxTEXT_ATTR_UNITS_TENTHS_MM
)
446 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_UP
448 void wxRichTextImageDialog::OnRichtextimagedialogParaUpClick( wxCommandEvent
& WXUNUSED(event
))
450 // Before editing this code, remove the block markers.
451 wxRichTextRange range
= m_image
->GetRange();
452 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
458 if (iter
->GetData() == m_parent
)
460 iter
= iter
->GetNext();
463 iter
= iter
->GetPrevious();
467 wxRichTextObject
*obj
= iter
->GetData();
468 wxRichTextRange rg
= obj
->GetRange();
469 m_image
= m_image
->Clone();
471 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
472 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
474 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));
479 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_DOWN
482 void wxRichTextImageDialog::OnRichtextimagedialogDownClick( wxCommandEvent
& WXUNUSED(event
))
484 // Before editing this code, remove the block markers.
485 wxRichTextRange range
= m_image
->GetRange();
486 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
492 if (iter
->GetData() == m_parent
)
494 iter
= iter
->GetNext();
497 iter
= iter
->GetNext();
501 wxRichTextObject
*obj
= iter
->GetData();
502 wxRichTextRange rg
= obj
->GetRange();
503 m_image
= m_image
->Clone();
505 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
506 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
508 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));