summaryrefslogtreecommitdiff
path: root/linux/main.cpp
blob: 9c97e5e2bdd0c6cab5e21f0f6c3ae829026d0678 (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// LeoCAD
//
// Linux specific initialization
//

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <stdlib.h>
#include <stdio.h>
#include "gtkglarea.h"
#include "menu.h"
#include "custom.h"
#include "project.h"
#include "toolbar.h"
#include "globals.h"
#include "main.h"

// Variables

GtkWidget *main_window;
GtkWidget *drawing_area;

static char default_path[] = "/usr/share/LeoCAD/";
bool ignore_commands = false;

// Functions

void OnCommandDirect(GtkWidget *w, gpointer data)
{
  project->HandleCommand((LC_COMMANDS)(int)data, 0);
}

void OnCommand(GtkWidget* widget, gpointer data)
{
  int id = (int)data;

  if (ignore_commands)
    return;

  if (id >= ID_FILE_RECENT1 && id <= ID_FILE_RECENT4)
  {
    project->HandleCommand(LC_FILE_RECENT, id - ID_FILE_RECENT1);
    return;
  }
  
  if (id >= ID_ACTION_SELECT && id <= ID_ACTION_ROLL)
  {
    project->SetAction(id - ID_ACTION_SELECT);
    return;
  }

  if (id >= ID_VIEW_VIEWPORTS_01 && id <= ID_VIEW_VIEWPORTS_14)
  {
    project->HandleCommand(LC_VIEW_VIEWPORTS, id - ID_VIEW_VIEWPORTS_01);
    return;
  }

  if (id >= ID_CAMERA_FIRST && id <= ID_CAMERA_LAST)
  {
    project->HandleCommand(LC_VIEW_CAMERA_MENU, id - ID_CAMERA_FIRST);
    return;
  }

  switch (id)
  {
    case ID_FILE_EXIT: {
      gtk_main_quit();
    } break;

    case ID_SNAP_A: {
      project->HandleCommand(LC_TOOLBAR_SNAPMENU, 5);
    } break;
  }
}

static gint button_press_event (GtkWidget *widget, GdkEventButton *event)
{
  int x, y;
  x = (int)event->x;
  y = widget->allocation.height - (int)event->y - 1;

  if (event->button == 1)
    project->OnLeftButtonDown(x, y);
  if (event->button == 3)
    project->OnRightButtonDown(x, y);

  gtk_window_set_focus(GTK_WINDOW(main_window), drawing_area);
  
  return TRUE;
}

static gint button_release_event (GtkWidget *widget, GdkEventButton *event)
{
  int x, y;
  x = (int)event->x;
  y = widget->allocation.height - (int)event->y - 1;

  if (event->button == 1)
    project->OnLeftButtonUp(x, y);
  if (event->button == 3)
    project->OnRightButtonUp(x, y);

  return TRUE;
}

static gint motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
{
  int x, y;
  GdkModifierType state;

  if (event->is_hint)
    gdk_window_get_pointer (event->window, &x, &y, &state);
  else
  {
    x = (int)event->x;
    y = (int)event->y;
    state = (GdkModifierType)event->state;
  }

  y = widget->allocation.height - (int)event->y - 1;

  //  if (state)
  project->OnMouseMove(x, y);

  return TRUE;
}

static gint key_press_event(GtkWidget* widget, GdkEventKey* event, gpointer data)
{
  char code = 0;

  if (event->keyval < 0x100)
    code = gdk_keyval_to_upper(event->keyval);
  else
  {
    switch (event->keyval)
    {
    case GDK_Shift_L: case GDK_Shift_R: code = KEY_SHIFT; break;
    case GDK_Control_L: case GDK_Control_R: code = KEY_CONTROL; break;
    case GDK_Escape: code = KEY_ESCAPE; break;
    case GDK_Tab: code = KEY_TAB; break;
    case GDK_Insert: code = KEY_INSERT; break;
    case GDK_Delete: code = KEY_DELETE; break;
    case GDK_Up: code = KEY_UP; break;
    case GDK_Down: code = KEY_DOWN; break;
    case GDK_Left: code = KEY_LEFT; break;
    case GDK_Right: code = KEY_RIGHT; break;
    case GDK_Prior: code = KEY_PRIOR; break;
    case GDK_Next: code = KEY_NEXT; break;
    }
  }

  if (code != 0)
  {
    if (project->OnKeyDown(code, (event->state & GDK_CONTROL_MASK) != 0, 
			   (event->state & GDK_SHIFT_MASK) != 0))
	gtk_signal_emit_stop_by_name (GTK_OBJECT(widget), "key_press_event");
  }

  return TRUE;
}

static gint init(GtkWidget *widget)
{
  // OpenGL functions can be called only if make_current returns true
  if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
  {
    glViewport(0,0, widget->allocation.width, widget->allocation.height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,100, 100,0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  }
  
  return TRUE;
}

// When widget is exposed it's contents are redrawn.
static gint draw_view(GtkWidget *widget, GdkEventExpose *event)
{
  // Draw only last expose.
  if (event->count > 0)
    return TRUE;

  project->Render(false);
  // TODO: call SystemSwap from Render()
  gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));

  return TRUE;
}

// Save the new size of the window.
static gint reshape_view(GtkWidget *widget, GdkEventConfigure *event)
{
  project->SetViewSize(widget->allocation.width, widget->allocation.height);

  return TRUE;
}

static gint main_quit (GtkWidget *widget, GdkEvent* event, gpointer data)
{
  if (!project->SaveModified())
    return TRUE;

  gtk_main_quit ();
  return FALSE;
}

int main(int argc, char* argv[])
{
  GtkWidget *vbox, *hbox;

  int attrlist[] =
  {
    GLX_RGBA,
    GLX_DOUBLEBUFFER,
    GLX_DEPTH_SIZE, 16,
    0
  };

  gtk_set_locale ();
  gtk_init (&argc, &argv);

  // Check if OpenGL is supported.
  if (gdk_gl_query() == FALSE)
  {
    g_print("ERROR: OpenGL not supported\n");
    return 1;
  }

//  startup_message ("Loading user preferences ...");
  init_rc ();
  project = new Project();

  main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (main_window), "LeoCAD");
  gtk_container_border_width (GTK_CONTAINER (main_window), 0);
//  gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, FALSE);
  gtk_widget_realize (main_window);
// read preferences
  gtk_widget_set_usize (GTK_WIDGET(main_window), 400, 300);
  gdk_window_resize (main_window->window, 600, 400);
  gtk_window_set_default_size (GTK_WINDOW (main_window), 600, 400);
 
  gtk_signal_connect (GTK_OBJECT (main_window), "delete_event", (GtkSignalFunc) main_quit, NULL);
  gtk_signal_connect (GTK_OBJECT (main_window), "destroy", (GtkSignalFunc) gtk_main_quit, NULL);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (main_window), vbox);
  gtk_widget_show (vbox);

//  startup_message ("Creating Main Menu ...");
  create_main_menu(main_window, vbox);

//  startup_message ("Creating Toolbars ...");
  create_toolbars (main_window, vbox);

  hbox = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (vbox), hbox);
  gtk_widget_show (hbox);

  /* You should always delete gtk_gl_widget widgets before exit or else
     GLX contexts are left undeleted, this may cause problems (=core dump)
     in some systems.
     Destroy method of objects is not automatically called on exit.
     You need to manually enable this feature. Do gtk_quit_add_destroy()
     for all your top level windows unless you are certain that they get
     destroy signal by other means.
  */
  gtk_quit_add_destroy(1, GTK_OBJECT(main_window));

  // Create new OpenGL widget.
  drawing_area = GTK_WIDGET(gtk_gl_area_new(attrlist));

  GTK_WIDGET_SET_FLAGS(drawing_area, GTK_CAN_FOCUS);

  gtk_widget_set_events(GTK_WIDGET(drawing_area),
			GDK_EXPOSURE_MASK |
			GDK_KEY_PRESS_MASK |
			GDK_BUTTON_PRESS_MASK |
			GDK_BUTTON_RELEASE_MASK |
			GDK_POINTER_MOTION_MASK |
			GDK_POINTER_MOTION_HINT_MASK);

  // Connect signal handlers
  gtk_signal_connect(GTK_OBJECT(drawing_area), "expose_event", 
      GTK_SIGNAL_FUNC(draw_view), NULL);
  gtk_signal_connect(GTK_OBJECT(drawing_area), "configure_event",
      GTK_SIGNAL_FUNC(reshape_view), NULL);
  gtk_signal_connect(GTK_OBJECT(drawing_area), "realize",
      GTK_SIGNAL_FUNC(init), NULL);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
      GTK_SIGNAL_FUNC(motion_notify_event), NULL);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
      GTK_SIGNAL_FUNC(button_press_event), NULL);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "button_release_event",
      GTK_SIGNAL_FUNC(button_release_event), NULL);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
      GTK_SIGNAL_FUNC(key_press_event), NULL);
 
  /* set minimum size */
  gtk_widget_set_usize(GTK_WIDGET(drawing_area), 100,100);

  gtk_container_add(GTK_CONTAINER(hbox),GTK_WIDGET(drawing_area));
  gtk_widget_show(GTK_WIDGET(drawing_area));

  create_piecebar(main_window, hbox);
  create_statusbar(main_window, vbox);
//  gtk_box_pack_start (GTK_BOX (vbox), create_status_bar (), FALSE, TRUE, 2);
  //  GtkWidget* statusbar = gtk_statusbar_new ();
  //gtk_widget_show (statusbar);
  //gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, TRUE, 0);   

  gtk_widget_show(GTK_WIDGET(main_window));

  char* path;
  path = getenv("LEOCAD_LIB");
  if (path == NULL)
    path = default_path;

  if (project->Initialize(argc, argv, path) == false)
  {
    delete project;
    return 1;
  }

  gtk_main();

  delete project;
  return 0;
}