OSDN Git Service

The (experimental) command line version does not compile; empty MainView_resizeToFit...
[molby/Molby.git] / wxSources / MyCommand.cpp
1 /*
2  *  MyCommand.cpp
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 08/10/25.
6  *  Copyright 2008 Toshi Nagata. All rights reserved.
7  *
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation version 2 of the License.
11  
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16  */
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #if !wxUSE_DOC_VIEW_ARCHITECTURE
30 #error "You should have DocView architecture enabled in your wxWidgets installation."
31 #endif
32
33 #include "MoleculeView.h"
34 #include "MyCommand.h"
35 #include "MyDocument.h"
36
37 MyCommand::MyCommand(Molecule *aMolecule, const wxString& name):
38         wxCommand(true, name)
39 {
40         mol = MoleculeRetain(aMolecule);
41         undoActions = redoActions = NULL;
42         numUndoActions = numRedoActions = 0;
43 }
44
45 MyCommand::~MyCommand()
46 {
47         MoleculeRelease(mol);
48         SetUndoActions(NULL, 0);
49         SetRedoActions(NULL, 0);
50 }
51
52 void
53 MyCommand::SetUndoActions(MolAction **actions, int count)
54 {
55         int i;
56         if (undoActions != NULL) {
57                 for (i = 0; i < numUndoActions; i++)
58                         MolActionRelease(undoActions[i]);
59                 free(undoActions);
60         }
61         undoActions = actions;
62         numUndoActions = count;
63 }
64
65 void
66 MyCommand::SetRedoActions(MolAction **actions, int count)
67 {
68         int i;
69         if (redoActions != NULL) {
70                 for (i = 0; i < numRedoActions; i++)
71                         MolActionRelease(redoActions[i]);
72                 free(redoActions);
73         }
74         redoActions = actions;
75         numRedoActions = count;
76 }
77
78 bool
79 MyCommand::Do()
80 {
81         MyDocument *doc = MyDocumentFromMolecule(mol);
82         int i;
83         if (doc != NULL) {
84                 bool retval = true;
85                 if (redoActions != NULL) {
86                         doc->SetCurrentCommand(this);
87                         for (i = numRedoActions - 1; i >= 0; i--) {
88                                 if (MolActionPerform(mol, redoActions[i]) != 0) {
89                                         retval = false;
90                                         break;
91                                 }
92                         }
93                         doc->CleanUndoStack(retval);
94                 }
95                 return retval;
96         }
97         return false;
98 }
99
100 bool
101 MyCommand::Undo()
102 {
103         MyDocument *doc = MyDocumentFromMolecule(mol);
104         int i;
105         if (doc != NULL) {
106                 bool retval = true;
107                 if (undoActions != NULL) {
108                         doc->SetIsUndoing(true);
109                         doc->SetCurrentCommand(this);
110                         for (i = numUndoActions - 1; i >= 0; i--) {
111                                 if (MolActionPerform(mol, undoActions[i]) != 0) {
112                                         retval = false;
113                                         break;
114                                 }
115                         }
116                         doc->CleanUndoStack(retval);
117                 }
118                 return retval;
119         }
120         return false;
121 }