1 /********************************************************************
2 **** Utils.c: Some useful functions that doesn't change too much ****
3 **** Free software under GNU license, started on 11/11/2000 ****
4 **** © T.Pierron, C.Guillaume. ****
5 ********************************************************************/
8 #include <intuition/intuition.h>
9 #include <libraries/gadtools.h>
10 #include <libraries/asl.h>
13 #include <proto/exec.h>
14 #include <proto/dos.h>
15 #include <proto/graphics.h>
16 #include <proto/diskfont.h>
17 #include <proto/intuition.h>
18 #include <proto/asl.h>
19 #include <proto/gadtools.h>
25 #include "JanoPrefs.h"
28 #define CATCOMP_NUMBERS
29 #include "../../tools/Edit/strings.h"
31 extern struct Window
*Wnd
;
32 struct FileRequester
*fr
=NULL
;
33 static PREFS oldprefs
;
35 void ThrowError(struct Window
*W
, UBYTE
*Msg
);
36 void ThrowDOSError(struct Window
*W
, STRPTR Prefix
, UBYTE err
);
38 #define GetSI(gad) ((struct StringInfo *)gad->SpecialInfo)
40 /*** Convert number to dec ***/
41 WORD
AddNum(ULONG nb
, STRPTR buf
)
43 static UBYTE temp
[10];
45 for(dst
=temp
+9; nb
>=10; nb
/=10) /* Mouais :-\ */
48 CopyMem(dst
,buf
,temp
+10-dst
);
49 return (WORD
)(temp
+10-dst
);
53 /*** Measure the maximal lenght of a NULL-terminated array of string: ***/
54 WORD
meas_table(UBYTE
**strings
)
56 extern struct RastPort RPT
;
58 register WORD maxlen
,len
;
60 for(p
=strings
, maxlen
=0; *p
; p
++)
61 if(maxlen
< (len
=TextLength(&RPT
,*p
,strlen(*p
)))) maxlen
= len
;
66 /*** Extract some information of a TextFont struct ***/
67 void font_info(UBYTE
*buf
, struct TextFont
*fnt
)
69 UBYTE
*name
= fnt
->tf_Message
.mn_Node
.ln_Name
;
72 /* Fontname / Height */
73 size
= strlen(name
)-5; /* -".font" */
74 CopyMem(name
,buf
,size
); buf
+=size
;
75 *buf
++='/'; buf
+= AddNum(fnt
->tf_YSize
, buf
);
79 /*** Same job with a (struct Screen *) ***/
80 void scr_info(UBYTE
*buf
, WORD Width
, WORD Height
, WORD Depth
)
82 /* Width x Height x Depth */
83 buf
+= AddNum(Width
, buf
); *buf
++='x';
84 buf
+= AddNum(Height
, buf
); *buf
++='x';
85 buf
+= AddNum(Depth
, buf
); *buf
=0;
88 /*** Try to load an already loaded font ***/
89 struct TextFont
*get_old_font( STRPTR fmt
)
91 static UBYTE FontName
[50];
93 for(p
=fmt
; *p
&& *p
!='/'; p
++);
97 CopyMem(fmt
,FontName
,p
-fmt
); strcpy(FontName
+(p
-fmt
),".font");
98 font
.ta_Name
= FontName
;
99 font
.ta_YSize
= atoi(p
+1);
100 font
.ta_Style
= FS_NORMAL
;
101 font
.ta_Flags
= FPF_DISKFONT
;
102 return (struct TextFont
*)OpenDiskFont(&font
);
107 /*** Be sure a window fits in a screen ***/
108 void fit_in_screen(struct NewWindow
*wnd
, struct Screen
*scr
)
110 /* Adjust left edge and width of window */
111 if(wnd
->LeftEdge
+ wnd
->Width
> scr
->Width
)
112 wnd
->LeftEdge
= scr
->Width
- wnd
->Width
;
114 if(wnd
->LeftEdge
< 0) wnd
->LeftEdge
=0, wnd
->Width
=scr
->Width
;
116 /* Adjust top edge and height */
117 if(wnd
->TopEdge
+ wnd
->Height
> scr
->Height
)
118 wnd
->TopEdge
= scr
->Height
- wnd
->Height
;
120 if(wnd
->TopEdge
< 0) wnd
->TopEdge
=0, wnd
->Height
=scr
->Height
;
124 /*** Performs some checks on what user has enterred ***/
125 void check_tab(struct Gadget
*str
)
127 UBYTE
*buf
= GetSI(str
)->Buffer
, *start
;
129 for(start
=buf
; *buf
; buf
++)
130 if(*buf
< '0' || *buf
> '9')
132 /* Wrong char, avert user */
133 GetSI(str
)->BufferPos
= buf
-start
;
135 ActivateGadget(str
, Wnd
, NULL
);
140 /*** Update gui components ***/
141 void show_changes(PREFS
*old
, PREFS
*new)
143 extern struct Gadget
*gads
[];
144 extern struct Screen
*Scr
;
145 extern UBYTE StrInfo
[60],Modif
[];
148 if(old
->tabsize
!= new->tabsize
)
150 StrInfo
[ AddNum(new->tabsize
, StrInfo
) ] = 0;
151 GT_SetGadgetAttrs(gads
[0], Wnd
, NULL
, GTST_String
, StrInfo
, TAG_DONE
);
153 GT_SetGadgetAttrs(gads
[1], Wnd
, NULL
, GTST_String
, new->wordssep
, TAG_DONE
);
155 extern struct TagItem TextFontTags
[], ScrFontTags
[], ScrMdTags
[];
156 extern ULONG extended
;
158 ScrFontTags
[1].ti_Data
= 0;
159 ScrMdTags
[1].ti_Data
= 0;
160 TextFontTags
[1].ti_Data
= 0; extended
=0;
161 if( new->use_txtfont
)
162 font_info(StrInfo
, new->txtfont
),
163 TextFontTags
[0].ti_Data
= (ULONG
) FTCycTxt
, extended
|= 1;
165 TextFontTags
[0].ti_Data
= (ULONG
) (FTCycTxt
+1);
167 if( new->use_scrfont
)
168 font_info(StrInfo
+20, new->scrfont
),
169 ScrFontTags
[0].ti_Data
= (ULONG
) FSCycTxt
, extended
|= 2;
171 ScrFontTags
[0].ti_Data
= (ULONG
) (FSCycTxt
+1);
173 if( new->use_pub
==1 )
174 scr_info(StrInfo
+40,Scr
->Width
, Scr
->Height
, Scr
->RastPort
.BitMap
->Depth
),
175 ScrMdTags
[0].ti_Data
= (ULONG
) ScrCycTxt
, extended
|= 4;
177 ScrMdTags
[0].ti_Data
= (ULONG
) (ScrCycTxt
+1);
178 if(new->use_pub
!=2) ScrMdTags
[1].ti_Data
= 1; /* Clone */
182 GT_SetGadgetAttrsA(gads
[2],Wnd
,NULL
,TextFontTags
);
183 GT_SetGadgetAttrsA(gads
[3],Wnd
,NULL
,ScrFontTags
);
184 GT_SetGadgetAttrsA(gads
[4],Wnd
,NULL
,ScrMdTags
);
189 GT_SetGadgetAttrs(gads
[CGS
+i
], Wnd
, NULL
, GTCB_Checked
, (&new->backdrop
)[i
], TAG_DONE
);
191 for(col
=i
=0; i
<sizeof(new->pen
); i
++)
192 if( (&new->pen
.bg
)[i
] != (&old
->pen
.bg
)[i
] ) col
|= Modif
[i
];
194 if(old
->scrfont
!= new->scrfont
) col
|= EDIT_GUI
;
195 if(old
->txtfont
!= new->txtfont
) col
|= EDIT_AREA
;
196 render_sample(Wnd
, col
);
200 /* ASL load or save requester tags: */
201 static IPTR tags
[] = {
202 ASLFR_InitialLeftEdge
,50,
203 ASLFR_InitialTopEdge
,50,
204 ASLFR_InitialWidth
,320,
205 ASLFR_InitialHeight
,256,
208 ASLFR_SleepWindow
,TRUE
,
209 ASLFR_InitialDrawer
,(IPTR
)"ENVARC:",
211 ASLFR_InitialPattern
,(IPTR
)"#?",
215 static UBYTE TempPath
[100];
216 extern UBYTE Path
[100];
218 /*** Save width and height of an ASL requester ***/
219 void save_asl_dim(struct FileRequester
*fr
)
221 tags
[1] = fr
->fr_LeftEdge
;
222 tags
[3] = fr
->fr_TopEdge
;
223 tags
[5] = fr
->fr_Width
;
224 tags
[7] = fr
->fr_Height
;
227 /*** Ask user for a new preference file to load ***/
228 void load_pref(PREFS
*prefs
)
230 if(fr
==NULL
&& !(fr
= (void *)AllocAslRequest(ASL_FileRequest
,NULL
)))
231 ThrowError(Wnd
, ErrMsg(ERR_NOASLREQ
));
234 tags
[11] = FILF_PATGAD
;
235 /* Save old preferences */
236 CopyMem(prefs
, &oldprefs
, sizeof(oldprefs
));
237 if(AslRequest(fr
, (struct TagItem
*)tags
))
241 CopyMem(Path
, TempPath
, sizeof(Path
));
242 CopyMem(fr
->fr_Drawer
, Path
, sizeof(Path
));
243 AddPart(Path
, fr
->fr_File
, sizeof(Path
));
244 switch( errcode
= load_prefs(prefs
, Path
) )
247 show_changes(&oldprefs
, prefs
),
248 SetWindowTitles(Wnd
, Path
, (STRPTR
)-1);
250 case ERROR_OBJECT_WRONG_TYPE
:
251 ThrowError(Wnd
, ErrMsg(ERR_BADPREFSFILE
)); break;
252 default: ThrowDOSError(Wnd
, Path
, errcode
);
254 /* Restore previous path */
255 CopyMem(TempPath
, Path
, sizeof(Path
));
260 /*** Ask user for a place to save the preference file ***/
261 void save_pref_as(PREFS
*prefs
)
263 if(fr
==NULL
&& !(fr
= (void *)AllocAslRequest(ASL_FileRequest
,NULL
)))
264 ThrowError(Wnd
, ErrMsg(ERR_NOASLREQ
));
267 tags
[11] = FILF_SAVE
| FILF_PATGAD
;
268 if(AslRequest(fr
, (struct TagItem
*)tags
))
271 CopyMem(fr
->fr_Drawer
, Path
, sizeof(Path
));
272 AddPart(Path
, fr
->fr_File
, sizeof(Path
));
274 SetWindowTitles(Wnd
, Path
, (STRPTR
)-1);
279 /*** Set default preference ***/
280 void default_prefs(PREFS
*prefs
)
284 CopyMem(prefs
, &oldprefs
, sizeof(oldprefs
));
285 set_default_prefs(prefs
, prefs
->parent
);
286 show_changes(&oldprefs
, prefs
);
289 static char edit_file
;
290 static char buffer
[ MAX(sizeof(Path
),sizeof(prefs
)) ];
292 /*** Save current configuration to restore it later if desired ***/
293 void save_config( char ConfigFile
)
295 #warning "CHECKME: = or ==?"
297 if(edit_file
= ConfigFile
)
298 CopyMem(Path
, buffer
, sizeof(Path
));
300 CopyMem(&prefs
, buffer
, sizeof(prefs
));
303 /*** Restore config ***/
304 void restore_config(PREFS
*prefs
)
307 CopyMem(prefs
, &oldprefs
, sizeof(oldprefs
));
310 /* The preferences come from Jano */
311 CopyMem(buffer
, prefs
, sizeof(*prefs
));
315 /* We have edited a static file */
316 if( load_prefs(prefs
, buffer
) == RETURN_OK
)
317 SetWindowTitles(Wnd
, Path
, (STRPTR
)-1);
319 set_default_prefs(prefs
, prefs
->parent
);
321 show_changes(&oldprefs
, prefs
);
324 void free_asl( void ) { FreeAslRequest( fr
); }