Commit f37c3683 authored by Sven Neumann's avatar Sven Neumann

Added the gimpunitmenu-widget Michael Natterer contributed.

--Sven
parent df051bd3
Sun Feb 21 20:43:00 MET 1999 Sven Neumann <sven@gimp.org>
* libgimp/Makefile.am
* libgimp/gimpunitmenu.[ch]: new files
Added the gimpunitmenu-widget Michael Natterer contributed.
Now lets see how this integrates with the gimpentry-widget...
Sat Feb 20 16:12:33 CST 1999 Shawn T. Amundson <amundson@gimp.org>
* app/tips_dialog.c: Add default to Cancel button, remove
......
......@@ -25,6 +25,8 @@ libgimpi_a_SOURCES = \
gimpprotocol.h \
gimpunit.c \
gimpunit.h \
gimpunitmenu.c \
gimpunitmenu.h \
gimpwire.c \
gimpwire.h \
gserialize.c \
......@@ -82,6 +84,7 @@ gimpinclude_HEADERS = \
gimpmodule.h \
gimpui.h \
gimpunit.h \
gimpunitmenu.h \
gimpintl.h \
gserialize.h \
parasite.h \
......
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpunitmenu.c
* Copyright (C) 1999 Michael Natterer <mitschel@cs.tu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gimpunitmenu.h"
#include "libgimp/gimpintl.h"
static const gchar* gimp_unit_menu_build_string (gchar *format, GUnit unit);
static void gimp_unit_menu_callback (GtkWidget *widget, gpointer data);
enum {
GUM_UNIT_CHANGED_SIGNAL,
LAST_SIGNAL
};
static gint gimp_unit_menu_signals[LAST_SIGNAL] = { 0 };
static void
gimp_unit_menu_class_init (GimpUnitMenuClass *class)
{
GtkObjectClass *object_class;
object_class = (GtkObjectClass*) class;
gimp_unit_menu_signals[GUM_UNIT_CHANGED_SIGNAL] =
gtk_signal_new ("unit_changed",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GimpUnitMenuClass, gimp_unit_menu),
gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
gtk_object_class_add_signals (object_class, gimp_unit_menu_signals,
LAST_SIGNAL);
class->gimp_unit_menu = NULL;
}
static void
gimp_unit_menu_init (GimpUnitMenu *gum)
{
gum->unit = UNIT_PIXEL;
gum->start = 0;
}
guint
gimp_unit_menu_get_type ()
{
static guint gum_type = 0;
if (!gum_type)
{
GtkTypeInfo gum_info =
{
"GimpUnitMenu",
sizeof (GimpUnitMenu),
sizeof (GimpUnitMenuClass),
(GtkClassInitFunc) gimp_unit_menu_class_init,
(GtkObjectInitFunc) gimp_unit_menu_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL
};
gum_type = gtk_type_unique (gtk_option_menu_get_type (), &gum_info);
}
return gum_type;
}
GtkWidget*
gimp_unit_menu_new (gchar *format,
GUnit unit,
gboolean with_pixels,
gboolean with_custom)
{
GimpUnitMenu *gum;
GtkWidget *menu;
GtkWidget *menuitem;
GUnit u;
if(with_custom); /* avoid 'unused variable' compiler warning */
gum = gtk_type_new (gimp_unit_menu_get_type ());
/* if we don't want pixels, start with inches */
gum->start = with_pixels ? UNIT_PIXEL : UNIT_INCH;
if ( (unit < gum->start) || (unit >= UNIT_END) )
unit = gum->start;
menu = gtk_menu_new();
for (u = gum->start; u < gimp_unit_get_number_of_units(); u++)
{
menuitem =
gtk_menu_item_new_with_label (gimp_unit_menu_build_string(format, u) );
gtk_menu_append (GTK_MENU (menu), menuitem);
gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
(GtkSignalFunc) gimp_unit_menu_callback, gum);
gtk_widget_show(menuitem);
gtk_object_set_data (GTK_OBJECT (menuitem), "gimp_unit_menu", (gpointer)u);
}
gtk_option_menu_set_menu (GTK_OPTION_MENU (gum), menu);
gum->unit = unit;
gtk_option_menu_set_history (GTK_OPTION_MENU (gum), unit - gum->start);
return GTK_WIDGET (gum);
}
void
gimp_unit_menu_set_unit (GimpUnitMenu *gum,
GUnit unit)
{
if ( (unit < gum->start) || (unit >= UNIT_END) )
unit = gum->start;
gum->unit = unit;
gtk_option_menu_set_history (GTK_OPTION_MENU (gum), unit - gum->start);
}
GUnit
gimp_unit_menu_get_unit (GimpUnitMenu *gum)
{
return gum->unit;
}
/* most of the next two functions is stolen from app/gdisplay.h ;-) */
static int
print (char *buf, int len, int start, const char *fmt, ...)
{
va_list args;
int printed;
va_start (args, fmt);
printed = g_vsnprintf (buf + start, len - start, fmt, args);
if (printed < 0)
printed = len - start;
va_end (args);
return printed;
}
static const gchar* gimp_unit_menu_build_string (gchar *format, GUnit unit)
{
#define BUFFER_LEN 64
static gchar buffer[BUFFER_LEN];
gchar format_buffer[16];
int i = 0;
while (i < (BUFFER_LEN - 1) && *format)
{
switch (*format)
{
case '%':
format++;
switch (*format)
{
case 0:
g_warning (_("unit-menu-format string ended within %%-sequence"));
break;
case '%':
buffer[i++] = '%';
break;
case 'f': /* factor (how many units make up an inch) */
g_snprintf(format_buffer, 16, "%%.%df",
gimp_unit_get_digits (unit));
i += print (buffer, BUFFER_LEN, i, format_buffer,
gimp_unit_get_factor (unit));
break;
case 'y': /* symbol ("''" for inch) */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_symbol (unit));
break;
case 'a': /* abbreviation */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_abbreviation (unit));
break;
case 's': /* singular */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_singular (unit));
break;
case 'p': /* plural */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_plural (unit));
break;
default:
g_warning (_("unit-menu-format contains unknown"
"format sequence '%%%c'"), *format);
break;
}
break;
default:
buffer[i++] = *format;
break;
}
format++;
}
buffer[MIN(i, BUFFER_LEN - 1)] = 0;
return buffer;
}
static void
gimp_unit_menu_callback (GtkWidget *widget,
gpointer data)
{
GimpUnitMenu *gum;
GUnit new_unit;
gum = data;
new_unit = (guint) gtk_object_get_data (GTK_OBJECT (widget),
"gimp_unit_menu");
if (gum->unit == new_unit)
return;
gum->unit = new_unit;
gtk_signal_emit (GTK_OBJECT (gum),
gimp_unit_menu_signals[GUM_UNIT_CHANGED_SIGNAL]);
}
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpunitmenu.h
* Copyright (C) 1999 Michael Natterer <mitschel@cs.tu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GIMP_UNIT_MENU_H__
#define __GIMP_UNIT_MENU_H__
#include <gtk/gtk.h>
#include "libgimp/gimpunit.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define GIMP_TYPE_UNIT_MENU (gimp_unit_menu_get_type ())
#define GIMP_UNIT_MENU(obj) (GTK_CHECK_CAST ((obj), GIMP_TYPE_UNIT_MENU, GimpUnitMenu))
#define GIMP_UNIT_MENU_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GIMP_TYPE_UNIT_MENU, GimpUnitMenuClass))
#define GIMP_IS_UNIT_MENU(obj) (GTK_CHECK_TYPE (obj, GIMP_TYPE_UNIT_MENU))
#define GIMP_IS_UNIT_MENU_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_UNIT_MENU))
typedef struct _GimpUnitMenu GimpUnitMenu;
typedef struct _GimpUnitMenuClass GimpUnitMenuClass;
struct _GimpUnitMenu
{
GtkOptionMenu optionmenu;
GUnit unit;
GUnit start;
};
struct _GimpUnitMenuClass
{
GtkOptionMenuClass parent_class;
void (* gimp_unit_menu) (GimpUnitMenu *gum);
};
guint gimp_unit_menu_get_type (void);
/* format -- a printf-like format string for the menu items
* unit -- the unit selected on widget creation
* with_pixels -- should the menu contain 'pixels' ?
* with_custom -- should the menu contain an item 'More...' to pop up
* the custom unit browser (not yet implemented)
*
* the format string supports the following percent expansions:
*
* %f -- factor (how many units make up an inch)
* %y -- symbol ("''" for inch)
* %a -- abbreviation
* %s -- singular
* %p -- plural
* %% -- literal percent
*/
GtkWidget* gimp_unit_menu_new (gchar* format,
GUnit unit,
gboolean with_pixels,
gboolean with_custom);
void gimp_unit_menu_set_unit (GimpUnitMenu *gum,
GUnit unit);
GUnit gimp_unit_menu_get_unit (GimpUnitMenu *gum);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __GIMP_UNIT_MENU_H__ */
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpunitmenu.c
* Copyright (C) 1999 Michael Natterer <mitschel@cs.tu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gimpunitmenu.h"
#include "libgimp/gimpintl.h"
static const gchar* gimp_unit_menu_build_string (gchar *format, GUnit unit);
static void gimp_unit_menu_callback (GtkWidget *widget, gpointer data);
enum {
GUM_UNIT_CHANGED_SIGNAL,
LAST_SIGNAL
};
static gint gimp_unit_menu_signals[LAST_SIGNAL] = { 0 };
static void
gimp_unit_menu_class_init (GimpUnitMenuClass *class)
{
GtkObjectClass *object_class;
object_class = (GtkObjectClass*) class;
gimp_unit_menu_signals[GUM_UNIT_CHANGED_SIGNAL] =
gtk_signal_new ("unit_changed",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GimpUnitMenuClass, gimp_unit_menu),
gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
gtk_object_class_add_signals (object_class, gimp_unit_menu_signals,
LAST_SIGNAL);
class->gimp_unit_menu = NULL;
}
static void
gimp_unit_menu_init (GimpUnitMenu *gum)
{
gum->unit = UNIT_PIXEL;
gum->start = 0;
}
guint
gimp_unit_menu_get_type ()
{
static guint gum_type = 0;
if (!gum_type)
{
GtkTypeInfo gum_info =
{
"GimpUnitMenu",
sizeof (GimpUnitMenu),
sizeof (GimpUnitMenuClass),
(GtkClassInitFunc) gimp_unit_menu_class_init,
(GtkObjectInitFunc) gimp_unit_menu_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL
};
gum_type = gtk_type_unique (gtk_option_menu_get_type (), &gum_info);
}
return gum_type;
}
GtkWidget*
gimp_unit_menu_new (gchar *format,
GUnit unit,
gboolean with_pixels,
gboolean with_custom)
{
GimpUnitMenu *gum;
GtkWidget *menu;
GtkWidget *menuitem;
GUnit u;
if(with_custom); /* avoid 'unused variable' compiler warning */
gum = gtk_type_new (gimp_unit_menu_get_type ());
/* if we don't want pixels, start with inches */
gum->start = with_pixels ? UNIT_PIXEL : UNIT_INCH;
if ( (unit < gum->start) || (unit >= UNIT_END) )
unit = gum->start;
menu = gtk_menu_new();
for (u = gum->start; u < gimp_unit_get_number_of_units(); u++)
{
menuitem =
gtk_menu_item_new_with_label (gimp_unit_menu_build_string(format, u) );
gtk_menu_append (GTK_MENU (menu), menuitem);
gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
(GtkSignalFunc) gimp_unit_menu_callback, gum);
gtk_widget_show(menuitem);
gtk_object_set_data (GTK_OBJECT (menuitem), "gimp_unit_menu", (gpointer)u);
}
gtk_option_menu_set_menu (GTK_OPTION_MENU (gum), menu);
gum->unit = unit;
gtk_option_menu_set_history (GTK_OPTION_MENU (gum), unit - gum->start);
return GTK_WIDGET (gum);
}
void
gimp_unit_menu_set_unit (GimpUnitMenu *gum,
GUnit unit)
{
if ( (unit < gum->start) || (unit >= UNIT_END) )
unit = gum->start;
gum->unit = unit;
gtk_option_menu_set_history (GTK_OPTION_MENU (gum), unit - gum->start);
}
GUnit
gimp_unit_menu_get_unit (GimpUnitMenu *gum)
{
return gum->unit;
}
/* most of the next two functions is stolen from app/gdisplay.h ;-) */
static int
print (char *buf, int len, int start, const char *fmt, ...)
{
va_list args;
int printed;
va_start (args, fmt);
printed = g_vsnprintf (buf + start, len - start, fmt, args);
if (printed < 0)
printed = len - start;
va_end (args);
return printed;
}
static const gchar* gimp_unit_menu_build_string (gchar *format, GUnit unit)
{
#define BUFFER_LEN 64
static gchar buffer[BUFFER_LEN];
gchar format_buffer[16];
int i = 0;
while (i < (BUFFER_LEN - 1) && *format)
{
switch (*format)
{
case '%':
format++;
switch (*format)
{
case 0:
g_warning (_("unit-menu-format string ended within %%-sequence"));
break;
case '%':
buffer[i++] = '%';
break;
case 'f': /* factor (how many units make up an inch) */
g_snprintf(format_buffer, 16, "%%.%df",
gimp_unit_get_digits (unit));
i += print (buffer, BUFFER_LEN, i, format_buffer,
gimp_unit_get_factor (unit));
break;
case 'y': /* symbol ("''" for inch) */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_symbol (unit));
break;
case 'a': /* abbreviation */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_abbreviation (unit));
break;
case 's': /* singular */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_singular (unit));
break;
case 'p': /* plural */
i += print (buffer, BUFFER_LEN, i, "%s",
gimp_unit_get_plural (unit));
break;
default:
g_warning (_("unit-menu-format contains unknown"
"format sequence '%%%c'"), *format);
break;
}
break;
default:
buffer[i++] = *format;
break;
}
format++;
}
buffer[MIN(i, BUFFER_LEN - 1)] = 0;
return buffer;
}
static void
gimp_unit_menu_callback (GtkWidget *widget,
gpointer data)
{
GimpUnitMenu *gum;
GUnit new_unit;
gum = data;
new_unit = (guint) gtk_object_get_data (GTK_OBJECT (widget),
"gimp_unit_menu");
if (gum->unit == new_unit)
return;
gum->unit = new_unit;
gtk_signal_emit (GTK_OBJECT (gum),
gimp_unit_menu_signals[GUM_UNIT_CHANGED_SIGNAL]);
}
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpunitmenu.h
* Copyright (C) 1999 Michael Natterer <mitschel@cs.tu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GIMP_UNIT_MENU_H__
#define __GIMP_UNIT_MENU_H__
#include <gtk/gtk.h>
#include "libgimp/gimpunit.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define GIMP_TYPE_UNIT_MENU (gimp_unit_menu_get_type ())
#define GIMP_UNIT_MENU(obj) (GTK_CHECK_CAST ((obj), GIMP_TYPE_UNIT_MENU, GimpUnitMenu))
#define GIMP_UNIT_MENU_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), GIMP_TYPE_UNIT_MENU, GimpUnitMenuClass))
#define GIMP_IS_UNIT_MENU(obj) (GTK_CHECK_TYPE (obj, GIMP_TYPE_UNIT_MENU))
#define GIMP_IS_UNIT_MENU_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_UNIT_MENU))
typedef struct _GimpUnitMenu GimpUnitMenu;
typedef struct _GimpUnitMenuClass GimpUnitMenuClass;
struct _GimpUnitMenu
{
GtkOptionMenu optionmenu;
GUnit unit;
GUnit start;
};
struct _GimpUnitMenuClass
{
GtkOptionMenuClass parent_class;
void (* gimp_unit_menu) (GimpUnitMenu *gum);
};
guint gimp_unit_menu_get_type (void);
/* format -- a printf-like format string for the menu items
* unit -- the unit selected on widget creation
* with_pixels -- should the menu contain 'pixels' ?
* with_custom -- should the menu contain an item 'More...' to pop up
* the custom unit browser (not yet implemented)
*
* the format string supports the following percent expansions:
*
* %f -- factor (how many units make up an inch)
* %y -- symbol ("''" for inch)
* %a -- abbreviation
* %s -- singular
* %p -- plural
* %% -- literal percent
*/
GtkWidget* gimp_unit_menu_new (gchar* format,
GUnit unit,
gboolean with_pixels,
gboolean with_custom);
void gimp_unit_menu_set_unit (GimpUnitMenu *gum,
GUnit unit);
GUnit gimp_unit_menu_get_unit (GimpUnitMenu *gum);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __GIMP_UNIT_MENU_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment