1 /*
2  * $Id: fontSelection.c,v 1.5 2005/01/01 15:27:54 baum Exp $
3  *
4  * This file implements the file selection dialog
5  *
6  * Copyright (c) 2002 - 2005 Peter G. Baum  http://www.dr-baum.net
7  *
8  * See the file "license.terms" for information on usage and redistribution
9  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10  *
11  */
12 
13 /**
14 \page page_fontSelection gnocl::fontSelection
15 \htmlinclude fontSelection.html
16 **/
17 
18 /**
19  \par Modification History
20  \verbatim
21 	2013-07: added commands, options, commands
22 	2008-10: added command, class
23 	2003-02: cleanups with GnoclOption
24 	2002-07: start of developement
25  \endverbatim
26 **/
27 
28 /*
29    History:
30    2008-10: added command, class
31    2003-02: cleanups with GnoclOption
32    2002-07: start of developement
33 */
34 
35 #include "gnocl.h"
36 
37 /*
38   "font"                     GdkFont*              : Read
39   "font-name"                gchar*                : Read / Write
40   "preview-text"             gchar*                : Read / Write
41 */
42 
43 /**
44 \brief
45 **/
setFont(Tcl_Interp * interp,GnoclOption * opt,GObject * obj,Tcl_Obj ** ret)46 static int setFont ( Tcl_Interp *interp, GnoclOption *opt, GObject *obj, Tcl_Obj **ret )
47 {
48 
49 #ifdef DEBUG
50 	printf ( "setFont 1\n" );
51 #endif
52 
53 	int retval;
54 
55 	retval = gtk_font_selection_set_font_name ( GTK_WIDGET ( obj ), Tcl_GetString ( opt->val.obj ) ) ;
56 
57 #ifdef DEBUG
58 	printf ( "setFont 2\n" );
59 #endif
60 
61 	if ( retval != 1 )
62 	{
63 		return TCL_ERROR;
64 	}
65 
66 	return TCL_OK;
67 }
68 
69 static GnoclOption fontSelectOptions[] =
70 {
71 	{ "-fontName", GNOCL_STRING, "font-name"},
72 	{ "-previewText", GNOCL_STRING, "preview-text" },
73 	{ NULL }
74 };
75 
76 static const int commandIdx    = 0;
77 static const int modalIdx      = 1;
78 static const int fontSelectIdx = 6; /* first index which relates to the GtkFontSelection and not to the dialog */
79 
80 /**
81 \brief
82 **/
configure(Tcl_Interp * interp,GtkWidget * widget,GnoclOption fontSelectOptions[])83 static int configure ( Tcl_Interp *interp, GtkWidget *widget, GnoclOption fontSelectOptions[] )
84 {
85 
86 #ifdef DEBUG
87 	int i;
88 
89 	for ( i = 0 ; i <= 4 ; i++ )
90 	{
91 		g_print ( "fontSelectOptions  %d %s\n", i, fontSelectOptions[i] );
92 	}
93 
94 #endif
95 	return TCL_OK;
96 }
97 
98 /**
99 \brief      Obtain current -option values.
100 **/
cget(Tcl_Interp * interp,GtkButton * button,GnoclOption options[],int idx)101 static int cget (   Tcl_Interp *interp, GtkButton *button,  GnoclOption options[],  int idx )
102 {
103 	return gnoclCgetNotImplemented ( interp, options + idx );
104 }
105 
106 static const char *cmds[] = { "delete", "configure", "class", "cget",  NULL };
107 
108 /**
109 \brief
110 **/
fontSelFunc(ClientData data,Tcl_Interp * interp,int objc,Tcl_Obj * const objv[])111 static int fontSelFunc ( ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj * const objv[] )
112 {
113 
114 	enum cmdIdx { DeleteIdx, ConfigureIdx, ClassIdx, CgetIdx };
115 
116 	GtkWidget *widget = GTK_WIDGET ( data  );
117 	int idx;
118 
119 	if ( objc < 2 )
120 	{
121 		Tcl_WrongNumArgs ( interp, 1, objv, "command" );
122 		return TCL_ERROR;
123 	}
124 
125 	if ( Tcl_GetIndexFromObj ( interp, objv[1], cmds, "command", TCL_EXACT, &idx ) != TCL_OK )
126 		return TCL_ERROR;
127 
128 	switch ( idx )
129 	{
130 
131 		case ClassIdx:
132 			{
133 				Tcl_SetObjResult ( interp, Tcl_NewStringObj ( "fontSelection", -1 ) );
134 			}
135 			break;
136 		case DeleteIdx:
137 			{
138 				return gnoclDelete ( interp, widget, objc, objv );
139 			}
140 		case ConfigureIdx:
141 			{
142 
143 				int ret = TCL_ERROR;
144 
145 				if ( gnoclParseAndSetOptions ( interp, objc - 1, objv + 1, fontSelectOptions, G_OBJECT ( widget ) ) == TCL_OK )
146 				{
147 					ret = configure ( interp, widget, fontSelectOptions );
148 				}
149 
150 				gnoclClearOptions ( fontSelectOptions );
151 
152 				return ret;
153 			}
154 		case CgetIdx:
155 			{
156 				int idx;
157 
158 				switch ( gnoclCget ( interp, objc, objv, G_OBJECT ( widget ), fontSelectOptions, &idx ) )
159 				{
160 					case GNOCL_CGET_ERROR:
161 						return TCL_ERROR;
162 					case GNOCL_CGET_HANDLED:
163 						return TCL_OK;
164 					case GNOCL_CGET_NOTHANDLED:
165 						return cget ( interp, widget, fontSelectOptions, idx );
166 				}
167 			}
168 	}
169 
170 	return TCL_OK;
171 }
172 
173 /**
174 \brief
175 **/
gnoclFontSelectionCmd(ClientData data,Tcl_Interp * interp,int objc,Tcl_Obj * const objv[])176 int gnoclFontSelectionCmd ( ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj * const objv[] )
177 {
178 	if ( gnoclGetCmdsAndOpts ( interp, cmds, fontSelectOptions, objv, objc ) == TCL_OK )
179 	{
180 		return TCL_OK;
181 	}
182 
183 
184 
185 	int ret = TCL_OK;
186 	GtkWidget *widget;
187 
188 
189 	if ( gnoclParseOptions ( interp, objc, objv, fontSelectOptions ) != TCL_OK )
190 	{
191 		gnoclClearOptions ( fontSelectOptions );
192 		return TCL_ERROR;
193 	}
194 
195 
196 	widget = gtk_font_selection_new() ;
197 
198 	gtk_widget_show ( GTK_WIDGET ( widget ) );
199 
200 	ret = gnoclSetOptions ( interp, fontSelectOptions, G_OBJECT ( widget ), -1 );
201 
202 	if ( ret == TCL_OK )
203 	{
204 		ret = configure ( interp, G_OBJECT ( widget ), fontSelectOptions );
205 	}
206 
207 
208 	gnoclClearOptions ( fontSelectOptions );
209 
210 	/* STEP 3)  -show the widget */
211 
212 	if ( ret != TCL_OK )
213 	{
214 		gtk_widget_destroy ( GTK_WIDGET ( widget ) );
215 		return TCL_ERROR;
216 	}
217 
218 
219 	/* STEP 4)  -everything has worked, register the widget with the Tcl interpretor. */
220 	return gnoclRegisterWidget ( interp, GTK_WIDGET ( widget ), fontSelFunc );
221 
222 
223 	return ret;
224 }
225