File: ctkvscale.c

package info (click to toggle)
libctk 3.0.24.6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,316 kB
  • ctags: 1,201
  • sloc: ansic: 10,623; sh: 10,438; makefile: 102
file content (201 lines) | stat: -rw-r--r-- 5,934 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* 
 * $Id: ctkvscale.c,v 1.10 2000/07/12 00:44:10 cbond Exp $
 *
 * CTK - Console Toolkit
 *
 * Copyright (C) 1998-2000 Stormix Technologies Inc.
 *
 * License: LGPL
 *
 * Authors: Kevin Lindsay, Wesley Terpstra
 *  
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser 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
 *    Lesser General Public License for more details.
 *    
 *    You should have received a copy of the GNU Lesser 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 <stdio.h>
#include <stdlib.h>
#include <glib.h>

#include "ctk.h"
#include "ctkcolor.h"
#include "ctkutil.h"

gpointer ctk_vscale_destroy(CtkObject* object);

gboolean ctk_vscale_key_press   (CtkWidget* widget, CdkEventKey*    event, gpointer data);
gboolean ctk_vscale_button_press(CtkWidget* widget, CdkEventButton* event, gpointer data);
gboolean ctk_vscale_drag        (CtkWidget* widget, CdkEventButton* event, gpointer data);

/* Initialize the vscale */
void ctk_vscale_init(CtkVScale* vscale)
{
	ctk_scale_init(&vscale->scale);
	CTK_OBJECT(vscale)->type = CtkTypeVScale;

	((CtkWidget *)vscale)->height = 2;
	((CtkWidget *)vscale)->orig_height = 2;
	((CtkWidget *)vscale)->main_col = ctk_calculate_palette(CTK_COLOR_RED,CTK_COLOR_LIGHT_GRAY);
	((CtkWidget *)vscale)->inverse_col = ctk_calculate_palette(CTK_COLOR_YELLOW,CTK_COLOR_RED);
	((CtkWidget *)vscale)->selected_col = ctk_calculate_palette(CTK_COLOR_BLUE,CTK_COLOR_LIGHT_GRAY);
	((CtkObject *)vscale)->destroy_func = ctk_vscale_destroy;

	ctk_signal_connect(CTK_OBJECT(vscale), "button_press_event",
			   CTK_SIGNAL_FUNC(&ctk_vscale_button_press), NULL);
	ctk_signal_connect(CTK_OBJECT(vscale), "key_press_event",
			   CTK_SIGNAL_FUNC(&ctk_vscale_key_press), NULL);
	ctk_signal_connect(CTK_OBJECT(vscale), "ctk_drag",
			   CTK_SIGNAL_FUNC(&ctk_vscale_drag), NULL);
}

/* New VScale */
CtkWidget* ctk_vscale_new(CtkAdjustment* adjustment)
{
	CtkVScale* vscale;
	gchar*     upper;

	vscale = g_malloc(sizeof(CtkVScale));

	ctk_vscale_init(vscale);

	if (!adjustment)
	    adjustment = CTK_ADJUSTMENT(ctk_adjustment_new(0,0,0,0,0,0));
	
	upper = ctk_util_ftos(adjustment->upper,0);
	
	((CtkRange *)vscale)->adjustment = adjustment;
	((CtkWidget *)vscale)->orig_width = strlen(upper);

	return ((CtkWidget *)vscale);
}

/* Handle Key Events */
gboolean ctk_vscale_key_press(CtkWidget *widget, CdkEventKey* event, gpointer data)
{
      CtkVScale* vscale;
      CtkRange*  range;
      gboolean   redraw;
      gint       ch;

      redraw = TRUE;
      ch     = event->keyval;

      if (!widget)
	    return FALSE;
	
      vscale = CTK_VSCALE(widget);
      range  = CTK_RANGE (widget);

      if ((ch == AK_ARROW_RIGHT) || (ch == AK_ARROW_DOWN)) {
	    ctk_adjustment_set_value(range->adjustment,
				     range->adjustment->value+
				     range->adjustment->step_increment);
      }
      else if ((ch == AK_ARROW_LEFT) || (ch == AK_ARROW_UP)) {
	    ctk_adjustment_set_value(range->adjustment,
				     range->adjustment->value-
				     range->adjustment->step_increment);
      }

      else if (ch == AK_PAGEDOWN) 
      {
	    ctk_adjustment_set_value(range->adjustment,
				     range->adjustment->value+
				     range->adjustment->page_increment);
      }
      else if (ch == AK_PAGEUP)
      {
	    ctk_adjustment_set_value(range->adjustment,
				     range->adjustment->value-
				     range->adjustment->page_increment);
      } else
	    redraw = FALSE;
	
      if (redraw) 
      {
	    ctk_draw_mark_changed(widget);
      }
	
      return TRUE;
}

/* VScale mouse handle events */
gboolean ctk_vscale_button_press(CtkWidget* widget, CdkEventButton* event, gpointer data)
{
      CtkAdjustment* adjustment;
      gint           row_with_bullet;

      adjustment = CTK_RANGE(widget)->adjustment;

      row_with_bullet = (widget->height-3) * (adjustment->value - adjustment->lower) 
	    / (adjustment->upper - adjustment->lower);

      row_with_bullet += widget->row + 2;
      
      if (event->y == widget->row + 1)
      {	  
	    ctk_adjustment_set_value(adjustment,
				     adjustment->value -
				     adjustment->step_increment);	    
	    ctk_draw_mark_changed(widget);
      }
      else if (event->y == widget->row + widget->height - 1)
      {
	    ctk_adjustment_set_value(adjustment,
				     adjustment->value +
				     adjustment->step_increment);	    
	    ctk_draw_mark_changed(widget);
      }
      else if (event->y > row_with_bullet)
      {
	    ctk_adjustment_set_value(adjustment,
				     adjustment->value +
				     adjustment->page_increment);
	    ctk_draw_mark_changed(widget);
      }
      else if (event->y < row_with_bullet)
      {
	    ctk_adjustment_set_value(adjustment,
				     adjustment->value -
				     adjustment->page_increment);
	    ctk_draw_mark_changed(widget);
      }

      return TRUE;
}

gboolean ctk_vscale_drag(CtkWidget* widget, CdkEventButton* event, gpointer data)
{
      CtkAdjustment* adjustment;
      gint           adjust;
      
      adjustment = CTK_RANGE(widget)->adjustment;

      adjust = (adjustment->upper - adjustment->lower) * event->dy / (widget->height - 3);

      ctk_adjustment_set_value(adjustment,
			       adjustment->value + adjust);
      ctk_draw_mark_changed(widget);
            
      return TRUE;
}

/* Destroy Hscale data */
gpointer ctk_vscale_destroy(CtkObject* object)
{
	if (object)
	    ctk_range_destroy(CTK_RANGE(object));

	return NULL;
}