xref: /trunk/main/svtools/source/control/svxbox.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 #include <tools/debug.hxx>
31 #include <vcl/svapp.hxx>
32 #include <svtools/svxbox.hxx>
33 #include <unotools/charclass.hxx>
34 
35 // -----------------------------------------------------------------------
36 
37 SV_IMPL_PTRARR(SvxEntryLst, SvxBoxEntry*)
38 
39 /*--------------------------------------------------------------------
40      Beschreibung: Ein ListboxElement
41  --------------------------------------------------------------------*/
42 
43 SvxBoxEntry::SvxBoxEntry() :
44     nId(LISTBOX_ENTRY_NOTFOUND),
45     bModified(sal_False),
46     bNew(sal_False)
47 {
48 }
49 
50 
51 SvxBoxEntry::SvxBoxEntry(const String& aNam, sal_uInt16 nIdx) :
52     aName(aNam),
53     nId(nIdx),
54     bModified(sal_False),
55     bNew(sal_False)
56 {
57 }
58 
59 
60 SvxBoxEntry::SvxBoxEntry(const SvxBoxEntry& rOld) :
61     aName(rOld.aName),
62     nId(rOld.nId),
63     bModified(rOld.bModified),
64     bNew(rOld.bNew)
65 {
66 }
67 
68 /*--------------------------------------------------------------------
69      Beschreibung:
70  --------------------------------------------------------------------*/
71 
72 SvxListBox::SvxListBox(Window* pParent, WinBits nBits) :
73     ListBox(pParent, nBits)
74 {
75     InitListBox();
76 }
77 
78 
79 SvxListBox::SvxListBox(Window* pParent, const ResId& rId):
80     ListBox(pParent, rId)
81 {
82     InitListBox();
83 }
84 
85 /*--------------------------------------------------------------------
86      Beschreibung: Basisklasse Dtor
87  --------------------------------------------------------------------*/
88 
89 __EXPORT SvxListBox::~SvxListBox()
90 {
91     aEntryLst.DeleteAndDestroy(0,   aEntryLst.Count());
92     aDelEntryLst.DeleteAndDestroy(0, aDelEntryLst.Count());
93 }
94 
95 /*--------------------------------------------------------------------
96      Beschreibung: Evtl. Liste aus der Ressource beachten
97  --------------------------------------------------------------------*/
98 
99 void SvxListBox::InitListBox()
100 {
101     // Verwaltung fuer die Stringlist aus der Resource aufbauen
102     sal_uInt16 nSize = GetEntryCount();
103     for(sal_uInt16 i=0; i < nSize; ++i)
104     {   const SvxBoxEntry* pTmp = new SvxBoxEntry(ListBox::GetEntry(i), i);
105         const SvxBoxEntry* &rpTmp = pTmp;
106         aEntryLst.Insert(rpTmp, aEntryLst.Count());
107     }
108 }
109 
110 /*--------------------------------------------------------------------
111      Beschreibung: neue Eintraege verwalten
112  --------------------------------------------------------------------*/
113 
114 void SvxListBox::InsertNewEntry(const SvxBoxEntry& rEntry)
115 {
116     SvxBoxEntry* pNew = new SvxBoxEntry(rEntry);
117     pNew->bNew = sal_True;
118     InsertSorted(pNew);
119 }
120 
121 /*--------------------------------------------------------------------
122      Beschreibung: Eintrag in die ListBox aufnehmen
123  --------------------------------------------------------------------*/
124 
125 void SvxListBox::InsertEntry(const SvxBoxEntry& rEntry, sal_uInt16 nPos)
126 {
127     if(nPos != LISTBOX_ENTRY_NOTFOUND)
128     {
129         SvxBoxEntry* pEntry = new SvxBoxEntry(rEntry);
130         ListBox::InsertEntry(pEntry->aName, nPos);
131         //const SvxBoxEntry* &rpEntry = pEntry;
132         aEntryLst.C40_INSERT(SvxBoxEntry, pEntry, nPos);
133     }
134     else
135         InsertSorted(new SvxBoxEntry(rEntry));
136 }
137 
138 /*--------------------------------------------------------------------
139      Beschreibung: Eintrag aus der Liste loeschen
140  --------------------------------------------------------------------*/
141 
142 void SvxListBox::RemoveEntry(sal_uInt16 nPos)
143 {
144     if(nPos >= aEntryLst.Count())
145         return;
146 
147     // Altes Element austragen
148     SvxBoxEntry* pEntry = aEntryLst[nPos];
149     aEntryLst.Remove(nPos, 1);
150     ListBox::RemoveEntry(nPos);
151 
152     // keine neuen Eintraege in die Liste mit aufnehmen
153     if(pEntry->bNew)
154         return;
155 
156     // in DeleteListe eintragen
157     aDelEntryLst.C40_INSERT(SvxBoxEntry, pEntry, aDelEntryLst.Count());
158 }
159 
160 /*--------------------------------------------------------------------
161      Beschreibung: Eintrag ueber konkretes Obkjekt loeschen
162  --------------------------------------------------------------------*/
163 
164 void SvxListBox::RemoveEntry(const SvxBoxEntry& rEntry)
165 {
166     sal_uInt16 nPos = ListBox::GetEntryPos(rEntry.aName);
167     RemoveEntry(nPos);
168 }
169 
170 /*--------------------------------------------------------------------
171      Beschreibung: Listen loeschen und Anzeige loeschen
172  --------------------------------------------------------------------*/
173 
174 void SvxListBox::Clear()
175 {
176     ListBox::Clear();
177     aEntryLst.DeleteAndDestroy(0, aEntryLst.Count());
178     aDelEntryLst.DeleteAndDestroy(0, aDelEntryLst.Count());
179 }
180 
181 /*--------------------------------------------------------------------
182      Beschreibung: Position by Name
183  --------------------------------------------------------------------*/
184 
185 sal_uInt16 SvxListBox::GetEntryPos(const SvxBoxEntry& rEntry) const
186 {
187     return ListBox::GetEntryPos(rEntry.aName);
188 }
189 
190 /*--------------------------------------------------------------------
191      Beschreibung: Rund um die Entries
192  --------------------------------------------------------------------*/
193 
194 const SvxBoxEntry& SvxListBox::GetSvxBoxEntry(sal_uInt16 nPos) const
195 {
196     if(nPos < aEntryLst.Count())
197         return *aEntryLst[nPos];
198     else
199         return aDefault;
200 }
201 
202 /*--------------------------------------------------------------------
203      Beschreibung: aktullen Eintrag zurueckgeben
204  --------------------------------------------------------------------*/
205 
206 const SvxBoxEntry& SvxListBox::GetSelectSvxBoxEntry(sal_uInt16 nSelId) const
207 {
208     String aName(ListBox::GetSelectEntry(nSelId));
209 
210     if(aName.Len() > 0)
211     {
212         for (sal_uInt16 i=0; i < aEntryLst.Count(); i++)
213         {
214             if(aEntryLst[i]->aName == aName )
215                 return *aEntryLst[i];
216         }
217     }
218     return aDefault;
219 }
220 
221 /*--------------------------------------------------------------------
222      Beschreibung: modifizierte Eintraege
223  --------------------------------------------------------------------*/
224 
225 sal_uInt16 SvxListBox::GetModifiedCount() const
226 {
227     sal_uInt16 nMod  = 0;
228     sal_uInt16 nSize = aEntryLst.Count();
229     for(sal_uInt16 i=0; i < nSize; ++i)
230     {   if(aEntryLst[i]->bModified)
231             nMod++;
232     }
233     return nMod;
234 }
235 
236 /*--------------------------------------------------------------------
237      Beschreibung: Modifizierte Eintraege behandeln
238  --------------------------------------------------------------------*/
239 
240 void SvxListBox::ModifyEntry(sal_uInt16 nPos, const String& rName)
241 {
242     if(nPos >= aEntryLst.Count())
243         return;
244 
245     SvxBoxEntry* pEntry = aEntryLst[nPos];
246     aEntryLst.Remove(nPos, 1);
247     aEntryLst[nPos]->aName      = rName;
248     aEntryLst[nPos]->bModified  = sal_True;
249     ListBox::RemoveEntry(nPos);
250 
251     InsertSorted(pEntry);
252 }
253 
254 /*--------------------------------------------------------------------
255      Beschreibung: alle modifizierten Eintraege bahandeln
256  --------------------------------------------------------------------*/
257 
258 const SvxBoxEntry& SvxListBox::GetModifiedEntry(sal_uInt16 nPos) const
259 {
260     sal_uInt16 nSize = aEntryLst.Count();
261     sal_uInt16 nMod  = 0;
262     for(sal_uInt16 i=0; i < nSize; ++i)
263     {   if(aEntryLst[i]->bModified)
264         {   if(nMod == nPos)
265                 return *aEntryLst[i];
266             nMod++;
267         }
268     }
269     return aDefault;
270 }
271 
272 /*--------------------------------------------------------------------
273      Beschreibung: geloeschte Eintraege
274  --------------------------------------------------------------------*/
275 
276 sal_uInt16 SvxListBox::GetRemovedCount() const
277 {
278     return aDelEntryLst.Count();
279 }
280 
281 
282 const SvxBoxEntry& SvxListBox::GetRemovedEntry(sal_uInt16 nPos) const
283 {
284     if(nPos < aDelEntryLst.Count())
285         return *aDelEntryLst[nPos];
286 
287     return aDefault;
288 }
289 
290 /*--------------------------------------------------------------------
291      Beschreibung: Neue Entries begutachten
292  --------------------------------------------------------------------*/
293 
294 sal_uInt16 SvxListBox::GetNewCount() const
295 {
296     sal_uInt16 nNew = 0;
297     sal_uInt16 nSize = aEntryLst.Count();
298     for(sal_uInt16 i=0; i < nSize; ++i)
299     {   if(aEntryLst[i]->bNew)
300             nNew++;
301     }
302     return nNew;
303 }
304 
305 /*--------------------------------------------------------------------
306      Beschreibung:  Alle neuen Eintraege ueberpruefen
307  --------------------------------------------------------------------*/
308 
309 const SvxBoxEntry& SvxListBox::GetNewEntry(sal_uInt16 nPos) const
310 {
311     sal_uInt16 nSize = aEntryLst.Count();
312     sal_uInt16 nNew  = 0;
313     for(sal_uInt16 i=0; i < nSize; ++i)
314     {   if(aEntryLst[i]->bNew)
315         {   if(nNew == nPos)
316                 return *aEntryLst[i];
317             nNew++;
318         }
319     }
320     return aDefault;
321 }
322 
323 /*--------------------------------------------------------------------
324      Beschreibung: Sortiert einfuegen
325  --------------------------------------------------------------------*/
326 
327 void SvxListBox::InsertSorted(SvxBoxEntry* pEntry)
328 {
329     ListBox::InsertEntry(pEntry->aName);
330     sal_uInt16 nPos = ListBox::GetEntryPos(pEntry->aName);
331     aEntryLst.C40_INSERT(SvxBoxEntry, pEntry, nPos);
332 }
333 
334 /*--------------------------------------------------------------------
335      Beschreibung: ComboBoxen mit Verwaltungseinheit
336  --------------------------------------------------------------------*/
337 
338 SvxComboBox::SvxComboBox(Window* pParent, WinBits nBits, sal_uInt16 nStyleBits) :
339     ComboBox(pParent, nBits),
340     nStyle(nStyleBits)
341 {
342     InitComboBox();
343 }
344 
345 
346 SvxComboBox::SvxComboBox(Window* pParent, const ResId& rId, sal_uInt16 nStyleBits ):
347     ComboBox(pParent, rId),
348     nStyle(nStyleBits)
349 {
350     InitComboBox();
351 }
352 
353 /*--------------------------------------------------------------------
354      Beschreibung: Basisklasse Dtor
355  --------------------------------------------------------------------*/
356 
357 __EXPORT SvxComboBox::~SvxComboBox()
358 {
359     aEntryLst.DeleteAndDestroy(0,   aEntryLst.Count());
360     aDelEntryLst.DeleteAndDestroy(0, aDelEntryLst.Count());
361 }
362 
363 /*--------------------------------------------------------------------
364      Beschreibung: Evtl. Liste aus der Ressource beachten
365  --------------------------------------------------------------------*/
366 
367 void SvxComboBox::InitComboBox()
368 {
369     // Verwaltung fuer die Stringlist aus der Resource aufbauen
370     sal_uInt16 nSize = GetEntryCount();
371     for(sal_uInt16 i=0; i < nSize; ++i)
372     {   const SvxBoxEntry* pTmp = new SvxBoxEntry(ComboBox::GetEntry(i), i);
373         const SvxBoxEntry* &rpTmp = pTmp;
374         aEntryLst.Insert(rpTmp, aEntryLst.Count());
375     }
376 }
377 
378 /*--------------------------------------------------------------------
379      Beschreibung: neue Eintraege verwalten
380  --------------------------------------------------------------------*/
381 
382 void SvxComboBox::InsertNewEntry(const SvxBoxEntry& rEntry)
383 {
384     SvxBoxEntry* pNew = new SvxBoxEntry(rEntry);
385     pNew->bNew = sal_True;
386     InsertSorted(pNew);
387 }
388 
389 /*--------------------------------------------------------------------
390      Beschreibung: Eintrag in die ComboBox aufnehmen
391  --------------------------------------------------------------------*/
392 
393 void SvxComboBox::InsertEntry(const SvxBoxEntry& rEntry)
394 {
395     InsertSorted(new SvxBoxEntry(rEntry));
396 }
397 
398 /*--------------------------------------------------------------------
399      Beschreibung: Eintrag aus der Liste loeschen
400  --------------------------------------------------------------------*/
401 
402 void SvxComboBox::RemoveEntry(sal_uInt16 nPos)
403 {
404     if(nPos >= aEntryLst.Count())
405         return;
406 
407     // Altes Element austragen
408     SvxBoxEntry* pEntry = aEntryLst[nPos];
409     aEntryLst.Remove(nPos, 1);
410     ComboBox::RemoveEntry(nPos);
411 
412     // keine neuen Eintraege in die Liste mit aufnehmen
413     if(pEntry->bNew)
414         return;
415 
416     // in DeleteListe eintragen
417     aDelEntryLst.C40_INSERT(SvxBoxEntry, pEntry, aDelEntryLst.Count());
418 }
419 
420 /*--------------------------------------------------------------------
421      Beschreibung: Eintrag ueber konkretes Obkjekt loeschen
422  --------------------------------------------------------------------*/
423 
424 void SvxComboBox::RemoveEntry(const SvxBoxEntry& rEntry)
425 {
426     sal_uInt16 nPos = ComboBox::GetEntryPos(rEntry.aName);
427     RemoveEntry(nPos);
428 }
429 
430 /*--------------------------------------------------------------------
431      Beschreibung: Listen loeschen und Anzeige loeschen
432  --------------------------------------------------------------------*/
433 
434 void SvxComboBox::Clear()
435 {
436     ComboBox::Clear();
437     aEntryLst.DeleteAndDestroy(0, aEntryLst.Count());
438     aDelEntryLst.DeleteAndDestroy(0, aDelEntryLst.Count());
439 }
440 
441 
442 /*--------------------------------------------------------------------
443      Beschreibung: Position by Name
444  --------------------------------------------------------------------*/
445 
446 sal_uInt16 SvxComboBox::GetEntryPos(const SvxBoxEntry& rEntry) const
447 {
448     return ComboBox::GetEntryPos(rEntry.aName);
449 }
450 
451 /*--------------------------------------------------------------------
452      Beschreibung: Rund um die Entries
453  --------------------------------------------------------------------*/
454 
455 const SvxBoxEntry& SvxComboBox::GetEntry(sal_uInt16 nPos) const
456 {
457     if(nPos < aEntryLst.Count())
458         return *aEntryLst[nPos];
459     else
460         return aDefault;
461 }
462 
463 /*--------------------------------------------------------------------
464      Beschreibung: modifizierte Eintraege
465  --------------------------------------------------------------------*/
466 
467 sal_uInt16 SvxComboBox::GetModifiedCount() const
468 {
469     sal_uInt16 nMod  = 0;
470     sal_uInt16 nSize = aEntryLst.Count();
471     for(sal_uInt16 i=0; i < nSize; ++i)
472     {   if(aEntryLst[i]->bModified)
473             nMod++;
474     }
475     return nMod;
476 }
477 
478 /*--------------------------------------------------------------------
479      Beschreibung: Modifizierte Eintraege behandeln
480  --------------------------------------------------------------------*/
481 
482 void SvxComboBox::ModifyEntry(sal_uInt16 nPos, const String& rName)
483 {
484     if(nPos >= aEntryLst.Count())
485         return;
486 
487     SvxBoxEntry* pEntry = aEntryLst[nPos];
488     aEntryLst.Remove(nPos, 1);
489     aEntryLst[nPos]->aName      = rName;
490     aEntryLst[nPos]->bModified  = sal_True;
491     ComboBox::RemoveEntry(nPos);
492 
493     InsertSorted(pEntry);
494 }
495 
496 /*--------------------------------------------------------------------
497      Beschreibung: alle modifizierten Eintraege bahandeln
498  --------------------------------------------------------------------*/
499 
500 const SvxBoxEntry& SvxComboBox::GetModifiedEntry(sal_uInt16 nPos) const
501 {
502     sal_uInt16 nSize = aEntryLst.Count();
503     sal_uInt16 nMod  = 0;
504     for(sal_uInt16 i=0; i < nSize; ++i)
505     {   if(aEntryLst[i]->bModified)
506         {   if(nMod == nPos)
507                 return *aEntryLst[i];
508             nMod++;
509         }
510     }
511     return aDefault;
512 }
513 
514 /*--------------------------------------------------------------------
515      Beschreibung: geloeschte Eintraege
516  --------------------------------------------------------------------*/
517 
518 sal_uInt16 SvxComboBox::GetRemovedCount() const
519 {
520     return aDelEntryLst.Count();
521 }
522 
523 
524 const SvxBoxEntry& SvxComboBox::GetRemovedEntry(sal_uInt16 nPos) const
525 {
526     if(nPos < aDelEntryLst.Count())
527         return *aDelEntryLst[nPos];
528 
529     return aDefault;
530 }
531 
532 /*--------------------------------------------------------------------
533      Beschreibung: Neue Entries begutachten
534  --------------------------------------------------------------------*/
535 
536 sal_uInt16 SvxComboBox::GetNewCount() const
537 {
538     sal_uInt16 nNew = 0;
539     sal_uInt16 nSize = aEntryLst.Count();
540     for(sal_uInt16 i=0; i < nSize; ++i)
541     {   if(aEntryLst[i]->bNew)
542             nNew++;
543     }
544     return nNew;
545 }
546 
547 /*--------------------------------------------------------------------
548      Beschreibung:  Alle neuen Eintraege ueberpruefen
549  --------------------------------------------------------------------*/
550 
551 const SvxBoxEntry& SvxComboBox::GetNewEntry(sal_uInt16 nPos) const
552 {
553     sal_uInt16 nSize = aEntryLst.Count();
554     sal_uInt16 nNew  = 0;
555     for(sal_uInt16 i=0; i < nSize; ++i)
556     {   if(aEntryLst[i]->bNew)
557         {   if(nNew == nPos)
558                 return *aEntryLst[i];
559             nNew++;
560         }
561     }
562     return aDefault;
563 }
564 
565 /*--------------------------------------------------------------------
566      Beschreibung: Sortiert einfuegen
567  --------------------------------------------------------------------*/
568 
569 void SvxComboBox::InsertSorted(SvxBoxEntry* pEntry)
570 {
571     ComboBox::InsertEntry(pEntry->aName);
572     sal_uInt16 nPos = ComboBox::GetEntryPos(pEntry->aName);
573     aEntryLst.C40_INSERT(SvxBoxEntry, pEntry, nPos);
574 }
575 
576 
577 /*--------------------------------------------------------------------
578     Beschreibung: Je nach Option bestimmte Zeichen ausblenden
579  --------------------------------------------------------------------*/
580 
581 void __EXPORT SvxComboBox::KeyInput( const KeyEvent& rKEvt )
582 {
583     sal_Unicode cChar = rKEvt.GetCharCode();
584 
585     if(nStyle & SVX_CBS_FILENAME)
586     {
587 #if defined UNX
588         if( cChar == sal_Unicode( '/' ) || cChar == sal_Unicode( ' ' ) )
589             return;
590 #else
591         if( cChar == sal_Unicode( ':' ) || cChar == sal_Unicode( '\\' ) ||
592             cChar == sal_Unicode( '.' ) || cChar == sal_Unicode( ' ' ) )
593             return;
594 #endif
595     }
596     ComboBox::KeyInput(rKEvt);
597 }
598 
599 /*--------------------------------------------------------------------
600     Beschreibung: Text nach Option konvertieren
601  --------------------------------------------------------------------*/
602 
603 String SvxComboBox::GetText() const
604 {
605     String aTxt(ComboBox::GetText());
606     CharClass aCharClass( Application::GetSettings().GetLocale() );
607 
608     if(nStyle & SVX_CBS_LOWER)
609         return aCharClass.lower(aTxt);
610 
611     if(nStyle & SVX_CBS_UPPER)
612         return aCharClass.upper(aTxt);
613 
614     return aTxt;
615 }
616 
617 
618