1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
#include "wx/wxprec.h" // for compilers that support precompilation
#ifndef WX_PRECOMP
#include "wx/wx.h" // for all others include the necessary headers
#endif
#include "wx/spinctrl.h" // for wxSpinCtrl
#include "wx/progdlg.h" // for wxProgressDialog
#include "wx/image.h" // for wxImage
#include "wxgolly.h" // for wxGetApp, viewptr, mainptr
#include "wxview.h" // for viewptr->...
#include "wxmain.h" // for mainptr->...
#include "wxscript.h" // for inscript, showprogress, PassKeyToScript
#include "wxprefs.h" // for allowbeep
#include "wxutils.h"
// -----------------------------------------------------------------------------
// need platform-specific gap after OK/Cancel buttons
#ifdef __WXMAC__
const int STDHGAP = 0;
#elif defined(__WXMSW__)
const int STDHGAP = 6;
#else
const int STDHGAP = 10;
#endif
// -----------------------------------------------------------------------------
void Note(const wxString& msg, bool showCancel)
{
wxString title = wxGetApp().GetAppName() + _(" note:");
if (inscript && showCancel) {
// add a Cancel button so user can abort script
int button = wxMessageBox(msg, title, wxOK | wxCANCEL | wxICON_INFORMATION,
wxGetActiveWindow());
viewptr->ResetMouseDown();
if (button == wxCANCEL) {
PassKeyToScript(WXK_ESCAPE); // abort script
}
} else {
wxMessageBox(msg, title, wxOK | wxICON_INFORMATION, wxGetActiveWindow());
viewptr->ResetMouseDown();
}
}
// -----------------------------------------------------------------------------
void Warning(const wxString& msg, bool showCancel)
{
Beep();
wxString title = wxGetApp().GetAppName() + _(" warning:");
if (inscript && showCancel) {
// add a Cancel button so user can abort script
int button = wxMessageBox(msg, title, wxOK | wxCANCEL | wxICON_EXCLAMATION,
wxGetActiveWindow());
viewptr->ResetMouseDown();
if (button == wxCANCEL) {
PassKeyToScript(WXK_ESCAPE); // abort script
}
} else {
wxMessageBox(msg, title, wxOK | wxICON_EXCLAMATION, wxGetActiveWindow());
viewptr->ResetMouseDown();
}
}
// -----------------------------------------------------------------------------
void Fatal(const wxString& msg)
{
Beep();
wxString title = wxGetApp().GetAppName() + _(" error:");
wxMessageBox(msg, title, wxOK | wxICON_ERROR, wxGetActiveWindow());
viewptr->ResetMouseDown();
exit(1); // safer than calling wxExit()
}
// -----------------------------------------------------------------------------
void Beep()
{
if (allowbeep) wxBell();
}
// =============================================================================
// define a modal dialog for getting a string
class StringDialog : public wxDialog
{
public:
StringDialog(wxWindow* parent, const wxString& title,
const wxString& prompt, const wxString& instring);
virtual bool TransferDataFromWindow(); // called when user hits OK
wxString GetValue() { return result; }
private:
wxTextCtrl* textbox; // text box for entering the string
wxString result; // the resulting string
};
// -----------------------------------------------------------------------------
StringDialog::StringDialog(wxWindow* parent, const wxString& title,
const wxString& prompt, const wxString& instring)
{
Create(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize);
// create the controls
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
SetSizer(topSizer);
textbox = new wxTextCtrl(this, wxID_ANY, instring);
wxStaticText* promptlabel = new wxStaticText(this, wxID_STATIC, prompt);
wxSizer* stdbutts = CreateButtonSizer(wxOK | wxCANCEL);
// position the controls
wxBoxSizer* stdhbox = new wxBoxSizer(wxHORIZONTAL);
stdhbox->Add(stdbutts, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, STDHGAP);
wxSize minsize = stdhbox->GetMinSize();
if (minsize.GetWidth() < 250) {
minsize.SetWidth(250);
stdhbox->SetMinSize(minsize);
}
topSizer->AddSpacer(12);
topSizer->Add(promptlabel, 0, wxLEFT | wxRIGHT, 10);
topSizer->AddSpacer(10);
topSizer->Add(textbox, 0, wxGROW | wxLEFT | wxRIGHT, 10);
topSizer->AddSpacer(12);
topSizer->Add(stdhbox, 1, wxGROW | wxTOP | wxBOTTOM, 10);
GetSizer()->Fit(this);
GetSizer()->SetSizeHints(this);
Centre();
// select initial string (must do this last on Windows)
textbox->SetFocus();
textbox->SetSelection(-1,-1);
}
// -----------------------------------------------------------------------------
bool StringDialog::TransferDataFromWindow()
{
result = textbox->GetValue();
return true;
}
// -----------------------------------------------------------------------------
bool GetString(const wxString& title, const wxString& prompt,
const wxString& instring, wxString& outstring)
{
StringDialog dialog(wxGetApp().GetTopWindow(), title, prompt, instring);
int button = dialog.ShowModal();
viewptr->ResetMouseDown();
if (button == wxID_OK) {
outstring = dialog.GetValue();
return true;
} else {
// user hit Cancel button
return false;
}
}
// =============================================================================
// define a modal dialog for getting an integer
class IntegerDialog : public wxDialog
{
public:
IntegerDialog(wxWindow* parent,
const wxString& title,
const wxString& prompt,
int inval, int minval, int maxval);
virtual bool TransferDataFromWindow(); // called when user hits OK
#ifdef __WXMAC__
void OnSpinCtrlChar(wxKeyEvent& event);
#endif
int GetValue() { return result; }
private:
enum {
ID_SPIN_CTRL = wxID_HIGHEST + 1
};
wxSpinCtrl* spinctrl; // for entering the integer
int minint; // minimum value
int maxint; // maximum value
int result; // the resulting integer
};
// -----------------------------------------------------------------------------
#ifdef __WXMAC__
// override key event handler for wxSpinCtrl to allow key checking
class MySpinCtrl : public wxSpinCtrl
{
public:
MySpinCtrl(wxWindow* parent, wxWindowID id) : wxSpinCtrl(parent, id)
{
// create a dynamic event handler for the underlying wxTextCtrl
wxTextCtrl* textctrl = GetText();
if (textctrl) {
textctrl->Connect(wxID_ANY, wxEVT_CHAR,
wxKeyEventHandler(IntegerDialog::OnSpinCtrlChar));
}
}
};
void IntegerDialog::OnSpinCtrlChar(wxKeyEvent& event)
{
int key = event.GetKeyCode();
if (event.CmdDown()) {
// allow handling of cmd-x/v/etc
event.Skip();
} else if ( key == WXK_TAB ) {
wxSpinCtrl* sc = (wxSpinCtrl*) FindWindowById(ID_SPIN_CTRL);
if ( sc ) {
sc->SetFocus();
sc->SetSelection(-1,-1);
}
} else if ( key >= ' ' && key <= '~' ) {
if ( (key >= '0' && key <= '9') || key == '+' || key == '-' ) {
// allow digits and + or -
event.Skip();
} else {
// disallow any other displayable ascii char
Beep();
}
} else {
event.Skip();
}
}
#else
#define MySpinCtrl wxSpinCtrl
#endif // __WXMAC__
// -----------------------------------------------------------------------------
IntegerDialog::IntegerDialog(wxWindow* parent,
const wxString& title,
const wxString& prompt,
int inval, int minval, int maxval)
{
Create(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize);
minint = minval;
maxint = maxval;
// create the controls
wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
SetSizer(topSizer);
spinctrl = new MySpinCtrl(this, ID_SPIN_CTRL);
spinctrl->SetRange(minval, maxval);
spinctrl->SetValue(inval);
wxStaticText* promptlabel = new wxStaticText(this, wxID_STATIC, prompt);
wxSizer* stdbutts = CreateButtonSizer(wxOK | wxCANCEL);
// position the controls
wxBoxSizer* stdhbox = new wxBoxSizer(wxHORIZONTAL);
stdhbox->Add(stdbutts, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, STDHGAP);
wxSize minsize = stdhbox->GetMinSize();
if (minsize.GetWidth() < 250) {
minsize.SetWidth(250);
stdhbox->SetMinSize(minsize);
}
topSizer->AddSpacer(12);
topSizer->Add(promptlabel, 0, wxLEFT | wxRIGHT, 10);
topSizer->AddSpacer(10);
topSizer->Add(spinctrl, 0, wxGROW | wxLEFT | wxRIGHT, 10);
topSizer->AddSpacer(12);
topSizer->Add(stdhbox, 1, wxGROW | wxTOP | wxBOTTOM, 10);
GetSizer()->Fit(this);
GetSizer()->SetSizeHints(this);
Centre();
// select initial value (must do this last on Windows)
spinctrl->SetFocus();
spinctrl->SetSelection(-1,-1);
}
// -----------------------------------------------------------------------------
bool IntegerDialog::TransferDataFromWindow()
{
// spinctrl->GetValue() always returns a value within range even if
// the text ctrl doesn't contain a valid number -- yuk!
result = spinctrl->GetValue();
if (result < minint || result > maxint) {
wxString msg;
msg.Printf(_("Value must be from %d to %d."), minint, maxint);
Warning(msg);
spinctrl->SetFocus();
spinctrl->SetSelection(-1,-1);
return false;
} else {
return true;
}
}
// -----------------------------------------------------------------------------
bool GetInteger(const wxString& title, const wxString& prompt,
int inval, int minval, int maxval, int* outval)
{
IntegerDialog dialog(wxGetApp().GetTopWindow(), title, prompt,
inval, minval, maxval);
int button = dialog.ShowModal();
viewptr->ResetMouseDown();
if (button == wxID_OK) {
*outval = dialog.GetValue();
return true;
} else {
// user hit Cancel button
return false;
}
}
// -----------------------------------------------------------------------------
int SaveChanges(const wxString& query, const wxString& msg)
{
#if defined(__WXMAC__)
// use a standard looking modal dialog on wxOSX;
// sadly, positioning over center of parent window is not supported by NSAlert
wxMessageDialog dialog(wxGetActiveWindow(), msg, query,
wxCENTER | wxNO_DEFAULT | wxYES_NO | wxCANCEL |
wxICON_INFORMATION);
dialog.SetYesNoCancelLabels("Cancel", "Save", "Don't Save");
int button = dialog.ShowModal();
viewptr->ResetMouseDown();
switch (button) {
case wxID_YES: return 0; // Cancel
case wxID_NO: return 2; // Save
case wxID_CANCEL: return 1; // Don't Save
default: return 0; // should never happen
}
#else
// Windows/Linux
int answer = wxMessageBox(msg, query,
wxICON_QUESTION | wxYES_NO | wxCANCEL,
wxGetActiveWindow());
viewptr->ResetMouseDown();
switch (answer) {
case wxYES: return 2;
case wxNO: return 1;
default: return 0; // answer == wxCANCEL
}
#endif
}
// =============================================================================
// globals for showing progress
wxProgressDialog* progdlg = NULL; // progress dialog
const int maxprogrange = 1000000000; // maximum range (best if very large)
wxStopWatch* progwatch = NULL; // stopwatch for progress dialog
long prognext; // when to update progress dialog
wxString progtitle; // title for progress dialog
// -----------------------------------------------------------------------------
#ifdef __WXMAC__
// define a key event handler to allow escape key to cancel progress dialog
class ProgressHandler : public wxEvtHandler
{
public:
void OnKeyDown(wxKeyEvent& event);
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(ProgressHandler, wxEvtHandler)
EVT_KEY_DOWN (ProgressHandler::OnKeyDown)
END_EVENT_TABLE()
void ProgressHandler::OnKeyDown(wxKeyEvent& event)
{
int key = event.GetKeyCode();
if (key == WXK_ESCAPE || key == '.') {
wxCommandEvent cancel(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
wxWindow* buttwin = progdlg->FindWindow(wxID_CANCEL);
if (buttwin) {
cancel.SetEventObject(buttwin);
buttwin->GetEventHandler()->ProcessEvent(cancel);
}
} else {
event.Skip();
}
}
#endif
// -----------------------------------------------------------------------------
void BeginProgress(const wxString& dlgtitle)
{
if (inscript && !showprogress) return;
if (progdlg) {
// better do this in case of nested call
delete progdlg;
progdlg = NULL;
}
if (progwatch) {
delete progwatch;
}
progwatch = new wxStopWatch();
progtitle = dlgtitle;
// avoid cursor flickering if this is called during a script
if (!inscript) {
// let user know they'll have to wait
if (mainptr->infront) viewptr->SetCursor(*wxHOURGLASS_CURSOR);
}
}
// -----------------------------------------------------------------------------
bool AbortProgress(double fraction_done, const wxString& newmsg)
{
if (inscript && !showprogress) return false;
long msecs = progwatch->Time();
if (progdlg) {
if (msecs < prognext) return false;
prognext = msecs + 100; // call Update/Pulse about 10 times per sec
bool cancel;
if (fraction_done < 0.0) {
// show indeterminate progress gauge
cancel = !progdlg->Pulse(newmsg);
} else {
cancel = !progdlg->Update(int((double)maxprogrange * fraction_done), newmsg);
}
if (cancel) {
// user hit Cancel button
if (inscript) PassKeyToScript(WXK_ESCAPE); // abort script
return true;
} else {
return false;
}
} else {
// note that fraction_done is not always an accurate estimator for how long
// the task will take, especially when we use nextcell for cut/copy
if ( (msecs > 1000 && fraction_done < 0.3) || msecs > 2500 ) {
// task is probably going to take a while so create progress dialog
progdlg = new wxProgressDialog(progtitle, wxEmptyString,
maxprogrange, wxGetActiveWindow(),
wxPD_AUTO_HIDE | wxPD_APP_MODAL |
wxPD_CAN_ABORT | wxPD_SMOOTH |
wxPD_ESTIMATED_TIME | wxPD_REMAINING_TIME);
#if defined(__WXMAC__)
if (progdlg) {
// install key event handler
progdlg->PushEventHandler(new ProgressHandler());
}
#endif
}
prognext = msecs + 10; // short delay until 1st Update/Pulse
return false; // don't abort
}
}
// -----------------------------------------------------------------------------
void EndProgress()
{
if (inscript && !showprogress) return;
if (progdlg) {
#if defined(__WXMAC__)
// remove and delete ProgressHandler
progdlg->PopEventHandler(true);
#endif
delete progdlg;
progdlg = NULL;
}
if (progwatch) {
delete progwatch;
progwatch = NULL;
}
if (!inscript) {
// BeginProgress changed cursor so reset it
viewptr->CheckCursor(mainptr->infront);
}
}
// =============================================================================
void FillRect(wxDC& dc, wxRect& rect, wxBrush& brush)
{
// set pen transparent so brush fills rect
dc.SetPen(*wxTRANSPARENT_PEN);
dc.SetBrush(brush);
dc.DrawRectangle(rect);
dc.SetBrush(wxNullBrush); // restore brush
dc.SetPen(wxNullPen); // restore pen
}
// -----------------------------------------------------------------------------
void CreatePaleBitmap(const wxBitmap& inmap, wxBitmap& outmap)
{
wxImage oldimg = inmap.ConvertToImage();
wxImage newimg;
newimg.Create(oldimg.GetWidth(), oldimg.GetHeight(), false);
unsigned char* dest = newimg.GetData();
unsigned char* src = oldimg.GetData();
bool hasMask = oldimg.HasMask();
unsigned char maskRed = oldimg.GetMaskRed();
unsigned char maskGreen = oldimg.GetMaskGreen();
unsigned char maskBlue = oldimg.GetMaskBlue();
if (hasMask)
newimg.SetMaskColour(maskRed, maskGreen, maskBlue);
const long size = oldimg.GetWidth() * oldimg.GetHeight();
for ( long i = 0; i < size; i++, src += 3, dest += 3 ) {
if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue ) {
// don't modify the mask
memcpy(dest, src, 3);
} else {
// make pixel a pale shade of gray
int gray = (int) ((src[0] + src[1] + src[2]) / 3.0);
gray = (int) (170.0 + (gray / 4.0));
dest[0] = dest[1] = dest[2] = gray;
}
}
// copy the alpha channel, if any
if (oldimg.HasAlpha()) {
const size_t alphaSize = oldimg.GetWidth() * oldimg.GetHeight();
unsigned char* alpha = (unsigned char*) malloc(alphaSize);
if (alpha) {
memcpy(alpha, oldimg.GetAlpha(), alphaSize);
newimg.InitAlpha();
newimg.SetAlpha(alpha);
}
}
outmap = wxBitmap(newimg);
}
// -----------------------------------------------------------------------------
bool IsScriptFile(const wxString& filename)
{
wxString ext = filename.AfterLast('.');
// if filename has no extension then ext == filename
if (ext == filename) return false;
// Golly supports Lua and Python scripts
// (with optional support for Perl if ENABLE_PERL is defined)
return ( ext.IsSameAs(wxT("lua"),false) ||
ext.IsSameAs(wxT("py"),false) ||
ext.IsSameAs(wxT("pl"),false) );
}
// -----------------------------------------------------------------------------
bool IsHTMLFile(const wxString& filename)
{
wxString ext = filename.AfterLast('.');
// if filename has no extension then ext == filename
if (ext == filename) return false;
return ( ext.IsSameAs(wxT("htm"),false) ||
ext.IsSameAs(wxT("html"),false) );
}
// -----------------------------------------------------------------------------
bool IsTextFile(const wxString& filename)
{
if (!IsHTMLFile(filename)) {
// if non-html file name contains "readme" then assume it's a text file
wxString name = filename.AfterLast(wxFILE_SEP_PATH).MakeLower();
if (name.Contains(wxT("readme"))) return true;
}
wxString ext = filename.AfterLast('.');
// if filename has no extension then ext == filename
if (ext == filename) return false;
return ( ext.IsSameAs(wxT("txt"),false) ||
ext.IsSameAs(wxT("doc"),false) );
}
// -----------------------------------------------------------------------------
bool IsZipFile(const wxString& filename)
{
wxString ext = filename.AfterLast('.');
// if filename has no extension then ext == filename
if (ext == filename) return false;
return ( ext.IsSameAs(wxT("zip"),false) ||
ext.IsSameAs(wxT("gar"),false) );
}
// -----------------------------------------------------------------------------
bool IsRuleFile(const wxString& filename)
{
wxString ext = filename.AfterLast('.');
// if filename has no extension then ext == filename
if (ext == filename) return false;
return ( ext.IsSameAs(wxT("rule"),false) ||
ext.IsSameAs(wxT("table"),false) ||
ext.IsSameAs(wxT("tree"),false) ||
ext.IsSameAs(wxT("colors"),false) ||
ext.IsSameAs(wxT("icons"),false) );
}
|