changeset 2414:5e5fab842901

iOS: Implement dw_font_choose() and dw_color_choose().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 02 Apr 2021 00:44:17 +0000
parents 8727ad1a19c3
children f33a81fa29e9
files ios/dw.m
diffstat 1 files changed, 131 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/ios/dw.m	Thu Apr 01 21:15:55 2021 +0000
+++ b/ios/dw.m	Fri Apr 02 00:44:17 2021 +0000
@@ -868,6 +868,61 @@
 -(BOOL)acceptsFirstResponder { return YES; }
 @end
 
+@interface DWFontPickerDelegate : UIResponder <UIFontPickerViewControllerDelegate>
+{
+    DWDialog *dialog;
+}
+-(void)fontPickerViewControllerDidPickFont:(UIFontPickerViewController *)viewController;
+-(void)fontPickerViewControllerDidCancel:(UIFontPickerViewController *)viewController;
+-(void)setDialog:(DWDialog *)newdialog;
+@end
+
+@implementation DWFontPickerDelegate
+-(void)fontPickerViewControllerDidPickFont:(UIFontPickerViewController *)viewController
+{
+    if(viewController.selectedFontDescriptor)
+    {
+        UIFont *font = [UIFont fontWithDescriptor:viewController.selectedFontDescriptor size:9];
+        dw_dialog_dismiss(dialog, font);
+    }
+    else
+        dw_dialog_dismiss(dialog, NULL);
+}
+-(void)fontPickerViewControllerDidCancel:(UIFontPickerViewController *)viewController
+{
+    dw_dialog_dismiss(dialog, NULL);
+}
+-(void)setDialog:(DWDialog *)newdialog { dialog = newdialog; }
+@end
+
+@interface DWColorPickerDelegate : UIResponder <UIColorPickerViewControllerDelegate>
+{
+    DWDialog *dialog;
+}
+-(void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0));
+-(void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0));
+-(void)setDialog:(DWDialog *)newdialog;
+@end
+
+@implementation DWColorPickerDelegate
+-(void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0))
+{
+    if([viewController selectedColor])
+    {
+        CGFloat red, green, blue;
+        [[viewController selectedColor] getRed:&red green:&green blue:&blue alpha:NULL];
+        dw_dialog_dismiss(dialog, DW_UINT_TO_POINTER(DW_RGB((int)(red * 255), (int)(green *255), (int)(blue *255))));
+    }
+    else
+        dw_dialog_dismiss(dialog, NULL);
+}
+-(void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0))
+{
+    dw_dialog_dismiss(dialog, NULL);
+}
+-(void)setDialog:(DWDialog *)newdialog { dialog = newdialog; }
+@end
+
 @implementation DWObject
 -(id)init
 {
@@ -897,6 +952,62 @@
     if(mycallback)
         mycallback(params);
 }
+-(void)colorPicker:(NSMutableArray *)params
+{
+    if (@available(iOS 14.0, *))
+    {
+        DWDialog *dialog = dw_dialog_new(NULL);
+        UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
+        DWColorPickerDelegate *delegate = [[DWColorPickerDelegate alloc] init];
+
+        /* Unhide our hidden window and make it key */
+        [hiddenWindow setHidden:NO];
+        [hiddenWindow makeKeyAndVisible];
+        [delegate setDialog:dialog];
+        [picker setDelegate:delegate];
+        /* Wait for them to pick a color */
+        [[hiddenWindow rootViewController] presentViewController:picker animated:YES completion:nil];
+        [params addObject:[NSNumber numberWithUnsignedLong:DW_POINTER_TO_UINT(dw_dialog_wait(dialog))]];
+        /* Once the dialog is gone we can rehide our window */
+        [hiddenWindow resignKeyWindow];
+        [hiddenWindow setHidden:YES];
+        [picker release];
+        [delegate release];
+    } else {
+        // Fallback on earlier versions
+    };
+}
+
+-(void)fontPicker:(NSPointerArray *)params
+{
+    DWDialog *dialog = dw_dialog_new(NULL);
+    UIFontPickerViewController *picker = [[UIFontPickerViewController alloc] init];
+    DWFontPickerDelegate *delegate = [[DWFontPickerDelegate alloc] init];
+    UIFont *font;
+
+    /* Unhide our hidden window and make it key */
+    [hiddenWindow setHidden:NO];
+    [hiddenWindow makeKeyAndVisible];
+    [delegate setDialog:dialog];
+    [picker setDelegate:delegate];
+    /* Wait for them to pick a color */
+    [[hiddenWindow rootViewController] presentViewController:picker animated:YES completion:nil];
+    font = dw_dialog_wait(dialog);
+    /* Once the dialog is gone we can rehide our window */
+    [hiddenWindow resignKeyWindow];
+    [hiddenWindow setHidden:YES];
+
+    if(font)
+    {
+        NSString *fontname = [font fontName];
+        NSString *output = [NSString stringWithFormat:@"%d.%s", (int)[font pointSize], [fontname UTF8String]];
+        [params addPointer:strdup([output UTF8String])];
+    }
+    else
+        [params addPointer:NULL];
+    [picker release];
+    [delegate release];
+}
 -(void)messageBox:(NSMutableArray *)params
 {
     __block DWDialog *dialog = dw_dialog_new(NULL);
@@ -4714,48 +4825,14 @@
  */
 unsigned long API dw_color_choose(unsigned long value)
 {
-#if 0 /* TODO: Implement this with UIColorPickerViewController */
-    /* Create the Color Chooser Dialog class. */
-    DWColorChoose *colorDlg;
-    DWDialog *dialog;
-    DW_LOCAL_POOL_IN;
-
-    if(![DWColorChoose sharedColorPanelExists])
-    {
-        colorDlg = (DWColorChoose *)[DWColorChoose sharedColorPanel];
-        /* Set defaults for the dialog. */
-        [colorDlg setContinuous:NO];
-        [colorDlg setTarget:colorDlg];
-        [colorDlg setAction:@selector(changeColor:)];
-    }
-    else
-        colorDlg = (DWColorChoose *)[DWColorChoose sharedColorPanel];
-
-    /* If someone is already waiting just return */
-    if([colorDlg dialog])
-    {
-        DW_LOCAL_POOL_OUT;
-        return value;
-    }
-
-    unsigned long tempcol = _dw_get_color(value);
-    UIColor *color = [[UIColor colorWithDeviceRed: DW_RED_VALUE(tempcol)/255.0 green: DW_GREEN_VALUE(tempcol)/255.0 blue: DW_BLUE_VALUE(tempcol)/255.0 alpha: 1] retain];
-    [colorDlg setColor:color];
-
-    dialog = dw_dialog_new(colorDlg);
-    [colorDlg setDialog:dialog];
-    [colorDlg makeKeyAndOrderFront:nil];
-
-    /* Wait for them to pick a color */
-    color = (UIColor *)dw_dialog_wait(dialog);
-
-    /* Figure out the value of what they returned */
-    CGFloat red, green, blue;
-    [color getRed:&red green:&green blue:&blue alpha:NULL];
-    value = DW_RGB((int)(red * 255), (int)(green *255), (int)(blue *255));
-    DW_LOCAL_POOL_OUT;
-#endif
-    return value;
+    NSMutableArray *params = [NSMutableArray arrayWithObject:[NSNumber numberWithUnsignedLong:value]];
+    unsigned long newcolor = value;
+
+    [DWObj safeCall:@selector(colorPicker:) withObject:params];
+    if([params count] > 1)
+        newcolor = [[params lastObject] unsignedLongValue];
+
+    return newcolor;
 }
 
 CGContextRef _dw_draw_context(UIImage *image, bool antialiased)
@@ -6300,7 +6377,7 @@
      * similar behavior.
      */
     DWMDI *mdi = [[[DWMDI alloc] init] retain];
-    /* [mdi setTag:cid]; Why doesn't this work? */
+    [mdi setTag:cid];
     return mdi;
 }
 
@@ -6946,7 +7023,7 @@
     DW_FUNCTION_INIT;
     DWWebView *web = [[[DWWebView alloc] init] retain];
     web.navigationDelegate = web;
-    /* [web setTag:cid]; Why doesn't this work? */
+    [web setTag:cid];
     DW_FUNCTION_RETURN_THIS(web);
 }
 
@@ -7612,6 +7689,7 @@
         DWBox *box = object;
 
         [box setColor:_back];
+        [box setBackgroundColor:bg];
     }
     else if([object isKindOfClass:[UITableView class]])
     {
@@ -7789,20 +7867,15 @@
  */
 char * API dw_font_choose(const char *currfont)
 {
-    __block UIFont *font = nil;
-    UIFontPickerViewController* picker = [[UIFontPickerViewController alloc] init];
-    /*TODO: Add handler for accepting the font fontPickerViewControllerDidPickFont */
-    /* Wait for them to pick a color */
-    [picker presentViewController:picker animated:YES completion:nil];
-    [picker release];
-
-    if(font)
-    {
-        NSString *fontname = [font fontName];
-        NSString *output = [NSString stringWithFormat:@"%d.%s", (int)[font pointSize], [fontname UTF8String]];
-        return strdup([output UTF8String]);
-    }
-    return NULL;
+    NSPointerArray *params = [[NSPointerArray pointerArrayWithOptions:NSPointerFunctionsOpaqueMemory] retain];
+    char *font = NULL;
+
+    [params addPointer:(void *)currfont];
+    [DWObj safeCall:@selector(fontPicker:) withObject:params];
+    if([params count] > 1)
+        font = [params pointerAtIndex:1];
+
+    return font;
 }
 
 /* Internal function to return a pointer to an item struct