OSDN Git Service

Unit Cell dialog is being modified. Still not working very well.
[molby/Molby.git] / wxSources / MyGLCanvas.cpp
1 /*
2  *  MyGLCanvas.cpp
3  *  Molby
4  *
5  *  Created by Toshi Nagata on 08/10/24.
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 "wx/docview.h"
34
35 #include "MyGLCanvas.h"
36 #include "MoleculeView.h"
37 #include "MyApp.h"
38
39 #include "../MolLib/MolLib.h"
40
41 BEGIN_EVENT_TABLE(MyGLCanvas, wxGLCanvas)
42     EVT_SIZE(MyGLCanvas::OnSize)
43     EVT_ERASE_BACKGROUND(MyGLCanvas::OnEraseBackground)
44     EVT_MOUSE_EVENTS(MyGLCanvas::OnMouseEvent)
45     EVT_PAINT(MyGLCanvas::OnPaint)
46         EVT_MOUSE_CAPTURE_LOST(MyGLCanvas::OnCaptureLost)
47 END_EVENT_TABLE()
48
49 #ifdef __WXMSW__
50 int *MyGLAttributes = NULL;
51 #else
52 int MyGLAttributes[20] = { WX_GL_RGBA, WX_GL_MIN_RED, 1, WX_GL_MIN_GREEN, 1,
53         WX_GL_MIN_BLUE, 1, WX_GL_DEPTH_SIZE, 1,
54         WX_GL_DOUBLEBUFFER,
55 #  if defined(__WXMAC__) || defined(__WXCOCOA__)
56         GL_NONE
57 #  else
58         None
59 #  endif
60         };
61 #endif
62
63 // Define a constructor for my canvas
64 MyGLCanvas::MyGLCanvas(MoleculeView *v, wxWindow *frame, const wxPoint& pos, const wxSize& size, long style):
65   wxGLCanvas(frame, wxID_ANY, MyGLAttributes, pos, size, style)
66 {
67         view = v;
68         context = new wxGLContext(this);
69 }
70
71 MyGLCanvas::~MyGLCanvas()
72 {
73         delete context;
74 }
75
76 // Define the repainting behaviour
77 void
78 MyGLCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
79 {
80     wxPaintDC dc(this);
81
82 #ifndef __WXMOTIF__
83 //    if (!GetContext()) return;
84 #endif
85
86     context->SetCurrent(*this);
87     if (view)
88       view->OnDraw(&dc);
89         else {
90                 glClearColor (0, 0, 0, 0);
91                 glClear(GL_COLOR_BUFFER_BIT |
92                                 GL_DEPTH_BUFFER_BIT);
93         }
94     SwapBuffers();
95 }
96
97 void
98 MyGLCanvas::OnCaptureLost(wxMouseCaptureLostEvent &event)
99 {
100         wxPoint pt;
101         int w, h, modifierFlags;
102         float p[2];
103         MainView *mview;
104
105         if (view == NULL)
106                 return;
107
108         pt = ScreenToClient(::wxGetMousePosition());
109         GetClientSize(&w, &h);
110         p[0] = pt.x;
111         p[1] = h - pt.y;
112         modifierFlags = MainViewCallback_modifierFlags(NULL);
113         mview = ((MoleculeView *)view)->mview;
114         MoleculeLock(mview->mol);
115         MainView_mouseUp(((MoleculeView *)view)->mview, p, modifierFlags, 0);
116         MoleculeUnlock(mview->mol);
117 }
118
119 void
120 MyGLCanvas::OnMouseEvent(wxMouseEvent &event)
121 {
122         if (!view)
123                 return;
124
125         wxPoint pt(event.GetPosition());
126         float p[2];
127         int modifierFlags, clickCount;
128         MainView *mview = ((MoleculeView *)view)->mview;
129         int w, h;
130         GetClientSize(&w, &h);
131
132         p[0] = pt.x;
133         p[1] = h - pt.y;  /*  The origin of the internal coordinate should be left-bottom  */
134         modifierFlags = MainViewCallback_modifierFlags(&event);
135         clickCount = MainViewCallback_clickCount(&event);
136
137         if (event.LeftUp() /* || (mview->isDragging && event.Entering() && !event.LeftIsDown()) */) {
138                 if (HasCapture())
139                         ReleaseMouse();
140                 MoleculeLock(mview->mol);
141                 MainView_mouseUp(mview, p, modifierFlags, clickCount);
142                 MoleculeUnlock(mview->mol);
143         } else if (event.LeftDClick()) {
144                 if (HasCapture())
145                         ReleaseMouse();
146                 MoleculeLock(mview->mol);
147                 MainView_mouseUp(mview, p, modifierFlags, 2);
148                 MoleculeUnlock(mview->mol);
149         } else if (event.Dragging()) {
150                 MoleculeLock(mview->mol);
151                 MainView_mouseDragged(mview, p, modifierFlags);
152                 MoleculeUnlock(mview->mol);
153         } else if (event.LeftDown()) {
154                 if (wxWindow::FindFocus() != this)
155                         SetFocus();
156                 CaptureMouse();
157                 MoleculeLock(mview->mol);
158                 MainView_mouseDown(mview, p, modifierFlags);
159                 MoleculeUnlock(mview->mol);
160         } else event.Skip();
161 }
162
163 void
164 MyGLCanvas::OnSize(wxSizeEvent &event)
165 {
166     // this is also necessary to update the context on some platforms
167 //    wxGLCanvas::OnSize(event);
168
169     // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
170     int w, h;
171     GetClientSize(&w, &h);
172 #ifndef __WXMOTIF__
173 //    if (GetContext())
174 #endif
175     {
176         context->SetCurrent(*this);
177         glViewport(0, 0, (GLint) w, (GLint) h);
178     }
179 #if defined(__WXMSW__)
180         //  On MSW, the window is not repainted upon resize event
181         Refresh();
182 #endif
183 }
184
185 void
186 MyGLCanvas::OnEraseBackground(wxEraseEvent & WXUNUSED(event))
187 {
188     // Do nothing, to avoid flashing.
189 }