comparison mac/dw.m @ 1204:5cb7e52f76c7

Put Mac MLE under mutex protection because of crashes when accessing the MLE from threads.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 30 Sep 2011 05:03:25 +0000
parents 5c1a01c6384d
children c7bb48cda53a
comparison
equal deleted inserted replaced
1203:fc87309372ef 1204:5cb7e52f76c7
4297 * startpoint: Point to start entering text. 4297 * startpoint: Point to start entering text.
4298 */ 4298 */
4299 unsigned int API dw_mle_import(HWND handle, char *buffer, int startpoint) 4299 unsigned int API dw_mle_import(HWND handle, char *buffer, int startpoint)
4300 { 4300 {
4301 NSScrollView *sv = handle; 4301 NSScrollView *sv = handle;
4302 int _locked_by_me = FALSE;
4303 DW_MUTEX_LOCK;
4302 DWMLE *mle = [sv documentView]; 4304 DWMLE *mle = [sv documentView];
4303 NSTextStorage *ts = [mle textStorage]; 4305 NSTextStorage *ts = [mle textStorage];
4304 NSString *nstr = [NSString stringWithUTF8String:buffer]; 4306 NSString *nstr = [NSString stringWithUTF8String:buffer];
4305 NSMutableString *ms = [ts mutableString]; 4307 NSMutableString *ms = [ts mutableString];
4306 NSUInteger length = [ms length]; 4308 NSUInteger length = [ms length];
4307 if(startpoint < 0) 4309 if(startpoint < 0)
4308 startpoint = 0; 4310 startpoint = 0;
4309 if(startpoint > length) 4311 if(startpoint > length)
4310 startpoint = (int)length; 4312 startpoint = (int)length;
4311 [ms insertString:nstr atIndex:startpoint]; 4313 [ms insertString:nstr atIndex:startpoint];
4314 DW_MUTEX_UNLOCK;
4312 return (unsigned int)strlen(buffer) + startpoint; 4315 return (unsigned int)strlen(buffer) + startpoint;
4313 } 4316 }
4314 4317
4315 /* 4318 /*
4316 * Grabs text from an MLE box. 4319 * Grabs text from an MLE box.
4321 * length: Amount of text to be grabbed. 4324 * length: Amount of text to be grabbed.
4322 */ 4325 */
4323 void API dw_mle_export(HWND handle, char *buffer, int startpoint, int length) 4326 void API dw_mle_export(HWND handle, char *buffer, int startpoint, int length)
4324 { 4327 {
4325 NSScrollView *sv = handle; 4328 NSScrollView *sv = handle;
4329 int _locked_by_me = FALSE;
4330 DW_MUTEX_LOCK;
4326 DWMLE *mle = [sv documentView]; 4331 DWMLE *mle = [sv documentView];
4327 NSTextStorage *ts = [mle textStorage]; 4332 NSTextStorage *ts = [mle textStorage];
4328 NSMutableString *ms = [ts mutableString]; 4333 NSMutableString *ms = [ts mutableString];
4329 const char *tmp = [ms UTF8String]; 4334 const char *tmp = [ms UTF8String];
4330 strncpy(buffer, tmp+startpoint, length); 4335 strncpy(buffer, tmp+startpoint, length);
4331 buffer[length] = '\0'; 4336 buffer[length] = '\0';
4337 DW_MUTEX_UNLOCK;
4332 } 4338 }
4333 4339
4334 /* 4340 /*
4335 * Obtains information about an MLE box. 4341 * Obtains information about an MLE box.
4336 * Parameters: 4342 * Parameters:
4339 * lines: A pointer to a variable to return the number of lines. 4345 * lines: A pointer to a variable to return the number of lines.
4340 */ 4346 */
4341 void API dw_mle_get_size(HWND handle, unsigned long *bytes, unsigned long *lines) 4347 void API dw_mle_get_size(HWND handle, unsigned long *bytes, unsigned long *lines)
4342 { 4348 {
4343 NSScrollView *sv = handle; 4349 NSScrollView *sv = handle;
4350 int _locked_by_me = FALSE;
4351 DW_MUTEX_LOCK;
4344 DWMLE *mle = [sv documentView]; 4352 DWMLE *mle = [sv documentView];
4345 NSTextStorage *ts = [mle textStorage]; 4353 NSTextStorage *ts = [mle textStorage];
4346 NSMutableString *ms = [ts mutableString]; 4354 NSMutableString *ms = [ts mutableString];
4347 NSUInteger numberOfLines, index, stringLength = [ms length]; 4355 NSUInteger numberOfLines, index, stringLength = [ms length];
4348 4356
4353 for(index=0, numberOfLines=0; index < stringLength; numberOfLines++) 4361 for(index=0, numberOfLines=0; index < stringLength; numberOfLines++)
4354 index = NSMaxRange([ms lineRangeForRange:NSMakeRange(index, 0)]); 4362 index = NSMaxRange([ms lineRangeForRange:NSMakeRange(index, 0)]);
4355 4363
4356 *lines = numberOfLines; 4364 *lines = numberOfLines;
4357 } 4365 }
4366 DW_MUTEX_LOCK;
4358 } 4367 }
4359 4368
4360 /* 4369 /*
4361 * Deletes text from an MLE box. 4370 * Deletes text from an MLE box.
4362 * Parameters: 4371 * Parameters:
4365 * length: Amount of text to be deleted. 4374 * length: Amount of text to be deleted.
4366 */ 4375 */
4367 void API dw_mle_delete(HWND handle, int startpoint, int length) 4376 void API dw_mle_delete(HWND handle, int startpoint, int length)
4368 { 4377 {
4369 NSScrollView *sv = handle; 4378 NSScrollView *sv = handle;
4379 int _locked_by_me = FALSE;
4380 DW_MUTEX_LOCK;
4370 DWMLE *mle = [sv documentView]; 4381 DWMLE *mle = [sv documentView];
4371 NSTextStorage *ts = [mle textStorage]; 4382 NSTextStorage *ts = [mle textStorage];
4372 NSMutableString *ms = [ts mutableString]; 4383 NSMutableString *ms = [ts mutableString];
4373 NSUInteger mslength = [ms length]; 4384 NSUInteger mslength = [ms length];
4374 if(startpoint < 0) 4385 if(startpoint < 0)
4376 if(startpoint > mslength) 4387 if(startpoint > mslength)
4377 startpoint = (int)mslength; 4388 startpoint = (int)mslength;
4378 if(startpoint + length > mslength) 4389 if(startpoint + length > mslength)
4379 length = (int)mslength - startpoint; 4390 length = (int)mslength - startpoint;
4380 [ms deleteCharactersInRange:NSMakeRange(startpoint, length)]; 4391 [ms deleteCharactersInRange:NSMakeRange(startpoint, length)];
4392 DW_MUTEX_UNLOCK;
4381 } 4393 }
4382 4394
4383 /* 4395 /*
4384 * Clears all text from an MLE box. 4396 * Clears all text from an MLE box.
4385 * Parameters: 4397 * Parameters:
4386 * handle: Handle to the MLE to be cleared. 4398 * handle: Handle to the MLE to be cleared.
4387 */ 4399 */
4388 void API dw_mle_clear(HWND handle) 4400 void API dw_mle_clear(HWND handle)
4389 { 4401 {
4390 NSScrollView *sv = handle; 4402 NSScrollView *sv = handle;
4403 int _locked_by_me = FALSE;
4404 DW_MUTEX_LOCK;
4391 DWMLE *mle = [sv documentView]; 4405 DWMLE *mle = [sv documentView];
4392 NSTextStorage *ts = [mle textStorage]; 4406 NSTextStorage *ts = [mle textStorage];
4393 NSMutableString *ms = [ts mutableString]; 4407 NSMutableString *ms = [ts mutableString];
4394 NSUInteger length = [ms length]; 4408 NSUInteger length = [ms length];
4395 [ms deleteCharactersInRange:NSMakeRange(0, length)]; 4409 [ms deleteCharactersInRange:NSMakeRange(0, length)];
4410 DW_MUTEX_UNLOCK;
4396 } 4411 }
4397 4412
4398 /* 4413 /*
4399 * Sets the visible line of an MLE box. 4414 * Sets the visible line of an MLE box.
4400 * Parameters: 4415 * Parameters:
4402 * line: Line to be visible. 4417 * line: Line to be visible.
4403 */ 4418 */
4404 void API dw_mle_set_visible(HWND handle, int line) 4419 void API dw_mle_set_visible(HWND handle, int line)
4405 { 4420 {
4406 NSScrollView *sv = handle; 4421 NSScrollView *sv = handle;
4422 int _locked_by_me = FALSE;
4423 DW_MUTEX_LOCK;
4407 DWMLE *mle = [sv documentView]; 4424 DWMLE *mle = [sv documentView];
4408 NSTextStorage *ts = [mle textStorage]; 4425 NSTextStorage *ts = [mle textStorage];
4409 NSMutableString *ms = [ts mutableString]; 4426 NSMutableString *ms = [ts mutableString];
4410 NSUInteger numberOfLines, index, stringLength = [ms length]; 4427 NSUInteger numberOfLines, index, stringLength = [ms length];
4411 4428
4414 4431
4415 if(line == numberOfLines) 4432 if(line == numberOfLines)
4416 { 4433 {
4417 [mle scrollRangeToVisible:[ms lineRangeForRange:NSMakeRange(index, 0)]]; 4434 [mle scrollRangeToVisible:[ms lineRangeForRange:NSMakeRange(index, 0)]];
4418 } 4435 }
4436 DW_MUTEX_UNLOCK;
4419 } 4437 }
4420 4438
4421 /* 4439 /*
4422 * Sets the editablity of an MLE box. 4440 * Sets the editablity of an MLE box.
4423 * Parameters: 4441 * Parameters:
4465 * point: Point to position cursor. 4483 * point: Point to position cursor.
4466 */ 4484 */
4467 void API dw_mle_set_cursor(HWND handle, int point) 4485 void API dw_mle_set_cursor(HWND handle, int point)
4468 { 4486 {
4469 NSScrollView *sv = handle; 4487 NSScrollView *sv = handle;
4488 int _locked_by_me = FALSE;
4489 DW_MUTEX_LOCK;
4470 DWMLE *mle = [sv documentView]; 4490 DWMLE *mle = [sv documentView];
4471 NSTextStorage *ts = [mle textStorage]; 4491 NSTextStorage *ts = [mle textStorage];
4472 NSMutableString *ms = [ts mutableString]; 4492 NSMutableString *ms = [ts mutableString];
4473 NSUInteger length = [ms length]; 4493 NSUInteger length = [ms length];
4474 if(point < 0) 4494 if(point < 0)
4475 point = 0; 4495 point = 0;
4476 if(point > length) 4496 if(point > length)
4477 point = (int)length; 4497 point = (int)length;
4478 [mle setSelectedRange: NSMakeRange(point,point)]; 4498 [mle setSelectedRange: NSMakeRange(point,point)];
4479 [mle scrollRangeToVisible:NSMakeRange(point,point)]; 4499 [mle scrollRangeToVisible:NSMakeRange(point,point)];
4500 DW_MUTEX_UNLOCK;
4480 } 4501 }
4481 4502
4482 /* 4503 /*
4483 * Finds text in an MLE box. 4504 * Finds text in an MLE box.
4484 * Parameters: 4505 * Parameters:
4488 * flags: Search specific flags. 4509 * flags: Search specific flags.
4489 */ 4510 */
4490 int API dw_mle_search(HWND handle, char *text, int point, unsigned long flags) 4511 int API dw_mle_search(HWND handle, char *text, int point, unsigned long flags)
4491 { 4512 {
4492 NSScrollView *sv = handle; 4513 NSScrollView *sv = handle;
4514 int _locked_by_me = FALSE;
4515 DW_MUTEX_LOCK;
4493 DWMLE *mle = [sv documentView]; 4516 DWMLE *mle = [sv documentView];
4494 NSTextStorage *ts = [mle textStorage]; 4517 NSTextStorage *ts = [mle textStorage];
4495 NSMutableString *ms = [ts mutableString]; 4518 NSMutableString *ms = [ts mutableString];
4496 NSString *searchForMe = [NSString stringWithUTF8String:text]; 4519 NSString *searchForMe = [NSString stringWithUTF8String:text];
4497 NSRange searchRange = NSMakeRange(point, [ms length] - point); 4520 NSRange searchRange = NSMakeRange(point, [ms length] - point);
4500 4523
4501 if(ms) 4524 if(ms)
4502 { 4525 {
4503 range = [ms rangeOfString:searchForMe options:options range:searchRange]; 4526 range = [ms rangeOfString:searchForMe options:options range:searchRange];
4504 } 4527 }
4505 4528 DW_MUTEX_UNLOCK;
4506 if(range.location != NSNotFound) 4529 if(range.location != NSNotFound)
4507 { 4530 {
4508 return -1; 4531 return -1;
4509 } 4532 }
4510 return (int)range.location; 4533 return (int)range.location;