差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
fltk:example-app-countdown_timer [2025/05/27 17:15] – [スクリーンショット] Upload anmated gif freemikanfltk:example-app-countdown_timer [2025/06/12 11:45] (現在) – [スクリーンショット] Remove imagebox freemikan
行 1: 行 1:
-====== サンプルアプリケーション: カウントダウンタイマー ====== +====== : カウントダウンタイマー ====== 
-{{:fltk:fltk_shadow.png?200|FLTK}}+[[fltk:|{{:fltk:fltk_shadow.png?200|}}]]
  
  
 ===== スクリーンショット ===== ===== スクリーンショット =====
- +{{ :fltk:countdown_timer_app.gif |カウントダウンタイマー}}
-{{ :fltk:countdown_timer_app.gif |}}+
 ===== プロジェクトレイアウト ===== ===== プロジェクトレイアウト =====
  
行 44: 行 43:
 #include <FL/Fl_Window.H> #include <FL/Fl_Window.H>
  
-#include <array+#include <format
-#include <cstdio>+#include <string>
  
 struct AppContext { struct AppContext {
   int start = 10;   int start = 10;
   int count = 0;   int count = 0;
-  Fl_Box *countdown_digits;+  Fl_Box *digits;
   Fl_Button *start_button;   Fl_Button *start_button;
   Fl_Button *reset_button;   Fl_Button *reset_button;
-  std::array<char, 100> cbuf;+  std::string sbuf;
 }; };
  
 AppContext *as_app_context(void *ptr) { return static_cast<AppContext *>(ptr); } AppContext *as_app_context(void *ptr) { return static_cast<AppContext *>(ptr); }
  
-void update_countdown_digits_label(AppContext *ctx) { +void update_digits_label(AppContext *ctx) { 
-  snprintf(ctx->cbuf.data(), sizeof(ctx->cbuf), "%d", ctx->count); +  ctx->sbuf.clear()
-  ctx->countdown_digits->label(ctx->cbuf.data());+  std::format_to(std::back_inserter(ctx->sbuf), "{}", ctx->count); 
 +  ctx->digits->label(ctx->sbuf.c_str());
 } }
  
行 67: 行 67:
  
   if (ctx->count <= 0) {   if (ctx->count <= 0) {
-    ctx->start_button->activate();+    // Finished 
 +    ctx->start_button->label("スタート");
     return;     return;
   }   }
行 73: 行 74:
   --ctx->count;   --ctx->count;
  
-  update_countdown_digits_label(ctx);+  update_digits_label(ctx);
  
   Fl::repeat_timeout(1.0, timeout_cb, ctx);   Fl::repeat_timeout(1.0, timeout_cb, ctx);
行 79: 行 80:
  
 void start_button_cb(Fl_Widget *w, void *data) { void start_button_cb(Fl_Widget *w, void *data) {
-  Fl::add_timeout(1.0, timeout_cb, data); +  if (!Fl::has_timeout(timeout_cb, data)) { 
-  w->deactivate();+    // Start button pressed 
 +    Fl::add_timeout(1.0, timeout_cb, data); 
 +    w->label("一時停止"); 
 +  } else { 
 +    // Pause button pressed 
 +    Fl::remove_timeout(timeout_cb, data); 
 +    w->label("スタート"); 
 +  }
 } }
  
行 86: 行 94:
   auto ctx = as_app_context(data);   auto ctx = as_app_context(data);
   ctx->count = ctx->start;   ctx->count = ctx->start;
-  update_countdown_digits_label(ctx);+  update_digits_label(ctx);
 } }
  
 int main() { int main() {
-  auto window = new Fl_Window(600600, "Timer foo");+  auto window = new Fl_Window(300300, "Countdown Timer");
  
-  auto countdown_digits = new Fl_Box(200200200200); +  auto digits = new Fl_Box(1010280240); 
-  countdown_digits->box(FL_DOWN_BOX); +  digits->box(FL_DOWN_BOX); 
-  countdown_digits->labelfont(FL_BOLD); +  digits->labelfont(FL_BOLD); 
-  countdown_digits->labelsize(80); +  digits->labelsize(80); 
-  countdown_digits->labeltype(FL_SHADOW_LABEL);+  digits->labeltype(FL_SHADOW_LABEL);
  
-  auto start_button = new Fl_Button(21042080, 30, "スタート"); +  auto start_button = new Fl_Button(10260140, 30, "スタート"); 
-  auto reset_button = new Fl_Button(31042080, 30, "リセット");+  auto reset_button = new Fl_Button(150260140, 30, "リセット");
  
   int const countdown_start = 10;   int const countdown_start = 10;
行 106: 行 114:
       .start = countdown_start,       .start = countdown_start,
       .count = countdown_start,       .count = countdown_start,
-      .countdown_digits countdown_digits,+      .digits digits,
       .start_button = start_button,       .start_button = start_button,
       .reset_button = reset_button,       .reset_button = reset_button,
   };   };
  
-  update_countdown_digits_label(&ctx);+  update_digits_label(&ctx);
   start_button->callback(start_button_cb, &ctx);   start_button->callback(start_button_cb, &ctx);
   reset_button->callback(reset_button_cb, &ctx);   reset_button->callback(reset_button_cb, &ctx);
文書の先頭へ