source: trunk/Lucide/SOURCE/gui/recent.cpp @ 242

Last change on this file since 242 was 229, checked in by Eugene Romanenko, 14 years ago

List of recent files

File size: 5.3 KB
RevLine 
[229]1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34#define INCL_WIN
35#include <os2.h>
36
37#include <vector>
38#include <string>
39#include <stdio.h>
40#include <string.h>
41
42#include "recent.h"
43#include "globals.h"
44#include "luutils.h"
45#include "Lucide_res.h"
46#include "messages.h"
47
48const char * const sRecentFiles = "RecentFiles";
49
50
51RecentFiles::RecentFiles( HWND hMenu )
52{
53    hWndMenu = hMenu;
54
55    MENUITEM mi = { 0 };
56    WinSendMsg( hWndMenu, MM_QUERYITEM, MPFROM2SHORT( CM_RECENT, TRUE ), (MPARAM)&mi );
57    hSubMenu = mi.hwndSubMenu;
58
59    files = new std::vector<std::string>;
60    clearList = newstrdupL( "MENU_CLEAR_LIST" );
61    load();
62
63    if ( files->size() == 0 ) {
64        clearMenu();
65    }
66}
67
68RecentFiles::~RecentFiles()
69{
70    delete files;
71    delete clearList;
72}
73
74void RecentFiles::addFile( const char *f, bool atEnd )
75{
76    // if such item exist - erase it
77    std::vector<std::string>::iterator iter;
78    for ( iter = files->begin(); iter != files->end(); iter++ ) {
79        if ( stricmp( (*iter).c_str(), f ) == 0 ) {
80            files->erase( iter );
81            break;
82        }
83    }
84
85    if ( atEnd ) {
86        files->push_back(  f );
87    }
88    else {
89        // put to beginning
90        files->insert( files->begin(), f );
91    }
92
93    // shrink to RECENT_SIZE
94    while ( files->size() > RECENT_SIZE ) {
95        files->pop_back();
96    }
97
98    fillSubmenu();
99}
100
101void RecentFiles::clear()
102{
103    files->clear();
104    clearMenu();
105}
106
107void RecentFiles::fillSubmenu()
108{
109    clearMenu();
110
111    MENUITEM mi = { 0 };
112    int id = 1;
113    std::vector<std::string>::const_iterator iter;
114    for ( iter = files->begin(); iter != files->end(); iter++ )
115    {
116        char txt[ CCHMAXPATH + 10 ];
117        snprintf( txt, sizeof( txt ), "~%d  %s", id, (*iter).c_str() );
118
119        mi.iPosition   = MIT_END;
120        mi.afStyle     = MIS_TEXT;
121        mi.afAttribute = 0;
122        mi.id          = CM_RECENT + id++;
123        mi.hwndSubMenu = NULLHANDLE;
124        mi.hItem       = 0;
125        WinSendMsg( hSubMenu, MM_INSERTITEM, (MPARAM)&mi, MPFROMP( txt ) );
126    }
127
128    if ( files->size() > 0 )
129    {
130        WinEnableMenuItem( hWndMenu, CM_RECENT, TRUE );
131
132        mi.iPosition   = MIT_END;
133        mi.afStyle     = MIS_SEPARATOR;
134        mi.afAttribute = 0;
135        mi.id          = CM_RECENT + id++;
136        mi.hwndSubMenu = NULLHANDLE;
137        mi.hItem       = 0;
138        WinSendMsg( hSubMenu, MM_INSERTITEM, (MPARAM)&mi, MPFROMP( "" ) );
139
140        mi.iPosition   = MIT_END;
141        mi.afStyle     = MIS_TEXT;
142        mi.afAttribute = 0;
143        mi.id          = CM_RECENTCLEAR;
144        mi.hwndSubMenu = NULLHANDLE;
145        mi.hItem       = 0;
146        WinSendMsg( hSubMenu, MM_INSERTITEM, (MPARAM)&mi, MPFROMP( clearList ) );
147    }
148}
149
150void RecentFiles::load()
151{
152    int bufsize = ( CCHMAXPATH * RECENT_SIZE ) + RECENT_SIZE + 2;
153    char *buf = new char[ bufsize ];
154    memset( buf, 0, bufsize );
155
156    PrfQueryProfileString( HINI_USERPROFILE, appName, sRecentFiles, "", buf, bufsize );
157
158    char *p = strtok( buf, "|" );
159    while ( p != NULL ) {
160        addFile( p, true );
161        p = strtok( NULL, "|" );
162    }
163
164    delete buf;
165}
166
167void RecentFiles::save()
168{
169    std::string s = "";
170    std::vector<std::string>::const_iterator iter;
171    for ( iter = files->begin(); iter != files->end(); iter++ )
172    {
173        s += *iter;
174        s += '|';
175    }
176
177    PrfWriteProfileString( HINI_USERPROFILE, appName, sRecentFiles, s.c_str() );
178}
179
180
181void RecentFiles::clearMenu()
182{
183    int id = CM_RECENT + 1;
184    while ( WinSendMsg( hSubMenu, MM_DELETEITEM,
185                        MPFROM2SHORT( id++, TRUE ), MPVOID ) != 0 );
186    WinEnableMenuItem( hWndMenu, CM_RECENT, FALSE );
187}
188
189std::string RecentFiles::getFile( USHORT cmd )
190{
191    cmd = ( cmd - CM_RECENT ) - 1;
192    return (*files)[ cmd ];
193}
194
Note: See TracBrowser for help on using the repository browser.