Browse Source

resize the window to fit the subtitle

Olivier Marty 9 years ago
parent
commit
af582b6e2c
3 changed files with 36 additions and 39 deletions
  1. 6 14
      main.c
  2. 25 22
      printer.c
  3. 5 3
      printer.h

+ 6 - 14
main.c

@@ -29,8 +29,6 @@ void displayUsage(char *name)
   printf("Options :\n");
   printf("  -s sec\t: skip the first x seconds\n");
   printf("  -d sec\t: wait x seconds before starting (default : 5)\n");
-  printf("  -w px\t\t: width of the window\n");
-  printf("  -H px\t\t: height of the window\n");
   printf("  -m px\t\t: margin with the bottom of the screen\n");
   printf("  -f fontname\t: name of the font to use\n");
   printf("  -i fontname\t: name of the italic font to use\n");
@@ -41,13 +39,13 @@ void displayUsage(char *name)
 
 int main(int argc, char **argv)
 {
-  int i, shift = 0, delay = 5, width = -1, height = 240, margin_bottom = 50;
+  int i, shift = 0, delay = 5, margin_bottom = 50;
   char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
   FILE *f = NULL;
   
   // parse arguments
   int c;
-  while((c = getopt (argc, argv, "s:d:w:H:m:f:i:b:j:h")) != -1)
+  while((c = getopt (argc, argv, "s:d:m:f:i:b:j:h")) != -1)
     switch(c)
     {
       case 's':
@@ -56,12 +54,6 @@ int main(int argc, char **argv)
       case 'd':
         delay = atoi(optarg);
         break;
-      case 'w':
-        width = atoi(optarg);
-        break;
-      case 'H':
-        height = atoi(optarg);
-        break;
       case 'm':
         margin_bottom = atoi(optarg);
         break;
@@ -104,8 +96,8 @@ int main(int argc, char **argv)
   }
   
   // open the window
-  struct printerEnv penv = printerOpenWindow(width, height, margin_bottom,
-    font, font_i, font_b, font_bi);
+  struct printerEnv penv = printerOpenWindow(margin_bottom, font, font_i,
+    font_b, font_bi);
   
   // show a counter before start the clock
   for(i = delay; i > 0; i--)
@@ -113,7 +105,7 @@ int main(int argc, char **argv)
     char t[16];
     sprintf(t, "%d...\n", i);
     printf("%s", t);
-    printerShow(penv, t, T_ITALIC);
+    printerShow(&penv, t, T_ITALIC);
     sleep(1);
   }
   printf("0 !\n");
@@ -130,7 +122,7 @@ int main(int argc, char **argv)
       printf("%ds\n", sline.begin.tv_sec);
       // show
       printf("%s\n", sline.text);
-      printerShow(penv, sline.text, 0);
+      printerShow(&penv, sline.text, 0);
       
       // hide
       timeSleepUntil(sline.end);

+ 25 - 22
printer.c

@@ -61,8 +61,8 @@ void loadFont(struct printerEnv *env, XFontStruct **font, char* fontname)
   }
 }
 
-struct printerEnv printerOpenWindow(int width, int height, int margin_bottom,
-  char *font, char *font_i, char *font_b, char *font_bi)
+struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
+  char *font_b, char *font_bi)
 {
   struct printerEnv env;
 
@@ -99,12 +99,13 @@ struct printerEnv printerOpenWindow(int width, int height, int margin_bottom,
   //env.color_background = a*256*256*256 + r*256*256 + g*256 + b;
   
   // create the window
-  env.width = (width < 0) ? RootAttr.width : width;
-  env.height = height;
-  env.w = XCreateWindow(env.d, RootWindow(env.d, env.s),
-    (RootAttr.width - env.width)/2,
-    RootAttr.height - env.height - margin_bottom, env.width, env.height, 0,
-    vinfo.depth, InputOutput, vinfo.visual,
+  env.margin_bottom = margin_bottom;
+  env.root_width = RootAttr.width;
+  env.root_height = RootAttr.height;
+  env.width = 1;
+  env.height = 1;
+  env.w = XCreateWindow(env.d, RootWindow(env.d, env.s), 0, 0, env.width,
+    env.height, 0, vinfo.depth, InputOutput, vinfo.visual,
     CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, &attr);
   
   // setting fonts
@@ -312,11 +313,10 @@ int printLines(struct printerEnv env, char *text, int gap, int y,
   return width;
 }
 
-void printerShow(struct printerEnv env, char* text, enum t_type font)
+void printerShow(struct printerEnv *env, char* text, enum t_type font)
 {
   int gap = 5, padding = 5;
-  int nlines = 1,
-      width, height;
+  int nlines = 1;
   char *p = text;
   while((p = strchr(p, '\n')) != NULL)
   {
@@ -325,22 +325,25 @@ void printerShow(struct printerEnv env, char* text, enum t_type font)
       break;
     nlines++;
   }
+  //  width and height
+  env->width = printLines(*env, text, gap, 0, font, 0) + 2*padding;
+  env->height = nlines * (env->maxascent + env->maxdescent + gap) - gap +
+    2*padding;
   
-  XClearWindow(env.d, env.w);
-  //  max width
-  width = printLines(env, text, gap, 0, font, 0);
-  height = nlines * (env.maxascent + env.maxdescent + gap) - gap;
+  XClearWindow(env->d, env->w);
+  
+  XMoveResizeWindow(env->d, env->w, (env->root_width - env->width)/2,
+    env->root_height - env->margin_bottom - env->height, env->width,
+    env->height);
   
   // frame
-  XSetForeground(env.d, env.gc, env.color_background);
-  XFillRectangle(env.d, env.w, env.gc, (env.width - width)/2 - padding,
-    env.height - 2*padding - height, width + 2*padding, height + 2*padding);
-  XSetForeground(env.d, env.gc, env.color_text);
+  XSetForeground(env->d, env->gc, env->color_background);
+  XFillRectangle(env->d, env->w, env->gc, 0, 0, env->width, env->height);
+  XSetForeground(env->d, env->gc, env->color_text);
   
   // text
-  printLines(env, text, gap,
-    env.height - padding - height + env.maxascent, font, 1);
-  XFlush(env.d);
+  printLines(*env, text, gap, padding + env->maxascent, font, 1);
+  XFlush(env->d);
 }
 
 void printerClean(struct printerEnv env)

+ 5 - 3
printer.h

@@ -33,17 +33,19 @@ struct printerEnv
   XFontStruct *fontinfo_bi; // both
   int maxascent, maxdescent;
   int width, height;
+  int root_width, root_height;
+  int margin_bottom;
   unsigned long color_background, color_text;
 };
 
 enum t_type {T_ITALIC = 1, T_BOLD = 2};
 
 // if width < 0 the window will be as larger as possible
-struct printerEnv printerOpenWindow(int width, int height, int margin_bottom,
-  char *font, char *font_i, char *font_b, char *font_bi);
+struct printerEnv printerOpenWindow(int margin_bottom, char *font, char *font_i,
+  char *font_b, char *font_bi);
 void printerCloseWindow(struct printerEnv env);
 
-void printerShow(struct printerEnv env, char* text, enum t_type font);
+void printerShow(struct printerEnv *env, char* text, enum t_type font);
 void printerClean(struct printerEnv env);
 
 #endif