OSDN Git Service

Ruby-2.0.0 is now included in the repository.
[molby/Molby.git] / MolLib / MainViewCommon.c
1 /*
2  *  MainViewCommon.c
3  *
4  *  Created by Toshi Nagata on 12/09/01.
5  *  Copyright 2012 Toshi Nagata. All rights reserved.
6  *
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 2 of the License.
10  
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  */
16
17 /*  On MinGW, #include of <GL/gl.h> should be before something in MolLib.h.
18     Therefore, we include MainView.h first separately and then include
19     the rest of MolLib.h.  */
20 #include "MainView.h"
21 #include "MolLib.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #pragma mark ====== Drawing Settings ======
28
29 MainView *
30 MainView_new(void)
31 {
32         MainView *mview = (MainView *)calloc(sizeof(MainView), 1);
33         if (mview != NULL) {
34                 /*  Initialize common members (that are used both in GUI and CMD versions  */
35                 memset(mview, 0, sizeof(MainView));
36                 mview->track = TrackballNew();
37                 mview->atomRadius = 0.2;
38                 mview->bondRadius = 0.1;
39                 mview->atomResolution = 12;
40                 mview->bondResolution = 8;
41                 mview->probabilityScale = 1.5382;
42                 mview->dimension = 10.0;
43                 mview->showHydrogens = mview->showDummyAtoms = mview->showExpandedAtoms = 1;
44                 mview->showPeriodicBox = 1;
45                 mview->showGraphite = 5;
46         }
47         return mview;
48 }
49
50 void
51 MainView_release(MainView *mview)
52 {
53         if (mview != NULL) {
54                 if (mview->ref != NULL) {
55                         fprintf(stderr, "%s:%d:Memory leak warning: mview->ref is not NULL\n", __FILE__, __LINE__);
56                         return;
57                 }
58         //      MainView_setMolecule(mview, NULL);
59                 TrackballRelease(mview->track);
60 #if !defined(__CMDMAC__)
61                 IntGroupRelease(mview->tableCache);
62                 IntGroupRelease(mview->tableSelection);
63                 if (mview->nlabels > 0) {
64                         int i;
65                         for (i = 0; i < mview->nlabels; i++) {
66                                 MainViewCallback_releaseLabel(mview->labels[i].label);
67                         }
68                         free(mview->labels);
69                         free(mview->sortedLabels);
70                 }
71                 if (mview->rotateFragment != NULL)
72                         IntGroupRelease(mview->rotateFragment);
73                 if (mview->rotateFragmentOldPos != NULL)
74                         free(mview->rotateFragmentOldPos);
75                 if (mview->visibleFlags != NULL)
76                         free(mview->visibleFlags);
77 #endif
78         }
79         free(mview);
80 }
81
82 /*
83 void
84 MainView_setMolecule(MainView *mview, struct Molecule *mol)
85 {
86         if (mview == NULL || mview->mol == mol)
87                 return;
88         if (mview->mol != NULL) {
89                 mview->mol->mview = NULL;  //  No need to release
90                 MoleculeRelease(mview->mol);
91         }
92         mview->mol = mol;
93         if (mol != NULL) {
94                 MoleculeRetain(mol);
95                 mol->mview = mview;  //  No retain
96                 MainViewCallback_moleculeReplaced(mview, mol);
97                 MoleculeCallback_notifyModification(mol, 0);
98         }
99 }
100 */
101
102 void
103 MainView_setBackgroundColor(MainView *mview, float red, float green, float blue)
104 {
105         if (mview != NULL) {
106                 mview->background_color[0] = red;
107                 mview->background_color[1] = green;
108                 mview->background_color[2] = blue;
109                 MoleculeCallback_notifyModification(mview->mol, 0);
110         }
111 }
112
113 void
114 MainView_getBackgroundColor(const MainView *mview, float *rgb)
115 {
116         if (mview != NULL) {
117                 rgb[0] = mview->background_color[0];
118                 rgb[1] = mview->background_color[1];
119                 rgb[2] = mview->background_color[2];
120         }
121 }
122
123 #pragma mark ====== Graphics ======
124
125 int
126 MainView_insertGraphic(MainView *mview, int index, const MainViewGraphic *graphic)
127 {
128         if (index < 0 || index >= mview->ngraphics)
129                 index = mview->ngraphics;
130         InsertArray(&mview->graphics, &mview->ngraphics, sizeof(MainViewGraphic), index, 1, graphic);
131         MoleculeCallback_notifyModification(mview->mol, 0);
132         return index;
133 }
134
135 int
136 MainView_removeGraphic(MainView *mview, int index)
137 {
138         MainViewGraphic *g;
139         if (index < 0 || index >= mview->ngraphics)
140                 return -1;
141         g = &mview->graphics[index];
142         if (g->points != NULL)
143                 free(g->points);
144         if (g->normals != NULL)
145                 free(g->normals);
146         DeleteArray(&mview->graphics, &mview->ngraphics, sizeof(MainViewGraphic), index, 1, NULL);
147         MoleculeCallback_notifyModification(mview->mol, 0);
148         return index;
149 }
150