diff --git a/examples/basic/colorpicker.c b/examples/basic/colorpicker.c index fb92fda..dbce7fe 100644 --- a/examples/basic/colorpicker.c +++ b/examples/basic/colorpicker.c @@ -1,10 +1,10 @@ #include -MwWidget cpicker; +MwWidget fpicker; MwWidget window; MwWidget button; -void MWAPI color_callback(MwWidget handle, void* user_data, void* call_data) { +void MWAPI file_callback(MwWidget handle, void* user_data, void* call_data) { char hexColor[8]; MwRGB* rgb = call_data; @@ -18,14 +18,14 @@ void MWAPI color_callback(MwWidget handle, void* user_data, void* call_data) { MwSetText(window, MwNbackground, hexColor); } -void MWAPI color_picker(MwWidget handle, void* user_data, void* call_data) { +void MWAPI file_picker(MwWidget handle, void* user_data, void* call_data) { MwWidget cpicker = MwColorPicker(window, "cpicker"); (void)handle; (void)user_data; (void)call_data; - MwAddUserHandler(cpicker, MwNcolorChosenHandler, color_callback, NULL); + MwAddUserHandler(cpicker, MwNcolorChosenHandler, file_callback, NULL); MwSetText(cpicker, MwNbackground, MwGetInteger(cpicker, MwNdarkTheme) ? MwDefaultDarkBackground : MwDefaultBackground); } @@ -42,7 +42,7 @@ int main() { NULL); MwSetText(button, MwNbackground, MwGetInteger(window, MwNdarkTheme) ? MwDefaultBackground : MwDefaultDarkBackground); - MwAddUserHandler(button, MwNactivateHandler, color_picker, NULL); + MwAddUserHandler(button, MwNactivateHandler, file_picker, NULL); MwLoop(window); } diff --git a/examples/basic/filechooser.c b/examples/basic/filechooser.c new file mode 100644 index 0000000..2637f86 --- /dev/null +++ b/examples/basic/filechooser.c @@ -0,0 +1,70 @@ +#include + +MwWidget fpicker; +MwWidget window; +MwWidget button; +MwWidget mb; + +MwBool fpicker_wait = MwFALSE; +MwBool msgbox_wait = MwFALSE; + +void MWAPI ok(MwWidget handle, void* user, void* call) { + (void)handle; + (void)call; + msgbox_wait = MwFALSE; +} + +void MWAPI file_callback(MwWidget handle, void* user_data, void* call_data) { + mb = MwMessageBox(handle, call_data, "File chosen", MwMB_ICONERROR | MwMB_BUTTONOK); + + (void)user_data; + + MwAddUserHandler(MwMessageBoxGetChild(mb, MwMB_BUTTONOK), MwNactivateHandler, ok, mb); + MwAddUserHandler(mb, MwNcloseHandler, ok, mb); + + msgbox_wait = MwTRUE; + + while(msgbox_wait) { + MwStep(mb); + } + + fpicker_wait = MwFALSE; +} + +void MWAPI file_picker(MwWidget handle, void* user_data, void* call_data) { + fpicker = MwFileChooserEx(handle, "file chooser", 0); + + (void)handle; + (void)user_data; + (void)call_data; + + MwAddUserHandler(fpicker, MwNfileChosenHandler, file_callback, NULL); + + MwSetText(fpicker, MwNbackground, MwGetInteger(fpicker, MwNdarkTheme) ? MwDefaultDarkBackground : MwDefaultBackground); + + fpicker_wait = MwTRUE; + + while(fpicker_wait) { + MwStep(fpicker); + } + + MwDestroyWidget(fpicker); + MwDestroyWidget(mb); +} + +int main() { + MwLibraryInit(); + + window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, + MwDEFAULT, 640, 480, MwNtitle, "color picker", NULL); + MwSetText(window, MwNbackground, MwGetInteger(window, MwNdarkTheme) ? MwDefaultBackground : MwDefaultDarkBackground); + + button = MwVaCreateWidget(MwButtonClass, "button", window, 160, 180, 320, 120, + MwNtext, "select file", + NULL); + MwSetText(button, MwNbackground, MwGetInteger(window, MwNdarkTheme) ? MwDefaultBackground : MwDefaultDarkBackground); + + MwAddUserHandler(button, MwNactivateHandler, file_picker, NULL); + + MwLoop(window); +} diff --git a/pl/rules.pl b/pl/rules.pl index 68386b4..a725c6c 100644 --- a/pl/rules.pl +++ b/pl/rules.pl @@ -191,6 +191,7 @@ new_example("examples/basic/box"); new_example("examples/basic/clipboard"); new_example("examples/basic/sevensegment"); new_example("examples/basic/calculator"); +new_example("examples/basic/filechooser"); new_example("examples/basic/periodic"); if (param_get("opengl")) { diff --git a/src/backend/wayland.c b/src/backend/wayland.c index aad034f..f554623 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -2498,6 +2498,7 @@ static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) { struct xdg_toplevel_icon_v1* icon = xdg_toplevel_icon_manager_v1_create_icon(icon_manager); MwU64 size = pixmap->common.width * pixmap->common.height * 4; int i = 0; + int line = (pixmap->common.width < pixmap->common.height) ? pixmap->common.height : pixmap->common.width; if(handle->wayland.icon == NULL) { handle->wayland.icon = malloc(sizeof(struct _MwLLWaylandShmBuffer)); @@ -2511,7 +2512,7 @@ static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) { } handle->wayland.icon->surface = wl_compositor_create_surface(handle->wayland.compositor); - buffer_setup(handle->wayland.icon, pixmap->common.width, pixmap->common.height); + buffer_setup(handle->wayland.icon, line, line); wl_surface_attach(handle->wayland.icon->surface, handle->wayland.icon->shm_buffer, 0, 0); wl_surface_commit(handle->wayland.icon->surface); diff --git a/src/dialog/filechooser.c b/src/dialog/filechooser.c index 0c4d1b8..138fc6c 100644 --- a/src/dialog/filechooser.c +++ b/src/dialog/filechooser.c @@ -504,32 +504,12 @@ static void scan(MwWidget handle, const char* path, int record) { } } -MwWidget MwFileChooserEx(MwWidget handle, const char* title, int dir) { - MwWidget window; - int w, h; - filechooser_t* fc = malloc(sizeof(*fc)); +static void setup_fallback_filechooser(MwWidget window, int dir) { char* path; MwLLPixmap icon; - int wx; - int wy; - + filechooser_t* fc = malloc(sizeof(*fc)); memset(fc, 0, sizeof(*fc)); - w = 700; - h = w * 2 / 3; - - if(handle == NULL) { - wx = wy = MwDEFAULT; - } else { - wx = MwGetInteger(handle, MwNx) + (MwGetInteger(handle, MwNwidth) - w) / 2; - wy = MwGetInteger(handle, MwNy) + (MwGetInteger(handle, MwNheight) - h) / 2; - } - - window = MwVaCreateWidget(MwWindowClass, "filechooser", NULL, wx, wy, w, h, - MwNtitle, title, - NULL); - if(handle != NULL) MwReparent(window, handle); - fc->history_seek = 0; fc->dir = MwLoadIcon(window, MwIconDirectory); @@ -558,8 +538,32 @@ MwWidget MwFileChooserEx(MwWidget handle, const char* title, int dir) { free(path); MwLLBeginStateChange(window->lowlevel); - MwLLMakePopup(window->lowlevel, handle == NULL ? NULL : handle->lowlevel); + MwLLMakePopup(window->lowlevel, window == NULL ? NULL : window->lowlevel); MwLLEndStateChange(window->lowlevel); +} + +MwWidget MwFileChooserEx(MwWidget handle, const char* title, int dir) { + MwWidget window; + int w, h; + int wx; + int wy; + + w = 700; + h = w * 2 / 3; + + if(handle == NULL) { + wx = wy = MwDEFAULT; + } else { + wx = MwGetInteger(handle, MwNx) + (MwGetInteger(handle, MwNwidth) - w) / 2; + wy = MwGetInteger(handle, MwNy) + (MwGetInteger(handle, MwNheight) - h) / 2; + } + + window = MwVaCreateWidget(MwWindowClass, "filechooser", NULL, wx, wy, w, h, + MwNtitle, title, + NULL); + if(handle != NULL) MwReparent(window, handle); + + setup_fallback_filechooser(window, dir); return window; }