fixed window layout
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 11 Jan 2020 22:22:43 +0000 (00:22 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 11 Jan 2020 22:22:43 +0000 (00:22 +0200)
src/main.c
src/widgets.c

index 3717955..c2dce3b 100644 (file)
@@ -9,6 +9,9 @@
 
 #define IMG_FNAME      "disk.img"
 
 
 #define IMG_FNAME      "disk.img"
 
+#define WIN_XPAD       10
+#define WIN_YPAD       15
+
 static int instimg(const char *devpath);
 static void update_disks(void);
 static void onclick(struct wgt_widget *w);
 static int instimg(const char *devpath);
 static void update_disks(void);
 static void onclick(struct wgt_widget *w);
@@ -30,11 +33,11 @@ static const char *err_noimg_fmt =
        "%s\n"
        "\n"
        "Please run this installer from the 256boss distribution directory";
        "%s\n"
        "\n"
        "Please run this installer from the 256boss distribution directory";
-
+static const char *fail_err_str;
 
 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int showcmd)
 {
 
 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int showcmd)
 {
-       int x, y;
+       int x, y, width, height, max_width;
        MSG msg;
        char *msgbuf, *errmsg;
 
        MSG msg;
        char *msgbuf, *errmsg;
 
@@ -52,19 +55,28 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int show
                fclose(imgfile);
                return 1;
        }
                fclose(imgfile);
                return 1;
        }
-       lb_instto = wgt_label(win, "Install to device:", 10, 20);
+
+       max_width = 0;
+       y = WIN_YPAD;
+       lb_instto = wgt_label(win, "Install to device:", WIN_XPAD, WIN_YPAD);
 
        x = wgt_xpos_after(lb_instto, WGT_AUTO);
 
        x = wgt_xpos_after(lb_instto, WGT_AUTO);
-       cb_devs = wgt_combo(win, items, num_rawdev, 0, x, 20, 270, WGT_AUTO, onmodify);
+       cb_devs = wgt_combo(win, items, num_rawdev, 0, x, WIN_YPAD, 270, WGT_AUTO, onmodify);
+       if((width = wgt_xpos_after(cb_devs, WIN_XPAD)) > max_width) max_width = width;
 
        y = wgt_ypos_after(cb_devs, 16);
        ck_usbonly = wgt_checkbox(win, "only show USB devices", 1, x, y,
                        WGT_AUTO, WGT_AUTO, ck_usbonly_handler);
 
        y = wgt_ypos_after(cb_devs, 16);
        ck_usbonly = wgt_checkbox(win, "only show USB devices", 1, x, y,
                        WGT_AUTO, WGT_AUTO, ck_usbonly_handler);
+       if((width = wgt_xpos_after(ck_usbonly, WIN_XPAD)) > max_width) max_width = width;
 
        y = wgt_ypos_after(ck_usbonly, 32);
        bn_inst = wgt_button(win, "Install", 70, y, 100, WGT_AUTO, onclick);
        bn_cancel = wgt_button(win, "Cancel", 230, y, 100, WGT_AUTO, onclick);
 
 
        y = wgt_ypos_after(ck_usbonly, 32);
        bn_inst = wgt_button(win, "Install", 70, y, 100, WGT_AUTO, onclick);
        bn_cancel = wgt_button(win, "Cancel", 230, y, 100, WGT_AUTO, onclick);
 
+       height = wgt_ypos_after(bn_inst, WIN_YPAD);
+
+       wgt_resize_window(win, max_width, height);
+
        update_disks();
 
        while(GetMessage(&msg, 0, 0, 0)) {
        update_disks();
 
        while(GetMessage(&msg, 0, 0, 0)) {
@@ -77,8 +89,6 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int show
        return 0;
 }
 
        return 0;
 }
 
-#define WIN_XPAD       10
-#define WIN_YPAD       10
 #define MIN_BS         (4 * 1024 * 1024)
 
 static int instimg(const char *devpath)
 #define MIN_BS         (4 * 1024 * 1024)
 
 static int instimg(const char *devpath)
@@ -95,14 +105,17 @@ static int instimg(const char *devpath)
        long num_blk_done, total_blk;
        int progr;
 
        long num_blk_done, total_blk;
        int progr;
 
-       if(!(hdev = CreateFile(devpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
-                       0, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0))) {
+       if((hdev = CreateFile(devpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
+                       0, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0)) == INVALID_HANDLE_VALUE) {
+               DWORD err = GetLastError();
+               FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0,
+                               &fail_err_str, 0, 0);
                return -1;
        }
 
                return -1;
        }
 
-       if(rawdisk_eject(hdev) == 0) {
+       /*if(rawdisk_eject(hdev) == 0) {
                rawdisk_load(hdev);
                rawdisk_load(hdev);
-       }
+       }*/
 
        if(DeviceIoControl(hdev, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, &geom, sizeof geom, &wrcount, 0)) {
                blksz = geom.BytesPerSector;
 
        if(DeviceIoControl(hdev, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, &geom, sizeof geom, &wrcount, 0)) {
                blksz = geom.BytesPerSector;
@@ -115,6 +128,7 @@ static int instimg(const char *devpath)
 
        if(!(blkbuf = malloc(blksz))) {
                CloseHandle(hdev);
 
        if(!(blkbuf = malloc(blksz))) {
                CloseHandle(hdev);
+               fail_err_str = "failed to allocate block buffer";
                return -1;
        }
 
                return -1;
        }
 
@@ -152,8 +166,6 @@ static int instimg(const char *devpath)
 
        height = wgt_ypos_after(bn_cancel_inst, WIN_YPAD);
 
 
        height = wgt_ypos_after(bn_cancel_inst, WIN_YPAD);
 
-       max_width += GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
-       height += GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
        wgt_resize_window(win_inst, max_width, height);
 
        /* go through any pending messages once first, to allow the window to get drawn correctly */
        wgt_resize_window(win_inst, max_width, height);
 
        /* go through any pending messages once first, to allow the window to get drawn correctly */
@@ -171,7 +183,12 @@ static int instimg(const char *devpath)
                        memset(blkbuf + sz, 0, blksz - sz);
                }
                SetFilePointer(hdev, num_blk_done * blksz, 0, FILE_BEGIN);
                        memset(blkbuf + sz, 0, blksz - sz);
                }
                SetFilePointer(hdev, num_blk_done * blksz, 0, FILE_BEGIN);
-               WriteFile(hdev, blkbuf, blksz, &wrcount, 0);
+               if(!WriteFile(hdev, blkbuf, blksz, &wrcount, 0)) {
+                       DWORD err = GetLastError();
+                       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0,
+                                       &fail_err_str, 0, 0);
+                       goto end;
+               }
 
                num_blk_done++;
                progr = num_blk_done * 100 / total_blk;
 
                num_blk_done++;
                progr = num_blk_done * 100 / total_blk;
@@ -275,6 +292,10 @@ static const char *success_str = \
        "If the 256boss partition doesn't show up immediately,\n"
        "please eject and re-attach the device\n";
 
        "If the 256boss partition doesn't show up immediately,\n"
        "please eject and re-attach the device\n";
 
+static const char *fail_fmt = \
+       "Installation failed!\n"
+       "Error: %s\n";
+
 static void onclick(struct wgt_widget *w)
 {
        int sel, len;
 static void onclick(struct wgt_widget *w)
 {
        int sel, len;
@@ -298,7 +319,13 @@ static void onclick(struct wgt_widget *w)
                if(MessageBox(0, msgbuf, "Proceed with installation?", flags) == IDYES) {
                        inst_running = 1;
                        if(instimg(rawdev[sel].path) == -1) {
                if(MessageBox(0, msgbuf, "Proceed with installation?", flags) == IDYES) {
                        inst_running = 1;
                        if(instimg(rawdev[sel].path) == -1) {
-                               MessageBox(0, "Installation failed", 0, MB_OK | MB_ICONSTOP | MB_APPLMODAL | MB_SETFOREGROUND);
+                               int prev_len = len;
+                               len = strlen(fail_fmt) + (fail_err_str ? strlen(fail_err_str) : strlen("unknown"));
+                               if(len > prev_len) {
+                                       alloca(len - prev_len);
+                               }
+                               sprintf(msgbuf, fail_fmt, fail_err_str ? fail_err_str : "unknown");
+                               MessageBox(0, msgbuf, 0, MB_OK | MB_ICONSTOP | MB_APPLMODAL | MB_SETFOREGROUND);
                        } else {
                                MessageBox(0, success_str, "Done", MB_OK | MB_ICONASTERISK | MB_APPLMODAL | MB_SETFOREGROUND);
                        }
                        } else {
                                MessageBox(0, success_str, "Done", MB_OK | MB_ICONASTERISK | MB_APPLMODAL | MB_SETFOREGROUND);
                        }
index 1b97637..9ae4c1e 100644 (file)
@@ -6,6 +6,8 @@
 #include <commctrl.h>
 #include "widgets.h"
 
 #include <commctrl.h>
 #include "widgets.h"
 
+#define WIN_STYLE      WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME
+
 struct wgt_window {
        HWND handle;
        struct wgt_rect rect;
 struct wgt_window {
        HWND handle;
        struct wgt_rect rect;
@@ -98,8 +100,14 @@ struct wgt_window *wgt_window(const char *title, int width, int height)
                return 0;
        }
 
                return 0;
        }
 
-       if(!(win->handle = CreateWindow(win->cname, title, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME,
-                       CW_USEDEFAULT, CW_USEDEFAULT, width, height, 0, 0, hinst, 0))) {
+       rect.left = rect.top = 0;
+       rect.right = width;
+       rect.bottom = height;
+       AdjustWindowRect(&rect, WIN_STYLE, FALSE);
+
+       if(!(win->handle = CreateWindow(win->cname, title, WIN_STYLE, CW_USEDEFAULT,
+                       CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top,
+                       0, 0, hinst, 0))) {
                fprintf(stderr, "wgt_create_window: failed to create window\n");
                UnregisterClass(title, hinst);
                return 0;
                fprintf(stderr, "wgt_create_window: failed to create window\n");
                UnregisterClass(title, hinst);
                return 0;
@@ -154,9 +162,20 @@ void wgt_destroy_widget(struct wgt_widget *w)
 
 void wgt_resize_window(struct wgt_window *win, int width, int height)
 {
 
 void wgt_resize_window(struct wgt_window *win, int width, int height)
 {
-       MoveWindow(win->handle, win->rect.x, win->rect.y, width, height, TRUE);
+       RECT rect;
+
        win->rect.width = width;
        win->rect.height = height;
        win->rect.width = width;
        win->rect.height = height;
+
+       rect.left = win->rect.x;
+       rect.top = win->rect.y;
+       rect.right = rect.left + width;
+       rect.bottom = rect.top + height;
+       if(AdjustWindowRect(&rect, WIN_STYLE, FALSE)) {
+               width = rect.right - rect.left;
+               height = rect.bottom - rect.top;
+       }
+       MoveWindow(win->handle, win->rect.x, win->rect.y, width, height, TRUE);
 }
 
 void wgt_quit_on_close(struct wgt_window *win, int quit)
 }
 
 void wgt_quit_on_close(struct wgt_window *win, int quit)