1 //
2 // "$Id: FavoritesMenu.cxx 386 2006-03-04 14:15:27Z mike $"
3 //
4 // FavoritesMenu widget code.
5 //
6 // Copyright 2005 by Michael Sweet.
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License v2 as published
10 // by the Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Contents:
18 //
19 // FavoritesMenu::add_favorite() - Add saved directory.
20 // FavoritesMenu::handle() - Handle UI events.
21 // Favorites::load_menu() - Load the current directory favorites.
22 // FavoritesMenu::quote() - Quote a filename for a menu item.
23 // FavoritesMenu::unquote() - Unquote a pathname from a menu.
24 //
25
26 #include "FavoritesMenu.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31
32 //
33 // 'FavoritesMenu::add_favorite()' - Add saved directory.
34 //
35
36 void
add_favorite(const char * d)37 FavoritesMenu::add_favorite(const char *d)
38 // I - Directory to add
39 {
40 int i; // Looping var
41 char name[255], // Preference name
42 filename[1024]; // Filename
43 Fl_Preferences prefs(Fl_Preferences::USER, "fltk.org", "filechooser");
44 // File chooser preferences
45
46
47 // See if directory is already in favorites...
48 for (i = 0; i < 100; i ++)
49 {
50 sprintf(name, "favorite%02d", i);
51
52 prefs.get(name, filename, "", sizeof(filename));
53
54 if (!filename[0])
55 break;
56
57 if (!strcmp(filename, d))
58 return;
59 }
60
61 prefs.set(name, d);
62 }
63
64
65 //
66 // 'FavoritesMenu::handle()' - Handle UI events.
67 //
68
69 int // O - 1 if handled, 0 otherwise
handle(int event)70 FavoritesMenu::handle(int event) // I - Event
71 {
72 // Reload the menu when a mouse button or key is pressed...
73 if (event == FL_PUSH || event == FL_SHORTCUT)
74 load_menu();
75
76 // Now pass the event to the menu button widget...
77 return (Fl_Menu_Button::handle(event));
78 }
79
80
81 //
82 // 'Favorites::load_menu()' - Load the current directory favorites.
83 //
84
85 void
load_menu()86 FavoritesMenu::load_menu()
87 {
88 int i; // Looping var
89 char name[255], // Preference name
90 filename[1024], // Filename
91 menuitem[2048]; // Menu item
92 const char *home; // Home directory
93 Fl_Preferences prefs(Fl_Preferences::USER, "fltk.org", "filechooser");
94 // File chooser preferences
95
96
97 // Clear the list
98 clear();
99 add("Add to Favorites", FL_ALT + 'a', 0);
100 add("Manage Favorites", FL_ALT + 'm', 0, 0, FL_MENU_DIVIDER);
101 add("Filesystems", FL_ALT + 'f', 0);
102
103 if ((home = getenv("HOME")) != NULL)
104 {
105 quote(menuitem, home, sizeof(menuitem));
106 add(menuitem, FL_ALT + 'h', 0);
107 }
108
109 for (i = 0; i < 100; i ++)
110 {
111 sprintf(name, "favorite%02d", i);
112 prefs.get(name, filename, "", sizeof(filename));
113 if (!filename[0]) break;
114
115 quote(menuitem, filename, sizeof(menuitem));
116
117 add(menuitem, i < 10 ? FL_ALT + '0' + i : 0, 0);
118 }
119
120 if (i == 100)
121 ((Fl_Menu_Item *)menu())[0].deactivate();
122 }
123
124
125 //
126 // 'FavoritesMenu::quote()' - Quote a filename for a menu item.
127 //
128
129 void
quote(char * dst,const char * src,int dstsize)130 FavoritesMenu::quote(char *dst, // O - Destination string
131 const char *src, // I - Source string
132 int dstsize)// I - Size of destination string
133 {
134 dstsize --;
135
136 while (*src && dstsize > 1)
137 {
138 if (*src == '\\')
139 {
140 // Convert backslash to forward slash...
141 *dst++ = '\\';
142 *dst++ = '/';
143 src ++;
144 }
145 else if (*src == '@')
146 {
147 // Quote @...
148 *dst++ = '@';
149 *dst++ = *src++;
150 }
151 else if (*src == '/')
152 {
153 // Quote /...
154 *dst++ = '\\';
155 *dst++ = *src++;
156 }
157 else
158 *dst++ = *src++;
159 }
160
161 *dst = '\0';
162 }
163
164
165 //
166 // 'FavoritesMenu::unquote()' - Unquote a pathname from a menu.
167 //
168
169 void
unquote(char * dst,const char * src,int dstsize)170 FavoritesMenu::unquote(char *dst, // O - Destination string
171 const char *src, // I - Source string
172 int dstsize)
173 // I - Size of destination string
174 {
175 dstsize --;
176
177 while (*src && dstsize > 1)
178 {
179 if (*src == '\\' || *src == '@')
180 src ++;
181
182 *dst++ = *src++;
183 }
184
185 *dst = '\0';
186 }
187
188
189 //
190 // End of "$Id: FavoritesMenu.cxx 386 2006-03-04 14:15:27Z mike $".
191 //
192