|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "../../src/capi/application_c.h" |
| 6 | +#include "../../src/capi/message_dialog_c.h" |
| 7 | +#include "../../src/capi/string_utils_c.h" |
| 8 | + |
| 9 | +int main() { |
| 10 | + printf("=== MessageDialog C API Example ===\n\n"); |
| 11 | + |
| 12 | + // Initialize application |
| 13 | + native_application_t app = native_application_get_instance(); |
| 14 | + if (!app) { |
| 15 | + fprintf(stderr, "Failed to get application instance\n"); |
| 16 | + return 1; |
| 17 | + } |
| 18 | + |
| 19 | + printf("Application instance obtained successfully\n\n"); |
| 20 | + |
| 21 | + // Example 1: Create a simple informational dialog |
| 22 | + printf("1. Creating a simple informational dialog...\n"); |
| 23 | + native_message_dialog_t info_dialog = native_message_dialog_create( |
| 24 | + "Information", |
| 25 | + "This is an informational message dialog.\n\n" |
| 26 | + "MessageDialog can be used to display various types of messages to users."); |
| 27 | + |
| 28 | + if (!info_dialog) { |
| 29 | + fprintf(stderr, "Failed to create informational dialog\n"); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + // Get and print dialog properties |
| 34 | + char* title = native_message_dialog_get_title(info_dialog); |
| 35 | + char* message = native_message_dialog_get_message(info_dialog); |
| 36 | + if (title) { |
| 37 | + printf("Dialog title: %s\n", title); |
| 38 | + free_c_str(title); |
| 39 | + } |
| 40 | + if (message) { |
| 41 | + printf("Dialog message: %s\n", message); |
| 42 | + free_c_str(message); |
| 43 | + } |
| 44 | + |
| 45 | + // Set modality to Application modal |
| 46 | + native_message_dialog_set_modality(info_dialog, NATIVE_DIALOG_MODALITY_APPLICATION); |
| 47 | + native_dialog_modality_t modality = native_message_dialog_get_modality(info_dialog); |
| 48 | + printf("Modality: %s\n", |
| 49 | + modality == NATIVE_DIALOG_MODALITY_APPLICATION ? "Application" : "Unknown"); |
| 50 | + |
| 51 | + printf("Opening dialog...\n"); |
| 52 | + if (native_message_dialog_open(info_dialog)) { |
| 53 | + printf("Dialog was opened successfully\n\n"); |
| 54 | + } else { |
| 55 | + printf("Failed to open dialog\n\n"); |
| 56 | + } |
| 57 | + |
| 58 | + // Clean up first dialog |
| 59 | + native_message_dialog_destroy(info_dialog); |
| 60 | + |
| 61 | + // Example 2: Create a warning dialog |
| 62 | + printf("2. Creating a warning dialog...\n"); |
| 63 | + native_message_dialog_t warning_dialog = native_message_dialog_create( |
| 64 | + "Warning", |
| 65 | + "This is a warning message.\n\n" |
| 66 | + "Warning dialogs are used to alert users about potential issues."); |
| 67 | + |
| 68 | + if (warning_dialog) { |
| 69 | + native_message_dialog_set_modality(warning_dialog, NATIVE_DIALOG_MODALITY_APPLICATION); |
| 70 | + native_message_dialog_open(warning_dialog); |
| 71 | + native_message_dialog_destroy(warning_dialog); |
| 72 | + } |
| 73 | + printf("\n"); |
| 74 | + |
| 75 | + // Example 3: Create a dialog with updated content |
| 76 | + printf("3. Creating a dialog with updated content...\n"); |
| 77 | + native_message_dialog_t dynamic_dialog = |
| 78 | + native_message_dialog_create("Update Available", "Initial message"); |
| 79 | + |
| 80 | + if (dynamic_dialog) { |
| 81 | + // Update the title and message before opening |
| 82 | + native_message_dialog_set_title(dynamic_dialog, "System Update"); |
| 83 | + native_message_dialog_set_message(dynamic_dialog, |
| 84 | + "A new version of the application is available.\n\n" |
| 85 | + "Version 2.0 includes:\n" |
| 86 | + "• Improved performance\n" |
| 87 | + "• New features\n" |
| 88 | + "• Bug fixes\n\n" |
| 89 | + "Would you like to update now?"); |
| 90 | + |
| 91 | + title = native_message_dialog_get_title(dynamic_dialog); |
| 92 | + if (title) { |
| 93 | + printf("Updated title: %s\n", title); |
| 94 | + free_c_str(title); |
| 95 | + } |
| 96 | + |
| 97 | + native_message_dialog_set_modality(dynamic_dialog, NATIVE_DIALOG_MODALITY_APPLICATION); |
| 98 | + printf("Opening updated dialog...\n"); |
| 99 | + native_message_dialog_open(dynamic_dialog); |
| 100 | + native_message_dialog_destroy(dynamic_dialog); |
| 101 | + } |
| 102 | + printf("\n"); |
| 103 | + |
| 104 | + // Example 4: Demonstrate different modality types |
| 105 | + printf("4. Demonstrating different modality types...\n"); |
| 106 | + |
| 107 | + // Non-modal dialog |
| 108 | + native_message_dialog_t non_modal_dialog = native_message_dialog_create( |
| 109 | + "Non-Modal Dialog", |
| 110 | + "This dialog does not block interaction.\n\n" |
| 111 | + "The application continues running and users can interact with other windows."); |
| 112 | + if (non_modal_dialog) { |
| 113 | + native_message_dialog_set_modality(non_modal_dialog, NATIVE_DIALOG_MODALITY_NONE); |
| 114 | + modality = native_message_dialog_get_modality(non_modal_dialog); |
| 115 | + printf("Modality: %s (non-modal)\n", |
| 116 | + modality == NATIVE_DIALOG_MODALITY_NONE ? "None" : "Unknown"); |
| 117 | + native_message_dialog_open(non_modal_dialog); |
| 118 | + native_message_dialog_destroy(non_modal_dialog); |
| 119 | + } |
| 120 | + |
| 121 | + // Application modal dialog |
| 122 | + native_message_dialog_t app_modal_dialog = native_message_dialog_create( |
| 123 | + "Application Modal", |
| 124 | + "This dialog blocks interaction with all windows in the current application."); |
| 125 | + if (app_modal_dialog) { |
| 126 | + native_message_dialog_set_modality(app_modal_dialog, NATIVE_DIALOG_MODALITY_APPLICATION); |
| 127 | + modality = native_message_dialog_get_modality(app_modal_dialog); |
| 128 | + printf("Modality: %s\n", |
| 129 | + modality == NATIVE_DIALOG_MODALITY_APPLICATION ? "Application" : "Unknown"); |
| 130 | + native_message_dialog_open(app_modal_dialog); |
| 131 | + native_message_dialog_destroy(app_modal_dialog); |
| 132 | + } |
| 133 | + |
| 134 | + // Window modal dialog (behaves as Application on macOS) |
| 135 | + native_message_dialog_t window_modal_dialog = native_message_dialog_create( |
| 136 | + "Window Modal", |
| 137 | + "This dialog blocks interaction with a specific parent window.\n\n" |
| 138 | + "Note: On macOS, this behaves as Application."); |
| 139 | + if (window_modal_dialog) { |
| 140 | + native_message_dialog_set_modality(window_modal_dialog, NATIVE_DIALOG_MODALITY_WINDOW); |
| 141 | + modality = native_message_dialog_get_modality(window_modal_dialog); |
| 142 | + printf("Modality: %s\n", modality == NATIVE_DIALOG_MODALITY_WINDOW ? "Window" : "Unknown"); |
| 143 | + native_message_dialog_open(window_modal_dialog); |
| 144 | + native_message_dialog_destroy(window_modal_dialog); |
| 145 | + } |
| 146 | + |
| 147 | + printf("\n=== MessageDialog C API Example Complete ===\n"); |
| 148 | + printf("This example demonstrated:\n"); |
| 149 | + printf("1. Creating MessageDialog instances\n"); |
| 150 | + printf("2. Setting and getting title and message\n"); |
| 151 | + printf("3. Updating dialog content programmatically\n"); |
| 152 | + printf("4. Opening dialogs with different modality types (None, Application, Window)\n"); |
| 153 | + printf("5. Modal vs non-modal dialog behavior\n"); |
| 154 | + printf("6. Proper cleanup of dialog resources\n"); |
| 155 | + |
| 156 | + printf("\nExiting MessageDialog C API Example...\n"); |
| 157 | + return 0; |
| 158 | +} |
0 commit comments