summaryrefslogtreecommitdiffstats
path: root/src/Gui/RenderArea.cpp
blob: ec37f678425d006145ec0e3a1f709cfb8d5d44fd (plain)
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
#include "RenderArea.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <gtkmm/toolbutton.h>
#include <GL/gl.h>

namespace ZoomEdit {
namespace Gui {

GdkGLConfig *RenderArea::glconfig = NULL;

RenderArea::RenderArea(BaseObjectType *cobject, const Glib::RefPtr<Gnome::Glade::Xml> &xml)
: Gtk::DrawingArea(cobject), zoomLevel(0), scale(100), x(0), y(0) {
  if(!glconfig) {
    glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DOUBLE));
    if(!glconfig) glconfig = gdk_gl_config_new_by_mode(GDK_GL_MODE_RGB); // Hmm, can't find double buffered config
    if(!glconfig) {
      std::cerr << "*** No appropriate OpenGL-capable visual found." << std::endl;
      std::exit(1);
    }
  }
  
  signal_realize().connect(sigc::mem_fun(this, &RenderArea::onRealize));
  signal_configure_event().connect(sigc::mem_fun(this, &RenderArea::onConfigureEvent));
  signal_expose_event().connect(sigc::mem_fun(this, &RenderArea::onExposeEvent));
  signal_scroll_event().connect(sigc::mem_fun(this, &RenderArea::onScrollEvent));
  
  Gtk::ToolButton *button;
  xml->get_widget("ToolButtonZoomIn", button);
  if(button)
    button->signal_clicked().connect(sigc::bind(sigc::mem_fun(this, &RenderArea::zoom), 2, 0.5f, 0.5f));
  
  xml->get_widget("ToolButtonZoomOut", button);
  if(button)
    button->signal_clicked().connect(sigc::bind(sigc::mem_fun(this, &RenderArea::zoom), -2, 0.5f, 0.5f));
  
  gtk_widget_set_gl_capability(GTK_WIDGET(cobject), glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE);
}

void RenderArea::onRealize() {
  if(!gdkGLBegin())
    return;
  
  glClearColor(0, 0, 0, 0);
  
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  
  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_POINT_SMOOTH);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  gdkGLEnd();
}

bool RenderArea::onConfigureEvent(GdkEventConfigure *event) {
  updateViewport();
  return true;
}

bool RenderArea::onExposeEvent(GdkEventExpose *event) {
  if(!gdkGLBegin())
    return false;
  
  glClear(GL_COLOR_BUFFER_BIT);
  
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  
  glScalef(scale, scale, 1);
  glTranslatef(-x, -y, 0);
  
  
  drawGrid();
  
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();

  
  gdkSwapBuffers();
  gdkGLEnd();
  
  return true;
}

bool RenderArea::onScrollEvent(GdkEventScroll *event) {
  switch(event->direction) {
    case GDK_SCROLL_UP:
      zoom(1, event->x/get_width(), event->y/get_height());
      return true;
    case GDK_SCROLL_DOWN:
      zoom(-1, event->x/get_width(), event->y/get_height());
      return true;
    default:
      return false;
  }
}

void RenderArea::zoom(int zoom, float x, float y) {
  zoomLevel = std::max(std::min(zoomLevel + zoom, 50), -100);
  scale = 100*std::pow(1.1f, zoomLevel);
  
  queue_draw();
}

void RenderArea::updateViewport() {
  if(!gdkGLBegin())
    return;
  
  glViewport(0, 0, get_width(), get_height());
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  
  if(get_width() != 0 && get_height() != 0)
    glScalef(2.0f/get_width(), -2.0f/get_height(), 1);
  
  gdkGLEnd();
  
  queue_draw();
}

void RenderArea::drawGrid() {
  float depth = std::log10(scale)-0.75f;
  float depth2 = std::floor(depth);
  float step = std::pow(0.1f, depth2);
  float f;
  int i;
  float x1 = x-get_width()/(2*scale), y1 = y-get_height()/(2*scale);
  float x2 = x+get_width()/(2*scale), y2 = y+get_height()/(2*scale);
  
  
  glLineWidth(1.0f);
  
  glBegin(GL_LINES);
  
  for(i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; i++) {
    f = std::min(0.4f*(depth-depth2+i), 0.5f);
    glColor3f(f, f, f);
    
    for(f = x1 - std::fmod(x1, step) - step; f <= x2; f+=step) {
      glVertex2f(f, y1);
      glVertex2f(f, y2);
    }
    
    for(f = y1 - std::fmod(y1, step) - step; f <= y2; f+=step) {
      glVertex2f(x1, f);
      glVertex2f(x2, f);
    }
    
    step *= 10;
  }
  
  glEnd();
}

}
}