Menu

[r2734]: / trunk / src / plugins / scriptedwizard / wiz.cpp  Maximize  Restore  History

Download this file

1127 lines (995 with data), 35.2 kB

   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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
/***************************************************************
* Name: wiz.cpp
* Purpose: Code::Blocks plugin
* Author: <>
* Copyright: (c)
* License: GPL
**************************************************************/
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma implementation "wiz.h"
#endif
#include "wiz.h"
#include <wx/dir.h>
#include <wx/intl.h>
#include <wx/xrc/xmlres.h>
#include <wx/wizard.h>
#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/panel.h>
#include <wx/checkbox.h>
#include <wx/combobox.h>
#include <globals.h>
#include <cbexception.h>
#include <manager.h>
#include <configmanager.h>
#include <projectmanager.h>
#include <scriptingmanager.h>
#include <compilerfactory.h>
#include <compiler.h>
#include <projectbuildtarget.h>
#include <filefilters.h>
#include <licenses.h> // defines some common licenses (like the GPL)
#include <scripting/bindings/sc_base_types.h>
#include "wizpage.h"
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(Wizards);
// Implement the plugin's hooks
CB_IMPLEMENT_PLUGIN(Wiz, "Scripted wizard");
// scripting support
DECLARE_INSTANCE_TYPE(Wiz);
Wiz::Wiz()
: m_pWizard(0),
m_pWizProjectPathPanel(0),
m_pWizFilePathPanel(0),
m_pWizCompilerPanel(0),
m_pWizBuildTargetPanel(0)
{
//ctor
m_PluginInfo.name = _T("ScriptedWizard");
m_PluginInfo.title = _("Scripted wizard");
m_PluginInfo.version = _T("0.9");
m_PluginInfo.description = _("A generic scripted wizard");
m_PluginInfo.author = _T("Yiannis Mandravellos");
m_PluginInfo.authorEmail = _T("mandrav@codeblocks.org");
m_PluginInfo.authorWebsite = _T("http://www.codeblocks.org");
m_PluginInfo.thanksTo = _("");
m_PluginInfo.license = LICENSE_GPL;
m_TemplatePath = ConfigManager::GetDataFolder() + _T("/templates/wizard/");
}
Wiz::~Wiz()
{
//dtor
}
void Wiz::OnAttach()
{
// make sure the VM is initialized
Manager::Get()->GetScriptingManager();
if (!SquirrelVM::GetVMPtr())
{
cbMessageBox(_("Project wizard disabled: scripting not initialized"), _("Error"), wxICON_ERROR);
return;
}
// read configuration
RegisterWizard();
// run main wizard script
// this registers all available wizard scripts with us
Manager::Get()->GetScriptingManager()->LoadScript(m_TemplatePath + _T("config.script"));
try
{
SqPlus::SquirrelFunction<void> f("RegisterWizards");
f();
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
}
}
int Wiz::GetCount() const
{
// return the number of template wizards contained in this plugin
return m_Wizards.GetCount();
}
TemplateOutputType Wiz::GetOutputType(int index) const
{
//return this wizard's output type
//make sure you set this!
cbAssert(index >= 0 && index < GetCount());
return m_Wizards[index].output_type;
}
wxString Wiz::GetTitle(int index) const
{
//return this wizard's title
//this will appear in the new-project dialog
//make sure you set this!
cbAssert(index >= 0 && index < GetCount());
return m_Wizards[index].title;
}
wxString Wiz::GetDescription(int index) const
{
//return this wizard's description
//make sure you set this!
cbAssert(index >= 0 && index < GetCount());
return m_PluginInfo.description;
}
wxString Wiz::GetCategory(int index) const
{
//return this wizard's category
//try to match an existing category
//make sure you change this!
cbAssert(index >= 0 && index < GetCount());
return m_Wizards[index].cat;
}
const wxBitmap& Wiz::GetBitmap(int index) const
{
//return this wizard's bitmap
//this will appear in the new-project dialog
cbAssert(index >= 0 && index < GetCount());
return m_Wizards[index].templatePNG;
}
wxString Wiz::GetScriptFilename(int index) const
{
//return this wizard's script filename
cbAssert(index >= 0 && index < GetCount());
return m_Wizards[index].script;
}
void Wiz::Clear()
{
if (m_pWizard)
m_pWizard->Destroy();
m_pWizard = 0;
m_Pages.Clear();
// if the ABI is not sufficient, we 're in trouble the next time the wizard runs...
#if wxABI_VERSION > 20601
if (!m_LastXRC.IsEmpty())
wxXmlResource::Get()->Unload(m_LastXRC);
#endif
m_pWizProjectPathPanel = 0;
m_pWizCompilerPanel = 0;
m_pWizBuildTargetPanel = 0;
m_pWizFilePathPanel = 0;
}
CompileTargetBase* Wiz::Launch(int index, wxString* pFilename)
{
// TODO: pFilename is not used
cbAssert(index >= 0 && index < GetCount());
// early check: build target wizards need an active project
if (m_Wizards[index].output_type == totTarget &&
!Manager::Get()->GetProjectManager()->GetActiveProject())
{
cbMessageBox(_("You need to open (or create) a project first!"), _("Error"), wxICON_ERROR);
return 0;
}
m_LaunchIndex = index;
wxString script = m_TemplatePath + m_Wizards[index].script;
wxString commons = m_TemplatePath + _T("common_functions.script");
m_LastXRC = m_TemplatePath + m_Wizards[index].xrc;
if (wxFileExists(m_LastXRC))
wxXmlResource::Get()->Load(m_LastXRC);
else
m_LastXRC.Clear();
// create wizard
m_pWizard = new wxWizard;
m_pWizard->Create(Manager::Get()->GetAppWindow(), wxID_ANY,
m_Wizards[index].title,
m_Wizards[index].wizardPNG,
wxDefaultPosition,
wxDEFAULT_DIALOG_STYLE);
if (!Manager::Get()->GetScriptingManager()->LoadScript(commons) || // load common functions
!Manager::Get()->GetScriptingManager()->LoadScript(script)) // build and run script
{
// any errors have been displayed by ScriptingManager
Clear();
return 0;
}
// call BeginWizard()
try
{
SqPlus::SquirrelFunction<void> f("BeginWizard");
f();
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
Clear();
return 0;
}
// check if *any* pages were added
if (m_Pages.GetCount() == 0)
{
cbMessageBox(m_Wizards[index].title + _(" has failed to run..."), _("Error"), wxICON_ERROR);
Clear();
return 0;
}
// check if *mandatory* pages (i.e. used by the following code) were added
// currently, project path is a mandatory page for new projects...
if (m_Wizards[index].output_type == totProject && !m_pWizProjectPathPanel)
{
cbMessageBox(_("This wizard is missing the following mandatory wizard page:\n\n"
"Project path selection\n"
"Execution aborted..."), _("Error"), wxICON_ERROR);
Clear();
return 0;
}
// build the wizard pages
Finalize();
// run wizard
CompileTargetBase* base = 0; // ret value
if (m_pWizard->RunWizard(m_Pages[0]))
{
// ok, wizard done
switch (m_Wizards[index].output_type)
{
case totProject: base = RunProjectWizard(pFilename); break;
case totTarget: base = RunTargetWizard(pFilename); break;
case totFiles: base = RunFilesWizard(pFilename); break;
case totCustom: base = RunCustomWizard(pFilename); break;
default: break;
}
}
Clear();
return base;
}
CompileTargetBase* Wiz::RunProjectWizard(wxString* pFilename)
{
cbProject* theproject = 0;
// first get the project filename
wxString prjname = GetProjectFullFilename();
// create the dir for the project
wxFileName fname(prjname);
wxString prjdir = fname.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
if (!CreateDirRecursively(prjdir))
{
cbMessageBox(_("Couldn't create the project directory:\n") + prjdir, _("Error"), wxICON_ERROR);
Clear();
return 0;
}
// now create the project
theproject = Manager::Get()->GetProjectManager()->NewProject(prjname);
if (!theproject)
{
cbMessageBox(_("Couldn't create the new project:\n") + prjdir, _("Error"), wxICON_ERROR);
Clear();
return 0;
}
// set the project title and project-wide compiler
theproject->SetTitle(GetProjectTitle());
theproject->SetCompilerID(GetCompilerID());
// create the targets
if (GetWantDebug())
{
ProjectBuildTarget* target = theproject->AddBuildTarget(GetDebugName());
if (target)
{
target->SetCompilerID(GetCompilerID());
target->SetIncludeInTargetAll(false);
// target->SetOutputFilename(GetDebugOutputDir() + wxFILE_SEP_PATH + GetProjectName() + ext);
target->SetObjectOutput(GetDebugObjectOutputDir());
}
}
if (GetWantRelease())
{
ProjectBuildTarget* target = theproject->AddBuildTarget(GetReleaseName());
if (target)
{
target->SetCompilerID(GetCompilerID());
target->SetIncludeInTargetAll(false);
// target->SetOutputFilename(GetReleaseOutputDir() + wxFILE_SEP_PATH + GetProjectName() + ext);
target->SetObjectOutput(GetReleaseObjectOutputDir());
}
}
// add all the template files
// first get the dirs with the files by calling GetFilesDir()
wxString srcdir;
try
{
SqPlus::SquirrelFunction<wxString&> f("GetFilesDir");
if (!f.func.IsNull())
srcdir = f();
if (!srcdir.IsEmpty())
{
// now break them up (remember: semicolon-separated list of dirs)
wxArrayString tmpsrcdirs = GetArrayFromString(srcdir, _T(";"), true);
// and copy files from each source dir we got
for (size_t i = 0; i < tmpsrcdirs.GetCount(); ++i)
CopyFiles(theproject, prjdir, tmpsrcdirs[i]);
}
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
Clear();
return 0;
}
// add generated files
try
{
SqPlus::SquirrelFunction<wxString&> f("GetGeneratedFile");
if (!f.func.IsNull())
{
wxArrayString files;
wxArrayString contents;
int idx = 0;
// safety limit to avoid infinite loops because of badly written scripts: 50 files
while (idx < 50)
{
wxString fileAndContents = f(idx++);
if (fileAndContents.IsEmpty())
break;
wxString tmpFile = fileAndContents.BeforeFirst(_T(';'));
wxString tmpContents = fileAndContents.AfterFirst(_T(';'));
tmpFile.Trim();
tmpContents.Trim();
if (tmpFile.IsEmpty() || tmpContents.IsEmpty())
break;
files.Add(tmpFile);
contents.Add(tmpContents);
};
if (files.GetCount() != 0 && contents.GetCount() == files.GetCount())
{
// ok, we have to generate some files here
size_t count = files.GetCount();
for (size_t i = 0; i < count; ++i)
{
// GenerateFile() performs sanity and security checks
wxString actual = GenerateFile(theproject->GetBasePath(), files[i], contents[i]);
if (!actual.IsEmpty())
{
DBGLOG(_T("Generated file %s"), actual.c_str());
// add it to the project
ProjectFile* pf = theproject->AddFile(0, actual);
// to all targets...
for (int x = 1; x < theproject->GetBuildTargetsCount(); ++x)
{
pf->AddBuildTarget(theproject->GetBuildTarget(x)->GetTitle());
}
}
}
}
}
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
Clear();
return 0;
}
// if (srcdir.IsEmpty())
// cbMessageBox(_("The wizard didn't provide any files to copy!"), _("Warning"), wxICON_WARNING);
// ask the script to setup the new project (edit targets, setup options, etc)
// call SetupProject()
try
{
SqPlus::SquirrelFunction<bool> f("SetupProject");
if (!f(theproject))
{
cbMessageBox(wxString::Format(_("Couldn't setup project options:\n%s"),
prjdir.c_str()),
_("Error"), wxICON_ERROR);
Clear();
return 0;
}
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
Clear();
return 0;
}
// save the project and...
theproject->Save();
if (pFilename)
*pFilename = theproject->GetFilename();
// finally, make sure everything looks ok
Manager::Get()->GetProjectManager()->RebuildTree();
Manager::Get()->GetProjectManager()->GetTree()->Expand(theproject->GetProjectNode());
return theproject;
}
CompileTargetBase* Wiz::RunTargetWizard(wxString* pFilename)
{
cbProject* theproject = Manager::Get()->GetProjectManager()->GetActiveProject(); // can't fail; if no project, the wizard didn't even run
ProjectBuildTarget* target = theproject->AddBuildTarget(GetTargetName());
if (!target)
{
cbMessageBox(_("Failed to create build target!"), _("Error"), wxICON_ERROR);
Clear();
return 0;
}
// setup the target
target->SetCompilerID(GetTargetCompilerID());
target->SetIncludeInTargetAll(false);
target->SetObjectOutput(GetTargetObjectOutputDir());
target->SetWorkingDir(GetTargetOutputDir());
// add all the template files (if any)
// first get the dirs with the files by calling GetFilesDir()
// wxString srcdir;
// try
// {
// SqPlus::SquirrelFunction<wxString&> f("GetFilesDir");
// srcdir = f();
// if (!srcdir.IsEmpty())
// {
// // now break them up (remember: semicolon-separated list of dirs)
// wxArrayString tmpsrcdirs = GetArrayFromString(srcdir, _T(";"), true);
// // and copy files from each source dir we got
// for (size_t i = 0; i < tmpsrcdirs.GetCount(); ++i)
// CopyFiles(theproject, prjdir, tmpsrcdirs[i]);
// }
// }
// catch (SquirrelError& e)
// {
// Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
// Clear();
// return 0;
// }
// ask the script to setup the new target (setup options, etc)
// call SetupTarget()
try
{
SqPlus::SquirrelFunction<bool> f("SetupTarget");
if (!f(target, GetTargetEnableDebug()))
{
cbMessageBox(_("Couldn't setup target options:"), _("Error"), wxICON_ERROR);
Clear();
return 0;
}
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
Clear();
return 0;
}
return target;
}
CompileTargetBase* Wiz::RunFilesWizard(wxString* pFilename)
{
try
{
SqPlus::SquirrelFunction<wxString&> f("CreateFiles");
wxString files = f();
if (files.IsEmpty())
cbMessageBox(_("Wizard failed..."), _("Error"), wxICON_ERROR);
else
{
if (pFilename)
*pFilename = files.BeforeFirst(_T(';'));
}
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
}
Clear();
return 0;
}
CompileTargetBase* Wiz::RunCustomWizard(wxString* pFilename)
{
try
{
SqPlus::SquirrelFunction<bool> f("SetupCustom");
if (!f())
cbMessageBox(_("Wizard failed..."), _("Error"), wxICON_ERROR);
}
catch (SquirrelError& e)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&e);
}
Clear();
return 0;
}
wxString Wiz::GenerateFile(const wxString& basePath, const wxString& filename, const wxString& contents)
{
wxFileName fname(filename);
// extension sanity check
FileType ft = FileTypeOf(fname.GetFullPath());
switch (ft)
{
case ftCodeBlocksProject:
case ftCodeBlocksWorkspace:
case ftExecutable:
case ftDynamicLib:
case ftStaticLib:
case ftResourceBin:
case ftObject:
case ftOther:
DBGLOG(_T("Attempt to generate a file with forbidden extension!\nFile: %s"), fname.GetFullPath().c_str());
return wxEmptyString;
default: break;
}
// make sure filename is relative
if (!fname.IsRelative())
fname.MakeRelativeTo(basePath);
// make sure filename is located inside the project path (should already be)
if (fname.GetFullPath().StartsWith(_T("..")))
{
// attempt to create file outside the project dir
// remove any path info from the filename
fname = fname.GetFullName();
DBGLOG(_T("Attempt to generate a file outside the project base dir:\nOriginal: %s\nConverted to:%s"), filename.c_str(), fname.GetFullPath().c_str());
}
fname = basePath + wxFILE_SEP_PATH + fname.GetFullName();
// create the file with the passed contents
wxFile f(fname.GetFullPath(), wxFile::write);
if (cbWrite(f, contents + _T('\n')))
return fname.GetFullPath(); // success
return wxEmptyString; // failed
}
void Wiz::CopyFiles(cbProject* theproject, const wxString& prjdir, const wxString& srcdir)
{
// first get the dir with the files
wxArrayString filesList;
wxString enumdirs = GetTemplatePath() + srcdir;
wxString basepath = wxFileName(enumdirs).GetFullPath();
// recursively enumerate all files under srcdir
wxDir::GetAllFiles(enumdirs, &filesList);
// now get each file and copy it to the destination directory,
// adding it to all targets in the project
for (unsigned int i = 0; i < filesList.GetCount(); ++i)
{
wxString srcfile = filesList[i];
wxString dstfile = srcfile;
// fixup destination filename (remove srcdir from path)
dstfile.Replace(basepath, prjdir);
// make sure the destination directory exists
wxFileName fname(dstfile);
wxString dstdir = fname.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
CreateDirRecursively(dstdir);
// copy the file
wxCopyFile(srcfile, dstfile, true);
// and add it to the project
fname.MakeRelativeTo(prjdir);
ProjectFile* pf = theproject->AddFile(0, fname.GetFullPath());
// to all targets...
for (int x = 1; x < theproject->GetBuildTargetsCount(); ++x)
{
pf->AddBuildTarget(theproject->GetBuildTarget(x)->GetTitle());
}
}
}
////////////////////////
// Scripting - BEGIN
////////////////////////
TemplateOutputType Wiz::GetWizardType()
{
cbAssert(m_LaunchIndex >= 0 && m_LaunchIndex < GetCount());
return m_Wizards[m_LaunchIndex].output_type;
}
void Wiz::FillComboboxWithCompilers(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxComboBox* win = dynamic_cast<wxComboBox*>(page->FindWindowByName(name, page));
if (win && win->GetCount() == 0)
{
for (size_t i = 0; i < CompilerFactory::GetCompilersCount(); ++i)
{
win->Append(CompilerFactory::GetCompiler(i)->GetName());
}
win->SetSelection(win->FindString(CompilerFactory::GetDefaultCompiler()->GetName()));
}
}
}
void Wiz::EnableWindow(const wxString& name, bool enable)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxWindow* win = page->FindWindowByName(name, page);
if (win)
win->Enable(enable);
}
}
void Wiz::SetComboboxSelection(const wxString& name, int sel)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxComboBox* win = dynamic_cast<wxComboBox*>(page->FindWindowByName(name, page));
if (win)
win->SetSelection(sel);
}
}
wxString Wiz::GetCompilerFromCombobox(const wxString& name)
{
int id = GetComboboxSelection(name);
Compiler* compiler = CompilerFactory::GetCompiler(id);
if (compiler)
return compiler->GetID();
return wxEmptyString;
}
wxString Wiz::GetComboboxStringSelection(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxComboBox* win = dynamic_cast<wxComboBox*>(page->FindWindowByName(name, page));
if (win)
return win->GetStringSelection();
}
return wxEmptyString;
}
int Wiz::GetComboboxSelection(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxComboBox* win = dynamic_cast<wxComboBox*>(page->FindWindowByName(name, page));
if (win)
return win->GetSelection();
}
return -1;
}
int Wiz::GetRadioboxSelection(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxRadioBox* win = dynamic_cast<wxRadioBox*>(page->FindWindowByName(name, page));
if (win)
return win->GetSelection();
}
return -1;
}
void Wiz::SetRadioboxSelection(const wxString& name, int sel)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxRadioBox* win = dynamic_cast<wxRadioBox*>(page->FindWindowByName(name, page));
if (win)
win->SetSelection(sel);
}
}
int Wiz::GetListboxSelection(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxListBox* win = dynamic_cast<wxListBox*>(page->FindWindowByName(name, page));
if (win)
return win->GetSelection();
}
return -1;
}
void Wiz::SetListboxSelection(const wxString& name, int sel)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxListBox* win = dynamic_cast<wxListBox*>(page->FindWindowByName(name, page));
if (win)
win->SetSelection(sel);
}
}
void Wiz::CheckCheckbox(const wxString& name, bool check)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxCheckBox* win = dynamic_cast<wxCheckBox*>(page->FindWindowByName(name, page));
if (win)
win->SetValue(check);
}
}
bool Wiz::IsCheckboxChecked(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxCheckBox* win = dynamic_cast<wxCheckBox*>(page->FindWindowByName(name, page));
if (win)
return win->IsChecked();
}
return false;
}
void Wiz::SetTextControlValue(const wxString& name, const wxString& value)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxTextCtrl* win = dynamic_cast<wxTextCtrl*>(page->FindWindowByName(name, page));
if (win)
win->SetValue(value);
}
}
wxString Wiz::GetTextControlValue(const wxString& name)
{
wxWizardPage* page = m_pWizard->GetCurrentPage();
if (page)
{
wxTextCtrl* win = dynamic_cast<wxTextCtrl*>(page->FindWindowByName(name, page));
if (win)
return win->GetValue();
}
return wxEmptyString;
}
void Wiz::AddIntroPage(const wxString& intro_msg)
{
WizIntroPanel* page = new WizIntroPanel(intro_msg, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG);
m_Pages.Add(page);
}
void Wiz::AddFilePathPage(bool showHeaderGuard)
{
if (m_pWizFilePathPanel)
return; // already added
m_pWizFilePathPanel = new WizFilePathPanel(showHeaderGuard, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG);
m_Pages.Add(m_pWizFilePathPanel);
}
void Wiz::AddProjectPathPage()
{
if (m_pWizProjectPathPanel)
return; // already added
m_pWizProjectPathPanel = new WizProjectPathPanel(m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG);
m_Pages.Add(m_pWizProjectPathPanel);
}
void Wiz::AddCompilerPage(const wxString& compilerID, const wxString& validCompilerIDs, bool allowCompilerChange, bool allowConfigChange)
{
if (m_pWizCompilerPanel)
return; // already added
m_pWizCompilerPanel = new WizCompilerPanel(compilerID, validCompilerIDs, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG, allowCompilerChange, allowConfigChange);
m_Pages.Add(m_pWizCompilerPanel);
}
void Wiz::AddBuildTargetPage(const wxString& targetName, bool isDebug, bool showCompiler, const wxString& compilerID, const wxString& validCompilerIDs, bool allowCompilerChange)
{
if (m_pWizBuildTargetPanel)
return; // already added
m_pWizBuildTargetPanel = new WizBuildTargetPanel(targetName, isDebug, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG, showCompiler, compilerID, validCompilerIDs, allowCompilerChange);
m_Pages.Add(m_pWizBuildTargetPanel);
}
void Wiz::AddGenericSingleChoiceListPage(const wxString& pageName, const wxString& descr, const wxString& choices, int defChoice)
{
// we don't track this; can add more than one
m_Pages.Add(new WizGenericSingleChoiceList(pageName, descr, GetArrayFromString(choices, _T(";")), defChoice, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG));
}
void Wiz::AddGenericSelectPathPage(const wxString& pageId, const wxString& descr, const wxString& label, const wxString& defValue)
{
// we don't track this; can add more than one
m_Pages.Add(new WizGenericSelectPathPanel(pageId, descr, label, defValue, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG));
}
void Wiz::AddPage(const wxString& panelName)
{
WizPage* page = new WizPage(panelName, m_pWizard, m_Wizards[m_LaunchIndex].wizardPNG);
m_Pages.Add(page);
}
void Wiz::Finalize()
{
// chain pages
for (size_t i = 1; i < m_Pages.GetCount(); ++i)
wxWizardPageSimple::Chain(m_Pages[i - 1], m_Pages[i]);
// allow the wizard to size itself around the pages
for (size_t i = 1; i < m_Pages.GetCount(); ++i)
m_pWizard->GetPageAreaSizer()->Add(m_Pages[i]);
m_pWizard->Fit();
}
void Wiz::AddWizard(TemplateOutputType otype,
const wxString& title,
const wxString& cat,
const wxString& script,
const wxString& templatePNG,
const wxString& wizardPNG,
const wxString& xrc)
{
WizardInfo info;
info.output_type = otype;
info.title = title;
info.cat = cat;
info.script = script;
info.templatePNG.LoadFile(m_TemplatePath + templatePNG, wxBITMAP_TYPE_PNG);
info.wizardPNG.LoadFile(m_TemplatePath + wizardPNG, wxBITMAP_TYPE_PNG);
info.xrc = xrc;
m_Wizards.Add(info);
wxString typS;
switch (otype)
{
case totProject: typS = _T("Project"); break;
case totTarget: typS = _T("Build-target"); break;
case totFiles: typS = _T("File(s)"); break;
case totCustom: typS = _T("Custom"); break;
default: break;
}
Manager::Get()->GetMessageManager()->DebugLog(typS + _T(" wizard added for '%s'"), title.c_str());
}
wxString Wiz::GetProjectPath()
{
if (m_pWizProjectPathPanel)
return m_pWizProjectPathPanel->GetPath();
return wxEmptyString;
}
wxString Wiz::GetProjectName()
{
if (m_pWizProjectPathPanel)
return m_pWizProjectPathPanel->GetName();
return wxEmptyString;
}
wxString Wiz::GetProjectFullFilename()
{
if (m_pWizProjectPathPanel)
return m_pWizProjectPathPanel->GetFullFileName();
return wxEmptyString;
}
wxString Wiz::GetProjectTitle()
{
if (m_pWizProjectPathPanel)
return m_pWizProjectPathPanel->GetTitle();
return wxEmptyString;
}
wxString Wiz::GetCompilerID()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetCompilerID();
return wxEmptyString;
}
bool Wiz::GetWantDebug()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetWantDebug();
return false;
}
wxString Wiz::GetDebugName()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetDebugName();
return wxEmptyString;
}
wxString Wiz::GetDebugOutputDir()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetDebugOutputDir();
return wxEmptyString;
}
wxString Wiz::GetDebugObjectOutputDir()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetDebugObjectOutputDir();
return wxEmptyString;
}
bool Wiz::GetWantRelease()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetWantRelease();
return false;
}
wxString Wiz::GetReleaseName()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetReleaseName();
return wxEmptyString;
}
wxString Wiz::GetReleaseOutputDir()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetReleaseOutputDir();
return wxEmptyString;
}
wxString Wiz::GetReleaseObjectOutputDir()
{
if (m_pWizCompilerPanel)
return m_pWizCompilerPanel->GetReleaseObjectOutputDir();
return wxEmptyString;
}
wxString Wiz::GetTargetCompilerID()
{
if (m_pWizBuildTargetPanel)
return m_pWizBuildTargetPanel->GetCompilerID();
return wxEmptyString;
}
bool Wiz::GetTargetEnableDebug()
{
if (m_pWizBuildTargetPanel)
return m_pWizBuildTargetPanel->GetEnableDebug();
return false;
}
wxString Wiz::GetTargetName()
{
if (m_pWizBuildTargetPanel)
return m_pWizBuildTargetPanel->GetTargetName();
return wxEmptyString;
}
wxString Wiz::GetTargetOutputDir()
{
if (m_pWizBuildTargetPanel)
return m_pWizBuildTargetPanel->GetTargetOutputDir();
return wxEmptyString;
}
wxString Wiz::GetTargetObjectOutputDir()
{
if (m_pWizBuildTargetPanel)
return m_pWizBuildTargetPanel->GetTargetObjectOutputDir();
return wxEmptyString;
}
wxString Wiz::GetFileName()
{
if (m_pWizFilePathPanel)
return m_pWizFilePathPanel->GetFilename();
return wxEmptyString;
}
wxString Wiz::GetFileHeaderGuard()
{
if (m_pWizFilePathPanel)
return m_pWizFilePathPanel->GetHeaderGuard();
return wxEmptyString;
}
bool Wiz::GetFileAddToProject()
{
if (m_pWizFilePathPanel)
return m_pWizFilePathPanel->GetAddToProject();
return false;
}
int Wiz::GetFileTargetIndex()
{
if (m_pWizFilePathPanel)
return m_pWizFilePathPanel->GetTargetIndex();
return -1;
}
void Wiz::SetFilePathSelectionFilter(const wxString& filter)
{
if (m_pWizFilePathPanel)
m_pWizFilePathPanel->SetFilePathSelectionFilter(filter);
}
void Wiz::RegisterWizard()
{
SqPlus::SQClassDef<Wiz>("Wiz").
// register new wizards
func(&Wiz::AddWizard, "AddWizard").
// add wizard pages
func(&Wiz::AddIntroPage, "AddIntroPage").
func(&Wiz::AddProjectPathPage, "AddProjectPathPage").
func(&Wiz::AddFilePathPage, "AddFilePathPage").
func(&Wiz::AddCompilerPage, "AddCompilerPage").
func(&Wiz::AddBuildTargetPage, "AddBuildTargetPage").
func(&Wiz::AddGenericSingleChoiceListPage, "AddGenericSingleChoiceListPage").
func(&Wiz::AddGenericSelectPathPage, "AddGenericSelectPathPage").
func(&Wiz::AddPage, "AddPage").
// GUI controls
func(&Wiz::EnableWindow, "EnableWindow").
func(&Wiz::SetTextControlValue, "SetTextControlValue").
func(&Wiz::GetTextControlValue, "GetTextControlValue").
func(&Wiz::CheckCheckbox, "CheckCheckbox").
func(&Wiz::IsCheckboxChecked, "IsCheckboxChecked").
func(&Wiz::FillComboboxWithCompilers, "FillComboboxWithCompilers").
func(&Wiz::GetCompilerFromCombobox, "GetCompilerFromCombobox").
func(&Wiz::GetComboboxStringSelection, "GetComboboxStringSelection").
func(&Wiz::GetComboboxSelection, "GetComboboxSelection").
func(&Wiz::SetComboboxSelection, "SetComboboxSelection").
func(&Wiz::GetRadioboxSelection, "GetRadioboxSelection").
func(&Wiz::SetRadioboxSelection, "SetRadioboxSelection").
func(&Wiz::GetListboxSelection, "GetListboxSelection").
func(&Wiz::SetListboxSelection, "SetListboxSelection").
// get various common info
func(&Wiz::GetWizardType, "GetWizardType").
func(&Wiz::GetTemplatePath, "GetTemplatePath").
// project path page
func(&Wiz::GetProjectPath, "GetProjectPath").
func(&Wiz::GetProjectName, "GetProjectName").
func(&Wiz::GetProjectFullFilename, "GetProjectFullFilename").
func(&Wiz::GetProjectTitle, "GetProjectTitle").
// compiler page
func(&Wiz::GetCompilerID, "GetCompilerID").
// + debug target
func(&Wiz::GetWantDebug, "GetWantDebug").
func(&Wiz::GetDebugName, "GetDebugName").
func(&Wiz::GetDebugOutputDir, "GetDebugOutputDir").
func(&Wiz::GetDebugObjectOutputDir, "GetDebugObjectOutputDir").
// + release target
func(&Wiz::GetWantRelease, "GetWantRelease").
func(&Wiz::GetReleaseName, "GetReleaseName").
func(&Wiz::GetReleaseOutputDir, "GetReleaseOutputDir").
func(&Wiz::GetReleaseObjectOutputDir, "GetReleaseObjectOutputDir").
// build target page
func(&Wiz::GetTargetCompilerID, "GetTargetCompilerID").
func(&Wiz::GetTargetEnableDebug, "GetTargetEnableDebug").
func(&Wiz::GetTargetName, "GetTargetName").
func(&Wiz::GetTargetOutputDir, "GetTargetOutputDir").
func(&Wiz::GetTargetObjectOutputDir, "GetTargetObjectOutputDir").
// file path page
func(&Wiz::GetFileName, "GetFileName").
func(&Wiz::GetFileHeaderGuard, "GetFileHeaderGuard").
func(&Wiz::GetFileAddToProject, "GetFileAddToProject").
func(&Wiz::GetFileTargetIndex, "GetFileTargetIndex").
func(&Wiz::SetFilePathSelectionFilter, "SetFilePathSelectionFilter");
SqPlus::BindVariable(this, "Wizard", SqPlus::VAR_ACCESS_READ_ONLY);
}
////////////////////////
// Scripting - END
////////////////////////