/*
	The way IDs work is a bit bizarre. You actually dictate them to whomever you are speaking.

	This cuts down on response bandwidth and simplifies coding; you can use it immediately in order.

	You should start with 1 and move up from there.
	If you create something repeating an old id, the resource that had that ID is destroyed and replaced with a fresh one.

*/

server:
	// Regarding properties of your app as a whole...
	void setApplicationTitle(string title);
	void setApplicationIcon(int imageId);

	// Top level windows
	void createMainWindow(int wid); // has menu, docks, tool bar, and status bar. Can be tabbed. takes widgets
		void setStatusBarText(int wid, string text);
		void addMenu(int wid, int menuId); // adds it to the menu bar
		void addToolBar(int wid, int actionId); // the name and the icon are associated with the action
	void createDialog(int wid, int modalTo); // takes widgets
	void createPopup(int wid); // frameless

	// These act like windows in that they hold controls - they have a window id.
	void createFrame(int wid, int parent);
	void createPage(int wid, int parent);

	// Data functions
	void createDataStore(int id);
	void setDataStoreSize(int id, int size);
	void setData(int id, int position, byte[] data);
	void setCompressedData(int id, int position, byte[] compressedData); // compressed with zlib
	void destroyDataStore(int id);

	// Image functions
	void createImage(int id);
	void setImageData(int id, byte[] data);
	void setImageDataFromStore(int id, int dataId);
	void destroyImage(int id);

	// You can eval() the data in a store; good for predictive paths
	void executeDataStoreAsCommands(int id);


	// Free-form widgets
	void createConsole(int consoleId);
		void writeLines(int consoleId, string text); // it is like writing to stdout - at the cursor position, and advances the cursor. Auto scrolls.
		void writeAt(int consoleId, int x, int y, string text); // writes without moving the cursor at all
		void moveCursor(int consoleId, int x, int y); // moves the text cursor - y is ignored in linear mode
		string getLine(int consoleId);
//		void textBlit(int consoleId, int x, int y, int w, int h, byte[] data); // data is packed attributes and text data
		void setConsoleMode(int id, int mode); // modes: linear output, cellular output, cooked input, raw input, linear input, auto echo, cooked output, word wrap, truncate
		void setConsoleAttributes(int id, int attrs); // attributes: color, underline, bold, reverse
		void setConsoleTitle(int id, string title);
		void clearScreen(int consoleId);

		/*
			We need some state for plain output:
				attributes
				foreground color
				background color

				console mode: linear, cellular, xml
				output types: plain, rich, xml

		*/
	void createCanvas(int id);

	// Generic window functions
	void setWindowTitle(int wid, string title);
	void setWindowIcon(int wid, int imageId); // FIXME
	void closeWindow(int wid);
	void hideWindow(int id);
	void showWindow(int id);

	// Simple functions
	void messageBox(string title, string message); // the information message box with an ok button
	void beep(int frequency, int duration);
	void bell(); // just plays a default beep or system sound

	// Action functions
	void createAction(int actionId);
	void setActionText(int id, string text);
	void setActionIcon(int id, int imageId);
	void disableAction(int id);
	void enableAction(int id);

	// Menu functions
	void createMenu(int menuId, string title);
	void addMenuItem(int menuId, int actionId); // text is associated with the action
	void addMenuSeparator(int menuId);
	void destroyMenu(int menuId);


	// Control functions
	void createControl(int id, int type, int parentWindowId);
	void setControlGeometry(int id, int x, int y, int w, int h);
	void destroyControl(int id);

	void disableControl(int id);
	void enableControl(int id);

	void showControl(int id);
	void hideControl(int id);

	void setControlText(int id, string text);
	void appendControlText(int id, string text);
	void setControlImage(int id, int imageId);
	void setControlImagePosition(int id, int position); // above text, below, to the left, to the right

	void setControlValue(int id, int val);
	void setControlValue2(int id, int val);
	void setControlUpperLimit(int id, int val);
	void setControlLowerLimit(int id, int val);

	// Modes: 0 (default) - moderate buffering. Try to strike a balance between network traffic and response time.
	// 1 - no buffering, send changes immediately. Eats lots of network, but should give fast results.
	// 2 - buffer indefinitely. The only way to get the text is to ask the display for it. Data will be lost if the display suddenly disconnects.
	// mode 0 is recommended and should be fine for most cases.
	void setTextControlBufferMode(int id, int mode); // user changes to text boxes are usually buffered a bit on the display
							// to cut down on network traffic. The default is 3000 msec (3 seconds) on my
							// implementations. You can adjust that here if you need faster notifications, although it
							// comes at the cost of increased network chatter.

	// Items go into list boxes and the sort.
	void createItem(int id, int parent);
	void setItemText(int id, string text);
	void setItemIcon(int id, int imageId);
	void destroyItem(int id);

	void addItemToControl(int itemId, int controlId);
	void removeItemFromControl(int itemId, int controlId);


	// signals and slots management
		/*
			How does this work? You can take functions sent by controls and connect them
			to a function that operates on a control. Then it is handled on the display
			as well as being sent down the network.

			This leads to faster UI response as well as less network traffic while still
			maintaining detach-ability. (The data is sent to the manager, but the UI
			needn't wait on a round trip on the network.)
		*/
	void connect(int controlId, int srcfunc, int receiverControlId, int dstfunc);
	void disconnect(int controlId, int srcfunc, int receiverControlId, int dstfunc);







	// Application configuration database functions
		/*
			An app can get its configuration from the display. This lets
			a user maintain a consistent theme and such if everything is well
			written.

			Your app should try to use the display's configuration settings first.
			Then, it should fall back on its local config if it doesn't work.

			(This works kinda like X resources.)
		*/

	
	// Clipboard functions
		/*
			You shouldn't need most of these - the display can generally handle some degree
			of copying and pasting on its own. (Specifically, the text widgets work well on their
			own on my displays.)

			However, when that fails, you get these functions.

			Like X, you have a selection and the clipboard.

		*/





	/////////////////////////
	// Notifications from the manager
	void newClient(int id);
	void clientNameChanged(int id, string name);
	void clientIconChanged(int id, int imageId);
	void clientAttachedElsewhere(int id);
	void clientDetached(int id);
	void clientTerminated(int id);
	void clientZombied(int id); // this is when the program crashed - you can keep the window open if you like, but the program won't respond
client:
	// Requests for the manager
	void attachClient(int id);
	void detachClient(int id);
	void setDisplayName(string name);
	/*
		Some of the caps:
			Images
			Sound
			Midi
			Files
			Devices
			Canvas
			Data
			Gzip
			Jpeg

			Is local or remote
	*/
	void displayCapabilitiesUpdate(int flags);
	////////////////////


	// Display change notifications
	void attached();
	void detached();


	// Message notifications
	void actionTriggered(int actionId);
	void itemSelected(int itemId, int controlId);
	void itemDoubleClicked(int itemId, int controlId);

	// Signals from windows
	void windowCloseRequested(int wid); // the user clicked the close button

	// Signals from controls
	void stateChanged(int controlId, int state);
	void controlTriggered(int controlId);
	void controlValueChanged(int controlId, int value);


	// Special text control signals
	void textChanged(int controlId, string newText);
	void textInserted(int controlId, int pos, string text);
	void textRemoved(int controlId, int start, int end);
	void cursorMoved(int controlId, int pos);
	void scrollPositionChanged(int controlId, int value);
	void selectionChanged(int controlId, int start, int end);

	// These are only sent for the "freeform" widgets and GameWindows and only if you subscribe to them
	// KEY_EVENT
		void keyDown(int key);
		void keyUp(int key);
		void receiveFocus();
		void loseFocus();
	// MOUSE_MOVE_EVENT
		void mouseMove(int dx, int dy);
	// MOUSE_BUTTON_EVENT
		void mouseButtonDown(int button);
		void mouseButtonUp(int button);
		void mouseScroll(int dx);

	// Only sent to freeform widgets
	void repaint(int wid, int x, int y, int width, int height);



/* NEXT VERSION
	int createGameWindow(); // cannot be resized, only allows GL/SDL drawing in it
*/