OpenASIP  2.0
ProDe.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProDe.cc
26  *
27  * Implementation of the ProDe class.
28  *
29  * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <iostream>
34 #include <string>
35 #include <wx/docmdi.h>
36 #include <wx/mdi.h>
37 #include <wx/docview.h>
38 #include <wx/cmdline.h>
39 #include <wx/imagpng.h>
40 #include <wx/splash.h>
41 #include <boost/format.hpp>
42 
43 #include "ProDe.hh"
44 #include "KeyboardShortcut.hh"
45 #include "ToolbarButton.hh"
46 #include "ProDeOptions.hh"
47 #include "MDFView.hh"
48 #include "MDFDocument.hh"
49 #include "MainFrame.hh"
50 #include "CommandRegistry.hh"
51 #include "ProDeConstants.hh"
52 #include "Conversion.hh"
54 #include "WxConversion.hh"
55 #include "ErrorDialog.hh"
56 #include "FileSystem.hh"
57 #include "Environment.hh"
58 #include "ProDeClipboard.hh"
59 #include "ProDeTextGenerator.hh"
60 #include "UserManualCmd.hh"
61 #include "ObjectState.hh"
62 
63 using std::string;
64 using std::cerr;
65 using std::endl;
66 
67 IMPLEMENT_APP(ProDe)
68 
69 /**
70  * The constructor.
71  */
72 ProDe::ProDe(): docManager_((wxDocManager*)NULL), options_(NULL) {
73 }
74 
75 
76 
77 /**
78  * Parses the command line and initializes the editor accordingly.
79  *
80  * Called only once by wxWindows when the program is executed. Command
81  * line is parsed using wxCmdLineParser class. If no script is provided
82  * with a "-s" option, main frame is created and set as the main
83  * window. An instance of the wxDocManager will be created to handle
84  * document templates, if the editor is ran in interactive mode.
85  *
86  * @return true if application was succesfully initialized, false otherwise.
87  */
88 bool
90 
92 
93  // parse command line
94  static const wxCmdLineEntryDesc cmdLineDesc[] = {
95 
96 #if wxCHECK_VERSION(3, 0, 0)
97  { wxCMD_LINE_PARAM, "", "", "file", wxCMD_LINE_VAL_STRING,
98  wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
99 
100  { wxCMD_LINE_NONE, "", "", "", wxCMD_LINE_VAL_STRING, 0}
101 #else
102  { wxCMD_LINE_PARAM, _T(""), _T(""), _T("file"), wxCMD_LINE_VAL_STRING,
103  wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
104 
105  { wxCMD_LINE_NONE, _T(""), _T(""), _T(""), wxCMD_LINE_VAL_STRING, 0}
106 #endif
107 
108  };
109 
110  wxCmdLineParser parser(cmdLineDesc, argc, argv);
111  int parserStatus = parser.Parse();
112  if (parserStatus != 0) {
113  return false;
114  }
115 
116  // create a document template for ADF files.
117  docManager_ = new wxDocManager;
118  (void) new wxDocTemplate((wxDocManager*) docManager_,
119  _T("Architecture Definition"),
120  _T("*.adf;*.xml;*.cfg"), _T(""),
121  _T("adf"), _T("Architecture Definition File"),
122  _T("ADF View"), CLASSINFO(MDFDocument),
123  CLASSINFO(MDFView));
124 
125  // Dummy doc template for cfgs.
126  // Older versions of wxWidgets don't handle the *.adf;*.cfg file filter
127  // correctly and .cfgs don't get associated to the correct document class
128  // without this invisble template.
129  (void) new wxDocTemplate((wxDocManager*) docManager_,
130  _T("Processor Configuration"),
131  _T("*.cfg"), _T(""),
132  _T("cfg"), _T("Architecture Definition File"),
133  _T("ADF View"), CLASSINFO(MDFDocument),
134  CLASSINFO(MDFView), wxTEMPLATE_INVISIBLE);
135 
136  // Dummy doc template for xmls.
137  (void) new wxDocTemplate((wxDocManager*) docManager_,
138  _T("Architecture Definition File"),
139  _T("*.xml"), _T(""),
140  _T("xml"), _T("Architecture Definition File"),
141  _T("ADF View"), CLASSINFO(MDFDocument),
142  CLASSINFO(MDFView), wxTEMPLATE_INVISIBLE);
143 
144  // create a registry of commands
146 
147  // load image handler for pngs
148  wxImage::AddHandler(new wxPNGHandler);
149 
150  // set configurations
151  string configFile = Environment::confPath("ProDe.conf");
152 
153  // Name of the XML Schema for options file.
154  string schemaFile =
155  Environment::dataDirPath("ProDe")+
156  FileSystem::DIRECTORY_SEPARATOR + "confschema.xsd";
157 
158  ProDeOptionsSerializer reader;
159  reader.setSourceFile(configFile);
160  reader.setSchemaFile(schemaFile);
161  reader.setUseSchema(true);
162  bool erroneousOptions = false;
163  try {
164  ObjectState* optionsState = reader.readState();
165  options_ = new ProDeOptions(optionsState);
166  delete optionsState;
167  options_->validate();
168  options_->setFileName(configFile);
169  } catch (Exception& e) {
170  cerr << e.errorMessage() << endl
171  << "Default options will be used." << endl;
172  erroneousOptions = true;
173  }
174 
175  if (erroneousOptions) {
177  } else {
179  }
180 
181  // create the main frame window
182  mainFrame_ =
183  new MainFrame((wxDocManager*) docManager_, (wxFrame*) NULL,
185  wxPoint(options_->xPosition(), options_->yPosition()),
186  wxSize(options_->windowWidth(),
188  wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE);
189 
190  mainFrame_->Show(TRUE);
191  SetTopWindow(mainFrame_);
192 
193  // open documents given in command line
194  for (unsigned int i = 0; i < parser.GetParamCount(); i++) {
195  wxString file = parser.GetParam(i);
196  wxDocument* doc = docManager_->CreateDocument(file, wxDOC_SILENT);
197  if (doc == NULL) {
198  string fileName = WxConversion::toString(file);
199  if (FileSystem::fileExists(fileName)) {
200  std::cerr << "Improper file '"
201  << fileName
202  << "' couldn't be opened."
203  << std::endl;
204  } else {
205  std::cerr << "Cannot open file '"
206  << fileName
207  << "'."
208  << std::endl;
209  }
210  }
211  }
212 
213  //mainFrame_->updateMenubar();
214  //mainFrame_->updateToolbar();
215 
216  return true;
217 }
218 
219 
220 /**
221  * Deletes the application level dynamic objects not handled by wxWindows.
222  *
223  * Called when the editor is about to exit, and all application
224  * windows are already destroyed.
225  */
226 int
230  delete docManager_;
231  delete options_;
232  return 0;
233 }
234 
235 
236 /**
237  * Returns pointer to the main frame of the editor.
238  *
239  * @return Pointer to the main frame of the editor.
240  */
241 MainFrame*
243  return mainFrame_;
244 }
245 
246 
247 /**
248  * Returns editor options.
249  *
250  * @return Current editor options.
251  */
253 ProDe::options() const {
254  return options_;
255 }
256 
257 
258 /**
259  * Sets the editor options, and deletes old options.
260  *
261  * @param options ProDeOptions to set as new options.
262  */
263 void
265  if (options_ != NULL) {
266  delete options_;
267  }
268  options_ = options;
270 }
271 
272 
273 /**
274  * Creates default options.
275  */
276 void
278 
279  // kb shortcut: ctrl-N = New Document
280  KeyboardShortcut* scNew = new KeyboardShortcut(
281  ProDeConstants::CMD_NAME_NEW_DOC, 0, true, false, int('N'));
282 
283  // kb shortcut: ctrl-O = Open Document
284  KeyboardShortcut* scOpen = new KeyboardShortcut(
285  ProDeConstants::CMD_NAME_OPEN_DOC, 0, true, false, int('O'));
286 
287  // kb shortcut: ctrl-S = Save Document
288  KeyboardShortcut* scSave = new KeyboardShortcut(
289  ProDeConstants::CMD_NAME_SAVE_DOC, 0, true, false, int('S'));
290 
291  // kb shortcut: ctrl-W = close document
292  KeyboardShortcut* scClose = new KeyboardShortcut(
293  ProDeConstants::CMD_NAME_CLOSE_DOC, 0, true, false, int('W'));
294 
295  // kb shortcut: ctrl-P = print document
296  KeyboardShortcut* scPrint = new KeyboardShortcut(
297  ProDeConstants::CMD_NAME_PRINT, 0, true, false, int('P'));
298 
299  // kb shortcut: ctrl-Q = quit
300  KeyboardShortcut* scQuit = new KeyboardShortcut(
301  ProDeConstants::CMD_NAME_QUIT, 0, true, false, int('Q'));
302 
303 
304  // kb shortcut: ctrl-C = copy
305  KeyboardShortcut* scCopy = new KeyboardShortcut(
306  ProDeConstants::CMD_NAME_COPY, 0, true, false, int('C'));
307 
308  // kb shortcut: ctrl-X = cut
309  KeyboardShortcut* scCut = new KeyboardShortcut(
310  ProDeConstants::CMD_NAME_CUT, 0, true, false, int('X'));
311 
312  // kb shortcut: ctrl-V = paste
313  KeyboardShortcut* scPaste = new KeyboardShortcut(
314  ProDeConstants::CMD_NAME_PASTE, 0, true, false, int('V'));
315 
316  // kb shortcut: ctrl-Z = undo
317  KeyboardShortcut* scUndo = new KeyboardShortcut(
318  ProDeConstants::CMD_NAME_UNDO, 0, true, false, int('Z'));
319 
320  // kb shortcut: ctrl-Y = redo
321  KeyboardShortcut* scRedo = new KeyboardShortcut(
322  ProDeConstants::CMD_NAME_REDO, 0, true, false, int('Y'));
323 
324  // kb shortcut: F1 = Help
325  KeyboardShortcut* scUserManual = new KeyboardShortcut(
326  UserManualCmd::COMMAND_NAME, 1, false, false, 0);
327 
328  // toolbar buttons
329  ToolbarButton* buttonNew = new ToolbarButton(
331 
332  ToolbarButton* buttonOpen = new ToolbarButton(
334 
335  ToolbarButton* buttonSave = new ToolbarButton(
337 
338  ToolbarButton* buttonDelete = new ToolbarButton(
340 
341  ToolbarButton* buttonModify = new ToolbarButton(
343 
344  ToolbarButton* buttonZoomIn = new ToolbarButton(
346 
347  ToolbarButton* buttonZoomOut = new ToolbarButton(
349 
350  ToolbarButton* buttonZoomFit = new ToolbarButton(
352 
353  ToolbarButton* buttonOptions = new ToolbarButton(
355 
356  ToolbarButton* buttonHelp = new ToolbarButton(
358 
359  options_ = new ProDeOptions();
360 
362  options_->addKeyboardShortcut(scOpen);
363  options_->addKeyboardShortcut(scSave);
364  options_->addKeyboardShortcut(scClose);
365  options_->addKeyboardShortcut(scQuit);
366  options_->addKeyboardShortcut(scPrint);
367  options_->addKeyboardShortcut(scCopy);
369  options_->addKeyboardShortcut(scPaste);
370  options_->addKeyboardShortcut(scUndo);
371  options_->addKeyboardShortcut(scRedo);
372  options_->addKeyboardShortcut(scUserManual);
373 
374  options_->addToolbarButton(buttonNew);
375  options_->addToolbarButton(buttonOpen);
376  options_->addToolbarButton(buttonSave);
377  options_->addToolbarButton(buttonDelete);
378  options_->addToolbarButton(buttonModify);
379  options_->addToolbarButton(buttonZoomIn);
380  options_->addToolbarButton(buttonZoomOut);
381  options_->addToolbarButton(buttonZoomFit);
382  options_->addToolbarButton(buttonOptions);
383  options_->addToolbarButton(buttonHelp);
386  options_->addSeparator(10);
387 
388  // default toolbar layout and visibility
391 
392  // default undo levels
394 
395  // set MainFrame size and position
396  options_->setFullScreen(false);
397  options_->setWindowSize(600, 500);
398  options_->setWindowPosition(100, 50);
399 }
400 
401 
402 /**
403  * Returns command registry of the editor.
404  */
407  return commandRegistry_;
408 }
409 
410 
411 
412 /**
413  * Returns document manager of the editor.
414  */
415 wxDocManager*
417  return docManager_;
418 }
419 
420 
421 /**
422  * Returns path of toolbar icon files.
423  *
424  * @return ProDe icon files path.
425  */
426 string
428  string path = Environment::bitmapsDirPath("ProDe") +
430  return path;
431 }
ProDeConstants::CMD_NAME_ZOOM_IN
static const std::string CMD_NAME_ZOOM_IN
Command name for the "Zoom In" command.
Definition: ProDeConstants.hh:156
ToolbarButton.hh
ProDe.hh
ProDeOptions
Definition: ProDeOptions.hh:44
ProDeConstants::CMD_NAME_EDIT_OPTIONS
static const std::string CMD_NAME_EDIT_OPTIONS
Command name for the "Edit Options" command.
Definition: ProDeConstants.hh:177
FileSystem.hh
ProDeTextGenerator::destroy
static void destroy()
Definition: ProDeTextGenerator.cc:394
ProDeConstants::CMD_NAME_SAVE_DOC
static const std::string CMD_NAME_SAVE_DOC
Command name for the "Save Document" command.
Definition: ProDeConstants.hh:80
ProDe::options
ProDeOptions * options() const
Definition: ProDe.cc:253
CommandRegistry.hh
XMLSerializer::setSourceFile
void setSourceFile(const std::string &fileName)
Definition: XMLSerializer.cc:115
MDFDocument.hh
GUIOptions::clearModified
void clearModified()
Definition: GUIOptions.cc:502
GUIOptions::setWindowSize
void setWindowSize(int width, int height)
Definition: GUIOptions.cc:229
GUIOptions::addToolbarButton
void addToolbarButton(ToolbarButton *button)
Definition: GUIOptions.cc:292
MainFrame
Definition: MainFrame.hh:50
ProDe::mainFrame_
MainFrame * mainFrame_
Main frame of the application.
Definition: ProDe.hh:72
GUIOptions::yPosition
int yPosition() const
Definition: GUIOptions.cc:183
ProDe::createDefaultOptions
void createDefaultOptions()
Definition: ProDe.cc:277
ProDe::mainFrame
MainFrame * mainFrame() const
Definition: ProDe.cc:242
ObjectState
Definition: ObjectState.hh:59
ToolbarButton
Definition: ToolbarButton.hh:48
ProDe::commandRegistry
CommandRegistry * commandRegistry() const
Definition: ProDe.cc:406
ProDeConstants::CMD_NAME_MODIFY_COMP
static const std::string CMD_NAME_MODIFY_COMP
Command name for the "Modify Component" command.
Definition: ProDeConstants.hh:119
ProDeConstants::CMD_NAME_DELETE_COMP
static const std::string CMD_NAME_DELETE_COMP
Command name for the "Delete Component" command.
Definition: ProDeConstants.hh:117
GUIOptions::setToolbarVisibility
void setToolbarVisibility(bool visible)
Definition: GUIOptions.cc:256
ProDe::bitmapsDirPath
static std::string bitmapsDirPath()
Definition: ProDe.cc:427
ProDeTextGenerator.hh
ProDeConstants::CMD_NAME_PRINT
static const std::string CMD_NAME_PRINT
Command name for the "Print" command.
Definition: ProDeConstants.hh:84
ProDeConstants::CMD_NAME_PASTE
static const std::string CMD_NAME_PASTE
Command name for the "Paste" command.
Definition: ProDeConstants.hh:130
ProDeOptionsSerializer.hh
ProDe
Definition: ProDe.hh:54
Environment::bitmapsDirPath
static std::string bitmapsDirPath(const std::string &prog)
Definition: Environment.cc:201
GUIOptionsSerializer::readState
ObjectState * readState()
Definition: GUIOptionsSerializer.cc:149
GUIOptions::BOTH
@ BOTH
Buttons contains text and icon.
Definition: GUIOptions.hh:67
ProDeOptions::setUndoStackSize
void setUndoStackSize(int size)
Definition: ProDeOptions.cc:106
ProDeClipboard::destroy
static void destroy()
Definition: ProDeClipboard.cc:74
KeyboardShortcut.hh
XMLSerializer::setSchemaFile
void setSchemaFile(const std::string &fileName)
Definition: XMLSerializer.cc:168
ErrorDialog.hh
Conversion.hh
GUIOptions::setWindowPosition
void setWindowPosition(int x, int y)
Definition: GUIOptions.cc:243
ProDe::OnInit
bool OnInit()
Definition: ProDe.cc:89
ObjectState.hh
CommandRegistry
Definition: CommandRegistry.hh:47
ProDe::setOptions
void setOptions(ProDeOptions *options)
Definition: ProDe.cc:264
GUIOptions::addSeparator
void addSeparator(int position)
Definition: GUIOptions.cc:305
ProDe::docManager_
wxDocManager * docManager_
Manages multiple documents.
Definition: ProDe.hh:70
ProDeConstants::CMD_NAME_QUIT
static const std::string CMD_NAME_QUIT
Command name for the "Quit" command.
Definition: ProDeConstants.hh:90
Environment.hh
ProDeConstants::CMD_NAME_OPEN_DOC
static const std::string CMD_NAME_OPEN_DOC
Command name for the "Open Document" command.
Definition: ProDeConstants.hh:76
GUIOptions::windowWidth
int windowWidth() const
Definition: GUIOptions.cc:150
GUIOptions::setFileName
void setFileName(const std::string &fileName)
Definition: GUIOptions.cc:632
GUIOptions::addKeyboardShortcut
void addKeyboardShortcut(KeyboardShortcut *shortcut)
Definition: GUIOptions.cc:280
Exception
Definition: Exception.hh:54
ProDe::commandRegistry_
CommandRegistry * commandRegistry_
editor command registry
Definition: ProDe.hh:76
GUIOptions::xPosition
int xPosition() const
Definition: GUIOptions.cc:172
TRUE
const string TRUE
Value used for true in attribute and element values.
Definition: GUIOptionsSerializer.cc:65
GUIOptions::setToolbarLayout
void setToolbarLayout(ToolbarLayout layout)
Definition: GUIOptions.cc:268
XMLSerializer::setUseSchema
void setUseSchema(bool useSchema)
Definition: XMLSerializer.cc:179
ProDe::options_
ProDeOptions * options_
editor options
Definition: ProDe.hh:74
ProDe::docManager
wxDocManager * docManager() const
Definition: ProDe.cc:416
GUIOptions::validate
virtual void validate() const
Definition: GUIOptions.cc:529
ProDeConstants::CMD_NAME_COPY
static const std::string CMD_NAME_COPY
Command name for the "Copy" command.
Definition: ProDeConstants.hh:128
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
ProDeConstants::EDITOR_NAME
static const wxString EDITOR_NAME
Full name of the Editor.
Definition: ProDeConstants.hh:386
MDFDocument
Definition: MDFDocument.hh:51
ProDeConstants.hh
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
ProDeConstants::CMD_NAME_UNDO
static const std::string CMD_NAME_UNDO
Command name for the "Undo" command.
Definition: ProDeConstants.hh:122
KeyboardShortcut
Definition: KeyboardShortcut.hh:50
MainFrame.hh
Environment::confPath
static TCEString confPath(const std::string &fileName)
Definition: Environment.cc:277
MDFView.hh
ProDeConstants::CMD_NAME_ZOOM_OUT
static const std::string CMD_NAME_ZOOM_OUT
Command name for the "Zoom Out" command.
Definition: ProDeConstants.hh:158
ProDeOptions.hh
ProDeConstants::CMD_NAME_ZOOM_FIT_WIN
static const std::string CMD_NAME_ZOOM_FIT_WIN
Command name for the "Fit Window" command.
Definition: ProDeConstants.hh:160
FileSystem::fileExists
static bool fileExists(const std::string fileName)
Application::initialize
static void initialize()
Definition: Application.cc:99
ProDeConstants::CMD_NAME_CUT
static const std::string CMD_NAME_CUT
Command name for the "Cut" command.
Definition: ProDeConstants.hh:126
GUIOptions::windowHeight
int windowHeight() const
Definition: GUIOptions.cc:161
GUIOptions::setFullScreen
void setFullScreen(bool fullScreen)
Definition: GUIOptions.cc:216
UserManualCmd::COMMAND_NAME
static const std::string COMMAND_NAME
Command name string.
Definition: UserManualCmd.hh:52
WxConversion.hh
ProDeClipboard.hh
WxConversion::toString
static std::string toString(const wxString &source)
ProDeConstants::CMD_NAME_REDO
static const std::string CMD_NAME_REDO
Command name for the "Redo" command.
Definition: ProDeConstants.hh:124
UserManualCmd.hh
MainFrame::createToolbar
void createToolbar()
Definition: MainFrame.cc:746
ProDe::OnExit
int OnExit()
Definition: ProDe.cc:227
Environment::dataDirPath
static std::string dataDirPath(const std::string &prog)
Definition: Environment.cc:176
ProDeConstants::CMD_NAME_NEW_DOC
static const std::string CMD_NAME_NEW_DOC
Command name for the "New Document" command.
Definition: ProDeConstants.hh:74
ProDeConstants::CMD_NAME_CLOSE_DOC
static const std::string CMD_NAME_CLOSE_DOC
Command name for the "Close Document" command.
Definition: ProDeConstants.hh:78
ProDeOptionsSerializer
Definition: ProDeOptionsSerializer.hh:43
WxConversion::toCStringArray
static char ** toCStringArray(size_t elements, wxChar **source)
Definition: WxConversion.cc:123
MDFView
Definition: MDFView.hh:59