"Fossies" - the Fresh Open Source Software Archive

Member "mozplugger-2.1.6/widgets.c" (17 Apr 2014, 10133 Bytes) of package /linux/www/old/mozplugger-2.1.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * This file is part of mozplugger a fork of plugger, for list of developers
    3  * see the README file.
    4  *
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation; either version 2 of the License, or
    8  * (at your option) any later version.
    9  *
   10  * This program 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, write to the Free Software
   17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
   18  */
   19 
   20 #ifdef HAVE_CONFIG_H
   21 #include "config.h"
   22 #endif
   23 
   24 #include <stdlib.h>
   25 #include <unistd.h>
   26 #include <string.h>
   27 #include <stdio.h>
   28 #include <sysexits.h>
   29 #include <signal.h>
   30 #include <sys/stat.h>
   31 #include <fcntl.h>
   32 #include <sys/wait.h>
   33 #include <errno.h>
   34 #include <X11/X.h>
   35 #include <X11/Xutil.h>
   36 
   37 #include "debug.h"
   38 #include "widgets.h"
   39 
   40 #define MAX_CONTROLS_WIDTH 300
   41 #define MAX_CONTROLS_HEIGHT 100
   42 
   43 #define DEFAULT_CONTROLS_WIDTH 60
   44 #define DEFAULT_CONTROLS_HEIGHT 20
   45 
   46 #define MIN_CONTROLS_WIDTH 48
   47 #define MIN_CONTROLS_HEIGHT 16
   48 
   49 #define WINDOW_BORDER_WIDTH 1
   50 
   51 #define BUTTON_DEPTH 2
   52 
   53 /**
   54  * Given v when size is 16 scale to v if size was size,
   55  * make sure we round up or down correctly.
   56  *
   57  * @param[in] v The value to scale
   58  * @param[in] size
   59  *
   60  * @return The scaled value
   61  */
   62 static int scale(int v, int size)
   63 {
   64      return (v * size +8) / 16;
   65 }
   66 
   67 /**
   68  * Given origin point and an x and y translation, calculate new point
   69  *
   70  * @param[in] origin The origin
   71  * @param[in] x The X translation
   72  * @param[in] y The Y translation
   73  *
   74  * @return The co-ordinates as a XPoint structure
   75  */
   76 static XPoint coordAdd(const XPoint * const origin, int x, int y)
   77 {
   78      XPoint ret;
   79      ret.x = origin->x + x;
   80      ret.y = origin->y + y;
   81      return ret;
   82 }
   83 
   84 /**
   85  * Draw the play button
   86  *
   87  * ...0-1..........
   88  * ...|..\.........
   89  * ...|...\........
   90  * ...|....\.......
   91  * ...|.....\......
   92  * ...|......2.....
   93  * ...|......|\....
   94  * ...|......|.2...
   95  * ...|......3.|...
   96  * ...|...../..|...
   97  * ...|..../...3...
   98  * ...|.../.../....
   99  * ...|../.../.....
  100  * ...5-4.../......
  101  * ....\.../.......
  102  * .....5-4........
  103  *
  104  * @param[in] dpy The display
  105  * @param[in] win The window
  106  * @param[in] base The origin
  107  * @param[in] size The size of the button
  108  * @param[in] colour The colour of the button
  109  * @param[in] shadow The colour of the shadow
  110  * @param[in] bg The background colour
  111  * @param[in] pressed Is the button pressed
  112  */
  113 void drawPlayButton(Display * dpy, Window win,
  114                                     const XPoint * base, int size,
  115                                         GC colour, GC shadow, GC bg, int pressed)
  116 
  117 {
  118      XPoint points[6];
  119 
  120      const int len2 = scale(2, size);
  121      const int len5 = scale(5, size);
  122      const int len13 = scale(13, size);
  123 
  124      int s = !pressed ? BUTTON_DEPTH : 0;
  125 
  126      points[0] = coordAdd(base,        len5 - s,  len2 - s);
  127      points[1] = coordAdd(&points[0],  len2,      0);
  128      points[2] = coordAdd(&points[1],  len5,      len5);
  129 
  130      points[5] = coordAdd(base,        len5 - s,  len13 - s);
  131      points[4] = coordAdd(&points[5],  len2,      0);
  132      points[3] = coordAdd(&points[4],  len5,      -len5);
  133 
  134 
  135      if(!pressed)
  136      {
  137           int i;
  138 
  139           XFillPolygon(dpy, win, colour, points, 6, Convex, CoordModeOrigin);
  140 
  141           points[2].y++;
  142           points[5].x++;
  143 
  144           /* Draw the shadow */
  145           for(i = BUTTON_DEPTH; i > 0; i--)
  146           {
  147                XDrawLines(dpy, win, shadow, &points[2], 4, CoordModeOrigin);
  148 
  149                XDrawLine(dpy, win, shadow, points[3].x-1, points[3].y,
  150                                                  points[4].x-1, points[4].y);
  151 
  152                points[2].x++;
  153                points[2].y++;
  154                points[3].x++;
  155                points[3].y++;
  156                points[4].x++;
  157                points[4].y++;
  158                points[5].x++;
  159                points[5].y++;
  160           }
  161      }
  162      else
  163      {
  164           int x = points[0].x;
  165           int y = points[0].y;
  166           unsigned w = points[2].x - points[0].x;
  167           unsigned h = points[4].y - points[0].y;
  168 
  169           XFillPolygon(dpy, win, colour, points, 6, Convex, CoordModeOrigin);
  170 
  171           /* erase left bit of previous not pressed button */
  172           XFillRectangle(dpy, win, bg,
  173                   x - BUTTON_DEPTH, y - BUTTON_DEPTH, BUTTON_DEPTH, h + BUTTON_DEPTH);
  174           /* erase top bit of previous not pressed button */
  175           XFillRectangle(dpy, win, bg,
  176                                             x, y-BUTTON_DEPTH, w, BUTTON_DEPTH);
  177      }
  178 }
  179 
  180 /**
  181  * Draw a rectangle, if pressed is false, give the rectangle shadow to give
  182  * impress of a raised button
  183  *
  184  * @param[in] dpy The display
  185  * @param[in] win The window
  186  * @param[in] x X-coordinate
  187  * @param[in] y Y-coordinate
  188  * @param[in] w width
  189  * @param[in] h height
  190  * @param[in] d depth
  191  * @param[in] colour The colour of the button
  192  * @param[in] shadow The colour of the shadow
  193  * @param[in] bg The background colour
  194  * @param[in] pressed If the button is pressed
  195  */
  196 static void draw3dRectangle(Display * dpy, Window win,
  197                   int x, int y, unsigned w, unsigned h,
  198                             GC colour, GC shadow, GC bg, int d, int pressed)
  199 {
  200      if(pressed)
  201      {
  202           XFillRectangle(dpy, win, colour, x, y, w, h);
  203           /* erase left bit of previous not pressed button */
  204           XFillRectangle(dpy, win, bg, x-d, y-d, d, h+d);
  205           /* erase top bit of previous not pressed button */
  206           XFillRectangle(dpy, win, bg, x, y-d, w, d);
  207      }
  208      else
  209      {
  210           int i;
  211 
  212           XFillRectangle(dpy, win, colour, x-d, y-d, w, h);
  213 
  214           /* Draw shadow */
  215           for(i=d; i > 0; i--)
  216           {
  217                XDrawLine(dpy, win, shadow, x+w-i, y+1-i, x+w-i, y+h-i);
  218                XDrawLine(dpy, win, shadow, x+1-i, y+h-i, x+w-i, y+h-i);
  219           }
  220      }
  221 }
  222 
  223 /**
  224  * Draw a pause button
  225  *
  226  * .0---1.4---5...
  227  * .|...|.|...|\..
  228  * .|...|.|...|.5.
  229  * .|...|.|...|.|.
  230  * .|...|.|...|.|.
  231  * .|...|.|...|.|.
  232  * .|...|.|...|.|.
  233  * .|...|.|...|.|.
  234  * .|...|.|...|.|.
  235  * .|...|.|...|.|.
  236  * .|...|.|...|.|.
  237  * .|...|.|...|.|.
  238  * .3---2.7---6.|.
  239  * ..\...\.\...\|.
  240  * ...3---2.7---6.
  241  * ...............
  242  *
  243  * @param[in] dpy The display
  244  * @param[in] win The window
  245  * @param[in] base X-coordinate and Y-coordinate
  246  * @param[in] size The size of the button
  247  * @param[in] colour The colour of the button
  248  * @param[in] shadow The colour of the shadow
  249  * @param[in] bg The background colour
  250  * @param[in] pressed If the button is pressed
  251  */
  252 void drawPauseButton(Display * dpy, Window win,
  253                                  const XPoint * base, int size,
  254                                        GC colour, GC shadow, GC bg, int pressed)
  255 {
  256      const int len2 = scale(2, size);
  257      const int len3 = scale(3, size);
  258      const int len9 = scale(9, size);
  259      const int len11 = scale(11, size);
  260 
  261      const int y = base->y + len2;
  262      const unsigned w = (unsigned) len3 + 1;
  263      const unsigned h = (unsigned) len11 + 1;
  264 
  265      draw3dRectangle(dpy, win,
  266                      base->x + len3, y, w, h,
  267                      colour, shadow, bg, BUTTON_DEPTH, pressed);
  268 
  269      draw3dRectangle(dpy, win,
  270                      base->x + len9, y, w, h,
  271                      colour, shadow, bg, BUTTON_DEPTH, pressed);
  272 }
  273 
  274 /**
  275  * Draw a stop button
  276  *
  277  * @param[in] dpy The display
  278  * @param[in] win The window
  279  * @param[in] base X-coordinate and Y-coordinate
  280  * @param[in] size The size of the button
  281  * @param[in] colour The colour of the button
  282  * @param[in] shadow The colour of the shadow
  283  * @param[in] bg The background colour
  284  * @param[in] pressed If the button is pressed
  285  */
  286 void drawStopButton(Display * dpy, Window win,
  287                                 const XPoint * base, int size,
  288                                       GC colour, GC shadow, GC bg, int pressed)
  289 {
  290      const int len3 = scale(3, size);
  291      const int len9 = scale(9, size);
  292 
  293      unsigned w = (unsigned) len9;
  294 
  295      draw3dRectangle(dpy, win,
  296                      base->x + len3 + 1, base->y + len3 + 1, w, w,
  297                      colour, shadow, bg, BUTTON_DEPTH, pressed);
  298 }
  299 
  300 /**
  301  * Draw progress bar
  302  */
  303 void drawProgressBar(Display * dpy, Window win,
  304                                 const XPoint * base, int size,
  305                                       GC colour, GC shadow, GC bg, int progress)
  306 {
  307      unsigned width = scale(1, size) + 1;
  308 
  309      static const int coords[4][2] =
  310      {
  311           {0,   0},
  312           {15,  0},
  313           {15, 15},
  314           {0,  15}
  315      };
  316 
  317      int corner = progress % 4;
  318      int x, y;
  319      int i;
  320 
  321      if(progress >= 0)
  322      {
  323           x = scale(coords[corner][0], size);
  324           y = scale(coords[corner][1], size);
  325 
  326           XFillRectangle(dpy, win, colour, x, y, width, width);
  327      }
  328 
  329      for(i = 0; i < 4; i++)
  330      {
  331           if(i == corner)
  332           {
  333                 continue;
  334           }
  335           x = scale(coords[i][0], size);
  336           y = scale(coords[i][1], size);
  337 
  338           XFillRectangle(dpy, win, bg, x, y, width, width);
  339      }
  340 }
  341 
  342 
  343 /**
  344  * Set the Window Class Hint and Name
  345  */
  346 void setWindowClassHint(Display * dpy, Window window, char * name)
  347 {
  348      XClassHint classhint;
  349 
  350      classhint.res_name = name;
  351      classhint.res_class = name;
  352 
  353      XSetClassHint(dpy, window, &classhint);
  354      XStoreName(dpy, window, name);
  355 }
  356 
  357 /**
  358  * Set the Window Hints
  359  */
  360 void setWindowHints(Display * dpy, Window window, int numButtons)
  361 {
  362      XSizeHints wmHints;
  363 
  364      wmHints.x = 0;
  365      wmHints.y = 0;
  366      wmHints.min_width = numButtons * MIN_BUTTON_SIZE;
  367      wmHints.min_height = MIN_BUTTON_SIZE;
  368      wmHints.base_width = numButtons * DEFAULT_BUTTON_SIZE;
  369      wmHints.base_height = DEFAULT_BUTTON_SIZE;
  370      wmHints.flags = PPosition | USPosition | PMinSize;
  371 
  372      XSetWMNormalHints(dpy, window, &wmHints);
  373 }
  374