gedit-file-chooser-dialog-osx.c 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * gedit-file-chooser-dialog-osx.c
 * This file is part of gedit
 *
 * Copyright (C) 2014 - Jesse van den Kieboom
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

25 26
#include "gedit-file-chooser-dialog-osx.h"

27 28 29 30 31 32
#import <Cocoa/Cocoa.h>
#include <gdk/gdkquartz.h>
#include <glib/gi18n.h>

#include "gedit-encoding-items.h"
#include "gedit-encodings-dialog.h"
33
#include "gedit-utils.h"
34

35
struct _GeditFileChooserDialogOSX
36
{
37 38
	GObject parent_instance;

39 40 41 42 43 44 45 46 47
	/* Note this can be either an NSSavePanel or NSOpenPanel,
	 * and NSOpenPanel inherits from NSSavePanel. */
	NSSavePanel *panel;
	GtkWindow *parent;
	NSPopUpButton *newline_button;
	NSPopUpButton *encoding_button;

	gboolean is_open;
	gboolean is_modal;
48
	gboolean is_running;
49

50 51 52
	GtkResponseType cancel_response;
	GtkResponseType accept_response;

53 54
	gulong destroy_id;

55 56 57 58 59 60 61 62 63 64
	GeditFileChooserFlags flags;
};

static void gedit_file_chooser_dialog_osx_chooser_init (gpointer g_iface, gpointer iface_data);

G_DEFINE_TYPE_EXTENDED (GeditFileChooserDialogOSX,
                        gedit_file_chooser_dialog_osx,
                        G_TYPE_OBJECT,
                        0,
                        G_IMPLEMENT_INTERFACE (GEDIT_TYPE_FILE_CHOOSER_DIALOG,
65
                                               gedit_file_chooser_dialog_osx_chooser_init))
66 67 68 69 70 71 72 73 74 75 76 77 78 79

@interface NewlineItem : NSMenuItem

@property (readonly) GtkSourceNewlineType newline_type;

-(id)initWithType:(GtkSourceNewlineType)type;

@end

@implementation NewlineItem
-(id)initWithType:(GtkSourceNewlineType)type
{
	NSString *title;

80
	title = [NSString stringWithUTF8String:gedit_utils_newline_type_to_string (type)];
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	self = [super initWithTitle:title action:nil keyEquivalent:@""];

	if (self)
	{
		_newline_type = type;
	}

	return self;
}
@end

@interface EncodingItem : NSMenuItem

@property (readonly) GeditEncodingItem *encoding;

-(id)initWithEncoding:(GeditEncodingItem *)encoding;

-(void)dealloc;
-(const GtkSourceEncoding *)source_encoding;

@end

@implementation EncodingItem
-(id)initWithEncoding:(GeditEncodingItem *)encoding
{
	NSString *title;

	title = [NSString stringWithUTF8String:gedit_encoding_item_get_name (encoding)];

	self = [super initWithTitle:title action:nil keyEquivalent:@""];

	if (self)
	{
		_encoding = encoding;
	}

	return self;
}

-(void)dealloc
{
	gedit_encoding_item_free (_encoding);
	_encoding = NULL;

	[super dealloc];
}

-(const GtkSourceEncoding *)source_encoding
{
	if (_encoding != NULL)
	{
		return gedit_encoding_item_get_encoding (_encoding);
	}

	return NULL;
}

@end

static void
chooser_set_encoding (GeditFileChooserDialog  *dialog,
                      const GtkSourceEncoding *encoding)
{
145
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
146 147
	gint i;

148
	if (dialog_osx->encoding_button == NULL)
149 150 151 152
	{
		return;
	}

153
	NSMenu *menu = [dialog_osx->encoding_button menu];
154 155 156 157 158 159 160 161 162 163 164 165
	NSArray *items = [menu itemArray];

	for (i = 0; i < [items count]; i++)
	{
		NSMenuItem *item = [items objectAtIndex:i];

		if ([item isKindOfClass:[EncodingItem class]])
		{
			EncodingItem *eitem = (EncodingItem *)item;

			if ([eitem source_encoding] == encoding)
			{
166
				[dialog_osx->encoding_button selectItemAtIndex:i];
167 168 169 170 171 172 173 174 175
				break;
			}
		}
	}
}

static const GtkSourceEncoding *
chooser_get_encoding (GeditFileChooserDialog *dialog)
{
176
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
177 178
	NSMenuItem *item;

179
	if (dialog_osx->encoding_button == NULL)
180 181 182 183
	{
		return gtk_source_encoding_get_utf8 ();
	}

184
	item = [dialog_osx->encoding_button selectedItem];
185 186 187 188 189 190 191 192 193 194 195 196 197

	if (item != nil && [item isKindOfClass:[EncodingItem class]])
	{
		return [(EncodingItem *)item source_encoding];
	}

	return NULL;
}

static void
chooser_set_newline_type (GeditFileChooserDialog *dialog,
                          GtkSourceNewlineType    newline_type)
{
198
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
199 200
	gint i;

201
	if (dialog_osx->newline_button == NULL)
202 203 204 205
	{
		return;
	}

206
	NSMenu *menu = [dialog_osx->newline_button menu];
207 208 209 210 211 212 213 214
	NSArray *items = [menu itemArray];

	for (i = 0; i < [items count]; i++)
	{
		NewlineItem *item = (NewlineItem *)[items objectAtIndex:i];

		if (item.newline_type == newline_type)
		{
215
			[dialog_osx->newline_button selectItemAtIndex:i];
216 217 218 219 220 221 222 223
			break;
		}
	}
}

static GtkSourceNewlineType
chooser_get_newline_type (GeditFileChooserDialog *dialog)
{
224
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
225

226
	if (dialog_osx->newline_button == NULL)
227 228 229 230
	{
		return GTK_SOURCE_NEWLINE_TYPE_DEFAULT;
	}

231
	NewlineItem *item = (NewlineItem *)[dialog_osx->newline_button selectedItem];
232 233 234 235 236 237 238
	return item.newline_type;
}

static void
chooser_set_current_folder (GeditFileChooserDialog *dialog,
                            GFile                  *folder)
{
239
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
240 241 242 243 244 245

	if (folder != NULL)
	{
		gchar *uri;

		uri = g_file_get_uri (folder);
246
		[dialog_osx->panel setDirectoryURL:[NSURL URLWithString:[NSString stringWithUTF8String:uri]]];
247 248 249 250 251 252 253 254
		g_free (uri);
	}
}

static void
chooser_set_current_name (GeditFileChooserDialog *dialog,
                          const gchar            *name)
{
255
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
256

257
	[dialog_osx->panel setNameFieldStringValue:[NSString stringWithUTF8String:name]];
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

static void
chooser_set_file (GeditFileChooserDialog *dialog,
                  GFile                  *file)
{
	GFile *folder;
	gchar *name;

	if (file == NULL)
	{
		return;
	}

	folder = g_file_get_parent (file);
	name = g_file_get_basename (file);

	chooser_set_current_folder (dialog, folder);
	chooser_set_current_name (dialog, name);

	g_object_unref (folder);
	g_free (name);
}

static GSList *chooser_get_files (GeditFileChooserDialog *dialog);

static GFile *
ns_url_to_g_file (NSURL *url)
{
	if (url == nil)
	{
		return NULL;
	}

	return g_file_new_for_uri ([[url absoluteString] UTF8String]);
}

static GFile *
chooser_get_file (GeditFileChooserDialog *dialog)
{
298
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
299

300
	if (dialog_osx->is_open)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	{
		GSList *ret;
		GFile *file = NULL;

		ret = chooser_get_files (dialog);

		if (ret != NULL)
		{
			file = ret->data;
			ret = g_slist_delete_link (ret, ret);
		}

		g_slist_free_full (ret, (GDestroyNotify)g_object_unref);
		return file;
	}
	else
	{
318
		return ns_url_to_g_file ([dialog_osx->panel URL]);
319 320 321 322 323 324 325
	}
}


static GSList *
chooser_get_files (GeditFileChooserDialog *dialog)
{
326
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
327 328 329

	GSList *ret = NULL;

330
	if (dialog_osx->is_open)
331 332 333 334
	{
		NSArray *urls;
		gint i;

335
		urls = [(NSOpenPanel *)dialog_osx->panel URLs];
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

		for (i = 0; i < [urls count]; i++)
		{
			NSURL *url;

			url = (NSURL *)[urls objectAtIndex:i];
			ret = g_slist_prepend (ret, ns_url_to_g_file (url));
		}
	}
	else
	{
		GFile *file;

		file = chooser_get_file (dialog);

		if (file != NULL)
		{
			ret = g_slist_prepend (ret, file);
		}
	}

	return g_slist_reverse (ret);
}

static void
chooser_set_do_overwrite_confirmation (GeditFileChooserDialog *dialog,
                                       gboolean                overwrite_confirmation)
{
	// TODO: this is only implementable on OS X through hacks
	// I guess it's better just not having it
}

static void
fill_encodings (GeditFileChooserDialogOSX *dialog)
{
	NSPopUpButton *button;
	GSList *encodings;
	NSMenu *menu;
	gint i = 0;
	gint first = 0;
	const GtkSourceEncoding *encoding;

	encoding = gedit_file_chooser_dialog_get_encoding (GEDIT_FILE_CHOOSER_DIALOG (dialog));

380
	button = dialog->encoding_button;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	menu = [button menu];

	while (i < [menu numberOfItems])
	{
		NSMenuItem *item = [menu itemAtIndex:i];

		if ([item isKindOfClass:[EncodingItem class]])
		{
			EncodingItem *eitem = (EncodingItem *)item;

			if ([eitem source_encoding] != NULL)
			{
				if (first == 0)
				{
					first = i;
				}

				[menu removeItemAtIndex:i];
			}
			else
			{
				i++;
			}
		}
		else
		{
			i++;

409
			if ((dialog->flags & GEDIT_FILE_CHOOSER_OPEN) != 0 && first == 0)
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
			{
				first = i;
			}
		}
	}

	encodings = gedit_encoding_items_get ();

	while (encodings)
	{
		GeditEncodingItem *item = encodings->data;

		[menu insertItem:[[EncodingItem alloc] initWithEncoding:item] atIndex:first];

		if (encoding == gedit_encoding_item_get_encoding (item))
		{
			[button selectItemAtIndex:first];
		}

		first++;
		encodings = g_slist_delete_link (encodings, encodings);
	}

	if (encoding == NULL)
	{
		[button selectItemAtIndex:0];
	}
}

static void
dialog_response_cb (GtkDialog                 *dialog,
                    gint                       response_id,
                    GeditFileChooserDialogOSX *chooser_dialog)
{
	if (response_id == GTK_RESPONSE_OK)
	{
		fill_encodings (chooser_dialog);
	}

	if (response_id != GTK_RESPONSE_HELP)
	{
451
		[chooser_dialog->panel setAlphaValue:1.0f];
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
		gtk_widget_destroy (GTK_WIDGET (dialog));
	}
}

@interface ConfigureEncodings : NSObject {
	GeditFileChooserDialogOSX *_dialog;
	const GtkSourceEncoding *_current;
}

-(id)initWithDialog:(GeditFileChooserDialogOSX *)dialog;

-(void)activateConfigure:(id)sender;
-(void)selectionChanged:(id)sender;

@end

@implementation ConfigureEncodings

-(id)initWithDialog:(GeditFileChooserDialogOSX *)dialog
{
	self = [super init];

	if (self)
	{
		_dialog = dialog;
	}

	return self;
}

-(void)activateConfigure:(id)sender
{
	GtkWidget *dialog;

	gedit_file_chooser_dialog_set_encoding (GEDIT_FILE_CHOOSER_DIALOG (_dialog),
	                                        _current);

	dialog = gedit_encodings_dialog_new ();

491
	if (_dialog->parent != NULL)
492 493 494
	{
		GtkWindowGroup *wg;

495
		gtk_window_set_transient_for (GTK_WINDOW (dialog), _dialog->parent);
496

497
		if (gtk_window_has_group (_dialog->parent))
498
		{
499
			wg = gtk_window_get_group (_dialog->parent);
500 501 502 503
		}
		else
		{
			wg = gtk_window_group_new ();
504
			gtk_window_group_add_window (wg, _dialog->parent);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		}

		gtk_window_group_add_window (wg, GTK_WINDOW (dialog));
	}

	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

	g_signal_connect_after (dialog,
				"response",
				G_CALLBACK (dialog_response_cb),
				_dialog);

	gtk_widget_show (dialog);

	/* We set the alpha value to 0 here because it was the only way that we could
	   find to make the panel not receive any events. Ideally the encodings dialog
	   should be modal on top of the NSPanel, but the modal loops of gtk and NS
	   do not mix. This is an ugly hack, but at least we can't interact with the
	   NSPanel while showing the encodings dialog... */
524
	[_dialog->panel setAlphaValue:0.0f];
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
}

-(void)selectionChanged:(id)sender
{
	_current = gedit_file_chooser_dialog_get_encoding (GEDIT_FILE_CHOOSER_DIALOG (_dialog));
}

@end

static gint
create_encoding_combo (GeditFileChooserDialogOSX *dialog,
                       NSView                    *container)
{
	NSTextField *label;
	NSPopUpButton *button;
	NSMenu *menu;
	ConfigureEncodings *config;
	NSMenuItem *citem;

	label = [NSTextField new];

	[label setTranslatesAutoresizingMaskIntoConstraints:NO];
	[label setStringValue:[NSString stringWithUTF8String:_("Character Encoding:")]];
	[label setDrawsBackground:NO];
	[label setBordered:NO];
	[label setBezeled:NO];
	[label setSelectable:NO];
	[label setEditable:NO];

	[container addSubview:label];

	button = [NSPopUpButton new];
	[button setTranslatesAutoresizingMaskIntoConstraints:NO];

	menu = [button menu];

561
	if ((dialog->flags & GEDIT_FILE_CHOOSER_OPEN) != 0)
562 563 564 565 566 567 568 569 570 571 572 573
	{
		NSString *title;

		title = [NSString stringWithUTF8String:_("Automatically Detected")];

		[menu addItem:[[EncodingItem alloc] initWithTitle:title action:nil keyEquivalent:@""]];
		[menu addItem:[NSMenuItem separatorItem]];
	}

	config = [[ConfigureEncodings alloc] initWithDialog:dialog];

	[menu addItem:[NSMenuItem separatorItem]];
574
	citem = [[EncodingItem alloc] initWithTitle:[NSString stringWithUTF8String:_("Add or Remove…")]
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
	                                     action:@selector(activateConfigure:)
	                              keyEquivalent:@""];

	[citem setTarget:config];
	[menu addItem:citem];

	[button setTarget:config];
	[button setAction:@selector(selectionChanged:)];

	[button synchronizeTitleAndSelectedItem];
	[config selectionChanged:nil];

	[container addSubview:button];

	[container addConstraint:[NSLayoutConstraint constraintWithItem:button
	                                                      attribute:NSLayoutAttributeTop
	                                                      relatedBy:NSLayoutRelationEqual
	                                                         toItem:container
	                                                      attribute:NSLayoutAttributeTop
	                                                     multiplier:1
	                                                       constant:3]];

	[container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[label]-[button]"
	                                                                  options:NSLayoutFormatAlignAllBaseline
	                                                                  metrics:nil
	                                                                    views:NSDictionaryOfVariableBindings(label, button)]];

602
	dialog->encoding_button = button;
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

	fill_encodings (dialog);

	return (gint)([button intrinsicContentSize].width + [label intrinsicContentSize].width);
}

static gint
create_newline_combo (GeditFileChooserDialogOSX *dialog,
                      NSView                    *container)
{
	NSTextField *label;
	NSMenu *menu;
	NSPopUpButton *button;

	label = [NSTextField new];

	[label setTranslatesAutoresizingMaskIntoConstraints:NO];
	[label setStringValue:[NSString stringWithUTF8String:_("Line Ending:")]];
	[label setDrawsBackground:NO];
	[label setBordered:NO];
	[label setBezeled:NO];
	[label setSelectable:NO];
	[label setEditable:NO];

	[container addSubview:label];

	button = [NSPopUpButton new];

	[button setTranslatesAutoresizingMaskIntoConstraints:NO];

	menu = [button menu];

	[menu addItem:[[NewlineItem alloc] initWithType:GTK_SOURCE_NEWLINE_TYPE_LF]];
	[menu addItem:[[NewlineItem alloc] initWithType:GTK_SOURCE_NEWLINE_TYPE_CR]];
	[menu addItem:[[NewlineItem alloc] initWithType:GTK_SOURCE_NEWLINE_TYPE_CR_LF]];

	[button synchronizeTitleAndSelectedItem];

	[container addSubview:button];

	[container addConstraint:[NSLayoutConstraint constraintWithItem:button
	                                                      attribute:NSLayoutAttributeTop
	                                                      relatedBy:NSLayoutRelationEqual
	                                                         toItem:container
	                                                      attribute:NSLayoutAttributeTop
	                                                     multiplier:1
	                                                       constant:3]];

	[container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[label]-[button]-|"
	                                                                  options:NSLayoutFormatAlignAllBaseline
	                                                                  metrics:nil
	                                                                    views:NSDictionaryOfVariableBindings(label, button)]];

656
	dialog->newline_button = button;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

	return (gint)([button intrinsicContentSize].width + [label intrinsicContentSize].width);
}

static void
create_extra_widget (GeditFileChooserDialogOSX *dialog)
{
	gboolean needs_encoding;
	gboolean needs_line_ending;
	GeditFileChooserFlags flags;
	NSSize size;
	NSView *parent;
	NSView *container;
	gint minw = 0;

672
	flags = dialog->flags;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

	needs_encoding = (flags & GEDIT_FILE_CHOOSER_ENABLE_ENCODING) != 0;
	needs_line_ending = (flags & GEDIT_FILE_CHOOSER_ENABLE_LINE_ENDING) != 0;

	if (!needs_encoding && !needs_line_ending)
	{
		return;
	}

	container = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 400, 30)];

	if (needs_encoding)
	{
		minw += create_encoding_combo (dialog, container);
	}

	if (needs_line_ending)
	{
		minw += create_newline_combo (dialog, container);
	}

	minw += 90;

	[container setFrame:NSMakeRect(0, 0, minw, 30)];

698
	[dialog->panel setAccessoryView:container];
699 700 701 702 703 704 705 706 707 708 709 710 711

	parent = [[container superview] superview];

	if ([parent isKindOfClass:[NSBox class]])
	{
		NSBox *box = (NSBox *)parent;
		[box setTransparent:YES];
	}

	size = [[container superview] frame].size;
	size.width = minw;

	[container setFrame:NSMakeRect(0, 0, size.width, size.height)];
712
	[dialog->panel setContentMinSize:size];
713 714 715 716 717
}

static void
chooser_show (GeditFileChooserDialog *dialog)
{
718
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
719

720
	if (dialog_osx->is_running)
721 722
	{
		// Just show it again
723
		[dialog_osx->panel makeKeyAndOrderFront:nil];
724 725 726
		return;
	}

727
	dialog_osx->is_running = TRUE;
728 729 730 731 732 733

	void (^handler)(NSInteger ret) = ^(NSInteger result) {
		GtkResponseType response;

		if (result == NSFileHandlingPanelOKButton)
		{
734
			response = dialog_osx->accept_response;
735 736 737
		}
		else
		{
738
			response = dialog_osx->cancel_response;
739 740 741 742 743
		}

		g_signal_emit_by_name (dialog, "response", response);
	};

744
	if (dialog_osx->parent != NULL && dialog_osx->is_modal)
745 746 747 748
	{
		GdkWindow *win;
		NSWindow *nswin;

749
		win = gtk_widget_get_window (GTK_WIDGET (dialog_osx->parent));
750 751
		nswin = gdk_quartz_window_get_nswindow (win);

752
		[dialog_osx->panel setLevel:NSModalPanelWindowLevel];
753

754
		[dialog_osx->panel beginSheetModalForWindow:nswin completionHandler:handler];
755 756 757
	}
	else
	{
758 759
		[dialog_osx->panel setLevel:NSModalPanelWindowLevel];
		[dialog_osx->panel beginWithCompletionHandler:handler];
760 761 762
	}
}

763 764 765
static void
chooser_hide (GeditFileChooserDialog *dialog)
{
766
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
767

768
	if (!dialog_osx->is_running || dialog_osx->panel == NULL)
769 770 771 772
	{
		return;
	}

773
	[dialog_osx->panel orderOut:nil];
774 775
}

776 777 778
static void
chooser_destroy (GeditFileChooserDialog *dialog)
{
779
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
780

781
	if (dialog_osx->parent != NULL)
782
	{
783 784
		g_object_remove_weak_pointer (G_OBJECT (dialog_osx->parent),
		                              (gpointer *)&dialog_osx->parent);
785

786
		if (dialog_osx->destroy_id != 0)
787
		{
788 789
			g_signal_handler_disconnect (dialog_osx->parent, dialog_osx->destroy_id);
			dialog_osx->destroy_id = 0;
790 791 792
		}
	}

793
	if (dialog_osx->panel != NULL)
794
	{
795 796
		[dialog_osx->panel close];
		dialog_osx->panel = NULL;
797 798
	}

799 800 801 802 803 804 805
	g_object_unref (dialog);
}

static void
chooser_set_modal (GeditFileChooserDialog *dialog,
                   gboolean is_modal)
{
806
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (dialog);
807

808
	dialog_osx->is_modal = is_modal;
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
}

static void
gedit_file_chooser_dialog_osx_chooser_init (gpointer g_iface,
                                            gpointer iface_data)
{
	GeditFileChooserDialogInterface *iface = g_iface;

	iface->set_encoding = chooser_set_encoding;
	iface->get_encoding = chooser_get_encoding;

	iface->set_newline_type = chooser_set_newline_type;
	iface->get_newline_type = chooser_get_newline_type;

	iface->set_current_folder = chooser_set_current_folder;
	iface->set_current_name = chooser_set_current_name;
	iface->set_file = chooser_set_file;
	iface->get_file = chooser_get_file;
	iface->get_files = chooser_get_files;
	iface->set_do_overwrite_confirmation = chooser_set_do_overwrite_confirmation;
	iface->show = chooser_show;
830
	iface->hide = chooser_hide;
831 832 833 834 835 836 837
	iface->destroy = chooser_destroy;
	iface->set_modal = chooser_set_modal;
}

static void
gedit_file_chooser_dialog_osx_dispose (GObject *object)
{
838
	GeditFileChooserDialogOSX *dialog_osx = GEDIT_FILE_CHOOSER_DIALOG_OSX (object);
839

840
	if (dialog_osx->panel != NULL)
841
	{
842 843
		[dialog_osx->panel close];
		dialog_osx->panel = NULL;
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
	}

	if (G_OBJECT_CLASS (gedit_file_chooser_dialog_osx_parent_class)->dispose != NULL)
	{
		G_OBJECT_CLASS (gedit_file_chooser_dialog_osx_parent_class)->dispose (object);
	}
}

static void
gedit_file_chooser_dialog_osx_class_init (GeditFileChooserDialogOSXClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = gedit_file_chooser_dialog_osx_dispose;
}

static void
gedit_file_chooser_dialog_osx_init (GeditFileChooserDialogOSX *dialog)
{
}

@protocol CanSetShowsTagField
- (void)setShowsTagField:(BOOL)val;
@end

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
static gchar *
strip_mnemonic (const gchar *s)
{
	gchar *escaped;
	gchar *ret = NULL;

	escaped = g_markup_escape_text (s, -1);
	pango_parse_markup (escaped, -1, '_', NULL, &ret, NULL, NULL);

	if (ret != NULL)
	{
		return ret;
	}
	else
	{
		return g_strdup (s);
	}
}

888 889 890 891 892 893 894
static void
on_parent_destroyed (GtkWindow                 *parent,
                     GeditFileChooserDialogOSX *dialog)
{
	chooser_destroy (GEDIT_FILE_CHOOSER_DIALOG (dialog));
}

895 896 897 898
GeditFileChooserDialog *
gedit_file_chooser_dialog_osx_create (const gchar             *title,
			              GtkWindow               *parent,
			              GeditFileChooserFlags    flags,
899 900 901 902 903
			              const GtkSourceEncoding *encoding,
			              const gchar             *cancel_label,
			              GtkResponseType          cancel_response,
			              const gchar             *accept_label,
			              GtkResponseType          accept_response)
904 905
{
	GeditFileChooserDialogOSX *ret;
906
	gchar *nomnem;
907 908 909

	ret = g_object_new (GEDIT_TYPE_FILE_CHOOSER_DIALOG_OSX, NULL);

910 911
	ret->cancel_response = cancel_response;
	ret->accept_response = accept_response;
912

913 914 915 916 917 918 919 920 921
	if ((flags & GEDIT_FILE_CHOOSER_SAVE) != 0)
	{
		NSSavePanel *panel = [[NSSavePanel savePanel] retain];

		if ([panel respondsToSelector:@selector(setShowsTagField:)])
		{
			[(id<CanSetShowsTagField>)panel setShowsTagField:NO];
		}

922 923
		ret->panel = panel;
		ret->is_open = FALSE;
924 925 926 927 928 929 930 931
	}
	else
	{
		NSOpenPanel *panel = [[NSOpenPanel openPanel] retain];

		[panel setAllowsMultipleSelection:YES];
		[panel setCanChooseDirectories:NO];

932 933
		ret->panel = panel;
		ret->is_open = TRUE;
934 935
	}

936
	[ret->panel setReleasedWhenClosed:YES];
937

938
	nomnem = strip_mnemonic (accept_label);
939
	[ret->panel setPrompt:[NSString stringWithUTF8String:nomnem]];
940 941
	g_free (nomnem);

942 943
	if (parent != NULL)
	{
944 945
		ret->parent = parent;
		g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *)&ret->parent);
946

947 948 949 950
		ret->destroy_id = g_signal_connect (parent,
		                                    "destroy",
		                                    G_CALLBACK (on_parent_destroyed),
		                                    ret);
951 952
	}

953
	ret->flags = flags;
954 955
	create_extra_widget (ret);

956
	[ret->panel setTitle:[NSString stringWithUTF8String:title]];
957 958 959 960
	return GEDIT_FILE_CHOOSER_DIALOG (ret);
}

/* ex:set ts=8 noet: */