summaryrefslogtreecommitdiffstats
path: root/xparts/xpart_notepad/xp_notepad.cpp
blob: 5dc7157c25791179b1b426b35ec43abedb80b612 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
#include <dcopclient.h>
#include <kapplication.h>
#include <tqmultilineedit.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <stdio.h>
#include <iostream>
#include <xpart.h>
#include <kdebug.h>

#include "xparthost_stub.h"

class XPartNotepad :  public TQMultiLineEdit, virtual public XPart
{

public:
	XPartNotepad(char *)
	{
		setText("This is a xpart component editor");
		setReadOnly( false );
		resize( 200, 200 );
		TQMultiLineEdit::setFocus();
	}

	/** needed by XPartManager to embed the part */
	Q_UINT32 windowId()
	{ return winId(); }

	/** used by XPartManager to show the part */
	void show()
	{ printf("show()\n"); TQWidget::show(); }

	/** sent by the XPartHost to query an url */
	bool openURL( const TQCString& url )
	{ printf("openURL %s\n", (const char *) url ); 
		TQFile f(url);
		TQString s;
		if ( ! f.open(IO_ReadOnly) ) {
			return false;
		}
		TQTextStream t( &f );
		while ( !t.eof() ) {
			s += t.readLine() + "\n";
		}
		f.close();
		setText(s);
		return true;
	}

	/** sent by the XPartHost to close an url */
	bool closeURL()
	{ printf("closeURL\n"); setText(""); return true; } 

	/** called when an action has been activated. name is
	* the name of the
	*      * action, state is used with Toggle actions to
	*      precise the current state.
	*           */
	void activateAction( const TQString &name, int state )
	{ printf("activateAction: %s, %d\n", name.latin1(), state ); }	

	/** are extentions available -> browser extension
	 * / TextEditor ? */
	DCOPRef queryExtension( const TQCString& extension )
	{ printf("query Extension : %s\n", (const char * ) extension ); return DCOPRef(); } 


protected:
	void focusInEvent( TQFocusEvent * ) 
		{ kdDebug() << "Focus in" << endl; TQMultiLineEdit::setFocus(); }
	void focusOutEvent( TQFocusEvent * ) 
		{ kdDebug() << "Focus Out" << endl;TQMultiLineEdit::setFocus(); }
};

int main( int argc, char **argv )
{
	if (argc < 3) {
		printf("application need arguments");
		return 1;
	}
	printf("args: XPartHost appId = %s , XPartHost_KPart objId = %s\n", argv[1], argv[2] );

    KApplication app( argc, argv, "xp_notepad" );
	XPartNotepad * xpn = new XPartNotepad("xp_notepad");
	app.setMainWidget( xpn );
	app.dcopClient()->attach();	
    TQCString appId = app.dcopClient()->registerAs("xp_notepad", true);

	XPartHost_stub xph_stub( argv[1], argv[2] );
	DCOPRef xpn_dcopref( xpn );

	DCOPRef xph_dcopref = xph_stub.registerXPart(xpn_dcopref);
	if ( xph_dcopref.isNull() ) {
		printf("could not register\n");
		return 2;
	}
	printf("XPart registered!\n");

	// Initializing actions:
	const char * actions = 
"<!DOCTYPE actionList SYSTEM \"actionlist.dtd\">\n"
"<Actionlist>\n"
"    <Action name=\"open_url\" />\n"
"    <Action name=\"close_url\" />\n"
"    <Action name=\"nonexistant\" />\n"
"    <XMLFile location=\"./xp_notepad.rc\" />\n"
"</Actionlist>\n";
	xph_stub.createActions( actions );

    return app.exec();
}