Handle the cancel button in the color picker dialog better

Return wxNullColor rather than the original color on cancel so that the
calling code can actually tell if it was cancelled.

If one of the subs edit box color buttons is cancelled, undo the changes
made rather than restoring the original text of the active line. This
makes the cancel button actually work with multiple lines selected, and
eliminates some undo state noise.

Closes #1465.

Originally committed to SVN as r6663.
This commit is contained in:
Thomas Goyne 2012-04-06 01:55:14 +00:00
parent acf566a062
commit 0d4846d012
4 changed files with 17 additions and 11 deletions

View File

@ -65,6 +65,8 @@ ColourButton::~ColourButton() {
/// @brief Callback for the color picker dialog /// @brief Callback for the color picker dialog
/// @param col New color /// @param col New color
void ColourButton::SetColour(wxColour col) { void ColourButton::SetColour(wxColour col) {
if (!col.IsOk()) return;
colour = col; colour = col;
// Draw colour // Draw colour
@ -91,8 +93,11 @@ wxColour ColourButton::GetColour() {
/// @brief Click handler /// @brief Click handler
/// @param event /// @param event
void ColourButton::OnClick(wxCommandEvent &event) { void ColourButton::OnClick(wxCommandEvent &event) {
if (event.GetClientData() != this) if (event.GetClientData() == this)
GetColorFromUser<ColourButton, &ColourButton::SetColour>(GetParent(), colour, this);
else
event.Skip(); event.Skip();
else {
wxColour initial = colour;
if (!GetColorFromUser<ColourButton, &ColourButton::SetColour>(GetParent(), colour, this).IsOk())
SetColour(initial);
}
} }

View File

@ -633,6 +633,8 @@ wxColour GetColorFromUser(wxWindow *parent, wxColour original, ColorCallback cal
DialogColorPicker dialog(parent, original, callback, userdata); DialogColorPicker dialog(parent, original, callback, userdata);
if (dialog.ShowModal() == wxID_OK) if (dialog.ShowModal() == wxID_OK)
original = dialog.GetColor(); original = dialog.GetColor();
else
original = wxNullColour;
if (callback) if (callback)
callback(userdata, original); callback(userdata, original);
return original; return original;

View File

@ -52,7 +52,7 @@ void ColorCallbackWrapper(void* obj, wxColor color) {
/// @param original Initial color to select /// @param original Initial color to select
/// @param callback Function called whenever the selected color changes if not NULL /// @param callback Function called whenever the selected color changes if not NULL
/// @param userdata Passed to callback function /// @param userdata Passed to callback function
/// @return Last selected color when dialog is closed, or original if the dialog was cancelled /// @return Last selected color when dialog is closed, or wxNullColour if the dialog was cancelled
wxColor GetColorFromUser(wxWindow* parent, wxColor original, ColorCallback callback = NULL, void* userdata = NULL); wxColor GetColorFromUser(wxWindow* parent, wxColor original, ColorCallback callback = NULL, void* userdata = NULL);
/// @brief Get a color from the user via a color picker dialog /// @brief Get a color from the user via a color picker dialog
@ -61,7 +61,7 @@ wxColor GetColorFromUser(wxWindow* parent, wxColor original, ColorCallback callb
/// @param parent Parent window /// @param parent Parent window
/// @param original Initial color to select /// @param original Initial color to select
/// @param callbackObj Object to call callback method on. Must be of type T. /// @param callbackObj Object to call callback method on. Must be of type T.
/// @return Last selected color when dialog is closed, or original if the dialog was cancelled /// @return Last selected color when dialog is closed, or wxNullColour if the dialog was cancelled
template<class T, void (T::*method)(wxColor)> template<class T, void (T::*method)(wxColor)>
wxColor GetColorFromUser(wxWindow* parent, wxColor original, T* callbackObj) { wxColor GetColorFromUser(wxWindow* parent, wxColor original, T* callbackObj) {
return GetColorFromUser(parent, original, &ColorCallbackWrapper<T, method>, callbackObj); return GetColorFromUser(parent, original, &ColorCallbackWrapper<T, method>, callbackObj);

View File

@ -722,15 +722,14 @@ void SubsEditBox::OnColorButton(AssColor (AssStyle::*field), const char *tag, co
int blockn = block_at_pos(line->Text, sel.first); int blockn = block_at_pos(line->Text, sel.first);
color = get_value(*line, blockn, color, colorTag, alt); color = get_value(*line, blockn, color, colorTag, alt);
wxString initialText = line->Text;
wxColor newColor = GetColorFromUser<SubsEditBox, &SubsEditBox::SetColorCallback>(c->parent, color, this); wxColor newColor = GetColorFromUser<SubsEditBox, &SubsEditBox::SetColorCallback>(c->parent, color, this);
if (newColor == color) {
TextEdit->SetTextTo(initialText);
TextEdit->SetSelectionU(sel.first, sel.second);
}
line->ClearBlocks(); line->ClearBlocks();
CommitText(_("set color")); CommitText(_("set color"));
if (!newColor.IsOk()) {
c->ass->Undo();
TextEdit->SetSelectionU(sel.first, sel.second);
}
} }
void SubsEditBox::SetColorCallback(wxColor newColor) { void SubsEditBox::SetColorCallback(wxColor newColor) {