geany/src/geanyentryaction.c
Matthew Brush 4efcbab332 Include what you use
This is a mega-commit - because most of it had to be done in one go
otherwise some commits would fail to compile - that attempts to fix a
few problems with Geany's includes as well as various other related
cleanups. After this change it's easier to use includes and there's
little worry about which order things are included in or who includes
what.

Overview of changes:

* Include config.h at the start of each source file if HAVE_CONFIG_H
  is defined (and never in headers).
* Go through each source file and make the includes section generally
  like this:
  - Always config.h first as above
  - Then if the file has a header with the same name, include that
  - Then include in alphabetical order each other internal/geany header.
  - Then include standard headers
  - Then include non-standard system headers
  - Then include GLib/GTK+ related stuff
* Doing as above makes it easier to find implicit header include
  dependencies and it exposed quite a few weird problems with includes
  or forward declarations, fix those.
* Make geany.h contain not much besides some defines.
  - Add a little header file "app.h" for GeanyApp and move it there
  - Move "app" global to new "app.h" file
  - Move "ignore_callback" global to "callbacks.h"
  - Move "geany_object" global to "geanyobject.h"
* Add an include in "geany.h" for "app.h" since GeanyApp used to be
  defined there and some plugins included this header to access
  GeanyApp.
* Include "gtkcompat.h" everywhere instead of gtk/gtk.h so that
  everywhere sees the same definitions (not a problem in practice AFAIK
  so this could be changed back if better that way.
* Remove forward declarations from previous commits as some people
  apparently consider this bad style, despite that it reduces inter-
  header dependencies.

TODO:
* As always, to test on win32
* As always, to test with not Autotools
* Test plugins better, both builtin and geany-plugins, likely API/ABI bump
* Test with various defines/flags that may change what is included
* win32.[ch] not really touched since I couldn't test
2014-05-21 15:37:19 -07:00

198 lines
5.7 KiB
C

/*
* geanyentryaction.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2008-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2008-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* GtkAction subclass to provide a GtkEntry in a toolbar.
* This class is missing the action_create_menu_item() function and so can't be
* used for creating menu items. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "geanyentryaction.h"
#include "ui_utils.h"
#include <ctype.h>
typedef struct _GeanyEntryActionPrivate GeanyEntryActionPrivate;
#define GEANY_ENTRY_ACTION_GET_PRIVATE(obj) (GEANY_ENTRY_ACTION(obj)->priv)
struct _GeanyEntryActionPrivate
{
GtkWidget *entry;
gboolean numeric;
gboolean connected;
};
enum
{
ENTRY_ACTIVATE,
ENTRY_ACTIVATE_BACKWARD,
ENTRY_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
G_DEFINE_TYPE(GeanyEntryAction, geany_entry_action, GTK_TYPE_ACTION)
static GtkWidget *geany_entry_action_create_tool_item(GtkAction *action)
{
GtkWidget *toolitem;
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
priv->entry = gtk_entry_new();
if (priv->numeric)
gtk_entry_set_width_chars(GTK_ENTRY(priv->entry), 9);
ui_entry_add_clear_icon(GTK_ENTRY(priv->entry));
ui_entry_add_activate_backward_signal(GTK_ENTRY(priv->entry));
gtk_widget_show(priv->entry);
toolitem = g_object_new(GTK_TYPE_TOOL_ITEM, NULL);
gtk_container_add(GTK_CONTAINER(toolitem), priv->entry);
return toolitem;
}
static void delegate_entry_activate_cb(GtkEntry *entry, GeanyEntryAction *action)
{
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
g_signal_emit(action, signals[ENTRY_ACTIVATE], 0, text);
}
static void delegate_entry_activate_backward_cb(GtkEntry *entry, GeanyEntryAction *action)
{
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
g_signal_emit(action, signals[ENTRY_ACTIVATE_BACKWARD], 0, text);
}
static void delegate_entry_changed_cb(GtkEditable *editable, GeanyEntryAction *action)
{
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
const gchar *text = gtk_entry_get_text(GTK_ENTRY(priv->entry));
g_signal_emit(action, signals[ENTRY_CHANGED], 0, text);
}
static void geany_entry_action_connect_proxy(GtkAction *action, GtkWidget *widget)
{
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
/* make sure not to connect handlers twice */
if (! priv->connected)
{
if (priv->numeric)
g_signal_connect(priv->entry, "insert-text",
G_CALLBACK(ui_editable_insert_text_callback), NULL);
g_signal_connect(priv->entry, "changed", G_CALLBACK(delegate_entry_changed_cb), action);
g_signal_connect(priv->entry, "activate", G_CALLBACK(delegate_entry_activate_cb), action);
g_signal_connect(priv->entry, "activate-backward",
G_CALLBACK(delegate_entry_activate_backward_cb), action);
priv->connected = TRUE;
}
GTK_ACTION_CLASS(geany_entry_action_parent_class)->connect_proxy(action, widget);
}
static void geany_entry_action_class_init(GeanyEntryActionClass *klass)
{
GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
action_class->connect_proxy = geany_entry_action_connect_proxy;
action_class->create_tool_item = geany_entry_action_create_tool_item;
action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
g_type_class_add_private(klass, sizeof(GeanyEntryActionPrivate));
signals[ENTRY_CHANGED] = g_signal_new("entry-changed",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
signals[ENTRY_ACTIVATE] = g_signal_new("entry-activate",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
signals[ENTRY_ACTIVATE_BACKWARD] = g_signal_new("entry-activate-backward",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
}
static void geany_entry_action_init(GeanyEntryAction *action)
{
GeanyEntryActionPrivate *priv;
action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
GEANY_ENTRY_ACTION_TYPE, GeanyEntryActionPrivate);
priv = action->priv;
priv->entry = NULL;
priv->numeric = FALSE;
priv->connected = FALSE;
}
GtkAction *geany_entry_action_new(const gchar *name, const gchar *label,
const gchar *tooltip, gboolean numeric)
{
GtkAction *action = g_object_new(GEANY_ENTRY_ACTION_TYPE,
"name", name,
"label", label,
"tooltip", tooltip,
NULL);
GeanyEntryActionPrivate *priv = GEANY_ENTRY_ACTION_GET_PRIVATE(action);
priv->numeric = numeric;
return action;
}