Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2008,2010 Vincent Sanders <vince@simtec.co.uk>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #ifndef NETSURF_FB_FBTK_H
  20. #define NETSURF_FB_FBTK_H
  21.  
  22. #ifdef FBTK_LOGGING
  23. #define FBTK_LOG(x) LOG(x)
  24. #else
  25. #define FBTK_LOG(x)
  26. #endif
  27.  
  28. #define FB_SCROLL_COLOUR 0xFFAAAAAA
  29. #define FB_FRAME_COLOUR 0xFFDDDDDD
  30. #define FB_COLOUR_BLACK 0xFF000000
  31. #define FB_COLOUR_WHITE 0xFFFFFFFF
  32.  
  33. #define FBTK_WIDGET_PADDING 30 /* percentage of widget size used for padding */
  34. #define FBTK_DPI 90 /* screen DPI */
  35.  
  36. typedef struct fbtk_widget_s fbtk_widget_t;
  37.  
  38. /* Widget Callback handling */
  39. typedef enum fbtk_callback_type {
  40.         FBTK_CBT_START = 0,
  41.         FBTK_CBT_SCROLLX,
  42.         FBTK_CBT_SCROLLY,
  43.         FBTK_CBT_CLICK,
  44.         FBTK_CBT_INPUT,
  45.         FBTK_CBT_POINTERMOVE,
  46.         FBTK_CBT_POINTERLEAVE,
  47.         FBTK_CBT_POINTERENTER,
  48.         FBTK_CBT_REDRAW,
  49.         FBTK_CBT_DESTROY,
  50.         FBTK_CBT_USER,
  51.         FBTK_CBT_STRIP_FOCUS,
  52.         FBTK_CBT_END,
  53. } fbtk_callback_type;
  54.  
  55. typedef struct fbtk_callback_info {
  56.         enum fbtk_callback_type type;
  57.         void *context;
  58.         nsfb_event_t *event;
  59.         int x;
  60.         int y;
  61.         char *text;
  62.         fbtk_widget_t *widget;
  63. } fbtk_callback_info;
  64.  
  65. /* structure for framebuffer toolkit bitmaps  */
  66. struct fbtk_bitmap {
  67.         int width;
  68.         int height;
  69.         uint8_t *pixdata;
  70.         bool opaque;
  71.  
  72.         /* The following two are only used for cursors */
  73.         int hot_x;
  74.         int hot_y;
  75. };
  76.  
  77. /* Key modifier status */
  78. typedef enum fbtk_modifier_type {
  79.         FBTK_MOD_CLEAR  = 0,
  80.         FBTK_MOD_LSHIFT = (1 << 0),
  81.         FBTK_MOD_RSHIFT = (1 << 1),
  82.         FBTK_MOD_LCTRL  = (1 << 2),
  83.         FBTK_MOD_RCTRL  = (1 << 3)
  84. } fbtk_modifier_type;
  85.  
  86. typedef int (*fbtk_callback)(fbtk_widget_t *widget, fbtk_callback_info *cbi);
  87.  
  88. /* enter pressed on writable icon */
  89. typedef int (*fbtk_enter_t)(void *pw, char *text);
  90.  
  91.  
  92. /************************ Core ****************************/
  93.  
  94.  
  95. /** Initialise widget toolkit.
  96.  *
  97.  * Initialises widget toolkit against a framebuffer.
  98.  *
  99.  * @param fb The underlying framebuffer.
  100.  * @return The root widget handle.
  101.  */
  102. fbtk_widget_t *fbtk_init(nsfb_t *fb);
  103.  
  104. /** Retrieve the framebuffer library handle from toolkit widget.
  105.  *
  106.  * @param widget A fbtk widget.
  107.  * @return The underlying framebuffer.
  108.  */
  109. nsfb_t *fbtk_get_nsfb(fbtk_widget_t *widget);
  110.  
  111. /** Perform any pending widget redraws.
  112.  *
  113.  * @param widget A fbtk widget.
  114.  */
  115. int fbtk_redraw(fbtk_widget_t *widget);
  116.  
  117. /** Determine if there are any redraws pending for a widget.
  118.  *
  119.  * Mainly used by clients on the root widget to determine if they need
  120.  * to call ::fbtk_redraw
  121.  *
  122.  * @param widget to check.
  123.  */
  124. bool fbtk_get_redraw_pending(fbtk_widget_t *widget);
  125.  
  126. /** clip a bounding box to a widgets area.
  127.  */
  128. bool fbtk_clip_to_widget(fbtk_widget_t *widget, bbox_t *  box);
  129.  
  130. /** clip one bounding box to another.
  131.  */
  132. bool fbtk_clip_rect(const bbox_t *  clip, bbox_t *  box);
  133.  
  134. /***************** Callback processing ********************/
  135.  
  136. /** Helper function to allow simple calling of callbacks with parameters.
  137.  *
  138.  * @param widget The fbtk widget to post the callback to.
  139.  * @param cbt The type of callback to post
  140.  * @param ... Parameters appropriate for the callback type.
  141.  */
  142. int fbtk_post_callback(fbtk_widget_t *widget, fbtk_callback_type cbt, ...);
  143.  
  144. /** Set a callback handler.
  145.  *
  146.  * Set a callback handler and the pointer to pass for a widget.
  147.  *
  148.  * @param widget The widget to set the handler for.
  149.  * @param cbt The type of callback to set.
  150.  * @param cb The callback.
  151.  * @param pw The private pointer to pass when calling teh callback.
  152.  * @return The previous callback handler for the type or NULL.
  153.  */
  154. fbtk_callback fbtk_set_handler(fbtk_widget_t *widget, fbtk_callback_type cbt, fbtk_callback cb, void *pw);
  155.  
  156. /** Get a callback handler.
  157.  */
  158. fbtk_callback fbtk_get_handler(fbtk_widget_t *widget, fbtk_callback_type cbt);
  159.  
  160.  
  161. /******************* Event processing **********************/
  162.  
  163. /** Retrive events from the framebuffer input.
  164.  *
  165.  * Obtain events from the framebuffer input system with a
  166.  * timeout. Some events may be used by the toolkit instead of being
  167.  * returned to the caller.
  168.  *
  169.  * @param root An fbtk widget.
  170.  * @param event an event structure to update.
  171.  * @param timeout The number of miliseconds to wait for an event. 0
  172.  *                means do not wait and -1 means wait foreevr.
  173.  * @return wether \a event has been updated.
  174.  */
  175. bool fbtk_event(fbtk_widget_t *root, nsfb_event_t *event, int timeout);
  176.  
  177. /** Insert mouse button press into toolkit.
  178.  */
  179. void fbtk_click(fbtk_widget_t *widget, nsfb_event_t *event);
  180.  
  181. /** Insert input into toolkit.
  182.  */
  183. void fbtk_input(fbtk_widget_t *widget, nsfb_event_t *event);
  184.  
  185. /** Move pointer.
  186.  *
  187.  * Move the pointer cursor to a given location.
  188.  *
  189.  * @param widget any tookit widget.
  190.  * @parm x movement in horizontal plane.
  191.  * @parm y movement in vertical plane.
  192.  * @parm relative Wheter the /a x and /a y should be considered relative to
  193.  *                current pointer position.
  194.  */
  195. void fbtk_warp_pointer(fbtk_widget_t *widget, int x, int y, bool relative);
  196.  
  197. /** Toggle pointer grab.
  198.  *
  199.  * Toggles the movement grab for a widget.
  200.  *
  201.  * @param widget The widget trying to grab the movement.
  202.  * @return true if the grab was ok, false if the grab failed (already grabbed).
  203.  */
  204. bool fbtk_tgrab_pointer(fbtk_widget_t *widget);
  205.  
  206. /** Convert a framebuffer keycode to ucs4.
  207.  *
  208.  * Character mapping between keycode with modifier state and ucs-4.
  209.  */
  210. int fbtk_keycode_to_ucs4(int code, fbtk_modifier_type mods);
  211.  
  212.  
  213. /******************* Widget Information **********************/
  214.  
  215. /** Obtain the widget at a point on screen.
  216.  *
  217.  * @param widget any tookit widget.
  218.  * @parm x location in horizontal plane.
  219.  * @parm y location in vertical plane.
  220.  * @return widget or NULL.
  221.  */
  222. fbtk_widget_t *fbtk_get_widget_at(fbtk_widget_t *widget, int x, int y);
  223.  
  224. /** Get a widget's absolute horizontal screen co-ordinate.
  225.  *
  226.  * @param widget The widget to inspect.
  227.  * @return The absolute screen co-ordinate.
  228.  */
  229. int fbtk_get_absx(fbtk_widget_t *widget);
  230.  
  231. /** Get a widget's absolute vertical screen co-ordinate.
  232.  *
  233.  * @param widget The widget to inspect.
  234.  * @return The absolute screen co-ordinate.
  235.  */
  236. int fbtk_get_absy(fbtk_widget_t *widget);
  237.  
  238. /** Get a widget's width.
  239.  *
  240.  * @param widget The widget to inspect.
  241.  * @return The widget width.
  242.  */
  243. int fbtk_get_width(fbtk_widget_t *widget);
  244.  
  245. /** Get a widget's height.
  246.  *
  247.  * @param widget The widget to inspect.
  248.  * @return The widget height.
  249.  */
  250. int fbtk_get_height(fbtk_widget_t *widget);
  251.  
  252. /** Get a widget's bounding box in absolute screen co-ordinates.
  253.  *
  254.  * @param widget The widget to inspect.
  255.  * @param bbox The bounding box structure to update.
  256.  * @return If the \a bbox parameter has been updated.
  257.  */
  258. bool fbtk_get_bbox(fbtk_widget_t *widget, struct nsfb_bbox_s *bbox);
  259.  
  260. /** Get a widget caret pos, if it owns caret.
  261.  *
  262.  * @param widget  The widget to inspect.
  263.  * @param x       If widget has caret, returns x-coord of caret within widget
  264.  * @param y       If widget has caret, returns y-coord of caret within widget
  265.  * @param height  If widget has caret, returns caret height
  266.  * @return true iff widget has caret
  267.  */
  268. bool fbtk_get_caret(fbtk_widget_t *widget, int *x, int *y, int *height);
  269.  
  270.  
  271. /******************* Widget Manipulation **********************/
  272.  
  273. /** Change the widget's position and size.
  274.  *
  275.  */
  276. bool fbtk_set_pos_and_size(fbtk_widget_t *widget, int x, int y, int width, int height);
  277.  
  278. /** Set caret owner and position
  279.  *
  280.  * @param widget  widget to give caret to, or ensure caret is released from
  281.  * @param set     true: caret to be set for widget, false: caret to be released
  282.  * @param x       x-coordinate of caret top
  283.  * @param y       y-coordinate of caret top
  284.  * @param height  height of caret
  285.  */
  286. void fbtk_set_caret(fbtk_widget_t *widget, bool set, int x, int y, int height,
  287.                 void (*remove_caret)(fbtk_widget_t *widget));
  288.  
  289. /** Map a widget and request it is redrawn.
  290.  */
  291. int fbtk_set_mapping(fbtk_widget_t *widget, bool mapped);
  292.  
  293. /** Set the z order of a widget.
  294.  */
  295. int fbtk_set_zorder(fbtk_widget_t *widget, int z);
  296.  
  297. /** Indicate a widget should be redrawn.
  298.  */
  299. void fbtk_request_redraw(fbtk_widget_t *widget);
  300.  
  301. /** Destroy a widget and all its descendants.
  302.  *
  303.  * Removes a widget from the hierachy and frees it and all its children.
  304.  *
  305.  * @param widget The widget to destroy.
  306.  * @return 0 on success or -1 on error.
  307.  */
  308. int fbtk_destroy_widget(fbtk_widget_t *widget);
  309.  
  310.  
  311.  
  312. /********************************* Widgets *********************************/
  313.  
  314.  
  315. /** Create a window widget.
  316.  *
  317.  * @param parent The parent window or the root widget for a top level window.
  318.  * @param x The x location relative to the parent window.
  319.  * @param y the y location relative to the parent window.
  320.  * @param width The width of the window. 0 indicates parents width should be
  321.  *              used. Negative value indicates parents width less the value
  322.  *              should be used. The width is limited to lie within the parent
  323.  *              window.
  324.  * @param height The height of the window limited in a similar way to the
  325.  *               /a width.
  326.  * @param c The background colour.
  327.  * @return new window widget handle or NULL on error.
  328.  */
  329. fbtk_widget_t *fbtk_create_window(fbtk_widget_t *parent, int x, int y, int width, int height, colour bg);
  330.  
  331.  
  332.  
  333. /** Create a filled rectangle
  334.  *
  335.  * Create a widget which is a filled rectangle, usually used for backgrounds.
  336.  *
  337.  * @param window The window to add the filled area widget to.
  338.  * @return new widget handle or NULL on error.
  339.  */
  340. fbtk_widget_t *
  341. fbtk_create_fill(fbtk_widget_t *window, int x, int y, int width, int height, colour c);
  342.  
  343.  
  344.  
  345.  
  346. /** Create a horizontal scroll widget
  347.  *
  348.  * Create a horizontal scroll widget.
  349.  *
  350.  * @param window The window to add the filled area widget to.
  351.  * @return new widget handle or NULL on error.
  352.  */
  353. fbtk_widget_t *
  354. fbtk_create_hscroll(fbtk_widget_t *window, int x, int y, int width, int height, colour fg, colour bg, fbtk_callback callback, void *context);
  355.  
  356. /** Create a vertical scroll widget
  357.  *
  358.  * Create a vertical scroll widget.
  359.  *
  360.  * @param window The window to add the filled area widget to.
  361.  * @return new widget handle or NULL on error.
  362.  */
  363. fbtk_widget_t *
  364. fbtk_create_vscroll(fbtk_widget_t *window, int x, int y, int width, int height, colour fg, colour bg, fbtk_callback callback, void *context);
  365.  
  366. bool fbtk_set_scroll_parameters(fbtk_widget_t *widget, int min, int max, int thumb, int page);
  367.  
  368. bool fbtk_set_scroll_position(fbtk_widget_t *widget, int pos);
  369.  
  370.  
  371.  
  372.  
  373.  
  374. /** Create a user widget.
  375.  *
  376.  * Create a widget which is to be handled entirely by the calling application.
  377.  *
  378.  * @param window The window to add the user widget to.
  379.  * @param pw The private pointer which can be read using ::fbtk_get_pw
  380.  * @return new widget handle or NULL on error.
  381.  */
  382. fbtk_widget_t *fbtk_create_user(fbtk_widget_t *window, int x, int y, int width, int height, void *pw);
  383.  
  384. void *fbtk_get_userpw(fbtk_widget_t *widget);
  385.  
  386.  
  387.  
  388. /** Create a bitmap widget.
  389.  *
  390.  * Create a widget which shows a bitmap.
  391.  *
  392.  * @param window The window to add the bitmap widget to.
  393.  * @return new widget handle or NULL on error.
  394.  */
  395. fbtk_widget_t *fbtk_create_bitmap(fbtk_widget_t *window, int x, int y, int width, int height, colour c,struct fbtk_bitmap *image);
  396.  
  397. void fbtk_set_bitmap(fbtk_widget_t *widget, struct fbtk_bitmap *image);
  398.  
  399. /** Create a button widget.
  400.  *
  401.  * Helper function which creates a bitmap widget and associate a handler for
  402.  * when it is clicked.
  403.  *
  404.  * @param window The window to add the button widget to.
  405.  * @return new widget handle or NULL on error.
  406.  */
  407. fbtk_widget_t *fbtk_create_button(fbtk_widget_t *window, int x, int y, int width, int height, colour c, struct fbtk_bitmap *image, fbtk_callback click, void *pw);
  408.  
  409.  
  410.  
  411.  
  412.  
  413. /** Create a text widget.
  414.  *
  415.  * @param window The window to add the text widget to.
  416.  * @return new widget handle or NULL on error.
  417.  */
  418. fbtk_widget_t *fbtk_create_text(fbtk_widget_t *window, int x, int y, int width, int height, colour bg, colour fg, bool outline);
  419.  
  420. /** Create a button with text.
  421.  *
  422.  * @param window The window to add the text widget to.
  423.  * @return new widget handle or NULL on error.
  424.  */
  425. fbtk_widget_t *fbtk_create_text_button(fbtk_widget_t *window, int x, int y, int width, int height, colour bg, colour fg, fbtk_callback click, void *pw);
  426.  
  427. /** Create a writable text widget.
  428.  *
  429.  * Helper function which creates a text widget and configures an input handler
  430.  * to create a writable text field. This call is equivalent to calling
  431.  * ::fbtk_create_text followed by ::fbtk_writable_text
  432.  *
  433.  * @param window The window to add the text widget to.
  434.  * @return new widget handle or NULL on error.
  435.  */
  436. fbtk_widget_t *fbtk_create_writable_text(fbtk_widget_t *window, int x, int y, int width, int height, colour bg, colour fg, bool outline, fbtk_enter_t enter, void *pw);
  437.  
  438. /** Alter a text widget to be writable.
  439.  *
  440.  * @param widget Text widget.
  441.  * @param enter The routine to call when enter is pressed.
  442.  * @param pw The context to pass to teh enter callback routine.
  443.  */
  444. void fbtk_writable_text(fbtk_widget_t *widget, fbtk_enter_t enter, void *pw);
  445.  
  446. /** Change the text of a text widget.
  447.  *
  448.  * @param widget Text widget.
  449.  * @param text The new UTF-8 text to put in the widget.
  450.  */
  451. void fbtk_set_text(fbtk_widget_t *widget, const char *text);
  452.  
  453.  
  454. /** Give widget input focus.
  455.  *
  456.  * @param widget Widget to be given input focus.
  457.  */
  458. void fbtk_set_focus(fbtk_widget_t *widget);
  459.  
  460.  
  461.  
  462.  
  463. /** enable the on screen keyboard for input */
  464. void fbtk_enable_oskb(fbtk_widget_t *widget);
  465.  
  466. /** show the osk. */
  467. void map_osk(void);
  468.  
  469. #endif
  470.