1 // Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef DRAW_CONTEXT_FLTK_H
7 #define DRAW_CONTEXT_FLTK_H
8 
9 #include "GmshConfig.h"
10 #include <algorithm>
11 #if !defined(HAVE_NO_STDINT_H)
12 #include <stdint.h>
13 #elif defined(HAVE_NO_INTPTR_T)
14 typedef unsigned long intptr_t;
15 #endif
16 #include <FL/x.H>
17 #include <FL/gl.h>
18 #include "GmshMessage.h"
19 #include "FlGui.h"
20 #include "drawContext.h"
21 #include "graphicWindow.h"
22 #include "optionWindow.h"
23 #include "openglWindow.h"
24 #include "Context.h"
25 
26 class drawContextFltk : public drawContextGlobal {
27 public:
28   void draw(bool rateLimited = true)
29   {
30     if(!FlGui::available()) return;
31     if(FlGui::instance()->fullscreen->shown()) {
32       FlGui::instance()->fullscreen->make_current();
33       FlGui::instance()->fullscreen->redraw();
34     }
35     else {
36       for(std::size_t i = 0; i < FlGui::instance()->graph.size(); i++) {
37         for(std::size_t j = 0; j < FlGui::instance()->graph[i]->gl.size();
38             j++) {
39           FlGui::instance()->graph[i]->gl[j]->make_current();
40           FlGui::instance()->graph[i]->gl[j]->redraw();
41           glFlush();
42           // FIXME: I don't think this should be done here
43           drawContext *ctx =
44             FlGui::instance()->graph[i]->gl[j]->getDrawContext();
45           ctx->camera.update();
46         }
47       }
48     }
49     FlGui::check(rateLimited);
50   }
drawCurrentOpenglWindow(bool make_current)51   void drawCurrentOpenglWindow(bool make_current)
52   {
53     if(!FlGui::available()) return;
54     openglWindow *gl = FlGui::instance()->getCurrentOpenglWindow();
55     if(make_current) gl->make_current();
56     gl->redraw();
57     glFlush();
58     FlGui::check();
59   }
getFontIndex(const char * fontname)60   int getFontIndex(const char *fontname)
61   {
62     if(fontname) {
63       for(int i = 0; i < NUM_FONTS; i++)
64         if(!strcmp(menu_font_names[i].label(), fontname)) return i;
65     }
66     Msg::Error("Unknown font \"%s\" (using \"Helvetica\" instead)", fontname);
67     Msg::Info("Available fonts:");
68     for(int i = 0; i < NUM_FONTS; i++)
69       Msg::Info("  \"%s\"", menu_font_names[i].label());
70     return 4;
71   }
getFontEnum(int index)72   int getFontEnum(int index)
73   {
74     if(index >= 0 && index < NUM_FONTS)
75       return (intptr_t)menu_font_names[index].user_data();
76     return FL_HELVETICA;
77   }
getFontName(int index)78   const char *getFontName(int index)
79   {
80     if(index >= 0 && index < NUM_FONTS) return menu_font_names[index].label();
81     return "Helvetica";
82   }
getFontAlign(const char * alignstr)83   int getFontAlign(const char *alignstr)
84   {
85     if(alignstr) {
86       if(!strcmp(alignstr, "BottomLeft") || !strcmp(alignstr, "Left") ||
87          !strcmp(alignstr, "left"))
88         return 0;
89       else if(!strcmp(alignstr, "BottomCenter") ||
90               !strcmp(alignstr, "Center") || !strcmp(alignstr, "center"))
91         return 1;
92       else if(!strcmp(alignstr, "BottomRight") || !strcmp(alignstr, "Right") ||
93               !strcmp(alignstr, "right"))
94         return 2;
95       else if(!strcmp(alignstr, "TopLeft"))
96         return 3;
97       else if(!strcmp(alignstr, "TopCenter"))
98         return 4;
99       else if(!strcmp(alignstr, "TopRight"))
100         return 5;
101       else if(!strcmp(alignstr, "CenterLeft"))
102         return 6;
103       else if(!strcmp(alignstr, "CenterCenter"))
104         return 7;
105       else if(!strcmp(alignstr, "CenterRight"))
106         return 8;
107     }
108     Msg::Error("Unknown font alignment \"%s\" (using \"Left\" instead)",
109                alignstr);
110     Msg::Info("Available font alignments:");
111     Msg::Info("  \"Left\" (or \"BottomLeft\")");
112     Msg::Info("  \"Center\" (or \"BottomCenter\")");
113     Msg::Info("  \"Right\" (or \"BottomRight\")");
114     Msg::Info("  \"TopLeft\"");
115     Msg::Info("  \"TopCenter\"");
116     Msg::Info("  \"TopRight\"");
117     Msg::Info("  \"CenterLeft\"");
118     Msg::Info("  \"CenterCenter\"");
119     Msg::Info("  \"CenterRight\"");
120     return 0;
121   }
getFontSize()122   int getFontSize()
123   {
124     if(CTX::instance()->fontSize > 0) { return CTX::instance()->fontSize; }
125     else {
126       int h = Fl::h(); // main (first) screen
127       if(h < 800)
128         return 11;
129       else if(h < 1000)
130         return 12;
131       else if(h < 1200)
132         return 13;
133       else if(h < 1400)
134         return 14;
135       else if(h < 1600)
136         return 15;
137       else if(h < 1800)
138         return 16;
139       float dpih, dpiv;
140       Fl::screen_dpi(dpih, dpiv); // main (first) screen
141       int s = std::max(16, (int)(dpih / 10.));
142       return s;
143     }
144   }
setFont(int fontid,int fontsize)145   void setFont(int fontid, int fontsize) { gl_font(fontid, fontsize); }
getStringWidth(const char * str)146   double getStringWidth(const char *str) { return gl_width(str); }
getStringHeight()147   int getStringHeight() { return gl_height(); }
getStringDescent()148   int getStringDescent() { return gl_descent(); }
drawString(const char * str)149   void drawString(const char *str) { gl_draw(str); }
resetFontTextures()150   void resetFontTextures()
151   {
152 #if((FL_MAJOR_VERSION == 1) && (FL_MINOR_VERSION >= 4)) || defined(__APPLE__)
153     // force font texture recomputation
154     gl_texture_pile_height(gl_texture_pile_height());
155 #endif
156   }
getName()157   std::string getName() { return "Fltk"; }
158 };
159 
160 #endif
161