Debian Codegraph @

src/main.cpp

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 //
2 // qutemol on wxWidgets MAIN
3 //
4 // (on DevCpp, download and install "wxWidgets 2.6.1 unicode" DevPak)
5 //
6 ////////////////////////////////////////////////////////////////////
7
8 #define SnapButton 1
9 #define OpenButton 2
10 #define QuitButton 3
11
12
13
14 #include <GL/glew.h>
15
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #pragma interface
20 #endif
21
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
24
25 #include "wx/image.h"
26 #include "wx/stdpaths.h"
27
28 //#include <iostream>
29 #include <wx/cmdline.h>
30 #include <wx/statline.h>
31
32 #ifdef __BORLANDC__
33 #pragma hdrstop
34 #endif
35
36 #ifndef WX_PRECOMP
37 #include "wx/wx.h"
38 #endif
39
40 #if !wxUSE_GLCANVAS
41 #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
42 #endif
43
44 #include "main.h"
45 #ifdef __WXMAC__
46 # ifdef __DARWIN__
47 # include <OpenGL/glu.h>
48 # else
49 # include <glu.h>
50 # endif
51 #else
52 # include <GL/glu.h>
53 #endif
54
•55 #include "sample.xpm"
56
57 typedef unsigned char Byte;
58 typedef unsigned int uint;
59
60 #include "CgUtil.h"
61
62 #include <math.h>
63
64 #include <vector>
65
66 #include <vcg/space/point3.h>
67 #include <vcg/space/color4.h>
68
69 #include "gen_normal.h"
70 #include <wrap/gui/trackball.h>
71
72 using namespace vcg;
73 using namespace std;
74
75 #include "CubeMapSamp.h"
76 #include "OctaMapSamp.h"
77 #include "Mol.h"
78 //#include "AOgpu.h"
79
80 #include "MyCanvas.h"
81
82 #include "ShadowMap.h"
83 #include "HardSettings.h"
84
85 #include "Common.h"
86
87 #include "MyTab.h"
88
89 #include "saveSnapDialog.h"
90
91 #include "progress.h"
92
93 #include "AtomColor.h"
94
95 extern vcg::Trackball track;
96 extern vcg::Trackball lightTrack;
97
98 extern Mol mol;
99
100 bool mustDoHQ=false;
101
102 #include <wx/dnd.h>
103 #include <wx/notebook.h>
104
105 wxStopWatch sw;
106
107
108 #include "gifSave.h"
109
110 // defined in pngSave
111 bool PNGSaveWithAlpha( const char * filename, const Byte * data, int sx, int sy, int reverse = 0);
112 void downsample2x2(Byte * data, int sx, int sy);
113 void downsample2x2NoAlpha(Byte * data, int sx, int sy);
114
115
116
117 wxBitmap *LoadPngImage(wxString st){
•118 #ifndef _WIN32
119 wxString basePath = wxStandardPaths::Get().GetResourcesDir();
120 wxBitmap* res=new wxBitmap(_T(PKGDATADIR"/image/")+st+_T(".png"),wxBITMAP_TYPE_PNG);
121 #else
122 wxBitmap* res=new wxBitmap(_T("image/")+st+_T(".png"),wxBITMAP_TYPE_PNG);
123 #endif
124 //wxBitmap* res=new wxBitmap(st,wxBITMAP_TYPE_PNG_RESOURCE);
125
126 // Marke all black pixels as transparent (useful for Windows NT only)
127 //res->SetMask(new wxMask(*res, wxColor(0,0,0)));
128
129 return res;
130 }
131
132 void MyTab::SceneChanged(){
133 ((TestGLCanvas*)frame2redraw)->SceneChanged();
134 }
135
136
137 void TestGLCanvas::SceneChanged(){
138 mustDoHQ=false;
139 Refresh(false);
140 }
141
142 void StartTime(){
143 sw.Start();
144 }
145
146 long int TakeTime(FILE *f , char *st){
147 long int delta=sw.Time();
148 fprintf(f,"%5ldmsec: %s\n",delta,st);
149 //globaltime=timen;
150 return delta;
151 }
152 /*long int TakeTotalTime(){
153 long int timen=getTicks(), delta=timen-startingtime;
154 printf("------------------\nTotal time: %5dmsec\n",delta);
155 globaltime=timen;
156 return delta;
157 }*/
158
159
160 long int getTicks(){
161 return 0;
162 }
163
164 void MyToolbar::SetTitleText(wxString s){
•165 #ifdef _WIN32
166 titleText->SetLabel(s);
167 titleTextSizer->Layout();
168 #endif
169 }
170
171 class MyDropTarget : public wxFileDropTarget
172 {
173 public:
174 MyDropTarget(MyFrame *_parent) { parent = _parent; }
175
176 virtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames){
177 parent->OnReadFile(filenames[0]);
178 };
179
180 private:
181 MyFrame *parent;
182 };
183
184 void MyToolbar::OnDrag(wxMouseEvent &event){
185
186 if ( event.LeftDClick()) {
187 parent->Maximize( !(parent->IsMaximized()) );
188 }
189
190 static int omx, omy;
191 int mx, my;
192 mx = event.GetX();
193 my = event.GetY();
194
195 if ( event.Dragging() ){
196 if (!(parent->IsMaximized())) {
197 CaptureMouse();
198 wxPoint cur=parent->GetPosition();
199 int dx=mx-omx;
200 int dy=my-omy;
201 cur.x+=dx;
202 cur.y+=dy;
203 omx=mx-dx;
204 omy=my-dy;
205 parent->SetPosition( cur );
206 }
207 } else {
208 omx=mx; omy=my;
209 ReleaseMouse();
210 }
211 }
212
213 wxNotebook *notebook;
214
215 void MyToolbar::UpdateGearsIcon(){
216 UpdateGearsIcon( mol.DoingAO() );
217 }
218
219 void MyToolbar::UpdateGearsIcon(bool b){
220 if (gearPresent==b) return;
221 if (!b) gearS->Detach(gear); else gearS->Add(gear);
222 gear->Show(b);
223 gearS->Layout();
224 gearPresent=b;
225 /*
226 if (b)
227 gear->Raise();
228 else
229 gear->Lower();*/
230 }
231
232 MyToolbar::MyToolbar(wxTopLevelWindow *_parent, wxWindowID id,
233 const wxPoint& pos,
234 const wxSize& size,
235 long style,
236 const wxString& name): wxPanel(_parent, id, pos, size, style, name){
237
238 parent=_parent;
239
240 // let's build notebook
241 /*wxNotebook **/notebook = new wxNotebook(
242 this, id, pos, size,
243 0,/*style/*|wxNO_FULL_REPAINT_ON_RESIZE|wxNB_TOP,*/
244 name);
245
246 for (int i=0; i<MyTab::Count(); i++) {
247 //if (i==2)
248 notebook->AddPage(new MyTab(notebook,i), MyTab::Title(i), i==0 );
249 parent->SetBackgroundColour(notebook->GetBackgroundColour());
250 }
251
•252 #ifdef _WIN32
253 wxBitmapButton *snapButton = new wxBitmapButton(this, SnapButton,
254 *LoadPngImage(_T("snap_off2")), wxDefaultPosition, wxSize(48,48),0);
255 snapButton->SetBitmapSelected(*LoadPngImage(_T("snap_on")));
256
257 wxBitmapButton *openButton = new wxBitmapButton(this, OpenButton,
258 *LoadPngImage(_T("open_off2")),wxDefaultPosition, wxSize(48,48),0);
259 openButton->SetBitmapSelected(*LoadPngImage(_T("open_on")));
260 #else
261 wxBitmapButton *snapButton = new wxBitmapButton(this, SnapButton,
262 *LoadPngImage(_T("snap_noborder")), wxDefaultPosition, wxSize(48,48),0);
263 wxBitmapButton *openButton = new wxBitmapButton(this, OpenButton,
264 *LoadPngImage(_T("open_noborder")),wxDefaultPosition, wxSize(48,48),0);
265
266 #endif
267 wxBitmapButton *quitButton = new wxBitmapButton(this, QuitButton,
268 *LoadPngImage(_T("quitoff")),
269 wxDefaultPosition, wxSize(15,16) , 0);
270 quitButton->SetBitmapSelected(*LoadPngImage(_T("quiton")));
271
272 wxStaticBitmap *logo = new wxStaticBitmap(this, wxID_ANY, *LoadPngImage(_T("qutemolsm")), wxDefaultPosition );
273
274 gear = new wxStaticBitmap(this, wxID_ANY, *LoadPngImage(_T("gear2")), wxDefaultPosition );
275
276 wxSizer *sizerButtons = new wxBoxSizer( wxVERTICAL );
277 sizerButtons->Add(openButton, 0, wxALL, 1);
278 sizerButtons->Add(snapButton, 0, wxALL, 1);
279
280 titleText = new wxStaticText(this, wxID_ANY, wxT(""));
281 titleText->Disable();
282
•283 #ifdef _WIN32
284 wxSizer *linesizer1 = new wxBoxSizer(wxVERTICAL);
285
286 linesizer1->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
287 linesizer1->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
288 linesizer1->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
289
290 wxSizer *linesizer2 = new wxBoxSizer(wxVERTICAL);
291 linesizer2->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
292 linesizer2->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
293 linesizer2->Add(new wxStaticLine(this),0,wxALL| wxEXPAND, 1);
294
295 titleTextSizer = new wxBoxSizer(wxHORIZONTAL);
296 titleTextSizer->Add(3,3, 0, wxALL, 3);
297 titleTextSizer->Add(linesizer1, 1, wxALL|wxALIGN_CENTER_VERTICAL, 0);
298 titleTextSizer->Add(titleText, 0, wxALL|wxALIGN_CENTER_VERTICAL, 0);
299 titleTextSizer->Add(linesizer2, 20, wxALL|wxALIGN_CENTER_VERTICAL, 0);
300 titleTextSizer->Add(3,3, 0, wxALL, 3); // spacer
301 titleTextSizer->Add(quitButton, 0, wxALL|wxALIGN_CENTER_VERTICAL, 2);
302
303 #else
304 quitButton->Hide();
305 titleText->Hide();
306 #endif
307
308
309
310 wxSizer *imgsizer = new wxBoxSizer(wxHORIZONTAL);
311 imgsizer->Add(22,22, 0);
312 //imgsizer->Add(1,1, 10);
313 imgsizer->Add(logo, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 0); // LOGO
314 //imgsizer->Add(1,1, 9);
315
316 gearS = new wxBoxSizer(wxVERTICAL);
317 gearS->Add(22,22,1);
318 gearS->Add(gear);
319 gearPresent=true;
320
321 imgsizer->Add(gearS, 0, wxALL|wxALIGN_BOTTOM|wxALIGN_CENTER_HORIZONTAL, 2); // LOGO
322
323 wxSizer *topRsizer = new wxBoxSizer(wxVERTICAL);
•324 #ifdef _WIN32
325 topRsizer->Add(titleTextSizer,0, wxALL| wxEXPAND, 0);
326 #endif
•327 topRsizer->Add(imgsizer,1, wxALL| wxEXPAND, 0);
328
329 topsizer = new wxBoxSizer(wxHORIZONTAL);
330 topsizer->Add(sizerButtons, 0, wxALL, 5);
331 topsizer->Add(topRsizer, 1, wxALIGN_TOP| wxALL| wxEXPAND, 0);
332
333
334 wxSizer *globalsizer = new wxBoxSizer(wxVERTICAL);
335 globalsizer->Add(topsizer, 0, wxALL| wxEXPAND, 0);
336 //globalsizer->Add(5, 5, 0, wxALL, 0); // spacer
337 globalsizer->Add(notebook, 1, wxALL|wxEXPAND, 0);
338
339 SetSizer(globalsizer);
340
341 UpdateGearsIcon(false);
342
343 }
344
345
346 // `Main program' equivalent, creating windows and returning main app frame
347 bool MyApp::OnInit()
348 {
349 hardSettings.OnStart();
350
351 cgSettings.SetDefaults(); // <-- quick hack (solves wrong constructor order):
352
353 //if (!wxApp::OnInit()) return false;
354
355 // questo per caricare salvare PNG...
356 wxImage::AddHandler(new wxPNGHandler);
357 wxImage::AddHandler(new wxJPEGHandler);
358
359 // Create the main frame window
360 MyFrame *frame = new MyFrame(NULL, wxT("QuteMol"),
361 // wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE //wxRESIZE_BORDER
•362 #ifndef _WIN32
363 wxDefaultPosition, wxDefaultSize,wxDEFAULT_FRAME_STYLE| wxRESIZE_BORDER
364 #else
365 wxDefaultPosition, wxDefaultSize,wxRESIZE_BORDER
366 #endif
367 );
368
369 /* Make a menubar */
370
371 /*
372 // ...or, maybe NOT
373 wxMenu *fileMenu = new wxMenu;
374
375 fileMenu->Append(wxID_EXIT, wxT("E&xit"));
376 fileMenu->Append(wxID_ABOUT, wxT("A&bout"));
377 fileMenu->Append(wxID_OPEN, wxT("O&pen"));
378
379 wxMenuBar *menuBar = new wxMenuBar;
380 menuBar->Append(fileMenu, wxT("&File"));
381
382 frame->SetMenuBar(menuBar);
383 */
384
385 frame->SetCanvas(
386 new TestGLCanvas(
387 frame, wxID_ANY, wxPoint(0,0), wxSize( winx, winy ), wxNO_BORDER //wxSUNKEN_BORDER
388 )
389 );
390
391 // onle TestGLCanvas to process idles...
392 wxIdleEvent::SetMode(wxIDLE_PROCESS_SPECIFIED);
393 frame->SetExtraStyle(wxWS_EX_PROCESS_IDLE );
394
395 MyTab::frame2redraw=frame->GetCanvas();
396
397 frame->SetToolbar(
398 new MyToolbar(
399 frame, wxID_ANY, wxPoint(winx,0), wxDefaultSize,
400 // wxNO_BORDER //wxSUNKEN_BORDER
401 wxDEFAULT_FRAME_STYLE
402 |
403 // wxNO_FULL_REPAINT_ON_RESIZE |
404 wxCLIP_CHILDREN |
405 wxTAB_TRAVERSAL
406 )
407 );
408
409 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
410 sizer->Add(frame->GetCanvas(), 1, wxGROW|wxSHAPED);
411 sizer->Add(frame->GetToolbar(), 0, wxGROW);
412 frame->SetSizer(sizer);
413
•414 int tbsize=frame->GetToolbar()->GetEffectiveMinSize().x;
415 frame->GetToolbar()->SetSize(tbsize,winy);
416 frame->GetToolbar()->Layout();
417
418 //frame->SetMinSize(wxSize(tbsize+200,200));
419 frame->SetClientSize(wxSize(winx+tbsize,winy));
420
421 static const wxCmdLineEntryDesc cmdLineDesc[] =
422 {
423 {
•424 wxCMD_LINE_PARAM,wxT_2(""),wxT_2(""),wxT_2("filename.pdb:(molecule to be drawn)"),
425 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
426 },
427 {
•428 wxCMD_LINE_OPTION,wxT_2("a"),wxT_2(""),wxT_2("filename.art: optional Atom Render Table"),
429 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
430 },
431 {
•432 wxCMD_LINE_SWITCH,wxT_2("v"),wxT_2(""),wxT_2("don't start, show version name"),
433 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
434 },
435 { wxCMD_LINE_NONE }
436 };
437
438 wxCmdLineParser parser(cmdLineDesc, argc, argv);
439
440 parser.Parse();
441
442 InitQuteMol( NULL );
443
444 wxString artFilename;
445 if (parser.Found(_T("v"))>0) {
446 FILE *f=fopen("output.txt", "wt");
447 if (f) {
448 fprintf(f,"ver 0.4.1");
449 fclose(f);
450 }
451 exit(0);
452 }
453
454 if (parser.Found(_T("a"),&artFilename)>0) {
455 if (!readArtFile(artFilename.ToAscii())) {
456 wxMessageBox(
•457 wxString(_T("Error reading art file \"") + artFilename + _T("\"")),
458 _T("Error reading art file"),
459 wxOK | wxICON_EXCLAMATION, frame);
460 exit(0);
461 return false;
462 }
463 }
464
465 if (parser.GetParamCount()>0) frame->OnReadFile(parser.GetParam(0));
466 /* InitQuteMol( parser.GetParam(0).mb_str(wxConvUTF8) );
467 } else {
468 InitQuteMol( NULL );
469 }*/
470
471 /* Show the frame */
472
473 frame->Center();
474 frame->SetDropTarget( new MyDropTarget(frame) );
475 frame->Show(true);
476
477 return true;
478 }
479 void MyFrame::ResetAO(){
480 mol.ResetAO();
481 }
482
483
484 IMPLEMENT_APP(MyApp)
485
486 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
487 //EVT_PAINT(MyFrame::OnPaint)
488 #if defined(_WIN32)
489 EVT_ERASE_BACKGROUND(MyFrame::OnEraseBackground)
490 #endif
491 EVT_SIZE(MyFrame::OnSize)
492 EVT_BUTTON(OpenButton, MyFrame::OnOpenFile)
493 EVT_BUTTON(SnapButton, MyFrame::OnSaveSnap)
494 EVT_BUTTON(QuitButton, MyFrame::OnExit)
495 EVT_KEY_DOWN(MyFrame::OnKeyDown)
496 EVT_KEY_UP(MyFrame::OnKeyUp)
497 EVT_IDLE(MyFrame::OnIdle)
498 END_EVENT_TABLE()
499
500
501 BEGIN_EVENT_TABLE(MyToolbar, wxPanel)
502 EVT_MOUSE_EVENTS(MyToolbar::OnDrag)
503 END_EVENT_TABLE()
504
505
506 void MyFrame::OnKeyUp( wxKeyEvent& event ){
507 m_canvas->OnKeyUp(event);
508 }
509
510 void MyFrame::OnKeyDown( wxKeyEvent& event ){
511 m_canvas->OnKeyDown(event);
512 }
513
514
515 /* My frame constructor */
516 MyFrame::MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos,
517 const wxSize& size, long style)
518 : wxFrame(frame, wxID_ANY, title, pos, size, style)
519 {
520 m_canvas = NULL;
521 m_tb= NULL;
•522 #if WIN32
523 SetIcon(wxIcon(_T("sample"),wxBITMAP_TYPE_ICO_RESOURCE,32,32));
524 #else
525 SetIcon(wxIcon(sample_xpm));
526 #endif
•527
528 resize_mode=NATURAL;
529 }
530
531 //void MyFrame::OnPaint( wxPaintEvent& event )
532 //{
533 // wxFrame::OnPaint(event);
534 //}
535
536
537 void MyFrame::OnSize(wxSizeEvent& event){
538
539 int w,h;
540 GetClientSize(&w,&h);
541 int hlogo = m_tb->topsizer->GetMinSize().y + 40;
542 if (MyTab::Redispose(h - hlogo )) {
543 GetToolbar()->SetSize(200,winy);
544 }
545
546 if (resize_mode==FORCED ) {
547 // TODO: fullscreen resize
548 //m_canvas->;
549 //m_tb->;
550 resize_mode=NATURAL;
551 wxFrame::OnSize(event);
552 //m_tb->Fit();
553 //m_tb->GetSizer()->RecalcSizes();
554 //GetSizer()->RecalcSizes();
555 //notebook->Layout();
556 //notebook->SetSize(100,100);
557 //m_tb->SetSize(100,100);
558 //notebook->GetSizer()->RecalcSizes();;
559 //m_tb->Layout();
560 //Layout();
561 // Fit();
562 } else
563 if (resize_mode==NATURAL ) {
564 wxFrame::OnSize(event);
565 /*
566 GetClientSize(&w,&h);
567 int tbsize=GetToolbar()->GetBestFittingSize().x;
568 winx=w-tbsize;
569 winy=h;
570
571 GetToolbar()->SetPosition( wxPoint(winx,0) );
572 GetToolbar()->SetSize(tbsize,winy);
573 GetCanvas()->SetSize(winx,winy);
574 */
575 resize_mode=CUSTOM;
576 }
577 else {
578
579 static int oldw=0,oldh=0;
580
581
•582 int w1=m_tb->GetEffectiveMinSize().x;
583 //
584
585 int sx=w-w1, sy=h , s;
586
587 if ((oldw==w) && (oldh!=h)) s=sy; else
588 if ((oldh==h) && (oldw!=w)) s=sx; else
589 s=(sx+sy)/2;
590
591 wxSize size=wxSize(s+w1, s);
592 resize_mode=NATURAL;
593 SetClientSize(size);
594
595 //wxFrame::OnSize( event );
596 //wxSizeEvent event2=wxSizeEvent(size);
597 //wxFrame::OnSize(event2);
598
599 //oldw=w;oldh=h;
600 }
601 }
602
603 void MyFrame::Maximize(bool max){
604
605 //if (custom_resizing) {
606 resize_mode=FORCED;
607 wxFrame::Maximize(max);
608 //}
609 }
610
611 /* Intercept menu commands */
612 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
613 {
614 Close();
615 }
616
617 BEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas)
618 EVT_SIZE(TestGLCanvas::OnSize)
619 EVT_PAINT(TestGLCanvas::OnPaint)
620 EVT_ERASE_BACKGROUND(TestGLCanvas::OnEraseBackground)
621 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouse)
622 EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
623 EVT_KEY_UP(TestGLCanvas::OnKeyUp)
624 END_EVENT_TABLE()
625
626 TestGLCanvas::TestGLCanvas(wxWindow *_parent, wxWindowID id,
627 const wxPoint& pos, const wxSize& size, long style, const wxString& name)
•628 : wxGLCanvas(_parent, id, NULL, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
629 {
630 initdone=false;
631 shownHQ=false;
•632 m_glRC = new wxGLContext(this);
633 }
634
635 TestGLCanvas::~TestGLCanvas()
636 {
•637 delete m_glRC;
638 }
639
640
641 wxString errorMSG(int errcode){
642 wxString res;
643 res=_T("OpenGL problems:\n\n");
644 if (errcode&ERRGL_NO_GLEW) {
645 res+=_T(" - cannot initialize GLEW:\n ");
646 res+= wxString::FromAscii(CgUtil::lasterr);
647 res+=_T("\n");
648 }
649 if (errcode&ERRGL_NO_FS) res+=_T(" - no Programmable Fragment Shader found\n");
650 if (errcode&ERRGL_NO_VS) res+=_T(" - no Programmable Vertex Shader found\n");
651 if (errcode&ERRGL_NO_FBO_SHADOWMAP) res+=_T(" - cannot initialize FrameBufferObject for shadowmaps\n");
652 if (errcode&ERRGL_NO_FBO_HALO) res+=_T(" - cannot initialize FrameBufferObject for halos\n");
653 if (errcode&ERRGL_NO_FBO_HALO) res+=_T(" - cannot initialize FrameBufferObject for A.O. computation\n");
654 res+=_T("\n(Hint: update graphic card drivers)");
655 return res;
656 }
657
658 void TestGLCanvas::OnPaint( wxPaintEvent& event )
659 {
660
661
662 /* must always be here */
663 wxPaintDC dc(this);
664
•665 SetCurrent(*m_glRC);
666
667 if (!initdone) {
668 static bool once=false;
669 if (!once) {
670 once=true;
671 int errcode=initGl();
672 if (errcode!=ERRGL_OK){
673 wxMessageBox(errorMSG(errcode), _T("Unrecoverable error: Problems initializing graphics"), wxOK | wxICON_EXCLAMATION, this);
674 exit(0);
675 }
676 else initdone=true;
677 }
678 }
679
•680 #ifdef _WIN32
681 if (!initdone) wxGLCanvas::OnPaint(event); else
•682 #endif
683 if (mol.IsReady()) {
684 if (mustDoHQ) {
685 drawFrame( hardSettings.STILL_QUALITY );
686 shownHQ=true;
687 mustDoHQ=false;
688 } else {
689 drawFrame( hardSettings.MOVING_QUALITY );
690 shownHQ=false;
691 }
692 SwapBuffers();
693 }
694 else {
695 clearFrame();
696 SwapBuffers();
697 }
698 }
699
700 void TestGLCanvas::OnSize(wxSizeEvent& event)
701 {
702
703 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
704
705 GetClientSize(&winx, &winy);
706 mainCanvas.SetVideoSize(winx);
707 {
•708 SetCurrent(*m_glRC);
709 glViewport(0, 0, (GLint) winx, (GLint) winy);
710 }
711 }
712
713
•714 #if defined(_WIN32)
715 void MyFrame::OnEraseBackground(wxEraseEvent& event)
716 {
717 wxFrame::OnEraseBackground(event);
718 /* Do nothing, to avoid flashing on MSW */
719 }
•720 #endif
721
722 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
723 {
•724 #if defined(_WIN32)
725 if (!initdone) wxGLCanvas::OnEraseBackground(event);
•726 #endif
727 /* Do else do nothing, to avoid flashing on MSW */
728 }
729
730 bool wxConsumeTrackBallEvent(wxMouseEvent& event, Trackball &track);
731 bool wxConsumeTrackBallEvent(wxKeyEvent& event, bool down, Trackball &track);
732
733 void TestGLCanvas::OnKeyUp( wxKeyEvent& event ){
734 wxConsumeTrackBallEvent(event,false,track);
735 }
736
737 extern int CSIZE; // number of texels for a patch of an aotm
738
739 void TestGLCanvas::OnKeyDown( wxKeyEvent& event ){
740 wxConsumeTrackBallEvent(event,true,track);
•741 #ifndef _WIN32
742 wxString path = wxStandardPaths::Get().GetResourcesDir() + _T("/presets/new.preset");
743 #else
744 wxString path = "presets\\new.preset";
745 #endif
746 if (event.GetKeyCode() == WXK_F7 ) {
•747 cgSettings.Save((const char *)path.c_str());
748 }
749
750 if (event.GetKeyCode() == WXK_F6 ) {
•751 if (cgSettings.Load((const char *)path.c_str())) {
752 MyTab::UpdateAll();
753
754 cgSettings.ResetHalo();
755 cgSettings.UpdateShaders();
756 SceneChanged();
757 }
758
759 }
760
761 if (event.GetKeyCode() == WXK_F5 ) {
762 mol.PrepareAOSingleView();
763 SceneChanged();
764 }
765
766 if (event.GetKeyCode() == WXK_F2 ) {
767 static int status=0;
768 status=(status+1) % 5;
769 draw_balls = (status==0) || (status==1) || (status==2) ;
770 draw_sticks = (status==0) || (status==1) || (status==3) || (status==4) ;
771 draw_wireframe_balls= (status==1) ;
772 draw_wireframe_sticks= (status==1) || (status==3) ;
773
774 SceneChanged();
775 }
776
777 if (event.GetKeyCode() == WXK_F1 ) {
778 wxString text;
779 if (!mol.IsReady() ) text=wxT("(no molecule)");
780 else {
781
782 text=wxString( mol.GetMolName(), wxConvUTF8 ).BeforeLast('.') +
783 wxT("\n (file: \"")+wxString( mol.filename, wxConvUTF8 ) +wxT("\")\n\n");
784
785 text=text+wxString::Format(wxT("%d atoms\n"),mol.atom.size());
786
787 if (mol.sticks) {
788 text=text+wxString::Format(wxT("%d bonds\n"),mol.bond.size());
789 }
790
791
792 text=text+wxString::Format(
793 _T("\nUsing:\n Texture size = %dx%d\n Patch size=%dx%d"),
794 moltextureCanvas.GetHardRes(),
795 moltextureCanvas.GetHardRes(),
796 CSIZE,CSIZE
797 );
798
799
800 }
801 wxMessageBox(text, _T("QuteMol - file info"), wxOK | wxICON_INFORMATION, this);
802 }
803
804 if (event.GetKeyCode() == WXK_F6 ) {
805 use_accurate_halo=!use_accurate_halo;
806 SceneChanged();
807 }
808
809 // temp, should e set auto depending on dist
810 if (event.GetKeyCode() == WXK_F2 ) {
811
812 cgSettings.P_capping=!cgSettings.P_capping;
813 cgSettings.UpdateShaders();
814 SceneChanged();
815 }
816 }
817
818
819 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
820 {
821 }
822
823
824
825 void MyFrame::OnSaveSnap (wxCommandEvent & event)
826 {
827
828 int sx,sy;
829 static const wxString FILETYPES = _T(
830 "PNG (lossless)|*.png|"
831 "JPEG (lossy)|*.jpg|"
832 "GIF animation|*.gif"
833 );
834 wxFileDialog * saveSnapDialog =
835 new wxFileDialog ( this,
836 wxString (_T("Save a snapshot")),
837 wxString(), // def path
838 wxString(),
839 FILETYPES,
•840 wxFD_SAVE | /*wxFD_CHANGE_DIR |*/ wxFD_OVERWRITE_PROMPT
841 );
842
843 static int lastFilterIndex=-1;
844
845 if (lastFilterIndex==-1) lastFilterIndex=saveSnapDialog->GetFilterIndex();
846 saveSnapDialog->SetFilterIndex( lastFilterIndex );
847
848 wxString ext;
849 if (lastFilterIndex==1) ext=_T(".jpg");
850 else if (lastFilterIndex==2) ext=_T(".gif");
851 else ext=_T(".png");
852 saveSnapDialog->SetFilename( wxString( mol.GetFilenameSnap(), wxConvUTF8 )+ ext );
853 static wxString CANNOT_SAVE = _T("Could not save snap!\n\nYou might try setting\na lower resolution\nor removing AntiAliasing...");
854 if (saveSnapDialog->ShowModal() == wxID_OK)
855 if ((new savesnapDialog(this,saveSnapDialog->GetFilterIndex()))->ShowModal() == wxID_OK )
856 {
857
858
859 int jj=saveSnapDialog->GetFilterIndex();
860 lastFilterIndex=jj;
861
•862 Byte* snap = 0;
863 int AAMult=(hardSettings.SNAP_ANTIALIAS)?2:1;
864 bool useTransp=(hardSettings.PNG_TRANSPARENT==1) && (jj==0);
865 if (jj!=2) {
866
867 sx=sy=hardSettings.SNAP_SIZE*AAMult;
868
869 if ((useTransp) && (cgSettings.UseHalo()>0)) {
870 cgSettings.doingAlphaSnapshot=true;
871 cgSettings.ResetHalo();
872 cgSettings.UpdateShaders();
873 }
874
875 snap= GetSnapshot(sx,sy, useTransp );
876
877 if (cgSettings.doingAlphaSnapshot) {
878 cgSettings.doingAlphaSnapshot=false;
879 cgSettings.ResetHalo();
880 cgSettings.UpdateShaders();
881 }
882 if (!snap) {
883 wxMessageBox(CANNOT_SAVE, _T("OpenGL problems?"), wxOK | wxICON_EXCLAMATION, this);
884 return;
885 }
886 } else sx=sy=hardSettings.GIF_SNAP_SIZE*AAMult;
887 {
888
889 wxString fn=saveSnapDialog->GetPath();
890
891 // fix filename Extension
892 //////////////////////////
893 //wxString ext[3]={ _T("png"), _T("jpg") , _T("png") };
894 //wxString cext=fn.AfterLast('.');
895 //if (cext.CmpNoCase(ext[0])||cext.CmpNoCase(ext[1])) fn=fn.BeforeLast('.');
896 //fn=fn+_T('.')+ext[ jj ];
897
898 switch (jj) {
899 case 0:
900 case 1:
901 if (!useTransp) {
902 StartProgress("Saving snap!", 2);
903 wxImage snapi(sx,sy,snap);
904 if (AAMult!=1) snapi=snapi.Scale(sx/AAMult,sy/AAMult);
905 UpdateProgress(1);
906 wxBitmap(snapi.Mirror(false)).SaveFile(
907 fn,
908 (jj==0)?wxBITMAP_TYPE_PNG:wxBITMAP_TYPE_JPEG
909 );
910 UpdateProgress(2);
911 EndProgress();
912 }
913 else {
914 if (AAMult!=1) downsample2x2(snap, sx, sy);
915 PNGSaveWithAlpha((const char*)fn.mb_str(wxConvUTF8),snap,sx/AAMult,sy/AAMult,1);
916 };
917 break;
918 case 2: {
919 GifWrapper gifw;
•920 int N = 0;
921 double totalTime = 0;
922 double subStepTime=0;
923 double startTime;startTime=hardSettings.GIF_INITIAL_PAUSE/1000.0;
924 if (hardSettings.GIF_ANIMATION_MODE==0) {
925 N=hardSettings.GIF_ROT_N_FRAMES;
926 totalTime=hardSettings.GIF_ROT_MSEC/1000.0;
927 }
928 if (hardSettings.GIF_ANIMATION_MODE==1) {
929 N=hardSettings.GIF_INSP_N_FRAMES;
930 totalTime=hardSettings.GIF_INSP_MSEC/1000.0;
931 }
932 if (hardSettings.GIF_ANIMATION_MODE==2) {
933 N=hardSettings.GIF_6SIDES_N_FRAMES*6;
934 totalTime=hardSettings.GIF_6SIDES_MSEC*6.0/1000.0;
935 subStepTime=hardSettings.GIF_6SIDES_PAUSE/1000.0;
936 }
937
938 StartProgress("Saving GIF", N);
939 for (int i=0; i<N; i++) {
940 if (!UpdateProgress(i)) break;
941 setAniStep(double(i)/N);
942 snap = GetSnapshot(sx,sy, false);
943
944 if (!snap) {
945 wxMessageBox(CANNOT_SAVE, _T("OpenGL problems?"), wxOK | wxICON_EXCLAMATION, this);
946 EndProgress();
947 return;
948 }
949
950 if (AAMult!=1) downsample2x2NoAlpha(snap, sx, sy);
951 gifw.AddFrame(snap, sx/AAMult, sy/AAMult,
952 totalTime/N
953 + ( (i==0)?startTime:0)
954 + ( (i%(N/6)==0)?subStepTime:0)
955 );
956 }
957 stopAni();
958 gifw.Save( (const char*)fn.mb_str(wxConvUTF8) );
959 EndProgress();
960 } break;
961 }
962
963 }
964 }
965
966 }
967
968
969 void MyFrame::OnReadFile (wxString filename)
970 {
971 if (mol.ReadPdb((const char*)filename.mb_str(wxConvUTF8) )) {
972 if ((mol.natm==0) && (mol.nhetatm!=0)) geoSettings.showHetatm=true;
973 MyTab::EnableGeom();
974 UpdateShadowmap();
975 wxString name( mol.GetMolName(), wxConvUTF8 );
976 SetTitle( name.BeforeLast('.')+ _T(" - QuteMol") );
977 m_tb->SetTitleText(name);
978 geoSettings.Apply();
979
980 // redo shaders, as texture size could have changed
981 cgSettings.UpdateShaders();
982
983 } else {
984 wxMessageBox(wxString::FromAscii(QAtom::lastReadError), _T(":-("), wxOK | wxICON_EXCLAMATION, this);
985 m_tb->SetTitleText();
986 }
987
988 // remake shaders cos texture size could have changed...
989
990
991 //theText->LoadFile(openFileDialog->GetFilename());
992 //SetStatusText(GetCurrentFilename(), 0);
993 }
994
995
996 void MyFrame::OnOpenFile (wxCommandEvent & event)
997 {
998 static const wxString FILETYPES = _T(
999 "Protein Data Bank molecule (pdb, vdb)|*.pdb;*.vdb|"
1000 "All files|*.*"
1001 );
1002 wxFileDialog * openFileDialog =
1003 new wxFileDialog ( this,
1004 wxString(_T("Open file")),
1005 wxString(), // def path
1006 wxString(),
1007 FILETYPES,
•1008 wxFD_OPEN | wxFD_FILE_MUST_EXIST/*| wxFD_CHANGE_DIR*/,
1009 wxDefaultPosition);
1010
1011 if (openFileDialog->ShowModal() == wxID_OK) {
1012 OnReadFile( openFileDialog->GetPath() );
1013 }
1014 /* wxString filename=openFileDialog->GetFilename();
1015 wxString dir=openFileDialog->GetDirectory();*/
1016 }
1017
1018 void MyFrame::OnIdle(wxIdleEvent& event){
1019 if (mol.DoingAO()) {
1020 m_tb->UpdateGearsIcon(true);
1021 if (mol.PrepareAOstep()) {
1022 m_canvas->SceneChanged();
1023 event.RequestMore(false);
1024 m_tb->UpdateGearsIcon(false);
1025 return;
1026 }
1027 else event.RequestMore(true);
1028 } else {
1029 event.RequestMore(false);
1030 if (hardSettings.STILL_QUALITY!=hardSettings.MOVING_QUALITY)
1031 if (!m_canvas->shownHQ) {
1032 m_canvas->shownHQ=true;
1033 mustDoHQ=true;
1034 m_canvas->Refresh(false);
1035 }
1036 m_tb->UpdateGearsIcon(false);
1037 }
1038 }
1039
1040 void TestGLCanvas::OnMouse( wxMouseEvent& event )
1041 {
1042
1043 static bool useLightTrack=false;
1044
1045 if ( event.m_rightDown || event.m_leftDown || event.m_middleDown ) {
1046 if (!HasCapture()) CaptureMouse();
1047 }
1048 else if (HasCapture()) ReleaseMouse();
1049
1050 useLightTrack=event.m_rightDown;
1051
1052 bool consumed=false;
1053 if (useLightTrack) {
1054 MovingLightMode=true;
1055 if (wxConsumeTrackBallEvent( event, lightTrack)) {
1056 SetFocus();
1057 consumed=true;
1058 }
1059 } else {
1060 MovingLightMode=false;
1061 if (wxConsumeTrackBallEvent( event, track)) {
1062 SetFocus();
1063 consumed=true;
1064 }
1065 }
1066
1067 if ((consumed) && (!event.ButtonUp()) && (!event.ButtonDown())) SceneChanged();
1068
1069 /* if ( event.m_rightUp ) {
1070 useLightTrack=false;
1071 }*/
1072
1073 }
1074
1075 void TestGLCanvas::InitGL()
1076 {
1077 initGl();
1078 }
1079