File: extspinbutton.c

package info (click to toggle)
soundtracker 0.6.4-0.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,604 kB
  • ctags: 2,906
  • sloc: ansic: 26,588; asm: 1,506; sh: 327; makefile: 312; sed: 93
file content (139 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

/*
 * The Real SoundTracker - GTK+ Spinbutton extensions
 *
 * Copyright (C) 1999-2001 Michael Krause
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <math.h>

#include "extspinbutton.h"
#include "gui.h"

/* These two defines have been taken from gtkspinbutton.c in gtk+-1.2.1
   Unfortunately there's no cleaner solution */

#define MIN_SPIN_BUTTON_WIDTH              30
#define ARROW_SIZE                         11

static GtkEntryClass *parent_class = NULL;

static int
extspinbutton_find_display_digits (GtkAdjustment *adjustment)
{
    int num_digits;

    num_digits = adjustment->lower < 0 || adjustment->upper < 0;
    num_digits += ceil(log10(MAX(1000, MAX(ABS(adjustment->lower), ABS(adjustment->upper)))));

    return num_digits;
}

static void
extspinbutton_size_request (GtkWidget      *widget,
			    GtkRequisition *requisition)
{
    g_return_if_fail (widget != NULL);
    g_return_if_fail (requisition != NULL);
    g_return_if_fail (GTK_IS_SPIN_BUTTON (widget));

    GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);

    if(EXTSPINBUTTON(widget)->size_hack) {
	requisition->width = MAX (MIN_SPIN_BUTTON_WIDTH,
				  extspinbutton_find_display_digits(GTK_SPIN_BUTTON(widget)->adjustment)
				  * gdk_string_width(widget->style->font, "X"))
	    + ARROW_SIZE
	    + 2 * widget->style->klass->xthickness;
    } else {
	// This is the normal size_request() from gtk+-1.2.8
	requisition->width = MIN_SPIN_BUTTON_WIDTH + ARROW_SIZE 
	    + 2 * widget->style->klass->xthickness;
    }
}

static void
extspinbutton_value_changed (GtkSpinButton *spin)
{
    if(spin->button != 0) {
	// Should only do this if this widget is really in the main window.
	gtk_window_set_focus(GTK_WINDOW(mainwindow), NULL);
    }
}

GtkWidget *
extspinbutton_new (GtkAdjustment *adjustment,
		   gfloat climb_rate,
		   guint digits)
{
    ExtSpinButton *s;

    s = gtk_type_new(extspinbutton_get_type());
    s->size_hack = TRUE;
    gtk_spin_button_configure(GTK_SPIN_BUTTON(s), adjustment, climb_rate, digits);

    gtk_signal_connect(GTK_OBJECT(s), "changed",
		       GTK_SIGNAL_FUNC(extspinbutton_value_changed), NULL);

    return GTK_WIDGET(s);
}

void
extspinbutton_disable_size_hack (ExtSpinButton *b)
{
    b->size_hack = FALSE;
}

static void
extspinbutton_init (ExtSpinButton *s)
{
}

static void
extspinbutton_class_init (ExtSpinButtonClass *class)
{
    GtkWidgetClass   *widget_class;

    widget_class = (GtkWidgetClass*)class;

    widget_class->size_request = extspinbutton_size_request;

    parent_class = gtk_type_class (GTK_TYPE_ENTRY);
}

guint
extspinbutton_get_type (void)
{
    static guint extspinbutton_type = 0;
    
    if (!extspinbutton_type) {
	GtkTypeInfo extspinbutton_info =
	{
	    "ExtSpinButton",
	    sizeof(ExtSpinButton),
	    sizeof(ExtSpinButtonClass),
	    (GtkClassInitFunc) extspinbutton_class_init,
	    (GtkObjectInitFunc) extspinbutton_init,
	    (GtkArgSetFunc) NULL,
	    (GtkArgGetFunc) NULL,
	};
	
	extspinbutton_type = gtk_type_unique(gtk_spin_button_get_type (), &extspinbutton_info);
    }
    
    return extspinbutton_type;
}