Debian Codegraph @

src/main.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 2 of 3 — not the installed (fully patched) source.
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, pos, size, style|wxFULL_REPAINT_ON_RESIZE, name)
629 {
630 initdone=false;
631 shownHQ=false;
632 }
633
634 TestGLCanvas::~TestGLCanvas()
635 {
636 }
637
638
639 wxString errorMSG(int errcode){
640 wxString res;
641 res=_T("OpenGL problems:\n\n");
642 if (errcode&ERRGL_NO_GLEW) {
643 res+=_T(" - cannot initialize GLEW:\n ");
644 res+= wxString::FromAscii(CgUtil::lasterr);
645 res+=_T("\n");
646 }
647 if (errcode&ERRGL_NO_FS) res+=_T(" - no Programmable Fragment Shader found\n");
648 if (errcode&ERRGL_NO_VS) res+=_T(" - no Programmable Vertex Shader found\n");
649 if (errcode&ERRGL_NO_FBO_SHADOWMAP) res+=_T(" - cannot initialize FrameBufferObject for shadowmaps\n");
650 if (errcode&ERRGL_NO_FBO_HALO) res+=_T(" - cannot initialize FrameBufferObject for halos\n");
651 if (errcode&ERRGL_NO_FBO_HALO) res+=_T(" - cannot initialize FrameBufferObject for A.O. computation\n");
652 res+=_T("\n(Hint: update graphic card drivers)");
653 return res;
654 }
655
656 void TestGLCanvas::OnPaint( wxPaintEvent& event )
657 {
658
659
660 /* must always be here */
661 wxPaintDC dc(this);
662
663 #ifndef __WXMOTIF__
664 if (!GetContext()) return;
665 #endif
666
667 SetCurrent();
668
669 if (!initdone) {
670 static bool once=false;
671 if (!once) {
672 once=true;
673 int errcode=initGl();
674 if (errcode!=ERRGL_OK){
675 wxMessageBox(errorMSG(errcode), _T("Unrecoverable error: Problems initializing graphics"), wxOK | wxICON_EXCLAMATION, this);
676 exit(0);
677 }
678 else initdone=true;
679 }
680 }
681
682 #ifdef _WIN32
683 if (!initdone) wxGLCanvas::OnPaint(event); else
684 #endif
685 if (mol.IsReady()) {
686 if (mustDoHQ) {
687 drawFrame( hardSettings.STILL_QUALITY );
688 shownHQ=true;
689 mustDoHQ=false;
690 } else {
691 drawFrame( hardSettings.MOVING_QUALITY );
692 shownHQ=false;
693 }
694 SwapBuffers();
695 }
696 else {
697 clearFrame();
698 SwapBuffers();
699 }
700 }
701
702 void TestGLCanvas::OnSize(wxSizeEvent& event)
703 {
704
705 // this is also necessary to update the context on some platforms
706 wxGLCanvas::OnSize(event);
707
708 // set GL viewport (not called by wxGLCanvas::OnSize on all platforms...)
709
710 GetClientSize(&winx, &winy);
711 mainCanvas.SetVideoSize(winx);
712 #ifndef __WXMOTIF__
713 if ( GetContext() )
714 #endif
715 {
716 SetCurrent();
717 glViewport(0, 0, (GLint) winx, (GLint) winy);
718 }
719 }
720
721
722 #if defined(_WIN32)
723 void MyFrame::OnEraseBackground(wxEraseEvent& event)
724 {
725 wxFrame::OnEraseBackground(event);
726 /* Do nothing, to avoid flashing on MSW */
727 }
728 #endif
729
730 void TestGLCanvas::OnEraseBackground(wxEraseEvent& event)
731 {
732 #if defined(_WIN32)
733 if (!initdone) wxGLCanvas::OnEraseBackground(event);
734 #endif
735 /* Do else do nothing, to avoid flashing on MSW */
736 }
737
738 bool wxConsumeTrackBallEvent(wxMouseEvent& event, Trackball &track);
739 bool wxConsumeTrackBallEvent(wxKeyEvent& event, bool down, Trackball &track);
740
741 void TestGLCanvas::OnKeyUp( wxKeyEvent& event ){
742 wxConsumeTrackBallEvent(event,false,track);
743 }
744
745 extern int CSIZE; // number of texels for a patch of an aotm
746
747 void TestGLCanvas::OnKeyDown( wxKeyEvent& event ){
748 wxConsumeTrackBallEvent(event,true,track);
749 #ifndef _WIN32
750 wxString path = wxStandardPaths::Get().GetResourcesDir() + _T("/presets/new.preset");
751 #else
752 wxString path = "presets\\new.preset";
753 #endif
754 if (event.GetKeyCode() == WXK_F7 ) {
755 cgSettings.Save((const char *)path.c_str());
756 }
757
758 if (event.GetKeyCode() == WXK_F6 ) {
759 if (cgSettings.Load((const char *)path.c_str())) {
760 MyTab::UpdateAll();
761
762 cgSettings.ResetHalo();
763 cgSettings.UpdateShaders();
764 SceneChanged();
765 }
766
767 }
768
769 if (event.GetKeyCode() == WXK_F5 ) {
770 mol.PrepareAOSingleView();
771 SceneChanged();
772 }
773
774 if (event.GetKeyCode() == WXK_F2 ) {
775 static int status=0;
776 status=(status+1) % 5;
777 draw_balls = (status==0) || (status==1) || (status==2) ;
778 draw_sticks = (status==0) || (status==1) || (status==3) || (status==4) ;
779 draw_wireframe_balls= (status==1) ;
780 draw_wireframe_sticks= (status==1) || (status==3) ;
781
782 SceneChanged();
783 }
784
785 if (event.GetKeyCode() == WXK_F1 ) {
786 wxString text;
787 if (!mol.IsReady() ) text=wxT("(no molecule)");
788 else {
789
790 text=wxString( mol.GetMolName(), wxConvUTF8 ).BeforeLast('.') +
791 wxT("\n (file: \"")+wxString( mol.filename, wxConvUTF8 ) +wxT("\")\n\n");
792
793 text=text+wxString::Format(wxT("%d atoms\n"),mol.atom.size());
794
795 if (mol.sticks) {
796 text=text+wxString::Format(wxT("%d bonds\n"),mol.bond.size());
797 }
798
799
800 text=text+wxString::Format(
801 _T("\nUsing:\n Texture size = %dx%d\n Patch size=%dx%d"),
802 moltextureCanvas.GetHardRes(),
803 moltextureCanvas.GetHardRes(),
804 CSIZE,CSIZE
805 );
806
807
808 }
809 wxMessageBox(text, _T("QuteMol - file info"), wxOK | wxICON_INFORMATION, this);
810 }
811
812 if (event.GetKeyCode() == WXK_F6 ) {
813 use_accurate_halo=!use_accurate_halo;
814 SceneChanged();
815 }
816
817 // temp, should e set auto depending on dist
818 if (event.GetKeyCode() == WXK_F2 ) {
819
820 cgSettings.P_capping=!cgSettings.P_capping;
821 cgSettings.UpdateShaders();
822 SceneChanged();
823 }
824 }
825
826
827 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
828 {
829 }
830
831
832
833 void MyFrame::OnSaveSnap (wxCommandEvent & event)
834 {
835
836 int sx,sy;
837 static const wxString FILETYPES = _T(
838 "PNG (lossless)|*.png|"
839 "JPEG (lossy)|*.jpg|"
840 "GIF animation|*.gif"
841 );
842 wxFileDialog * saveSnapDialog =
843 new wxFileDialog ( this,
844 wxString (_T("Save a snapshot")),
845 wxString(), // def path
846 wxString(),
847 FILETYPES,
848 wxFD_SAVE | /*wxFD_CHANGE_DIR |*/ wxFD_OVERWRITE_PROMPT
849 );
850
851 static int lastFilterIndex=-1;
852
853 if (lastFilterIndex==-1) lastFilterIndex=saveSnapDialog->GetFilterIndex();
854 saveSnapDialog->SetFilterIndex( lastFilterIndex );
855
856 wxString ext;
857 if (lastFilterIndex==1) ext=_T(".jpg");
858 else if (lastFilterIndex==2) ext=_T(".gif");
859 else ext=_T(".png");
860 saveSnapDialog->SetFilename( wxString( mol.GetFilenameSnap(), wxConvUTF8 )+ ext );
861 static wxString CANNOT_SAVE = _T("Could not save snap!\n\nYou might try setting\na lower resolution\nor removing AntiAliasing...");
862 if (saveSnapDialog->ShowModal() == wxID_OK)
863 if ((new savesnapDialog(this,saveSnapDialog->GetFilterIndex()))->ShowModal() == wxID_OK )
864 {
865
866
867 int jj=saveSnapDialog->GetFilterIndex();
868 lastFilterIndex=jj;
869
870 Byte* snap = 0;
871 int AAMult=(hardSettings.SNAP_ANTIALIAS)?2:1;
872 bool useTransp=(hardSettings.PNG_TRANSPARENT==1) && (jj==0);
873 if (jj!=2) {
874
875 sx=sy=hardSettings.SNAP_SIZE*AAMult;
876
877 if ((useTransp) && (cgSettings.UseHalo()>0)) {
878 cgSettings.doingAlphaSnapshot=true;
879 cgSettings.ResetHalo();
880 cgSettings.UpdateShaders();
881 }
882
883 snap= GetSnapshot(sx,sy, useTransp );
884
885 if (cgSettings.doingAlphaSnapshot) {
886 cgSettings.doingAlphaSnapshot=false;
887 cgSettings.ResetHalo();
888 cgSettings.UpdateShaders();
889 }
890 if (!snap) {
891 wxMessageBox(CANNOT_SAVE, _T("OpenGL problems?"), wxOK | wxICON_EXCLAMATION, this);
892 return;
893 }
894 } else sx=sy=hardSettings.GIF_SNAP_SIZE*AAMult;
895 {
896
897 wxString fn=saveSnapDialog->GetPath();
898
899 // fix filename Extension
900 //////////////////////////
901 //wxString ext[3]={ _T("png"), _T("jpg") , _T("png") };
902 //wxString cext=fn.AfterLast('.');
903 //if (cext.CmpNoCase(ext[0])||cext.CmpNoCase(ext[1])) fn=fn.BeforeLast('.');
904 //fn=fn+_T('.')+ext[ jj ];
905
906 switch (jj) {
907 case 0:
908 case 1:
909 if (!useTransp) {
910 StartProgress("Saving snap!", 2);
911 wxImage snapi(sx,sy,snap);
912 if (AAMult!=1) snapi=snapi.Scale(sx/AAMult,sy/AAMult);
913 UpdateProgress(1);
914 wxBitmap(snapi.Mirror(false)).SaveFile(
915 fn,
916 (jj==0)?wxBITMAP_TYPE_PNG:wxBITMAP_TYPE_JPEG
917 );
918 UpdateProgress(2);
919 EndProgress();
920 }
921 else {
922 if (AAMult!=1) downsample2x2(snap, sx, sy);
923 PNGSaveWithAlpha((const char*)fn.mb_str(wxConvUTF8),snap,sx/AAMult,sy/AAMult,1);
924 };
925 break;
926 case 2: {
927 GifWrapper gifw;
928 int N = 0;
929 double totalTime = 0;
930 double subStepTime=0;
931 double startTime;startTime=hardSettings.GIF_INITIAL_PAUSE/1000.0;
932 if (hardSettings.GIF_ANIMATION_MODE==0) {
933 N=hardSettings.GIF_ROT_N_FRAMES;
934 totalTime=hardSettings.GIF_ROT_MSEC/1000.0;
935 }
936 if (hardSettings.GIF_ANIMATION_MODE==1) {
937 N=hardSettings.GIF_INSP_N_FRAMES;
938 totalTime=hardSettings.GIF_INSP_MSEC/1000.0;
939 }
940 if (hardSettings.GIF_ANIMATION_MODE==2) {
941 N=hardSettings.GIF_6SIDES_N_FRAMES*6;
942 totalTime=hardSettings.GIF_6SIDES_MSEC*6.0/1000.0;
943 subStepTime=hardSettings.GIF_6SIDES_PAUSE/1000.0;
944 }
945
946 StartProgress("Saving GIF", N);
947 for (int i=0; i<N; i++) {
948 if (!UpdateProgress(i)) break;
949 setAniStep(double(i)/N);
950 snap = GetSnapshot(sx,sy, false);
951
952 if (!snap) {
953 wxMessageBox(CANNOT_SAVE, _T("OpenGL problems?"), wxOK | wxICON_EXCLAMATION, this);
954 EndProgress();
955 return;
956 }
957
958 if (AAMult!=1) downsample2x2NoAlpha(snap, sx, sy);
959 gifw.AddFrame(snap, sx/AAMult, sy/AAMult,
960 totalTime/N
961 + ( (i==0)?startTime:0)
962 + ( (i%(N/6)==0)?subStepTime:0)
963 );
964 }
965 stopAni();
966 gifw.Save( (const char*)fn.mb_str(wxConvUTF8) );
967 EndProgress();
968 } break;
969 }
970
971 }
972 }
973
974 }
975
976
977 void MyFrame::OnReadFile (wxString filename)
978 {
979 if (mol.ReadPdb((const char*)filename.mb_str(wxConvUTF8) )) {
980 if ((mol.natm==0) && (mol.nhetatm!=0)) geoSettings.showHetatm=true;
981 MyTab::EnableGeom();
982 UpdateShadowmap();
983 wxString name( mol.GetMolName(), wxConvUTF8 );
984 SetTitle( name.BeforeLast('.')+ _T(" - QuteMol") );
985 m_tb->SetTitleText(name);
986 geoSettings.Apply();
987
988 // redo shaders, as texture size could have changed
989 cgSettings.UpdateShaders();
990
991 } else {
992 wxMessageBox(wxString::FromAscii(QAtom::lastReadError), _T(":-("), wxOK | wxICON_EXCLAMATION, this);
993 m_tb->SetTitleText();
994 }
995
996 // remake shaders cos texture size could have changed...
997
998
999 //theText->LoadFile(openFileDialog->GetFilename());
1000 //SetStatusText(GetCurrentFilename(), 0);
1001 }
1002
1003
1004 void MyFrame::OnOpenFile (wxCommandEvent & event)
1005 {
1006 static const wxString FILETYPES = _T(
1007 "Protein Data Bank molecule (pdb, vdb)|*.pdb;*.vdb|"
1008 "All files|*.*"
1009 );
1010 wxFileDialog * openFileDialog =
1011 new wxFileDialog ( this,
1012 wxString(_T("Open file")),
1013 wxString(), // def path
1014 wxString(),
1015 FILETYPES,
1016 wxFD_OPEN | wxFD_FILE_MUST_EXIST/*| wxFD_CHANGE_DIR*/,
1017 wxDefaultPosition);
1018
1019 if (openFileDialog->ShowModal() == wxID_OK) {
1020 OnReadFile( openFileDialog->GetPath() );
1021 }
1022 /* wxString filename=openFileDialog->GetFilename();
1023 wxString dir=openFileDialog->GetDirectory();*/
1024 }
1025
1026 void MyFrame::OnIdle(wxIdleEvent& event){
1027 if (mol.DoingAO()) {
1028 m_tb->UpdateGearsIcon(true);
1029 if (mol.PrepareAOstep()) {
1030 m_canvas->SceneChanged();
1031 event.RequestMore(false);
1032 m_tb->UpdateGearsIcon(false);
1033 return;
1034 }
1035 else event.RequestMore(true);
1036 } else {
1037 event.RequestMore(false);
1038 if (hardSettings.STILL_QUALITY!=hardSettings.MOVING_QUALITY)
1039 if (!m_canvas->shownHQ) {
1040 m_canvas->shownHQ=true;
1041 mustDoHQ=true;
1042 m_canvas->Refresh(false);
1043 }
1044 m_tb->UpdateGearsIcon(false);
1045 }
1046 }
1047
1048 void TestGLCanvas::OnMouse( wxMouseEvent& event )
1049 {
1050
1051 static bool useLightTrack=false;
1052
1053 if ( event.m_rightDown || event.m_leftDown || event.m_middleDown ) {
1054 if (!HasCapture()) CaptureMouse();
1055 }
1056 else if (HasCapture()) ReleaseMouse();
1057
1058 useLightTrack=event.m_rightDown;
1059
1060 bool consumed=false;
1061 if (useLightTrack) {
1062 MovingLightMode=true;
1063 if (wxConsumeTrackBallEvent( event, lightTrack)) {
1064 SetFocus();
1065 consumed=true;
1066 }
1067 } else {
1068 MovingLightMode=false;
1069 if (wxConsumeTrackBallEvent( event, track)) {
1070 SetFocus();
1071 consumed=true;
1072 }
1073 }
1074
1075 if ((consumed) && (!event.ButtonUp()) && (!event.ButtonDown())) SceneChanged();
1076
1077 /* if ( event.m_rightUp ) {
1078 useLightTrack=false;
1079 }*/
1080
1081 }
1082
1083 void TestGLCanvas::InitGL()
1084 {
1085 initGl();
1086 }
1087