1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/***************************************************************************
* Fl_MathGL.h is part of Math Graphic Library
* Copyright (C) 2007-2016 Alexey Balakin <mathgl.abalakin@gmail.ru> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _MGL_FL_MATHGL_H_
#define _MGL_FL_MATHGL_H_
#ifdef __MWERKS__
# define FL_DLL
#endif
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Counter.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Progress.H>
#include <mgl2/fltk.h>
class mglCanvas;
class Fl_Double_Window;
//-----------------------------------------------------------------------------
/// Class is FLTK widget which display MathGL graphics
class MGL_EXPORT Fl_MathGL : public Fl_Widget
{
friend class Fl_MGLView;
public:
Fl_Valuator *tet_val; ///< pointer to external tet-angle validator
Fl_Valuator *phi_val; ///< pointer to external phi-angle validator
std::string prim; ///< manual primitives
bool use_pthr; ///< use pthread for update plot
Fl_MathGL(int x, int y, int w, int h, const char *label=0);
virtual ~Fl_MathGL();
/// Set drawing functions and its parameter
inline void set_draw(int (*func)(mglBase *gr, void *par), void *par)
{ if(draw_cl) delete draw_cl; draw_cl=0; draw_func=func; draw_par=par; }
/// Set drawing functions pointed on mglGraph
inline void set_draw(int (*dr)(mglGraph *gr))
{ set_draw(dr?mgl_draw_graph:0,(void*)dr); }
/// Set drawing based on instance of mglDraw class
inline void set_draw(mglDraw *dr)
{ if(draw_cl) delete draw_cl; draw_cl=dr; draw_func=0; }
/// Set function for parameters
inline void set_prop(void (*func)(char id, const char *val, void *par), void *par)
{ prop_func=func; prop_par=par; }
/// Set function for handling events. Note, use Fl::event_key() for getting pressed keybutton
inline void set_handle(int (*func)(int val, void *par), void *par)
{ hndl_func=func; hndl_par=par; }
/// Refresh image (without executing update)
void refresh();
/// Update (redraw) plot
virtual void update();
/// Set angles for additional plot rotation
inline void set_angle(double t, double p){ tet = t; phi = p; }
/// Set bitwise flags for general state (1-Alpha, 2-Light)
inline void set_flag(int f) { flag = f; }
/// Set flags for handling mouse
void set_state(bool z, bool r, bool g=false)
{ zoom = z; rotate = r; grid = g; }
/// Set zoom in/out region
inline void set_zoom(double X1, double Y1, double X2, double Y2)
{ x1 = X1; x2 = X2; y1 = Y1; y2 = Y2; update(); }
/// Get zoom region
inline void get_zoom(double *X1, double *Y1, double *X2, double *Y2)
{ *X1 = x1; *X2 = x2; *Y1 = y1; *Y2 = y2; }
/// Set popup menu pointer
inline void set_popup(const Fl_Menu_Item *pmenu, Fl_Widget *wdg, void *v)
{ popup = pmenu; wpar = wdg; vpar = v; }
/// Set grapher object instead of built-in one.
/// NOTE: Fl_MathGL will automatically delete this object
void set_graph(HMGL gr);
/// Set grapher object instead of built-in one.
inline void set_graph(mglGraph *Gr)
{ set_graph(Gr->Self()); }
/// Get pointer to grapher
inline HMGL get_graph() { return (HMGL)gr; }
/// Nullify grapher object for disabling double free. NOTE: this is internal function.
void no_graph() {gr=0;};
/// Get mglDraw pointer or NULL
inline mglDraw *get_class()
{ mglDraw *d=0;
if(draw_func==mgl_draw_class) d = (mglDraw*)draw_par;
if(draw_cl) d = draw_cl;
return d; }
inline void set_param(char id, const char *val)
{ mglDraw *d=get_class(); if(d) d->Param(id,val); else prop_func(id,val,prop_par); }
/// Show window with warnings after script parsing
inline void set_show_warn(bool s) { show_warn=s; }
/// Ask to stop of script parsing
void stop(bool stop=true);
/// Enable/disable key handling as in mglview (default is false)
inline void set_handle_key(bool val) { handle_keys=val; }
/// Get id of last clicked object
inline int get_last_id() { return last_id; }
void draw_plot(); ///< Single thread drawing itself
/// Check if script is parsing now or not
inline bool running() { return run; }
protected:
mglCanvas *gr; ///< Built-in mglCanvas instance (mglCanvasFL is used by default)
void *draw_par; ///< Parameters for drawing function draw_func().
/// Drawing function for window procedure. It should return the number of frames.
int (*draw_func)(mglBase *gr, void *par);
void *prop_par; ///< Parameters for prop_func().
/// Function for setting properties.
void (*prop_func)(char id, const char *val, void *par);
void *hndl_par; ///< Parameters for hndl_func().
/// Function for handling events.
int (*hndl_func)(int val, void *par);
mglDraw *draw_cl;
int last_id; ///< last selected object id
const Fl_Menu_Item *popup; ///< pointer to popup menu items
Fl_Widget *wpar; ///< widget for popup menu
void *vpar; ///< parameter for popup menu
double tet,phi; ///< rotation angles
bool rotate; ///< flag for handle mouse
bool zoom; ///< flag for zoom by mouse
bool grid; ///< flag to draw grid and edit prim
bool show_warn; ///< show window with warnings
bool handle_keys;
double x1,x2,y1,y2; ///< zoom region
int flag; ///< bitwise flag for general state (1-Alpha, 2-Light)
int x0,y0,xe,ye; ///< mouse position
char mouse_pos[128];
bool run; ///< flag that drawing in progress
const unsigned char *img; ///< image for drawing
#if (MGL_HAVE_PTHREAD|MGL_HAVE_PTHR_WIDGET)
pthread_t thr; ///< main thread for drawing
#endif
virtual void draw(); ///< quick drawing function
int handle(int code); ///< handle mouse events
void resize(int x, int y, int w, int h); ///< resize control
};
//-----------------------------------------------------------------------------
class MGL_EXPORT Fl_MGLView : public Fl_Window
{
public:
Fl_MathGL *FMGL; ///< Control which draw graphics
Fl_Scroll *scroll;
Fl_Menu_Bar *menu;
void *par; ///< Parameter for handling animation
void (*next)(void*); ///< Callback function for next frame
void (*prev)(void*); ///< Callback function for prev frame
mreal (*delay)(void*); ///< Callback function for delay
void (*reload)(void*); ///< Callback function for reloading
/// Toggle transparency (alpha) button
void toggle_alpha();
/// Toggle lighting button
void toggle_light();
/// Toggle slideshow button
void toggle_sshow();
/// Toggle grid drawing button
void toggle_grid();
/// Toggle mouse zoom button
void toggle_zoom() { toggle(zoom, zoom_bt); }
/// Toggle mouse rotate button
void toggle_rotate(){ toggle(rotate, rotate_bt); }
/// Switch off zoom button
void setoff_zoom() { setoff(zoom, zoom_bt); }
/// Switch off rotate button
void setoff_rotate(){ setoff(rotate, rotate_bt); }
/// Check if slideshow running
bool is_sshow() { return sshow; }
/// Toggle pause calculation button
void toggle_pause();
/// Adjust image sizes to the current widget sizes
void adjust()
{ mgl_set_size(FMGL->get_graph(),scroll->w(),scroll->h()); FMGL->size(scroll->w(),scroll->h()); update(); }
/// Get current grapher
HMGL get_graph() { return FMGL->get_graph(); }
/// Update picture by calling user drawing function
void update();
/// Create and show custom dialog
void dialog(const char *ids, char const * const *args, const char *title="")
{
if(!ids || *ids==0) return;
dlg_window(title);
for(int i=0;ids[i];i++) add_widget(ids[i], args[i]);
dlg_finish(); dlg_wnd->show();
}
void dialog(const std::string &ids, const std::vector<std::string> &args, const char *title="")
{
std::vector<const char *> buf; buf.reserve(args.size());
for(size_t i=0;i<args.size();i++) buf.push_back(args[i].c_str());
dialog(ids.c_str(), &(buf[0]), title);
// dlg_window(title);
// for(size_t i=0;i<ids.length();i++) add_widget(ids[i], args[i].c_str());
// dlg_finish(); dlg_wnd->show();
}
void dlg_window(const char *title=""); ///< Create/label dialog window
void add_widget(char id, const char *args); ///< Add widget to dialog
void dlg_show(); ///< Show window
void dlg_hide(); ///< Close window
void get_values(); ///< Get all values from dialog window
void set_progress(int value, int maximal); ///< Set progress
Fl_MGLView(int x, int y, int w, int h, const char *label=0);
virtual ~Fl_MGLView();
protected:
Fl_Button *alpha_bt, *light_bt, *rotate_bt, *anim_bt, *zoom_bt, *grid_bt, *pause_bt;
Fl_Progress *progress;
int grid, alpha, light; ///< Current states of wire, alpha, light switches (toggle buttons)
int sshow, rotate, zoom;///< Current states of slideshow, rotate, zoom switches (toggle buttons)
int pauseC; ///< Current state of pause for calculations
void toggle(int &val, Fl_Button *b, const char *txt=NULL);
void setoff(int &val, Fl_Button *b, const char *txt=NULL);
void exec_pause();
Fl_Double_Window *dlg_wnd; ///< Dialog window itself
std::vector<char*> strs; ///< Strings for widget labels
bool dlg_done; ///< Dialog is created
int dlg_ind; ///< Current index of widget
std::vector<char> dlg_kind; ///< Kind of elements
std::vector<Fl_Widget*> dlg_wdgt; ///< List of widgets
std::vector<char> dlg_ids; ///< Id of elements
std::vector<std::string> dlg_vals; ///< resulting strings
void dlg_finish(); ///< Finish dialog window creation
};
//-----------------------------------------------------------------------------
void MGL_EXPORT mgl_makemenu_fltk(Fl_Menu_ *m, Fl_MGLView *w);
MGL_EXPORT const char *mgl_file_chooser(const char *mess, const char *filter="", bool save=false);
MGL_EXPORT const char *mgl_dir_chooser(const char *mess, const char *path);
//-----------------------------------------------------------------------------
#endif
|