/*
* Copyright (C) 2013 - Juan Ferrer Toribio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "glade-vn.h"
#ifdef _HAVE_GTKSOURCEVIEW
#include
#include
#include
#include
#endif
#define NEW_SQL _("New SQL statement")
//+++++++++++++++++++++++++++++++++++++++++++++++++++ GladeEPropSql
typedef struct
{
GladeEditorProperty parent;
GtkWidget * button;
GtkWidget * entry;
gint width;
gint height;
}
GladeEPropSql;
GLADE_MAKE_EPROP (GladeEPropSql, glade_eprop_sql)
#define GLADE_TYPE_EPROP_SQL (glade_eprop_sql_get_type())
#define GLADE_EPROP_SQL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_EPROP_SQL, GladeEPropSql))
#define GLADE_EPROP_SQL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_EPROP_SQL, GladeEPropSqlClass))
#define GLADE_IS_EPROP_SQL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GLADE_TYPE_EPROP_SQL))
#define GLADE_IS_EPROP_SQL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GLADE_TYPE_EPROP_SQL))
#define GLADE_EPROP_SQL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GLADE_EPROP_SQL, GladeEPropSqlClass))
static void glade_eprop_sql_show_dialog (GtkButton * button, GladeEditorProperty * eprop)
{
gpointer text, buffer;
const gchar * sql;
GladeEPropSql * obj = GLADE_EPROP_SQL (eprop);
PangoFontDescription * font;
GtkWidget * scroll;
GladeProperty * p = glade_editor_property_get_property (eprop);
GtkDialog * dialog = GTK_DIALOG (gtk_dialog_new_with_buttons
(_("SQL Editor")
,GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (eprop)))
,GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT
,GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL
,GTK_STOCK_CLEAR, GTK_RESPONSE_REJECT
,GTK_STOCK_OK, GTK_RESPONSE_OK
,NULL
));
gtk_dialog_set_alternative_button_order (dialog,
GTK_RESPONSE_OK, GTK_RESPONSE_REJECT, GTK_RESPONSE_CANCEL, -1);
gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
gtk_window_set_default_size (GTK_WINDOW (dialog), obj->width, obj->height);
scroll = gtk_scrolled_window_new (gtk_adjustment_new (0,0,0,0,0,0),
gtk_adjustment_new (0,0,0,0,0,0));
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_IN);
gtk_widget_set_margin_top (scroll, 6);
gtk_widget_set_margin_right (scroll, 6);
gtk_widget_set_margin_bottom (scroll, 6);
gtk_widget_set_margin_left (scroll, 6);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (dialog)),
scroll, TRUE, TRUE, 6);
#ifdef _HAVE_GTKSOURCEVIEW
GtkSourceLanguageManager * lm = gtk_source_language_manager_new ();
GtkSourceLanguage * lang = gtk_source_language_manager_get_language (lm, "sql");
text = gtk_source_view_new ();
gtk_source_view_set_tab_width (text, 4);
gtk_source_view_set_show_line_numbers (text, TRUE);
gtk_source_view_set_highlight_current_line (text, TRUE);
buffer = gtk_text_view_get_buffer (text);
gtk_source_buffer_set_language (buffer, lang);
gtk_source_buffer_set_highlight_matching_brackets (buffer, TRUE);
#else
text = gtk_text_view_new ();
buffer = gtk_text_view_get_buffer (text);
#endif
font = pango_font_description_from_string ("Monospace");
gtk_widget_override_font (text, font);
pango_font_description_free (font);
sql = g_value_get_string (glade_property_inline_value (p));
if (sql)
gtk_text_buffer_set_text (buffer, sql, -1);
gtk_container_add (GTK_CONTAINER (scroll), text);
gtk_widget_show_all (scroll);
switch (gtk_dialog_run (dialog))
{
case GTK_RESPONSE_OK:
{
GValue val = G_VALUE_INIT;
GtkTextIter * start = g_new (GtkTextIter, 1),
* end = g_new (GtkTextIter, 1);
g_value_init (&val, G_TYPE_STRING);
gtk_text_buffer_get_bounds (buffer, start, end);
sql = gtk_text_buffer_get_text (buffer, start, end, TRUE);
g_value_set_string (&val, sql);
glade_command_set_property_value (p, &val);
g_value_unset (&val);
g_free (start);
g_free (end);
break;
}
case GTK_RESPONSE_REJECT:
{
GValue val = G_VALUE_INIT;
g_value_init (&val, G_TYPE_STRING);
g_value_set_string (&val, "");
glade_command_set_property_value (p, &val);
g_value_unset (&val);
break;
}
case GTK_RESPONSE_CANCEL:
default:
break;
}
obj->width = gtk_widget_get_allocated_width (GTK_WIDGET (dialog));
obj->height = gtk_widget_get_allocated_height (GTK_WIDGET (dialog));
gtk_widget_destroy (GTK_WIDGET (dialog));
}
static void glade_eprop_sql_entry_text_changed (GtkEntry * entry,
GladeEditorProperty * eprop)
{
const gchar * text = gtk_entry_get_text (entry);
GladeProperty * p = glade_editor_property_get_property (eprop);
if (text)
{
GValue val = G_VALUE_INIT;
g_value_init (&val, G_TYPE_STRING);
g_value_set_string (&val, text);
glade_command_set_property_value (p, &val);
g_value_unset (&val);
}
}
static GtkWidget * glade_eprop_sql_create_input (GladeEditorProperty * eprop)
{
GladeEPropSql * obj = GLADE_EPROP_SQL (eprop);
GtkWidget * box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
obj->entry = gtk_entry_new ();
gtk_widget_set_hexpand (obj->entry, TRUE);
g_signal_connect (obj->entry, "changed",
G_CALLBACK (glade_eprop_sql_entry_text_changed), obj);
gtk_container_add (GTK_CONTAINER (box), obj->entry);
obj->button = gtk_button_new_with_label ("...");
g_signal_connect (obj->button, "clicked",
G_CALLBACK (glade_eprop_sql_show_dialog), obj);
gtk_widget_set_tooltip_text (obj->button, _("Open the SQL Editor"));
gtk_container_add (GTK_CONTAINER (box), obj->button);
gtk_widget_show_all (box);
obj->width = 750;
obj->height = 550;
return box;
}
static void glade_eprop_sql_load (GladeEditorProperty * eprop, GladeProperty * property)
{
const gchar * sql;
GladeEPropSql * obj = GLADE_EPROP_SQL (eprop);
GladeEditorPropertyClass * parent_class =
g_type_class_peek_parent (GLADE_EDITOR_PROPERTY_GET_CLASS (eprop));
parent_class->load (eprop, property);
if (!property)
return;
if ((sql = g_value_get_string (glade_property_inline_value (property))))
gtk_entry_set_text (GTK_ENTRY (obj->entry), sql);
}
static void glade_eprop_sql_finalize (GObject * object)
{
GObjectClass * parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (object));
G_OBJECT_CLASS (parent_class)->finalize (object);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++ DbModel GladeWidgetAdaptor
GladeEditorProperty * glade_db_sql_create_eprop (GladeWidgetAdaptor * adaptor,
GladePropertyClass * klass, gboolean use_command)
{
GladeEditorProperty * eprop;
GParamSpec * pspec = glade_property_class_get_pspec (klass);
if (!g_strcmp0 (pspec->name, "sql"))
{
eprop = g_object_new (GLADE_TYPE_EPROP_SQL,
"property-class", klass,
"use-command", use_command, NULL);
}
else
eprop = GWA_GET_CLASS (G_TYPE_OBJECT)->create_eprop (adaptor, klass, use_command);
return eprop;
}