scribble_manager.h

Go to the documentation of this file.
00001 /*
00002  * File Name: scribble_manager.h
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026  
00027 #ifndef SCRIBBLE_MANAGER_H_
00028 #define SCRIBBLE_MANAGER_H_
00029 
00030 #include <liberscribble/scb.h>
00031 #include <string>
00032 #include <list>
00033 
00034 #include "gtk_widget.h"
00035 
00036 namespace test
00037 {
00038 
00039 /// Point structure.
00040 struct Point
00041 {
00042     Point() 
00043         : x(0), y(0)
00044     {}
00045 
00046     Point(const int ptx, const int pty)
00047         : x(ptx), y(pty)
00048     {}
00049 
00050     void reset(const int ptx, const int pty)
00051     {
00052         x = ptx; y = pty;
00053     }
00054 
00055     void move_x(const int ptx)
00056     {
00057         x = ptx;
00058     }
00059 
00060     void move_y(const int pty)
00061     {
00062         y = pty;
00063     }
00064 
00065     int x, y;
00066 };
00067 
00068 /// Scribble graphics context. 
00069 /// The scribble graphics context will be used by scribble manager to
00070 /// - Draw strokes new added.
00071 /// - Draw strokes removed.
00072 /// - Draw storkes loaded from scribble document.
00073 /// Usually scribble manager will draw storkes both on e-ink screen and on
00074 /// gdk window.
00075 struct ScribbleGC
00076 {
00077 public:
00078     float  realZoom;        ///< The real zoom factor (0, maxZoom).
00079     int    rotation;        ///< Currently it must be one of {0, 270}. It could be {90, 180, 270}. 
00080     GtkWidget *widget;      ///< The widget scribble will be drawn on.
00081     GdkGC     *gc;          ///< X11 graphics context.
00082 
00083     ScribbleGC()
00084     : realZoom(100.0f)
00085     , rotation(0)
00086     , widget(0)
00087     , gc(0)
00088     {}
00089 
00090     ~ScribbleGC() {}
00091 public:
00092     
00093     /// @brief Change the X11 gc to use new line color.
00094     inline void set_line_color(const unsigned long c)
00095     {
00096         GdkColor color;
00097         color.pixel = c;
00098         gdk_gc_set_foreground(gc, &color);
00099     }
00100 
00101     /// @brief Change the X11 gc to use new line attributes. 
00102     /// Currently, only change the width. Always use LineSolid.
00103     inline void set_line_attributes(const int width)
00104     {
00105         gdk_gc_set_line_attributes(gc
00106             , width
00107             , GDK_LINE_SOLID
00108             , GDK_CAP_PROJECTING
00109             , GDK_JOIN_MITER);
00110     }
00111 
00112     /// @brief Draw line on widget by using current line color, line width and 
00113     /// line style.
00114     inline void draw_line(const int x1, const int y1, const int x2, const int y2)
00115     {
00116         gdk_draw_line(widget->window, gc, x1, y1, x2, y2);
00117     }
00118 };
00119 
00120 
00121 /// Scribble manager.
00122 /// - Drawing scribble new added.
00123 /// - Drawing scribble just removed.
00124 /// - Redraw the scribbles with different zoom factor.
00125 /// - Redraw the scribbles in portrait and landscape.
00126 /// - Maintain scribble data by using erscribble library.
00127 class ScribbleMgr
00128 {
00129 public:
00130     ScribbleMgr(void);
00131     ~ScribbleMgr(void);
00132 
00133 public:
00134     /// Open scribble document.
00135     /// @param path The document absolute path.
00136     /// @return It returns true if the document has been successfully opened.
00137     /// Otherwise this function returns false.
00138     bool open(const char *doc_path);
00139 
00140     /// Close scribble document. This function will check the
00141     /// scribble data is dirty or not.
00142     /// bSave Whether or not to save current scribble data into
00143     /// persistent document.
00144     void close(bool bSave = true);
00145 
00146     /// @brief Save scribble document directly.
00147     void save();
00148 
00149     /// @brief Retrieve raw scribble document pointer.
00150     inline ScbDocPtr get_erscribble_doc() { return doc; }
00151 
00152     void clear_page(const ScbPageId &pageId);
00153 
00154     /// @brief Scribble stylus press event handler.
00155     /// This function will draw the new added scribbles both on
00156     /// e-ink screen and on gdk window.
00157     /// @param pageId The page id provided by caller. 
00158     /// @param sgc The scribble graphics context.
00159     /// @param absoluteX The point screen coordinate x.
00160     /// @param absoluteY The point screen coordinate y.
00161     /// @param offsetX The x offset value within the page.
00162     /// @param offsetY The y offset value within the page.
00163     /// @param pageX The x offset of the page display position.
00164     /// @param pageY The y offset of the page display position.
00165     void on_scribble_begin(const ScbPageId & pageId,
00166                            ScribbleGC & sgc,
00167                            const int absoluteX,
00168                            const int absoluteY,
00169                            const int offsetX,
00170                            const int offsetY,
00171                            const int pageX,
00172                            const int pageY);
00173 
00174     /// @brief Scribble stylus move event handler.
00175     /// This function will draw the new added scribbles both on
00176     /// e-ink screen and on gdk window.
00177     /// @param pageId The page id provided by caller. 
00178     /// @param sgc The scribble graphics context.
00179     /// @param absoluteX The point screen coordinate x.
00180     /// @param absoluteY The point screen coordinate y.
00181     /// @param offsetX The x offset value within the page.
00182     /// @param offsetY The y offset value within the page.
00183     /// @param pageX The x offset of the page display position.
00184     /// @param pageY The y offset of the page display position.
00185     void on_scribble_move(const ScbPageId & pageId, 
00186                           ScribbleGC & sgc,
00187                           const int absoluteX, 
00188                           const int absoluteY,      
00189                           const int offsetX, 
00190                           const int offsetY,
00191                           const int pageX,
00192                           const int pageY);
00193 
00194     /// @brief Scribble stylus release event handler.
00195     /// This function will draw the new added scribbles both on
00196     /// e-ink screen and on gdk window.
00197     /// @param pageId The page id provided by caller. 
00198     /// @param sgc The scribble graphics context.
00199     /// @param absoluteX The point screen coordinate x.
00200     /// @param absoluteY The point screen coordinate y.
00201     /// @param offsetX The x offset value within the page.
00202     /// @param offsetY The y offset value within the page.
00203     /// @param pageX The x offset of the page display position.
00204     /// @param pageY The y offset of the page display position.
00205     void on_scribble_end(const ScbPageId & pageId,
00206                          ScribbleGC & sgc,
00207                          const int absoluteX,
00208                          const int absoluteY,
00209                          const int offsetX,
00210                          const int offsetY,
00211                          const int pageX,
00212                          const int pageY);
00213 
00214     /// @brief Scribble stylus erase press event handler.
00215     /// This function will draw the strokes to be removed both on
00216     /// e-ink screen and on gdk window with color different from normal strokes.
00217     /// @param pageId The page id provided by caller. 
00218     /// @param sgc The scribble graphics context.
00219     /// @param absoluteX The point screen coordinate x.
00220     /// @param absoluteY The point screen coordinate y.
00221     /// @param offsetX The x offset value within the page.
00222     /// @param offsetY The y offset value within the page.
00223     /// @param pageX The x offset of the page display position.
00224     /// @param pageY The y offset of the page display position.
00225     void on_erase_begin(const ScbPageId & pageId,
00226                         ScribbleGC & sgc,
00227                         const int absoluteX,
00228                         const int absoluteY,
00229                         const int offsetX,
00230                         const int offsetY,
00231                         const int pageX,
00232                         const int pageY);
00233 
00234     /// @brief Scribble stylus erase move event handler.
00235     /// This function will draw the strokes to be removed both on
00236     /// e-ink screen and on gdk window with color different from normal strokes.
00237     /// @param pageId The page id provided by caller. 
00238     /// @param sgc The scribble graphics context.
00239     /// @param absoluteX The point screen coordinate x.
00240     /// @param absoluteY The point screen coordinate y.
00241     /// @param offsetX The x offset value within the page.
00242     /// @param offsetY The y offset value within the page.
00243     /// @param pageX The x offset of the page display position.
00244     /// @param pageY The y offset of the page display position.
00245     void on_erase_move(const ScbPageId & pageId,
00246                        ScribbleGC & sgc,
00247                        const int absoluteX,
00248                        const int absoluteY,
00249                        const int offsetX,
00250                        const int offsetY,
00251                        const int pageX,
00252                        const int pageY);
00253 
00254     /// @brief Scribble stylus erase release event handler.
00255     /// This function will draw the strokes to be removed both on
00256     /// e-ink screen and on gdk window with color different from normal strokes.
00257     /// @param pageId The page id provided by caller. 
00258     /// @param sgc The scribble graphics context.
00259     /// @param absoluteX The point screen coordinate x.
00260     /// @param absoluteY The point screen coordinate y.
00261     /// @param offsetX The x offset value within the page.
00262     /// @param offsetY The y offset value within the page.
00263     /// @param pageX The x offset of the page display position.
00264     /// @param pageY The y offset of the page display position.
00265     void on_erase_end(const ScbPageId & pageId,
00266                       ScribbleGC & sgc,
00267                       const int absoluteX,
00268                       const int absoluteY,
00269                       const int offsetX,
00270                       const int offsetY,
00271                       const int pageX,
00272                       const int pageY);
00273 
00274     /// @brief Is the scribble manager active?
00275     inline bool is_active() const {return doc != 0;}
00276 
00277     /// @brief Draw strokes belong to the specified page.
00278     /// @param pageId The page id provided by caller. 
00279     /// @param sgc The scribble graphics context.
00280     /// @param pageX The page screen coordinate x.
00281     /// @param pageY The page screen coordinate y.
00282     /// @param page_width width of the page, useful when in landscape mode
00283     /// @param page_height height of the page, useful when in landscape mode
00284     void draw_scribble_page(const ScbPageId & pageId, 
00285                             ScribbleGC & sgc,
00286                             const int pageX, 
00287                             const int pageY);
00288 
00289     /// TODO: This function should be provided by caller now.
00290     /// @brief Generate page id from anchor.
00291     /// @param pid The erscribble page id which will store the result.
00292     /// @param anchor The input anchor.
00293     /// @param pos The page number
00294     void generate_page_id(ScbPageId & pid, const std::string &anchor, const int pos);
00295 
00296     /// @brief Duplicate page id.
00297     static void copy_page_id(ScbPageId & dst, const ScbPageId & src); 
00298 
00299     /// @brief Clear page id.
00300     static void clear_page_id(ScbPageId & pageId);
00301 
00302     /// @brief Check page id is empty.
00303     static bool is_page_id_empty(const ScbPageId & pageId);
00304 
00305     /// @brief Compare two anchor objects.
00306     static bool cmp_page_id(const ScbPageId & src, const ScbPageId & dst);
00307 
00308 private:
00309     /// @brief Add a new stroke helper function.
00310     /// This function will draw the new added scribbles both on
00311     /// e-ink screen and on gdk window.
00312     /// @param pageId The page id provided by caller. 
00313     /// @param sgc The scribble graphics context.
00314     /// @param absoluteX The point screen coordinate x.
00315     /// @param absoluteY The point screen coordinate y.
00316     /// @param offsetX The x offset value within the page.
00317     /// @param offsetY The y offset value within the page.
00318     void begin_stroke(const ScbPageId & pageId,
00319                       ScribbleGC & sgc,
00320                       const int absoluteX,
00321                       const int absoluteY,
00322                       const int offsetX,
00323                       const int offsetY);
00324 
00325     /// @brief Add a new point to existing stroke.
00326     /// This function will draw the new added scribble both on
00327     /// e-ink screen and on gdk window.
00328     /// @param pageId The page id provided by caller. 
00329     /// @param sgc The scribble graphics context.
00330     /// @param absoluteX The point screen coordinate x.
00331     /// @param absoluteY The point screen coordinate y.
00332     /// @param offsetX The x offset value within the page.
00333     /// @param offsetY The y offset value within the page.
00334     void add_point(const ScbPageId & pageId, 
00335                    ScribbleGC & sgc,
00336                    const int absoluteX,
00337                    const int absoluteY,
00338                    const int offsetX,
00339                    const int offsetY);
00340 
00341     /// @brief Add the last point to existing stroke.
00342     /// This function will draw the last point both on
00343     /// e-ink screen and on gdk window.
00344     /// @param pageId The page id provided by caller. 
00345     /// @param sgc The scribble graphics context.
00346     /// @param absoluteX The point screen coordinate x.
00347     /// @param absoluteY The point screen coordinate y.
00348     /// @param offsetX The x offset value within the page.
00349     /// @param offsetY The y offset value within the page.
00350     /// @param pageX The x display position of the page.
00351     /// @param pageY The y display position of the page.
00352     void end_stroke(const ScbPageId & pageId, 
00353                     ScribbleGC & sgc,
00354                     const int absoluteX,
00355                     const int absoluteY,
00356                     const int offsetX,
00357                     const int offsetY,
00358                     const int pageX,
00359                     const int pageY);
00360 
00361     /// @brief Draw the last point without adding the point to stroke.
00362     /// This function will draw the last point both on
00363     /// e-ink screen and on gdk window.
00364     /// @param sgc The scribble graphics context.
00365     /// @param absoluteX The point screen coordinate x.
00366     /// @param absoluteY The point screen coordinate y.
00367     /// @param pageX The x offset value of the page.
00368     /// @param pageY The y offset value of the page.
00369     void end_stroke(ScribbleGC & sgc,
00370                     const int absoluteX,
00371                     const int absoluteY,
00372                     const int pageX,
00373                     const int pageY);
00374 
00375     /// @brief Draw the strokes of specified page in portrait view.
00376     /// This function will draw the strokes both on
00377     /// e-ink screen and on gdk window.
00378     /// @param page The raw scribble page pointer.
00379     /// @param sgc The scribble graphics context.
00380     /// @param pageX The page screen coordinate x.
00381     /// @param pageY The page screen coordinate y.
00382     /// @param page_width width of the page
00383     /// @param page_height height of the page
00384     void draw_scribble_page_portrait(ScbPagePtr page, 
00385                                      ScribbleGC & sgc, 
00386                                      const int pageX, 
00387                                      const int pageY);
00388 
00389     /// @brief Draw the strokes of specified page in landscape view.
00390     /// This function will draw the strokes both on
00391     /// e-ink screen and on gdk window.
00392     /// @param page The raw scribble page pointer.
00393     /// @param sgc The scribble graphics context.
00394     /// @param pageX The page screen coordinate x.
00395     /// @param pageY The page screen coordinate y.
00396     /// @param page_width width of the page
00397     /// @param page_height height of the page
00398     void draw_scribble_page_landscape(ScbPagePtr page, 
00399                                       ScribbleGC & sgc, 
00400                                       const int pageX, 
00401                                       const int pageY);
00402 
00403     /// @brief Draw the strokes erased in portrait view.
00404     /// This function will draw the erased strokes both on
00405     /// e-ink screen and on gdk window.
00406     /// @param strokes The raw scribble strokes pointer.
00407     /// @param sgc The scribble graphics context.
00408     /// @param pageX The page screen coordinate x.
00409     /// @param pageY The page screen coordinate y.
00410     void draw_erased_strokes_portrait(ScbStrokesPtr strokes,
00411                                       ScribbleGC & sgc, 
00412                                       const int pageX, 
00413                                       const int pageY);
00414 
00415     /// @brief Draw the strokes erased in landscape view.
00416     /// This function will draw the erased strokes both on
00417     /// e-ink screen and on gdk window.
00418     /// @param strokes The raw scribble strokes pointer.
00419     /// @param sgc The scribble graphics context.
00420     /// @param pageX The page screen coordinate x.
00421     /// @param pageY The page screen coordinate y.
00422     void draw_erased_strokes_landscape(ScbStrokesPtr strokes,
00423                                        ScribbleGC & sgc, 
00424                                        const int pageX, 
00425                                        const int pageY);
00426 
00427     /// @brief Draw specified stroke both on e-ink screen and on gdk window.
00428     /// @param stroke The storke pointer caller wants to draw.
00429     /// @param sgc The scribble graphics context.
00430     /// @param pageX The page screen coordinate x.
00431     /// @param pageY The page screen coordinate y.
00432     void draw_stroke(ScbStrokePtr stroke,
00433                      ScribbleGC & sgc,
00434                      const int pageX,
00435                      const int pageY);
00436 
00437     /// @brief Draw stroke directly on gdk window.
00438     /// @param stroke The stroke caller wants to draw.
00439     /// @param sgc The scribble graphics context.
00440     void draw_stroke_directly(ScbStrokePtr stroke,
00441                               ScribbleGC & sgc);
00442 
00443     /// @brief Helper function used to calculate the line size according to 
00444     /// storke zoomfactor and the display zoomfactor.
00445     /// @param penSize The input pen size.
00446     /// @param strokeZoom The zoom factor when storke is added.
00447     /// @param dispZoom The zoom factor currently to display.
00448     /// @return The result pen size in pixel.
00449     int calc_line_size(const int penSize, 
00450                        const float strokeZoom,
00451                        const float dispZoom);
00452 
00453     /// Is the page dirty
00454     bool is_page_dirty(ScbPagePtr page);
00455 
00456     /// Set the page to be dirty
00457     void set_page_dirty(ScbPagePtr page);
00458 
00459 private:
00460     typedef std::list<ScbPagePtr>   DirtyPages;
00461     typedef DirtyPages::iterator    DirtyPagesIter;
00462 
00463 private:
00464     ScbPath         path;           ///< The scribble document path.
00465     ScbDocPtr       doc;            ///< The scribble document.
00466     ScbPagePtr      page;           ///< Page currently used.
00467     ScbStrokePtr    stroke;         ///< Stroke currently used.
00468     ScbPageEraseCtx erase_context;  ///< Used by erscribble library.
00469 
00470     ScbPageId       last_page_id;   ///< ID of last page.
00471     bool            is_erased;      ///< When strokes have been erased.
00472 
00473     Point           last_absolute_point; ///< last absolute point
00474     Point           last_page_point;     ///< last point in page
00475     Point           last_page_position;  ///< display position of last page
00476 
00477     DirtyPages      dirty_pages;    ///< A list of dirty pages.
00478 };
00479 
00480 }; //namespace test
00481 
00482 #endif
Generated by  doxygen 1.6.2-20100208