rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / gtkmm2ext / hexentry.cc
1 /*
2     Copyright (C) 2000 Paul Barton-Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <stdio.h> /* for sprintf, sigh ... */
22 #include <string>
23 #include <ctype.h>
24
25 #include <gdk/gdkkeysyms.h>
26 #include <gtkmm2ext/hexentry.h>
27
28 using namespace std;
29 using namespace Gtkmm2ext;
30
31 bool
32 HexEntry::on_key_press_event (GdkEventKey *ev)
33
34 {
35         if ((ev->keyval >= GDK_a && ev->keyval <= GDK_f) ||
36             (ev->keyval >= GDK_A && ev->keyval <= GDK_A) ||
37             (ev->keyval >= GDK_0 && ev->keyval <= GDK_9) ||
38             ev->keyval == GDK_space || 
39             ev->keyval == GDK_Tab ||
40             ev->keyval == GDK_Return ||
41             ev->keyval == GDK_BackSpace ||
42             ev->keyval == GDK_Delete) {
43                 return Gtk::Entry::on_key_press_event (ev);
44         } else {
45                 gdk_beep ();
46                 return FALSE;
47         }
48 }
49
50
51 void
52 HexEntry::set_hex (unsigned char *msg, unsigned int len) 
53         
54 {
55         /* create a textual representation of the MIDI message */
56         
57         if (msg && len) {
58                 char *rep;
59                 
60                 rep = new char[(len * 3) + 1];
61                 for (size_t i = 0; i < len; i++) {
62                         sprintf (&rep[i*3], "%02x ", msg[i]);
63                 }
64                 rep[len * 3] = '\0';
65                 set_text (rep);
66                 delete [] rep;
67         } else {
68                 set_text ("");
69         }
70 }
71
72 unsigned int
73 HexEntry::get_hex (unsigned char *hexbuf, size_t buflen)
74
75 {
76         int fetched_len;
77         char buf[3];
78         string text = get_text();
79         string::size_type length = text.length ();
80         string::size_type offset;
81
82         fetched_len = 0;
83         buf[2] = '\0';
84         offset = 0;
85
86         while (1) {
87                 offset = text.find_first_of ("abcdef0123456789", offset);
88
89                 if (offset == string::npos) {
90                         break;
91                 }
92
93                 /* grab two characters, but no more */
94                 
95                 buf[0] = text[offset];
96
97                 if (offset < length - 1) {
98                         buf[1] = text[offset+1];
99                         offset += 2;
100                 } else {
101                         buf[1] = '\0';
102                         offset += 1;
103                 }
104
105                 hexbuf[fetched_len++] = (char) strtol (buf, 0, 16);
106         }
107
108         return fetched_len;
109 }
110
111