File: FavoritesWindow.cxx

package info (click to toggle)
fldiff 1.1-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 536 kB
  • ctags: 224
  • sloc: cpp: 2,977; makefile: 124
file content (311 lines) | stat: -rw-r--r-- 6,078 bytes parent folder | download | duplicates (7)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
// "$Id: FavoritesWindow.cxx 386 2006-03-04 14:15:27Z mike $"
//
// FavoritesWindows widget code.
//
// Copyright 2005 by Michael Sweet.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License v2 as published
// by the Free Software Foundation.
//
// 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.
//
// Contents:
//
//

#include "FavoritesWindow.h"


//
// 'FavoritesWindow::FavoritesWindow()' - Create a favorites management window.
//

FavoritesWindow::FavoritesWindow()
  : Fl_Double_Window(355, 150, "Manage Favorites"),
    browser_(10, 10, 300, 95),
    up_(320, 10, 25, 25, "@8>"),
    delete_(320, 45, 25, 25, "X"),
    down_(320, 80, 25, 25, "@2>"),
    ok_(185, 115, 75, 25, "OK"),
    cancel_(270, 115, 75, 25, "Cancel")
{
  end();

  modal();

  browser_.type(FL_HOLD_BROWSER);
  browser_.when(FL_WHEN_RELEASE_ALWAYS);
  browser_.callback((Fl_Callback *)browser_cb, this);

  up_.callback((Fl_Callback *)up_cb, this);

  delete_.callback((Fl_Callback *)delete_cb, this);

  down_.callback((Fl_Callback *)down_cb, this);

  ok_.callback((Fl_Callback *)ok_cb, this);

  cancel_.callback((Fl_Callback *)cancel_cb, this);
}


//
// 'FavoritesWindow::browser_cb()' - Handle clicks in the favorites list.
//

void
FavoritesWindow::browser_cb(Fl_File_Browser *b,
					// I - Favorites browser
                            FavoritesWindow *fw)
					// I - Favorites window
{
  int		i;			// Current item


  i = fw->browser_.value();
  if (i)
  {
    if (i > 1)
      fw->up_.activate();
    else
      fw->up_.deactivate();

    fw->delete_.activate();

    if (i < fw->browser_.size())
      fw->down_.activate();
    else
      fw->down_.deactivate();
  }
  else
  {
    fw->up_.deactivate();
    fw->delete_.deactivate();
    fw->down_.deactivate();
  }
}


//
// 'FavoritesWindow::cancel_cb()' - Cancel (close) the window.
//

void
FavoritesWindow::cancel_cb(Fl_Button       *b,
					// I - Cancel button
                           FavoritesWindow *fw)
					// I - Favorites window
{
  fw->hide();
}


//
// 'FavoritesWindow::delete_cb()' - Delete the current directory.
//

void
FavoritesWindow::delete_cb(Fl_Button       *b,
					// I - Delete button
                           FavoritesWindow *fw)
					// I - Favorites window
{
  int		i;			// Current item


  i = fw->browser_.value();

  fw->browser_.remove(i);

  if (i > fw->browser_.size())
    i --;
  fw->browser_.select(i);

  if (i < fw->browser_.size())
    fw->down_.activate();
  else
    fw->down_.deactivate();

  if (i > 1)
    fw->up_.activate();
  else
    fw->up_.deactivate();

  if (!i)
    fw->delete_.deactivate();

  fw->ok_.activate();
}


//
// 'FavoritesWindow::down_cb()' - Move the current directory down one line.
//

void
FavoritesWindow::down_cb(Fl_Button       *b,
					// I - Down button
                         FavoritesWindow *fw)
					// I - Favorites window
{
  int		i;			// Current item


  i = fw->browser_.value();

  fw->browser_.insert(i + 2, fw->browser_.text(i), fw->browser_.data(i));
  fw->browser_.remove(i);
  fw->browser_.select(i + 1);

  if ((i + 1) == fw->browser_.size())
    fw->down_.deactivate();

  fw->up_.activate();

  fw->ok_.activate();
}


//
// 'FavoritesWindow::load()' - Load the favorites list.
//

void
FavoritesWindow::load()
{
  int		i;			// Looping var
  char		name[32],		// Preference name
		filename[1024];		// Directory in list
  Fl_Preferences prefs(Fl_Preferences::USER, "fltk.org", "filechooser");
					// File chooser preferences


  // Load the favorites list...
  browser_.clear();
  browser_.deselect();

  for (i = 0; i < 100; i ++)
  {
    // Get favorite directory 0 to 99...
    sprintf(name, "favorite%02d", i);

    prefs.get(name, filename, "", sizeof(filename));

    // Stop on the first empty favorite...
    if (!filename[0])
      break;

    // Add the favorite to the list...
    browser_.add(filename,
                 Fl_File_Icon::find(filename, Fl_File_Icon::DIRECTORY));
  }

  up_.deactivate();
  delete_.deactivate();
  down_.deactivate();
  ok_.deactivate();
}


//
// 'FavoritesWindow::ok_cb()' - Save the favorites list and close.
//

void
FavoritesWindow::ok_cb(Fl_Return_Button *b,
					// I - OK button
                       FavoritesWindow  *fw)
					// I - Favorites window
{
  fw->save();
  fw->hide();
}


//
// 'FavoritesWindow::save()' - Save the favorites list.
//

void
FavoritesWindow::save()
{
  int		i;			// Looping var
  char		name[32],		// Preference name
		filename[1024];		// Directory in list
  Fl_Preferences prefs(Fl_Preferences::USER, "fltk.org", "filechooser");
					// File chooser preferences


  // Copy the new list over...
  for (i = 0; i < browser_.size(); i ++)
  {
    // Set favorite directory 0 to 99...
    sprintf(name, "favorite%02d", i);

    prefs.set(name, browser_.text(i + 1));
  }

  // Clear old entries as necessary...
  for (; i < 100; i ++)
  {
    // Clear favorite directory 0 to 99...
    sprintf(name, "favorite%02d", i);

    prefs.get(name, filename, "", sizeof(filename));

    if (filename[0])
      prefs.set(name, "");
    else
      break;
  }
}


//
// 'FavoritesWindow::show()' - Load the favorites list and show.
//

void
FavoritesWindow::show()
{
  load();
  Fl_Double_Window::show();
}


//
// 'FavoritesWindow::up_cb()' - Move the current directory up one line.
//

void
FavoritesWindow::up_cb(Fl_Button       *b,
					// I - Up button
                       FavoritesWindow *fw)
					// I - Favorites window
{
  int		i;			// Current item


  i = fw->browser_.value();

  fw->browser_.insert(i - 1, fw->browser_.text(i), fw->browser_.data(i));
  fw->browser_.remove(i + 1);
  fw->browser_.select(i - 1);

  if (i == 2)
    fw->up_.deactivate();

  fw->down_.activate();

  fw->ok_.activate();
}


//
// End of "$Id: FavoritesWindow.cxx 386 2006-03-04 14:15:27Z mike $".
//