-rw-r--r-- | core/launcher/startmenu.cpp | 253 | ||||
-rw-r--r-- | core/launcher/startmenu.h | 21 |
2 files changed, 170 insertions, 104 deletions
diff --git a/core/launcher/startmenu.cpp b/core/launcher/startmenu.cpp index 08ae885..f17c7f8 100644 --- a/core/launcher/startmenu.cpp +++ b/core/launcher/startmenu.cpp | |||
@@ -18,6 +18,11 @@ | |||
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | // TODO. During startup | ||
22 | // Launcher::typeAdded | ||
23 | // is called for each new tab and calls then each time the refresh of startmenu | ||
24 | // suboptimal | ||
25 | |||
21 | #define INCLUDE_MENUITEM_DEF | 26 | #define INCLUDE_MENUITEM_DEF |
22 | 27 | ||
23 | #include "startmenu.h" | 28 | #include "startmenu.h" |
@@ -32,9 +37,13 @@ | |||
32 | 37 | ||
33 | #include <qdict.h> | 38 | #include <qdict.h> |
34 | #include <qdir.h> | 39 | #include <qdir.h> |
35 | #include <qpainter.h> | 40 | //#include <qpainter.h> |
41 | |||
42 | //#include <stdlib.h> | ||
43 | |||
36 | 44 | ||
37 | #include <stdlib.h> | 45 | #define APPLNK_ID_OFFSET 250 |
46 | #define NO_ID -1 | ||
38 | 47 | ||
39 | 48 | ||
40 | void StartPopupMenu::keyPressEvent( QKeyEvent *e ) | 49 | void StartPopupMenu::keyPressEvent( QKeyEvent *e ) |
@@ -60,6 +69,7 @@ StartMenu::StartMenu(QWidget *parent) : QLabel( parent ) | |||
60 | setPixmap(pm); | 69 | setPixmap(pm); |
61 | setFocusPolicy( NoFocus ); | 70 | setFocusPolicy( NoFocus ); |
62 | 71 | ||
72 | useWidePopupMenu = true; | ||
63 | launchMenu = 0; | 73 | launchMenu = 0; |
64 | refreshMenu(); | 74 | refreshMenu(); |
65 | } | 75 | } |
@@ -85,116 +95,168 @@ void StartMenu::createMenu() | |||
85 | loadMenu( launchMenu ); | 95 | loadMenu( launchMenu ); |
86 | loadApplets(); | 96 | loadApplets(); |
87 | 97 | ||
88 | bool result = nother || ntabs || m_applets.count(); | 98 | bool result = currentItem || menuApplets.count(); |
89 | if ( result ) | 99 | if ( result ) |
90 | connect( launchMenu, SIGNAL(activated(int)), SLOT(itemSelected(int)) ); | 100 | connect( launchMenu, SIGNAL(activated(int)), SLOT(itemSelected(int)) ); |
91 | |||
92 | } | 101 | } |
93 | 102 | ||
94 | void StartMenu::refreshMenu() | 103 | void StartMenu::refreshMenu() |
95 | { | 104 | { |
96 | Config cfg("Taskbar"); | 105 | Config cfg( "StartMenu" ); |
97 | cfg.setGroup("Menu"); | 106 | cfg.setGroup("Menu"); |
98 | bool ltabs = cfg.readBoolEntry("LauncherTabs",TRUE); | 107 | bool ltabs = cfg.readBoolEntry("LauncherTabs",TRUE); |
99 | bool lot = cfg.readBoolEntry("LauncherOther",TRUE); | 108 | bool lot = cfg.readBoolEntry("LauncherOther",TRUE); |
100 | bool lt = ltabs || lot; | 109 | useWidePopupMenu = cfg.readBoolEntry( "LauncherSubPopup", TRUE ); |
101 | if ( launchMenu && !lt ) | 110 | |
102 | return; // nothing to do | 111 | if ( launchMenu && !(ltabs || lot) ) return; // nothing to do |
103 | 112 | ||
104 | if ( launchMenu ) { | ||
105 | int i; | ||
106 | /* find the first entry we want to remove */ | ||
107 | for (i=0; i<(int)launchMenu->count(); i++) { | ||
108 | QMenuItem* item = launchMenu->findItem(launchMenu->idAt(i)); | ||
109 | if ( item && item->id() >= 0 && item->id() < ntabs ) { | ||
110 | break; | ||
111 | } | ||
112 | if ( item && item->isSeparator() ) { | ||
113 | i++; | ||
114 | break; | ||
115 | } | ||
116 | } | ||
117 | /* remove them */ | ||
118 | while (i<(int)launchMenu->count()) | ||
119 | launchMenu->removeItemAt(i); | ||
120 | loadMenu(launchMenu); | ||
121 | addApplets(launchMenu); | ||
122 | } else { | ||
123 | createMenu(); | 113 | createMenu(); |
124 | } | 114 | } |
125 | } | ||
126 | 115 | ||
127 | void StartMenu::itemSelected( int id ) | 116 | void StartMenu::itemSelected( int id ) |
128 | { | 117 | { |
129 | if ( id >= 0 && id < ntabs ) { | 118 | if ( id == NO_ID ) return; |
130 | emit tabSelected(tabs[id]); | 119 | |
131 | } else if ( id >= 20 && id < 20+nother ) { | 120 | if ( id < 0 ) { |
132 | other.at(id-20)->execute(); | 121 | MenuApplet *applet = menuApplets.find( id ); |
133 | }else { | ||
134 | MenuApplet *applet = m_applets.find ( id ); | ||
135 | if ( applet ) { | 122 | if ( applet ) { |
136 | qWarning("activated"); | ||
137 | applet-> iface-> activated(); | 123 | applet-> iface-> activated(); |
138 | } | 124 | } |
125 | } else if ( id >= APPLNK_ID_OFFSET ) { | ||
126 | AppLnk * appLnk = appLnks.find( id ); | ||
127 | if ( appLnk ) { | ||
128 | appLnk->execute(); | ||
129 | } | ||
130 | } else { | ||
131 | QString *tabName = tabNames.find( id ); | ||
132 | if ( tabName ) { | ||
133 | emit tabSelected( *tabName ); | ||
134 | } | ||
139 | } | 135 | } |
140 | } | 136 | } |
141 | 137 | ||
142 | bool StartMenu::loadMenu( QPopupMenu *menu ) | 138 | void StartMenu::createAppEntry( QPopupMenu *menu, QDir dir, QString file ) |
143 | { | 139 | { |
144 | Config cfg("Taskbar"); | 140 | if ( file.right(8) == ".desktop" ) { |
145 | cfg.setGroup("Menu"); | 141 | AppLnk* applnk = new AppLnk( dir.path() + "/" + file ); |
146 | 142 | if ( !applnk->isValid() ) { | |
147 | bool ltabs = cfg.readBoolEntry("LauncherTabs",TRUE); | 143 | delete applnk; |
148 | bool lot = cfg.readBoolEntry("LauncherOther",TRUE); | 144 | return; |
149 | bool sepfirst = !ltabs && !lot; | 145 | } |
150 | |||
151 | tabs.clear(); | ||
152 | other.setAutoDelete(TRUE); | ||
153 | other.clear(); | ||
154 | ntabs = 0; | ||
155 | nother = 0; | ||
156 | 146 | ||
157 | bool f=TRUE; | ||
158 | if ( ltabs || lot ) { | ||
159 | QDir dir( MimeType::appsFolderName(), QString::null, QDir::Name ); | ||
160 | for (int i=0; i<(int)dir.count(); i++) { | ||
161 | QString d = dir[i]; | ||
162 | Config cfg(dir.path()+"/"+d+"/.directory",Config::File); | ||
163 | if ( cfg.isValid() ) { | ||
164 | QString nm = cfg.readEntry("Name"); | ||
165 | QString ic = cfg.readEntry("Icon"); | ||
166 | if ( !!nm && !!ic ) { | ||
167 | tabs.append(d); | ||
168 | menu->insertItem( Resource::loadIconSet(ic), nm, ntabs++ ); | ||
169 | } | ||
170 | } else if ( lot && d.right(8)==".desktop") { | ||
171 | AppLnk* applnk = new AppLnk(dir.path()+"/"+d); | ||
172 | if ( applnk->isValid() ) { | ||
173 | if ( applnk->type() == "Separator" ) { // No tr | 147 | if ( applnk->type() == "Separator" ) { // No tr |
174 | if ( lot ) { | ||
175 | menu->insertSeparator(); | 148 | menu->insertSeparator(); |
176 | sepfirst = f && !ltabs; | ||
177 | } | ||
178 | delete applnk; | 149 | delete applnk; |
179 | } else { | 150 | } else { |
180 | f = FALSE; | 151 | QPixmap test; |
181 | other.append(applnk); | 152 | test.convertFromImage( |
182 | menu->insertItem( Resource::loadIconSet(applnk->icon()), | 153 | Resource::loadImage( applnk->icon() ).smoothScale( |
183 | applnk->name(), 20+nother++ ); | 154 | AppLnk::smallIconSize(), AppLnk::smallIconSize() ), 0 ); |
155 | |||
156 | menu->insertItem( test, applnk->name(), | ||
157 | currentItem + APPLNK_ID_OFFSET ); | ||
158 | appLnks.insert( currentItem + APPLNK_ID_OFFSET, applnk ); | ||
159 | currentItem++; | ||
160 | } | ||
184 | } | 161 | } |
162 | |||
163 | } | ||
164 | |||
165 | void StartMenu::createDirEntry( QPopupMenu *menu, QDir dir, QString file, bool lot ) | ||
166 | { | ||
167 | // do some sanity checks and collect information | ||
168 | |||
169 | if ( file == "." || file == ".." ) return; | ||
170 | |||
171 | Config cfg( dir.path() + "/" + file + "/.directory", Config::File ); | ||
172 | if ( !cfg.isValid() ) return; | ||
173 | |||
174 | QString name = cfg.readEntry( "Name" ); | ||
175 | QString icon = cfg.readEntry( "Icon" ); | ||
176 | if ( !name || !icon ) return; | ||
177 | |||
178 | QDir subdir = QDir( dir ); | ||
179 | subdir.cd( file ); | ||
180 | subdir.setFilter( QDir::Files ); | ||
181 | subdir.setNameFilter( "*.desktop" ); | ||
182 | // we don' t show the menu if there are no entries | ||
183 | // perhaps one should check if there exist subsubdirs with entries... | ||
184 | if ( subdir.entryList().isEmpty() ) return; | ||
185 | |||
186 | // checks were ok | ||
187 | |||
188 | QPixmap test; | ||
189 | test.convertFromImage( Resource::loadImage( icon ).smoothScale( | ||
190 | AppLnk::smallIconSize(), AppLnk::smallIconSize() ), 0 ); | ||
191 | |||
192 | if ( useWidePopupMenu ) { | ||
193 | // generate submenu | ||
194 | QPopupMenu *submenu = new QPopupMenu( menu ); | ||
195 | connect( submenu, SIGNAL(activated(int)), SLOT(itemSelected(int)) ); | ||
196 | menu->insertItem( test, name, submenu, NO_ID ); | ||
197 | |||
198 | // ltabs is true cause else we wouldn't stuck around.. | ||
199 | createMenuEntries( submenu, subdir, true, lot ); | ||
185 | } else { | 200 | } else { |
186 | delete applnk; | 201 | // no submenus - just bring corresponding tab to front |
202 | menu->insertItem( test, name, currentItem ); | ||
203 | tabNames.insert( currentItem, new QString( file ) ); | ||
204 | currentItem++; | ||
187 | } | 205 | } |
188 | } | 206 | } |
207 | |||
208 | void StartMenu::createMenuEntries( QPopupMenu *menu, QDir dir, bool ltabs, bool lot ) | ||
209 | { | ||
210 | if ( lot ) { | ||
211 | dir.setFilter( QDir::Files ); | ||
212 | dir.setNameFilter( "*.desktop" ); | ||
213 | QStringList files = dir.entryList(); | ||
214 | files.sort(); | ||
215 | |||
216 | for ( QStringList::Iterator it = files.begin(); it != files.end(); it++ ) { | ||
217 | createAppEntry( menu, dir, *it ); | ||
218 | } | ||
189 | } | 219 | } |
220 | if ( ltabs ) { | ||
221 | dir.setNameFilter( "*" ); | ||
222 | dir.setFilter( QDir::Dirs ); | ||
223 | QStringList dirs = dir.entryList(); | ||
224 | dirs.sort(); | ||
190 | 225 | ||
191 | if ( !menu->count() ) | 226 | for ( QStringList::Iterator it = dirs.begin(); it != dirs.end(); it++ ) { |
192 | sepfirst = TRUE; | 227 | createDirEntry( menu, dir, *it, lot ); |
193 | } | 228 | } |
229 | } | ||
230 | } | ||
231 | |||
232 | bool StartMenu::loadMenu( QPopupMenu *menu ) | ||
233 | { | ||
234 | Config cfg("StartMenu"); | ||
235 | cfg.setGroup("Menu"); | ||
236 | |||
237 | bool ltabs = cfg.readBoolEntry("LauncherTabs", TRUE); | ||
238 | bool lot = cfg.readBoolEntry("LauncherOther", TRUE); | ||
239 | useWidePopupMenu = cfg.readBoolEntry( "LauncherSubPopup", TRUE ); | ||
240 | bool sepfirst = !ltabs && !lot; | ||
241 | |||
242 | currentItem = 0; | ||
243 | launchMenu->clear(); | ||
244 | |||
245 | appLnks.setAutoDelete( true ); | ||
246 | tabNames.setAutoDelete( true ); | ||
247 | appLnks.clear(); | ||
248 | tabNames.clear(); | ||
249 | appLnks.setAutoDelete( false ); | ||
250 | tabNames.setAutoDelete( false ); | ||
251 | |||
252 | QDir dir( MimeType::appsFolderName(), QString::null, QDir::Name ); | ||
253 | createMenuEntries( menu, dir, ltabs, lot ); | ||
254 | |||
255 | if ( !menu->count() ) sepfirst = TRUE; | ||
194 | 256 | ||
195 | launchMenu->setName(sepfirst ? "accessories" : "accessories_need_sep"); // No tr | 257 | launchMenu->setName(sepfirst ? "accessories" : "accessories_need_sep"); // No tr |
196 | 258 | ||
197 | return (nother || ntabs ); | 259 | return currentItem; |
198 | } | 260 | } |
199 | 261 | ||
200 | 262 | ||
@@ -211,7 +273,7 @@ void StartMenu::launch() | |||
211 | 273 | ||
212 | 274 | ||
213 | 275 | ||
214 | static int compareAppletPositions(const void *a, const void *b) | 276 | static int compareAppletPositions(const void *b, const void *a) |
215 | { | 277 | { |
216 | const MenuApplet* aa = *(const MenuApplet**)a; | 278 | const MenuApplet* aa = *(const MenuApplet**)a; |
217 | const MenuApplet* ab = *(const MenuApplet**)b; | 279 | const MenuApplet* ab = *(const MenuApplet**)b; |
@@ -225,7 +287,7 @@ void StartMenu::clearApplets() | |||
225 | if (launchMenu ) | 287 | if (launchMenu ) |
226 | launchMenu-> hide(); | 288 | launchMenu-> hide(); |
227 | 289 | ||
228 | for ( QIntDictIterator<MenuApplet> it ( m_applets ); it. current ( ); ++it ) { | 290 | for ( QIntDictIterator<MenuApplet> it( menuApplets ); it.current(); ++it ) { |
229 | MenuApplet *applet = it. current ( ); | 291 | MenuApplet *applet = it. current ( ); |
230 | if ( launchMenu ) { | 292 | if ( launchMenu ) { |
231 | launchMenu-> removeItem ( applet-> id ); | 293 | launchMenu-> removeItem ( applet-> id ); |
@@ -236,7 +298,7 @@ void StartMenu::clearApplets() | |||
236 | applet-> library-> unload(); | 298 | applet-> library-> unload(); |
237 | delete applet-> library; | 299 | delete applet-> library; |
238 | } | 300 | } |
239 | m_applets.clear(); | 301 | menuApplets.clear(); |
240 | } | 302 | } |
241 | 303 | ||
242 | 304 | ||
@@ -256,11 +318,7 @@ void StartMenu::loadApplets() | |||
256 | 318 | ||
257 | QString lang = getenv( "LANG" ); | 319 | QString lang = getenv( "LANG" ); |
258 | QString path = QPEApplication::qpeDir() + "/plugins/applets"; | 320 | QString path = QPEApplication::qpeDir() + "/plugins/applets"; |
259 | #ifdef Q_OS_MACX | ||
260 | QDir dir( path, "lib*.dylib" ); | ||
261 | #else | ||
262 | QDir dir( path, "lib*.so" ); | 321 | QDir dir( path, "lib*.so" ); |
263 | #endif /* Q_OS_MACX */ | ||
264 | QStringList list = dir.entryList(); | 322 | QStringList list = dir.entryList(); |
265 | QStringList::Iterator it; | 323 | QStringList::Iterator it; |
266 | int napplets=0; | 324 | int napplets=0; |
@@ -289,26 +347,21 @@ void StartMenu::loadApplets() | |||
289 | } | 347 | } |
290 | } | 348 | } |
291 | cfg.writeEntry( "ExcludeApplets", exclude, ',' ); | 349 | cfg.writeEntry( "ExcludeApplets", exclude, ',' ); |
292 | qsort(xapplets,napplets,sizeof(m_applets[0]),compareAppletPositions); | 350 | qsort(xapplets, napplets, sizeof(menuApplets[0]), compareAppletPositions); |
293 | 351 | ||
294 | 352 | ||
295 | int foo = ( launchMenu-> count ( )) ? launchMenu-> insertSeparator ( ) : 0; | ||
296 | |||
297 | while (napplets--) { | 353 | while (napplets--) { |
298 | MenuApplet *applet = xapplets[napplets]; | 354 | MenuApplet *applet = xapplets[napplets]; |
299 | 355 | ||
300 | applet-> popup = applet-> iface-> popup ( this ); | 356 | applet-> popup = applet-> iface-> popup ( this ); |
301 | 357 | ||
302 | if ( applet-> popup ) | 358 | // menuApplets got an id < -1 |
303 | applet-> id = launchMenu-> insertItem ( applet-> iface-> icon ( ), applet-> iface-> text ( ), applet-> popup ); | 359 | menuApplets.insert( -( currentItem + 2 ), new MenuApplet( *applet ) ); |
304 | else | 360 | currentItem++; |
305 | applet-> id = launchMenu-> insertItem ( applet-> iface-> icon ( ), applet-> iface-> text ( ) ); | ||
306 | |||
307 | |||
308 | m_applets.insert ( applet-> id, new MenuApplet(*applet)); | ||
309 | } | 361 | } |
310 | delete [] xapplets; | 362 | delete [] xapplets; |
311 | 363 | ||
364 | addApplets( launchMenu ); | ||
312 | } | 365 | } |
313 | 366 | ||
314 | 367 | ||
@@ -320,18 +373,20 @@ void StartMenu::addApplets(QPopupMenu* pop) { | |||
320 | if( pop-> count ( )) | 373 | if( pop-> count ( )) |
321 | pop-> insertSeparator ( ); | 374 | pop-> insertSeparator ( ); |
322 | 375 | ||
323 | for ( QIntDictIterator<MenuApplet> it ( m_applets ); it. current ( ); ++it ) { | 376 | for ( QIntDictIterator<MenuApplet> it( menuApplets ); it.current(); ++it ) { |
324 | MenuApplet *applet = it. current ( ); | 377 | MenuApplet *applet = it. current ( ); |
325 | if ( applet-> popup ) | 378 | if ( applet-> popup ) |
326 | applet-> id = pop-> insertItem ( applet-> iface-> icon ( ), applet-> iface-> text ( ), applet-> popup ); | 379 | applet->id = pop->insertItem( applet->iface->icon(), |
380 | applet->iface->text(), applet->popup ); | ||
327 | else | 381 | else |
328 | applet-> id = pop-> insertItem ( applet-> iface-> icon ( ), applet-> iface-> text ( ) ); | 382 | applet->id = pop->insertItem( applet->iface->icon(), |
383 | applet->iface->text() ); | ||
329 | 384 | ||
330 | dict.insert( applet->id, new MenuApplet(*applet) ); | 385 | dict.insert( applet->id, new MenuApplet(*applet) ); |
331 | } | 386 | } |
332 | /* need to update the key */ | 387 | /* need to update the key */ |
333 | m_applets.setAutoDelete( true ); | 388 | menuApplets.setAutoDelete( true ); |
334 | m_applets.clear(); | 389 | menuApplets.clear(); |
335 | m_applets.setAutoDelete( false ); | 390 | menuApplets.setAutoDelete( false ); |
336 | m_applets = dict; | 391 | menuApplets = dict; |
337 | } | 392 | } |
diff --git a/core/launcher/startmenu.h b/core/launcher/startmenu.h index 99df0f0..7218035 100644 --- a/core/launcher/startmenu.h +++ b/core/launcher/startmenu.h | |||
@@ -25,6 +25,8 @@ | |||
25 | #include <qintdict.h> | 25 | #include <qintdict.h> |
26 | #include <qstring.h> | 26 | #include <qstring.h> |
27 | #include <qlist.h> | 27 | #include <qlist.h> |
28 | #include <qmap.h> | ||
29 | #include <qdir.h> | ||
28 | #include <qlabel.h> | 30 | #include <qlabel.h> |
29 | #include <qpopupmenu.h> | 31 | #include <qpopupmenu.h> |
30 | 32 | ||
@@ -32,10 +34,13 @@ | |||
32 | 34 | ||
33 | class StartPopupMenu : public QPopupMenu | 35 | class StartPopupMenu : public QPopupMenu |
34 | { | 36 | { |
37 | |||
35 | public: | 38 | public: |
36 | StartPopupMenu( QWidget *parent ) : QPopupMenu( parent ) {} | 39 | StartPopupMenu( QWidget *parent ) : QPopupMenu( parent ) {} |
40 | |||
37 | protected: | 41 | protected: |
38 | void keyPressEvent( QKeyEvent *e ); | 42 | void keyPressEvent( QKeyEvent *e ); |
43 | |||
39 | }; | 44 | }; |
40 | 45 | ||
41 | class QLibrary; | 46 | class QLibrary; |
@@ -49,7 +54,9 @@ struct MenuApplet { | |||
49 | }; | 54 | }; |
50 | 55 | ||
51 | class StartMenu : public QLabel { | 56 | class StartMenu : public QLabel { |
57 | |||
52 | Q_OBJECT | 58 | Q_OBJECT |
59 | |||
53 | public: | 60 | public: |
54 | StartMenu( QWidget * ); | 61 | StartMenu( QWidget * ); |
55 | ~StartMenu(); | 62 | ~StartMenu(); |
@@ -77,6 +84,9 @@ private: | |||
77 | void clearApplets(); | 84 | void clearApplets(); |
78 | void addApplets( QPopupMenu* menu ); | 85 | void addApplets( QPopupMenu* menu ); |
79 | bool loadMenu( QPopupMenu *menu ); | 86 | bool loadMenu( QPopupMenu *menu ); |
87 | void createMenuEntries( QPopupMenu *menu, QDir dir, bool ltabs, bool lot ); | ||
88 | void createDirEntry( QPopupMenu *menu, QDir dir, QString file, bool lot ); | ||
89 | void createAppEntry( QPopupMenu *menu, QDir dir, QString file ); | ||
80 | 90 | ||
81 | private: | 91 | private: |
82 | bool useWidePopupMenu; | 92 | bool useWidePopupMenu; |
@@ -85,11 +95,12 @@ private: | |||
85 | bool startButtonIsFlat; | 95 | bool startButtonIsFlat; |
86 | QString startButtonPixmap; | 96 | QString startButtonPixmap; |
87 | 97 | ||
88 | QStringList tabs; | 98 | int currentItem; |
89 | QList<AppLnk> other; | 99 | |
90 | QIntDict<MenuApplet> m_applets; | 100 | QIntDict<AppLnk> appLnks; |
91 | int ntabs; | 101 | QIntDict<QString> tabNames; |
92 | int nother; | 102 | QIntDict<MenuApplet> menuApplets; |
103 | |||
93 | }; | 104 | }; |
94 | 105 | ||
95 | #endif // __START_MENU_H__ | 106 | #endif // __START_MENU_H__ |