gvplugin_xgtk.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <gtk/gtk.h>
33 
34 #include <graphviz/gvc.h>
35 #include <graphviz/gvplugin.h>
36 #include <graphviz/gvplugin_device.h>
37 #include <graphviz/types.h>
38 
39 #include <inttypes.h>
40 
41 #include <cairo.h>
42 
43 #include <iostream>
44 
45 #include <X11/Xlib.h>
46 #include <X11/Xutil.h>
47 
48 #include <signal.h>
49 #include <unistd.h>
50 #include <sys/time.h>
51 #include <stdio.h>
52 #include <string.h>
53 
54 #include "callbacks.h"
55 
63 #define GLADE_HOOKUP_OBJECT(component,widget,name) \
64  g_object_set_data_full (G_OBJECT (component), name, \
65  gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
66 
73 #define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
74  g_object_set_data (G_OBJECT (component), name, widget)
75 
86 GtkWidget*lookup_widget(GtkWidget*widget,const gchar*widget_name)
87 {
88  GtkWidget *parent, *found_widget;
89 
90  for (;;)
91  {
92  if (GTK_IS_MENU (widget))
93  parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
94  else
95  parent = widget->parent;
96 
97  if (!parent)
98  parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
99 
100  if (parent == NULL)
101  break;
102 
103  widget = parent;
104  }
105 
106  found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),widget_name);
107 
108  if (!found_widget)
109  g_warning ("Widget not found: %s", widget_name);
110 
111  return found_widget;
112 }
113 
120 GtkWidget*create_window(void)
121 {
122  GtkWidget *window;
123  GtkWidget *drawingarea;
124 
125  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
126  gtk_window_set_title (GTK_WINDOW (window), "Tree Graph");
127 
128  drawingarea = gtk_drawing_area_new ();
129  gtk_widget_show (drawingarea);
130  gtk_widget_set_size_request (drawingarea, 800, 800);
131  gtk_container_add (GTK_CONTAINER (window), drawingarea);
132 
133  g_signal_connect ((gpointer) window, "delete_event",G_CALLBACK (WindowDeleteEvent),NULL);
134  g_signal_connect ((gpointer) drawingarea, "expose_event",G_CALLBACK (DrawingareaExposeEvent),NULL);
135  g_signal_connect ((gpointer) drawingarea, "motion_notify_event",G_CALLBACK (DrawingareaMotionNotifyEvent),NULL);
136  g_signal_connect ((gpointer) drawingarea, "configure_event",G_CALLBACK (DrawingareaConfigureEvent),NULL);
137  g_signal_connect ((gpointer) drawingarea, "button-press-event",G_CALLBACK (DrawingareaButtonPressEvent),NULL);
138  g_signal_connect ((gpointer) drawingarea, "button-release-event",G_CALLBACK (DrawingareaButtonPressEvent),NULL);
139  g_signal_connect ((gpointer) drawingarea, "scroll-event",G_CALLBACK (DrawingareaScrollEvent),NULL);
140 
141  gtk_widget_set_events (drawingarea, GDK_ENTER_NOTIFY_MASK |GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK);
142 
143  /* Store pointers to all widgets, for use by lookup_widget(). */
144  GLADE_HOOKUP_OBJECT_NO_REF (window, window, "window");
145  GLADE_HOOKUP_OBJECT (window, drawingarea, "drawingarea");
146 
147  return window;
148 }
149 
157 gboolean Redraw(gpointer data)
158 {
159  GtkWidget*window=(GtkWidget*)data;
160  GtkWidget*drawingarea = lookup_widget (window, "drawingarea");
161 
162  if(drawGraph())
163  gdk_window_invalidate_rect(drawingarea->window,&drawingarea->allocation,FALSE);
164  return 1;
165 }
166 
176 static void gtk_initialize(GVJ_t *firstjob)
177 {
178  Display *dpy;
179  const char *display_name = NULL;
180  int scr;
181 
182  gtk_set_locale ();
183  gtk_init (NULL, NULL);
184 
185  dpy = XOpenDisplay(display_name);
186  if (dpy == NULL) {
187  fprintf(stderr, "Failed to open XLIB display: %s\n",
188  XDisplayName(NULL));
189  return;
190  }
191  scr = DefaultScreen(dpy);
192 
193  firstjob->device_dpi.x = DisplayWidth(dpy, scr) * 25.4 / DisplayWidthMM(dpy, scr);
194  firstjob->device_dpi.y = DisplayHeight(dpy, scr) * 25.4 / DisplayHeightMM(dpy, scr);
195 
196  firstjob->device_sets_dpi = TRUE;
197 }
198 
206 static void gtk_finalize(GVJ_t *firstjob)
207 {
208  GVJ_t *job;
209  GtkWidget *window, *drawingarea;
210 
211  for (job = firstjob; job; job = job->next_active)
212  {
213  window = create_window();
214 
215  g_object_set_data(G_OBJECT(window), "job", (gpointer) job);
216 
217  drawingarea = lookup_widget (window, "drawingarea");
218  g_object_set_data(G_OBJECT(drawingarea), "job", (gpointer) job);
219 
220  g_timeout_add(50,Redraw,window);
221 
222  gtk_widget_show (window);
223  }
224 
225  gtk_main();
226 }
227 
229 static gvdevice_engine_t device_engine_xgtk = {
231  NULL, /* gtk_format */
232  gtk_finalize,
233 };
234 
236 static gvdevice_features_t device_features_xgtk = {
237  GVDEVICE_DOES_TRUECOLOR | GVDEVICE_EVENTS , /* flags */
238  {0.,0.}, /* default margin - points */
239  {100.,100.}, /* default page width, height - points */
240  {96.,96.}, /* dpi */
241 };
242 
244 gvplugin_installed_t gvdevice_types_xgtk[] = {
245  {0,(char*)"xgtk:cairo", 0, &device_engine_xgtk, &device_features_xgtk},
246  {0, NULL, 0, NULL, NULL}
247 };
248 
250 static gvplugin_api_t apis[] = {
251  {API_device, gvdevice_types_xgtk},
252  {(api_t)0, 0},
253 };
254 
256 gvplugin_library_t gvplugin_xgtk_LTX_library = {(char*)"xgtk", apis };
gvplugin_library_t gvplugin_xgtk_LTX_library
Libgvc plug-in library object, the name of this variable is very specific (its specification is in th...
gboolean Redraw(gpointer data)
Redraw main window.
#define GLADE_HOOKUP_OBJECT(component, widget, name)
Hookup object to widget with call to g_object_set_data_full.
static gvplugin_api_t apis[]
Libgvc api descriptor, type and gvplugin_installed_t structure.
gboolean WindowDeleteEvent(GtkWidget *widget, GdkEvent *event, gpointer user_data)
Definition: callbacks.cpp:260
static void gtk_finalize(GVJ_t *firstjob)
End point for the plug-in engine.
GtkWidget * lookup_widget(GtkWidget *widget, const gchar *widget_name)
Obtain a widget from a lower point in the widget structure.
gboolean DrawingareaExposeEvent(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
Definition: callbacks.cpp:81
gboolean DrawingareaScrollEvent(GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
Definition: callbacks.cpp:232
GtkWidget * create_window(void)
Create a GTK window.
gboolean DrawingareaButtonPressEvent(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
Definition: callbacks.cpp:156
static gvdevice_engine_t device_engine_xgtk
Libgvc plug-in render engine description.
static void gtk_initialize(GVJ_t *firstjob)
Start point for the plug-in.
static gvdevice_features_t device_features_xgtk
Libgvc plug-in render feature descriptor.
gboolean DrawingareaConfigureEvent(GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
Definition: callbacks.cpp:266
gboolean DrawingareaMotionNotifyEvent(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
Definition: callbacks.cpp:175
gvplugin_installed_t gvdevice_types_xgtk[]
Libgvc plug-in descriptor, name. engine and features, must be null terminated.
#define GLADE_HOOKUP_OBJECT_NO_REF(component, widget, name)
Hookup object to widget with call to g_object_set_data.
gboolean drawGraph(void)
Callback header, only function prototypes.


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18