ToolTalk ユーザーズガイド

オブジェクト指向メッセージの例

edit_demo というプログラムを実行すれば、ToolTalk のオブジェクト指向メッセージのデモを見ることができます。このデモプログラムは、cntledit という 2 つのプログラムで構成されています。cntl の方は ToolTalk サービスを使用して、指定されたファイルを編集する編集プロセスを起動します。edit を使用すれば、ToolTalk オブジェクトを作成し、その指定されたファイル中のテキストと関連付けることができます。オブジェクトを作成してテキストと関連付けると、cntl を使用してそのファイルにオブジェクトを照会し、そのオブジェクトにメッセージを送信できます。

例 12–3 は、C 言語形式のコメントを前後につけてユーザーのオブジェクトを作成します。2 つのコード例から成り立ちます。オブジェクト仕様を作成して otype を設定し、その仕様を ToolTalk データベースに書き込むことにより、ユーザーの選択をラップします。さらに、アプリケーションは ToolTalk_EditDemo_new_object 操作を使って新しいオブジェクトを作成し、メッセージを監視する他のアプリケーションを更新した後、手続きアドレス指定された通知も送信します。他のアプリケーションが、ToolTalk_EditDemo が管理するファイル内のオブジェクトのリストを表示している場合、この通知を受信した後にリストを更新します。


例 12–3 オブジェクトの作成 - 1

/*
 * Make a ToolTalk spec out of the selected text in this textpane. Once
 * the spec is successfully created and written to a database, wrap the
 * text with C-style comments in order to delimit the object and send out
 * a notification that an object has been created in this file.
 */
Menu_item
edit_ui_make_object(item, event)
   Panel_item		item;
   Event		*event;
{
         int               mark = tt_mark();
	char		*objid;
	char		*file;
	char		*sel;
	Textsw_index		first, last;
	char		obj_start_text[100];
	char		obj_end_text[100];
	Tt_message		msg;

	if (! get_selection(edit_ui_xserver, edit_ui_textpane,
			  &sel, &first, &last)) {
		xv_set(edit_ui_base_window, FRAME_LEFT_FOOTER,
		       “First select some text”, NULL);
		tt_release(mark);
		return item;
	}
	file = tt_default_file();

	if (file == (char *)0) {
		xv_set(edit_ui_base_window, FRAME_LEFT_FOOTER,
		       “Not editing any file”, NULL);
		tt_release(mark);
		return item;
	}



例 12–4 オブジェクトの作成 - 2

/*
	/* create a new spec */

	objid = tt_spec_create(tt_default_file());
	if (tt_pointer_error(objid) != TT_OK) {
		xv_set(edit_ui_base_window, FRAME_LEFT_FOOTER,
		       “Couldn't create object”, NULL);
		tt_release(mark);
		return item;
	}

	/* set its otype */

	tt_spec_type_set(objid, “Sun_EditDemo_object”);
	if (tt_spec_write(objid) != TT_OK) {
		xv_set(edit_ui_base_window, FRAME_LEFT_FOOTER,
		       “Couldn't write out object”, NULL);
		tt_release(mark);
		return item;
	}

	/* wrap spec's contents (the selected text) with C-style */
	/* comments. */

	sprintf(obj_start_text,” /* begin_object(%s) */”, objid);
	sprintf(obj_end_text,”	/* end_object(%s) */”, objid);
	(void)wrap_selection(edit_ui_xserver, edit_ui_textpane,
			   obj_start_text, obj_end_text);

	/* now send out a notification that we've added a new object */

	msg = tt_pnotice_create(TT_FILE_IN_SESSION,”Sun_EditDemo_new_object”);
	tt_message_file_set(msg, file);
	tt_message_send(msg);

	tt_release(mark);
	return item;
}