GTKMainWindow.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // Filename: GTKMainWindow.cpp
3 // Author: Florian Hecht
4 // Date: 2009
5 // ****************************************************************************
6 
7 
8 // ****************************************************************************
9 // Includes
10 // ****************************************************************************
11 
12 #include "GTKMainWindow.h"
13 #include "GTKMainWindowWidget.h"
14 #include "GTKApplicationHandler.h"
16 
17 #include "Image/ByteImage.h"
18 #include "Image/ImageProcessor.h"
19 
20 #ifdef USE_OPENGL
21 #include "GTKGLContext.h"
22 #endif
23 
24 #include <string.h>
25 
26 
27 
28 static unsigned int last_modifier_key_state = 0;
29 
30 static void button_pressed_callback(GtkWidget *widget, gpointer data)
31 {
33  if (ptr)
34  ptr->Clicked();
35 }
36 
37 static void checkbox_toggled_callback(GtkWidget *widget, gpointer data)
38 {
39  int state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
41  if (ptr)
42  ptr->Toggled(state > 0);
43 }
44 
45 static void slider_changed_callback(GtkWidget *widget, gpointer data)
46 {
48 
49  if (ptr)
50  {
51  int value = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(ptr->m_obj));
52  ptr->ValueChanged(value);
53  }
54 }
55 
56 static void combo_box_changed_callback(GtkWidget *widget, gpointer data)
57 {
59 
60  if (ptr)
61  {
62  int value = gtk_combo_box_get_active(GTK_COMBO_BOX(ptr->m_widget));
63  ptr->ValueChanged(value);
64  }
65 }
66 
67 static void text_edit_changed_callback(GtkWidget *widget, gpointer data)
68 {
70 
71  if (ptr)
72  {
73  ptr->TextChanged(NULL);
74  }
75 }
76 
77 static gboolean ivtimage_configure_event(GtkWidget *widget, GdkEventConfigure *event)
78 {
79  IVTImage *ptr = (IVTImage *)widget;
80 
81  if (ptr->pixmap)
82  g_object_unref(ptr->pixmap);
83 
84  ptr->pixmap = gdk_pixmap_new(widget->window,
85  widget->allocation.width,
86  widget->allocation.height,
87  -1);
88  gdk_draw_rectangle(ptr->pixmap,
89  widget->style->black_gc,
90  TRUE,
91  0, 0,
92  widget->allocation.width,
93  widget->allocation.height);
94 
95  return TRUE;
96 }
97 
98 static gboolean ivtimage_expose_event(GtkWidget *widget, GdkEventExpose *event)
99 {
100  IVTImage *ptr = (IVTImage *)widget;
101 
102  GdkPixmap *pixmap = ptr->pixmap;
103 
104  if (ptr->image == 0)
105  {
106  gdk_draw_drawable(widget->window,
107  widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
108  pixmap,
109  event->area.x, event->area.y,
110  event->area.x, event->area.y,
111  event->area.width, event->area.height);
112  }
113  else
114  {
115  gdk_draw_rgb_image(widget->window,
116  widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
117  0, 0, ptr->image->width, ptr->image->height,
118  GDK_RGB_DITHER_NONE, ptr->image->pixels, ptr->image->width*3);
119  }
120 
121  if (ptr->mouse_down == 1)
122  {
123  int x0 = ptr->mouse_start_x;
124  int y0 = ptr->mouse_start_y;
125  int x1 = ptr->mouse_x;
126  int y1 = ptr->mouse_y;
127 
128  if (x0 > x1)
129  {
130  int tmp = x0;
131  x0 = x1;
132  x1 = tmp;
133  }
134  if (y0 > y1)
135  {
136  int tmp = y0;
137  y0 = y1;
138  y1 = tmp;
139  }
140 
141  int dx = x1 - x0;
142  int dy = y1 - y0;
143 
144  GdkGC *gc = widget->style->white_gc;
145  gdk_gc_set_function(gc, GDK_XOR);
146  gdk_draw_rectangle(widget->window,
147  gc,
148  FALSE,
149  x0, y0,
150  dx, dy);
151  gdk_gc_set_function(gc, GDK_COPY);
152  }
153 
154  return FALSE;
155 }
156 
157 static gboolean ivtimage_button_press_event(GtkWidget *widget, GdkEventButton *event)
158 {
159  IVTImage *ptr = (IVTImage *)widget;
160 
161  guint modifiers = gtk_accelerator_get_default_mod_mask();
162  last_modifier_key_state = event->state & modifiers;
163 
164  gtk_widget_grab_focus(widget);
165 
167  if (callback)
168  {
169  int btn = 0;
170  if (event->button == 1) btn = IVT_LEFT_BUTTON;
171  if (event->button == 3) btn = IVT_RIGHT_BUTTON;
172  if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
173 
174  callback->MouseDown((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
175  }
176 
177  if (event->button == 1)
178  {
179  ptr->mouse_down = 1;
180  ptr->mouse_start_x = event->x;
181  ptr->mouse_start_y = event->y;
182  ptr->mouse_x = event->x;
183  ptr->mouse_y = event->y;
184  }
185 
186  return TRUE;
187 }
188 
189 static gboolean ivtimage_button_release_event(GtkWidget *widget, GdkEventButton *event)
190 {
191  IVTImage *ptr = (IVTImage *)widget;
192 
193  guint modifiers = gtk_accelerator_get_default_mod_mask();
194  last_modifier_key_state = event->state & modifiers;
195 
197  if (callback)
198  {
199  int btn = 0;
200  if (event->button == 1) btn = IVT_LEFT_BUTTON;
201  if (event->button == 3) btn = IVT_RIGHT_BUTTON;
202  if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
203 
204  callback->MouseUp((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
205  }
206 
207  if (ptr->mouse_down == 1 && event->button == 1)
208  {
209  ptr->mouse_down = 0;
210 
211  int x0 = ptr->mouse_start_x;
212  int y0 = ptr->mouse_start_y;
213  int x1 = ptr->mouse_x;
214  int y1 = ptr->mouse_y;
215 
216  if (x0 > x1)
217  {
218  int tmp = x0;
219  x0 = x1;
220  x1 = tmp;
221  }
222  if (y0 > y1)
223  {
224  int tmp = y0;
225  y0 = y1;
226  y1 = tmp;
227  }
228 
229  int dx = x1 - x0;
230  int dy = y1 - y0;
231 
233  if (callback)
234  {
235  if (dx < 5 && dy < 5)
236  {
237  callback->PointClicked((WIDGET_HANDLE)ptr->main_window_widget, (x0 + x1) / 2, (y0 + y1) / 2);
238  }
239  else
240  {
241  callback->RectSelected((WIDGET_HANDLE)ptr->main_window_widget, x0, y0, x1, y1);
242  }
243  }
244 
245  gtk_widget_queue_draw_area(widget, x0, y0, dx, dy);
246 
247  }
248 
249  return TRUE;
250 }
251 
252 static gboolean ivtimage_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
253 {
254  IVTImage *ptr = (IVTImage *)widget;
255 
256  guint modifiers = gtk_accelerator_get_default_mod_mask();
257  last_modifier_key_state = event->state & modifiers;
258 
260  if (callback)
261  {
262  int x, y;
263  GdkModifierType state;
264 
265  if (event->is_hint)
266  gdk_window_get_pointer(event->window, &x, &y, &state);
267  else
268  {
269  x = (int)event->x;
270  y = (int)event->y;
271  state = (GdkModifierType)event->state;
272  }
273 
274  callback->MouseMove((WIDGET_HANDLE)ptr->main_window_widget, x, y);
275  }
276 
277  if (ptr->mouse_down != 1)
278  return FALSE;
279 
280  int x, y;
281  GdkModifierType state;
282 
283  if (event->is_hint)
284  gdk_window_get_pointer(event->window, &x, &y, &state);
285  else
286  {
287  x = event->x;
288  y = event->y;
289  state = (GdkModifierType)event->state;
290  }
291 
292  if (state & GDK_BUTTON1_MASK)
293  {
294  ptr->mouse_x = x;
295  ptr->mouse_y = y;
296 
297  gtk_widget_queue_draw_area(widget, 0, 0, ptr->image->width, ptr->image->height);
298  }
299 
300  return TRUE;
301 }
302 
303 static gboolean ivtimage_key_press_event(GtkWidget *widget, GdkEventKey *event)
304 {
305  IVTImage *ptr = (IVTImage *)widget;
306 
307  guint modifiers = gtk_accelerator_get_default_mod_mask();
308  last_modifier_key_state = event->state & modifiers;
309 
311  if (callback)
312  {
313  if (event->length != 1)
314  {
315  callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, -1);
316  }
317  else
318  {
319  callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
320  }
321  }
322 
323  return TRUE;
324 }
325 
326 static gboolean ivtimage_key_release_event(GtkWidget *widget, GdkEventKey *event)
327 {
328  IVTImage *ptr = (IVTImage *)widget;
329 
330  guint modifiers = gtk_accelerator_get_default_mod_mask();
331  last_modifier_key_state = event->state & modifiers;
332 
334  if (callback)
335  {
336  if (event->length != 1)
337  {
338  callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, -1);
339  }
340  else
341  {
342  callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
343  }
344  }
345 
346  return TRUE;
347 }
348 
349 
350 #ifdef USE_OPENGL
351 
352 
353 static gboolean ivtglwidget_configure_event(GtkWidget *widget, GdkEventConfigure *event)
354 {
355  IVTGLWidget *ptr = (IVTGLWidget *)widget;
356 
357  if (ptr->glcontext)
358  {
359  // the first time the widget becomes visible we make it the current gl context
360  ptr->glcontext->MakeCurrent(widget->window);
361  }
362 
363  return TRUE;
364 }
365 
366 static gboolean ivtglwidget_button_press_event(GtkWidget *widget, GdkEventButton *event)
367 {
368  IVTGLWidget *ptr = (IVTGLWidget *)widget;
369 
370  guint modifiers = gtk_accelerator_get_default_mod_mask();
371  last_modifier_key_state = event->state & modifiers;
372 
373  gtk_widget_grab_focus(widget);
374 
375  CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
376  if (callback)
377  {
378  int btn = 0;
379  if (event->button == 1) btn = IVT_LEFT_BUTTON;
380  if (event->button == 3) btn = IVT_RIGHT_BUTTON;
381  if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
382 
383  callback->MouseDown((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
384  }
385 
386  return TRUE;
387 }
388 
389 static gboolean ivtglwidget_button_release_event(GtkWidget *widget, GdkEventButton *event)
390 {
391  IVTGLWidget *ptr = (IVTGLWidget *)widget;
392 
393  guint modifiers = gtk_accelerator_get_default_mod_mask();
394  last_modifier_key_state = event->state & modifiers;
395 
396  CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
397  if (callback)
398  {
399  int btn = 0;
400  if (event->button == 1) btn = IVT_LEFT_BUTTON;
401  if (event->button == 3) btn = IVT_RIGHT_BUTTON;
402  if (event->button == 2) btn = IVT_MIDDLE_BUTTON;
403 
404  callback->MouseUp((WIDGET_HANDLE)ptr->main_window_widget, btn, (int)event->x, (int)event->y);
405  }
406 
407  return TRUE;
408 }
409 
410 static gboolean ivtglwidget_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
411 {
412  IVTGLWidget *ptr = (IVTGLWidget *)widget;
413 
414  guint modifiers = gtk_accelerator_get_default_mod_mask();
415  last_modifier_key_state = event->state & modifiers;
416 
417  CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
418  if (callback)
419  {
420  int x, y;
421  GdkModifierType state;
422 
423  if (event->is_hint)
424  gdk_window_get_pointer(event->window, &x, &y, &state);
425  else
426  {
427  x = (int)event->x;
428  y = (int)event->y;
429  state = (GdkModifierType)event->state;
430  }
431 
432  callback->MouseMove((WIDGET_HANDLE)ptr->main_window_widget, x, y);
433  }
434 
435  return TRUE;
436 }
437 
438 static gboolean ivtglwidget_key_press_event(GtkWidget *widget, GdkEventKey *event)
439 {
440  IVTGLWidget *ptr = (IVTGLWidget *)widget;
441 
442  guint modifiers = gtk_accelerator_get_default_mod_mask();
443  last_modifier_key_state = event->state & modifiers;
444 
445  CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
446  if (callback)
447  {
448  if (event->length != 1)
449  {
450  callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, -1);
451  }
452  else
453  {
454  callback->KeyDown((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
455  }
456  }
457 
458  return TRUE;
459 }
460 
461 static gboolean ivtglwidget_key_release_event(GtkWidget *widget, GdkEventKey *event)
462 {
463  IVTGLWidget *ptr = (IVTGLWidget *)widget;
464 
465  guint modifiers = gtk_accelerator_get_default_mod_mask();
466  last_modifier_key_state = event->state & modifiers;
467 
468  CMainWindowEventInterface *callback = ptr->main_window_widget->m_main_window->GetEventCallback();
469  if (callback)
470  {
471  if (event->length != 1)
472  {
473  callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, -1);
474  }
475  else
476  {
477  callback->KeyUp((WIDGET_HANDLE)ptr->main_window_widget, event->string[0]);
478  }
479  }
480 
481  return TRUE;
482 }
483 
484 #endif /* USE_OPENGL */
485 
486 
487 static int destroy_window_callback(GtkWidget *widget, gpointer data)
488 {
489  // process internal things (reference counter, exit flag)
490  CGTKMainWindow *pMainWindow = (CGTKMainWindow *) data;
491  pMainWindow->Destroy(widget);
492 
493  // destroy widget
494  gtk_widget_destroy(widget);
495 
496  return 0;
497 }
498 
499 
501 
502 void CGTKMainWindow::Destroy(GtkWidget *widget)
503 {
504  // set flag, so that the destructor knows it doesn't have to destroy m_main_window again
505  if (widget == m_main_window)
506  m_bDestroyed = true;
507 
508  if (--m_ref_count == 0)
509  {
511  if (pApplicationHandler)
512  pApplicationHandler->SetExit(true);
513  }
514 }
515 
516 
517 CGTKMainWindow::CGTKMainWindow(int x, int y, int width, int height, const char *title)
518 {
519  m_main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
520  gtk_window_set_title(GTK_WINDOW(m_main_window), title);
521  gtk_window_set_default_size(GTK_WINDOW(m_main_window), width, height);
522  gtk_window_set_position(GTK_WINDOW(m_main_window), GTK_WIN_POS_NONE);
523  gtk_window_move(GTK_WINDOW(m_main_window), x, y);
524 
525  g_signal_connect(G_OBJECT(m_main_window), "destroy", G_CALLBACK(destroy_window_callback), this);
526 
527  m_fixed_container = gtk_fixed_new();
528  gtk_container_add(GTK_CONTAINER(m_main_window), m_fixed_container);
529  gtk_widget_show(m_fixed_container);
530 
531  m_event_callback = NULL;
532  m_ref_count++;
533 
534  m_bDestroyed = false;
535 }
536 
538 {
539  // delete widgets in inverse order
540  const int nWidgets = m_widgets.size();
541  for (int i = nWidgets - 1; i >= 0; i--)
542  delete m_widgets[i];
543 
544  // clear list
545  m_widgets.clear();
546 
547  // check if m_main_window has already been destroyed (by closing the window)
548  if (!m_bDestroyed)
549  {
550  // m_main_window has not been destroyed yet, destroy it now
551  destroy_window_callback(m_main_window, this); // has same effect
552 
553  // process pending events (otherwise window the won't close)
554  while (gtk_events_pending())
555  gtk_main_iteration();
556  }
557 }
558 
559 // create widgets
561 {
563 
564  GtkWidget *image;
565  image = ivtimage_new();
566 
567  gtk_signal_connect(GTK_OBJECT(image), "expose_event", (GtkSignalFunc)ivtimage_expose_event, NULL);
568  gtk_signal_connect(GTK_OBJECT(image), "configure_event", (GtkSignalFunc)ivtimage_configure_event, NULL);
569  gtk_signal_connect(GTK_OBJECT(image), "motion_notify_event", (GtkSignalFunc)ivtimage_motion_notify_event, NULL);
570  gtk_signal_connect(GTK_OBJECT(image), "button_press_event", (GtkSignalFunc)ivtimage_button_press_event, NULL);
571  gtk_signal_connect(GTK_OBJECT(image), "button_release_event", (GtkSignalFunc)ivtimage_button_release_event, NULL);
572  gtk_signal_connect(GTK_OBJECT(image), "key_press_event", (GtkSignalFunc)ivtimage_key_press_event, NULL);
573  gtk_signal_connect(GTK_OBJECT(image), "key_release_event", (GtkSignalFunc)ivtimage_key_release_event, NULL);
574 
575  gtk_widget_set_events(image, GDK_EXPOSURE_MASK
576  | GDK_LEAVE_NOTIFY_MASK
577  | GDK_BUTTON_PRESS_MASK
578  | GDK_BUTTON_RELEASE_MASK
579  | GDK_POINTER_MOTION_MASK
580  | GDK_POINTER_MOTION_HINT_MASK
581  | GDK_KEY_PRESS_MASK
582  | GDK_KEY_RELEASE_MASK);
583 
584 
585  gtk_fixed_put(GTK_FIXED(m_fixed_container), image, x, y);
586  gtk_drawing_area_size(GTK_DRAWING_AREA(image), width, height);
587 
588  ((IVTImage*)image)->image = new CByteImage(width, height, CByteImage::eRGB24);
589  ((IVTImage*)image)->main_window_widget = w;
590 
591  gtk_widget_show(image);
592 
593  w->m_widget = image;
594 
595  m_widgets.push_back(w);
596 
597  return (WIDGET_HANDLE)w;
598 }
599 
600 WIDGET_HANDLE CGTKMainWindow::AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
601 {
603 
604  GtkWidget *button;
605  button = gtk_button_new_with_label(text);
606  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_pressed_callback), w);
607  gtk_fixed_put(GTK_FIXED(m_fixed_container), button, x, y);
608  gtk_widget_set_size_request(button, width, height);
609  gtk_widget_show(button);
610 
611  w->m_widget = button;
612 
613  m_widgets.push_back(w);
614 
615  return (WIDGET_HANDLE)w;
616 }
617 
618 WIDGET_HANDLE CGTKMainWindow::AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
619 {
621 
622  GtkWidget *label;
623  label = gtk_label_new(text);
624  gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.5f);
625  gtk_fixed_put(GTK_FIXED(m_fixed_container), label, x, y);
626  gtk_widget_set_size_request(label, width, height);
627  gtk_widget_show(label);
628 
629  w->m_widget = label;
630 
631  m_widgets.push_back(w);
632 
633  return (WIDGET_HANDLE)w;
634 }
635 
636 WIDGET_HANDLE CGTKMainWindow::AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent)
637 {
639 
640  GtkWidget *check_button;
641  check_button = gtk_check_button_new_with_label(text);
642  g_signal_connect(G_OBJECT(check_button), "toggled", G_CALLBACK(checkbox_toggled_callback), w);
643  gtk_fixed_put(GTK_FIXED(m_fixed_container), check_button, x, y);
644  gtk_widget_set_size_request(check_button, width, height);
645  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_button), checked);
646  gtk_widget_show(check_button);
647 
648  w->m_widget = check_button;
649 
650  m_widgets.push_back(w);
651 
652  return (WIDGET_HANDLE)w;
653 }
654 
655 WIDGET_HANDLE CGTKMainWindow::AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent)
656 {
658 
659  GtkWidget *edit;
660  edit = gtk_entry_new();
661 
662  gtk_entry_set_text(GTK_ENTRY(edit), text);
663 
664  g_signal_connect(G_OBJECT(edit), "changed", G_CALLBACK(text_edit_changed_callback), w);
665 
666  gtk_fixed_put(GTK_FIXED(m_fixed_container), edit, x, y);
667  gtk_widget_set_size_request(edit, width, height);
668  gtk_widget_show(edit);
669 
670  w->m_widget = edit;
671 
672  m_widgets.push_back(w);
673 
674  return (WIDGET_HANDLE)w;
675 }
676 
677 WIDGET_HANDLE CGTKMainWindow::AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent)
678 {
680 
681  GtkObject *adjustment;
682  adjustment = gtk_adjustment_new(value, min_value, max_value, 1.0, step, 1.0);
683  g_signal_connect(G_OBJECT(adjustment), "value_changed", G_CALLBACK(slider_changed_callback), w);
684 
685  GtkWidget *slider;
686  slider = gtk_hscale_new(GTK_ADJUSTMENT(adjustment));
687  gtk_scale_set_draw_value(GTK_SCALE(slider), FALSE);
688 
689  gtk_fixed_put(GTK_FIXED(m_fixed_container), slider, x, y);
690  gtk_widget_set_size_request(slider, width, height);
691  gtk_widget_show(slider);
692 
693  w->m_widget = slider;
694  w->m_obj = adjustment;
695 
696  m_widgets.push_back(w);
697 
698  return (WIDGET_HANDLE)w;
699 }
700 
701 WIDGET_HANDLE CGTKMainWindow::AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent)
702 {
704 
705  GtkWidget *cbox;
706  cbox = gtk_combo_box_new_text();
707 
708  for (int i = 0; i < num_entries; i++)
709  {
710  gtk_combo_box_append_text(GTK_COMBO_BOX(cbox), entries[i]);
711  }
712 
713  gtk_combo_box_set_active(GTK_COMBO_BOX(cbox), current_entry);
714 
715  g_signal_connect(G_OBJECT(cbox), "changed", G_CALLBACK(combo_box_changed_callback), w);
716 
717  gtk_fixed_put(GTK_FIXED(m_fixed_container), cbox, x, y);
718  gtk_widget_set_size_request(cbox, width, height);
719  gtk_widget_show(cbox);
720 
721  w->m_widget = cbox;
722 
723  m_widgets.push_back(w);
724 
725  return (WIDGET_HANDLE)w;
726 }
727 
729 {
730 #ifdef USE_OPENGL
732 
733  CGTKGLContext *glcontext = new CGTKGLContext();
734  if (!glcontext->IsInitialized())
735  {
736  printf("CGTKMainWindow::AddGLWidget: creation of GLWidget failed\n");
737  return NULL;
738  }
739 
740  // get the visual for the OpenGL context
741  GdkVisual *visual = glcontext->GetVisual();
742 
743  // use a colormap and the visual that is used for the OpenGL context
744  gtk_widget_push_colormap(gdk_colormap_new(visual, TRUE));
745  gtk_widget_push_visual(visual);
746 
747  GtkWidget *glw;
748  glw = ivtglwidget_new();
749 
750  // pop back the default visual settings
751  gtk_widget_pop_visual();
752  gtk_widget_pop_colormap();
753 
754  gtk_signal_connect(GTK_OBJECT(glw), "configure_event", (GtkSignalFunc)ivtglwidget_configure_event, NULL);
755  gtk_signal_connect(GTK_OBJECT(glw), "motion_notify_event", (GtkSignalFunc)ivtglwidget_motion_notify_event, NULL);
756  gtk_signal_connect(GTK_OBJECT(glw), "button_press_event", (GtkSignalFunc)ivtglwidget_button_press_event, NULL);
757  gtk_signal_connect(GTK_OBJECT(glw), "button_release_event", (GtkSignalFunc)ivtglwidget_button_release_event, NULL);
758  gtk_signal_connect(GTK_OBJECT(glw), "key_press_event", (GtkSignalFunc)ivtglwidget_key_press_event, NULL);
759  gtk_signal_connect(GTK_OBJECT(glw), "key_release_event", (GtkSignalFunc)ivtglwidget_key_release_event, NULL);
760 
761  gtk_widget_set_events(glw, GDK_LEAVE_NOTIFY_MASK
762  | GDK_BUTTON_PRESS_MASK
763  | GDK_BUTTON_RELEASE_MASK
764  | GDK_POINTER_MOTION_MASK
765  | GDK_POINTER_MOTION_HINT_MASK
766  | GDK_KEY_PRESS_MASK
767  | GDK_KEY_RELEASE_MASK);
768 
769 
770  gtk_fixed_put(GTK_FIXED(m_fixed_container), glw, x, y);
771  gtk_drawing_area_size(GTK_DRAWING_AREA(glw), width, height);
772 
773  ((IVTGLWidget*)glw)->main_window_widget = w;
774  ((IVTGLWidget*)glw)->glcontext = glcontext;
775 
776  gtk_widget_show(glw);
777 
778  // this forces the window to be created, so we can call MakeCurrent
779  gtk_widget_realize(glw);
780  ((IVTGLWidget*)glw)->glcontext->MakeCurrent(glw->window);
781 
782  w->m_widget = glw;
783 
784  m_widgets.push_back(w);
785 
786  return (WIDGET_HANDLE)w;
787 #else
788  printf("WARNING: CGTKMainWindow::AddGLWidget has been called, but USE_OPENGL = 0 in Makefile.base\n");
789  return (WIDGET_HANDLE)NULL;
790 
791 #endif
792 }
793 
794 
795 // access to widget attributes
796 bool CGTKMainWindow::GetText(WIDGET_HANDLE widget, char *text, int len)
797 {
799 
800  if (!w)
801  return false;
802 
803  if (w->m_type == eLabel)
804  {
805  const char *ptr = gtk_label_get_text(GTK_LABEL(w->m_widget));
806  strncpy(text, ptr, len);
807 
808  return true;
809  }
810  else if (w->m_type == eButton || w->m_type == eCheckBox)
811  {
812  const char *ptr = gtk_button_get_label(GTK_BUTTON(w->m_widget));
813  strncpy(text, ptr, len);
814 
815  return true;
816  }
817  else if (w->m_type == eTextEdit)
818  {
819  const char *ptr = gtk_entry_get_text(GTK_ENTRY(w->m_widget));
820  strncpy(text, ptr, len);
821 
822  return true;
823  }
824 
825  return false;
826 }
827 bool CGTKMainWindow::SetText(WIDGET_HANDLE widget, const char *text)
828 {
830 
831  if (!w)
832  return false;
833 
834  if (w->m_type == eLabel)
835  {
836  gtk_label_set_text(GTK_LABEL(w->m_widget), text);
837 
838  return true;
839  }
840  else if (w->m_type == eButton || w->m_type == eCheckBox)
841  {
842  gtk_button_set_label(GTK_BUTTON(w->m_widget), text);
843 
844  return true;
845  }
846  else if (w->m_type == eTextEdit)
847  {
848  gtk_entry_set_text(GTK_ENTRY(w->m_widget), text);
849 
850  return true;
851  }
852 
853  return false;
854 }
855 
857 {
859 
860  if (!w)
861  return false;
862 
863  if (w->m_type == eImage)
864  {
865  CByteImage *bi = ((IVTImage*)w->m_widget)->image;
866 
867  if (bi->width != pImage->width || bi->height != pImage->height)
868  {
869  printf("error: SetImage: image size does not match size of image widget\n");
870  return false;
871  }
872 
873  if (bi->type != pImage->type)
874  {
875  ImageProcessor::ConvertImage(pImage, bi);
876  }
877  else
878  {
879  ImageProcessor::CopyImage(pImage, bi);
880  }
881 
882  // init redraw
883  gtk_widget_queue_draw_area(w->m_widget, 0, 0, bi->width, bi->height);
884 
885  }
886 
887  return false;
888 }
889 
891 {
893 
894  if (!w)
895  return false;
896 
897  if (w->m_type == eCheckBox)
898  {
899  value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w->m_widget));
900  return true;
901  }
902  else if (w->m_type == eSlider)
903  {
904  value = (int)gtk_adjustment_get_value(GTK_ADJUSTMENT(w->m_obj));
905  return true;
906  }
907  else if (w->m_type == eComboBox)
908  {
909  value = gtk_combo_box_get_active(GTK_COMBO_BOX(w->m_widget));
910  return true;
911  }
912 
913  return false;
914 }
915 
917 {
919 
920  if (!w)
921  return false;
922 
923  if (w->m_type == eCheckBox)
924  {
925  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w->m_widget), value);
926  return true;
927  }
928  else if (w->m_type == eSlider)
929  {
930  gtk_adjustment_set_value(GTK_ADJUSTMENT(w->m_obj), (double)value);
931  return true;
932  }
933  else if (w->m_type == eComboBox)
934  {
935  gtk_combo_box_set_active(GTK_COMBO_BOX(w->m_widget), value);
936  return true;
937  }
938 
939  return false;
940 }
941 
943 {
944 #ifdef USE_OPENGL
946 
947  if (!w)
948  return false;
949 
950  if (w->m_type == eGLWidget)
951  {
952  IVTGLWidget* glw = (IVTGLWidget*)w->m_widget;
953 
954  if (glw->glcontext)
955  glw->glcontext->SwapBuffers(GTK_WIDGET(glw)->window);
956 
957  return true;
958  }
959 #endif
960 
961  return false;
962 }
963 
965 {
966 #ifdef USE_OPENGL
968 
969  if (!w)
970  return false;
971 
972  if (w->m_type == eGLWidget)
973  {
974  IVTGLWidget* glw = (IVTGLWidget*)w->m_widget;
975 
976  if (glw->glcontext)
977  glw->glcontext->MakeCurrent(GTK_WIDGET(glw)->window);
978 
979  return true;
980  }
981 #endif
982 
983  return false;
984 }
985 
986 
987 
988 // window control
990 {
991  if (widget == NULL)
992  {
993  gtk_widget_show(m_main_window);
994  }
995  else
996  {
998  gtk_widget_show(w->m_widget);
999  }
1000 }
1002 {
1003  if (widget == NULL)
1004  {
1005  gtk_widget_hide(m_main_window);
1006  }
1007  else
1008  {
1010  gtk_widget_hide(w->m_widget);
1011  }
1012 }
1013 
1014 
1016 {
1017  int state = 0;
1018 
1019  if (last_modifier_key_state & GDK_SHIFT_MASK) state |= IVT_SHIFT_KEY;
1020  if (last_modifier_key_state & GDK_CONTROL_MASK) state |= IVT_CONTROL_KEY;
1021  if (last_modifier_key_state & GDK_MOD1_MASK) state |= IVT_ALT_KEY;
1022 
1023  return state;
1024 }
GtkWidget * m_fixed_container
Definition: GTKMainWindow.h:81
virtual void KeyUp(WIDGET_HANDLE widget, int key)
static gboolean ivtimage_key_press_event(GtkWidget *widget, GdkEventKey *event)
void Destroy(GtkWidget *widget)
WIDGET_HANDLE AddLabel(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
static CGTKApplicationHandler * GetApplicationHandler()
static void checkbox_toggled_callback(GtkWidget *widget, gpointer data)
virtual void KeyDown(WIDGET_HANDLE widget, int key)
GtkWidget * m_main_window
Definition: GTKMainWindow.h:80
CMainWindowEventInterface * m_event_callback
Definition: GTKMainWindow.h:76
#define IVT_SHIFT_KEY
CByteImage * image
int width
The width of the image in pixels.
Definition: ByteImage.h:257
static unsigned int last_modifier_key_state
CGTKMainWindow(int x, int y, int width, int height, const char *title)
bool SetImage(WIDGET_HANDLE widget, const CByteImage *pImage)
virtual ~CGTKMainWindow()
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3131
WIDGET_HANDLE AddImage(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddComboBox(int x, int y, int width, int height, int num_entries, const char **entries, int current_entry, WIDGET_HANDLE parent=0)
static gboolean ivtimage_key_release_event(GtkWidget *widget, GdkEventKey *event)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
#define IVT_LEFT_BUTTON
_GdkDrawable GdkPixmap
Definition: GTKGLContext.h:27
GLenum GLsizei len
Definition: glext.h:3940
virtual void MouseDown(WIDGET_HANDLE widget, int button, int x, int y)
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
bool ConvertImage(const CByteImage *pInputImage, CByteImage *pOutputImage, bool bFast=false, const MyRegion *pROI=0)
Converts a grayscale CByteImage to an RGB CByteImage image and vice versa.
static void slider_changed_callback(GtkWidget *widget, gpointer data)
#define IVT_CONTROL_KEY
static int destroy_window_callback(GtkWidget *widget, gpointer data)
WIDGET_HANDLE AddTextEdit(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
static void combo_box_changed_callback(GtkWidget *widget, gpointer data)
GdkPixmap * pixmap
WIDGET_HANDLE AddSlider(int x, int y, int width, int height, int min_value, int max_value, int step, int value, WIDGET_HANDLE parent=0)
GLenum GLint x
Definition: glext.h:3125
static gboolean ivtimage_expose_event(GtkWidget *widget, GdkEventExpose *event)
void * WIDGET_HANDLE
void ValueChanged(int value)
GtkWidget * ivtimage_new()
GLsizei const GLfloat * value
Definition: glext.h:3538
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3129
virtual void MouseMove(WIDGET_HANDLE widget, int x, int y)
bool CopyImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bUseSameSize=false)
Copies one CByteImage to another.
bool SetText(WIDGET_HANDLE widget, const char *text)
virtual void PointClicked(WIDGET_HANDLE widget, int x, int y)
_GdkVisual GdkVisual
Definition: GTKGLContext.h:31
bool GetText(WIDGET_HANDLE widget, char *text, int len)
#define IVT_RIGHT_BUTTON
int height
The height of the image in pixels.
Definition: ByteImage.h:264
static gboolean ivtimage_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
CGTKMainWindowWidget * main_window_widget
bool GetValue(WIDGET_HANDLE widget, int &value)
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
static gboolean ivtimage_configure_event(GtkWidget *widget, GdkEventConfigure *event)
#define IVT_MIDDLE_BUTTON
GdkVisual * GetVisual()
Definition: GTKGLContext.h:52
ImageType type
The type of the image.
Definition: ByteImage.h:292
CMainWindowEventInterface * GetEventCallback()
Definition: GTKMainWindow.h:70
void TextChanged(const char *str)
void Hide(WIDGET_HANDLE widget=0)
GLenum GLint GLint y
Definition: glext.h:3125
static gboolean ivtimage_button_release_event(GtkWidget *widget, GdkEventButton *event)
static gboolean ivtimage_button_press_event(GtkWidget *widget, GdkEventButton *event)
bool SetValue(WIDGET_HANDLE widget, int value)
bool SwapBuffersGLWidget(WIDGET_HANDLE widget)
static void text_edit_changed_callback(GtkWidget *widget, gpointer data)
bool IsInitialized()
bool MakeCurrentGLWidget(WIDGET_HANDLE widget)
void Show(WIDGET_HANDLE widget=0)
CGTKMainWindow * m_main_window
Interface for the event mechanism of GUIs using the GUI toolkit of the IVT.
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
virtual void RectSelected(WIDGET_HANDLE widget, int x0, int y0, int x1, int y1)
static void callback(IplImage *pIplImage)
WIDGET_HANDLE AddGLWidget(int x, int y, int width, int height, WIDGET_HANDLE parent=0)
static void button_pressed_callback(GtkWidget *widget, gpointer data)
#define IVT_ALT_KEY
std::vector< CGTKMainWindowWidget * > m_widgets
Definition: GTKMainWindow.h:78
virtual void MouseUp(WIDGET_HANDLE widget, int button, int x, int y)
WIDGET_HANDLE AddButton(int x, int y, int width, int height, const char *text, WIDGET_HANDLE parent=0)
WIDGET_HANDLE AddCheckBox(int x, int y, int width, int height, const char *text, bool checked, WIDGET_HANDLE parent=0)
static int m_ref_count
Definition: GTKMainWindow.h:83


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28