1 /*************************************************************************** 2 * wnd.h is part of Math Graphic Library 3 * Copyright (C) 2007-2016 Alexey Balakin <mathgl.abalakin@gmail.ru> * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU Lesser General Public License as * 7 * published by the Free Software Foundation; either version 3 of the * 8 * License, or (at your option) any later version. * 9 * * 10 * This program is distributed in the hope that it will be useful, * 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 * GNU General Public License for more details. * 14 * * 15 * You should have received a copy of the GNU Lesser General Public * 16 * License along with this program; if not, write to the * 17 * Free Software Foundation, Inc., * 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 ***************************************************************************/ 20 #ifndef _MGL_WND_H_ 21 #define _MGL_WND_H_ 22 23 #include "mgl2/mgl.h" 24 #include "mgl2/wnd_cf.h" 25 //----------------------------------------------------------------------------- 26 MGL_EXPORT void *mgl_draw_calc(void *p); 27 void MGL_EXPORT mgl_parse_comments(const char *text, double &a1, double &a2, double &da, std::vector<std::string> &anim, std::string &dlg_ids, std::vector<std::string> &dlg_par); 28 void MGL_EXPORT mgl_parse_comments(const wchar_t *text, double &a1, double &a2, double &da, std::vector<std::wstring> &anim, std::string &dlg_ids, std::vector<std::wstring> &dlg_par); 29 void MGL_EXPORT mgl_parse_animation(const char *text, std::vector<std::string> &anim); 30 void MGL_EXPORT mgl_parse_animation(const wchar_t *text, std::vector<std::wstring> &anim); 31 //----------------------------------------------------------------------------- 32 /// Class for drawing in windows (like, mglCanvasFL, mglCanvasQT and so on) 33 /// Make inherited class and redefine Draw() function if you don't want to use function pointers. 34 class MGL_EXPORT mglDraw 35 { 36 public: 37 virtual int Draw(mglGraph *)=0; ///< Function for drawing Reload()38 virtual void Reload(){} ///< Function for reloading data Param(char id,const char * val)39 virtual void Param(char id, const char *val){} ///< Function for setting parameter Click()40 virtual void Click() {} ///< Callback function on mouse click 41 #if MGL_HAVE_PTHR_WIDGET mglDraw()42 mglDraw() { running=false; pthread_mutex_init(&mutex,NULL); thr=0; } ~mglDraw()43 virtual ~mglDraw() { pthread_mutex_destroy(&mutex); } 44 Calc()45 virtual void Calc() {} ///< Function for calculations Run()46 inline void Run() ///< Run/resume calculation in other thread 47 { 48 if(!running) 49 { pthread_mutex_trylock(&mutex); pthread_mutex_unlock(&mutex); 50 pthread_create(&thr,0,mgl_draw_calc,this); 51 pthread_detach(thr); running = true; } 52 } Cancel()53 inline void Cancel() ///< Cancel thread with calculations 54 { pthread_cancel(thr); running = false; } Pause()55 inline void Pause() ///< Pause calculation 56 { pthread_mutex_lock(&mutex); } Continue()57 inline void Continue() ///< Continue calculation 58 { pthread_mutex_trylock(&mutex); pthread_mutex_unlock(&mutex); } Check()59 inline void Check() ///< Check if calculation can be continued (should be called inside Calc() ) 60 { pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); } 61 //protected: 62 pthread_t thr; 63 bool running; 64 pthread_mutex_t mutex; 65 66 #else mglDraw()67 mglDraw() {} ~mglDraw()68 virtual ~mglDraw() {} 69 #endif 70 }; 71 //----------------------------------------------------------------------------- 72 extern "C" { 73 int MGL_EXPORT mgl_draw_graph(HMGL gr, void *p); 74 // NOTE: MGL_EXPORT mgl_draw_class() and MGL_EXPORT mgl_draw_load() use mglWindow* only. Don't use it with inherited classes 75 int MGL_EXPORT mgl_draw_class(HMGL gr, void *p); 76 void MGL_EXPORT mgl_click_class(void *p); 77 void MGL_EXPORT mgl_reload_class(void *p); 78 void MGL_EXPORT mgl_prop_class(char id, const char *val, void *p); 79 void MGL_EXPORT mgl_prop_func(char id, const char *val, void *p); 80 extern MGL_EXPORT const char *mgl_hints[]; 81 } 82 //----------------------------------------------------------------------------- 83 /// Abstract class for windows displaying graphics 84 class MGL_EXPORT mglWnd : public mglGraph 85 { mglWnd(const mglWnd &)86 mglWnd(const mglWnd &) {} // copying is not allowed 87 const mglWnd &operator=(const mglWnd &t) { return t; } 88 public: mglWnd()89 mglWnd() : mglGraph(-1) {} ~mglWnd()90 virtual ~mglWnd() { mgl_use_graph(gr,-255); } 91 virtual int Run()=0; ///< Run main loop for event handling Window()92 inline void *Window() ///< Return pointer to widget (Fl_Window* or QMainWindow*) used for plotting 93 { return mgl_wnd_window(gr); } Widget()94 inline void *Widget() ///< Return pointer to widget (Fl_MGLView* or QMathGL*) used for plotting 95 { return mgl_wnd_widget(gr); } WndSize(int w,int h)96 inline void WndSize(int w, int h) ///< Resize window 97 { mgl_wnd_size(gr,w,h); } WndMove(int x,int y)98 inline void WndMove(int x, int y) ///< Move window 99 { mgl_wnd_move(gr,x,y); } 100 ToggleAlpha()101 inline void ToggleAlpha() ///< Switch on/off transparency (do not overwrite user settings) 102 { mgl_wnd_toggle_alpha(gr); } ToggleLight()103 inline void ToggleLight() ///< Switch on/off lighting (do not overwrite user settings) 104 { mgl_wnd_toggle_light(gr); } ToggleZoom()105 inline void ToggleZoom() ///< Switch on/off zooming by mouse 106 { mgl_wnd_toggle_zoom(gr); } ToggleRotate()107 inline void ToggleRotate() ///< Switch on/off rotation by mouse 108 { mgl_wnd_toggle_rotate(gr); } ToggleNo()109 inline void ToggleNo() ///< Switch off all zooming and rotation 110 { mgl_wnd_toggle_no(gr); } Update()111 inline void Update() ///< Update picture by calling user drawing function 112 { mgl_wnd_update(gr); } ReLoad()113 inline void ReLoad() ///< Reload user data and update picture 114 { mgl_wnd_reload(gr); } Adjust()115 inline void Adjust() ///< Adjust size of bitmap to window size 116 { mgl_wnd_adjust(gr); } NextFrame()117 inline void NextFrame() ///< Show next frame (if one) 118 { mgl_wnd_next_frame(gr); } PrevFrame()119 inline void PrevFrame() ///< Show previous frame (if one) 120 { mgl_wnd_prev_frame(gr); } Animation()121 inline void Animation() ///< Run slideshow (animation) of frames 122 { mgl_wnd_animation(gr); } SetClickFunc(void (* func)(void * p))123 inline void SetClickFunc(void (*func)(void *p)) ///< Callback function for mouse click 124 { mgl_set_click_func(gr,func); } 125 /// Set callback function for properties setup 126 void SetPropFunc(void (*prop)(char id, const char *val, void *p), void *par=NULL) 127 { mgl_wnd_set_prop(gr,prop,par); } 128 /// Make custom dialog for parameters ids of element properties defined by args MakeDialog(const char * ids,char const * const * args,const char * title)129 inline void MakeDialog(const char *ids, char const * const *args, const char *title) 130 { mgl_wnd_make_dialog(gr, ids, args, title); } 131 inline void MakeDialog(const std::string &ids, const std::vector<std::string> &args, const char *title="") 132 { 133 if(args.size()>0) 134 { 135 std::vector<const char *> buf; buf.reserve(args.size()); 136 for(size_t i=0;i<args.size();i++) buf.push_back(args[i].c_str()); 137 MakeDialog(ids.c_str(), &(buf[0]), title); 138 } 139 } 140 /// Set callback functions for drawing and data reloading 141 inline void SetDrawFunc(int (*draw)(mglBase *gr, void *p), void *par=NULL, void (*reload)(void *p)=NULL) 142 { mgl_wnd_set_func(gr,draw,par,reload); } SetDrawFunc(int (* draw)(mglGraph * gr))143 inline void SetDrawFunc(int (*draw)(mglGraph *gr)) 144 { mgl_wnd_set_func(gr,draw?mgl_draw_graph:0,(void*)draw,0); } SetDrawFunc(mglDraw * draw)145 inline void SetDrawFunc(mglDraw *draw) 146 { mgl_wnd_set_func(gr,draw?mgl_draw_class:0,draw,mgl_reload_class); 147 #if MGL_HAVE_PTHR_WIDGET 148 mgl_wnd_set_mutex(gr, &(draw->mutex)); 149 #endif 150 mgl_set_click_func(gr, mgl_click_class); } 151 #if MGL_HAVE_PTHR_WIDGET 152 /// Mutex for lock/unlock by widget SetMutex(pthread_mutex_t * mutex)153 inline void SetMutex(pthread_mutex_t *mutex) 154 { mgl_wnd_set_mutex(gr, mutex); } 155 #endif 156 SetDelay(double dt)157 inline void SetDelay(double dt) ///< Set delay for animation in seconds 158 { mgl_wnd_set_delay(gr, dt); } GetDelay()159 inline double GetDelay() ///< Get delay for animation in seconds 160 { return mgl_wnd_get_delay(gr); } 161 inline void Setup(bool clf_upd=true, bool showpos=false) 162 { mgl_setup_window(gr, clf_upd, showpos); } LastMousePos()163 inline mglPoint LastMousePos() ///< Last mouse position 164 { mreal x,y,z; mgl_get_last_mouse_pos(gr,&x,&y,&z); return mglPoint(x,y,z); } 165 }; 166 //----------------------------------------------------------------------------- 167 #endif 168