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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
|
/*
* TreeView
* A phylogenetic tree viewer.
* Copyright (C) 2001 Roderic D. M. Page <r.page@bio.gla.ac.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// $Id: tview.cpp,v 1.40 2007/03/09 13:59:19 rdmp1c Exp $
#ifdef __GNUG__
// #pragma implementation
#endif
#include <strstream>
// TreeLib (must go before any wx stuff to avoid clash between STL and Windows min/max)
#include "TreeLib.h"
#include "Parse.h"
#include "treedrawer.h"
#include "treeorder.h"
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if !wxUSE_DOC_VIEW_ARCHITECTURE
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
#endif
#include <wx/clipbrd.h>
#include <wx/fontdlg.h>
#include <wx/metafile.h>
#include <wx/filename.h>
// Use the native Mac OS X font dialog
#ifdef __WXMAC__
#undef wxFontDialog
#include "wx/mac/fontdlg.h"
#endif
// Pictures
#ifdef __WXMSW__
#else
#ifdef USE_SVG
#if wxMAJOR_VERSION >= 3
#include <wx/dcsvg.h>
#else
#include <wx/svg/dcsvg.h>
#endif
#endif
#endif
// GUI interface
#include "tv.h"
#include "tdoc.h"
#include "tview.h"
// Dialogs
#include "treeorder_dialog.h"
#if wxUSE_PRINTING_ARCHITECTURE
class WXDLLEXPORT wxTVPrintout : public wxDocPrintout
{
public:
wxTVPrintout (wxView *view = (wxView *) NULL, const wxString& title = wxT("Printout")) : wxDocPrintout (view, title) {};
bool OnPrintPage(int page);
};
#endif // wxUSE_PRINTING_ARCHITECTURE
//------------------------------------------------------------------------------
// The wxWindows Printing Framework scales the printout to the screen image,
// whereas I want to print the tree on the whole page, regardless of how it looks
// on screen. To do this we extend wxDocPrintout to compute the size of the page
// and to set the flag "printing" in the associated TView window before calling
// the ancestral OnPrintPage. This lets TView:OnDraw know whether to scale the tree
// for the screen or for the printed page.
bool wxTVPrintout::OnPrintPage(int page)
{
bool result = false;
TView *t = (TView *)GetView();
if (t)
{
int w_mm, h_mm;
GetPageSizeMM (&w_mm, &h_mm);
// Page size in points
t->page_width = (int) ((float)(w_mm * 72.0) / 25.4);
t->page_height = (int) ((float)(h_mm * 72.0) / 25.4);
t->printing = true;
}
result = wxDocPrintout::OnPrintPage (page);
if (t)
{
t->printing = false;
}
return result;
}
IMPLEMENT_DYNAMIC_CLASS(TView, wxView)
/*
BEGIN_EVENT_TABLE(DrawingView, wxView)
EVT_MENU(DOODLE_CUT, DrawingView::OnCut)
END_EVENT_TABLE() */
//------------------------------------------------------------------------------
TView::TView ()
{
canvas = (MyCanvas *) NULL;
frame = (wxMDIChildFrame *) NULL;
TreeStyleCmd = SLANTED_TREE_CMD;
Order = DEFAULT_ORDER;
LeafFont = *wxSWISS_FONT;
printing = false;
magnification = 1;
mShowInternalLabels = false;
}
//------------------------------------------------------------------------------
wxPrintout *TView::OnCreatePrintout()
{
wxDocPrintout *p = new wxTVPrintout (this);
return p;
}
//------------------------------------------------------------------------------
// What to do when a view is created. Creates actual
// windows for displaying the view.
bool TView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
{
frame = wxGetApp().CreateChildFrame(doc, this, TRUE);
frame->SetTitle(wxT("TView"));
canvas = GetMainFrame()->CreateCanvas(this, frame);
#ifdef __X__
// X seems to require a forced resize
int x, y;
frame->GetSize(&x, &y);
frame->SetSize(-1, -1, x, y);
#endif
frame->Show(TRUE);
Activate(TRUE);
return TRUE;
}
//------------------------------------------------------------------------------
void TView::OnSavePicture (wxCommandEvent& WXUNUSED(event))
{
#ifdef __WXMSW__
// Use metafiles for Windows
wxString pictureFileName = GetFrame()->GetTitle();
pictureFileName += wxT(".emf");
wxFrame *f = GetMainFrame();
wxFileDialog dialog((wxWindow *)f, wxT("Save Picture as"), wxT(""), pictureFileName,
wxT("Enhanced metafile (*.emf)|*.emf"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if (dialog.ShowModal() == wxID_OK)
{
wxMetafileDC pictureDC (dialog.GetPath(), 600, 650) ;
OnDraw (&pictureDC);
pictureDC.Close();
}
#else
#ifdef USE_SVG
wxString pictureFileName = frame->GetTitle();
// Use SVG on other platforms
pictureFileName += wxT(".svg");
#ifdef __WXMAC__
wxFrame *f = GetMainFrame();
#else
wxFrame *f = GetMainFrame();
#endif
wxFileDialog dialog((wxWindow *)f, wxT("Save Picture as"), wxT(""), pictureFileName,
wxT("SVG vector picture files (*.svg)|*.svg"),
wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if ((dialog.ShowModal() == wxID_OK) && (p.GetNumTrees() != 0))
{
wxSVGFileDC pictureDC (dialog.GetPath(), 600, 650) ;
OnDraw (&pictureDC);
}
#endif
#endif
}
//------------------------------------------------------------------------------
// Sneakily gets used for default print/preview
// as well as drawing on the screen.
void TView::OnDraw(wxDC *dc)
{
if (p.GetNumTrees() > 0)
{
dc->SetFont(LeafFont);
wxPen pen (*wxBLACK_PEN);
pen.SetCap (wxCAP_PROJECTING);
dc->SetPen(pen);
Tree t = p.GetCurrentTree();
t.Update();
int width, height;
if (printing)
{
width = page_width;
height = page_height;
#if defined(__WXGTK__) || defined(__WXMOTIF__)
height -= 72;
#endif
}
else
if (magnification == 1)
{
canvas->GetClientSize (&width, &height);
}
else
{
int ignore;
canvas->GetVirtualSize (&ignore, &height);
canvas->GetClientSize (&width, &ignore);
} wxRect r (0, 0, width, height);
#if defined(__WXGTK__) || defined(__WXMOTIF__)
if (printing)
{
r.Offset (0, 72);
}
#endif
// Ensure a little space around the tree
r.Inflate (-20, -20);
t.MakeNodeList();
int maxlen = 0;
for (int i = 0; i < t.GetNumLeaves(); i++)
{
wxCoord w, h;
Node *p;
p = t[i];
wxString s;
#if wxUSE_UNICODE
char buf[256];
strcpy (buf, p->GetLabel().c_str());
wchar_t wbuf[256];
mbstowcs (wbuf, buf, 256);
s << wbuf;
#else
s << p->GetLabel().c_str();
#endif
dc->GetTextExtent (s, &w, &h);
if (w > maxlen)
maxlen = w;
}
r.SetWidth (r.GetWidth() - maxlen);
if (Order != DEFAULT_ORDER)
{
TreeOrder *order = NULL;
switch (Order)
{
case ALPHA_ORDER:
order = new AlphaOrder (&t);
break;
case LEFT_ORDER:
order = new LeftOrder (&t);
break;
case RIGHT_ORDER:
order = new RightOrder (&t);
break;
}
order->Order ();
delete order;
}
TreeDrawer *td = NULL;
switch (TreeStyleCmd)
{
case SLANTED_TREE_CMD:
td = new TreeDrawer (&t);
break;
case RECTANGULAR_TREE_CMD:
td = new RectangleTreeDrawer (&t);
break;
case PHYLOGRAM_CMD:
td = new PhylogramDrawer (&t);
break;
}
td->SetDC (dc);
td->SetRect (r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
td->CalcCoordinates ();
td->SetPenWidth (2);
td->SetDrawInternalLabels (mShowInternalLabels);
td->Draw ();
delete td;
}
}
//------------------------------------------------------------------------------
void TView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
{
if (canvas)
canvas->Refresh();
/* Is the following necessary?
#ifdef __WXMSW__
if (canvas)
canvas->Refresh();
#else
if (canvas)
{
wxClientDC dc(canvas);
dc.Clear();
OnDraw(& dc);
}
#endif
*/
}
//------------------------------------------------------------------------------
// Clean up windows used for displaying the view.
bool TView::OnClose(bool deleteWindow)
{
if (!GetDocument()->Close())
return FALSE;
// Clear the canvas in case we're in single-window mode,
// and the canvas stays.
#if (wxMAJOR_VERSION >= 3 || wxMINOR_VERSION > 4) // from 2.5 Clear is deprecated
canvas->ClearBackground();
#else
canvas->Clear();
#endif
canvas->view = (wxView *) NULL;
canvas = (MyCanvas *) NULL;
wxString s(wxTheApp->GetAppName());
if (frame)
frame->SetTitle(s);
SetFrame((wxFrame*)NULL);
Activate(FALSE);
if (deleteWindow)
{
delete frame;
return TRUE;
}
return TRUE;
}
//------------------------------------------------------------------------------
bool TView::ReadTrees (const wxString &filename)
{
bool result = false;
ifstream f;
f.open (filename.mb_str(), ios::binary | ios::in);
if (p.ReadTrees (f))
{
// Set window name to that of file
wxFileName fn (filename);
wxString title = fn.GetName();
if (fn.HasExt())
{
title += wxT(".");
title += fn.GetExt();
}
frame->SetTitle(title);
result = true;
}
else
{
wxMessageBox(wxT("Failed to read trees"), wxT("ReadTrees"), wxICON_EXCLAMATION | wxOK);
result = false;
}
f.close();
return result;
}
//------------------------------------------------------------------------------
bool TView::WriteTrees (const wxString &filename)
{
bool result = false;
ofstream f (filename.mb_str());
if (p.WriteTrees (f))
{
// Set window name to that of file
wxFileName fn (filename);
wxString title = fn.GetName();
if (fn.HasExt())
{
title += wxT(".");
title += fn.GetExt();
}
frame->SetTitle(title);
result = true;
}
else
{
wxMessageBox(wxT("Failed to write trees"), wxT("TreeView X"), wxICON_EXCLAMATION | wxOK);
result = false;
}
f.close();
return result;
}
//------------------------------------------------------------------------------
void TView::OnActivateView (bool activate, wxView *activeView, wxView *deactiveView)
{
if (activate)
{
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
wxMenuBar* menuBar = frame->GetMenuBar();
if (p.GetNumTrees() > 0)
{
toolBar->EnableTool (PREVIOUS_TREE_CMD, (p.GetCurrentTreeNumber() > 0));
toolBar->EnableTool (NEXT_TREE_CMD, (p.GetCurrentTreeNumber() < (p.GetNumTrees() - 1)));
toolBar->EnableTool (SLANTED_TREE_CMD, true);
toolBar->EnableTool (RECTANGULAR_TREE_CMD, true);
toolBar->EnableTool (PHYLOGRAM_CMD, p.CurrentTreeHasEdgeLengths());
toolBar->ToggleTool (SLANTED_TREE_CMD, FALSE );
toolBar->ToggleTool (RECTANGULAR_TREE_CMD, FALSE );
toolBar->ToggleTool (PHYLOGRAM_CMD, FALSE );
#ifndef __WXMOTIF__
menuBar->Enable (COPY_AS_TEXT_CMD, true);
#endif
menuBar->Enable (SLANTED_TREE_CMD, true);
menuBar->Enable (RECTANGULAR_TREE_CMD, true);
menuBar->Enable (PHYLOGRAM_CMD, p.CurrentTreeHasEdgeLengths());
menuBar->Check (SLANTED_TREE_CMD, false );
menuBar->Check (RECTANGULAR_TREE_CMD, false );
menuBar->Check (PHYLOGRAM_CMD, false );
menuBar->Enable (CHOOSE_TREE_CMD, (p.GetNumTrees() > 1));
menuBar->Enable (INTERNAL_LABELS_CMD, p.CurrentTreeHasInternallabels());
menuBar->Check (INTERNAL_LABELS_CMD, mShowInternalLabels );
toolBar->EnableTool (INTERNAL_LABELS_CMD, p.CurrentTreeHasInternallabels());
toolBar->ToggleTool (INTERNAL_LABELS_CMD, mShowInternalLabels );
ShowTreeName ();
TreeStyleMenu (TreeStyleCmd);
}
else
{
toolBar->EnableTool (PREVIOUS_TREE_CMD, false);
toolBar->EnableTool (NEXT_TREE_CMD, false);
toolBar->EnableTool (PREVIOUS_TREE_CMD, false);
toolBar->EnableTool (SLANTED_TREE_CMD, false);
toolBar->EnableTool (RECTANGULAR_TREE_CMD, false);
toolBar->EnableTool (PHYLOGRAM_CMD, false);
menuBar->Enable (COPY_AS_TEXT_CMD, false);
menuBar->Enable (SLANTED_TREE_CMD, false);
menuBar->Enable (RECTANGULAR_TREE_CMD, false);
menuBar->Enable (PHYLOGRAM_CMD, false);
menuBar->Enable (INTERNAL_LABELS_CMD, false );
}
}
}
//------------------------------------------------------------------------------
void TView::PreviousTree (wxCommandEvent& WXUNUSED(event))
{
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
int n = p.GetCurrentTreeNumber ();
if (n >= 0)
{
n--;
p.SetCurrentTreeNumber (n);
if (n == 0)
toolBar->EnableTool (PREVIOUS_TREE_CMD, false);
toolBar->EnableTool (NEXT_TREE_CMD, true);
TreeStyleMenu(TreeStyleCmd);
ShowTreeName ();
OnUpdate (this, NULL); // Ensure we redraw window
}
}
//------------------------------------------------------------------------------
void TView::NextTree (wxCommandEvent& WXUNUSED(event))
{
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
int n = p.GetCurrentTreeNumber ();
if (n < p.GetNumTrees () - 1)
{
n++;
p.SetCurrentTreeNumber (n);
if (n == (p.GetNumTrees () - 1))
toolBar->EnableTool (NEXT_TREE_CMD, false);
toolBar->EnableTool (PREVIOUS_TREE_CMD, true);
TreeStyleMenu(TreeStyleCmd);
ShowTreeName ();
OnUpdate (this, NULL); // Ensure we redraw window
}
}
//------------------------------------------------------------------------------
void TView::ShowTreeName ()
{
wxString txt = wxT("");
Tree t = p.GetCurrentTree ();
if (t.GetName() != "")
{
#if wxUSE_UNICODE
char buf[256];
strcpy (buf, t.GetName().c_str());
wchar_t wbuf[256];
mbstowcs (wbuf, buf, 256);
txt << wbuf;
#else
txt << t.GetName().c_str();
#endif
txt << wxT(" ");
}
txt << wxT("(") << (p.GetCurrentTreeNumber() + 1) << wxT("/") << p.GetNumTrees() << wxT(")");
#if defined(__WXMSW__) || defined(__WXMAC__)
// Status bar for the view window
frame->SetStatusText (txt, 0);
#else
GetMainFrame()->SetStatusText (txt, 0);
#endif
}
//------------------------------------------------------------------------------
void TView::TreeStyleMenu (int style)
{
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
wxMenuBar* menuBar = frame->GetMenuBar();
toolBar->ToggleTool( TreeStyleCmd, FALSE );
menuBar->Check (TreeStyleCmd, FALSE);
// Ensure style is consistent with this tree (we may have a mix of trees with and without
// branch lengths and/or internal labels)
if (!p.CurrentTreeHasEdgeLengths() && (style == PHYLOGRAM_CMD))
{
menuBar->Enable (PHYLOGRAM_CMD, FALSE);
toolBar->EnableTool ( PHYLOGRAM_CMD, FALSE );
style = RECTANGULAR_TREE_CMD;
}
else if (p.CurrentTreeHasEdgeLengths())
{
menuBar->Enable (PHYLOGRAM_CMD, TRUE);
toolBar->EnableTool ( PHYLOGRAM_CMD, TRUE );
}
menuBar->Enable (INTERNAL_LABELS_CMD, p.CurrentTreeHasInternallabels());
menuBar->Check (INTERNAL_LABELS_CMD, mShowInternalLabels );
toolBar->EnableTool (INTERNAL_LABELS_CMD, p.CurrentTreeHasInternallabels());
toolBar->ToggleTool (INTERNAL_LABELS_CMD, mShowInternalLabels );
toolBar->ToggleTool( style, TRUE );
menuBar->Check (TreeStyleCmd, FALSE);
menuBar->Check (style, TRUE);
TreeStyleCmd = style;
}
//------------------------------------------------------------------------------
void TView::OnSlant (wxCommandEvent& WXUNUSED(event))
{
TreeStyleMenu (SLANTED_TREE_CMD);
OnUpdate (this, NULL); // Ensure we redraw window
}
//------------------------------------------------------------------------------
void TView::OnRectangle (wxCommandEvent& WXUNUSED(event))
{
TreeStyleMenu (RECTANGULAR_TREE_CMD);
OnUpdate (this, NULL); // Ensure we redraw window
}
//------------------------------------------------------------------------------
void TView::OnPhylogram (wxCommandEvent& WXUNUSED(event))
{
TreeStyleMenu (PHYLOGRAM_CMD);
OnUpdate (this, NULL); // Ensure we redraw window
}
//------------------------------------------------------------------------------
void TView::OnOrderTree (wxCommandEvent& WXUNUSED(event))
{
#ifdef __WXMAC__
TreeOrderDlg dialog((wxWindow *)GetFrame());
#else
TreeOrderDlg dialog((wxWindow *)GetMainFrame());
#endif
dialog.SetTreeOrder(Order);
dialog.Centre (wxBOTH);
if (dialog.ShowModal() == wxID_OK)
{
// Get data
Order = dialog.GetTreeOrder();
OnUpdate (this, NULL); // Ensure we redraw window
}
}
//------------------------------------------------------------------------------
void TView::OnInternalLabels (wxCommandEvent& WXUNUSED(event))
{
mShowInternalLabels = !mShowInternalLabels;
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->ToggleTool( INTERNAL_LABELS_CMD, mShowInternalLabels );
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Check (INTERNAL_LABELS_CMD, mShowInternalLabels);
OnUpdate (this, NULL); // Ensure we redraw window
}
//------------------------------------------------------------------------------
void TView::OnLeafFont (wxCommandEvent& WXUNUSED(event))
{
#if 0
TreeOrderDlg *dlg = new TreeOrderDlg(frame);
dlg->ShowModal();
#else
wxFontData data;
data.SetInitialFont (LeafFont);
#if wxMAJOR_VERSION >= 3 || defined __WXMAC__
wxFontDialog dialog((wxWindow *)GetFrame(), data);
#else
wxFontDialog dialog((wxWindow *)GetMainFrame(), &data);
#endif
if ( dialog.ShowModal() == wxID_OK )
{
wxFontData retData = dialog.GetFontData();
LeafFont = retData.GetChosenFont();
OnUpdate (this, NULL); // Ensure we redraw window
}
#endif
}
//------------------------------------------------------------------------------
void TView::OnChooseTree (wxCommandEvent& WXUNUSED(event))
{
wxString *choices = new wxString[p.GetNumTrees()];
for (int i = 0; i < p.GetNumTrees(); i++)
{
#if wxUSE_UNICODE
char buf[256];
strcpy (buf, p.GetIthTreeName(i).c_str());
wchar_t wbuf[256];
mbstowcs (wbuf, buf, 256);
std::wstring tname = wbuf;
// If tree has no name create one from it's place in the profile
if (tname == L"")
{
wchar_t buf[32];
swprintf (buf, wcslen(buf), L"%d", int (i+1));
tname += buf;
}
choices[i] = tname.c_str();
#else
std::string tname = p.GetIthTreeName(i);
// If tree has no name create one from it's place in the profile
if (tname == "")
{
char buf[32];
sprintf (buf, "%d", (i+1));
tname += buf;
}
choices[i] = tname.c_str();
#endif
}
wxSingleChoiceDialog dialog(frame, wxT("Tree names"),
wxT("Choose a tree"), p.GetNumTrees(), choices);
dialog.SetSelection(p.GetCurrentTreeNumber());
/*
From charles@plessy.org 13 February 2007 14:55:04 GMT
An Ubuntu user found a bug in TreeView X, and a patch was made by
William Alexander Grant, an Ubuntu developper (CCed). I tested it and
confirmed that it efficiently fixes the problem.
The bug is described in this page:
https://launchpad.net/ubuntu/+source/treeviewx/+bug/75137
(actually, the segfaulting option name seems to be "choose tree")
You can find the pach here:
http://patches.ubuntu.com/t/treeviewx/treeviewx_0.5.1-2ubuntu1.patch
Or more simply:
diff -urNad treeviewx-0.5.1~/tview.cpp treeviewx-0.5.1/tview.cpp
--- treeviewx-0.5.1~/tview.cpp 2006-12-17 10:33:21.000000000 +1100
+++ treeviewx-0.5.1/tview.cpp 2006-12-17 10:33:53.000000000 +1100
@@ -742,7 +742,7 @@
dialog.SetSelection(p.GetCurrentTreeNumber());
- if (dialog.ShowModal() == wxID_OK)
+ if ((dialog.ShowModal() == wxID_OK) && (p.GetNumTrees() != 0))
{
int j = dialog.GetSelection ();
#ifdef __WXMAC__
By the way, do not hesitate to advertise the Debian and Ubuntu packages
on your website. They are more up to date than the RPM, and support SVG.
*/
if ((dialog.ShowModal() == wxID_OK) && (p.GetNumTrees() != 0))
{
int j = dialog.GetSelection ();
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->EnableTool (PREVIOUS_TREE_CMD, (j != 0));
toolBar->EnableTool (NEXT_TREE_CMD, (j < p.GetNumTrees()-1));
p.SetCurrentTreeNumber (j);
TreeStyleMenu(TreeStyleCmd);
ShowTreeName ();
OnUpdate (this, NULL); // Ensure we redraw window
}
delete [] choices;
}
#ifndef __WXMOTIF__
// ----------------------------------------------------------------------------
void TView::OnUpdateCopyCommand(wxUpdateUIEvent& event)
{
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Enable (wxID_COPY, (p.GetNumTrees() > 0));
menuBar->Enable (COPY_AS_TEXT_CMD, (p.GetNumTrees() > 0));
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->EnableTool (wxID_COPY, (p.GetNumTrees() > 0));
}
// ----------------------------------------------------------------------------
void TView::OnUpdatePasteCommand(wxUpdateUIEvent& event)
{
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Enable (wxID_PASTE, (p.GetNumTrees() == 0) && wxTheClipboard->IsSupported( wxDF_TEXT ));
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->EnableTool (wxID_PASTE, (p.GetNumTrees() == 0) && wxTheClipboard->IsSupported( wxDF_TEXT ));
}
// ----------------------------------------------------------------------------
void TView::OnCopy (wxCommandEvent& WXUNUSED(event))
{
#if defined(__WXMAC__) || defined(__WXMSW__)
// Get current tree
Tree t = p.GetCurrentTree();
t.Update();
// Write tree to clipboard as a metafile
wxMetafileDC dc;
if (dc.Ok())
{
OnDraw (&dc);
wxMetafile *mf = dc.Close();
if (mf)
{
bool success = mf->SetClipboard((int)(dc.MaxX() + 10), (int)(dc.MaxY() + 10));
delete mf;
}
}
#endif
}
// ----------------------------------------------------------------------------
void TView::OnCopyAsText (wxCommandEvent& WXUNUSED(event))
{
// Get current tree
Tree t = p.GetCurrentTree();
t.Update();
// Write tree to a string stream
ostrstream f;
NewickTreeWriter tw (&t);
tw.SetStream (&f);
tw.Write();
f << '\0';
// Store on clipboard
if (wxTheClipboard->Open())
{
// Data objects are held by the clipboard,
// so do not delete them in the app.
string str = f.str();
wxTheClipboard->SetData( new wxTextDataObject( wxString(str.c_str(), *wxConvCurrent)) );
wxTheClipboard->Close();
}
}
// ----------------------------------------------------------------------------
void TView::OnPaste (wxCommandEvent& WXUNUSED(event))
{
// Paste tree description from clipboard into this window -- duh! need to make this a call to the main frame too
if (wxTheClipboard->Open())
{
if (wxTheClipboard->IsSupported( wxDF_TEXT ))
{
wxTextDataObject data;
wxTheClipboard->GetData( data );
// Temporary stream to hold clipbard text
istrstream f (data.GetText().mb_str());
if (p.ReadTrees (f))
{
Activate (TRUE); // ensure menus are set correctly
OnUpdate (this, NULL); // ensure tree is drawn
GetDocument()->Modify (TRUE); // tell document that content has changed
}
}
wxTheClipboard->Close();
}
}
#endif
//------------------------------------------------------------------------------
void TView::ZoomToFit (wxCommandEvent& WXUNUSED(event))
{
magnification = 1;
Resize();
}
//------------------------------------------------------------------------------
void TView::ZoomIn (wxCommandEvent& WXUNUSED(event))
{
magnification++;
Resize();
}
//------------------------------------------------------------------------------
void TView::ZoomOut (wxCommandEvent& WXUNUSED(event))
{
magnification--;
Resize();
}
//------------------------------------------------------------------------------
void TView::Resize ()
{
canvas->SetMagnification (magnification);
canvas->Resize ();
OnUpdate (this, NULL); // Ensure we redraw window
}
// ----------------------------------------------------------------------------
void TView::OnUpdateZoomToFitCommand (wxUpdateUIEvent& event)
{
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Enable (ZOOM_TO_FIT_CMD, (magnification != 1));
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
//toolBar->EnableTool (ZOOM_TO_FIT_CMD, (magnification != 1));
}
// ----------------------------------------------------------------------------
void TView::OnUpdateZoomInCommand (wxUpdateUIEvent& event)
{
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Enable (ZOOM_IN_CMD, (magnification < MAX_MAGNIFICATION));
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->EnableTool (ZOOM_IN_CMD, (magnification < MAX_MAGNIFICATION));
}
// ----------------------------------------------------------------------------
void TView::OnUpdateZoomOutCommand (wxUpdateUIEvent& event)
{
wxMenuBar* menuBar = frame->GetMenuBar();
menuBar->Enable (ZOOM_OUT_CMD, (magnification > 1));
#ifdef __WXMAC__
wxToolBar* toolBar = frame->GetToolBar();
#else
wxToolBar* toolBar = GetMainFrame()->GetToolBar();
#endif
toolBar->EnableTool (ZOOM_OUT_CMD, (magnification > 1));
}
//------------------------------------------------------------------------------
// Map commands to methods in TView
BEGIN_EVENT_TABLE(TView, wxView)
EVT_MENU(NEXT_TREE_CMD, TView::NextTree)
EVT_MENU(PREVIOUS_TREE_CMD, TView::PreviousTree)
EVT_MENU(SLANTED_TREE_CMD, TView::OnSlant)
EVT_MENU(RECTANGULAR_TREE_CMD, TView::OnRectangle)
EVT_MENU(PHYLOGRAM_CMD, TView::OnPhylogram)
EVT_MENU(INTERNAL_LABELS_CMD, TView::OnInternalLabels)
EVT_MENU(LEAF_FONT_CMD, TView::OnLeafFont)
EVT_MENU(CHOOSE_TREE_CMD, TView::OnChooseTree)
EVT_MENU(ORDER_TREE_CMD, TView::OnOrderTree)
#if defined __WXMSW__ || defined USE_SVG
EVT_MENU(SAVEAS_PICTURE_CMD, TView::OnSavePicture)
#endif
#ifndef __WXMOTIF__
EVT_MENU(wxID_COPY, TView::OnCopy)
EVT_MENU(COPY_AS_TEXT_CMD, TView::OnCopyAsText)
EVT_MENU(wxID_PASTE, TView::OnPaste)
#endif
EVT_MENU(ZOOM_TO_FIT_CMD, TView::ZoomToFit)
EVT_MENU(ZOOM_IN_CMD, TView::ZoomIn)
EVT_MENU(ZOOM_OUT_CMD, TView::ZoomOut)
#ifndef __WXMOTIF__
EVT_UPDATE_UI(wxID_COPY, TView::OnUpdateCopyCommand)
EVT_UPDATE_UI(COPY_AS_TEXT_CMD, TView::OnUpdateCopyCommand)
EVT_UPDATE_UI(wxID_PASTE, TView::OnUpdatePasteCommand)
#endif
EVT_UPDATE_UI(ZOOM_TO_FIT_CMD, TView::OnUpdateZoomToFitCommand)
EVT_UPDATE_UI(ZOOM_IN_CMD, TView::OnUpdateZoomInCommand)
EVT_UPDATE_UI(ZOOM_OUT_CMD, TView::OnUpdateZoomOutCommand)
END_EVENT_TABLE()
/*
* Window implementations
*/
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
EVT_SIZE(MyCanvas::OnSize)
END_EVENT_TABLE()
// Define a constructor for my canvas
MyCanvas::MyCanvas(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
wxScrolledWindow(frame, -1, pos, size, style)
{
SetBackgroundColour(wxColour(wxT("WHITE")));
view = v;
magnification = 1;
}
//------------------------------------------------------------------------------
// Define the repainting behaviour
void MyCanvas::OnDraw(wxDC& dc)
{
if (view)
view->OnDraw(& dc);
}
// ----------------------------------------------------------------------------
void MyCanvas::OnSize(wxSizeEvent& event)
{
Resize ();
Refresh();
event.Skip();
}
// ----------------------------------------------------------------------------
void MyCanvas::Resize()
{
int width, height;
GetClientSize (&width, &height);
int range = height * magnification;
int steps = range / 50;
SetScrollbars(0, steps, 0, 50);
}
//------------------------------------------------------------------------------
// This implements a tiny doodling program. Drag the mouse using
// the left button.
void MyCanvas::OnMouseEvent(wxMouseEvent& event)
{
}
|