| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "gdrawP.h" |
| 31 |
#include "ustring.h" |
| 32 |
|
| 33 |
#include <stdarg.h> |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define ERR_LINE_MAX 20 |
| 39 |
static GWindow error; |
| 40 |
enum err_type { et_info, et_warn, et_error, et_fatal }; |
| 41 |
static struct errinfo { |
| 42 |
unichar_t *lines[ERR_LINE_MAX]; |
| 43 |
unsigned int dismissed: 1; |
| 44 |
int width; |
| 45 |
enum err_type err_type; |
| 46 |
} errinfo; |
| 47 |
|
| 48 |
static int e_h(GWindow gw, GEvent *event) { |
| 49 |
int line; |
| 50 |
int x,len, max_len; |
| 51 |
GRect r; |
| 52 |
static unichar_t ok[] = { 'O', 'K', '\0' }; |
| 53 |
|
| 54 |
if ( event->type == et_expose ) { |
| 55 |
max_len = 0; |
| 56 |
for ( line = 0; line<ERR_LINE_MAX && errinfo.lines[line]!=NULL; ++line ) { |
| 57 |
len = GDrawGetTextWidth(gw,errinfo.lines[line],-1); |
| 58 |
if ( len>max_len ) max_len = len; |
| 59 |
} |
| 60 |
x = (errinfo.width-max_len)/2; |
| 61 |
for ( line = 0; line<ERR_LINE_MAX && errinfo.lines[line]!=NULL; ++line ) |
| 62 |
GDrawDrawText(gw,x, 10+10+15*line, errinfo.lines[line],-1,0x000000); |
| 63 |
|
| 64 |
x = (errinfo.width-(len = GDrawGetTextWidth(gw,ok,2)))/2; |
| 65 |
r.x = x-10; r.y = 25+15*line; r.width = len+20; r.height = 18; |
| 66 |
GDrawFillRect(gw,&r,0xffffff); |
| 67 |
GDrawDrawRect(gw,&r,0x000000); |
| 68 |
GDrawDrawText(gw,x,r.y+13,ok,2,0x000000); |
| 69 |
} else if ( event->type==et_char ) { |
| 70 |
if ( event->u.chr.chars[0]=='\r' || event->u.chr.chars[0]=='\33' ) |
| 71 |
errinfo.dismissed = true; |
| 72 |
} else if ( event->type==et_mouseup ) { |
| 73 |
errinfo.dismissed = true; |
| 74 |
} else if ( event->type==et_close ) { |
| 75 |
errinfo.dismissed = true; |
| 76 |
} |
| 77 |
return( 1 ); |
| 78 |
} |
| 79 |
|
| 80 |
static void RunError() { |
| 81 |
errinfo.dismissed = false; |
| 82 |
GDrawSetVisible(error,true); |
| 83 |
while ( !errinfo.dismissed ) |
| 84 |
GDrawProcessOneEvent(NULL); |
| 85 |
GDrawSetVisible(error,false); |
| 86 |
GDrawSync(NULL); |
| 87 |
GDrawProcessPendingEvents(NULL); |
| 88 |
} |
| 89 |
|
| 90 |
static void ProcessText(unichar_t *ubuf,char *buf, enum err_type et) { |
| 91 |
int max_len = 60, len; |
| 92 |
char *pt, *ept, *last_space; |
| 93 |
unichar_t *ue = ubuf; |
| 94 |
int line=0; |
| 95 |
|
| 96 |
pt = buf; |
| 97 |
for ( line=0; line<ERR_LINE_MAX && *pt; ++line ) { |
| 98 |
last_space = NULL; |
| 99 |
for ( ept = pt; *ept!='\n' && *ept!='\0' && ept-pt<max_len; ++ept ) |
| 100 |
if ( *ept==' ' ) |
| 101 |
last_space = ept; |
| 102 |
if ( *ept!='\n' && *ept!='\0' && last_space!=NULL ) |
| 103 |
ept = last_space; |
| 104 |
errinfo.lines[line] = def2u_strncpy(ue,pt,ept-pt); |
| 105 |
ue[ept-pt] = '\0'; ue += (ept+1-pt); |
| 106 |
if ( *ept=='\n' || *ept==' ' ) ++ept; |
| 107 |
pt = ept; |
| 108 |
} |
| 109 |
for ( ; line<ERR_LINE_MAX ; ++line ) |
| 110 |
errinfo.lines[line] = NULL; |
| 111 |
errinfo.err_type = et; |
| 112 |
|
| 113 |
max_len = 0; |
| 114 |
for ( line = 0; line<ERR_LINE_MAX && errinfo.lines[line]!=NULL; ++line ) { |
| 115 |
len = GDrawGetTextWidth(error,errinfo.lines[line],-1); |
| 116 |
if ( len>max_len ) max_len = len; |
| 117 |
} |
| 118 |
errinfo.width = max_len+30; |
| 119 |
GDrawResize(error,max_len+30,15*line+50); |
| 120 |
} |
| 121 |
|
| 122 |
GDisplay *global_gd; |
| 123 |
|
| 124 |
void _GDraw_InitError(GDisplay *gd) { |
| 125 |
GRect screen, pos; |
| 126 |
static unichar_t title[]= { 'E', 'r', 'r', 'o', 'r', '\0' }; |
| 127 |
static GDisplay *static_gd; |
| 128 |
GWindowAttrs wattrs; |
| 129 |
FontRequest rq; |
| 130 |
|
| 131 |
if ( gd!=NULL ) |
| 132 |
static_gd = gd; |
| 133 |
else |
| 134 |
screen_display = gd = static_gd; |
| 135 |
|
| 136 |
global_gd = static_gd; |
| 137 |
|
| 138 |
if ( gd==NULL ) |
| 139 |
return; |
| 140 |
|
| 141 |
if ( error != NULL ) |
| 142 |
return; |
| 143 |
GDrawGetSize(GDrawGetRoot(gd),&screen); |
| 144 |
|
| 145 |
memset(&wattrs,0,sizeof(wattrs)); |
| 146 |
wattrs.mask = wam_events|wam_positioned|wam_cursor|wam_wtitle|wam_backcol| |
| 147 |
wam_restrict|wam_redirect|wam_isdlg; |
| 148 |
wattrs.event_masks = -1; |
| 149 |
wattrs.positioned = 1; |
| 150 |
wattrs.cursor = ct_pointer; |
| 151 |
wattrs.window_title = title; |
| 152 |
wattrs.background_color = 0xbbbbbb; |
| 153 |
wattrs.restrict_input_to_me = true; |
| 154 |
wattrs.redirect_chars_to_me = true; |
| 155 |
wattrs.is_dlg = true; |
| 156 |
pos.width = 300; pos.height = 180; |
| 157 |
pos.x = (screen.width-pos.width)/2; |
| 158 |
pos.y = (screen.width-pos.width)/3; |
| 159 |
errinfo.width = pos.width; |
| 160 |
|
| 161 |
error = GDrawCreateTopWindow(gd,&pos,e_h,NULL,&wattrs); |
| 162 |
|
| 163 |
|
| 164 |
memset(&rq,0,sizeof(rq)); |
| 165 |
rq.utf8_family_name = MONO_UI_FAMILIES; |
| 166 |
rq.point_size = -12; |
| 167 |
rq.weight = 400; |
| 168 |
rq.style = 0; |
| 169 |
GDrawAttachFont(error,&rq); |
| 170 |
} |
| 171 |
|
| 172 |
void GDrawIError(const char *fmt,...) { |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
GDisplay * gd = global_gd; |
| 177 |
char * buffer = NULL; |
| 178 |
va_list ap; |
| 179 |
va_start(ap, fmt); |
| 180 |
buffer = vsmprintf(fmt, ap); |
| 181 |
va_end(ap); |
| 182 |
if (buffer != NULL ) { |
| 183 |
if ( gd==NULL ) { |
| 184 |
fprintf(stderr, "%s", buffer); |
| 185 |
} else { |
| 186 |
if ((gd->err_flag) && (gd->err_report != NULL)) { |
| 187 |
if (strlen(gd->err_report) + strlen(buffer) + 1 < 2048) { |
| 188 |
|
| 189 |
char * tmp = smprintf("%s%s\n", gd->err_report, buffer); |
| 190 |
free(gd->err_report); gd->err_report = tmp; |
| 191 |
} |
| 192 |
} else { |
| 193 |
|
| 194 |
gd->err_report = smprintf("%s\n", buffer); |
| 195 |
} |
| 196 |
gd->err_flag |= 1; |
| 197 |
} |
| 198 |
} |
| 199 |
free(buffer); buffer = NULL; |
| 200 |
} |
| 201 |
|
| 202 |
void GDrawIErrorRun(const char *fmt,...) { |
| 203 |
char buf[1025]; unichar_t ubuf[1025]; |
| 204 |
va_list ap; |
| 205 |
|
| 206 |
strcpy(buf,"Internal Error:\n"); |
| 207 |
va_start(ap, fmt); |
| 208 |
vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, ap); |
| 209 |
va_end(ap); |
| 210 |
fprintf( stderr, "%s\n", buf ); |
| 211 |
_GDraw_InitError(NULL); |
| 212 |
if ( error!=NULL ) { |
| 213 |
ProcessText(ubuf,buf,et_error); |
| 214 |
RunError(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void GDrawError(const char *fmt,...) { |
| 219 |
char buf[1025]; unichar_t ubuf[1025]; |
| 220 |
va_list ap; |
| 221 |
|
| 222 |
va_start(ap, fmt); |
| 223 |
vsprintf(buf, fmt, ap); |
| 224 |
va_end(ap); |
| 225 |
_GDraw_InitError(NULL); |
| 226 |
if ( error==NULL ) |
| 227 |
fprintf( stderr, "%s\n", buf ); |
| 228 |
else { |
| 229 |
ProcessText(ubuf,buf,et_error); |
| 230 |
RunError(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void GDrawFatalError(const char *fmt,...) { |
| 235 |
char buf[1025]; unichar_t ubuf[1025]; |
| 236 |
va_list ap; |
| 237 |
|
| 238 |
strcpy(buf,"Fatal Error:\n"); |
| 239 |
va_start(ap, fmt); |
| 240 |
vsprintf(buf+strlen(buf), fmt, ap); |
| 241 |
va_end(ap); |
| 242 |
fprintf( stderr, "%s\n", buf ); |
| 243 |
if ( error!=NULL ) { |
| 244 |
ProcessText(ubuf,buf,et_fatal); |
| 245 |
RunError(); |
| 246 |
} |
| 247 |
exit(1); |
| 248 |
} |