File: FXRecentFiles.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (237 lines) | stat: -rw-r--r-- 7,507 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
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
/********************************************************************************
*                                                                               *
*                     R e c e n t   F i l e s   L i s t                         *
*                                                                               *
*********************************************************************************
* Copyright (C) 1998,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* 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 3 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 program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXRecentFiles.h"



/*
  Notes:
  - Use the auto-hide or auto-gray feature to hide menus which are connected
    to the FXRecentFiles class.
  - Default constructor is deprecated in applications; used only for serialization.
*/


using namespace FX;

/*******************************************************************************/

namespace FX {


// Up to 32 files kepts
const FXchar FXRecentFiles::key[32][7]={
  {"FILE1"}, {"FILE2"}, {"FILE3"}, {"FILE4"}, {"FILE5"}, {"FILE6"}, {"FILE7"}, {"FILE8"},
  {"FILE9"}, {"FILE10"},{"FILE11"},{"FILE12"},{"FILE13"},{"FILE14"},{"FILE15"},{"FILE16"},
  {"FILE17"},{"FILE18"},{"FILE19"},{"FILE20"},{"FILE21"},{"FILE22"},{"FILE23"},{"FILE24"},
  {"FILE25"},{"FILE26"},{"FILE27"},{"FILE28"},{"FILE29"},{"FILE30"},{"FILE31"},{"FILE32"}
  };



// Furnish our own version
extern FXAPI FXint __snprintf(FXchar* string,FXint length,const FXchar* format,...);


// Message map
FXDEFMAP(FXRecentFiles) FXRecentFilesMap[] = {
  FXMAPFUNC(SEL_UPDATE,FXRecentFiles::ID_ANYFILES,FXRecentFiles::onUpdAnyFiles),
  FXMAPFUNC(SEL_UPDATE,FXRecentFiles::ID_CLEAR,FXRecentFiles::onUpdAnyFiles),
  FXMAPFUNC(SEL_COMMAND,FXRecentFiles::ID_CLEAR,FXRecentFiles::onCmdClear),
  FXMAPFUNCS(SEL_COMMAND,FXRecentFiles::ID_FILE_1,FXRecentFiles::ID_FILE_32,FXRecentFiles::onCmdFile),
  FXMAPFUNCS(SEL_UPDATE,FXRecentFiles::ID_FILE_1,FXRecentFiles::ID_FILE_32,FXRecentFiles::onUpdFile),
  };


// Class implementation
FXIMPLEMENT(FXRecentFiles,FXObject,FXRecentFilesMap,ARRAYNUMBER(FXRecentFilesMap))


// Serialization
FXRecentFiles::FXRecentFiles():settings(nullptr),target(nullptr),message(0),maxfiles(10){
  }


// Make new Recent Files group
FXRecentFiles::FXRecentFiles(FXApp* a,const FXString& gp,FXObject *tgt,FXSelector sel):settings(&a->reg()),target(tgt),message(sel),group(gp),maxfiles(10){
  }


// Make new Recent Files group
FXRecentFiles::FXRecentFiles(FXSettings* st,const FXString& gp,FXObject *tgt,FXSelector sel):settings(st),target(tgt),message(sel),group(gp),maxfiles(10){
  }



// Change number of files we're tracking
void FXRecentFiles::setMaxFiles(FXuint mx){
  maxfiles=FXCLAMP(1,mx,ARRAYNUMBER(key));
  }


// Obtain the filename at index
FXString FXRecentFiles::getFile(FXuint index) const {
  return settings->readStringEntry(group,key[index],FXString::null);
  }


// Change the filename at index
void FXRecentFiles::setFile(FXuint index,const FXString& filename){
  if(!filename.empty()){
    settings->writeStringEntry(group,key[index],filename.text());
    }
  }


// Append a file; its added to the top of the list, and everything else
// is moved down the list one notch; the last one is dropped from the list.
void FXRecentFiles::appendFile(const FXString& filename){
  if(!filename.empty()){
    FXString newname=filename;
    FXString oldname;
    FXuint i=0;
    FXuint j=0;
    do{
      do{ oldname=settings->readStringEntry(group,key[j++],nullptr); }while(oldname==filename);
      settings->writeStringEntry(group,key[i],newname.text());
      if(oldname.empty()) break;
      newname=oldname;
      }
    while(++i<maxfiles);
    }
  }


// Remove a file
void FXRecentFiles::removeFile(const FXString& filename){
  if(!filename.empty()){
    FXString name;
    FXuint i=0;
    FXuint j=0;
    do{
      name=settings->readStringEntry(group,key[i],nullptr);
      if(name.empty()) break;
      if(name!=filename){
        settings->writeStringEntry(group,key[j++],name.text());
        }
      }
    while(++i<maxfiles);
    settings->deleteEntry(group,key[j++]);
    }
  }


// Remove all files from the list
void FXRecentFiles::clear(){
  settings->deleteSection(group);
  }


// Clear the files list
long FXRecentFiles::onCmdClear(FXObject*,FXSelector,void*){
  clear();
  return 1;
  }


// User clicks on one of the file names
long FXRecentFiles::onCmdFile(FXObject*,FXSelector sel,void*){
  const FXchar *filename=settings->readStringEntry(group,key[FXSELID(sel)-ID_FILE_1],nullptr);
  if(filename){
    if(target){ target->handle(this,FXSEL(SEL_COMMAND,message),(void*)filename); }
    }
  return 1;
  }


// Update handler for same
long FXRecentFiles::onUpdFile(FXObject *sender,FXSelector sel,void*){
  const FXchar *filename=settings->readStringEntry(group,key[FXSELID(sel)-ID_FILE_1],nullptr);
  if(filename){
    FXint which=FXSELID(sel)-ID_FILE_1+1;
    FXString string;
    string.format("%d %s",which,filename);
    sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETSTRINGVALUE),(void*)&string);
    sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SHOW),nullptr);
    }
  else{
    sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_HIDE),nullptr);
    }
  return 1;
  }


// Show or hide depending on whether there are any files
long FXRecentFiles::onUpdAnyFiles(FXObject *sender,FXSelector,void*){
  if(settings->readStringEntry(group,key[0],nullptr))
    sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SHOW),nullptr);
  else
    sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_HIDE),nullptr);
  return 1;
  }


// Save data
void FXRecentFiles::save(FXStream& store) const {
  FXObject::save(store);
//  store << settings;
  store << target;
  store << message;
  store << group;
  store << maxfiles;
  }


// Load data
void FXRecentFiles::load(FXStream& store){
  FXObject::load(store);
//  store >> settings;
  store >> target;
  store >> message;
  store >> group;
  store >> maxfiles;
  }


// Destructor
FXRecentFiles::~FXRecentFiles(){
  target=(FXObject*)-1L;
  }

}