1 /* AbiWord
2  * Copyright (C) 1998 AbiSource, Inc.
3  * Copyright (C) 2001-2002 Hubert Figuiere
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA.
19  */
20 
21 #import <Cocoa/Cocoa.h>
22 
23 #include "ut_assert.h"
24 #include "ut_vector.h"
25 #include "ut_hash.h"
26 #include "ut_debugmsg.h"
27 
28 #include "ap_CocoaToolbar_FontCombo.h"
29 #include "ap_Toolbar_Id.h"
30 #include "xap_CocoaApp.h"
31 #include "xap_Frame.h"
32 
33 #include "ev_CocoaToolbar.h"
34 #include "ev_Toolbar.h"
35 
36 
static_constructor(EV_Toolbar * pToolbar,XAP_Toolbar_Id tlbrid)37 EV_Toolbar_Control * AP_CocoaToolbar_FontCombo::static_constructor(EV_Toolbar * pToolbar,
38 								   XAP_Toolbar_Id tlbrid)
39 {
40 	AP_CocoaToolbar_FontCombo * p = new AP_CocoaToolbar_FontCombo(pToolbar,tlbrid);
41 	return p;
42 }
43 
AP_CocoaToolbar_FontCombo(EV_Toolbar * pToolbar,XAP_Toolbar_Id tlbrid)44 AP_CocoaToolbar_FontCombo::AP_CocoaToolbar_FontCombo(EV_Toolbar * pToolbar,
45 						     XAP_Toolbar_Id tlbrid)
46 	: EV_Toolbar_Control(pToolbar/*,id*/)
47 {
48 	UT_DEBUG_ONLY_ARG(tlbrid);
49 
50 	UT_ASSERT(tlbrid == AP_TOOLBAR_ID_FMT_FONT);
51 	m_nPixels = 150;
52 	m_nLimit = 32;
53 }
54 
~AP_CocoaToolbar_FontCombo(void)55 AP_CocoaToolbar_FontCombo::~AP_CocoaToolbar_FontCombo(void)
56 {
57 	UT_VECTOR_FREEALL(char *, m_vecContents);
58 }
59 
60 // this is the comparator to sort the combo box.
61 // TODO: move it else where in a place it is reusable by anybody.
compareStrings(const void * ppS1,const void * ppS2)62 static  int compareStrings(const void * ppS1, const void * ppS2)
63 {
64 	#warning TODO: move it else where in a place it is reusable by anybody.
65 	const char ** sz1 = (const char **) (ppS1);
66 	const char ** sz2 = (const char **) (ppS2);
67 	return strcmp(*sz1, *sz2);
68 }
69 
populate(void)70 bool AP_CocoaToolbar_FontCombo::populate(void)
71 {
72     UT_ASSERT(m_pToolbar);
73 
74     // Things are relatively easy with the font manager.  Just
75     // request all fonts and ask them their names.
76     NSArray *list = [[NSFontManager sharedFontManager] availableFontFamilies];
77 
78     m_vecContents.clear();
79 
80     NSEnumerator* e = [list objectEnumerator];
81     while(NSString *item = [e nextObject])
82     {
83         const char *fName = g_strdup([item UTF8String]);
84         UT_ASSERT (fName);
85         m_vecContents.addItem(fName);
86     }
87     m_vecContents.qsort(compareStrings);
88 
89     return true;
90 }
91