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
|
/////////////////////////////////////////////////////////////////////////////
// Name: xti.cpp
// Purpose: eXtended RTTI support sample
// Author: Stefan Csomor, Francesco Montorsi
// Modified by:
// Created: 13/5/2007
// Copyright: (c) Stefan Csomor, Francesco Montorsi
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/wx.h"
#include "wx/variant.h"
#include "wx/xml/xml.h"
#include "wx/frame.h"
#include "wx/notebook.h"
#include "wx/event.h"
#include "wx/spinbutt.h"
#include "wx/spinctrl.h"
#include "wx/xtistrm.h"
#include "wx/xtixml.h"
#include "wx/txtstrm.h"
#include "wx/wfstream.h"
#include "wx/sstream.h"
#include "wx/spinctrl.h"
#include "classlist.h"
#include "codereadercallback.h"
#if !wxUSE_EXTENDED_RTTI
#error This sample requires XTI (eXtended RTTI) enabled
#endif
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title);
void OnPersist(wxCommandEvent& event);
void OnDepersist(wxCommandEvent& event);
void OnGenerateCode(wxCommandEvent& event);
void OnDumpClasses(wxCommandEvent& event);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// any class wishing to process wxWidgets events must use this macro
wxDECLARE_EVENT_TABLE();
};
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
// menu items
Minimal_Persist = wxID_HIGHEST,
Minimal_Depersist,
Minimal_GenerateCode,
Minimal_DumpClasses,
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};
// ----------------------------------------------------------------------------
// event tables and other macros for wxWidgets
// ----------------------------------------------------------------------------
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Persist, MyFrame::OnPersist)
EVT_MENU(Minimal_Depersist, MyFrame::OnDepersist)
EVT_MENU(Minimal_GenerateCode, MyFrame::OnGenerateCode)
EVT_MENU(Minimal_DumpClasses, MyFrame::OnDumpClasses)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
// ============================================================================
// implementation
// ============================================================================
void RegisterFrameRTTI();
// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
RegisterFrameRTTI();
// create the main application window
MyFrame *frame = new MyFrame(wxT("Extended RTTI sample"));
// and show it (the frames, unlike simple controls, are not shown when
// created initially)
frame->Show(true);
// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned false here, the
// application would exit immediately.
return true;
}
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
{
// set the frame icon
SetIcon(wxICON(sample));
#if wxUSE_MENUS
// create a menu bar
wxMenu *fileMenu = new wxMenu;
// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, wxT("&About\tF1"), wxT("Show about dialog"));
fileMenu->Append(Minimal_Persist, wxT("Persist a wxFrame to XML..."),
wxT("Creates a wxFrame using wxXTI and saves its description as XML"));
fileMenu->Append(Minimal_Depersist, wxT("Depersist XML file..."),
wxT("Loads the description of wxFrame from XML"));
fileMenu->Append(Minimal_GenerateCode, wxT("Generate code for a wxFrame saved to XML..."),
wxT("Generates the C++ code which belong to a persisted wxFrame"));
fileMenu->AppendSeparator();
fileMenu->Append(Minimal_DumpClasses, wxT("Dump registered classes..."),
wxT("Dumps the description of all wxWidgets classes registered in XTI"));
fileMenu->AppendSeparator();
fileMenu->Append(Minimal_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS
#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText(wxT("Welcome to wxWidgets!"));
#endif // wxUSE_STATUSBAR
}
// ----------------------------------------------------------------------------
// XTI sample code
// ----------------------------------------------------------------------------
// this is the kind of source code that would end up being generated by a
// designer corresponding to the information we are setting up via RTTI
// in the CreateFrameRTTI function:
//
// class MyXTIFrame : public wxFrame
// {
// public:
// // construction
// MyXTIFrame()
// {
// Init();
// m_button = NULL;
// }
//
// bool Create(wxWindow *parent,
// wxWindowID id,
// const wxString& title,
// const wxPoint& pos = wxDefaultPosition,
// const wxSize& size = wxDefaultSize,
// long style = wxDEFAULT_FRAME_STYLE,
// const wxString& name = wxFrameNameStr)
// {
// return wxFrame::Create( parent, id, title, pos, size, style, name );
// }
//
// void SetButton( wxButton * button ) { m_button = button; }
// wxButton* GetButton() const { return m_button; }
//
// void ButtonClickHandler( wxEvent & WXUNUSED(event) )
// {
// wxMessageBox( "Button Clicked ", "Hi!", wxOK );
// }
//
// protected:
// wxButton* m_button;
//
// wxDECLARE_EVENT_TABLE();
// wxDECLARE_DYNAMIC_CLASS_NO_COPY(MyXTIFrame);
// };
//
// IMPLEMENT_DYNAMIC_CLASS_XTI(MyXTIFrame, MyXTIFrame, "x.h")
//
// WX_BEGIN_PROPERTIES_TABLE(MyXTIFrame)
// WX_PROPERTY( Button, wxButton*, SetButton, GetButton, )
// WX_END_PROPERTIES_TABLE()
//
// WX_BEGIN_HANDLERS_TABLE(MyXTIFrame)
// WX_HANDLER( ButtonClickHandler, wxCommandEvent )
// WX_END_HANDLERS_TABLE()
//
// WX_CONSTRUCTOR_5( MyXTIFrame, wxWindow*, Parent, wxWindowID, Id,
// wxString, Title, wxPoint, Position, wxSize, Size )
//
// wxBEGIN_EVENT_TABLE(MyXTIFrame, wxFrame)
// wxEND_EVENT_TABLE()
// the following class "persists" (i.e. saves) a wxFrame into a wxObjectWriter
class MyDesignerPersister : public wxObjectWriterCallback
{
public:
MyDesignerPersister( wxDynamicObject * frame)
{
m_frame = frame;
}
virtual bool BeforeWriteDelegate( wxObjectWriter *WXUNUSED(writer),
const wxObject *object,
const wxClassInfo* WXUNUSED(classInfo),
const wxPropertyInfo *propInfo,
const wxObject *&eventSink,
const wxHandlerInfo* &handlerInfo )
{
// this approach would be used if the handler would not
// be connected really in the designer, so we have to supply
// the information
const wxObject* but = wxAnyGetAsObjectPtr( m_frame->GetProperty(wxT("Button")) );
if ( object == but &&
propInfo == wxCLASSINFO( wxButton )->FindPropertyInfo(wxT("OnClick")) )
{
eventSink = m_frame;
handlerInfo = m_frame->GetClassInfo()->
FindHandlerInfo(wxT("ButtonClickHandler"));
return true;
}
return false;
}
private:
wxDynamicObject *m_frame;
};
// sometimes linkers (at least MSVC and GCC ones) optimize the final EXE
// even in debug mode pruning the object files which he "thinks" are useless;
// thus the classes defined in those files won't be available in the XTI
// table and the program will fail to allocate them.
// The following macro implements a simple hack to ensure that a given
// class is linked in.
//
// TODO: in wx/link.h there are already similar macros (also more "optimized":
// don't need the creation of fake object) which however require to use
// the wxFORCE_LINK_THIS_MODULE() macro inside the source files corresponding
// to the class being discarded.
//
#define wxENSURE_CLASS_IS_LINKED(x) { x test; }
void RegisterFrameRTTI()
{
// set up the RTTI info for a class (MyXTIFrame) which
// is not defined anywhere in this program
wxDynamicClassInfo *dyninfo =
wx_dynamic_cast( wxDynamicClassInfo *, wxClassInfo::FindClass(wxT("MyXTIFrame")));
if ( dyninfo == NULL )
{
dyninfo = new wxDynamicClassInfo(wxT("myxtiframe.h"),
wxT("MyXTIFrame"),
CLASSINFO(wxFrame) );
// this class has a property named "Button" and the relative handler:
dyninfo->AddProperty(wxT("Button"), wxGetTypeInfo((wxButton**) NULL));
dyninfo->AddHandler(wxT("ButtonClickHandler"),
NULL /* no instance of the handler method */, CLASSINFO( wxEvent ) );
}
}
wxDynamicObject* CreateFrameRTTI()
{
int baseID = 100;
wxAny Params[10];
// the class is now part of XTI internal table so that we can
// get a pointer to it just searching it like any other class:
wxFrame* frame;
wxClassInfo *info = wxClassInfo::FindClass(wxT("MyXTIFrame"));
wxASSERT( info );
wxDynamicObject* frameWrapper =
wx_dynamic_cast(wxDynamicObject*, info->CreateObject() );
Params[0] = wxAny((wxWindow*)(NULL));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("This is a frame created from XTI")));
Params[3] = wxAny(wxPoint(-1,-1));
Params[4] = wxAny(wxSize(400,300));
Params[5] = wxAny((long)wxDEFAULT_FRAME_STYLE);
wxASSERT( info->Create(frameWrapper, 6, Params ));
frame = wx_dynamic_cast(wxFrame*, frameWrapper->GetSuperClassInstance());
#if 1
// now build a notebook inside it:
wxNotebook* notebook;
info = wxClassInfo::FindClass("wxNotebook");
wxASSERT( info );
notebook = wxDynamicCast( info->CreateObject(), wxNotebook );
Params[0] = wxAny((wxWindow*)frame);
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxPoint( 10, 10 ));
Params[3] = wxAny(wxDefaultSize);
Params[4] = wxAny((long)0);
wxASSERT( info->Create(notebook, 5, Params ));
// button page
wxPanel* panel;
info = wxClassInfo::FindClass("wxPanel");
wxASSERT( info );
panel = wxDynamicCast( info->CreateObject(), wxPanel );
Params[0] = wxAny((wxWindow*)(notebook));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxPoint(-1,-1));
Params[3] = wxAny(wxSize(-1,-1));
Params[4] = wxAny((long)0);
Params[5] = wxAny(wxString(wxT("Hello")));
wxASSERT( info->Create(panel, 6, Params ));
notebook->AddPage( panel, "Buttons" );
wxButton* button;
info = wxClassInfo::FindClass("wxButton");
wxASSERT( info );
button = wxDynamicCast( info->CreateObject(), wxButton );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("Click Me!")));
Params[3] = wxAny(wxPoint( 10, 10 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(button, 6, Params ));
frameWrapper->SetProperty( wxT("Button"), wxAny( button ) );
// other controls page
info = wxClassInfo::FindClass("wxPanel");
wxASSERT( info );
panel = wxDynamicCast( info->CreateObject(), wxPanel );
Params[0] = wxAny((wxWindow*)(notebook));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxPoint(-1,-1));
Params[3] = wxAny(wxSize(-1,-1));
Params[4] = wxAny((long)0);
Params[5] = wxAny(wxString(wxT("Hello")));
wxASSERT( info->Create(panel, 6, Params ));
notebook->AddPage( panel, "Other Standard controls" );
wxControl* control;
info = wxClassInfo::FindClass("wxCheckBox");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("A Checkbox")));
Params[3] = wxAny(wxPoint( 10, 10 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(control, 6, Params ));
info = wxClassInfo::FindClass("wxRadioButton");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("A Radiobutton")));
Params[3] = wxAny(wxPoint( 10, 30 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(control, 6, Params ));
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("Another One")));
Params[3] = wxAny(wxPoint( 10, 50 ));
wxASSERT( info->Create(control, 6, Params ));
info = wxClassInfo::FindClass("wxStaticText");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("A Static Text!")));
Params[3] = wxAny(wxPoint( 10, 70 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(control, 6, Params ));
info = wxClassInfo::FindClass("wxStaticBox");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("A Static Box")));
Params[3] = wxAny(wxPoint( 10, 90 ));
Params[4] = wxAny(wxSize(100,80));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(control, 6, Params ));
info = wxClassInfo::FindClass("wxTextCtrl");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString(wxT("A Text Control")));
Params[3] = wxAny(wxPoint( 10, 200 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long)0);
wxASSERT( info->Create(control, 6, Params ));
// spins and gauges page
info = wxClassInfo::FindClass("wxPanel");
wxASSERT( info );
panel = wxDynamicCast( info->CreateObject(), wxPanel );
Params[0] = wxAny((wxWindow*)(notebook));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxPoint(-1,-1));
Params[3] = wxAny(wxSize(-1,-1));
Params[4] = wxAny((long)0);
Params[5] = wxAny(wxString(wxT("Hello")));
wxASSERT( info->Create(panel, 6, Params ));
notebook->AddPage( panel, "Spins and Sliders" );
wxENSURE_CLASS_IS_LINKED(wxSpinButton);
info = wxClassInfo::FindClass("wxSpinButton");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxPoint( 10, 10 ));
Params[3] = wxAny(wxSize(-1,-1));
Params[4] = wxAny((long)wxSP_VERTICAL | wxSP_ARROW_KEYS);
wxASSERT( info->Create(control, 5, Params ));
wxENSURE_CLASS_IS_LINKED(wxSpinCtrl);
info = wxClassInfo::FindClass("wxSpinCtrl");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny(wxString("20"));
Params[3] = wxAny(wxPoint( 40, 10 ));
Params[4] = wxAny(wxSize(40,-1));
Params[5] = wxAny((long) wxSP_ARROW_KEYS);
wxASSERT( info->Create(control, 6, Params ));
// MSVC likes to exclude from link wxGauge...
wxENSURE_CLASS_IS_LINKED(wxGauge)
wxENSURE_CLASS_IS_LINKED(wxCheckBox)
wxENSURE_CLASS_IS_LINKED(wxSpinCtrl)
info = wxClassInfo::FindClass("wxGauge");
wxASSERT( info );
control = wxDynamicCast( info->CreateObject(), wxControl );
Params[0] = wxAny((wxWindow*)(panel));
Params[1] = wxAny(wxWindowID(baseID++));
Params[2] = wxAny((int) 100);
Params[3] = wxAny(wxPoint( 10, 50 ));
Params[4] = wxAny(wxSize(-1,-1));
Params[5] = wxAny((long) wxGA_HORIZONTAL);
wxASSERT( info->Create(control, 6, Params ));
wx_dynamic_cast(wxGauge*, control)->SetValue(20);
#endif
return frameWrapper;
}
bool SaveFrameRTTI(const wxString &testFileName, wxDynamicObject *frame)
{
// setup the XML document
wxXmlDocument xml;
wxXmlNode *root = new wxXmlNode(wxXML_ELEMENT_NODE,
"TestXTI", "This is the content");
xml.SetRoot(root);
// setup the XTI writer and persister
wxObjectXmlWriter writer(root);
MyDesignerPersister persister(frame);
// write the given wxObject into the XML document
wxStringToAnyHashMap empty;
writer.WriteObject( frame, frame->GetClassInfo(), &persister,
wxString("myTestFrame"), empty );
return xml.Save(testFileName);
}
wxDynamicObject* LoadFrameRTTI(const wxString &fileName)
{
// load the XML document
wxXmlDocument xml;
if (!xml.Load(fileName))
return NULL;
wxXmlNode *root = xml.GetRoot();
if (root->GetName() != "TestXTI")
return NULL;
// now depersist the wxFrame we saved into it using wxObjectRuntimeReaderCallback
wxObjectRuntimeReaderCallback Callbacks;
wxObjectXmlReader Reader( root );
int obj = Reader.ReadObject( wxString("myTestFrame"), &Callbacks );
return (wxDynamicObject*)Callbacks.GetObject( obj );
}
bool GenerateFrameRTTICode(const wxString &inFileName, const wxString &outFileName)
{
// is loading the streamed out component from xml and writing code that
// will create the same component
wxFFileOutputStream fos( outFileName );
wxTextOutputStream tos( fos );
if (!fos.IsOk())
return false;
wxXmlDocument xml;
if (!xml.Load(inFileName))
return false;
wxXmlNode *root = xml.GetRoot();
if (root->GetName() != "TestXTI")
return false;
// read the XML file using the wxObjectCodeReaderCallback
wxString headerincludes;
wxString sourcecode;
wxObjectCodeReaderCallback Callbacks(headerincludes,sourcecode);
wxObjectXmlReader Reader(root);
// ReadObject will return the ID of the object read??
Reader.ReadObject( wxString("myTestFrame"), &Callbacks );
// header preamble
tos <<
"#include \"wx/wxprec.h\" \n#ifdef __BORLANDC__\n#pragma hdrstop\n#endif\n#ifndef WX_PRECOMP\n#include \"wx/wx.h\" \n#endif\n\n";
// add object includes
tos.WriteString( headerincludes );
tos << "\n\nvoid test()\n{";
tos.WriteString( sourcecode );
tos << "}";
return true;
}
// ----------------------------------------------------------------------------
// MyFrame event handlers
// ----------------------------------------------------------------------------
void MyFrame::OnPersist(wxCommandEvent& WXUNUSED(event))
{
// first create a frame using XTI calls
wxDynamicObject *frame = CreateFrameRTTI();
if (!frame)
{
wxLogError(wxT("Cannot create the XTI frame!"));
return;
}
// show the frame we're going to save to the user
wxFrame *trueFrame = wx_dynamic_cast(wxFrame *, frame->GetSuperClassInstance() );
trueFrame->Show();
// ask the user where to save it
wxFileDialog dlg(this, wxT("Where should the frame be saved?"),
wxEmptyString, wxT("test.xml"), wxT("XML files (*.xml)|*.xml"),
wxFD_SAVE);
if (dlg.ShowModal() == wxID_CANCEL)
return;
// then save it to a test XML file
if (!SaveFrameRTTI(dlg.GetPath(), frame))
{
wxLogError(wxT("Cannot save the XTI frame into '%s'"), dlg.GetPath());
return;
}
// now simply delete it
delete frame;
}
void MyFrame::OnDepersist(wxCommandEvent& WXUNUSED(event))
{
// ask the user which file to load
wxFileDialog dlg(this, wxT("Which file contains the frame to depersist?"),
wxEmptyString, wxT("test.xml"), wxT("XML files (*.xml)|*.xml"),
wxFD_OPEN);
if (dlg.ShowModal() == wxID_CANCEL)
return;
wxObject *frame = LoadFrameRTTI(dlg.GetPath());
if (!frame)
{
wxLogError(wxT("Could not depersist the wxFrame from '%s'"), dlg.GetPath());
return;
}
wxFrame *trueFrame = wx_dynamic_cast(wxFrame*, frame );
if ( !trueFrame )
{
wxDynamicObject* dyno = wx_dynamic_cast(wxDynamicObject*, frame );
if ( dyno )
trueFrame = wx_dynamic_cast(wxFrame *, dyno->GetSuperClassInstance() );
}
if ( trueFrame )
trueFrame->Show();
else
wxLogError(wxT("Could not show the frame"));
}
void MyFrame::OnGenerateCode(wxCommandEvent& WXUNUSED(event))
{
// ask the user which file to load
wxFileDialog dlg(this, wxT("Which file contains the frame to work on?"),
wxEmptyString, wxT("test.xml"), wxT("XML files (*.xml)|*.xml"),
wxFD_OPEN);
if (dlg.ShowModal() == wxID_CANCEL)
return;
// ask the user which file to load
wxFileDialog dlg2(this, wxT("Where should the C++ code be saved?"),
wxEmptyString, wxT("test.cpp"), wxT("Source files (*.cpp)|*.cpp"),
wxFD_SAVE);
if (dlg2.ShowModal() == wxID_CANCEL)
return;
// do generate code
if (!GenerateFrameRTTICode(dlg.GetPath(), dlg2.GetPath()))
{
wxLogError(wxT("Could not generate the code for the frame!"));
return;
}
// show the generated code
{
wxFileInputStream f(dlg2.GetPath());
wxStringOutputStream str;
f.Read(str);
wxDialog dlg(this, wxID_ANY, wxT("Generated code"),
wxDefaultPosition, wxDefaultSize,
wxRESIZE_BORDER|wxDEFAULT_DIALOG_STYLE);
wxPanel *panel = new wxPanel(&dlg);
wxSizer *sz = new wxBoxSizer(wxVERTICAL);
sz->Add(new wxTextCtrl(panel, wxID_ANY, str.GetString(),
wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE|wxTE_READONLY|wxTE_DONTWRAP),
1, wxGROW|wxALL, 5);
sz->Add(new wxButton(panel, wxID_OK), 0, wxALIGN_RIGHT|wxALL, 5);
panel->SetSizerAndFit(sz);
dlg.ShowModal();
}
}
void MyFrame::OnDumpClasses(wxCommandEvent& WXUNUSED(event))
{
ClassListDialog dlg(this);
dlg.ShowModal();
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// true is to force the frame to close
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxString::Format(
wxT("Welcome to %s!\n")
wxT("\n")
wxT("This sample demonstrates wxWidgets eXtended RTTI (XTI) system."),
wxVERSION_STRING
),
wxT("About wxWidgets XTI sample"),
wxOK | wxICON_INFORMATION,
this);
}
|