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"
22 #include "wx/textctrl.h"
25 #include "wx/statline.h"
27 #include "wx/richtext/richtextimagedlg.h"
28 #include "wx/richtext/richtextctrl.h"
35 * wxRichTextImageDialog type definition
38 IMPLEMENT_DYNAMIC_CLASS( wxRichTextImageDialog
, wxDialog
)
42 * wxRichTextImageDialog event table definition
45 BEGIN_EVENT_TABLE( wxRichTextImageDialog
, wxDialog
)
47 ////@begin wxRichTextImageDialog event table entries
48 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_PARA_UP
, wxRichTextImageDialog::OnRichtextimagedialogParaUpClick
)
50 EVT_BUTTON( ID_RICHTEXTIMAGEDIALOG_DOWN
, wxRichTextImageDialog::OnRichtextimagedialogDownClick
)
52 ////@end wxRichTextImageDialog event table entries
58 * wxRichTextImageDialog constructors
61 wxRichTextImageDialog::wxRichTextImageDialog()
66 wxRichTextImageDialog::wxRichTextImageDialog( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
69 Create(parent
, id
, caption
, pos
, size
, style
);
74 * wxRichTextImageDlg creator
77 bool wxRichTextImageDialog::Create( wxWindow
* parent
, wxWindowID id
, const wxString
& caption
, const wxPoint
& pos
, const wxSize
& size
, long style
)
79 ////@begin wxRichTextImageDialog creation
80 SetExtraStyle(wxDIALOG_EX_CONTEXTHELP
);
81 wxDialog::Create( parent
, id
, caption
, pos
, size
, style
);
86 GetSizer()->SetSizeHints(this);
89 ////@end wxRichTextImageDialog creation
95 * wxRichTextImageDialog destructor
98 wxRichTextImageDialog::~wxRichTextImageDialog()
100 ////@begin wxRichTextImageDialog destruction
101 ////@end wxRichTextImageDialog destruction
106 * Member initialisation
109 void wxRichTextImageDialog::Init()
111 ////@begin wxRichTextImageDialog member initialisation
119 m_unitsOffset
= NULL
;
121 m_cancelButton
= NULL
;
122 ////@end wxRichTextImageDialog member initialisation
127 * Control creation for wxRichTextImageDlg
130 void wxRichTextImageDialog::CreateControls()
133 SetWindowVariant(wxWINDOW_VARIANT_SMALL
);
136 ////@begin wxRichTextImageDialog content construction
137 wxRichTextImageDialog
* itemDialog1
= this;
139 wxBoxSizer
* itemBoxSizer2
= new wxBoxSizer(wxVERTICAL
);
140 itemDialog1
->SetSizer(itemBoxSizer2
);
142 wxBoxSizer
* itemBoxSizer3
= new wxBoxSizer(wxHORIZONTAL
);
143 itemBoxSizer2
->Add(itemBoxSizer3
, 0, wxGROW
|wxALL
, 5);
145 wxFlexGridSizer
* itemFlexGridSizer4
= new wxFlexGridSizer(0, 2, 0, 0);
146 itemBoxSizer3
->Add(itemFlexGridSizer4
, 0, wxALIGN_TOP
|wxRIGHT
, 5);
148 wxStaticText
* itemStaticText5
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Alignment:"), wxDefaultPosition
, wxDefaultSize
, 0 );
149 itemStaticText5
->Show(false);
150 itemFlexGridSizer4
->Add(itemStaticText5
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
152 wxArrayString m_alignmentStrings
;
153 m_alignmentStrings
.Add(_("Left"));
154 m_alignmentStrings
.Add(_("Centre"));
155 m_alignmentStrings
.Add(_("Right"));
156 m_alignment
= new wxComboBox( itemDialog1
, ID_COMBOBOX_ALIGN
, _("Left"), wxDefaultPosition
, wxSize(80, -1), m_alignmentStrings
, wxCB_READONLY
);
157 m_alignment
->SetStringSelection(_("Left"));
158 m_alignment
->Show(false);
159 itemFlexGridSizer4
->Add(m_alignment
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
161 wxStaticText
* itemStaticText7
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Floating mode:"), wxDefaultPosition
, wxDefaultSize
, 0 );
162 itemFlexGridSizer4
->Add(itemStaticText7
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
164 wxArrayString m_floatStrings
;
165 m_floatStrings
.Add(_("None"));
166 m_floatStrings
.Add(_("Left"));
167 m_floatStrings
.Add(_("Right"));
168 m_float
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_FLOATING_MODE
, _("None"), wxDefaultPosition
, wxSize(80, -1), m_floatStrings
, wxCB_READONLY
);
169 m_float
->SetStringSelection(_("None"));
170 m_float
->SetHelpText(_("How the image will float relative to the text."));
171 if (wxRichTextImageDialog::ShowToolTips())
172 m_float
->SetToolTip(_("How the image will float relative to the text."));
173 itemFlexGridSizer4
->Add(m_float
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
175 wxStaticText
* itemStaticText9
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Width:"), wxDefaultPosition
, wxDefaultSize
, 0 );
176 itemFlexGridSizer4
->Add(itemStaticText9
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
178 wxBoxSizer
* itemBoxSizer10
= new wxBoxSizer(wxHORIZONTAL
);
179 itemFlexGridSizer4
->Add(itemBoxSizer10
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
181 m_width
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_WIDTH
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
182 m_width
->SetHelpText(_("The image width to be shown - does not change the source image width."));
183 if (wxRichTextImageDialog::ShowToolTips())
184 m_width
->SetToolTip(_("The image width to be shown - does not change the source image width."));
185 itemBoxSizer10
->Add(m_width
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
187 wxArrayString m_unitsWStrings
;
188 m_unitsWStrings
.Add(_("px"));
189 m_unitsWStrings
.Add(_("cm"));
190 m_unitsW
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_W
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsWStrings
, wxCB_READONLY
);
191 m_unitsW
->SetStringSelection(_("px"));
192 m_unitsW
->SetHelpText(_("Units for the image width."));
193 if (wxRichTextImageDialog::ShowToolTips())
194 m_unitsW
->SetToolTip(_("Units for the image width."));
195 itemBoxSizer10
->Add(m_unitsW
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
197 wxStaticText
* itemStaticText13
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Height:"), wxDefaultPosition
, wxDefaultSize
, 0 );
198 itemFlexGridSizer4
->Add(itemStaticText13
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
200 wxBoxSizer
* itemBoxSizer14
= new wxBoxSizer(wxHORIZONTAL
);
201 itemFlexGridSizer4
->Add(itemBoxSizer14
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
203 m_height
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_HEIGHT
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
204 m_height
->SetHelpText(_("The image height to be shown - does not change the source image height."));
205 if (wxRichTextImageDialog::ShowToolTips())
206 m_height
->SetToolTip(_("The image height to be shown - does not change the source image height."));
207 itemBoxSizer14
->Add(m_height
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
209 wxArrayString m_unitsHStrings
;
210 m_unitsHStrings
.Add(_("px"));
211 m_unitsHStrings
.Add(_("cm"));
212 m_unitsH
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_UNITS_H
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsHStrings
, wxCB_READONLY
);
213 m_unitsH
->SetStringSelection(_("px"));
214 m_unitsH
->SetHelpText(_("Units for the image height."));
215 if (wxRichTextImageDialog::ShowToolTips())
216 m_unitsH
->SetToolTip(_("Units for the image height."));
217 itemBoxSizer14
->Add(m_unitsH
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
219 wxStaticText
* itemStaticText17
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("Image Vertical &Offset:"), wxDefaultPosition
, wxDefaultSize
, 0 );
220 itemFlexGridSizer4
->Add(itemStaticText17
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
222 wxBoxSizer
* itemBoxSizer18
= new wxBoxSizer(wxHORIZONTAL
);
223 itemFlexGridSizer4
->Add(itemBoxSizer18
, 1, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
225 m_offset
= new wxTextCtrl( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET
, wxEmptyString
, wxDefaultPosition
, wxSize(65, -1), 0 );
226 m_offset
->SetMaxLength(10);
227 m_offset
->SetHelpText(_("The vertical offset relative to the paragraph."));
228 if (wxRichTextImageDialog::ShowToolTips())
229 m_offset
->SetToolTip(_("The vertical offset relative to the paragraph."));
230 itemBoxSizer18
->Add(m_offset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
232 wxArrayString m_unitsOffsetStrings
;
233 m_unitsOffsetStrings
.Add(_("px"));
234 m_unitsOffsetStrings
.Add(_("cm"));
235 m_unitsOffset
= new wxComboBox( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_OFFSET_UNITS
, _("px"), wxDefaultPosition
, wxSize(60, -1), m_unitsOffsetStrings
, wxCB_READONLY
);
236 m_unitsOffset
->SetStringSelection(_("px"));
237 m_unitsOffset
->SetHelpText(_("Units for the image offset."));
238 if (wxRichTextImageDialog::ShowToolTips())
239 m_unitsOffset
->SetToolTip(_("Units for the image offset."));
240 itemBoxSizer18
->Add(m_unitsOffset
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
242 wxStaticText
* itemStaticText21
= new wxStaticText( itemDialog1
, wxID_STATIC
, _("&Move the image to:"), wxDefaultPosition
, wxDefaultSize
, 0 );
243 itemFlexGridSizer4
->Add(itemStaticText21
, 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
245 wxBoxSizer
* itemBoxSizer22
= new wxBoxSizer(wxHORIZONTAL
);
246 itemFlexGridSizer4
->Add(itemBoxSizer22
, 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
, 5);
248 wxButton
* itemButton23
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_PARA_UP
, _("&Previous Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
249 itemButton23
->SetHelpText(_("Moves the image to the previous paragraph."));
250 if (wxRichTextImageDialog::ShowToolTips())
251 itemButton23
->SetToolTip(_("Moves the image to the previous paragraph."));
252 itemBoxSizer22
->Add(itemButton23
, 0, wxALIGN_CENTER_VERTICAL
|wxALL
, 5);
254 wxButton
* itemButton24
= new wxButton( itemDialog1
, ID_RICHTEXTIMAGEDIALOG_DOWN
, _("&Next Paragraph"), wxDefaultPosition
, wxDefaultSize
, 0 );
255 itemButton24
->SetHelpText(_("Moves the image to the next paragraph."));
256 if (wxRichTextImageDialog::ShowToolTips())
257 itemButton24
->SetToolTip(_("Moves the image to the next paragraph."));
258 itemBoxSizer22
->Add(itemButton24
, 0, wxALIGN_CENTER_VERTICAL
|wxRIGHT
|wxTOP
|wxBOTTOM
, 5);
260 wxStaticLine
* itemStaticLine25
= new wxStaticLine( itemDialog1
, wxID_STATIC
, wxDefaultPosition
, wxDefaultSize
, wxLI_HORIZONTAL
);
261 itemBoxSizer2
->Add(itemStaticLine25
, 0, wxGROW
|wxALL
, 5);
263 wxStdDialogButtonSizer
* itemStdDialogButtonSizer26
= new wxStdDialogButtonSizer
;
265 itemBoxSizer2
->Add(itemStdDialogButtonSizer26
, 0, wxGROW
|wxALL
, 5);
266 m_saveButton
= new wxButton( itemDialog1
, wxID_OK
, _("OK"), wxDefaultPosition
, wxDefaultSize
, 0 );
267 m_saveButton
->SetHelpText(_("Click to confirm your changes."));
268 if (wxRichTextImageDialog::ShowToolTips())
269 m_saveButton
->SetToolTip(_("Click to confirm your changes."));
270 itemStdDialogButtonSizer26
->AddButton(m_saveButton
);
272 m_cancelButton
= new wxButton( itemDialog1
, wxID_CANCEL
, _("Cancel"), wxDefaultPosition
, wxDefaultSize
, 0 );
273 m_cancelButton
->SetHelpText(_("Click to discard your changes."));
274 if (wxRichTextImageDialog::ShowToolTips())
275 m_cancelButton
->SetToolTip(_("Click to discard your changes."));
276 itemStdDialogButtonSizer26
->AddButton(m_cancelButton
);
278 itemStdDialogButtonSizer26
->Realize();
280 ////@end wxRichTextImageDialog content construction
285 * Should we show tooltips?
288 bool wxRichTextImageDialog::ShowToolTips()
294 * Get bitmap resources
297 wxBitmap
wxRichTextImageDialog::GetBitmapResource( const wxString
& name
)
300 ////@begin wxRichTextImageDialog bitmap retrieval
303 ////@end wxRichTextImageDialog bitmap retrieval
310 wxIcon
wxRichTextImageDialog::GetIconResource( const wxString
& name
)
313 ////@begin wxRichTextImageDialog icon retrieval
316 ////@end wxRichTextImageDialog icon retrieval
320 * Set the image attribute
322 void wxRichTextImageDialog::SetImageAttr(const wxRichTextAnchoredObjectAttr
& attr
)
325 TransferDataToWindow();
329 * Apply the new style
331 wxRichTextImage
* wxRichTextImageDialog::ApplyImageAttr()
333 wxRichTextImage
* image
= wxDynamicCast(m_image
, wxRichTextImage
);
335 TransferDataFromWindow();
336 if (m_buffer
->GetRichTextCtrl())
338 m_buffer
->GetRichTextCtrl()->SetImageStyle(image
, m_attr
);
343 void wxRichTextImageDialog::SetImageObject(wxRichTextImage
* image
, wxRichTextBuffer
* buffer
)
345 wxRichTextObject
* parent
= image
->GetParent();
350 SetImageAttr(image
->GetAnchoredAttr());
351 if (image
->GetImageCache().IsOk())
353 if (m_attr
.m_width
== -1)
355 m_attr
.m_width
= image
->GetImageCache().GetWidth();
356 wxASSERT(m_attr
.m_unitsW
== wxRICHTEXT_PX
);
358 m_attr
.m_unitsW
= wxRICHTEXT_PX
;
360 if (m_attr
.m_height
== -1)
362 m_attr
.m_height
= image
->GetImageCache().GetHeight();
363 wxASSERT(m_attr
.m_unitsH
== wxRICHTEXT_PX
);
365 m_attr
.m_unitsH
= wxRICHTEXT_PX
;
370 bool wxRichTextImageDialog::TransferDataToWindow()
374 m_alignment
->SetSelection(m_attr
.m_align
);
375 m_float
->SetSelection(m_attr
.m_floating
);
378 m_unitsW
->SetSelection(m_attr
.m_unitsW
);
379 m_unitsH
->SetSelection(m_attr
.m_unitsH
);
380 m_unitsOffset
->SetSelection(m_attr
.m_unitsOffset
);
384 if (m_attr
.m_unitsW
== wxRICHTEXT_MM
)
386 int remainder
= m_attr
.m_width
% remain
;
387 *m_width
<< m_attr
.m_width
/ remain
;
390 *m_width
<< '.' << remainder
;
395 *m_width
<< m_attr
.m_width
;
399 if (m_attr
.m_unitsH
== wxRICHTEXT_MM
)
401 int remainder
= m_attr
.m_height
% remain
;
402 *m_height
<< m_attr
.m_height
/ remain
;
405 *m_height
<< '.' << remainder
;
410 *m_height
<< m_attr
.m_height
;
414 if (m_attr
.m_unitsOffset
== wxRICHTEXT_MM
)
416 int remainder
= m_attr
.m_offset
% remain
;
417 *m_offset
<< m_attr
.m_offset
/ remain
;
420 *m_offset
<< '.' << remainder
;
425 *m_offset
<< m_attr
.m_offset
;
431 bool wxRichTextImageDialog::TransferDataFromWindow()
433 wxString width
= m_width
->GetValue();
434 wxString height
= m_height
->GetValue();
435 wxString offset
= m_offset
->GetValue();
438 m_attr
.m_align
= m_alignment
->GetSelection();
439 m_attr
.m_floating
= m_float
->GetSelection();
441 m_attr
.m_unitsW
= m_unitsW
->GetSelection();
442 m_attr
.m_unitsH
= m_unitsH
->GetSelection();
443 m_attr
.m_unitsOffset
= m_unitsOffset
->GetSelection();
445 if (ConvertFromString(width
, w
, m_attr
.m_unitsW
))
447 if (ConvertFromString(height
, h
, m_attr
.m_unitsH
))
449 if (ConvertFromString(offset
, o
, m_attr
.m_unitsOffset
))
455 bool wxRichTextImageDialog::ConvertFromString(const wxString
& string
, int& ret
, int scale
)
457 const wxChar
* chars
= string
.GetData();
462 for (unsigned int i
= 0; i
< string
.Len() && remain
; i
++)
464 if (!(chars
[i
] >= '0' && chars
[i
] <= '9') && !(scale
== wxRICHTEXT_MM
&& chars
[i
] == '.'))
476 ret
= ret
* 10 + chars
[i
] - '0';
479 while (remain
-- > 0 && scale
== wxRICHTEXT_MM
)
487 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_UP
489 void wxRichTextImageDialog::OnRichtextimagedialogParaUpClick( wxCommandEvent
& WXUNUSED(event
))
491 // Before editing this code, remove the block markers.
492 wxRichTextRange range
= m_image
->GetRange();
493 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
499 if (iter
->GetData() == m_parent
)
501 iter
= iter
->GetNext();
504 iter
= iter
->GetPrevious();
508 wxRichTextObject
*obj
= iter
->GetData();
509 wxRichTextRange rg
= obj
->GetRange();
510 m_image
= m_image
->Clone();
512 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
513 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
515 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));
520 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_PARA_DOWN
523 void wxRichTextImageDialog::OnRichtextimagedialogDownClick( wxCommandEvent
& WXUNUSED(event
))
525 // Before editing this code, remove the block markers.
526 wxRichTextRange range
= m_image
->GetRange();
527 wxRichTextObjectList::compatibility_iterator iter
= m_buffer
->GetChildren().GetFirst();
533 if (iter
->GetData() == m_parent
)
535 iter
= iter
->GetNext();
538 iter
= iter
->GetNext();
542 wxRichTextObject
*obj
= iter
->GetData();
543 wxRichTextRange rg
= obj
->GetRange();
544 m_image
= m_image
->Clone();
546 m_buffer
->DeleteRangeWithUndo(range
, m_buffer
->GetRichTextCtrl());
547 m_buffer
->InsertObjectWithUndo(rg
.GetEnd(), m_image
, m_buffer
->GetRichTextCtrl(), 0);
549 m_image
->SetRange(wxRichTextRange(rg
.GetEnd(), rg
.GetEnd()));